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 //-----------------------------------------------------------------------------
17 #include "proxmark3.h"
19 #include "util_posix.h"
22 #include "proxendian.h"
26 // FIXME: what the fuckity fuck
27 unsigned int current_command
= CMD_UNKNOWN
;
29 #define FLASH_START 0x100000
30 #define FLASH_SIZE (256*1024)
31 #define FLASH_END (FLASH_START + FLASH_SIZE)
32 #define BOOTLOADER_SIZE 0x2000
33 #define BOOTLOADER_END (FLASH_START + BOOTLOADER_SIZE)
35 #define BLOCK_SIZE 0x200
37 static const uint8_t elf_ident
[] = {
44 void CloseProxmark(receiver_arg
* conn
, char* serial_port_name
) {
45 pthread_mutex_lock(&conn
->recv_lock
);
47 // Block the port from being used by anything
48 serial_port
* my_port
= GetSerialPort();
51 // Then close the port.
53 pthread_mutex_unlock(&conn
->recv_lock
);
55 // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/*
56 unlink(serial_port_name
);
59 bool OpenProxmark(char* serial_port_name
) {
60 serial_port
*new_port
= uart_open(serial_port_name
);
61 if (new_port
== INVALID_SERIAL_PORT
|| new_port
== CLAIMED_SERIAL_PORT
) {
66 SetSerialPort(new_port
);
70 // Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
71 // unaligned segments if needed
72 static int build_segs_from_phdrs(flash_file_t
*ctx
, FILE *fd
, Elf32_Phdr
*phdrs
, int num_phdrs
)
74 Elf32_Phdr
*phdr
= phdrs
;
76 uint32_t last_end
= 0;
78 ctx
->segments
= malloc(sizeof(flash_seg_t
) * num_phdrs
);
80 fprintf(stderr
, "Out of memory\n");
86 fprintf(stderr
, "Loading usable ELF segments:\n");
87 for (int i
= 0; i
< num_phdrs
; i
++) {
88 if (le32(phdr
->p_type
) != PT_LOAD
) {
92 uint32_t vaddr
= le32(phdr
->p_vaddr
);
93 uint32_t paddr
= le32(phdr
->p_paddr
);
94 uint32_t filesz
= le32(phdr
->p_filesz
);
95 uint32_t memsz
= le32(phdr
->p_memsz
);
96 uint32_t offset
= le32(phdr
->p_offset
);
97 uint32_t flags
= le32(phdr
->p_flags
);
102 fprintf(stderr
, "%d: V 0x%08x P 0x%08x (0x%08x->0x%08x) [%c%c%c] @0x%x\n",
103 i
, vaddr
, paddr
, filesz
, memsz
,
104 flags
& PF_R
? 'R' : ' ',
105 flags
& PF_W
? 'W' : ' ',
106 flags
& PF_X
? 'X' : ' ',
108 if (filesz
!= memsz
) {
109 fprintf(stderr
, "Error: PHDR file size does not equal memory size\n"
110 "(DATA+BSS PHDRs do not make sense on ROM platforms!)\n");
113 if (paddr
< last_end
) {
114 fprintf(stderr
, "Error: PHDRs not sorted or overlap\n");
117 if (paddr
< FLASH_START
|| (paddr
+filesz
) > FLASH_END
) {
118 fprintf(stderr
, "Error: PHDR is not contained in Flash\n");
121 if (vaddr
>= FLASH_START
&& vaddr
< FLASH_END
&& (flags
& PF_W
)) {
122 fprintf(stderr
, "Error: Flash VMA segment is writable\n");
127 // make extra space if we need to move the data forward
128 data
= malloc(filesz
+ BLOCK_SIZE
);
130 fprintf(stderr
, "Out of memory\n");
133 if (fseek(fd
, offset
, SEEK_SET
) < 0 || fread(data
, 1, filesz
, fd
) != filesz
) {
134 fprintf(stderr
, "Error while reading PHDR payload\n");
139 uint32_t block_offset
= paddr
& (BLOCK_SIZE
-1);
142 flash_seg_t
*prev_seg
= seg
- 1;
143 uint32_t this_end
= paddr
+ filesz
;
144 uint32_t this_firstblock
= paddr
& ~(BLOCK_SIZE
-1);
145 uint32_t prev_lastblock
= (last_end
- 1) & ~(BLOCK_SIZE
-1);
147 if (this_firstblock
== prev_lastblock
) {
148 uint32_t new_length
= this_end
- prev_seg
->start
;
149 uint32_t this_offset
= paddr
- prev_seg
->start
;
150 uint32_t hole
= this_offset
- prev_seg
->length
;
151 uint8_t *new_data
= malloc(new_length
);
153 fprintf(stderr
, "Out of memory\n");
157 memset(new_data
, 0xff, new_length
);
158 memcpy(new_data
, prev_seg
->data
, prev_seg
->length
);
159 memcpy(new_data
+ this_offset
, data
, filesz
);
160 fprintf(stderr
, "Note: Extending previous segment from 0x%x to 0x%x bytes\n",
161 prev_seg
->length
, new_length
);
163 fprintf(stderr
, "Note: 0x%x-byte hole created\n", hole
);
165 free(prev_seg
->data
);
166 prev_seg
->data
= new_data
;
167 prev_seg
->length
= new_length
;
173 fprintf(stderr
, "Warning: segment does not begin on a block boundary, will pad\n");
174 memmove(data
+ block_offset
, data
, filesz
);
175 memset(data
, 0xFF, block_offset
);
176 filesz
+= block_offset
;
177 paddr
-= block_offset
;
182 seg
->length
= filesz
;
186 last_end
= paddr
+ filesz
;
192 // Sanity check segments and check for bootloader writes
193 static int check_segs(flash_file_t
*ctx
, int can_write_bl
) {
194 for (int i
= 0; i
< ctx
->num_segs
; i
++) {
195 flash_seg_t
*seg
= &ctx
->segments
[i
];
197 if (seg
->start
& (BLOCK_SIZE
-1)) {
198 fprintf(stderr
, "Error: Segment is not aligned\n");
201 if (seg
->start
< FLASH_START
) {
202 fprintf(stderr
, "Error: Segment is outside of flash bounds\n");
205 if (seg
->start
+ seg
->length
> FLASH_END
) {
206 fprintf(stderr
, "Error: Segment is outside of flash bounds\n");
209 if (!can_write_bl
&& seg
->start
< BOOTLOADER_END
) {
210 fprintf(stderr
, "Attempted to write bootloader but bootloader writes are not enabled\n");
217 // Load an ELF file and prepare it for flashing
218 int flash_load(flash_file_t
*ctx
, const char *name
, int can_write_bl
)
222 Elf32_Phdr
*phdrs
= NULL
;
226 fd
= fopen(name
, "rb");
228 fprintf(stderr
, "Could not open file '%s': ", name
);
233 fprintf(stderr
, "Loading ELF file '%s'...\n", name
);
235 if (fread(&ehdr
, sizeof(ehdr
), 1, fd
) != 1) {
236 fprintf(stderr
, "Error while reading ELF file header\n");
239 if (memcmp(ehdr
.e_ident
, elf_ident
, sizeof(elf_ident
))
240 || le32(ehdr
.e_version
) != 1)
242 fprintf(stderr
, "Not an ELF file or wrong ELF type\n");
245 if (le16(ehdr
.e_type
) != ET_EXEC
) {
246 fprintf(stderr
, "ELF is not executable\n");
249 if (le16(ehdr
.e_machine
) != EM_ARM
) {
250 fprintf(stderr
, "Wrong ELF architecture\n");
253 if (!ehdr
.e_phnum
|| !ehdr
.e_phoff
) {
254 fprintf(stderr
, "ELF has no PHDRs\n");
257 if (le16(ehdr
.e_phentsize
) != sizeof(Elf32_Phdr
)) {
258 // could be a structure padding issue...
259 fprintf(stderr
, "Either the ELF file or this code is made of fail\n");
262 num_phdrs
= le16(ehdr
.e_phnum
);
264 phdrs
= malloc(le16(ehdr
.e_phnum
) * sizeof(Elf32_Phdr
));
266 fprintf(stderr
, "Out of memory\n");
269 if (fseek(fd
, le32(ehdr
.e_phoff
), SEEK_SET
) < 0) {
270 fprintf(stderr
, "Error while reading ELF PHDRs\n");
273 if (fread(phdrs
, sizeof(Elf32_Phdr
), num_phdrs
, fd
) != num_phdrs
) {
274 fprintf(stderr
, "Error while reading ELF PHDRs\n");
278 res
= build_segs_from_phdrs(ctx
, fd
, phdrs
, num_phdrs
);
281 res
= check_segs(ctx
, can_write_bl
);
287 ctx
->filename
= name
;
299 // Get the state of the proxmark, backwards compatible
300 static int get_proxmark_state(uint32_t *state
)
303 c
.cmd
= CMD_DEVICE_INFO
;
306 while (!WaitForResponse(CMD_ANY
, &resp
)) {
307 // Keep waiting for a response
312 // 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
313 // 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
314 // 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
318 *state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
320 case CMD_DEBUG_PRINT_STRING
:
321 *state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
323 case CMD_DEVICE_INFO
:
324 *state
= resp
.arg
[0];
327 fprintf(stderr
, "Error: Couldn't get proxmark state, bad response type: 0x%04" PRIx64
"\n", resp
.cmd
);
335 // Enter the bootloader to be able to start flashing
336 static int enter_bootloader(receiver_arg
* conn
, char *serial_port_name
)
340 if (get_proxmark_state(&state
) < 0)
343 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
) {
344 /* Already in flash state, we're done. */
348 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
349 fprintf(stderr
,"Entering bootloader...\n");
351 memset(&c
, 0, sizeof (c
));
353 if ((state
& DEVICE_INFO_FLAG_BOOTROM_PRESENT
)
354 && (state
& DEVICE_INFO_FLAG_OSIMAGE_PRESENT
))
356 // New style handover: Send CMD_START_FLASH, which will reset the board
357 // and enter the bootrom on the next boot.
358 c
.cmd
= CMD_START_FLASH
;
360 fprintf(stderr
,"(Press and release the button only to abort)\n");
362 // Old style handover: Ask the user to press the button, then reset the board
363 c
.cmd
= CMD_HARDWARE_RESET
;
365 fprintf(stderr
,"Press and hold down button NOW if your bootloader requires it.\n");
369 CloseProxmark(conn
, serial_port_name
);
371 fprintf(stderr
,"Waiting for Proxmark to reappear on %s",serial_port_name
);
374 fprintf(stderr
, ".");
375 } while (!OpenProxmark(serial_port_name
));
377 fprintf(stderr
," Found.\n");
381 fprintf(stderr
, "Error: Unknown Proxmark mode\n");
385 static int wait_for_ack()
388 while (!WaitForResponse(CMD_ANY
, &resp
)) {
391 if (resp
.cmd
!= CMD_ACK
) {
392 printf("Error: Unexpected reply 0x%04" PRIx64
" (expected ACK)\n", resp
.cmd
);
398 // Go into flashing mode
399 int flash_start_flashing(receiver_arg
* conn
, int enable_bl_writes
,char *serial_port_name
)
403 if (enter_bootloader(conn
, serial_port_name
) < 0)
406 if (get_proxmark_state(&state
) < 0)
409 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
410 // This command is stupid. Why the heck does it care which area we're
411 // flashing, as long as it's not the bootloader area? The mind boggles.
412 UsbCommand c
= {CMD_START_FLASH
};
414 if (enable_bl_writes
) {
415 c
.arg
[0] = FLASH_START
;
416 c
.arg
[1] = FLASH_END
;
417 c
.arg
[2] = START_FLASH_MAGIC
;
419 c
.arg
[0] = BOOTLOADER_END
;
420 c
.arg
[1] = FLASH_END
;
424 return wait_for_ack();
426 fprintf(stderr
, "Note: Your bootloader does not understand the new START_FLASH command\n");
427 fprintf(stderr
, " It is recommended that you update your bootloader\n\n");
433 static int write_block(uint32_t address
, uint8_t *data
, uint32_t length
)
435 uint8_t block_buf
[BLOCK_SIZE
];
437 memset(block_buf
, 0xFF, BLOCK_SIZE
);
438 memcpy(block_buf
, data
, length
);
440 c
.cmd
= CMD_FINISH_WRITE
;
442 memcpy(c
.d
.asBytes
, block_buf
, length
);
444 return wait_for_ack();
447 // Write a file's segments to Flash
448 int flash_write(flash_file_t
*ctx
)
450 fprintf(stderr
, "Writing segments for file: %s\n", ctx
->filename
);
451 for (int i
= 0; i
< ctx
->num_segs
; i
++) {
452 flash_seg_t
*seg
= &ctx
->segments
[i
];
454 uint32_t length
= seg
->length
;
455 uint32_t blocks
= (length
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
456 uint32_t end
= seg
->start
+ length
;
458 fprintf(stderr
, " 0x%08x..0x%08x [0x%x / %d blocks]",
459 seg
->start
, end
- 1, length
, blocks
);
462 uint8_t *data
= seg
->data
;
463 uint32_t baddr
= seg
->start
;
466 uint32_t block_size
= length
;
467 if (block_size
> BLOCK_SIZE
)
468 block_size
= BLOCK_SIZE
;
470 if (write_block(baddr
, data
, block_size
) < 0) {
471 fprintf(stderr
, " ERROR\n");
472 fprintf(stderr
, "Error writing block %d of %d\n", block
, blocks
);
478 length
-= block_size
;
480 fprintf(stderr
, ".");
482 fprintf(stderr
, " OK\n");
487 // free a file context
488 void flash_free(flash_file_t
*ctx
)
493 for (int i
= 0; i
< ctx
->num_segs
; i
++)
494 free(ctx
->segments
[i
].data
);
496 ctx
->segments
= NULL
;
501 // just reset the unit
502 int flash_stop_flashing() {
503 UsbCommand c
= {CMD_HARDWARE_RESET
};