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