Skip to content

Commit a173d93

Browse files
davelongdevdlech
authored andcommitted
pbio/platform/build_hat: Reclaim bootloader RAM for the GC heap.
The linker script limited RAM to 240k because the Build HAT bootloader (BHBL) uses the top 16 KiB while it loads the firmware. Once BHBL jumps to the firmware it is no longer running, so the firmware is free to use that RAM. Extend RAM to 256k so the GC heap, which runs to the end of the RAM region, gains the 16 KiB. Add a link-time ASSERT that the loaded image (.text through .data) still ends below 0x2003C000, since that is the part BHBL copies in while it is still using the RAM above. The .bss section and heap may extend past that address because the firmware zeroes .bss itself after the jump. Replace the header comment with an explanation of the layout. Verified on a Build HAT loaded from a Raspberry Pi 5: micropython.mem_info() GC total 80256 -> 96256 bytes. Firmware size: 152188 -> 152188 bytes (unchanged). Fixes pybricks/support#2484
1 parent 9d2a823 commit a173d93

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

‎lib/pbio/platform/build_hat/rpi_build_hat.ld‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
/*
22
Modified from
33
pico-sdk/src/rp2_common/pico_standard_link/memmap_no_flash.ld
4-
to limit RAM use to 240K for use with bootloader.
4+
for use with the Build HAT bootloader (BHBL).
5+
6+
The firmware runs entirely from RAM. While loading it, BHBL uses the top
7+
16 KiB of RAM (0x2003C000-0x20040000) for its own stack and data.
8+
9+
Once BHBL hands control to the firmware, it is no longer running, so that
10+
16 KiB is unused. Thus, the RAM entry in the MEMORY code block in this file ends at
11+
the end of RAM rather than at 0x2003C000. Since the GC heap runs to the end
12+
of that entry, the reclaimed 16 KiB becomes heap.
13+
14+
The loaded image (.text through .data) must still end below 0x2003C000, or
15+
BHBL would overwrite itself during loading. An ASSERT at the end of this
16+
file checks that at link time.
517
*/
618
/* Based on GCC ARM embedded samples.
719
Defines the following symbols for use by code:
@@ -28,7 +40,7 @@ to limit RAM use to 240K for use with bootloader.
2840

2941
MEMORY
3042
{
31-
RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 240k /* <--- changed here */
43+
RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k
3244
SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k
3345
SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k
3446
}
@@ -251,6 +263,11 @@ SECTIONS
251263
*/
252264
ASSERT((pbsys_storage_heap_end - pbsys_storage_heap_start) > 32*1024, "GcHeap is too small")
253265

266+
/* The loaded image must not reach the RAM that BHBL uses while loading
267+
* it. See the comment at the top of this file. */
268+
__bootloader_ram_start = 0x2003C000;
269+
ASSERT(__data_end__ <= __bootloader_ram_start, "Firmware image is too large: it must end below 0x2003C000 (see the comment at the top of rpi_build_hat.ld). Link aborted, no firmware.bin or firmware.elf created.")
270+
254271
ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary")
255272
/* todo assert on extra code */
256273
}

0 commit comments

Comments
 (0)