X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=hostware%2Fcommandline%2Fmain.c;h=202dbeea6e92390411421fade9c54251631a3281;hp=8b8b2b09bac3db85bf2286e3382eb2e5bebe5055;hb=48646597db7ce609b6a3e0ba3d44a4723587cc0c;hpb=bef15b51e1f41aa3fc159ce2aa439182a393fd9c;ds=sidebyside diff --git a/hostware/commandline/main.c b/hostware/commandline/main.c index 8b8b2b0..202dbee 100644 --- a/hostware/commandline/main.c +++ b/hostware/commandline/main.c @@ -106,6 +106,7 @@ void set_secret(char *hex_string){ } else { buffer[length] |= t << 4; } + ++i; } usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_SECRET, length * 8, 0, (char*)buffer, length, 5000); @@ -174,154 +175,6 @@ void get_token(char *param){ } } -void read_mem(char* param){ - int length=0; - uint8_t *buffer, *addr; - int cnt; - FILE* f=NULL; - if(fname){ - f = fopen(fname, "wb"); - if(!f){ - fprintf(stderr, "ERROR: could not open %s for writing\n", fname); - exit(1); - } - } - sscanf(param, "%i:%i", (int*)&addr, &length); - if(length<=0){ - return; - } - buffer = malloc(length); - if(!buffer){ - if(f) - fclose(f); - fprintf(stderr, "ERROR: out of memory\n"); - exit(1); - } - cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_MEM, (intptr_t)addr, 0, (char*)buffer, length, 5000); - if(cnt!=length){ - if(f) - fclose(f); - fprintf(stderr, "ERROR: received %d bytes from device while expecting %d bytes\n", cnt, length); - exit(1); - } - if(f){ - cnt = fwrite(buffer, 1, length, f); - fclose(f); - if(cnt!=length){ - fprintf(stderr, "ERROR: could write only %d bytes out of %d bytes\n", cnt, length); - exit(1); - } - - }else{ - hexdump_block(stdout, buffer, addr, length, 8); - } -} - -void write_mem(char* param){ - int length; - uint8_t *addr, *buffer, *data=NULL; - int cnt=0; - FILE* f=NULL; - - if(fname){ - f = fopen(fname, "rb"); - if(!f){ - fprintf(stderr, "ERROR: could not open %s for writing\n", fname); - exit(1); - } - } - sscanf(param, "%i:%i:%n", (int*)&addr, &length, &cnt); - data += cnt; - if(length<=0){ - return; - } - buffer = malloc(length); - if(!buffer){ - if(f) - fclose(f); - fprintf(stderr, "ERROR: out of memory\n"); - exit(1); - } - memset(buffer, (uint8_t)pad, length); - if(!data && !f && length==0){ - fprintf(stderr, "ERROR: no data to write\n"); - exit(1); - } - if(f){ - cnt = fread(buffer, 1, length, f); - fclose(f); - if(cnt!=length && pad==-1){ - fprintf(stderr, "Warning: could ony read %d bytes from file; will only write these bytes", cnt); - } - }else if(data){ - char xbuffer[3]= {0, 0, 0}; - uint8_t fill=0; - unsigned idx=0; - while(*data && idx.......... set content of the debug register ( is a byte squence in hex of max. 8 bytes)\n" " -z --clr-dbg ................ clear the content of the debug register\n" - + " -S --get-secret ............. get secret (deactivated for productive devices)\n\n" " Please note:\n" " If you use optional parameters you have to use two different way to specify the parameter,\n" " depending on if you use short or long options.\n" @@ -440,12 +277,12 @@ int main(int argc, char **argv) } for (;;) { - c = getopt_long(argc, argv, "s:icrqDd:R::tbBxy:z", long_options, &option_index); + c = getopt_long(argc, argv, "s:icrqDd:R::tbBxy:zS", long_options, &option_index); if (c == -1) { break; } - if (action_fn && strchr("sicrqDdRtbBxyz", c)) { + if (action_fn && strchr("sicrqDdRtbBxyzS", c)) { /* action given while already having an action */ usage(argv[0]); exit(1); @@ -485,6 +322,7 @@ int main(int argc, char **argv) case 'y': action_fn = set_dbg; break; case 'x': action_fn = get_dbg; break; case 'z': action_fn = clr_dbg; break; + case 'S': action_fn = get_secret; break; default: break; }