Skip to content

Commit c43fed9

Browse files
committed
Re-jig to create more space for the AES code
Move vtor into main code body, leaving only binary info and embedded block at start Reduce initial stack size, then increase after AES code to overwrite the workspace Reduce vector table IRQs (requires pico-sdk pr 2200)
1 parent 6125d21 commit c43fed9

File tree

3 files changed

+26
-50
lines changed

3 files changed

+26
-50
lines changed

enc_bootloader/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ if (NOT USE_PRECOMPILED)
4040
# use stack guards, as AES variables are written near the stack
4141
target_compile_definitions(enc_bootloader PRIVATE
4242
PICO_USE_STACK_GUARDS=1
43-
PICO_STACK_SIZE=0x200
43+
PICO_STACK_SIZE=0x100
4444
PICO_NO_PROGRAM_INFO=1
4545
# No heap is used
4646
PICO_HEAP_SIZE=0
4747
# These inits are not required
4848
PICO_RUNTIME_SKIP_INIT_PER_CORE_IRQ_PRIORITIES=1
49-
PICO_RUNTIME_SKIP_INIT_BOOTROM_LOCKING_ENABLE=1)
49+
PICO_RUNTIME_SKIP_INIT_BOOTROM_LOCKING_ENABLE=1
50+
# Don't need any vtor irqs
51+
PICO_VECTOR_TABLE_NUM_IRQS=0)
52+
53+
# print memory usage
54+
target_link_options(enc_bootloader PUBLIC -Wl,--print-memory-usage)
5055

5156
pico_minimize_runtime(enc_bootloader)
5257

enc_bootloader/enc_bootloader.c

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
#ifdef DEBUG_PRINT
8-
#include <stdio.h>
9-
#endif
107
#include <string.h>
118
#include "pico/stdlib.h"
129
#include "boot/picobin.h"
1310
#include "pico/bootrom.h"
1411
#include "hardware/structs/otp.h"
15-
#if USE_USB_DPRAM
16-
#include "hardware/structs/usb_dpram.h"
17-
#endif
1812

1913
#include "pico/binary_info.h"
2014

@@ -65,20 +59,8 @@ static void init_aes() {
6559
init_lut_map();
6660
}
6761

68-
#if USE_USB_DPRAM
69-
uint8_t* workarea = (uint8_t*)USBCTRL_DPRAM_BASE;
70-
#else
71-
uint8_t* workarea = (uint8_t*)0x20080200; // AES Code & workspace from 0x20080180 -> 0x20081600
72-
#endif
73-
7462
int main() {
75-
#ifdef DEBUG_PRINT
76-
stdio_init_all();
77-
78-
printf("Decrypting the image\n");
79-
printf("OTP Valid Keys %x\n", otp_hw->key_valid);
80-
printf("Unlocking\n");
81-
#endif
63+
// Unlock OTP page with hardcoded key
8264
for (int i=0; i<4; i++) {
8365
uint32_t key_i = ((i*2+1) << 24) | ((i*2+1) << 16) |
8466
(i*2 << 8) | i*2;
@@ -101,12 +83,6 @@ int main() {
10183
memcpy(iv + 8, (void*)&iv2, sizeof(iv2));
10284
memcpy(iv + 12, (void*)&iv3, sizeof(iv3));
10385

104-
#ifdef DEBUG_PRINT
105-
printf("Pre decryption image begins with\n");
106-
for (int i=0; i < 4; i++)
107-
printf("%08x\n", *(uint32_t*)(data_start_addr + i*4));
108-
#endif
109-
11086
init_aes();
11187
// Read key directly from OTP - guarded reads will throw a bus fault if there are any errors
11288
uint16_t* otp_data = (uint16_t*)OTP_DATA_GUARDED_BASE;
@@ -115,25 +91,20 @@ int main() {
11591
otp_hw->sw_lock[otp_key_page] = 0xf;
11692
ctr_crypt_s(iv, (void*)data_start_addr, data_size/16);
11793

118-
#ifdef DEBUG_PRINT
119-
printf("Post decryption image begins with\n");
120-
for (int i=0; i < 4; i++)
121-
printf("%08x\n", *(uint32_t*)(data_start_addr + i*4));
122-
123-
printf("Chaining into %x, size %x\n", data_start_addr, data_size);
94+
// Increase stack limit by 0x100
95+
pico_default_asm_volatile(
96+
"mrs r0, msplim\n"
97+
"subs r0, 0x100\n"
98+
"msr msplim, r0"
99+
:::"r0");
124100

125-
stdio_deinit_all();
126-
#endif
127-
128-
int rc = rom_chain_image(
129-
workarea,
101+
// Chain into decrypted image
102+
rom_chain_image(
103+
(uint8_t*)0x20080200, // AES Code & workspace from 0x20080030 -> 0x20081500
130104
4 * 1024,
131105
data_start_addr,
132106
data_size
133107
);
134108

135-
#ifdef DEBUG_PRINT
136-
stdio_init_all();
137-
printf("Shouldn't return from ROM call %d\n", rc);
138-
#endif
109+
__breakpoint();
139110
}

enc_bootloader/memmap_enc_bootloader.ld

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
MEMORY
2525
{
26-
RAM_START(rwx) : ORIGIN = 0x20080000, LENGTH = 0x180
27-
SCRATCH_X(rwx) : ORIGIN = 0x20080180, LENGTH = 0xE80
28-
SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 0x800
29-
RAM(rwx) : ORIGIN = 0x20081800, LENGTH = 0x800
26+
RAM_START(rwx) : ORIGIN = 0x20080000, LENGTH = 0x30
27+
SCRATCH_X(rwx) : ORIGIN = 0x20080030, LENGTH = 0xFD0
28+
SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 0x700
29+
RAM(rwx) : ORIGIN = 0x20081700, LENGTH = 0x900
3030
}
3131

3232
ENTRY(_entry_point)
@@ -41,17 +41,17 @@ SECTIONS
4141

4242
.start_text : {
4343
__logical_binary_start = .;
44-
/* Vectors require 512-byte alignment on v8-M when >48 IRQs are used,
45-
so we would waste RAM if the vector table were not at the
46-
start. */
47-
KEEP (*(.vectors))
4844
KEEP (*(.binary_info_header))
4945
__binary_info_header_end = .;
5046
KEEP (*(.embedded_block))
5147
__embedded_block_end = .;
5248
} > RAM_START
5349

5450
.text : {
51+
/* Vectors require 512-byte alignment on v8-M when >48 IRQs are used,
52+
so we would waste RAM if the vector table were not at the
53+
start. */
54+
KEEP (*(.vectors))
5555
__reset_start = .;
5656
KEEP (*(.reset))
5757
__reset_end = .;

0 commit comments

Comments
 (0)