]>
git.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;
120 BOOL fastflash
= 0, flashboth
= 0;
123 if (argc
!= 3 && ! ((argc
== 4 && *argv
[3] == 'f') || (argc
== 5 && *argv
[4] == 'f'))) {
124 fprintf(stderr
,"Usage: %s {bootrom|os|fpga} image.s19 [f]ast\n", argv
[0]);
125 fprintf(stderr
," %s {both} osimage.s19 fpgaimage.s19 [f]ast\n", argv
[0]);
129 if (!strcmp(argv
[1],"bootrom")) {
131 } else if (!strcmp(argv
[1],"os")) {
132 addr
= FLASH_ADDR_OS
;
133 } else if (!strcmp(argv
[1],"fpga")) {
134 addr
= FLASH_ADDR_FPGA
;
135 } else if (!strcmp(argv
[1],"both")) {
138 fprintf(stderr
,"Unknown action '%s'!\n", argv
[1]);
142 if((argc
== 4 && *argv
[3] == 'f') || (argc
== 5 && *argv
[4] == 'f')) {
144 fprintf(stderr
,"Fastflash - device already in FLASH mode...\n");
149 fprintf(stderr
,"Waiting for Proxmark to appear on USB...\n");
150 while(!(devh
=OpenProxmark(0))) { sleep(1); }
151 fprintf(stderr
,"Found...\n");
154 fprintf(stderr
,"Entering flash-mode...\n");
155 bzero(&c
, sizeof(c
));
156 c
.cmd
= CMD_START_FLASH
;
157 SendCommand(&c
, FALSE
);
161 fprintf(stderr
,"Waiting for Proxmark to reappear on USB...\n");
162 fprintf(stderr
,"(Press and hold down button NOW if your bootloader requires it)\n");
163 while(!(devh
=OpenProxmark(0))) { sleep(1); }
164 fprintf(stderr
,"Found...\n");
168 LoadFlashFromSRecords(argv
[2], addr
);
170 fprintf(stderr
,"Flashing os...\n");
171 LoadFlashFromSRecords(argv
[2],FLASH_ADDR_OS
);
172 fprintf(stderr
,"Flashing fpga...\n");
173 LoadFlashFromSRecords(argv
[3],FLASH_ADDR_FPGA
);
176 bzero(&c
, sizeof(c
));
177 c
.cmd
= CMD_HARDWARE_RESET
;
178 SendCommand(&c
, FALSE
);
182 fprintf(stderr
,"Have a nice day!\n");