2010-02-21 08:47:22 +08:00
//-----------------------------------------------------------------------------
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
2010-02-26 22:03:43 +08:00
// Flasher frontend tool
2010-02-21 08:47:22 +08:00
//-----------------------------------------------------------------------------
2009-04-09 14:43:20 +08:00
# include <stdio.h>
2010-02-06 17:33:17 +08:00
# include <stdlib.h>
2010-02-26 22:03:43 +08:00
# include <string.h>
2010-02-21 07:35:09 +08:00
# include "sleep.h"
2013-03-01 06:22:24 +08:00
# include "proxmark3.h"
2010-01-04 13:11:08 +08:00
# include "flash.h"
2013-02-28 23:11:52 +08:00
# include "uart.h"
# include "usb_cmd.h"
2013-03-21 18:59:56 +08:00
# ifdef _WIN32
# define unlink(x)
# endif
2013-02-28 23:11:52 +08:00
static serial_port sp ;
static char * serial_port_name ;
void cmd_debug ( UsbCommand * UC ) {
// Debug
printf ( " UsbCommand length[len=%zd] \n " , sizeof ( UsbCommand ) ) ;
2013-03-01 06:22:24 +08:00
printf ( " cmd[len=%zd]: %016 " llx " \n " , sizeof ( UC - > cmd ) , UC - > cmd ) ;
printf ( " arg0[len=%zd]: %016 " llx " \n " , sizeof ( UC - > arg [ 0 ] ) , UC - > arg [ 0 ] ) ;
printf ( " arg1[len=%zd]: %016 " llx " \n " , sizeof ( UC - > arg [ 1 ] ) , UC - > arg [ 1 ] ) ;
printf ( " arg2[len=%zd]: %016 " llx " \n " , sizeof ( UC - > arg [ 2 ] ) , UC - > arg [ 2 ] ) ;
2013-02-28 23:11:52 +08:00
printf ( " data[len=%zd]: " , sizeof ( UC - > d . asBytes ) ) ;
for ( size_t i = 0 ; i < 16 ; i + + ) {
printf ( " %02x " , UC - > d . asBytes [ i ] ) ;
}
printf ( " ... \n " ) ;
}
void SendCommand ( UsbCommand * txcmd ) {
// printf("send: ");
// cmd_debug(txcmd);
if ( ! uart_send ( sp , ( byte_t * ) txcmd , sizeof ( UsbCommand ) ) ) {
printf ( " Sending bytes to proxmark failed \n " ) ;
exit ( 1 ) ;
}
}
void ReceiveCommand ( UsbCommand * rxcmd ) {
byte_t * prxcmd = ( byte_t * ) rxcmd ;
byte_t * prx = prxcmd ;
size_t rxlen ;
while ( true ) {
rxlen = sizeof ( UsbCommand ) - ( prx - prxcmd ) ;
if ( uart_receive ( sp , prx , & rxlen ) ) {
// printf("received [%zd] bytes\n",rxlen);
prx + = rxlen ;
if ( ( prx - prxcmd ) > = sizeof ( UsbCommand ) ) {
// printf("received: ");
// cmd_debug(rxcmd);
return ;
}
}
}
}
void CloseProxmark ( ) {
// Clean up the port
uart_close ( sp ) ;
2013-03-21 18:59:56 +08:00
// Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
unlink ( serial_port_name ) ;
2013-02-28 23:11:52 +08:00
}
int OpenProxmark ( size_t i ) {
sp = uart_open ( serial_port_name ) ;
2013-03-27 00:01:52 +08:00
if ( sp = = INVALID_SERIAL_PORT | | sp = = CLAIMED_SERIAL_PORT ) {
2013-03-04 23:15:30 +08:00
//poll once a second
2013-02-28 23:11:52 +08:00
return 0 ;
}
return 1 ;
}
2009-04-09 14:43:20 +08:00
2010-02-26 22:03:43 +08:00
static void usage ( char * argv0 )
2009-09-01 22:35:13 +08:00
{
2013-02-28 23:11:52 +08:00
fprintf ( stderr , " Usage: %s <port> [-b] image.elf [image.elf...] \n \n " , argv0 ) ;
2010-02-26 22:03:43 +08:00
fprintf ( stderr , " \t -b \t Enable flashing of bootloader area (DANGEROUS) \n \n " ) ;
2013-05-11 20:43:44 +08:00
//Is the example below really true? /Martin
fprintf ( stderr , " Example: \n \t %s path/to/osimage.elf path/to/fpgaimage.elf \n " , argv0 ) ;
fprintf ( stderr , " Example (Linux): \n \t %s /dev/ttyACM0 armsrc/obj/fullimage.elf \n " , argv0 ) ;
fprintf ( stderr , " \n Note (Linux): if the flasher gets stuck in 'Waiting for Proxmark to reappear on USB', try deactivating modem-manager: 'sudo service modemmanager stop' \n \n " ) ;
2009-09-01 22:35:13 +08:00
}
2009-04-09 14:43:20 +08:00
2010-02-26 22:03:43 +08:00
# define MAX_FILES 4
Client cleanup and restructuring. Stage 1...
Next Step is refactoring some of the giant functions which are
just copy/paste of some other ones with just a few line changes,
removing unnecessary 'goto' etc.
The MS Windows version is broken with this commit but will be fixed
soon. Everything can't be done all at once :P
The commands are now hierarchical, for example:
"hf 14a read" vs. "hf 14b read".
You can also request help:
"hf help", "data help", "hf 15 help" etc.
Indents are now space-based, not tab-based anymore. Hopefully
no one will be trolling about it, considering the suicide-prone work
being done here ;)
client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h,
client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h,
client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c,
client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h,
client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h,
client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h,
client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h,
client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h,
client/cmdhflegic.c, client/cmdhflegic.c: New files.
client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c,
client/proxmark3.h, client/Makefile: Update accordingly.
client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes.
client/translate.h, client/command.c, client/gui.c,
client/usb.c, client/prox.h: Remove.
include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd.
common/crc16.h: New file.
common/crc16.c: Modify accordingly.
common/iso14443crc.h: New file.
common/iso14443_crc.c: Rename to
common/iso14443crc.c: and modify accordingly.
armsrc/lfops.c, armsrc/iso14443.c,
armsrc/iso14443a.c: include .h files from
the common directory instead of including the c files.
common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
int main ( int argc , char * * argv )
{
2010-02-26 22:03:43 +08:00
int can_write_bl = 0 ;
int num_files = 0 ;
int res ;
flash_file_t files [ MAX_FILES ] ;
memset ( files , 0 , sizeof ( files ) ) ;
2013-02-28 23:11:52 +08:00
if ( argc < 3 ) {
2010-02-26 22:03:43 +08:00
usage ( argv [ 0 ] ) ;
return - 1 ;
}
2013-02-28 23:11:52 +08:00
for ( int i = 2 ; i < argc ; i + + ) {
2010-02-26 22:03:43 +08:00
if ( argv [ i ] [ 0 ] = = ' - ' ) {
if ( ! strcmp ( argv [ i ] , " -b " ) ) {
can_write_bl = 1 ;
} else {
usage ( argv [ 0 ] ) ;
return - 1 ;
}
} else {
res = flash_load ( & files [ num_files ] , argv [ i ] , can_write_bl ) ;
if ( res < 0 ) {
fprintf ( stderr , " Error while loading %s \n " , argv [ i ] ) ;
return - 1 ;
}
fprintf ( stderr , " \n " ) ;
num_files + + ;
}
}
2013-02-28 23:11:52 +08:00
serial_port_name = argv [ 1 ] ;
2013-03-22 00:06:09 +08:00
fprintf ( stderr , " Waiting for Proxmark to appear on USB... " ) ;
do {
sleep ( 1 ) ;
fprintf ( stderr , " . " ) ;
} while ( ! OpenProxmark ( 0 ) ) ;
fprintf ( stderr , " Found. \n " ) ;
2010-02-26 22:03:43 +08:00
res = flash_start_flashing ( can_write_bl ) ;
if ( res < 0 )
return - 1 ;
fprintf ( stderr , " \n Flashing... \n " ) ;
for ( int i = 0 ; i < num_files ; i + + ) {
res = flash_write ( & files [ i ] ) ;
if ( res < 0 )
return - 1 ;
flash_free ( & files [ i ] ) ;
fprintf ( stderr , " \n " ) ;
}
fprintf ( stderr , " Resetting hardware... \n " ) ;
res = flash_stop_flashing ( ) ;
if ( res < 0 )
return - 1 ;
CloseProxmark ( ) ;
fprintf ( stderr , " All done. \n \n " ) ;
fprintf ( stderr , " Have a nice day! \n " ) ;
return 0 ;
2009-04-09 14:43:20 +08:00
}