From: bg Date: Wed, 16 Oct 2013 00:04:39 +0000 (+0200) Subject: adding old command-line-tool X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=commitdiff_plain;h=14a4df0d4392a668439c879c89e135d5ac345688;ds=sidebyside adding old command-line-tool --- diff --git a/hostware/commandline/Makefile b/hostware/commandline/Makefile new file mode 100644 index 0000000..46cfbdd --- /dev/null +++ b/hostware/commandline/Makefile @@ -0,0 +1,48 @@ +# Name: Makefile +# Project: hid-custom-rq example +# Author: Christian Starkjohann +# Creation Date: 2008-04-06 +# Tabsize: 4 +# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH +# License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) +# This Revision: $Id: Makefile 692 2008-11-07 15:07:40Z cs $ + + +# Concigure the following definitions according to your system. +# This Makefile has been tested on Mac OS X, Linux and Windows. + +# Use the following 3 lines on Unix (uncomment the framework on Mac OS X): +USBFLAGS = `libusb-config --cflags` +USBLIBS = `libusb-config --libs` +EXE_SUFFIX = + +# Use the following 3 lines on Windows and comment out the 3 above. You may +# have to change the include paths to where you installed libusb-win32 +#USBFLAGS = -I/usr/local/include +#USBLIBS = -L/usr/local/lib -lusb +#EXE_SUFFIX = .exe + +NAME = badge-tool + +OBJECTS = opendevice.o hexdump.o main.o + +CC = gcc +CFLAGS = $(CPPFLAGS) $(USBFLAGS) -O -g -Wall +LIBS = $(USBLIBS) + +PROGRAM = $(NAME)$(EXE_SUFFIX) + + +all: $(PROGRAM) + +.c.o: + $(CC) $(CFLAGS) -c $< + +$(PROGRAM): $(OBJECTS) + $(CC) -o $(PROGRAM) $(OBJECTS) $(LIBS) + +strip: $(PROGRAM) + strip $(PROGRAM) + +clean: + rm -f *.o $(PROGRAM) diff --git a/hostware/commandline/hexdump.c b/hostware/commandline/hexdump.c new file mode 100644 index 0000000..6fb3742 --- /dev/null +++ b/hostware/commandline/hexdump.c @@ -0,0 +1,74 @@ +/* hexdump.c */ +/* + This file is part of the ARM-Crypto-Lib. + Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + +static +void int_hexdump_line_buffer(FILE* stream, uint8_t* buffer, unsigned width, unsigned fill){ + unsigned i; + for(i=0; iwidth){ + memcpy(buffer, block, width); + if(print_addr){ + fprintf(stream, "%8.8x ", (uint32_t)print_addr); + print_addr = (uint8_t*)print_addr + width; + } + fprintf(stream, "<%4.4x>: ", index); + int_hexdump_line_buffer(stream, buffer, width, width); + fputc('\n', stream); + block = (uint8_t*)block + width; + length -= width; + index += width; + } + memcpy(buffer, block, length); + if(print_addr){ + fprintf(stream, "%8.8x ", (uint32_t)print_addr); + } + fprintf(stream, "<%4.4x>: ", index); + int_hexdump_line_buffer(stream, buffer, width, length); + fputc('\n', stream); +} diff --git a/hostware/commandline/hexdump.h b/hostware/commandline/hexdump.h new file mode 100644 index 0000000..8b1cbcf --- /dev/null +++ b/hostware/commandline/hexdump.h @@ -0,0 +1,25 @@ +/* hexdump.h */ +/* + This file is part of the AVR-Crypto-Lib. + Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef HEXDUMP_H_ +#define HEXDUMP_H_ + +void hexdump_block(FILE* stream, void* block, void* print_addr, unsigned length, unsigned width); + +#endif /* HEXDUMP_H_ */ diff --git a/hostware/commandline/main.c b/hostware/commandline/main.c new file mode 100644 index 0000000..71d9b07 --- /dev/null +++ b/hostware/commandline/main.c @@ -0,0 +1,377 @@ +/* Name: set-led.c + * Project: hid-custom-rq example + * Author: Christian Starkjohann + * Creation Date: 2008-04-10 + * Tabsize: 4 + * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $ + */ + +/* +General Description: +This is the host-side driver for the custom-class example device. It searches +the USB for the LEDControl device and sends the requests understood by this +device. +This program must be linked with libusb on Unix and libusb-win32 on Windows. +See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/ +respectively. +*/ + +#include +#include +#include +#include +#include +#include /* this is libusb */ +#include +#include "hexdump.h" +#include "opendevice.h" /* common code moved to separate module */ + +#include "../../firmware/requests.h" /* custom request numbers */ +#include "../../firmware/usbconfig.h" /* device's VID/PID and names */ + +int safety_question_override=0; +int pad=-1; + +char* fname; +usb_dev_handle *handle = NULL; + +void set_rgb(char* color){ + uint16_t buffer[3] = {0, 0, 0}; + sscanf(color, "%hi:%hi:%hi", &(buffer[0]), &(buffer[1]), &(buffer[2])); + buffer[0] &= (1<<10)-1; + buffer[1] &= (1<<10)-1; + buffer[2] &= (1<<10)-1; + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_RGB, 0, 0, (char*)buffer, 6, 5000); +} + +void get_rgb(char* param){ + uint16_t buffer[3]; + int cnt; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_RGB, 0, 0, (char*)buffer, 6, 5000); + if(cnt!=6){ + fprintf(stderr, "ERROR: received %d bytes from device while expecting %d bytes\n", cnt, 6); + exit(1); + } + printf("red: %5hu\ngreen: %5hu\nblue: %5u\n", buffer[0], buffer[1], buffer[2]); +} + +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, (int)((unsigned)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] \n" + " where