]> git.zerfleddert.de Git - proxmark3-svn/blame - client/flash.c
BUG: forgot to remove
[proxmark3-svn] / client / flash.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
8fe1a992 2// Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
3//
a553f267 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//-----------------------------------------------------------------------------
8fe1a992 8// ELF file flasher
a553f267 9//-----------------------------------------------------------------------------
10
6e4d4ee6 11#include <stdio.h>
6e4d4ee6 12#include <string.h>
83a9b236 13#include <stdlib.h>
125a98a1 14#include "proxmark3.h"
4cd41f34 15#include "sleep.h"
6e4d4ee6 16#include "flash.h"
2cab856f 17#include "elf.h"
8fe1a992 18#include "proxendian.h"
28fdb04f 19#include "usb_cmd.h"
7838f4be 20#include "at91sam7s512.h"
28fdb04f 21
22void SendCommand(UsbCommand* txcmd);
23void ReceiveCommand(UsbCommand* rxcmd);
24void CloseProxmark();
25int OpenProxmark(size_t i);
6e4d4ee6 26
0ae6234a 27// FIXME: what the fuckity fuck
8fe1a992 28unsigned int current_command = CMD_UNKNOWN;
29
30#define FLASH_START 0x100000
5a085457 31
32#ifdef HAS_512_FLASH
33# define FLASH_SIZE (512*1024)
34#else
35# define FLASH_SIZE (256*1024)
36#endif
37
8fe1a992 38#define FLASH_END (FLASH_START + FLASH_SIZE)
39#define BOOTLOADER_SIZE 0x2000
40#define BOOTLOADER_END (FLASH_START + BOOTLOADER_SIZE)
41
28fdb04f 42#define BLOCK_SIZE 0x200
8fe1a992 43
44static 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
53static int build_segs_from_phdrs(flash_file_t *ctx, FILE *fd, Elf32_Phdr *phdrs, int num_phdrs)
7fe9b0b7 54{
8fe1a992 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;
6e4d4ee6 171}
172
8fe1a992 173// Sanity check segments and check for bootloader writes
174static 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
199int 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
66d6ba70 266 free(phdrs);
8fe1a992 267 fclose(fd);
268 ctx->filename = name;
269 return 0;
270
271fail:
272 if (phdrs)
273 free(phdrs);
274 if (fd)
275 fclose(fd);
276 flash_free(ctx);
277 return -1;
278}
6e4d4ee6 279
8fe1a992 280// Get the state of the proxmark, backwards compatible
281static int get_proxmark_state(uint32_t *state)
6e4d4ee6 282{
28fdb04f 283 UsbCommand c;
8fe1a992 284 c.cmd = CMD_DEVICE_INFO;
f62b5e12 285 SendCommand(&c);
28fdb04f 286 UsbCommand resp;
8fe1a992 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:
9c624f67 305 fprintf(stderr, "Error: Couldn't get proxmark state, bad response type: 0x%04" PRIx64 "\n", resp.cmd);
8fe1a992 306 return -1;
307 break;
308 }
309
310 return 0;
6e4d4ee6 311}
312
8fe1a992 313// Enter the bootloader to be able to start flashing
e12b82d3 314static int enter_bootloader(char *serial_port_name)
6e4d4ee6 315{
8fe1a992 316 uint32_t state;
317
318 if (get_proxmark_state(&state) < 0)
319 return -1;
320
5ebba500 321 /* Already in flash state, we're done. */
322 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM)
8fe1a992 323 return 0;
8fe1a992 324
325 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
326 fprintf(stderr,"Entering bootloader...\n");
28fdb04f 327 UsbCommand c;
8fe1a992 328 memset(&c, 0, sizeof (c));
329
330 if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT)
5ebba500 331 && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT)) {
8fe1a992 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;
28fdb04f 335 SendCommand(&c);
8fe1a992 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;
28fdb04f 340 SendCommand(&c);
8fe1a992 341 fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
342 }
5ebba500 343 msleep(100);
8fe1a992 344 CloseProxmark();
d8193fa5 345
e654346b 346 fprintf(stderr,"Waiting for Proxmark to reappear on %s",serial_port_name);
5ebba500 347 do {
8fe1a992 348 sleep(1);
349 fprintf(stderr, ".");
d8193fa5 350 } while (!OpenProxmark(0));
8fe1a992 351 fprintf(stderr," Found.\n");
8fe1a992 352 return 0;
353 }
354
355 fprintf(stderr, "Error: Unknown Proxmark mode\n");
356 return -1;
6e4d4ee6 357}
358
5ebba500 359static int wait_for_ack(UsbCommand *ack)
6e4d4ee6 360{
7838f4be 361 ReceiveCommand(ack);
362 if (ack->cmd != CMD_ACK) {
9c624f67 363 printf("Error: Unexpected reply 0x%04" PRIx64 " %s (expected ACK)\n", ack->cmd, (ack->cmd==CMD_NACK)?"NACK":"");
8fe1a992 364 return -1;
365 }
366 return 0;
6e4d4ee6 367}
368
8fe1a992 369// Go into flashing mode
e12b82d3 370int flash_start_flashing(int enable_bl_writes,char *serial_port_name)
6e4d4ee6 371{
8fe1a992 372 uint32_t state;
373
e12b82d3 374 if (enter_bootloader(serial_port_name) < 0)
8fe1a992 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.
28fdb04f 383 UsbCommand c = {CMD_START_FLASH};
8fe1a992 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 }
28fdb04f 394 SendCommand(&c);
7838f4be 395 return wait_for_ack(&c);
8fe1a992 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;
6e4d4ee6 402}
403
8fe1a992 404static int write_block(uint32_t address, uint8_t *data, uint32_t length)
6e4d4ee6 405{
8fe1a992 406 uint8_t block_buf[BLOCK_SIZE];
8fe1a992 407 memset(block_buf, 0xFF, BLOCK_SIZE);
408 memcpy(block_buf, data, length);
5ebba500 409 UsbCommand c = {CMD_FINISH_WRITE, {address, 0, 0}};
28fdb04f 410 memcpy(c.d.asBytes, block_buf, length);
5ebba500 411 SendCommand(&c);
7838f4be 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;
5ebba500 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" : "");
7838f4be 421 printf(" Lock Bits: 0x%04x\n", lock_bits);
422 }
423 return ret;
6e4d4ee6 424}
425
8fe1a992 426// Write a file's segments to Flash
427int flash_write(flash_file_t *ctx)
6e4d4ee6 428{
8fe1a992 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;
6e4d4ee6 464}
465
8fe1a992 466// free a file context
467void flash_free(flash_file_t *ctx)
7fe9b0b7 468{
8fe1a992 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
481int flash_stop_flashing(void) {
28fdb04f 482 UsbCommand c = {CMD_HARDWARE_RESET};
5ebba500 483 SendCommand(&c);
484 msleep(100);
485 return 0;
6e4d4ee6 486}
Impressum, Datenschutz