Skip to content

Commit 4b3378b

Browse files
committed
Reduce binary size
Add pico_minimize_runtime and some other runtime skips Re-use scratch_y for the workarea, without overwriting the stack
1 parent c05ea69 commit 4b3378b

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

enc_bootloader/CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,30 @@ if (NOT USE_PRECOMPILED)
3939
)
4040

4141
# use stack guards, as AES variables are written near the stack
42-
target_compile_definitions(enc_bootloader PRIVATE PICO_USE_STACK_GUARDS=1)
42+
target_compile_definitions(enc_bootloader PRIVATE
43+
PICO_USE_STACK_GUARDS=1
44+
PICO_STACK_SIZE=0x200
45+
# No heap is used
46+
PICO_HEAP_SIZE=0
47+
# These inits are not required
48+
PICO_RUNTIME_SKIP_INIT_PER_CORE_IRQ_PRIORITIES=1
49+
PICO_RUNTIME_SKIP_INIT_BOOTROM_LOCKING_ENABLE=1)
50+
51+
pico_minimize_runtime(enc_bootloader)
4352

4453
pico_set_binary_type(enc_bootloader no_flash)
4554
set(USE_USB_DPRAM FALSE)
4655
# create linker script to run from 0x20070000
4756
file(READ ${PICO_LINKER_SCRIPT_PATH}/memmap_no_flash.ld LINKER_SCRIPT)
4857
if (USE_USB_DPRAM)
49-
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x2007D000, LENGTH = 12k" LINKER_SCRIPT "${LINKER_SCRIPT}")
58+
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x2007F000, LENGTH = 4k" LINKER_SCRIPT "${LINKER_SCRIPT}")
5059
target_compile_definitions(enc_bootloader PRIVATE USE_USB_DPRAM=1)
5160
else()
52-
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x2007C000, LENGTH = 16k" LINKER_SCRIPT "${LINKER_SCRIPT}")
61+
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x2007F000, LENGTH = 4k" LINKER_SCRIPT "${LINKER_SCRIPT}")
5362
endif()
5463
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/memmap_enc_bootloader.ld "${LINKER_SCRIPT}")
5564
pico_set_linker_script(enc_bootloader ${CMAKE_CURRENT_BINARY_DIR}/memmap_enc_bootloader.ld)
65+
pico_add_dis_output(enc_bootloader)
5666
else()
5767
project(enc_bootloader C CXX ASM)
5868
message("Using precompiled enc_bootloader.elf")

enc_bootloader/enc_bootloader.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
#include "config.h"
2323

24-
volatile uint32_t systick_data[18]; // count, R0-R15,RETPSR
25-
2624
extern void remap();
2725
extern uint32_t gen_rand_sha();
2826
extern void init_key(uint8_t *key);
@@ -36,7 +34,7 @@ extern uint32_t lut_a_map[1];
3634
extern uint32_t lut_b_map[1];
3735
extern uint32_t rstate_sha[4],rstate_lfsr[2];
3836

39-
void resetrng() {
37+
void __scratch_x("aes") resetrng() {
4038
uint32_t f0,f1;
4139
do f0=get_rand_32(); while(f0==0); // make sure we don't initialise the LFSR to zero
4240
f1=get_rand_32();
@@ -52,15 +50,15 @@ void resetrng() {
5250
#endif
5351
}
5452

55-
static void init_lut_map() {
53+
static void __scratch_x("aes") init_lut_map() {
5654
int i;
5755
for(i=0;i<256;i++) lut_b[i]=gen_rand_sha()&0xff, lut_a[i]^=lut_b[i];
5856
lut_a_map[0]=0;
5957
lut_b_map[0]=0;
6058
remap();
6159
}
6260

63-
static void init_aes() {
61+
static void __scratch_x("aes") init_aes() {
6462
resetrng();
6563
gen_lut_sbox();
6664
init_lut_map();
@@ -69,7 +67,8 @@ static void init_aes() {
6967
#if USE_USB_DPRAM
7068
uint8_t* workarea = (uint8_t*)USBCTRL_DPRAM_BASE;
7169
#else
72-
static __attribute__((aligned(4))) uint8_t workarea[4 * 1024];
70+
// static __attribute__((aligned(4))) uint8_t workarea[4 * 1024];
71+
uint8_t* workarea = (uint8_t*)SRAM_SCRATCH_Y_BASE;
7372
#endif
7473

7574
int main() {
@@ -128,7 +127,7 @@ int main() {
128127

129128
int rc = rom_chain_image(
130129
workarea,
131-
4 * 1024,
130+
4 * 1024 - PICO_STACK_SIZE, // Don't use stack in workarea
132131
data_start_addr,
133132
data_size
134133
);
@@ -137,6 +136,4 @@ int main() {
137136
stdio_init_all();
138137
printf("Shouldn't return from ROM call %d\n", rc);
139138
#endif
140-
141-
reset_usb_boot(0, 0);
142139
}

0 commit comments

Comments
 (0)