1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
14 #include "proxmark3.h"
16 //#include "proxusb.h"
19 #include "proxendian.h"
22 void SendCommand(UsbCommand
* txcmd
);
23 void ReceiveCommand(UsbCommand
* rxcmd
);
25 int OpenProxmark(size_t i
);
27 // FIXME: what the fuckity fuck
28 unsigned int current_command
= CMD_UNKNOWN
;
30 #define FLASH_START 0x100000
31 #define FLASH_SIZE (256*1024)
32 #define FLASH_END (FLASH_START + FLASH_SIZE)
33 #define BOOTLOADER_SIZE 0x2000
34 #define BOOTLOADER_END (FLASH_START + BOOTLOADER_SIZE)
36 #define BLOCK_SIZE 0x200
38 static const uint8_t elf_ident
[] = {
45 // Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
46 // unaligned segments if needed
47 static int build_segs_from_phdrs(flash_file_t
*ctx
, FILE *fd
, Elf32_Phdr
*phdrs
, int num_phdrs
)
49 Elf32_Phdr
*phdr
= phdrs
;
51 uint32_t last_end
= 0;
53 ctx
->segments
= malloc(sizeof(flash_seg_t
) * num_phdrs
);
55 fprintf(stderr
, "Out of memory\n");
61 fprintf(stderr
, "Loading usable ELF segments:\n");
62 for (int i
= 0; i
< num_phdrs
; i
++) {
63 if (le32(phdr
->p_type
) != PT_LOAD
) {
67 uint32_t vaddr
= le32(phdr
->p_vaddr
);
68 uint32_t paddr
= le32(phdr
->p_paddr
);
69 uint32_t filesz
= le32(phdr
->p_filesz
);
70 uint32_t memsz
= le32(phdr
->p_memsz
);
71 uint32_t offset
= le32(phdr
->p_offset
);
72 uint32_t flags
= le32(phdr
->p_flags
);
77 fprintf(stderr
, "%d: V 0x%08x P 0x%08x (0x%08x->0x%08x) [%c%c%c] @0x%x\n",
78 i
, vaddr
, paddr
, filesz
, memsz
,
79 flags
& PF_R
? 'R' : ' ',
80 flags
& PF_W
? 'W' : ' ',
81 flags
& PF_X
? 'X' : ' ',
83 if (filesz
!= memsz
) {
84 fprintf(stderr
, "Error: PHDR file size does not equal memory size\n"
85 "(DATA+BSS PHDRs do not make sense on ROM platforms!)\n");
88 if (paddr
< last_end
) {
89 fprintf(stderr
, "Error: PHDRs not sorted or overlap\n");
92 if (paddr
< FLASH_START
|| (paddr
+filesz
) > FLASH_END
) {
93 fprintf(stderr
, "Error: PHDR is not contained in Flash\n");
96 if (vaddr
>= FLASH_START
&& vaddr
< FLASH_END
&& (flags
& PF_W
)) {
97 fprintf(stderr
, "Error: Flash VMA segment is writable\n");
102 // make extra space if we need to move the data forward
103 data
= malloc(filesz
+ BLOCK_SIZE
);
105 fprintf(stderr
, "Out of memory\n");
108 if (fseek(fd
, offset
, SEEK_SET
) < 0 || fread(data
, 1, filesz
, fd
) != filesz
) {
109 fprintf(stderr
, "Error while reading PHDR payload\n");
114 uint32_t block_offset
= paddr
& (BLOCK_SIZE
-1);
117 flash_seg_t
*prev_seg
= seg
- 1;
118 uint32_t this_end
= paddr
+ filesz
;
119 uint32_t this_firstblock
= paddr
& ~(BLOCK_SIZE
-1);
120 uint32_t prev_lastblock
= (last_end
- 1) & ~(BLOCK_SIZE
-1);
122 if (this_firstblock
== prev_lastblock
) {
123 uint32_t new_length
= this_end
- prev_seg
->start
;
124 uint32_t this_offset
= paddr
- prev_seg
->start
;
125 uint32_t hole
= this_offset
- prev_seg
->length
;
126 uint8_t *new_data
= malloc(new_length
);
128 fprintf(stderr
, "Out of memory\n");
132 memset(new_data
, 0xff, new_length
);
133 memcpy(new_data
, prev_seg
->data
, prev_seg
->length
);
134 memcpy(new_data
+ this_offset
, data
, filesz
);
135 fprintf(stderr
, "Note: Extending previous segment from 0x%x to 0x%x bytes\n",
136 prev_seg
->length
, new_length
);
138 fprintf(stderr
, "Note: 0x%x-byte hole created\n", hole
);
140 free(prev_seg
->data
);
141 prev_seg
->data
= new_data
;
142 prev_seg
->length
= new_length
;
148 fprintf(stderr
, "Warning: segment does not begin on a block boundary, will pad\n");
149 memmove(data
+ block_offset
, data
, filesz
);
150 memset(data
, 0xFF, block_offset
);
151 filesz
+= block_offset
;
152 paddr
-= block_offset
;
157 seg
->length
= filesz
;
161 last_end
= paddr
+ filesz
;
167 // Sanity check segments and check for bootloader writes
168 static int check_segs(flash_file_t
*ctx
, int can_write_bl
) {
169 for (int i
= 0; i
< ctx
->num_segs
; i
++) {
170 flash_seg_t
*seg
= &ctx
->segments
[i
];
172 if (seg
->start
& (BLOCK_SIZE
-1)) {
173 fprintf(stderr
, "Error: Segment is not aligned\n");
176 if (seg
->start
< FLASH_START
) {
177 fprintf(stderr
, "Error: Segment is outside of flash bounds\n");
180 if (seg
->start
+ seg
->length
> FLASH_END
) {
181 fprintf(stderr
, "Error: Segment is outside of flash bounds\n");
184 if (!can_write_bl
&& seg
->start
< BOOTLOADER_END
) {
185 fprintf(stderr
, "Attempted to write bootloader but bootloader writes are not enabled\n");
192 // Load an ELF file and prepare it for flashing
193 int flash_load(flash_file_t
*ctx
, const char *name
, int can_write_bl
)
197 Elf32_Phdr
*phdrs
= NULL
;
201 fd
= fopen(name
, "rb");
203 fprintf(stderr
, "Could not open file '%s': ", name
);
208 fprintf(stderr
, "Loading ELF file '%s'...\n", name
);
210 if (fread(&ehdr
, sizeof(ehdr
), 1, fd
) != 1) {
211 fprintf(stderr
, "Error while reading ELF file header\n");
214 if (memcmp(ehdr
.e_ident
, elf_ident
, sizeof(elf_ident
))
215 || le32(ehdr
.e_version
) != 1)
217 fprintf(stderr
, "Not an ELF file or wrong ELF type\n");
220 if (le16(ehdr
.e_type
) != ET_EXEC
) {
221 fprintf(stderr
, "ELF is not executable\n");
224 if (le16(ehdr
.e_machine
) != EM_ARM
) {
225 fprintf(stderr
, "Wrong ELF architecture\n");
228 if (!ehdr
.e_phnum
|| !ehdr
.e_phoff
) {
229 fprintf(stderr
, "ELF has no PHDRs\n");
232 if (le16(ehdr
.e_phentsize
) != sizeof(Elf32_Phdr
)) {
233 // could be a structure padding issue...
234 fprintf(stderr
, "Either the ELF file or this code is made of fail\n");
237 num_phdrs
= le16(ehdr
.e_phnum
);
239 phdrs
= malloc(le16(ehdr
.e_phnum
) * sizeof(Elf32_Phdr
));
241 fprintf(stderr
, "Out of memory\n");
244 if (fseek(fd
, le32(ehdr
.e_phoff
), SEEK_SET
) < 0) {
245 fprintf(stderr
, "Error while reading ELF PHDRs\n");
248 if (fread(phdrs
, sizeof(Elf32_Phdr
), num_phdrs
, fd
) != num_phdrs
) {
249 fprintf(stderr
, "Error while reading ELF PHDRs\n");
253 res
= build_segs_from_phdrs(ctx
, fd
, phdrs
, num_phdrs
);
256 res
= check_segs(ctx
, can_write_bl
);
262 ctx
->filename
= name
;
274 // Get the state of the proxmark, backwards compatible
275 static int get_proxmark_state(uint32_t *state
)
278 c
.cmd
= CMD_DEVICE_INFO
;
282 ReceiveCommand(&resp
);
285 // 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
286 // 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
287 // 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
291 *state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
293 case CMD_DEBUG_PRINT_STRING
:
294 *state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
296 case CMD_DEVICE_INFO
:
297 *state
= resp
.arg
[0];
300 fprintf(stderr
, "Error: Couldn't get proxmark state, bad response type: 0x%04"llx
"\n", resp
.cmd
);
308 // Enter the bootloader to be able to start flashing
309 static int enter_bootloader(char *serial_port_name
)
313 if (get_proxmark_state(&state
) < 0)
316 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
) {
317 /* Already in flash state, we're done. */
321 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
322 fprintf(stderr
,"Entering bootloader...\n");
324 memset(&c
, 0, sizeof (c
));
326 if ((state
& DEVICE_INFO_FLAG_BOOTROM_PRESENT
)
327 && (state
& DEVICE_INFO_FLAG_OSIMAGE_PRESENT
))
329 // New style handover: Send CMD_START_FLASH, which will reset the board
330 // and enter the bootrom on the next boot.
331 c
.cmd
= CMD_START_FLASH
;
333 fprintf(stderr
,"(Press and release the button only to abort)\n");
335 // Old style handover: Ask the user to press the button, then reset the board
336 c
.cmd
= CMD_HARDWARE_RESET
;
338 fprintf(stderr
,"Press and hold down button NOW if your bootloader requires it.\n");
343 fprintf(stderr
,"Waiting for Proxmark to reappear on %s",serial_port_name
);
346 fprintf(stderr
, ".");
347 } while (!OpenProxmark(0));
348 fprintf(stderr
," Found.\n");
353 fprintf(stderr
, "Error: Unknown Proxmark mode\n");
357 static int wait_for_ack(void)
360 ReceiveCommand(&ack
);
361 if (ack
.cmd
!= CMD_ACK
) {
362 printf("Error: Unexpected reply 0x%04"llx
" (expected ACK)\n", ack
.cmd
);
368 // Go into flashing mode
369 int flash_start_flashing(int enable_bl_writes
,char *serial_port_name
)
373 if (enter_bootloader(serial_port_name
) < 0)
376 if (get_proxmark_state(&state
) < 0)
379 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
380 // This command is stupid. Why the heck does it care which area we're
381 // flashing, as long as it's not the bootloader area? The mind boggles.
382 UsbCommand c
= {CMD_START_FLASH
};
384 if (enable_bl_writes
) {
385 c
.arg
[0] = FLASH_START
;
386 c
.arg
[1] = FLASH_END
;
387 c
.arg
[2] = START_FLASH_MAGIC
;
389 c
.arg
[0] = BOOTLOADER_END
;
390 c
.arg
[1] = FLASH_END
;
395 return wait_for_ack();
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");
404 static int write_block(uint32_t address
, uint8_t *data
, uint32_t length
)
406 uint8_t block_buf
[BLOCK_SIZE
];
408 memset(block_buf
, 0xFF, BLOCK_SIZE
);
409 memcpy(block_buf
, data
, length
);
412 c.cmd = {CMD_SETUP_WRITE};
413 for (int i = 0; i < 240; i += 48) {
414 memcpy(c.d.asBytes, block_buf + i, 48);
418 if (wait_for_ack() < 0) {
423 c
.cmd
= CMD_FINISH_WRITE
;
425 // memcpy(c.d.asBytes, block_buf+240, 16);
427 memcpy(c
.d
.asBytes
, block_buf
, length
);
429 return wait_for_ack();
432 // Write a file's segments to Flash
433 int flash_write(flash_file_t
*ctx
)
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
];
439 uint32_t length
= seg
->length
;
440 uint32_t blocks
= (length
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
441 uint32_t end
= seg
->start
+ length
;
443 fprintf(stderr
, " 0x%08x..0x%08x [0x%x / %d blocks]",
444 seg
->start
, end
- 1, length
, blocks
);
447 uint8_t *data
= seg
->data
;
448 uint32_t baddr
= seg
->start
;
451 uint32_t block_size
= length
;
452 if (block_size
> BLOCK_SIZE
)
453 block_size
= BLOCK_SIZE
;
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
);
463 length
-= block_size
;
465 fprintf(stderr
, ".");
467 fprintf(stderr
, " OK\n");
472 // free a file context
473 void flash_free(flash_file_t
*ctx
)
478 for (int i
= 0; i
< ctx
->num_segs
; i
++)
479 free(ctx
->segments
[i
].data
);
481 ctx
->segments
= NULL
;
486 // just reset the unit
487 int flash_stop_flashing(void) {
488 UsbCommand c
= {CMD_HARDWARE_RESET
};