From: bg Date: Sat, 19 Oct 2013 15:31:24 +0000 (+0200) Subject: modifying cmd-tool for new commands X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=commitdiff_plain;h=bef15b51e1f41aa3fc159ce2aa439182a393fd9c;hp=9774c6cbab2971904929c1ad28c34664b355c3f9;ds=sidebyside modifying cmd-tool for new commands --- diff --git a/hostware/commandline/main.c b/hostware/commandline/main.c index 917dbac..8b8b2b0 100644 --- a/hostware/commandline/main.c +++ b/hostware/commandline/main.c @@ -21,6 +21,7 @@ respectively. #include #include #include +#include #include #include #include /* this is libusb */ @@ -50,7 +51,7 @@ int8_t hex_to_int(char c) { return r; } -void press_button(char* param){ +void press_button(char *param){ usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_PRESS_BUTTON, 0, 0, NULL, 0, 5000); } @@ -76,7 +77,7 @@ void set_dbg(char *hex_string){ usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_DBG, 0, 0, (char*)buffer, length, 5000); } -void get_dbg(char* param){ +void get_dbg(char *param){ uint16_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); @@ -84,6 +85,95 @@ void get_dbg(char* param){ hexdump_block(stdout, buffer, 0, cnt, 16); } +void clr_dbg(char *param){ + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_CLR_DBG, 0, 0, NULL, 0, 5000); +} + +void set_secret(char *hex_string){ + uint8_t buffer[(strlen(hex_string) + 1) / 2]; + size_t i = 0, length = 0; + int8_t t; + + memset(buffer, 0, (strlen(hex_string) + 1) / 2); + + while (hex_string[i]) { + t = hex_to_int(hex_string[i]); + if (t == -1){ + break; + } + if (i & 1) { + buffer[length++] |= t; + } else { + buffer[length] |= t << 4; + } + } + + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_SECRET, length * 8, 0, (char*)buffer, length, 5000); +} + +void inc_counter(char *param){ + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_INC_COUNTER, 0, 0, NULL, 0, 5000); +} + +void get_counter(char *param){ + uint32_t counter; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_COUNTER, 0, 0, (char*)&counter, 4, 5000); + if (cnt == 4) { + printf("internal counter = %"PRId32"\n", counter); + } else { + fprintf(stderr, "Error: reading %d bytes for counter, expecting 4\n", cnt); + } +} + +void reset_counter(char *param){ + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_RESET_COUNTER, 0, 0, NULL, 0, 5000); +} + +void get_reset_counter (char *param){ + uint8_t counter; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_RESET_COUNTER, 0, 0, (char*)&counter, 1, 5000); + if (cnt == 1) { + printf("internal reset counter = %"PRId8"\n", counter); + } else { + fprintf(stderr, "Error: reading %d bytes for reset counter, expecting 1\n", cnt); + } +} + +void get_digits(char *param){ + uint8_t digits; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_DIGITS, 0, 0, (char*)&digits, 1, 5000); + if (cnt == 1) { + printf("digits = %"PRId8"\n", digits); + } else { + fprintf(stderr, "Error: reading %d bytes for reset counter, expecting 1\n", cnt); + } +} + +void set_digits(char *param){ + long d; + d = strtol(param, NULL, 10); + if (d > 9 || d < 6) { + fprintf(stderr, "Error: must be in range 6, 7, 8 or 9\n"); + return; + } + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_SET_DIGITS, d, 0, NULL, 0, 5000); +} + +void get_token(char *param){ + char token[10]; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_TOKEN, 0, 0, token, 9, 5000); + if (cnt < 9 ) { + token[cnt] = '\0'; + printf("token = %s\n", token); + } else { + fprintf(stderr, "Error: reading %d bytes for token, expecting max. 9\n", cnt); + } +} + void read_mem(char* param){ int length=0; uint8_t *buffer, *addr; @@ -189,52 +279,52 @@ void write_mem(char* param){ } -void read_flash(char* param){ +void read_flash(char* param) { int length=0; uint8_t *buffer, *addr; int cnt; FILE* f=NULL; - if(fname){ + if (fname) { f = fopen(fname, "wb"); - if(!f){ + 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){ + if (length <= 0){ return; } buffer = malloc(length); - if(!buffer){ - if(f) + 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) + 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){ + if (f) { cnt = fwrite(buffer, 1, length, f); fclose(f); - if(cnt!=length){ + if (cnt != length) { fprintf(stderr, "ERROR: could write only %d bytes out of %d bytes\n", cnt, length); exit(1); } - }else{ + } else { hexdump_block(stdout, buffer, addr, length, 8); } } void soft_reset(char* param){ - unsigned delay=0; - if(param){ + unsigned delay = 0; + if (param) { sscanf(param, "%i", &delay); } delay &= 0xf; @@ -248,10 +338,10 @@ void read_button(char* param){ } void wait_for_button(char* param){ - volatile uint8_t v=0, x=1; + volatile uint8_t v = 0, x = 1; if(param){ printf("DBG: having param: %s\n", param); - if(!(strcmp(param,"off") && strcmp(param,"0"))){ + if (!(strcmp(param,"off") && strcmp(param,"0"))) { x = 0; } } @@ -275,24 +365,22 @@ void read_temperature(char* param){ static struct option long_options[] = { - /* These options set a flag. */ - {"i-am-sure", no_argument, &safety_question_override, 1}, - {"pad", optional_argument, 0, 'p'}, /* These options don't set a flag. We distinguish them by their indices. */ - {"set-rgb", required_argument, 0, 's'}, - {"get-rgb", no_argument, 0, 'g'}, - {"read-mem", required_argument, 0, 'r'}, - {"write-mem", required_argument, 0, 'w'}, - {"read-flash", required_argument, 0, 'z'}, - {"exec-spm", required_argument, 0, 'x'}, - {"read-adc", required_argument, 0, 'a'}, - {"reset", optional_argument, 0, 'q'}, - {"read-button", required_argument, 0, 'b'}, - {"wait-for-button", optional_argument, 0, 'k'}, - {"read-temperature", no_argument, 0, 't'}, - {"file", required_argument, 0, 'f'}, - {"loop", required_argument, 0, 'l'}, + {"set-secret", required_argument, NULL, 's'}, + {"inc-counter", no_argument, NULL, 'i'}, + {"get-counter", no_argument, NULL, 'c'}, + {"reset-counter", no_argument, NULL, 'r'}, + {"get-reset-counter", no_argument, NULL, 'q'}, + {"get-digits", no_argument, NULL, 'D'}, + {"set-digits", required_argument, NULL, 'd'}, + {"reset", optional_argument, NULL, 'R'}, + {"get-token", no_argument, NULL, 't'}, + {"read-button", no_argument, NULL, 'b'}, + {"press-button", no_argument, NULL, 'B'}, + {"get-dbg", no_argument, NULL, 'x'}, + {"set-dbg", required_argument, NULL, 'y'}, + {"clr-dbg", no_argument, NULL, 'z'}, {0, 0, 0, 0} }; @@ -300,29 +388,28 @@ static void usage(char *name) { char *usage_str = "usage:\n" - " %s [