11#!/usr/bin/env python3
22
3- import os
43import sys
4+ import shutil
55import subprocess
66import binascii
77import struct
1313
1414repo = "raspberrypi/pico-sdk"
1515src = Path ("pico_sdk_src" )
16+ platforms = {
17+ "rp2040" : "rp2040" ,
18+ "rp2350" : "rp2350-arm-s"
19+ }
1620
1721tag = partial .latest_release_tag (repo )
1822partial .clone_repo (repo , src , branch = tag , overwrite = "--fast" not in sys .argv )
19- files = partial .copy_files (src , ["LICENSE.TXT" ])
20- files += partial .copy_files (Path ("pico_sdk_src/src/rp2040/hardware_regs" ), ["include/**/*.h" ])
21- files += partial .copy_files (Path ("pico_sdk_src/src/rp2040/hardware_structs" ), ["include/**/*.h" ], delete = False )
22- files += partial .copy_files (Path ("pico_sdk_src/src/rp2_common/cmsis/stub/CMSIS/Device/RP2040/Include" ), ["*.h" ], dest = Path ("include" ), delete = False )
23+ shutil .rmtree ("src" , ignore_errors = True )
24+ files = [Path ("src" )]
25+ files += partial .copy_files (src , ["LICENSE.TXT" ])
2326files += partial .copy_files (Path ("pico_sdk_src/tools" ), ["pioasm/**/*.[hct]*" ])
24- files += [Path ("src" )]
27+ for target in platforms :
28+ files += partial .copy_files (Path (f"pico_sdk_src/src/{ target } /hardware_regs/include/" ), ["**/*.h" ], dest = target )
29+ files += partial .copy_files (Path (f"pico_sdk_src/src/{ target } /hardware_structs/include/" ), ["**/*.h" ], dest = target , delete = False )
30+ files += partial .copy_files (Path (f"pico_sdk_src/src/rp2_common/cmsis/stub/CMSIS/Device/{ target .upper ()} /Include/" ), ["*.h" ], dest = target , delete = False )
2531
26- print ("Building boot2 variants..." )
27- boot2_variants = [
28- "generic_03h" ,
29- "at25sf128a" ,
30- "is25lp080" ,
31- "w25q080" ,
32- "w25x10cl"
33- ]
3432
33+ print ("Building boot2 variants..." )
34+ # See https://github.com/raspberrypi/pico-sdk/issues/2126
35+ boot2_variants = {
36+ "generic_03h" : ["rp2040" , "rp2350" ],
37+ "at25sf128a" : ["rp2040" ],
38+ "is25lp080" : ["rp2040" ],
39+ "w25q080" : ["rp2040" , "rp2350" ],
40+ "w25x10cl" : ["rp2040" ],
41+ }
3542def run (where , command , stdin = None ):
3643 print (command )
3744 result = subprocess .run (command , shell = True , cwd = where , input = stdin , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -46,41 +53,42 @@ def run(where, command, stdin=None):
4653def bitrev (x , width ):
4754 return int ("{:0{w}b}" .format (x , w = width )[::- 1 ], 2 )
4855
49- for variant in boot2_variants :
50- builddir = Path (f"build/boot2_{ variant } " )
51- builddir .mkdir (parents = True , exist_ok = True )
56+ for variant , targets in boot2_variants .items ():
57+ for target in targets :
58+ builddir = Path (f"build/boot2_{ variant } _{ target } " )
59+ builddir .mkdir (parents = True , exist_ok = True )
5260
53- run (builddir , f'cmake ../../pico_sdk_src -G "Unix Makefiles" -DPICO_DEFAULT_BOOT_STAGE2=boot2_{ variant } ' )
54- run (builddir , "make bs2_default_bin" ) # for validate
55- #run(builddir,'make bs2_default_bin')
56- ifile = builddir / "src/rp2040 /boot_stage2/bs2_default.bin"
57- try :
58- idata = open (ifile , "rb" ).read ()
59- except :
60- sys .exit (f"Could not open input file '{ ifile } '" )
61- pad = 256
62- if len (idata ) >= pad - 4 :
63- sys .exit (f"Input file size ({ len (idata )} bytes) too large for final size ({ pad } bytes)" )
64- idata_padded = idata + bytes (pad - 4 - len (idata ))
65- seed = 0xffffffff
66- # Our bootrom CRC32 is slightly bass-ackward but it's best to work around for now (FIXME)
67- # 100% worth it to save two Thumb instructions
68- checksum = bitrev (
69- (binascii .crc32 (bytes (bitrev (b , 8 ) for b in idata_padded ), seed ^ 0xffffffff ) ^ 0xffffffff ) & 0xffffffff , 32 )
70- odata = idata_padded + struct .pack ("<L" , checksum )
61+ run (builddir , f'cmake ../../pico_sdk_src -G "Unix Makefiles" -DPICO_DEFAULT_BOOT_STAGE2=boot2_{ variant } -DPICO_PLATFORM= { platforms [ target ] } ' )
62+ run (builddir , "make bs2_default_bin" ) # for validate
63+ #run(builddir,'make bs2_default_bin')
64+ ifile = builddir / f "src/{ target } /boot_stage2/bs2_default.bin"
65+ try :
66+ idata = open (ifile , "rb" ).read ()
67+ except :
68+ sys .exit (f"Could not open input file '{ ifile } '" )
69+ pad = 256
70+ if len (idata ) >= pad - 4 :
71+ sys .exit (f"Input file size ({ len (idata )} bytes) too large for final size ({ pad } bytes)" )
72+ idata_padded = idata + bytes (pad - 4 - len (idata ))
73+ seed = 0xffffffff
74+ # Our bootrom CRC32 is slightly bass-ackward but it's best to work around for now (FIXME)
75+ # 100% worth it to save two Thumb instructions
76+ checksum = bitrev (
77+ (binascii .crc32 (bytes (bitrev (b , 8 ) for b in idata_padded ), seed ^ 0xffffffff ) ^ 0xffffffff ) & 0xffffffff , 32 )
78+ odata = idata_padded + struct .pack ("<L" , checksum )
7179
72- ofilename = Path (f"src/boot2_{ variant } .cpp" )
73- try :
74- ofilename .parent .mkdir (parents = True , exist_ok = True )
75- with ofilename .open ("w" ) as ofile :
76- ofile .write ("// Stage2 bootloader\n \n " )
77- ofile .write ("#include <cstdint>\n " )
78- ofile .write ('extern "C" __attribute__((section(".boot2"))) const uint8_t boot2[256] = {\n ' )
79- for offs in range (0 , len (odata ), 16 ):
80- chunk = odata [offs :min (offs + 16 , len (odata ))]
81- ofile .write ("\t {},\n " .format (", " .join (f"0x{ b :02x} " for b in chunk )))
82- ofile .write ("};\n " )
83- except :
84- sys .exit ("Could not open output file '{}'" .format (ofilename ))
80+ ofilename = Path (f"src/boot2_{ target } _ { variant } .cpp" )
81+ try :
82+ ofilename .parent .mkdir (parents = True , exist_ok = True )
83+ with ofilename .open ("w" ) as ofile :
84+ ofile .write ("// Stage2 bootloader\n \n " )
85+ ofile .write ("#include <cstdint>\n " )
86+ ofile .write ('extern "C" __attribute__((section(".boot2"))) const uint8_t boot2[256] = {\n ' )
87+ for offs in range (0 , len (odata ), 16 ):
88+ chunk = odata [offs :min (offs + 16 , len (odata ))]
89+ ofile .write ("\t {},\n " .format (", " .join (f"0x{ b :02x} " for b in chunk )))
90+ ofile .write ("};\n " )
91+ except :
92+ sys .exit ("Could not open output file '{}'" .format (ofilename ))
8593
8694partial .commit (files , tag )
0 commit comments