]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/flash.c
CHG: `lf read` - forgot a var
[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" PRIx64 "\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 /* Already in flash state, we're done. */
322 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM)
323 return 0;
324
325 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
326 fprintf(stderr,"Entering bootloader...\n");
327 UsbCommand c;
328 memset(&c, 0, sizeof (c));
329
330 if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT)
331 && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT)) {
332 // New style handover: Send CMD_START_FLASH, which will reset the board
333 // and enter the bootrom on the next boot.
334 c.cmd = CMD_START_FLASH;
335 SendCommand(&c);
336 fprintf(stderr,"(Press and release the button only to abort)\n");
337 } else {
338 // Old style handover: Ask the user to press the button, then reset the board
339 c.cmd = CMD_HARDWARE_RESET;
340 SendCommand(&c);
341 fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
342 }
343 msleep(100);
344 CloseProxmark();
345
346 fprintf(stderr,"Waiting for Proxmark to reappear on %s",serial_port_name);
347 do {
348 sleep(1);
349 fprintf(stderr, ".");
350 } while (!OpenProxmark(0));
351 fprintf(stderr," Found.\n");
352 return 0;
353 }
354
355 fprintf(stderr, "Error: Unknown Proxmark mode\n");
356 return -1;
357 }
358
359 static int wait_for_ack(UsbCommand *ack)
360 {
361 ReceiveCommand(ack);
362 if (ack->cmd != CMD_ACK) {
363 printf("Error: Unexpected reply 0x%04" PRIx64 " %s (expected ACK)\n", ack->cmd, (ack->cmd==CMD_NACK)?"NACK":"");
364 return -1;
365 }
366 return 0;
367 }
368
369 // Go into flashing mode
370 int flash_start_flashing(int enable_bl_writes,char *serial_port_name)
371 {
372 uint32_t state;
373
374 if (enter_bootloader(serial_port_name) < 0)
375 return -1;
376
377 if (get_proxmark_state(&state) < 0)
378 return -1;
379
380 if (state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
381 // This command is stupid. Why the heck does it care which area we're
382 // flashing, as long as it's not the bootloader area? The mind boggles.
383 UsbCommand c = {CMD_START_FLASH};
384
385 if (enable_bl_writes) {
386 c.arg[0] = FLASH_START;
387 c.arg[1] = FLASH_END;
388 c.arg[2] = START_FLASH_MAGIC;
389 } else {
390 c.arg[0] = BOOTLOADER_END;
391 c.arg[1] = FLASH_END;
392 c.arg[2] = 0;
393 }
394 SendCommand(&c);
395 return wait_for_ack(&c);
396 } else {
397 fprintf(stderr, "Note: Your bootloader does not understand the new START_FLASH command\n");
398 fprintf(stderr, " It is recommended that you update your bootloader\n\n");
399 }
400
401 return 0;
402 }
403
404 static int write_block(uint32_t address, uint8_t *data, uint32_t length)
405 {
406 uint8_t block_buf[BLOCK_SIZE];
407 memset(block_buf, 0xFF, BLOCK_SIZE);
408 memcpy(block_buf, data, length);
409 UsbCommand c = {CMD_FINISH_WRITE, {address, 0, 0}};
410 memcpy(c.d.asBytes, block_buf, length);
411 SendCommand(&c);
412 int ret = wait_for_ack(&c);
413 if (ret && c.arg[0]) {
414 uint32_t lock_bits = c.arg[0] >> 16;
415 bool lock_error = c.arg[0] & AT91C_MC_LOCKE;
416 bool prog_error = c.arg[0] & AT91C_MC_PROGE;
417 bool security_bit = c.arg[0] & AT91C_MC_SECURITY;
418 printf("%s", lock_error ? " Lock Error\n" : "");
419 printf("%s", prog_error ? " Invalid Command or bad Keyword\n" : "");
420 printf("%s", security_bit ? " Security Bit is set!\n" : "");
421 printf(" Lock Bits: 0x%04x\n", lock_bits);
422 }
423 return ret;
424 }
425
426 // Write a file's segments to Flash
427 int flash_write(flash_file_t *ctx)
428 {
429 fprintf(stderr, "Writing segments for file: %s\n", ctx->filename);
430 for (int i = 0; i < ctx->num_segs; i++) {
431 flash_seg_t *seg = &ctx->segments[i];
432
433 uint32_t length = seg->length;
434 uint32_t blocks = (length + BLOCK_SIZE - 1) / BLOCK_SIZE;
435 uint32_t end = seg->start + length;
436
437 fprintf(stderr, " 0x%08x..0x%08x [0x%x / %d blocks]",
438 seg->start, end - 1, length, blocks);
439
440 int block = 0;
441 uint8_t *data = seg->data;
442 uint32_t baddr = seg->start;
443
444 while (length) {
445 uint32_t block_size = length;
446 if (block_size > BLOCK_SIZE)
447 block_size = BLOCK_SIZE;
448
449 if (write_block(baddr, data, block_size) < 0) {
450 fprintf(stderr, " ERROR\n");
451 fprintf(stderr, "Error writing block %d of %d\n", block, blocks);
452 return -1;
453 }
454
455 data += block_size;
456 baddr += block_size;
457 length -= block_size;
458 block++;
459 fprintf(stderr, ".");
460 }
461 fprintf(stderr, " OK\n");
462 }
463 return 0;
464 }
465
466 // free a file context
467 void flash_free(flash_file_t *ctx)
468 {
469 if (!ctx)
470 return;
471 if (ctx->segments) {
472 for (int i = 0; i < ctx->num_segs; i++)
473 free(ctx->segments[i].data);
474 free(ctx->segments);
475 ctx->segments = NULL;
476 ctx->num_segs = 0;
477 }
478 }
479
480 // just reset the unit
481 int flash_stop_flashing(void) {
482 UsbCommand c = {CMD_HARDWARE_RESET};
483 SendCommand(&c);
484 msleep(100);
485 return 0;
486 }
Impressum, Datenschutz