summaryrefslogtreecommitdiff
path: root/utils/nwztools/scsitools/scsitool.c
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-04 17:10:48 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-01-04 17:14:31 +0100
commitad2297d353412dd20fc439b8053b25b18872069c (patch)
tree89df80995147a2b5eec43eba6154184171349b5a /utils/nwztools/scsitools/scsitool.c
parentdbeb6db1b55a50dedf17e7d78ddb6fe9eebc2a63 (diff)
downloadrockbox-ad2297d353412dd20fc439b8053b25b18872069c.tar.gz
rockbox-ad2297d353412dd20fc439b8053b25b18872069c.zip
nwztools/scsitool: add option to force/specify series
We cannot auto-detect a device if we don't know its model ID, but we can't know the model ID if we haven't decrypted the upgrade which requires the key. The only way to solve this chicken-and-egg problem is to get the NVP table from kernel, create an empty series in the database (no model ID), then get the key using scsitool and forcing the model (using this commit), then decrypt the upgrade and get the model ID list. Change-Id: I8eced486a5f6a1a99028b25fdc4f87a3b11e31a8
Diffstat (limited to 'utils/nwztools/scsitools/scsitool.c')
-rw-r--r--utils/nwztools/scsitools/scsitool.c118
1 files changed, 66 insertions, 52 deletions
diff --git a/utils/nwztools/scsitools/scsitool.c b/utils/nwztools/scsitools/scsitool.c
index 3bff9b90fd..02fa2efe6a 100644
--- a/utils/nwztools/scsitools/scsitool.c
+++ b/utils/nwztools/scsitools/scsitool.c
@@ -46,20 +46,10 @@
46#endif 46#endif
47 47
48bool g_debug = false; 48bool g_debug = false;
49bool g_force = false; 49const char *g_force_series = NULL;
50char *g_out_prefix = NULL; 50char *g_out_prefix = NULL;
51int g_dev_fd = 0; 51int g_dev_fd = 0;
52 52
53#define let_the_force_flow(x) do { if(!g_force) return x; } while(0)
54#define continue_the_force(x) if(x) let_the_force_flow(x)
55
56#define check_field(v_exp, v_have, str_ok, str_bad) \
57 if((v_exp) != (v_have)) \
58 { cprintf(RED, str_bad); let_the_force_flow(__LINE__); } \
59 else { cprintf(RED, str_ok); }
60
61#define errorf(...) do { cprintf(GREY, __VA_ARGS__); return __LINE__; } while(0)
62
63#if 0 53#if 0
64void *buffer_alloc(int sz) 54void *buffer_alloc(int sz)
65{ 55{
@@ -362,42 +352,66 @@ int get_dnk_prop(int argc, char **argv)
362 352
363int get_model_and_series(int *model_index, int *series_index) 353int get_model_and_series(int *model_index, int *series_index)
364{ 354{
365 /* we need to get the model ID: code stolen from get_dnk_prop */ 355 /* if the user forced the series, simply match by name, special for '?' which
366 uint8_t mid_buf[4]; 356 * prompts the list */
367 int mid_buf_size = sizeof(mid_buf); 357 if(g_force_series)
368 int ret = do_dnk_cmd(true, 0x23, 9, 0, mid_buf, &mid_buf_size); 358 {
369 if(ret) 359 cprintf(RED, "User forced series, auto-detection disabled\n");
370 { 360 *series_index = -1;
371 printf("Cannot get model ID from device: %d\n", ret); 361 *model_index = -1;
372 return 2; 362 for(int i = 0; i < NWZ_SERIES_COUNT; i++)
363 if(strcmp(nwz_series[i].codename, g_force_series) == 0)
364 *series_index = i;
365 /* display list on error */
366 if(*series_index == -1)
367 {
368 if(strcmp(g_force_series, "?") != 0)
369 cprintf(GREY, "Unrecognized series '%s'\n", g_force_series);
370 cprintf(OFF, "Series list:\n");
371 for(int i = 0; i < NWZ_SERIES_COUNT; i++)
372 printf(" %-10s %s\n", nwz_series[i].codename, nwz_series[i].name);
373 return -1;
374 }
373 } 375 }
374 if(mid_buf_size != sizeof(mid_buf)) 376 else
375 { 377 {
376 printf("Cannot get model ID from device: device didn't send the expected amount of data\n"); 378 /* we need to get the model ID: code stolen from get_dnk_prop */
377 return 3; 379 uint8_t mid_buf[4];
380 int mid_buf_size = sizeof(mid_buf);
381 int ret = do_dnk_cmd(true, 0x23, 9, 0, mid_buf, &mid_buf_size);
382 if(ret)
383 {
384 cprintf(RED, "Cannot get model ID from device: %d\n", ret);
385 return 2;
386 }
387 if(mid_buf_size != sizeof(mid_buf))
388 {
389 cprintf(RED, "Cannot get model ID from device: device didn't send the expected amount of data\n");
390 return 3;
391 }
392 unsigned long model_id = get_big_endian32(&mid_buf);
393 *model_index = -1;
394 for(int i = 0; i < NWZ_MODEL_COUNT; i++)
395 if(nwz_model[i].mid == model_id)
396 *model_index = i;
397 if(*model_index == -1)
398 {
399 cprintf(RED, "Your device is not supported. Please contact developers.\n");
400 return 3;
401 }
402 *series_index = -1;
403 for(int i = 0; i < NWZ_SERIES_COUNT; i++)
404 for(int j = 0; j < nwz_series[i].mid_count; j++)
405 if(nwz_series[i].mid[j] == model_id)
406 *series_index = i;
407 if(*series_index == -1)
408 {
409 printf("Your device is not supported. Please contact developers.\n");
410 return 3;
411 }
378 } 412 }
379 unsigned long model_id = get_big_endian32(&mid_buf);
380 *model_index = -1;
381 for(int i = 0; i < NWZ_MODEL_COUNT; i++)
382 if(nwz_model[i].mid == model_id)
383 *model_index = i;
384 cprintf_field("Model: ", "%s\n", *model_index == -1 ? "Unknown" : nwz_model[*model_index].name); 413 cprintf_field("Model: ", "%s\n", *model_index == -1 ? "Unknown" : nwz_model[*model_index].name);
385 if(*model_index == -1)
386 {
387 printf("Your device is not supported. Please contact developers.\n");
388 return 3;
389 }
390 *series_index = -1;
391 for(int i = 0; i < NWZ_SERIES_COUNT; i++)
392 for(int j = 0; j < nwz_series[i].mid_count; j++)
393 if(nwz_series[i].mid[j] == model_id)
394 *series_index = i;
395 cprintf_field("Series: ", "%s\n", *series_index == -1 ? "Unknown" : nwz_series[*series_index].name); 414 cprintf_field("Series: ", "%s\n", *series_index == -1 ? "Unknown" : nwz_series[*series_index].name);
396 if(*series_index == -1)
397 {
398 printf("Your device is not supported. Please contact developers.\n");
399 return 3;
400 }
401 return 0; 415 return 0;
402} 416}
403 417
@@ -847,11 +861,11 @@ static void usage(void)
847{ 861{
848 printf("Usage: scsitool [options] <dev> <command> [arguments]\n"); 862 printf("Usage: scsitool [options] <dev> <command> [arguments]\n");
849 printf("Options:\n"); 863 printf("Options:\n");
850 printf(" -o <prefix>\tSet output prefix\n"); 864 printf(" -o <prefix> Set output prefix\n");
851 printf(" -f/--force\tForce to continue on errors\n"); 865 printf(" -?/--help Display this message\n");
852 printf(" -?/--help\tDisplay this message\n"); 866 printf(" -d/--debug Display debug messages\n");
853 printf(" -d/--debug\tDisplay debug messages\n"); 867 printf(" -c/--no-color Disable color output\n");
854 printf(" -c/--no-color\tDisable color output\n"); 868 printf(" -s/--series <name> Force series (disable auto-detection, use '?' for the list)\n");
855 printf("Commands:\n"); 869 printf("Commands:\n");
856 for(unsigned i = 0; i < NR_CMDS; i++) 870 for(unsigned i = 0; i < NR_CMDS; i++)
857 printf(" %s\t%s\n", cmd_list[i].name, cmd_list[i].desc); 871 printf(" %s\t%s\n", cmd_list[i].name, cmd_list[i].desc);
@@ -867,11 +881,11 @@ int main(int argc, char **argv)
867 {"help", no_argument, 0, '?'}, 881 {"help", no_argument, 0, '?'},
868 {"debug", no_argument, 0, 'd'}, 882 {"debug", no_argument, 0, 'd'},
869 {"no-color", no_argument, 0, 'c'}, 883 {"no-color", no_argument, 0, 'c'},
870 {"force", no_argument, 0, 'f'}, 884 {"series", required_argument, 0, 's'},
871 {0, 0, 0, 0} 885 {0, 0, 0, 0}
872 }; 886 };
873 887
874 int c = getopt_long(argc, argv, "?dcfo:", long_options, NULL); 888 int c = getopt_long(argc, argv, "?dcfo:s:", long_options, NULL);
875 if(c == -1) 889 if(c == -1)
876 break; 890 break;
877 switch(c) 891 switch(c)
@@ -884,15 +898,15 @@ int main(int argc, char **argv)
884 case 'd': 898 case 'd':
885 g_debug = true; 899 g_debug = true;
886 break; 900 break;
887 case 'f':
888 g_force = true;
889 break;
890 case '?': 901 case '?':
891 usage(); 902 usage();
892 break; 903 break;
893 case 'o': 904 case 'o':
894 g_out_prefix = optarg; 905 g_out_prefix = optarg;
895 break; 906 break;
907 case 's':
908 g_force_series = optarg;
909 break;
896 default: 910 default:
897 abort(); 911 abort();
898 } 912 }