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