Skip to content

Commit c05ea69

Browse files
committed
Remove debug prints, and add USE_USB_DPRAM option
1 parent 7c555a6 commit c05ea69

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

enc_bootloader/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ if (NOT USE_PRECOMPILED)
3838
pico_rand
3939
)
4040

41+
# use stack guards, as AES variables are written near the stack
42+
target_compile_definitions(enc_bootloader PRIVATE PICO_USE_STACK_GUARDS=1)
43+
4144
pico_set_binary_type(enc_bootloader no_flash)
45+
set(USE_USB_DPRAM FALSE)
4246
# create linker script to run from 0x20070000
4347
file(READ ${PICO_LINKER_SCRIPT_PATH}/memmap_no_flash.ld LINKER_SCRIPT)
44-
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x20078000, LENGTH = 32k" LINKER_SCRIPT "${LINKER_SCRIPT}")
48+
if (USE_USB_DPRAM)
49+
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x2007D000, LENGTH = 12k" LINKER_SCRIPT "${LINKER_SCRIPT}")
50+
target_compile_definitions(enc_bootloader PRIVATE USE_USB_DPRAM=1)
51+
else()
52+
string(REPLACE "RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k" "RAM(rwx) : ORIGIN = 0x2007C000, LENGTH = 16k" LINKER_SCRIPT "${LINKER_SCRIPT}")
53+
endif()
4554
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/memmap_enc_bootloader.ld "${LINKER_SCRIPT}")
4655
pico_set_linker_script(enc_bootloader ${CMAKE_CURRENT_BINARY_DIR}/memmap_enc_bootloader.ld)
4756
else()

enc_bootloader/enc_bootloader.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
*
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
6+
7+
#ifdef DEBUG_PRINT
68
#include <stdio.h>
9+
#endif
710
#include <string.h>
811
#include "pico/stdlib.h"
912
#include "boot/picobin.h"
10-
#include "hardware/uart.h"
1113
#include "pico/bootrom.h"
1214
#include "pico/rand.h"
1315
#include "hardware/structs/otp.h"
14-
#include "hardware/structs/qmi.h"
15-
#include "hardware/structs/xip_ctrl.h"
16+
#if USE_USB_DPRAM
17+
#include "hardware/structs/usb_dpram.h"
18+
#endif
1619

1720
#include "pico/binary_info.h"
1821

@@ -63,14 +66,20 @@ static void init_aes() {
6366
init_lut_map();
6467
}
6568

69+
#if USE_USB_DPRAM
70+
uint8_t* workarea = (uint8_t*)USBCTRL_DPRAM_BASE;
71+
#else
6672
static __attribute__((aligned(4))) uint8_t workarea[4 * 1024];
73+
#endif
6774

6875
int main() {
76+
#ifdef DEBUG_PRINT
6977
stdio_init_all();
7078

7179
printf("Decrypting the image\n");
7280
printf("OTP Valid Keys %x\n", otp_hw->key_valid);
7381
printf("Unlocking\n");
82+
#endif
7483
for (int i=0; i<4; i++) {
7584
uint32_t key_i = ((i*2+1) << 24) | ((i*2+1) << 16) |
7685
(i*2 << 8) | i*2;
@@ -93,9 +102,11 @@ int main() {
93102
memcpy(iv + 8, (void*)&iv2, sizeof(iv2));
94103
memcpy(iv + 12, (void*)&iv3, sizeof(iv3));
95104

105+
#ifdef DEBUG_PRINT
96106
printf("Pre decryption image begins with\n");
97107
for (int i=0; i < 4; i++)
98108
printf("%08x\n", *(uint32_t*)(data_start_addr + i*4));
109+
#endif
99110

100111
init_aes();
101112
// Read key directly from OTP - guarded reads will throw a bus fault if there are any errors
@@ -105,23 +116,27 @@ int main() {
105116
otp_hw->sw_lock[otp_key_page] = 0xf;
106117
ctr_crypt_s(iv, (void*)data_start_addr, data_size/16);
107118

119+
#ifdef DEBUG_PRINT
108120
printf("Post decryption image begins with\n");
109121
for (int i=0; i < 4; i++)
110122
printf("%08x\n", *(uint32_t*)(data_start_addr + i*4));
111123

112124
printf("Chaining into %x, size %x\n", data_start_addr, data_size);
113125

114126
stdio_deinit_all();
127+
#endif
115128

116129
int rc = rom_chain_image(
117130
workarea,
118-
sizeof(workarea),
131+
4 * 1024,
119132
data_start_addr,
120133
data_size
121134
);
122135

136+
#ifdef DEBUG_PRINT
123137
stdio_init_all();
124138
printf("Shouldn't return from ROM call %d\n", rc);
139+
#endif
125140

126141
reset_usb_boot(0, 0);
127142
}

enc_bootloader/enc_bootloader.elf

-20.2 KB
Binary file not shown.

main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4940,7 +4940,6 @@ bool encrypt_command::execute(device_map &devices) {
49404940
// Bootloader size
49414941
auto bootloader_txt = enc_elf->get_section(".text");
49424942
uint32_t bootloader_size = 0x20082000 - bootloader_txt->virtual_address();
4943-
printf("Bootloader size %08x start %08x\n", bootloader_size, bootloader_txt->virtual_address());
49444943

49454944
// Move bootloader down in physical space to start of SRAM (which will be start of flash once packaged)
49464945
enc_elf->move_all(data_start_address - bootloader_txt->virtual_address());

0 commit comments

Comments
 (0)