]>
Commit | Line | Data |
---|---|---|
1 | #ifdef WIN32 | |
2 | #include <windows.h> | |
3 | #include <setupapi.h> | |
4 | #include <ctype.h> | |
5 | #define bzero(b,len) (memset((b), '\0', (len)), (void) 0) | |
6 | BOOL UsbConnect(void); | |
7 | #else | |
8 | #include <usb.h> | |
9 | #endif | |
10 | #include <stdio.h> | |
11 | #include <unistd.h> | |
12 | #include <stdlib.h> | |
13 | #include <stdint.h> | |
14 | #include <stdbool.h> | |
15 | #include <strings.h> | |
16 | #include <string.h> | |
17 | #include <errno.h> | |
18 | #include <ctype.h> | |
19 | ||
20 | #include "prox.h" | |
21 | #ifndef WIN32 | |
22 | #include "proxmark3.h" | |
23 | #endif | |
24 | #include "flash.h" | |
25 | ||
26 | static uint32_t ExpectedAddr; | |
27 | static uint8_t QueuedToSend[256]; | |
28 | static bool AllWritten; | |
29 | #define PHYSICAL_FLASH_START 0x100000 | |
30 | ||
31 | void WaitForAck(void) { | |
32 | UsbCommand ack; | |
33 | ReceiveCommand(&ack); | |
34 | if(ack.cmd != CMD_ACK) { | |
35 | printf("bad ACK\n"); | |
36 | exit(-1); | |
37 | } | |
38 | } | |
39 | ||
40 | struct partition partitions[] = { | |
41 | {0x100000, 0x102000, 1, "bootrom"}, | |
42 | {0x102000, 0x110000, 0, "fpga"}, | |
43 | {0x110000, 0x140000, 0, "os"}, | |
44 | {0, 0, 0, NULL}, | |
45 | }; | |
46 | ||
47 | /* If translate is set, subtract PHYSICAL_FLASH_START to translate for old | |
48 | * bootroms. | |
49 | */ | |
50 | void FlushPrevious(int translate) | |
51 | { | |
52 | UsbCommand c; | |
53 | memset(&c, 0, sizeof(c)); | |
54 | ||
55 | printf("expected = %08x flush, ", ExpectedAddr); | |
56 | ||
57 | int i; | |
58 | for(i = 0; i < 240; i += 48) { | |
59 | c.cmd = CMD_SETUP_WRITE; | |
60 | memcpy(c.d.asBytes, QueuedToSend+i, 48); | |
61 | c.arg[0] = (i/4); | |
62 | SendCommand(&c); | |
63 | WaitForAck(); | |
64 | } | |
65 | ||
66 | c.cmd = CMD_FINISH_WRITE; | |
67 | c.arg[0] = (ExpectedAddr-1) & (~255); | |
68 | if(translate) { | |
69 | c.arg[0] -= PHYSICAL_FLASH_START; | |
70 | } | |
71 | printf("c.arg[0] = %08x\r", c.arg[0]); | |
72 | memcpy(c.d.asBytes, QueuedToSend+240, 16); | |
73 | SendCommand(&c); | |
74 | WaitForAck(); | |
75 | ||
76 | AllWritten = true; | |
77 | } | |
78 | ||
79 | /* Where must be between start_addr (inclusive) and end_addr (exclusive). | |
80 | */ | |
81 | void GotByte(uint32_t where, uint8_t which, int start_addr, int end_addr, int translate) | |
82 | { | |
83 | AllWritten = false; | |
84 | ||
85 | if(where < start_addr || where >= end_addr) { | |
86 | printf("bad: got byte at %08x, outside of range %08x-%08x\n", where, start_addr, end_addr); | |
87 | exit(-1); | |
88 | } | |
89 | ||
90 | if(where != ExpectedAddr) { | |
91 | printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr); | |
92 | exit(-1); | |
93 | } | |
94 | QueuedToSend[where & 255] = which; | |
95 | ExpectedAddr++; | |
96 | ||
97 | if((where & 255) == 255) { | |
98 | // we have completed a full page | |
99 | FlushPrevious(translate); | |
100 | } | |
101 | } | |
102 | ||
103 | int HexVal(int c) | |
104 | { | |
105 | c = tolower(c); | |
106 | if(c >= '0' && c <= '9') { | |
107 | return c - '0'; | |
108 | } else if(c >= 'a' && c <= 'f') { | |
109 | return (c - 'a') + 10; | |
110 | } else { | |
111 | printf("bad hex digit '%c'\n", c); | |
112 | exit(-1); | |
113 | } | |
114 | } | |
115 | ||
116 | uint8_t HexByte(char *s) | |
117 | { | |
118 | return (HexVal(s[0]) << 4) | HexVal(s[1]); | |
119 | } | |
120 | ||
121 | void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate) | |
122 | { | |
123 | ExpectedAddr = start_addr; | |
124 | ||
125 | FILE *f = fopen(file, "r"); | |
126 | if(!f) { | |
127 | printf("couldn't open file\n"); | |
128 | exit(-1); | |
129 | } | |
130 | ||
131 | char line[512]; | |
132 | while(fgets(line, sizeof(line), f)) { | |
133 | if(memcmp(line, "S3", 2)==0) { | |
134 | char *s = line + 2; | |
135 | int len = HexByte(s) - 5; | |
136 | s += 2; | |
137 | ||
138 | char addrStr[9]; | |
139 | memcpy(addrStr, s, 8); | |
140 | addrStr[8] = '\0'; | |
141 | uint32_t addr; | |
142 | sscanf(addrStr, "%x", &addr); | |
143 | s += 8; | |
144 | ||
145 | /* Accept files that are located at PHYSICAL_FLASH_START, and files that are located at 0 */ | |
146 | if(addr < PHYSICAL_FLASH_START) | |
147 | addr += PHYSICAL_FLASH_START; | |
148 | ||
149 | int i; | |
150 | for(i = 0; i < len; i++) { | |
151 | while((addr+i) > ExpectedAddr) { | |
152 | GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate); | |
153 | } | |
154 | GotByte(addr+i, HexByte(s), start_addr, end_addr, translate); | |
155 | s += 2; | |
156 | } | |
157 | } | |
158 | } | |
159 | ||
160 | if(!AllWritten) FlushPrevious(translate); | |
161 | ||
162 | fclose(f); | |
163 | printf("\ndone.\n"); | |
164 | } | |
165 | ||
166 | int PrepareFlash(struct partition *p, const char *filename, unsigned int state) | |
167 | { | |
168 | int translate = 0; | |
169 | if(state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) { | |
170 | UsbCommand c; | |
171 | c.cmd = CMD_START_FLASH; | |
172 | c.arg[0] = p->start; | |
173 | c.arg[1] = p->end; | |
174 | ||
175 | /* Only send magic when flashing bootrom */ | |
176 | if(p->precious) { | |
177 | c.arg[2] = START_FLASH_MAGIC; | |
178 | } else { | |
179 | c.arg[2] = 0; | |
180 | } | |
181 | SendCommand(&c); | |
182 | WaitForAck(); | |
183 | translate = 0; | |
184 | } else { | |
185 | fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n"); | |
186 | fprintf(stderr, " It is recommended that you update your bootloader\n\n"); | |
187 | translate = 1; | |
188 | } | |
189 | ||
190 | LoadFlashFromSRecords(filename, p->start, p->end, translate); | |
191 | return 1; | |
192 | } | |
193 | ||
194 | unsigned int GetProxmarkState(void) | |
195 | { | |
196 | unsigned int state = 0; | |
197 | ||
198 | UsbCommand c; | |
199 | c.cmd = CMD_DEVICE_INFO; | |
200 | SendCommand(&c); | |
201 | ||
202 | UsbCommand resp; | |
203 | ReceiveCommand(&resp); | |
204 | /* Three cases: | |
205 | * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK | |
206 | * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command" | |
207 | * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags | |
208 | */ | |
209 | ||
210 | switch(resp.cmd) { | |
211 | case CMD_ACK: | |
212 | state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM; | |
213 | break; | |
214 | case CMD_DEBUG_PRINT_STRING: | |
215 | state = DEVICE_INFO_FLAG_CURRENT_MODE_OS; | |
216 | break; | |
217 | case CMD_DEVICE_INFO: | |
218 | state = resp.arg[0]; | |
219 | break; | |
220 | default: | |
221 | fprintf(stderr, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp.cmd); | |
222 | exit(-1); | |
223 | break; | |
224 | } | |
225 | ||
226 | return state; | |
227 | } | |
228 | ||
229 | unsigned int EnterFlashState(void) | |
230 | { | |
231 | unsigned int state = GetProxmarkState(); | |
232 | ||
233 | if(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) { | |
234 | /* Already in flash state, we're done. */ | |
235 | return state; | |
236 | } | |
237 | ||
238 | if(state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) { | |
239 | fprintf(stderr,"Entering flash-mode...\n"); | |
240 | UsbCommand c; | |
241 | bzero(&c, sizeof(c)); | |
242 | ||
243 | if( (state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT) ) { | |
244 | /* New style handover: Send CMD_START_FLASH, which will reset the board and | |
245 | * enter the bootrom on the next boot. | |
246 | */ | |
247 | c.cmd = CMD_START_FLASH; | |
248 | SendCommand(&c); | |
249 | fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n"); | |
250 | fprintf(stderr,"Waiting for Proxmark to reappear on USB... "); | |
251 | } else { | |
252 | /* Old style handover: Ask the user to press the button, then reset the board */ | |
253 | c.cmd = CMD_HARDWARE_RESET; | |
254 | SendCommand(&c); | |
255 | fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n"); | |
256 | fprintf(stderr,"Waiting for Proxmark to reappear on USB... "); | |
257 | } | |
258 | ||
259 | #ifdef WIN32 | |
260 | Sleep(1000); | |
261 | while(!UsbConnect()) { Sleep(1000); } | |
262 | #else | |
263 | CloseProxmark(); | |
264 | sleep(1); | |
265 | ||
266 | while(!(devh=OpenProxmark(0))) { sleep(1); } | |
267 | #endif | |
268 | fprintf(stderr,"Found.\n"); | |
269 | ||
270 | return GetProxmarkState(); | |
271 | } | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
276 | /* On first call, have *offset = -1, *length = 0; */ | |
277 | int find_next_area(const char *str, int *offset, int *length) | |
278 | { | |
279 | if(*str == '\0') return 0; | |
280 | if((*offset >= 0) && str[*offset + *length] == '\0') return 0; | |
281 | *offset += 1 + *length; | |
282 | ||
283 | char *next_comma = strchr(str + *offset, ','); | |
284 | if(next_comma == NULL) { | |
285 | *length = strlen(str) - *offset; | |
286 | } else { | |
287 | *length = next_comma-(str+*offset); | |
288 | } | |
289 | return 1; | |
290 | } | |
291 | ||
292 | void do_flash(char **argv) { | |
293 | unsigned int state = EnterFlashState(); | |
294 | ||
295 | if (!(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM)) { | |
296 | fprintf(stderr, "Proxmark would not enter flash state, abort\n"); | |
297 | exit(-1); | |
298 | } | |
299 | ||
300 | int offset=-1, length=0; | |
301 | int current_area = 0; | |
302 | while(find_next_area(argv[1], &offset, &length)) { | |
303 | int i; | |
304 | struct partition *p = NULL; | |
305 | for (i=0; i<sizeof(partitions)/sizeof(partitions[0]); i++) { | |
306 | if (strncmp(partitions[i].name, argv[1] + offset, length) == 0) { | |
307 | /* Check if the name matches the bootrom partition, and if so, require "bootrom" to | |
308 | * be written in full. The other names may be abbreviated. | |
309 | */ | |
310 | if(!partitions[i].precious || (strlen(partitions[i].name) == length)) | |
311 | p = &partitions[i]; | |
312 | break; | |
313 | } | |
314 | } | |
315 | ||
316 | if(p == NULL) { | |
317 | fprintf(stderr, "Warning: area name '"); | |
318 | fwrite(argv[1]+offset, length, 1, stderr); | |
319 | fprintf(stderr, "' unknown, ignored\n"); | |
320 | } else { | |
321 | fprintf(stderr, "Flashing %s from %s\n", p->name, argv[2+current_area]); | |
322 | PrepareFlash(p, argv[2+current_area], state); | |
323 | } | |
324 | current_area++; | |
325 | } | |
326 | } |