Commit 50fc0f49eaae7cd6dbe8e85d7b7a8b745966aee3
1 parent
ec9155c9
Fix Pi Zero and some CM3 MSD boot cases, also secure boot support
Showing
3 changed files
with
39 additions
and
9 deletions
main.c
| ... | ... | @@ -5,6 +5,7 @@ |
| 5 | 5 | |
| 6 | 6 | #include <unistd.h> |
| 7 | 7 | |
| 8 | +int signed_boot = 0; | |
| 8 | 9 | int verbose = 0; |
| 9 | 10 | int loop = 0; |
| 10 | 11 | char * directory = NULL; |
| ... | ... | @@ -31,6 +32,7 @@ void usage(int error) |
| 31 | 32 | fprintf(dest, "Further options:\n"); |
| 32 | 33 | fprintf(dest, " -l : Loop forever\n"); |
| 33 | 34 | fprintf(dest, " -v : Verbose\n"); |
| 35 | + fprintf(dest, " -s : Signed using bootsig.bin\n"); | |
| 34 | 36 | fprintf(dest, " -h : This help\n"); |
| 35 | 37 | |
| 36 | 38 | exit(error ? -1 : 0); |
| ... | ... | @@ -54,7 +56,7 @@ libusb_device_handle * LIBUSB_CALL open_device_with_vid( |
| 54 | 56 | r = libusb_get_device_descriptor(dev, &desc); |
| 55 | 57 | if (r < 0) |
| 56 | 58 | goto out; |
| 57 | - if(verbose) | |
| 59 | + if(verbose == 2) | |
| 58 | 60 | printf("Found device %u idVendor=0x%04x idProduct=0x%04x\n", i, desc.idVendor, desc.idProduct); |
| 59 | 61 | if (desc.idVendor == vendor_id) { |
| 60 | 62 | if(desc.idProduct == 0x2763 || |
| ... | ... | @@ -68,6 +70,7 @@ libusb_device_handle * LIBUSB_CALL open_device_with_vid( |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | if (found) { |
| 73 | + sleep(1); | |
| 71 | 74 | r = libusb_open(found, &handle); |
| 72 | 75 | if (r < 0) |
| 73 | 76 | { |
| ... | ... | @@ -91,11 +94,16 @@ int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device) |
| 91 | 94 | *usb_device = open_device_with_vid(*ctx, 0x0a5c); |
| 92 | 95 | if (*usb_device == NULL) |
| 93 | 96 | { |
| 94 | - sleep(1); | |
| 97 | + usleep(200); | |
| 95 | 98 | return -1; |
| 96 | 99 | } |
| 97 | 100 | |
| 98 | 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 | 108 | // Handle 2837 where it can start with two interfaces, the first is mass storage |
| 101 | 109 | // the second is the vendor interface for programming |
| ... | ... | @@ -186,6 +194,14 @@ void get_options(int argc, char *argv[]) |
| 186 | 194 | { |
| 187 | 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 | 205 | else |
| 190 | 206 | { |
| 191 | 207 | usage(1); |
| ... | ... | @@ -198,7 +214,7 @@ void get_options(int argc, char *argv[]) |
| 198 | 214 | boot_message_t boot_message; |
| 199 | 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 | 219 | int size; |
| 204 | 220 | |
| ... | ... | @@ -206,6 +222,11 @@ int second_stage_prep(FILE *fp) |
| 206 | 222 | boot_message.length = ftell(fp); |
| 207 | 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 | 230 | second_stage_txbuf = (uint8_t *) malloc(boot_message.length); |
| 210 | 231 | if (second_stage_txbuf == NULL) |
| 211 | 232 | { |
| ... | ... | @@ -227,8 +248,6 @@ int second_stage_boot(libusb_device_handle *usb_device) |
| 227 | 248 | { |
| 228 | 249 | int size, retcode = 0; |
| 229 | 250 | |
| 230 | - sleep(1); | |
| 231 | - | |
| 232 | 251 | size = ep_write(&boot_message, sizeof(boot_message), usb_device); |
| 233 | 252 | if (size != sizeof(boot_message)) |
| 234 | 253 | { |
| ... | ... | @@ -244,7 +263,7 @@ int second_stage_boot(libusb_device_handle *usb_device) |
| 244 | 263 | return -1; |
| 245 | 264 | } |
| 246 | 265 | |
| 247 | - usleep(125); | |
| 266 | + sleep(1); | |
| 248 | 267 | size = ep_read((unsigned char *)&retcode, sizeof(retcode), usb_device); |
| 249 | 268 | |
| 250 | 269 | if (size > 0 && retcode == 0) |
| ... | ... | @@ -256,8 +275,6 @@ int second_stage_boot(libusb_device_handle *usb_device) |
| 256 | 275 | printf("Failed : 0x%x", retcode); |
| 257 | 276 | } |
| 258 | 277 | |
| 259 | - sleep(1); | |
| 260 | - | |
| 261 | 278 | return retcode; |
| 262 | 279 | |
| 263 | 280 | } |
| ... | ... | @@ -406,6 +423,7 @@ int file_server(libusb_device_handle * usb_device) |
| 406 | 423 | int main(int argc, char *argv[]) |
| 407 | 424 | { |
| 408 | 425 | FILE * second_stage; |
| 426 | + FILE * fp_sign; | |
| 409 | 427 | libusb_context *ctx; |
| 410 | 428 | libusb_device_handle *usb_device; |
| 411 | 429 | struct libusb_device_descriptor desc; |
| ... | ... | @@ -438,7 +456,18 @@ int main(int argc, char *argv[]) |
| 438 | 456 | fprintf(stderr, "Unable to open 'bootcode.bin' from /usr/share/rpiboot or supplied directory\n"); |
| 439 | 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 | 472 | fprintf(stderr, "Failed to prepare the second stage bootcode\n"); |
| 444 | 473 | exit(-1); |
| ... | ... | @@ -500,6 +529,7 @@ int main(int argc, char *argv[]) |
| 500 | 529 | } |
| 501 | 530 | |
| 502 | 531 | libusb_close(usb_device); |
| 532 | + sleep(1); | |
| 503 | 533 | } |
| 504 | 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