]> git.cryptolib.org Git - labortage2013badge.git/blobdiff - hostware/commandline/main.c
some minor fixes
[labortage2013badge.git] / hostware / commandline / main.c
index 8b8b2b09bac3db85bf2286e3382eb2e5bebe5055..18e9547e483611010ffb550d6298f7bd5bb19734 100644 (file)
@@ -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<length){
-                       while(*data && !isxdigit(*data)){
-                               ++data;
-                       }
-                       xbuffer[fill++] = *data;
-                       if(fill==2){
-                               uint8_t t;
-                               t = strtoul(xbuffer, NULL, 16);
-                               buffer[idx++] = t;
-                               fill = 0;
-                       }
-               }
-
-       }
-       cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_WRITE_MEM, (intptr_t)addr, 0, (char*)buffer, length, 5000);
-       if(cnt!=length){
-               fprintf(stderr, "ERROR: device accepted ony %d bytes out of %d\n", cnt, length);
-               exit(1);
-       }
-
-}
-
-void read_flash(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_FLASH, (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 soft_reset(char* param){
        unsigned delay = 0;
        if (param) {
@@ -332,34 +185,21 @@ void soft_reset(char* param){
 }
 
 void read_button(char* param){
-       uint8_t v;
+       int8_t v;
        usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_BUTTON, 0, 0, (char*)&v, 1, 5000);
-       printf("button is %s\n",v?"on":"off");
+       printf("button is %s (%"PRId8")\n", v ? "on" : "off", v);
 }
 
-void wait_for_button(char* param){
-       volatile uint8_t v = 0, x = 1;
-       if(param){
-               printf("DBG: having param: %s\n", param);
-               if (!(strcmp(param,"off") && strcmp(param,"0"))) {
-                       x = 0;
-               }
-       }
-       do{
-               usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_BUTTON, 0, 0, (char*)&v, 1, 5000);
-       }while(x!=v);
-       printf("button is %s\n",v?"on":"off");
-}
-
-void read_temperature(char* param){
-       uint16_t v;
-       int cnt;
-       cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_READ_TMPSENS, 0, 0, (char*)&v, 2, 5000);
-       if (cnt == 2) {
-           printf("temperature raw value: %hd 0x%hx\n", v, v);
-       } else {
-        fprintf(stderr, "Error: reading %d bytes for temperature, expecting 2\n", cnt);
-       }
+void get_secret(char *param){
+    uint8_t buffer[64];
+    int cnt;
+    cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_SECRET, 0, 0, (char*)buffer, 32, 5000);
+    if (cnt > 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 <data>.......... set content of the debug register (<data> 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;
        }