Commit 50fc0f49eaae7cd6dbe8e85d7b7a8b745966aee3

Authored by ghollingworth
1 parent ec9155c9

Fix Pi Zero and some CM3 MSD boot cases, also secure boot support

@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 5
6 #include <unistd.h> 6 #include <unistd.h>
7 7
  8 +int signed_boot = 0;
8 int verbose = 0; 9 int verbose = 0;
9 int loop = 0; 10 int loop = 0;
10 char * directory = NULL; 11 char * directory = NULL;
@@ -31,6 +32,7 @@ void usage(int error) @@ -31,6 +32,7 @@ void usage(int error)
31 fprintf(dest, "Further options:\n"); 32 fprintf(dest, "Further options:\n");
32 fprintf(dest, " -l : Loop forever\n"); 33 fprintf(dest, " -l : Loop forever\n");
33 fprintf(dest, " -v : Verbose\n"); 34 fprintf(dest, " -v : Verbose\n");
  35 + fprintf(dest, " -s : Signed using bootsig.bin\n");
34 fprintf(dest, " -h : This help\n"); 36 fprintf(dest, " -h : This help\n");
35 37
36 exit(error ? -1 : 0); 38 exit(error ? -1 : 0);
@@ -54,7 +56,7 @@ libusb_device_handle * LIBUSB_CALL open_device_with_vid( @@ -54,7 +56,7 @@ libusb_device_handle * LIBUSB_CALL open_device_with_vid(
54 r = libusb_get_device_descriptor(dev, &desc); 56 r = libusb_get_device_descriptor(dev, &desc);
55 if (r < 0) 57 if (r < 0)
56 goto out; 58 goto out;
57 - if(verbose) 59 + if(verbose == 2)
58 printf("Found device %u idVendor=0x%04x idProduct=0x%04x\n", i, desc.idVendor, desc.idProduct); 60 printf("Found device %u idVendor=0x%04x idProduct=0x%04x\n", i, desc.idVendor, desc.idProduct);
59 if (desc.idVendor == vendor_id) { 61 if (desc.idVendor == vendor_id) {
60 if(desc.idProduct == 0x2763 || 62 if(desc.idProduct == 0x2763 ||
@@ -68,6 +70,7 @@ libusb_device_handle * LIBUSB_CALL open_device_with_vid( @@ -68,6 +70,7 @@ libusb_device_handle * LIBUSB_CALL open_device_with_vid(
68 } 70 }
69 71
70 if (found) { 72 if (found) {
  73 + sleep(1);
71 r = libusb_open(found, &handle); 74 r = libusb_open(found, &handle);
72 if (r < 0) 75 if (r < 0)
73 { 76 {
@@ -91,11 +94,16 @@ int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device) @@ -91,11 +94,16 @@ int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device)
91 *usb_device = open_device_with_vid(*ctx, 0x0a5c); 94 *usb_device = open_device_with_vid(*ctx, 0x0a5c);
92 if (*usb_device == NULL) 95 if (*usb_device == NULL)
93 { 96 {
94 - sleep(1); 97 + usleep(200);
95 return -1; 98 return -1;
96 } 99 }
97 100
98 libusb_get_active_config_descriptor(libusb_get_device(*usb_device), &config); 101 libusb_get_active_config_descriptor(libusb_get_device(*usb_device), &config);
  102 + if(config == NULL)
  103 + {
  104 + printf("Failed to read config descriptor\n");
  105 + exit(-1);
  106 + }
99 107
100 // Handle 2837 where it can start with two interfaces, the first is mass storage 108 // Handle 2837 where it can start with two interfaces, the first is mass storage
101 // the second is the vendor interface for programming 109 // the second is the vendor interface for programming
@@ -186,6 +194,14 @@ void get_options(int argc, char *argv[]) @@ -186,6 +194,14 @@ void get_options(int argc, char *argv[])
186 { 194 {
187 verbose = 1; 195 verbose = 1;
188 } 196 }
  197 + else if(strcmp(*argv, "-vv") == 0)
  198 + {
  199 + verbose = 2;
  200 + }
  201 + else if(strcmp(*argv, "-s") == 0)
  202 + {
  203 + signed_boot = 1;
  204 + }
189 else 205 else
190 { 206 {
191 usage(1); 207 usage(1);
@@ -198,7 +214,7 @@ void get_options(int argc, char *argv[]) @@ -198,7 +214,7 @@ void get_options(int argc, char *argv[])
198 boot_message_t boot_message; 214 boot_message_t boot_message;
199 void *second_stage_txbuf; 215 void *second_stage_txbuf;
200 216
201 -int second_stage_prep(FILE *fp) 217 +int second_stage_prep(FILE *fp, FILE *fp_sig)
202 { 218 {
203 int size; 219 int size;
204 220
@@ -206,6 +222,11 @@ int second_stage_prep(FILE *fp) @@ -206,6 +222,11 @@ int second_stage_prep(FILE *fp)
206 boot_message.length = ftell(fp); 222 boot_message.length = ftell(fp);
207 fseek(fp, 0, SEEK_SET); 223 fseek(fp, 0, SEEK_SET);
208 224
  225 + if(fp_sig != NULL)
  226 + {
  227 + fread(boot_message.signature, 1, sizeof(boot_message.signature), fp_sig);
  228 + }
  229 +
209 second_stage_txbuf = (uint8_t *) malloc(boot_message.length); 230 second_stage_txbuf = (uint8_t *) malloc(boot_message.length);
210 if (second_stage_txbuf == NULL) 231 if (second_stage_txbuf == NULL)
211 { 232 {
@@ -227,8 +248,6 @@ int second_stage_boot(libusb_device_handle *usb_device) @@ -227,8 +248,6 @@ int second_stage_boot(libusb_device_handle *usb_device)
227 { 248 {
228 int size, retcode = 0; 249 int size, retcode = 0;
229 250
230 - sleep(1);  
231 -  
232 size = ep_write(&boot_message, sizeof(boot_message), usb_device); 251 size = ep_write(&boot_message, sizeof(boot_message), usb_device);
233 if (size != sizeof(boot_message)) 252 if (size != sizeof(boot_message))
234 { 253 {
@@ -244,7 +263,7 @@ int second_stage_boot(libusb_device_handle *usb_device) @@ -244,7 +263,7 @@ int second_stage_boot(libusb_device_handle *usb_device)
244 return -1; 263 return -1;
245 } 264 }
246 265
247 - usleep(125); 266 + sleep(1);
248 size = ep_read((unsigned char *)&retcode, sizeof(retcode), usb_device); 267 size = ep_read((unsigned char *)&retcode, sizeof(retcode), usb_device);
249 268
250 if (size > 0 && retcode == 0) 269 if (size > 0 && retcode == 0)
@@ -256,8 +275,6 @@ int second_stage_boot(libusb_device_handle *usb_device) @@ -256,8 +275,6 @@ int second_stage_boot(libusb_device_handle *usb_device)
256 printf("Failed : 0x%x", retcode); 275 printf("Failed : 0x%x", retcode);
257 } 276 }
258 277
259 - sleep(1);  
260 -  
261 return retcode; 278 return retcode;
262 279
263 } 280 }
@@ -406,6 +423,7 @@ int file_server(libusb_device_handle * usb_device) @@ -406,6 +423,7 @@ int file_server(libusb_device_handle * usb_device)
406 int main(int argc, char *argv[]) 423 int main(int argc, char *argv[])
407 { 424 {
408 FILE * second_stage; 425 FILE * second_stage;
  426 + FILE * fp_sign;
409 libusb_context *ctx; 427 libusb_context *ctx;
410 libusb_device_handle *usb_device; 428 libusb_device_handle *usb_device;
411 struct libusb_device_descriptor desc; 429 struct libusb_device_descriptor desc;
@@ -438,7 +456,18 @@ int main(int argc, char *argv[]) @@ -438,7 +456,18 @@ int main(int argc, char *argv[])
438 fprintf(stderr, "Unable to open 'bootcode.bin' from /usr/share/rpiboot or supplied directory\n"); 456 fprintf(stderr, "Unable to open 'bootcode.bin' from /usr/share/rpiboot or supplied directory\n");
439 usage(1); 457 usage(1);
440 } 458 }
441 - if(second_stage_prep(second_stage) != 0) 459 +
  460 + if(signed_boot)
  461 + {
  462 + fp_sign = check_file(directory, "bootsig.bin");
  463 + if(fp_sign == NULL)
  464 + {
  465 + fprintf(stderr, "Unable to open 'bootsig.bin'\n");
  466 + usage(1);
  467 + }
  468 + }
  469 +
  470 + if(second_stage_prep(second_stage, fp_sign) != 0)
442 { 471 {
443 fprintf(stderr, "Failed to prepare the second stage bootcode\n"); 472 fprintf(stderr, "Failed to prepare the second stage bootcode\n");
444 exit(-1); 473 exit(-1);
@@ -500,6 +529,7 @@ int main(int argc, char *argv[]) @@ -500,6 +529,7 @@ int main(int argc, char *argv[])
500 } 529 }
501 530
502 libusb_close(usb_device); 531 libusb_close(usb_device);
  532 + sleep(1);
503 } 533 }
504 while(loop || desc.iSerialNumber == 0); 534 while(loop || desc.iSerialNumber == 0);
505 535
msd/bootcode.bin
No preview for this file type
msd/start.elf
No preview for this file type