summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmaury Pouly <pamaury@rockbox.org>2011-10-29 17:04:20 +0000
committerAmaury Pouly <pamaury@rockbox.org>2011-10-29 17:04:20 +0000
commita8cde851fbcefdd33d826cf4b1f0daa8c0b48dc2 (patch)
treef1254b5625cc3f11d6c4d06ebb81bdd6b2aa5976
parentd2a58f3aadf33e11bcbc4743cac65d4464447db8 (diff)
downloadrockbox-a8cde851fbcefdd33d826cf4b1f0daa8c0b48dc2.tar.gz
rockbox-a8cde851fbcefdd33d826cf4b1f0daa8c0b48dc2.zip
sbtools: add options to override real key and IV, fix output prefix in sbtoelf, unify command line to add keys
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30852 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--utils/sbtools/elftosb.c68
-rw-r--r--utils/sbtools/sbtoelf.c53
2 files changed, 47 insertions, 74 deletions
diff --git a/utils/sbtools/elftosb.c b/utils/sbtools/elftosb.c
index 3e217a8979..c1a86ea23b 100644
--- a/utils/sbtools/elftosb.c
+++ b/utils/sbtools/elftosb.c
@@ -325,8 +325,9 @@ void usage(void)
325 printf(" -d/--debug\tEnable debug output\n"); 325 printf(" -d/--debug\tEnable debug output\n");
326 printf(" -k <file>\tAdd key file\n"); 326 printf(" -k <file>\tAdd key file\n");
327 printf(" -z\t\tAdd zero key\n"); 327 printf(" -z\t\tAdd zero key\n");
328 printf(" --single-key <key>\tAdd single key\n"); 328 printf(" --add-key <key>\tAdd single key (hex or usbotp)\n");
329 printf(" --usb-otp <vid>:<pid>\tAdd USB OTP device\n"); 329 printf(" --real-key <key>\tOverride real key\n");
330 printf(" --crypto-iv <iv>\tOverride crypto IV\n");
330 exit(1); 331 exit(1);
331} 332}
332 333
@@ -340,6 +341,10 @@ int main(int argc, char **argv)
340{ 341{
341 char *cmd_filename = NULL; 342 char *cmd_filename = NULL;
342 char *output_filename = NULL; 343 char *output_filename = NULL;
344 struct crypto_key_t real_key;
345 struct crypto_key_t crypto_iv;
346 real_key.method = CRYPTO_NONE;
347 crypto_iv.method = CRYPTO_NONE;
343 348
344 while(1) 349 while(1)
345 { 350 {
@@ -347,12 +352,13 @@ int main(int argc, char **argv)
347 { 352 {
348 {"help", no_argument, 0, '?'}, 353 {"help", no_argument, 0, '?'},
349 {"debug", no_argument, 0, 'd'}, 354 {"debug", no_argument, 0, 'd'},
350 {"single-key", required_argument, 0, 's'}, 355 {"add-key", required_argument, 0, 'a'},
351 {"usb-otp", required_argument, 0, 'u'}, 356 {"real-key", required_argument, 0, 'r'},
357 {"crypto-iv", required_argument, 0, 'i'},
352 {0, 0, 0, 0} 358 {0, 0, 0, 0}
353 }; 359 };
354 360
355 int c = getopt_long(argc, argv, "?do:c:k:z", long_options, NULL); 361 int c = getopt_long(argc, argv, "?do:c:k:za:", long_options, NULL);
356 if(c == -1) 362 if(c == -1)
357 break; 363 break;
358 switch(c) 364 switch(c)
@@ -379,40 +385,22 @@ int main(int argc, char **argv)
379 add_keys(&g_zero_key, 1); 385 add_keys(&g_zero_key, 1);
380 break; 386 break;
381 } 387 }
382 case 's': 388 case 'a':
389 case 'r':
390 case 'i':
383 { 391 {
384 struct crypto_key_t key; 392 struct crypto_key_t key;
385 key.method = CRYPTO_KEY; 393 char *s = optarg;
386 if(strlen(optarg) != 32) 394 if(!parse_key(&s, &key))
387 bug("The key given in argument is invalid"); 395 bug("Invalid key/iv specified as argument");
388 for(int i = 0; i < 16; i++) 396 if(*s != 0)
389 { 397 bug("Trailing characters after key/iv specified as argument");
390 byte a, b; 398 if(c == 'r')
391 if(convxdigit(optarg[2 * i], &a) || convxdigit(optarg[2 * i + 1], &b)) 399 memcpy(&real_key, &key, sizeof(key));
392 bugp("The key given in argument is invalid\n"); 400 else if(c == 'i')
393 key.u.key[i] = (a << 4) | b; 401 memcpy(&crypto_iv, &key, sizeof(key));
394 } 402 else
395 add_keys(&key, 1); 403 add_keys(&key, 1);
396 break;
397 }
398 case 'u':
399 {
400 int vid, pid;
401 char *p = strchr(optarg, ':');
402 if(p == NULL)
403 bug("Invalid VID/PID\n");
404
405 char *end;
406 vid = strtol(optarg, &end, 16);
407 if(end != p)
408 bug("Invalid VID/PID\n");
409 pid = strtol(p + 1, &end, 16);
410 if(end != (optarg + strlen(optarg)))
411 bug("Invalid VID/PID\n");
412 struct crypto_key_t key;
413 key.method = CRYPTO_USBOTP;
414 key.u.vid_pid = vid << 16 | pid;
415 add_keys(&key, 1);
416 break; 404 break;
417 } 405 }
418 default: 406 default:
@@ -443,6 +431,12 @@ int main(int argc, char **argv)
443 431
444 struct cmd_file_t *cmd_file = db_parse_file(cmd_filename); 432 struct cmd_file_t *cmd_file = db_parse_file(cmd_filename);
445 struct sb_file_t *sb_file = apply_cmd_file(cmd_file); 433 struct sb_file_t *sb_file = apply_cmd_file(cmd_file);
434
435 if(real_key.method == CRYPTO_KEY)
436 sb_file->real_key = &real_key.u.key;
437 if(crypto_iv.method == CRYPTO_KEY)
438 sb_file->crypto_iv = &crypto_iv.u.key;
439
446 sb_produce_file(sb_file, output_filename); 440 sb_produce_file(sb_file, output_filename);
447 441
448 return 0; 442 return 0;
diff --git a/utils/sbtools/sbtoelf.c b/utils/sbtools/sbtoelf.c
index 3824ee094e..87017ab12d 100644
--- a/utils/sbtools/sbtoelf.c
+++ b/utils/sbtools/sbtoelf.c
@@ -703,8 +703,7 @@ void usage(void)
703 printf(" -k <file>\tAdd key file\n"); 703 printf(" -k <file>\tAdd key file\n");
704 printf(" -z\t\tAdd zero key\n"); 704 printf(" -z\t\tAdd zero key\n");
705 printf(" -r\t\tUse raw command mode\n"); 705 printf(" -r\t\tUse raw command mode\n");
706 printf(" --single-key <key>\tAdd single key\n"); 706 printf(" --add-key <key>\tAdd single key (hex or usbotp)\n");
707 printf(" --usb-otp <vid>:<pid>\tAdd USB OTP device\n");
708 exit(1); 707 exit(1);
709} 708}
710 709
@@ -722,12 +721,11 @@ int main(int argc, char **argv)
722 { 721 {
723 {"help", no_argument, 0, '?'}, 722 {"help", no_argument, 0, '?'},
724 {"debug", no_argument, 0, 'd'}, 723 {"debug", no_argument, 0, 'd'},
725 {"single-key", required_argument, 0, 's'}, 724 {"add-key", required_argument, 0, 'a'},
726 {"usb-otp", required_argument, 0, 'u'},
727 {0, 0, 0, 0} 725 {0, 0, 0, 0}
728 }; 726 };
729 727
730 int c = getopt_long(argc, argv, "?do:k:zr", long_options, NULL); 728 int c = getopt_long(argc, argv, "?do:k:zra:", long_options, NULL);
731 if(c == -1) 729 if(c == -1)
732 break; 730 break;
733 switch(c) 731 switch(c)
@@ -753,39 +751,14 @@ int main(int argc, char **argv)
753 add_keys(&g_zero_key, 1); 751 add_keys(&g_zero_key, 1);
754 break; 752 break;
755 } 753 }
756 case 's': 754 case 'a':
757 { 755 {
758 struct crypto_key_t key; 756 struct crypto_key_t key;
759 key.method = CRYPTO_KEY; 757 char *s = optarg;
760 if(strlen(optarg) != 32) 758 if(!parse_key(&s, &key))
761 bug("The key given in argument is invalid"); 759 bug("Invalid key specified as argument");
762 for(int i = 0; i < 16; i++) 760 if(*s != 0)
763 { 761 bug("Trailing characters after key specified as argument");
764 byte a, b;
765 if(convxdigit(optarg[2 * i], &a) || convxdigit(optarg[2 * i + 1], &b))
766 bugp("The key given in argument is invalid\n");
767 key.u.key[i] = (a << 4) | b;
768 }
769 add_keys(&key, 1);
770 break;
771 }
772 case 'u':
773 {
774 int vid, pid;
775 char *p = strchr(optarg, ':');
776 if(p == NULL)
777 bug("Invalid VID/PID\n");
778
779 char *end;
780 vid = strtol(optarg, &end, 16);
781 if(end != p)
782 bug("Invalid VID/PID\n");
783 pid = strtol(p + 1, &end, 16);
784 if(end != (optarg + strlen(optarg)))
785 bug("Invalid VID/PID\n");
786 struct crypto_key_t key;
787 key.method = CRYPTO_USBOTP;
788 key.u.vid_pid = vid << 16 | pid;
789 add_keys(&key, 1); 762 add_keys(&key, 1);
790 break; 763 break;
791 } 764 }
@@ -794,8 +767,14 @@ int main(int argc, char **argv)
794 } 767 }
795 } 768 }
796 769
770 if(g_out_prefix == NULL)
771 g_out_prefix = "";
772
797 if(argc - optind != 1) 773 if(argc - optind != 1)
798 bug("Missing sb file or too many files after options\n"); 774 {
775 usage();
776 return 1;
777 }
799 778
800 const char *sb_file = argv[optind]; 779 const char *sb_file = argv[optind];
801 FILE *fd = fopen(sb_file, "rb"); 780 FILE *fd = fopen(sb_file, "rb");