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