X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=hostware%2Fcommandline%2Fmain.c;h=18e9547e483611010ffb550d6298f7bd5bb19734;hp=8b8b2b09bac3db85bf2286e3382eb2e5bebe5055;hb=46e9b3d1c8a23c3884c8edf4ace96ff650333648;hpb=bef15b51e1f41aa3fc159ce2aa439182a393fd9c diff --git a/hostware/commandline/main.c b/hostware/commandline/main.c index 8b8b2b0..18e9547 100644 --- a/hostware/commandline/main.c +++ b/hostware/commandline/main.c @@ -78,7 +78,7 @@ void set_dbg(char *hex_string){ } void get_dbg(char *param){ - uint16_t buffer[256]; + uint8_t buffer[256]; int cnt; cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_DBG, 0, 0, (char*)buffer, 256, 5000); printf("DBG-Buffer:\n"); @@ -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 0) { + printf("Secret (%d):\n", cnt); + hexdump_block(stdout, buffer, NULL, cnt, 16); + } else { + fprintf(stderr, "Error: usb_control_msg(...) returned %d\n", cnt); + } } @@ -381,6 +221,7 @@ static struct option long_options[] = {"get-dbg", no_argument, NULL, 'x'}, {"set-dbg", required_argument, NULL, 'y'}, {"clr-dbg", no_argument, NULL, 'z'}, + {"get-secret", no_argument, NULL, 'S'}, {0, 0, 0, 0} }; @@ -404,7 +245,7 @@ static void usage(char *name) " -x --get-dbg ................ get content of the debug register\n" " -y --set-dbg .......... 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 +281,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 +326,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; }