]> git.zerfleddert.de Git - proxmark3-svn/blob - client/hid-flasher/flash.c
Potential fix for 0-length usb packets seen on OSX
[proxmark3-svn] / client / hid-flasher / flash.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // ELF file flasher
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include "sleep.h"
15 #include "proxusb.h"
16 #include "flash.h"
17 #include "elf.h"
18 #include "proxendian.h"
19
20 // FIXME: what the fuckity fuck
21 unsigned int current_command = CMD_UNKNOWN;
22
23 #define FLASH_START 0x100000
24 #define FLASH_SIZE (256*1024)
25 #define FLASH_END (FLASH_START + FLASH_SIZE)
26 #define BOOTLOADER_SIZE 0x2000
27 #define BOOTLOADER_END (FLASH_START + BOOTLOADER_SIZE)
28
29 #define BLOCK_SIZE 0x100
30
31 static const uint8_t elf_ident[] = {
32 0x7f, 'E', 'L', 'F',
33 ELFCLASS32,
34 ELFDATA2LSB,
35 EV_CURRENT
36 };
37
38 // Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
39 // unaligned segments if needed
40 static int build_segs_from_phdrs(flash_file_t *ctx, FILE *fd, Elf32_Phdr *phdrs, int num_phdrs)
41 {
42 Elf32_Phdr *phdr = phdrs;
43 flash_seg_t *seg;
44 uint32_t last_end = 0;
45
46 ctx->segments = malloc(sizeof(flash_seg_t) * num_phdrs);
47 if (!ctx->segments) {
48 fprintf(stderr, "Out of memory\n");
49 return -1;
50 }
51 ctx->num_segs = 0;
52 seg = ctx->segments;
53
54 fprintf(stderr, "Loading usable ELF segments:\n");
55 for (int i = 0; i < num_phdrs; i++) {
56 if (le32(phdr->p_type) != PT_LOAD) {
57 phdr++;
58 continue;
59 }
60 uint32_t vaddr = le32(phdr->p_vaddr);
61 uint32_t paddr = le32(phdr->p_paddr);
62 uint32_t filesz = le32(phdr->p_filesz);
63 uint32_t memsz = le32(phdr->p_memsz);
64 uint32_t offset = le32(phdr->p_offset);
65 uint32_t flags = le32(phdr->p_flags);
66 if (!filesz) {
67 phdr++;
68 continue;
69 }
70 fprintf(stderr, "%d: V 0x%08x P 0x%08x (0x%08x->0x%08x) [%c%c%c] @0x%x\n",
71 i, vaddr, paddr, filesz, memsz,
72 flags & PF_R ? 'R' : ' ',
73 flags & PF_W ? 'W' : ' ',
74 flags & PF_X ? 'X' : ' ',
75 offset);
76 if (filesz != memsz) {
77 fprintf(stderr, "Error: PHDR file size does not equal memory size\n"
78 "(DATA+BSS PHDRs do not make sense on ROM platforms!)\n");
79 return -1;
80 }
81 if (paddr < last_end) {
82 fprintf(stderr, "Error: PHDRs not sorted or overlap\n");
83 return -1;
84 }
85 if (paddr < FLASH_START || (paddr+filesz) > FLASH_END) {
86 fprintf(stderr, "Error: PHDR is not contained in Flash\n");
87 return -1;
88 }
89 if (vaddr >= FLASH_START && vaddr < FLASH_END && (flags & PF_W)) {
90 fprintf(stderr, "Error: Flash VMA segment is writable\n");
91 return -1;
92 }
93
94 uint8_t *data;
95 // make extra space if we need to move the data forward
96 data = malloc(filesz + BLOCK_SIZE);
97 if (!data) {
98 fprintf(stderr, "Out of memory\n");
99 return -1;
100 }
101 if (fseek(fd, offset, SEEK_SET) < 0 || fread(data, 1, filesz, fd) != filesz) {
102 fprintf(stderr, "Error while reading PHDR payload\n");
103 free(data);
104 return -1;
105 }
106
107 uint32_t block_offset = paddr & (BLOCK_SIZE-1);
108 if (block_offset) {
109 if (ctx->num_segs) {
110 flash_seg_t *prev_seg = seg - 1;
111 uint32_t this_end = paddr + filesz;
112 uint32_t this_firstblock = paddr & ~(BLOCK_SIZE-1);
113 uint32_t prev_lastblock = (last_end - 1) & ~(BLOCK_SIZE-1);
114
115 if (this_firstblock == prev_lastblock) {
116 uint32_t new_length = this_end - prev_seg->start;
117 uint32_t this_offset = paddr - prev_seg->start;
118 uint32_t hole = this_offset - prev_seg->length;
119 uint8_t *new_data = malloc(new_length);
120 if (!new_data) {
121 fprintf(stderr, "Out of memory\n");
122 free(data);
123 return -1;
124 }
125 memset(new_data, 0xff, new_length);
126 memcpy(new_data, prev_seg->data, prev_seg->length);
127 memcpy(new_data + this_offset, data, filesz);
128 fprintf(stderr, "Note: Extending previous segment from 0x%x to 0x%x bytes\n",
129 prev_seg->length, new_length);
130 if (hole)
131 fprintf(stderr, "Note: 0x%x-byte hole created\n", hole);
132 free(data);
133 free(prev_seg->data);
134 prev_seg->data = new_data;
135 prev_seg->length = new_length;
136 last_end = this_end;
137 phdr++;
138 continue;
139 }
140 }
141 fprintf(stderr, "Warning: segment does not begin on a block boundary, will pad\n");
142 memmove(data + block_offset, data, filesz);
143 memset(data, 0xFF, block_offset);
144 filesz += block_offset;
145 paddr -= block_offset;
146 }
147
148 seg->data = data;
149 seg->start = paddr;
150 seg->length = filesz;
151 seg++;
152 ctx->num_segs++;
153
154 last_end = paddr + filesz;
155 phdr++;
156 }
157 return 0;
158 }
159
160 // Sanity check segments and check for bootloader writes
161 static int check_segs(flash_file_t *ctx, int can_write_bl) {
162 for (int i = 0; i < ctx->num_segs; i++) {
163 flash_seg_t *seg = &ctx->segments[i];
164
165 if (seg->start & (BLOCK_SIZE-1)) {
166 fprintf(stderr, "Error: Segment is not aligned\n");
167 return -1;
168 }
169 if (seg->start < FLASH_START) {
170 fprintf(stderr, "Error: Segment is outside of flash bounds\n");
171 return -1;
172 }
173 if (seg->start + seg->length > FLASH_END) {
174 fprintf(stderr, "Error: Segment is outside of flash bounds\n");
175 return -1;
176 }
177 if (!can_write_bl && seg->start < BOOTLOADER_END) {
178 fprintf(stderr, "Attempted to write bootloader but bootloader writes are not enabled\n");
179 return -1;
180 }
181 }
182 return 0;
183 }
184
185 // Load an ELF file and prepare it for flashing
186 int flash_load(flash_file_t *ctx, const char *name, int can_write_bl)
187 {
188 FILE *fd = NULL;
189 Elf32_Ehdr ehdr;
190 Elf32_Phdr *phdrs = NULL;
191 int num_phdrs;
192 int res;
193
194 fd = fopen(name, "rb");
195 if (!fd) {
196 fprintf(stderr, "Could not open file '%s': ", name);
197 perror(NULL);
198 goto fail;
199 }
200
201 fprintf(stderr, "Loading ELF file '%s'...\n", name);
202
203 if (fread(&ehdr, sizeof(ehdr), 1, fd) != 1) {
204 fprintf(stderr, "Error while reading ELF file header\n");
205 goto fail;
206 }
207 if (memcmp(ehdr.e_ident, elf_ident, sizeof(elf_ident))
208 || le32(ehdr.e_version) != 1)
209 {
210 fprintf(stderr, "Not an ELF file or wrong ELF type\n");
211 goto fail;
212 }
213 if (le16(ehdr.e_type) != ET_EXEC) {
214 fprintf(stderr, "ELF is not executable\n");
215 goto fail;
216 }
217 if (le16(ehdr.e_machine) != EM_ARM) {
218 fprintf(stderr, "Wrong ELF architecture\n");
219 goto fail;
220 }
221 if (!ehdr.e_phnum || !ehdr.e_phoff) {
222 fprintf(stderr, "ELF has no PHDRs\n");
223 goto fail;
224 }
225 if (le16(ehdr.e_phentsize) != sizeof(Elf32_Phdr)) {
226 // could be a structure padding issue...
227 fprintf(stderr, "Either the ELF file or this code is made of fail\n");
228 goto fail;
229 }
230 num_phdrs = le16(ehdr.e_phnum);
231
232 phdrs = malloc(le16(ehdr.e_phnum) * sizeof(Elf32_Phdr));
233 if (!phdrs) {
234 fprintf(stderr, "Out of memory\n");
235 goto fail;
236 }
237 if (fseek(fd, le32(ehdr.e_phoff), SEEK_SET) < 0) {
238 fprintf(stderr, "Error while reading ELF PHDRs\n");
239 goto fail;
240 }
241 if (fread(phdrs, sizeof(Elf32_Phdr), num_phdrs, fd) != num_phdrs) {
242 fprintf(stderr, "Error while reading ELF PHDRs\n");
243 goto fail;
244 }
245
246 res = build_segs_from_phdrs(ctx, fd, phdrs, num_phdrs);
247 if (res < 0)
248 goto fail;
249 res = check_segs(ctx, can_write_bl);
250 if (res < 0)
251 goto fail;
252
253 free(phdrs);
254 fclose(fd);
255 ctx->filename = name;
256 return 0;
257
258 fail:
259 if (phdrs)
260 free(phdrs);
261 if (fd)
262 fclose(fd);
263 flash_free(ctx);
264 return -1;
265 }
266
267 // Get the state of the proxmark, backwards compatible
268 static int get_proxmark_state(uint32_t *state)
269 {
270 UsbCommand c;
271 c.cmd = CMD_DEVICE_INFO;
272 SendCommand(&c);
273
274 UsbCommand resp;
275 ReceiveCommand(&resp);
276
277 // Three outcomes:
278 // 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
279 // 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
280 // 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
281
282 switch (resp.cmd) {
283 case CMD_ACK:
284 *state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM;
285 break;
286 case CMD_DEBUG_PRINT_STRING:
287 *state = DEVICE_INFO_FLAG_CURRENT_MODE_OS;
288 break;
289 case CMD_DEVICE_INFO:
290 *state = resp.arg[0];
291 break;
292 default:
293 fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04x\n", resp.cmd);
294 return -1;
295 break;
296 }
297
298 return 0;
299 }
300
301 // Enter the bootloader to be able to start flashing
302 static int enter_bootloader(void)
303 {
304 uint32_t state;
305
306 if (get_proxmark_state(&state) < 0)
307 return -1;
308
309 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) {
310 /* Already in flash state, we're done. */
311 return 0;
312 }
313
314 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
315 fprintf(stderr,"Entering bootloader...\n");
316 UsbCommand c;
317 memset(&c, 0, sizeof (c));
318
319 if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT)
320 && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT))
321 {
322 // New style handover: Send CMD_START_FLASH, which will reset the board
323 // and enter the bootrom on the next boot.
324 c.cmd = CMD_START_FLASH;
325 SendCommand(&c);
326 fprintf(stderr,"(Press and release the button only to abort)\n");
327 } else {
328 // Old style handover: Ask the user to press the button, then reset the board
329 c.cmd = CMD_HARDWARE_RESET;
330 SendCommand(&c);
331 fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
332 }
333 fprintf(stderr,"Waiting for Proxmark to reappear on USB...");
334
335 CloseProxmark();
336 sleep(1);
337 while (!OpenProxmark(0)) {
338 sleep(1);
339 fprintf(stderr, ".");
340 }
341 fprintf(stderr," Found.\n");
342
343 return 0;
344 }
345
346 fprintf(stderr, "Error: Unknown Proxmark mode\n");
347 return -1;
348 }
349
350 static int wait_for_ack(void)
351 {
352 UsbCommand ack;
353 ReceiveCommand(&ack);
354 if (ack.cmd != CMD_ACK) {
355 printf("Error: Unexpected reply 0x%04x (expected ACK)\n", ack.cmd);
356 return -1;
357 }
358 return 0;
359 }
360
361 // Go into flashing mode
362 int flash_start_flashing(int enable_bl_writes)
363 {
364 uint32_t state;
365
366 if (enter_bootloader() < 0)
367 return -1;
368
369 if (get_proxmark_state(&state) < 0)
370 return -1;
371
372 if (state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
373 // This command is stupid. Why the heck does it care which area we're
374 // flashing, as long as it's not the bootloader area? The mind boggles.
375 UsbCommand c = {CMD_START_FLASH};
376
377 if (enable_bl_writes) {
378 c.arg[0] = FLASH_START;
379 c.arg[1] = FLASH_END;
380 c.arg[2] = START_FLASH_MAGIC;
381 } else {
382 c.arg[0] = BOOTLOADER_END;
383 c.arg[1] = FLASH_END;
384 c.arg[2] = 0;
385 }
386 SendCommand(&c);
387 return wait_for_ack();
388 } else {
389 fprintf(stderr, "Note: Your bootloader does not understand the new START_FLASH command\n");
390 fprintf(stderr, " It is recommended that you update your bootloader\n\n");
391 }
392
393 return 0;
394 }
395
396 static int write_block(uint32_t address, uint8_t *data, uint32_t length)
397 {
398 uint8_t block_buf[BLOCK_SIZE];
399
400 memset(block_buf, 0xFF, BLOCK_SIZE);
401 memcpy(block_buf, data, length);
402
403 UsbCommand c = {CMD_SETUP_WRITE};
404 for (int i = 0; i < 240; i += 48) {
405 memcpy(c.d.asBytes, block_buf + i, 48);
406 c.arg[0] = i / 4;
407 SendCommand(&c);
408 if (wait_for_ack() < 0)
409 return -1;
410 }
411
412 c.cmd = CMD_FINISH_WRITE;
413 c.arg[0] = address;
414 memcpy(c.d.asBytes, block_buf+240, 16);
415 SendCommand(&c);
416 return wait_for_ack();
417 }
418
419 // Write a file's segments to Flash
420 int flash_write(flash_file_t *ctx)
421 {
422 fprintf(stderr, "Writing segments for file: %s\n", ctx->filename);
423 for (int i = 0; i < ctx->num_segs; i++) {
424 flash_seg_t *seg = &ctx->segments[i];
425
426 uint32_t length = seg->length;
427 uint32_t blocks = (length + BLOCK_SIZE - 1) / BLOCK_SIZE;
428 uint32_t end = seg->start + length;
429
430 fprintf(stderr, " 0x%08x..0x%08x [0x%x / %d blocks]",
431 seg->start, end - 1, length, blocks);
432
433 int block = 0;
434 uint8_t *data = seg->data;
435 uint32_t baddr = seg->start;
436
437 while (length) {
438 uint32_t block_size = length;
439 if (block_size > BLOCK_SIZE)
440 block_size = BLOCK_SIZE;
441
442 if (write_block(baddr, data, block_size) < 0) {
443 fprintf(stderr, " ERROR\n");
444 fprintf(stderr, "Error writing block %d of %d\n", block, blocks);
445 return -1;
446 }
447
448 data += block_size;
449 baddr += block_size;
450 length -= block_size;
451 block++;
452 fprintf(stderr, ".");
453 }
454 fprintf(stderr, " OK\n");
455 }
456 return 0;
457 }
458
459 // free a file context
460 void flash_free(flash_file_t *ctx)
461 {
462 if (!ctx)
463 return;
464 if (ctx->segments) {
465 for (int i = 0; i < ctx->num_segs; i++)
466 free(ctx->segments[i].data);
467 free(ctx->segments);
468 ctx->segments = NULL;
469 ctx->num_segs = 0;
470 }
471 }
472
473 // just reset the unit
474 int flash_stop_flashing(void) {
475 UsbCommand c = {CMD_HARDWARE_RESET};
476 SendCommand(&c);
477 return 0;
478 }
Impressum, Datenschutz