-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_old.py
73 lines (62 loc) · 3.03 KB
/
build_old.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import os
import shutil
import subprocess
import sys
def run(*cmd, cwd="."):
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=sys.stderr, cwd=cwd)
output, error = process.communicate()
if process.returncode != 0:
raise Exception("File handling failed %d %s %s" % (process.returncode, output, error))
return output
def get_cargo_version(project):
output = run("cargo", "metadata", "--no-deps", "--format-version", "1", cwd = project)
for item in json.loads(output)["packages"]:
if item["name"] == project:
return item["version"]
return None
def build_bootloader():
run("cargo", "build", "--release", cwd = "lasercan-bootloader")
version = get_cargo_version("lasercan-bootloader")
shutil.copy("lasercan-bootloader/target/thumbv7m-none-eabi/release/lasercan-bootloader", f"target/lasercan-bootloader-{version}.elf")
run("arm-none-eabi-objcopy", "-O", "binary", f"target/lasercan-bootloader-{version}.elf", f"target/lasercan-bootloader-{version}.grplbt")
def build_firmware():
run("cargo", "build", "--release", cwd = "lasercan-firmware")
version = get_cargo_version("lasercan-firmware")
run("arm-none-eabi-objcopy", '--update-section', '.firmware_flag=./firmware_ready_patch.bin', 'lasercan-firmware/target/thumbv7m-none-eabi/release/lasercan-firmware', f"target/lasercan-firmware-update-{version}.elf")
run("arm-none-eabi-objcopy", "-O", "binary", f"target/lasercan-firmware-update-{version}.elf", f"target/lasercan-firmware-update-{version}.grplfw")
# Bootloader 0.1.0 had an issue where if byte at offset 0x1400 was 0xFF, the bootloader wouldn't accept any new firmware because of a failure to erase the sector.
# This has since been fixed, but as a precaution, this will prevent us accidentally generating a firmware version with this property.
compat = True
with open(f"./target/lasercan-firmware-update-{version}.grplfw", 'rb') as f:
data = f.read()
for i in range(0, len(data), 1024):
if data[i] == 0xFF:
print("Bootloader 0.1.0 incompatible file! (index=0x{:x})".format(i), file=sys.stderr)
compat = False
if not compat:
os.remove(f"target/lasercan-firmware-update-{version}.elf")
os.remove(f"target/lasercan-firmware-update-{version}.grplfw")
exit(1)
def flash_bootloader():
build_bootloader()
run("cargo", "flash", "--chip=STM32F103C8", "--elf", "lasercan-bootloader/target/thumbv7m-none-eabi/release/lasercan-bootloader")
def flash_firmware():
# Can't flash what's in target/ because it contains the firmware flag reset.
build_firmware()
run("cargo", "flash", "--chip=STM32F103C8", "--elf", "lasercan-firmware/target/thumbv7m-none-eabi/release/lasercan-firmware")
if len(sys.argv) == 1:
build_bootloader()
build_firmware()
elif sys.argv[1] == "bootloader":
build_bootloader()
elif sys.argv[1] == "firmware":
build_firmware()
elif sys.argv[1] == "flash":
if len(sys.argv) == 2:
flash_bootloader()
flash_firmware()
elif sys.argv[2] == "bootloader":
flash_bootloader()
elif sys.argv[2] == "firmware":
flash_firmware()