proxmark3/client/flasher.c

164 lines
4.1 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// Flasher frontend tool
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sleep.h"
#include "proxmark3.h"
#include "flash.h"
#include "uart.h"
#include "usb_cmd.h"
#ifdef _WIN32
# define unlink(x)
#endif
static serial_port sp;
static char* serial_port_name;
void cmd_debug(UsbCommand* UC) {
// Debug
printf("UsbCommand length[len=%zd]\n",sizeof(UsbCommand));
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]);
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);
// Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
unlink(serial_port_name);
}
int OpenProxmark(size_t i) {
sp = uart_open(serial_port_name);
if (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT) {
2013-03-04 23:15:30 +08:00
//poll once a second
return 0;
}
return 1;
}
static void usage(char *argv0)
{
fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
fprintf(stderr, "\t-b\tEnable flashing of bootloader area (DANGEROUS)\n\n");
//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, "\nNote (Linux): if the flasher gets stuck in 'Waiting for Proxmark to reappear on USB', try deactivating modem-manager: 'sudo service modemmanager stop'\n\n");
}
#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)
{
int can_write_bl = 0;
int num_files = 0;
int res;
flash_file_t files[MAX_FILES];
memset(files, 0, sizeof(files));
if (argc < 3) {
usage(argv[0]);
return -1;
}
for (int i = 2; i < argc; i++) {
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++;
}
}
serial_port_name = argv[1];
fprintf(stderr,"Waiting for Proxmark to appear on USB...");
do {
sleep(1);
fprintf(stderr, ".");
} while (!OpenProxmark(0));
fprintf(stderr," Found.\n");
res = flash_start_flashing(can_write_bl);
if (res < 0)
return -1;
fprintf(stderr, "\nFlashing...\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;
}