1212import uos
1313
1414
15+ FLASH_START = 0x8000000
16+ FLASH_END = FLASH_START + 1024 * 1024
17+
1518FLASH_LEGO_START = 0x8008000
1619FLASH_PYBRICKS_START = 0x80C0000
17- FLASH_READ_OFFSET = FLASH_LEGO_START
18-
19- FLASH_SIZE = 0x8000000 + 1024 * 1024 - FLASH_LEGO_START
2020
2121FF = b'\xFF '
2222
2323
2424def read_flash (address , length ):
2525 """Read a given number of bytes from a given absolute address."""
26- return firmware .flash_read (address - FLASH_READ_OFFSET )[0 :length ]
26+ return firmware .flash_read (address - FLASH_LEGO_START )[0 :length ]
2727
2828
2929def read_flash_int (address ):
3030 """Gets a little endian uint32 integer from the internal flash."""
3131 return int .from_bytes (read_flash (address , 4 ), 'little' )
3232
3333
34- def get_base_firmware_reset_vector ():
34+ def get_lego_reset_vector ():
3535 """Gets the boot vector of the original firmware."""
3636
37- # Read from base firmware location.
38- firmware_reset_vector = read_flash (FLASH_LEGO_START + 4 , 4 )
37+ # Read from lego firmware location.
38+ reset_vector = read_flash (FLASH_LEGO_START + 4 , 4 )
3939
4040 # If it's not pointing at Pybricks, return as is.
41- if int .from_bytes (firmware_reset_vector , 'little' ) < FLASH_PYBRICKS_START :
42- return firmware_reset_vector
41+ if int .from_bytes (reset_vector , 'little' ) < FLASH_PYBRICKS_START :
42+ return reset_vector
4343
44- # Otherwise read the boot vector in Pybricks.
44+ # Otherwise read the reset vector in Pybricks.
4545 return read_flash (FLASH_PYBRICKS_START + 4 , 4 )
4646
4747
@@ -54,10 +54,10 @@ def install(pybricks_hash):
5454 pybricks_hash_calc = uhashlib .sha256 ()
5555 pybricks_size = 0
5656
57- with open ("_pybricks/firmware.bin" ) as fw :
57+ with open ("_pybricks/firmware.bin" ) as pybricks_bin_file :
5858 data = b'START'
5959 while len (data ) > 0 :
60- data = fw .read (128 )
60+ data = pybricks_bin_file .read (128 )
6161 pybricks_size += len (data )
6262 pybricks_hash_calc .update (data )
6363
@@ -70,12 +70,18 @@ def install(pybricks_hash):
7070
7171 # Get firmware information.
7272 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 )
81- print (get_base_firmware_reset_vector ())
73+ lego_checksum_position = read_flash_int (FLASH_LEGO_START + 0x204 )
74+ lego_size = lego_checksum_position + 4 - FLASH_LEGO_START
75+
76+ lego_version_position = read_flash_int (FLASH_LEGO_START + 0x200 )
77+ lego_version = read_flash (lego_version_position , 20 )
78+ print ("LEGO Firmware version:" , lego_version )
79+
80+ # Verify firmware sizes
81+ if FLASH_LEGO_START + lego_size >= FLASH_PYBRICKS_START :
82+ print ("LEGO firmware too big." )
83+ return
84+
85+ if FLASH_PYBRICKS_START + pybricks_size >= FLASH_END :
86+ print ("Pybricks firmware too big." )
87+ return
0 commit comments