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
6672static __attribute__((aligned (4 ))) uint8_t workarea [4 * 1024 ];
73+ #endif
6774
6875int 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}
0 commit comments