]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - linux/flasher.c
10 #include "translate.h"
11 #include "../winsrc/prox.h"
12 #include "proxmark3.h"
14 static DWORD ExpectedAddr
;
15 static BYTE QueuedToSend
[256];
16 static BOOL AllWritten
;
18 static void FlushPrevious(void)
21 memset(&c
, 0, sizeof(c
));
23 printf("expected = %08x flush, ", ExpectedAddr
);
26 for(i
= 0; i
< 240; i
+= 48) {
27 c
.cmd
= CMD_SETUP_WRITE
;
28 memcpy(c
.d
.asBytes
, QueuedToSend
+i
, 48);
30 SendCommand(&c
, TRUE
);
33 c
.cmd
= CMD_FINISH_WRITE
;
34 c
.ext1
= (ExpectedAddr
-1) & (~255);
35 printf("c.ext1 = %08x\r", c
.ext1
);
36 memcpy(c
.d
.asBytes
, QueuedToSend
+240, 16);
37 SendCommand(&c
, TRUE
);
42 static void GotByte(DWORD where
, BYTE which
)
46 if(where
!= ExpectedAddr
) {
47 printf("bad: got at %08x, expected at %08x\n", where
, ExpectedAddr
);
50 QueuedToSend
[where
& 255] = which
;
53 if((where
& 255) == 255) {
54 // we have completed a full page
59 static int HexVal(int c
)
62 if(c
>= '0' && c
<= '9') {
64 } else if(c
>= 'a' && c
<= 'f') {
65 return (c
- 'a') + 10;
67 printf("bad hex digit '%c'\n", c
);
72 static BYTE
HexByte(char *s
)
74 return (HexVal(s
[0]) << 4) | HexVal(s
[1]);
77 static void LoadFlashFromSRecords(char *file
, int addr
)
81 FILE *f
= fopen(file
, "r");
83 printf("couldn't open file\n");
88 while(fgets(line
, sizeof(line
), f
)) {
89 if(memcmp(line
, "S3", 2)==0) {
91 int len
= HexByte(s
) - 5;
95 memcpy(addrStr
, s
, 8);
98 sscanf(addrStr
, "%x", &addr
);
102 for(i
= 0; i
< len
; i
++) {
103 while((addr
+i
) > ExpectedAddr
) {
104 GotByte(ExpectedAddr
, 0xff);
106 GotByte(addr
+i
, HexByte(s
));
112 if(!AllWritten
) FlushPrevious();
118 int main(int argc
, char **argv
) {
119 unsigned int addr
= 0;
123 fprintf(stderr
,"Usage: %s {bootrom|os|fpga} image.s19\n", argv
[0]);
127 if (!strcmp(argv
[1],"bootrom")) {
129 } else if (!strcmp(argv
[1],"os")) {
131 } else if (!strcmp(argv
[1],"fpga")) {
134 fprintf(stderr
,"Unknown action '%s'!\n", argv
[1]);
140 fprintf(stderr
,"Waiting for Proxmark to appear on USB...\n");
141 while(!(devh
=OpenProxmark(0))) { sleep(1); }
142 fprintf(stderr
,"Found...\n");
144 fprintf(stderr
,"Entering flash-mode...\n");
145 bzero(&c
, sizeof(c
));
146 c
.cmd
= CMD_START_FLASH
;
147 SendCommand(&c
, FALSE
);
151 fprintf(stderr
,"Waiting for Proxmark to reappear on USB...\n");
152 while(!(devh
=OpenProxmark(0))) { sleep(1); }
153 fprintf(stderr
,"Found...\n");
155 LoadFlashFromSRecords(argv
[2], addr
);
157 bzero(&c
, sizeof(c
));
158 c
.cmd
= CMD_HARDWARE_RESET
;
159 SendCommand(&c
, FALSE
);
163 fprintf(stderr
,"Have a nice day!\n");