]>
Commit | Line | Data |
---|---|---|
6658905f | 1 | #include <usb.h> |
2 | #include <stdio.h> | |
3 | #include <unistd.h> | |
4 | #include <stdlib.h> | |
5 | #include <strings.h> | |
6 | #include <string.h> | |
7 | #include <errno.h> | |
8 | #include <ctype.h> | |
9 | ||
10 | #include "translate.h" | |
11 | #include "../winsrc/prox.h" | |
12 | #include "proxmark3.h" | |
13 | ||
14 | static DWORD ExpectedAddr; | |
15 | static BYTE QueuedToSend[256]; | |
16 | static BOOL AllWritten; | |
17 | ||
18 | static void FlushPrevious(void) | |
19 | { | |
20 | UsbCommand c; | |
21 | memset(&c, 0, sizeof(c)); | |
22 | ||
23 | printf("expected = %08x flush, ", ExpectedAddr); | |
24 | ||
25 | int i; | |
26 | for(i = 0; i < 240; i += 48) { | |
27 | c.cmd = CMD_SETUP_WRITE; | |
28 | memcpy(c.d.asBytes, QueuedToSend+i, 48); | |
29 | c.ext1 = (i/4); | |
30 | SendCommand(&c, TRUE); | |
31 | } | |
32 | ||
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); | |
38 | ||
39 | AllWritten = TRUE; | |
40 | } | |
41 | ||
42 | static void GotByte(DWORD where, BYTE which) | |
43 | { | |
44 | AllWritten = FALSE; | |
45 | ||
46 | if(where != ExpectedAddr) { | |
47 | printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr); | |
48 | exit(-1); | |
49 | } | |
50 | QueuedToSend[where & 255] = which; | |
51 | ExpectedAddr++; | |
52 | ||
53 | if((where & 255) == 255) { | |
54 | // we have completed a full page | |
55 | FlushPrevious(); | |
56 | } | |
57 | } | |
58 | ||
59 | static int HexVal(int c) | |
60 | { | |
61 | c = tolower(c); | |
62 | if(c >= '0' && c <= '9') { | |
63 | return c - '0'; | |
64 | } else if(c >= 'a' && c <= 'f') { | |
65 | return (c - 'a') + 10; | |
66 | } else { | |
67 | printf("bad hex digit '%c'\n", c); | |
68 | exit(-1); | |
69 | } | |
70 | } | |
71 | ||
72 | static BYTE HexByte(char *s) | |
73 | { | |
74 | return (HexVal(s[0]) << 4) | HexVal(s[1]); | |
75 | } | |
76 | ||
77 | static void LoadFlashFromSRecords(char *file, int addr) | |
78 | { | |
79 | ExpectedAddr = addr; | |
80 | ||
81 | FILE *f = fopen(file, "r"); | |
82 | if(!f) { | |
83 | printf("couldn't open file\n"); | |
84 | exit(-1); | |
85 | } | |
86 | ||
87 | char line[512]; | |
88 | while(fgets(line, sizeof(line), f)) { | |
89 | if(memcmp(line, "S3", 2)==0) { | |
90 | char *s = line + 2; | |
91 | int len = HexByte(s) - 5; | |
92 | s += 2; | |
93 | ||
94 | char addrStr[9]; | |
95 | memcpy(addrStr, s, 8); | |
96 | addrStr[8] = '\0'; | |
97 | DWORD addr; | |
98 | sscanf(addrStr, "%x", &addr); | |
99 | s += 8; | |
100 | ||
101 | int i; | |
102 | for(i = 0; i < len; i++) { | |
103 | while((addr+i) > ExpectedAddr) { | |
104 | GotByte(ExpectedAddr, 0xff); | |
105 | } | |
106 | GotByte(addr+i, HexByte(s)); | |
107 | s += 2; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | if(!AllWritten) FlushPrevious(); | |
113 | ||
114 | fclose(f); | |
115 | printf("\ndone.\n"); | |
116 | } | |
117 | ||
118 | int main(int argc, char **argv) { | |
119 | unsigned int addr = 0; | |
120 | UsbCommand c; | |
121 | ||
122 | if (argc != 3) { | |
123 | fprintf(stderr,"Usage: %s {bootrom|os|fpga} image.s19\n", argv[0]); | |
124 | exit(EXIT_FAILURE); | |
125 | } | |
126 | ||
127 | if (!strcmp(argv[1],"bootrom")) { | |
128 | addr = 0; | |
129 | } else if (!strcmp(argv[1],"os")) { | |
130 | addr = 0x10000; | |
131 | } else if (!strcmp(argv[1],"fpga")) { | |
132 | addr = 0x2000; | |
133 | } else { | |
134 | fprintf(stderr,"Unknown action '%s'!\n", argv[1]); | |
135 | exit(EXIT_FAILURE); | |
136 | } | |
137 | ||
138 | usb_init(); | |
139 | ||
140 | fprintf(stderr,"Waiting for Proxmark to appear on USB...\n"); | |
141 | while(!(devh=OpenProxmark(0))) { sleep(1); } | |
142 | fprintf(stderr,"Found...\n"); | |
143 | ||
144 | fprintf(stderr,"Entering flash-mode...\n"); | |
145 | bzero(&c, sizeof(c)); | |
146 | c.cmd = CMD_START_FLASH; | |
147 | SendCommand(&c, FALSE); | |
148 | CloseProxmark(); | |
149 | sleep(1); | |
150 | ||
151 | fprintf(stderr,"Waiting for Proxmark to reappear on USB...\n"); | |
06735128 | 152 | fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n"); |
6658905f | 153 | while(!(devh=OpenProxmark(0))) { sleep(1); } |
154 | fprintf(stderr,"Found...\n"); | |
155 | ||
156 | LoadFlashFromSRecords(argv[2], addr); | |
157 | ||
158 | bzero(&c, sizeof(c)); | |
159 | c.cmd = CMD_HARDWARE_RESET; | |
160 | SendCommand(&c, FALSE); | |
161 | ||
162 | CloseProxmark(); | |
163 | ||
164 | fprintf(stderr,"Have a nice day!\n"); | |
165 | ||
166 | return 0; | |
167 | } |