diff --git a/cli/mfg.c b/cli/mfg.c index 99d99ec..41147ab 100755 --- a/cli/mfg.c +++ b/cli/mfg.c @@ -2306,14 +2306,14 @@ static int sjtag_unlock(int argc, char **argv) struct switchtec_sn_ver_info sn_info = {}; uint8_t sjtag_hr[SJTAG_HR_LEN]; struct sjtag_debug_token debug_token; - FILE *debug_token_bin_file; - char dir_path[1024]; int ret; static struct { struct switchtec_dev *dev; FILE *sjtag_debug_token; const char *sjtag_debug_token_file; + int out_fd; + const char *out_filename; bool verbose; bool force_hr; } cfg = { @@ -2322,17 +2322,29 @@ static int sjtag_unlock(int argc, char **argv) const struct argconfig_options opts[] = { DEVICE_OPTION_MFG, - {"sjtag_debug_token_file", - .cfg_type=CFG_FILE_R, - .value_addr=&cfg.sjtag_debug_token, - .argument_type=optional_positional, - .help="Optional Argument. If not provided, the Debug Token will be generated\n"\ - "SJTAG Debug Token File(.bin)\n" \ - "Generated by the HSM Server\n"}, - {"verbose", 'v', "", CFG_NONE, &cfg.verbose, no_argument, - "print additional sjtag-unlock information"}, - {"force_hr", 'f', "", CFG_NONE, &cfg.force_hr, no_argument, - "send HR for all SJTAG mode"}, + { + "debug_token_input_file", 'i', + .cfg_type=CFG_FILE_R, + .value_addr=&cfg.sjtag_debug_token, + .argument_type=required_argument, + .help="Optional Argument. If not provided, the Debug Token will be generated\n" + }, + { + "debug_token_output_file", 'o', + .cfg_type=CFG_FD_WR, + .value_addr=&cfg.out_fd, + .argument_type=required_argument, + .force_default="sjtag_debug_token.bin", + .help="Optional Argument. If not provided, generated Debug Token File will be named sjtag_debug_token.bin\n" + }, + { + "verbose", 'v', "", CFG_NONE, &cfg.verbose, no_argument, + "print additional sjtag-unlock information" + }, + { + "force_hr", 'f', "", CFG_NONE, &cfg.force_hr, no_argument, + "send HR for all SJTAG mode" + }, {NULL} }; @@ -2400,18 +2412,16 @@ static int sjtag_unlock(int argc, char **argv) break; } - debug_token_bin_file = fopen("sjtag_debug_token.bin", "wb"); - if (NULL == debug_token_bin_file) + /* Save the generated Debug Token as a binary */ + ret = write(cfg.out_fd, debug_token.debug_token, SJTAG_DEBUG_TOKEN_LEN); + if (ret < 0) { - switchtec_perror("Error opening file"); - ret = -1; - break; - } - - fwrite(debug_token.debug_token, sizeof(uint8_t), SJTAG_DEBUG_TOKEN_LEN, debug_token_bin_file); - fclose(debug_token_bin_file); - getcwd(dir_path, sizeof(dir_path)); - printf("Generated SJTAG Debug Token Path: %s/sjtag_debug_token.bin\n", dir_path); + switchtec_perror("Error saving the Debug token"); + } + else + { + fprintf(stderr, "\nGenerated SJTAG Debug token saved to %s\n", cfg.out_filename); + } } ret = switchtec_sjtag_get_nonce(cfg.dev, &nonce);