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+ boot2_variants = {
35+ "generic_03h" : ["rp2040" , "rp2350" ],
36+ "at25sf128a" : ["rp2040" ],
37+ "is25lp080" : ["rp2040" ],
38+ "w25q080" : ["rp2040" , "rp2350" ],
39+ "w25x10cl" : ["rp2040" ],
40+ }
3541def run (where , command , stdin = None ):
3642 print (command )
3743 result = subprocess .run (command , shell = True , cwd = where , input = stdin , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -46,41 +52,42 @@ def run(where, command, stdin=None):
4652def bitrev (x , width ):
4753 return int ("{:0{w}b}" .format (x , w = width )[::- 1 ], 2 )
4854
49- for variant in boot2_variants :
50- builddir = Path (f"build/boot2_{ variant } " )
51- builddir .mkdir (parents = True , exist_ok = True )
55+ for variant , targets in boot2_variants :
56+ for target in targets :
57+ builddir = Path (f"build/boot2_{ variant } _{ target } " )
58+ builddir .mkdir (parents = True , exist_ok = True )
5259
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 )
60+ run (builddir , f'cmake ../../pico_sdk_src -G "Unix Makefiles" -DPICO_DEFAULT_BOOT_STAGE2=boot2_{ variant } -DPICO_PLATFORM= { platforms [ target ] } ' )
61+ run (builddir , "make bs2_default_bin" ) # for validate
62+ #run(builddir,'make bs2_default_bin')
63+ ifile = builddir / f "src/{ target } /boot_stage2/bs2_default.bin"
64+ try :
65+ idata = open (ifile , "rb" ).read ()
66+ except :
67+ sys .exit (f"Could not open input file '{ ifile } '" )
68+ pad = 256
69+ if len (idata ) >= pad - 4 :
70+ sys .exit (f"Input file size ({ len (idata )} bytes) too large for final size ({ pad } bytes)" )
71+ idata_padded = idata + bytes (pad - 4 - len (idata ))
72+ seed = 0xffffffff
73+ # Our bootrom CRC32 is slightly bass-ackward but it's best to work around for now (FIXME)
74+ # 100% worth it to save two Thumb instructions
75+ checksum = bitrev (
76+ (binascii .crc32 (bytes (bitrev (b , 8 ) for b in idata_padded ), seed ^ 0xffffffff ) ^ 0xffffffff ) & 0xffffffff , 32 )
77+ odata = idata_padded + struct .pack ("<L" , checksum )
7178
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 ))
79+ ofilename = Path (f"src/boot2_{ target } _ { variant } .cpp" )
80+ try :
81+ ofilename .parent .mkdir (parents = True , exist_ok = True )
82+ with ofilename .open ("w" ) as ofile :
83+ ofile .write ("// Stage2 bootloader\n \n " )
84+ ofile .write ("#include <cstdint>\n " )
85+ ofile .write ('extern "C" __attribute__((section(".boot2"))) const uint8_t boot2[256] = {\n ' )
86+ for offs in range (0 , len (odata ), 16 ):
87+ chunk = odata [offs :min (offs + 16 , len (odata ))]
88+ ofile .write ("\t {},\n " .format (", " .join (f"0x{ b :02x} " for b in chunk )))
89+ ofile .write ("};\n " )
90+ except :
91+ sys .exit ("Could not open output file '{}'" .format (ofilename ))
8592
8693partial .commit (files , tag )
0 commit comments