Initial commit for the firmware. Used the 20090306_ela version as baseline.

It is identical to the popular 20081211, with the doob addition (20090301), a
linux client, and two additional commands for LF analysis. Let me know if
you find issues here!
This commit is contained in:
edouard@lafargue.name 2009-04-09 06:43:20 +00:00
commit 6658905f18
91 changed files with 16661 additions and 0 deletions

29
linux/Makefile Normal file
View file

@ -0,0 +1,29 @@
LDFLAGS = -lusb -lreadline -lpthread -L/opt/local/lib
CFLAGS = -I. -I/opt/local/include -Wall
CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall
QTLDFLAGS = $(shell pkg-config --libs QtCore QtGui 2>/dev/null)
ifneq ($(QTLDFLAGS),)
QTGUI = proxgui.o proxguiqt.o proxguiqt.moc.o
CFLAGS += -DHAVE_GUI
MOC = $(shell type moc-qt4 >/dev/null 2>&1 && echo moc-qt4 || echo moc)
LINK.o = $(LINK.cpp)
else
QTGUI = guidummy.o
endif
all: proxmark3 snooper
proxmark3: LDFLAGS+=$(QTLDFLAGS)
proxmark3: proxmark3.o gui.o command.o usb.o $(QTGUI)
snooper: snooper.o gui.o command.o usb.o guidummy.o
proxguiqt.moc.cpp: proxguiqt.h
$(MOC) -o$@ $^
clean:
rm -f proxmark3 snooper *.o *.moc.cpp
.PHONY: all clean

2
linux/command.c Normal file
View file

@ -0,0 +1,2 @@
#include "translate.h"
#include "../winsrc/command.cpp"

166
linux/flasher.c Normal file
View file

@ -0,0 +1,166 @@
#include <usb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "translate.h"
#include "../winsrc/prox.h"
#include "proxmark3.h"
static DWORD ExpectedAddr;
static BYTE QueuedToSend[256];
static BOOL AllWritten;
static void FlushPrevious(void)
{
UsbCommand c;
memset(&c, 0, sizeof(c));
printf("expected = %08x flush, ", ExpectedAddr);
int i;
for(i = 0; i < 240; i += 48) {
c.cmd = CMD_SETUP_WRITE;
memcpy(c.d.asBytes, QueuedToSend+i, 48);
c.ext1 = (i/4);
SendCommand(&c, TRUE);
}
c.cmd = CMD_FINISH_WRITE;
c.ext1 = (ExpectedAddr-1) & (~255);
printf("c.ext1 = %08x\r", c.ext1);
memcpy(c.d.asBytes, QueuedToSend+240, 16);
SendCommand(&c, TRUE);
AllWritten = TRUE;
}
static void GotByte(DWORD where, BYTE which)
{
AllWritten = FALSE;
if(where != ExpectedAddr) {
printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);
exit(-1);
}
QueuedToSend[where & 255] = which;
ExpectedAddr++;
if((where & 255) == 255) {
// we have completed a full page
FlushPrevious();
}
}
static int HexVal(int c)
{
c = tolower(c);
if(c >= '0' && c <= '9') {
return c - '0';
} else if(c >= 'a' && c <= 'f') {
return (c - 'a') + 10;
} else {
printf("bad hex digit '%c'\n", c);
exit(-1);
}
}
static BYTE HexByte(char *s)
{
return (HexVal(s[0]) << 4) | HexVal(s[1]);
}
static void LoadFlashFromSRecords(char *file, int addr)
{
ExpectedAddr = addr;
FILE *f = fopen(file, "r");
if(!f) {
printf("couldn't open file\n");
exit(-1);
}
char line[512];
while(fgets(line, sizeof(line), f)) {
if(memcmp(line, "S3", 2)==0) {
char *s = line + 2;
int len = HexByte(s) - 5;
s += 2;
char addrStr[9];
memcpy(addrStr, s, 8);
addrStr[8] = '\0';
DWORD addr;
sscanf(addrStr, "%x", &addr);
s += 8;
int i;
for(i = 0; i < len; i++) {
while((addr+i) > ExpectedAddr) {
GotByte(ExpectedAddr, 0xff);
}
GotByte(addr+i, HexByte(s));
s += 2;
}
}
}
if(!AllWritten) FlushPrevious();
fclose(f);
printf("\ndone.\n");
}
int main(int argc, char **argv) {
unsigned int addr = 0;
UsbCommand c;
if (argc != 3) {
fprintf(stderr,"Usage: %s {bootrom|os|fpga} image.s19\n", argv[0]);
exit(EXIT_FAILURE);
}
if (!strcmp(argv[1],"bootrom")) {
addr = 0;
} else if (!strcmp(argv[1],"os")) {
addr = 0x10000;
} else if (!strcmp(argv[1],"fpga")) {
addr = 0x2000;
} else {
fprintf(stderr,"Unknown action '%s'!\n", argv[1]);
exit(EXIT_FAILURE);
}
usb_init();
fprintf(stderr,"Waiting for Proxmark to appear on USB...\n");
while(!(devh=OpenProxmark(0))) { sleep(1); }
fprintf(stderr,"Found...\n");
fprintf(stderr,"Entering flash-mode...\n");
bzero(&c, sizeof(c));
c.cmd = CMD_START_FLASH;
SendCommand(&c, FALSE);
CloseProxmark();
sleep(1);
fprintf(stderr,"Waiting for Proxmark to reappear on USB...\n");
while(!(devh=OpenProxmark(0))) { sleep(1); }
fprintf(stderr,"Found...\n");
LoadFlashFromSRecords(argv[2], addr);
bzero(&c, sizeof(c));
c.cmd = CMD_HARDWARE_RESET;
SendCommand(&c, FALSE);
CloseProxmark();
fprintf(stderr,"Have a nice day!\n");
return 0;
}

54
linux/gui.c Normal file
View file

@ -0,0 +1,54 @@
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include "proxgui.h"
#include "translate.h"
#include "../winsrc/prox.h"
int GraphBuffer[MAX_GRAPH_TRACE_LEN];
int GraphTraceLen;
double CursorScaleFactor;
int CommandFinished;
static char *logfilename = "proxmark3.log";
void PrintToScrollback(char *fmt, ...) {
va_list argptr;
static FILE *logfile = NULL;
static int logging=1;
if (logging && !logfile) {
logfile=fopen(logfilename, "a");
if (!logfile) {
fprintf(stderr, "Can't open logfile, logging disabled!\n");
logging=0;
}
}
va_start(argptr, fmt);
vprintf(fmt, argptr);
printf("\n");
if (logging && logfile) {
#if 0
char zeit[25];
time_t jetzt_t;
struct tm *jetzt;
jetzt_t = time(NULL);
jetzt = localtime(&jetzt_t);
strftime(zeit, 25, "%b %e %T", jetzt);
fprintf(logfile,"%s ", zeit);
#endif
vfprintf(logfile, fmt, argptr);
fprintf(logfile,"\n");
fflush(logfile);
}
va_end(argptr);
}
void setlogfilename(char *fn)
{
logfilename = fn;
}

17
linux/guidummy.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdio.h>
void ShowGraphWindow(void)
{
static int warned = 0;
if (!warned) {
printf("No GUI in this build!\n");
warned = 1;
}
}
void HideGraphWindow(void) {}
void RepaintGraphWindow(void) {}
void MainGraphics() {}
void InitGraphics(int argc, char **argv) {}
void ExitGraphics(void) {}

58
linux/proxgui.cpp Normal file
View file

@ -0,0 +1,58 @@
#include "proxgui.h"
#include "proxguiqt.h"
static ProxGuiQT *gui = NULL;
extern "C" void ShowGraphWindow(void)
{
if (!gui)
return;
gui->ShowGraphWindow();
}
extern "C" void HideGraphWindow(void)
{
if (!gui)
return;
gui->HideGraphWindow();
}
extern "C" void RepaintGraphWindow(void)
{
if (!gui)
return;
gui->RepaintGraphWindow();
}
extern "C" void MainGraphics(void)
{
if (!gui)
return;
gui->MainLoop();
}
extern "C" void InitGraphics(int argc, char **argv)
{
#ifdef Q_WS_X11
bool useGUI = getenv("DISPLAY") != 0;
#else
bool useGUI = true;
#endif
if (!useGUI)
return;
gui = new ProxGuiQT(argc, argv);
}
extern "C" void ExitGraphics(void)
{
if (!gui)
return;
delete gui;
gui = NULL;
}

20
linux/proxgui.h Normal file
View file

@ -0,0 +1,20 @@
#ifdef __cplusplus
extern "C" {
#endif
void ShowGraphWindow(void);
void HideGraphWindow(void);
void RepaintGraphWindow(void);
void MainGraphics(void);
void InitGraphics(int argc, char **argv);
void ExitGraphics(void);
#define MAX_GRAPH_TRACE_LEN (1024*128)
extern int GraphBuffer[MAX_GRAPH_TRACE_LEN];
extern int GraphTraceLen;
extern double CursorScaleFactor;
extern int CommandFinished;
#ifdef __cplusplus
}
#endif

309
linux/proxguiqt.cpp Normal file
View file

@ -0,0 +1,309 @@
#include <iostream>
#include <QPainterPath>
#include <QBrush>
#include <QPen>
#include <QTimer>
#include <QCloseEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <math.h>
#include <limits.h>
#include "proxguiqt.h"
#include "proxgui.h"
void ProxGuiQT::ShowGraphWindow(void)
{
emit ShowGraphWindowSignal();
}
void ProxGuiQT::RepaintGraphWindow(void)
{
emit RepaintGraphWindowSignal();
}
void ProxGuiQT::HideGraphWindow(void)
{
emit HideGraphWindowSignal();
}
void ProxGuiQT::_ShowGraphWindow(void)
{
if(!plotapp)
return;
if (!plotwidget)
plotwidget = new ProxWidget();
plotwidget->show();
}
void ProxGuiQT::_RepaintGraphWindow(void)
{
if (!plotapp || !plotwidget)
return;
plotwidget->update();
}
void ProxGuiQT::_HideGraphWindow(void)
{
if (!plotapp || !plotwidget)
return;
plotwidget->hide();
}
void ProxGuiQT::MainLoop()
{
plotapp = new QApplication(argc, argv);
connect(this, SIGNAL(ShowGraphWindowSignal()), this, SLOT(_ShowGraphWindow()));
connect(this, SIGNAL(RepaintGraphWindowSignal()), this, SLOT(_RepaintGraphWindow()));
connect(this, SIGNAL(HideGraphWindowSignal()), this, SLOT(_HideGraphWindow()));
plotapp->exec();
}
ProxGuiQT::ProxGuiQT(int argc, char **argv) : plotapp(NULL), plotwidget(NULL),
argc(argc), argv(argv)
{
}
ProxGuiQT::~ProxGuiQT(void)
{
if (plotwidget) {
delete plotwidget;
plotwidget = NULL;
}
if (plotapp) {
plotapp->quit();
delete plotapp;
plotapp = NULL;
}
}
void ProxWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPainterPath penPath, whitePath, greyPath, cursorAPath, cursorBPath;
QRect r;
QBrush brush(QColor(100, 255, 100));
QPen pen(QColor(100, 255, 100));
painter.setFont(QFont("Arial", 10));
if(GraphStart < 0) {
GraphStart = 0;
}
r = rect();
painter.fillRect(r, QColor(0, 0, 0));
whitePath.moveTo(r.left() + 40, r.top());
whitePath.lineTo(r.left() + 40, r.bottom());
int zeroHeight = r.top() + (r.bottom() - r.top()) / 2;
greyPath.moveTo(r.left(), zeroHeight);
greyPath.lineTo(r.right(), zeroHeight);
painter.setPen(QColor(100, 100, 100));
painter.drawPath(greyPath);
int startMax =
(GraphTraceLen - (int)((r.right() - r.left() - 40) / GraphPixelsPerPoint));
if(startMax < 0) {
startMax = 0;
}
if(GraphStart > startMax) {
GraphStart = startMax;
}
int absYMax = 1;
int i;
for(i = GraphStart; ; i++) {
if(i >= GraphTraceLen) {
break;
}
if(fabs((double)GraphBuffer[i]) > absYMax) {
absYMax = (int)fabs((double)GraphBuffer[i]);
}
int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint);
if(x > r.right()) {
break;
}
}
absYMax = (int)(absYMax*1.2 + 1);
// number of points that will be plotted
int span = (int)((r.right() - r.left()) / GraphPixelsPerPoint);
// one label every 100 pixels, let us say
int labels = (r.right() - r.left() - 40) / 100;
if(labels <= 0) labels = 1;
int pointsPerLabel = span / labels;
if(pointsPerLabel <= 0) pointsPerLabel = 1;
int yMin = INT_MAX;
int yMax = INT_MIN;
int yMean = 0;
int n = 0;
for(i = GraphStart; ; i++) {
if(i >= GraphTraceLen) {
break;
}
int x = 40 + (int)((i - GraphStart)*GraphPixelsPerPoint);
if(x > r.right() + GraphPixelsPerPoint) {
break;
}
int y = GraphBuffer[i];
if(y < yMin) {
yMin = y;
}
if(y > yMax) {
yMax = y;
}
yMean += y;
n++;
y = (y * (r.top() - r.bottom()) / (2*absYMax)) + zeroHeight;
if(i == GraphStart) {
penPath.moveTo(x, y);
} else {
penPath.lineTo(x, y);
}
if(GraphPixelsPerPoint > 10) {
QRect f(QPoint(x - 3, y - 3),QPoint(x + 3, y + 3));
painter.fillRect(f, brush);
}
if(((i - GraphStart) % pointsPerLabel == 0) && i != GraphStart) {
whitePath.moveTo(x, zeroHeight - 3);
whitePath.lineTo(x, zeroHeight + 3);
char str[100];
sprintf(str, "+%d", (i - GraphStart));
painter.setPen(QColor(255, 255, 255));
QRect size;
QFontMetrics metrics(painter.font());
size = metrics.boundingRect(str);
painter.drawText(x - (size.right() - size.left()), zeroHeight + 9, str);
penPath.moveTo(x,y);
}
if(i == CursorAPos || i == CursorBPos) {
QPainterPath *cursorPath;
if(i == CursorAPos) {
cursorPath = &cursorAPath;
} else {
cursorPath = &cursorBPath;
}
cursorPath->moveTo(x, r.top());
cursorPath->lineTo(x, r.bottom());
penPath.moveTo(x, y);
}
}
if(n != 0) {
yMean /= n;
}
painter.setPen(QColor(255, 255, 255));
painter.drawPath(whitePath);
painter.setPen(pen);
painter.drawPath(penPath);
painter.setPen(QColor(255, 255, 0));
painter.drawPath(cursorAPath);
painter.setPen(QColor(255, 0, 255));
painter.drawPath(cursorBPath);
char str[100];
sprintf(str, "@%d max=%d min=%d mean=%d n=%d/%d dt=%d [%.3f]",
GraphStart, yMax, yMin, yMean, n, GraphTraceLen,
CursorBPos - CursorAPos, (CursorBPos - CursorAPos)/CursorScaleFactor);
painter.setPen(QColor(255, 255, 255));
painter.drawText(50, r.bottom() - 20, str);
}
ProxWidget::ProxWidget(QWidget *parent) : QWidget(parent), GraphStart(0), GraphPixelsPerPoint(1)
{
resize(600, 500);
QPalette palette(QColor(0,0,0,0));
palette.setColor(QPalette::WindowText, QColor(255,255,255));
palette.setColor(QPalette::Text, QColor(255,255,255));
palette.setColor(QPalette::Button, QColor(100, 100, 100));
setPalette(palette);
setAutoFillBackground(true);
}
void ProxWidget::closeEvent(QCloseEvent *event)
{
event->ignore();
this->hide();
}
void ProxWidget::mouseMoveEvent(QMouseEvent *event)
{
int x = event->x();
x -= 40;
x = (int)(x / GraphPixelsPerPoint);
x += GraphStart;
if((event->buttons() & Qt::LeftButton)) {
CursorAPos = x;
} else if (event->buttons() & Qt::RightButton) {
CursorBPos = x;
}
this->update();
}
void ProxWidget::keyPressEvent(QKeyEvent *event)
{
switch(event->key()) {
case Qt::Key_Down:
if(GraphPixelsPerPoint <= 50) {
GraphPixelsPerPoint *= 2;
}
break;
case Qt::Key_Up:
if(GraphPixelsPerPoint >= 0.02) {
GraphPixelsPerPoint /= 2;
}
break;
case Qt::Key_Right:
if(GraphPixelsPerPoint < 20) {
GraphStart += (int)(20 / GraphPixelsPerPoint);
} else {
GraphStart++;
}
break;
case Qt::Key_Left:
if(GraphPixelsPerPoint < 20) {
GraphStart -= (int)(20 / GraphPixelsPerPoint);
} else {
GraphStart--;
}
break;
default:
QWidget::keyPressEvent(event);
return;
break;
}
this->update();
}

56
linux/proxguiqt.h Normal file
View file

@ -0,0 +1,56 @@
#include <QApplication>
#include <QPushButton>
#include <QObject>
#include <QWidget>
#include <QPainter>
class ProxWidget : public QWidget
{
Q_OBJECT;
private:
int GraphStart;
double GraphPixelsPerPoint;
int CursorAPos;
int CursorBPos;
public:
ProxWidget(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
void closeEvent(QCloseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event) { mouseMoveEvent(event); }
void keyPressEvent(QKeyEvent *event);
};
class ProxGuiQT : public QObject
{
Q_OBJECT;
private:
QApplication *plotapp;
ProxWidget *plotwidget;
int argc;
char **argv;
void (*main_func)(void);
public:
ProxGuiQT(int argc, char **argv);
~ProxGuiQT(void);
void ShowGraphWindow(void);
void RepaintGraphWindow(void);
void HideGraphWindow(void);
void MainLoop(void);
private slots:
void _ShowGraphWindow(void);
void _RepaintGraphWindow(void);
void _HideGraphWindow(void);
signals:
void ShowGraphWindowSignal(void);
void RepaintGraphWindowSignal(void);
void HideGraphWindowSignal(void);
};

91
linux/proxmark3.c Normal file
View file

@ -0,0 +1,91 @@
#include <usb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <pthread.h>
#include "translate.h"
#include "../winsrc/prox.h"
#include "proxmark3.h"
#include "proxgui.h"
struct usb_receiver_arg {
int run;
};
static void *usb_receiver(void *targ) {
struct usb_receiver_arg *arg = (struct usb_receiver_arg*)targ;
UsbCommand cmdbuf;
while(arg->run) {
if (ReceiveCommandP(&cmdbuf) > 0) {
int i;
for (i=0; i<strlen(PROXPROMPT); i++)
putchar(0x08);
UsbCommandReceived(&cmdbuf);
printf(PROXPROMPT);
fflush(NULL);
}
}
pthread_exit(NULL);
}
static void *main_loop(void *targ)
{
char *cmd = NULL;
while(1) {
struct usb_receiver_arg rarg;
pthread_t reader_thread;
rarg.run=1;
pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
cmd = readline(PROXPROMPT);
rarg.run=0;
pthread_join(reader_thread, NULL);
if (cmd) {
if (cmd[0] != 0x00) {
CommandReceived(cmd);
add_history(cmd);
}
free(cmd);
} else {
printf("\n");
break;
}
}
ExitGraphics();
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
pthread_t main_loop_t;
usb_init();
if (!(devh = OpenProxmark(1))) {
fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
exit(1);
}
pthread_create(&main_loop_t, NULL, &main_loop, NULL);
InitGraphics(argc, argv);
MainGraphics();
pthread_join(main_loop_t, NULL);
CloseProxmark();
return 0;
}

11
linux/proxmark3.h Normal file
View file

@ -0,0 +1,11 @@
#define PROXPROMPT "proxmark3> "
extern usb_dev_handle *devh;
extern unsigned char return_on_error;
extern unsigned char error_occured;
int ReceiveCommandP(UsbCommand *c);
usb_dev_handle* OpenProxmark(int);
void CloseProxmark(void);
void setlogfilename(char *fn);

49
linux/snooper.c Normal file
View file

@ -0,0 +1,49 @@
#include <usb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include "translate.h"
#include "../winsrc/prox.h"
#include "proxmark3.h"
#define HANDLE_ERROR if (error_occured) { \
error_occured = 0;\
break;\
}
int main()
{
usb_init();
setlogfilename("snooper.log");
return_on_error = 1;
while(1) {
while(!(devh=OpenProxmark(0))) { sleep(1); }
while(1) {
UsbCommand cmdbuf;
int i;
CommandReceived("hi14asnoop");
HANDLE_ERROR
ReceiveCommand(&cmdbuf);
HANDLE_ERROR
for (i=0; i<5; i++) {
ReceiveCommandP(&cmdbuf);
}
HANDLE_ERROR
CommandReceived("hi14alist");
HANDLE_ERROR
}
}
CloseProxmark();
return 0;
}

9
linux/translate.h Normal file
View file

@ -0,0 +1,9 @@
#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned int
#define TRUE 1
#define FALSE 0
#define BOOL int
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)>(b))?(b):(a))

16
linux/unbind-proxmark Executable file
View file

@ -0,0 +1,16 @@
#!/bin/sh
for i in /sys/bus/usb/devices/*; do
if grep "9ac4" "${i}/idVendor" >/dev/null 2>&1; then
echo "Found Proxmark..."
dev=`basename "${i}"`
for j in /sys/bus/usb/drivers/usbhid/*; do
if basename "${j}"|grep "^${dev}" >/dev/null; then
bound="`basename "${j}"`"
echo "Unbinding ${bound}..."
echo -n "${bound}" >/sys/bus/usb/drivers/usbhid/unbind
fi
done
fi
done

171
linux/usb.c Normal file
View file

@ -0,0 +1,171 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <usb.h>
#include <strings.h>
#include <errno.h>
#include "translate.h"
#include "../winsrc/prox.h"
#include "proxmark3.h"
usb_dev_handle *devh = NULL;
static unsigned int claimed_iface = 0;
unsigned char return_on_error = 0;
unsigned char error_occured = 0;
void SendCommand(UsbCommand *c, BOOL wantAck) {
int ret;
#if 0
printf("Sending %d bytes\n", sizeof(UsbCommand));
#endif
ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
if (ret<0) {
error_occured = 1;
if (return_on_error)
return;
fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n",
usb_strerror());
if (devh) {
usb_close(devh);
devh = NULL;
}
while(!(devh=OpenProxmark(0))) { sleep(1); }
printf(PROXPROMPT);
fflush(NULL);
return;
}
if(wantAck) {
UsbCommand ack;
ReceiveCommand(&ack);
if(ack.cmd != CMD_ACK) {
printf("bad ACK\n");
exit(-1);
}
}
}
int ReceiveCommandP(UsbCommand *c) {
int ret;
bzero(c, sizeof(UsbCommand));
ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500);
if (ret<0) {
if (ret != -ETIMEDOUT) {
error_occured = 1;
if (return_on_error)
return 0;
fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
usb_strerror(), ret);
if (devh) {
usb_close(devh);
devh = NULL;
}
while(!(devh=OpenProxmark(0))) { sleep(1); }
printf(PROXPROMPT);
fflush(NULL);
return 0;
}
} else {
if (ret && (ret < sizeof(UsbCommand))) {
fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
ret, (int)sizeof(UsbCommand));
}
#if 0
{
int i;
printf("Read %d bytes\n", ret);
for (i = 0; i < ret; i++) {
printf("0x%02X ", ((unsigned char*)c)[i]);
if (!((i+1)%8))
printf("\n");
}
printf("\n");
}
#endif
}
return ret;
}
void ReceiveCommand(UsbCommand *c) {
while(ReceiveCommandP(c)<0) {}
}
usb_dev_handle* findProxmark(int verbose, unsigned int *iface) {
struct usb_bus *busses, *bus;
usb_dev_handle *handle = NULL;
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
for (bus = busses; bus; bus = bus->next) {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next) {
struct usb_device_descriptor *desc = &(dev->descriptor);
if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) {
handle = usb_open(dev);
if (!handle) {
if (verbose)
fprintf(stderr, "open failed: %s!\n", usb_strerror());
return NULL;
}
*iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
return handle;
}
}
}
return NULL;
}
usb_dev_handle* OpenProxmark(int verbose) {
int ret;
usb_dev_handle *handle = NULL;
unsigned int iface;
#ifndef __APPLE__
handle = findProxmark(verbose, &iface);
if (!handle)
return NULL;
/* Whatever... */
usb_reset(handle);
#endif
handle = findProxmark(verbose, &iface);
if (!handle)
return NULL;
ret = usb_claim_interface(handle, iface);
if (ret<0) {
if (verbose)
fprintf(stderr, "claim failed: %s!\n", usb_strerror());
return NULL;
}
claimed_iface = iface;
devh = handle;
return handle;
}
void CloseProxmark(void) {
usb_release_interface(devh, claimed_iface);
usb_close(devh);
}

0
linux/windows.h Normal file
View file