Skip to content

Commit a534365

Browse files
committed
repl_installer: get firmware info
1 parent eab48c4 commit a534365

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pybricksdev/resources/install_pybricks.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
FLASH_PYBRICKS_START = 0x80C0000
1717
FLASH_READ_OFFSET = FLASH_LEGO_START
1818

19+
FLASH_SIZE = 0x8000000 + 1024 * 1024 - FLASH_LEGO_START
1920

2021
FF = b'\xFF'
2122

@@ -25,6 +26,11 @@ def read_flash(address, length):
2526
return firmware.flash_read(address - FLASH_READ_OFFSET)[0:length]
2627

2728

29+
def read_flash_int(address):
30+
"""Gets a little endian uint32 integer from the internal flash."""
31+
return int.from_bytes(read_flash(address, 4), 'little')
32+
33+
2834
def get_base_firmware_reset_vector():
2935
"""Gets the boot vector of the original firmware."""
3036

@@ -39,29 +45,37 @@ def get_base_firmware_reset_vector():
3945
return read_flash(FLASH_PYBRICKS_START + 4, 4)
4046

4147

42-
def install(pybricks_firmware_hash):
48+
def install(pybricks_hash):
4349
"""Main installation routine."""
4450

4551
# Start firmware binary verification.
4652
print("Starting installation script.")
4753
print("Checking uploaded firmware file.")
48-
fw_hash = uhashlib.sha256()
49-
fw_size = 0
54+
pybricks_hash_calc = uhashlib.sha256()
55+
pybricks_size = 0
5056

5157
with open("_pybricks/firmware.bin") as fw:
5258
data = b'START'
5359
while len(data) > 0:
5460
data = fw.read(128)
55-
fw_size += len(data)
56-
fw_hash.update(data)
61+
pybricks_size += len(data)
62+
pybricks_hash_calc.update(data)
5763

5864
# Compare hash against given value.
59-
if fw_hash.digest() == pybricks_firmware_hash:
65+
if pybricks_hash_calc.digest() == pybricks_hash:
6066
print("Firmware checksum is correct!")
6167
else:
6268
print("Bad firmware file. Stopping.")
6369
return
6470

6571
# Get firmware information.
6672
print("Getting firmware info.")
73+
version_position = read_flash_int(FLASH_LEGO_START + 0x200)
74+
checksum_position = read_flash_int(FLASH_LEGO_START + 0x204)
75+
base_firmware_size = checksum_position + 4 - FLASH_READ_OFFSET
76+
version = read_flash(version_position, 20)
77+
78+
# DEBUG
79+
print(version)
80+
print(base_firmware_size)
6781
print(get_base_firmware_reset_vector())

0 commit comments

Comments
 (0)