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 //-----------------------------------------------------------------------------
20 #include "proxendian.h"
21 #include "at91sam7s512.h"
22 #include "util_posix.h"
25 #define FLASH_START 0x100000
27 #define BOOTLOADER_SIZE 0x2000
28 #define BOOTLOADER_END (FLASH_START + BOOTLOADER_SIZE)
30 #define BLOCK_SIZE 0x200
32 #define FLASHER_VERSION BL_VERSION_1_0_0
34 static const uint8_t elf_ident
[] = {
41 static int chipid_to_mem_avail(uint32_t iChipID
) {
43 switch ((iChipID
& 0xF00) >> 8) {
77 // Turn PHDRs into flasher segments, checking for PHDR sanity and merging adjacent
78 // unaligned segments if needed
79 static int build_segs_from_phdrs(flash_file_t
*ctx
, FILE *fd
, Elf32_Phdr
*phdrs
, uint16_t num_phdrs
, uint32_t flash_end
) {
80 Elf32_Phdr
*phdr
= phdrs
;
82 uint32_t last_end
= 0;
84 ctx
->segments
= calloc(sizeof(flash_seg_t
) * num_phdrs
, sizeof(uint8_t));
86 PrintAndLogEx(ERR
, "Out of memory");
92 PrintAndLogEx(SUCCESS
, "Loading usable ELF segments:");
93 for (int i
= 0; i
< num_phdrs
; i
++) {
94 if (le32(phdr
->p_type
) != PT_LOAD
) {
98 uint32_t vaddr
= le32(phdr
->p_vaddr
);
99 uint32_t paddr
= le32(phdr
->p_paddr
);
100 uint32_t filesz
= le32(phdr
->p_filesz
);
101 uint32_t memsz
= le32(phdr
->p_memsz
);
102 uint32_t offset
= le32(phdr
->p_offset
);
103 uint32_t flags
= le32(phdr
->p_flags
);
108 PrintAndLogEx(SUCCESS
, " "_YELLOW_("%d")": V 0x%08x P 0x%08x (0x%08x->0x%08x) [%c%c%c] @0x%x",
109 i
, vaddr
, paddr
, filesz
, memsz
,
110 (flags
& PF_R
) ? 'R' : ' ',
111 (flags
& PF_W
) ? 'W' : ' ',
112 (flags
& PF_X
) ? 'X' : ' ',
114 if (filesz
!= memsz
) {
115 PrintAndLogEx(ERR
, "Error: PHDR file size does not equal memory size\n"
116 "(DATA+BSS PHDRs do not make sense on ROM platforms!)");
119 if (paddr
< last_end
) {
120 PrintAndLogEx(ERR
, "Error: PHDRs not sorted or overlap");
123 if (paddr
< FLASH_START
|| (paddr
+ filesz
) > flash_end
) {
124 PrintAndLogEx(ERR
, "Error: PHDR is not contained in Flash");
125 if ((paddr
+ filesz
) > flash_end
) {
126 PrintAndLogEx(ERR
, "Firmware probably too big for your device");
130 if (vaddr
>= FLASH_START
&& vaddr
< flash_end
&& (flags
& PF_W
)) {
131 PrintAndLogEx(ERR
, "Error: Flash VMA segment is writable");
136 // make extra space if we need to move the data forward
137 data
= calloc(filesz
+ BLOCK_SIZE
, sizeof(uint8_t));
139 PrintAndLogEx(ERR
, "Error: Out of memory");
142 if (fseek(fd
, offset
, SEEK_SET
) < 0 || fread(data
, 1, filesz
, fd
) != filesz
) {
143 PrintAndLogEx(ERR
, "Error while reading PHDR payload");
148 uint32_t block_offset
= paddr
& (BLOCK_SIZE
- 1);
151 flash_seg_t
*prev_seg
= seg
- 1;
152 uint32_t this_end
= paddr
+ filesz
;
153 uint32_t this_firstblock
= paddr
& ~(BLOCK_SIZE
- 1);
154 uint32_t prev_lastblock
= (last_end
- 1) & ~(BLOCK_SIZE
- 1);
156 if (this_firstblock
== prev_lastblock
) {
157 uint32_t new_length
= this_end
- prev_seg
->start
;
158 uint32_t this_offset
= paddr
- prev_seg
->start
;
159 uint32_t hole
= this_offset
- prev_seg
->length
;
160 uint8_t *new_data
= calloc(new_length
, sizeof(uint8_t));
162 PrintAndLogEx(ERR
, "Error: Out of memory");
166 memset(new_data
, 0xff, new_length
);
167 memcpy(new_data
, prev_seg
->data
, prev_seg
->length
);
168 memcpy(new_data
+ this_offset
, data
, filesz
);
169 PrintAndLogEx(INFO
, "Note: Extending previous segment from 0x%x to 0x%x bytes",
170 prev_seg
->length
, new_length
);
172 PrintAndLogEx(INFO
, "Note: 0x%x-byte hole created", hole
);
174 free(prev_seg
->data
);
175 prev_seg
->data
= new_data
;
176 prev_seg
->length
= new_length
;
182 PrintAndLogEx(WARNING
, "Warning: segment does not begin on a block boundary, will pad");
183 memmove(data
+ block_offset
, data
, filesz
);
184 memset(data
, 0xFF, block_offset
);
185 filesz
+= block_offset
;
186 paddr
-= block_offset
;
191 seg
->length
= filesz
;
195 last_end
= paddr
+ filesz
;
201 // Sanity check segments and check for bootloader writes
202 static int check_segs(flash_file_t
*ctx
, int can_write_bl
, uint32_t flash_end
) {
203 for (int i
= 0; i
< ctx
->num_segs
; i
++) {
204 flash_seg_t
*seg
= &ctx
->segments
[i
];
206 if (seg
->start
& (BLOCK_SIZE
- 1)) {
207 PrintAndLogEx(ERR
, "Error: Segment is not aligned");
210 if (seg
->start
< FLASH_START
) {
211 PrintAndLogEx(ERR
, "Error: Segment is outside of flash bounds");
214 if (seg
->start
+ seg
->length
> flash_end
) {
215 PrintAndLogEx(ERR
, "Error: Segment is outside of flash bounds");
218 if (!can_write_bl
&& seg
->start
< BOOTLOADER_END
) {
219 PrintAndLogEx(ERR
, "Attempted to write bootloader but bootloader writes are not enabled");
222 if (can_write_bl
&& seg
->start
< BOOTLOADER_END
&& (seg
->start
+ seg
->length
> BOOTLOADER_END
)) {
223 PrintAndLogEx(ERR
, "Error: Segment is outside of bootloader bounds");
230 // Load an ELF file and prepare it for flashing
231 int flash_load(flash_file_t
*ctx
, const char *name
, int can_write_bl
, int flash_size
) {
234 Elf32_Phdr
*phdrs
= NULL
;
236 uint32_t flash_end
= FLASH_START
+ flash_size
;
237 int res
= PM3_EUNDEF
;
239 fd
= fopen(name
, "rb");
241 PrintAndLogEx(ERR
, _RED_("Could not open file") " %s >>> ", name
);
246 PrintAndLogEx(SUCCESS
, _CYAN_("Loading ELF file") _YELLOW_(" %s"), name
);
248 if (fread(&ehdr
, sizeof(ehdr
), 1, fd
) != 1) {
249 PrintAndLogEx(ERR
, "Error while reading ELF file header");
253 if (memcmp(ehdr
.e_ident
, elf_ident
, sizeof(elf_ident
))
254 || le32(ehdr
.e_version
) != 1) {
255 PrintAndLogEx(ERR
, "Not an ELF file or wrong ELF type");
259 if (le16(ehdr
.e_type
) != ET_EXEC
) {
260 PrintAndLogEx(ERR
, "ELF is not executable");
264 if (le16(ehdr
.e_machine
) != EM_ARM
) {
265 PrintAndLogEx(ERR
, "Wrong ELF architecture");
269 if (!ehdr
.e_phnum
|| !ehdr
.e_phoff
) {
270 PrintAndLogEx(ERR
, "ELF has no PHDRs");
274 if (le16(ehdr
.e_phentsize
) != sizeof(Elf32_Phdr
)) {
275 // could be a structure padding issue...
276 PrintAndLogEx(ERR
, "Either the ELF file or this code is made of fail");
280 num_phdrs
= le16(ehdr
.e_phnum
);
282 phdrs
= calloc(le16(ehdr
.e_phnum
) * sizeof(Elf32_Phdr
), sizeof(uint8_t));
284 PrintAndLogEx(ERR
, "Out of memory");
288 if (fseek(fd
, le32(ehdr
.e_phoff
), SEEK_SET
) < 0) {
289 PrintAndLogEx(ERR
, "Error while reading ELF PHDRs");
293 if (fread(phdrs
, sizeof(Elf32_Phdr
), num_phdrs
, fd
) != num_phdrs
) {
295 PrintAndLogEx(ERR
, "Error while reading ELF PHDRs");
299 res
= build_segs_from_phdrs(ctx
, fd
, phdrs
, num_phdrs
, flash_end
);
300 if (res
!= PM3_SUCCESS
)
302 res
= check_segs(ctx
, can_write_bl
, flash_end
);
303 if (res
!= PM3_SUCCESS
)
308 ctx
->filename
= name
;
320 // Get the state of the proxmark, backwards compatible
321 static int get_proxmark_state(uint32_t *state
) {
322 SendCommandBL(CMD_DEVICE_INFO
, 0, 0, 0, NULL
, 0);
323 PacketResponseNG resp
;
324 WaitForResponse(CMD_UNKNOWN
, &resp
); // wait for any response. No timeout.
327 // 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
328 // 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
329 // 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
333 *state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
335 case CMD_DEBUG_PRINT_STRING
:
336 *state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
338 case CMD_DEVICE_INFO
:
339 *state
= resp
.oldarg
[0];
342 PrintAndLogEx(ERR
, _RED_("Error:") " Couldn't get Proxmark3 state, bad response type: 0x%04x", resp
.cmd
);
349 // Enter the bootloader to be able to start flashing
350 static int enter_bootloader(char *serial_port_name
) {
354 if ((ret
= get_proxmark_state(&state
)) != PM3_SUCCESS
)
357 /* Already in flash state, we're done. */
358 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
)
361 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
362 PrintAndLogEx(SUCCESS
, _CYAN_("Entering bootloader..."));
364 if ((state
& DEVICE_INFO_FLAG_BOOTROM_PRESENT
)
365 && (state
& DEVICE_INFO_FLAG_OSIMAGE_PRESENT
)) {
366 // New style handover: Send CMD_START_FLASH, which will reset the board
367 // and enter the bootrom on the next boot.
368 SendCommandBL(CMD_START_FLASH
, 0, 0, 0, NULL
, 0);
369 PrintAndLogEx(SUCCESS
, "(Press and release the button only to " _YELLOW_("abort") ")");
371 // Old style handover: Ask the user to press the button, then reset the board
372 SendCommandBL(CMD_HARDWARE_RESET
, 0, 0, 0, NULL
, 0);
373 PrintAndLogEx(SUCCESS
, "Press and hold down button NOW if your bootloader requires it.");
376 CloseProxmark(session
.current_device
);
377 // Let time to OS to make the port disappear
380 if (OpenProxmark(&session
.current_device
, serial_port_name
, true, 60, true, FLASHMODE_SPEED
)) {
381 PrintAndLogEx(NORMAL
, _GREEN_(" found"));
384 PrintAndLogEx(ERR
, _RED_("Error:") " Proxmark3 not found.");
389 PrintAndLogEx(ERR
, _RED_("Error:") " Unknown Proxmark3 mode");
393 static int wait_for_ack(PacketResponseNG
*ack
) {
394 WaitForResponse(CMD_UNKNOWN
, ack
);
396 if (ack
->cmd
!= CMD_ACK
) {
397 PrintAndLogEx(ERR
, "Error: Unexpected reply 0x%04x %s (expected ACK)",
399 (ack
->cmd
== CMD_NACK
) ? "NACK" : ""
406 static bool g_printed_msg
= false;
407 static void flash_suggest_update_bootloader(void) {
411 PrintAndLogEx(ERR
, _RED_("It is recommended that you first" _YELLOW_(" update your bootloader") _RED_(" alone,")));
412 PrintAndLogEx(ERR
, _RED_("reboot the Proxmark3 then only update the main firmware") "\n");
413 PrintAndLogEx(ERR
, "Follow these steps :");
414 PrintAndLogEx(ERR
, " 1) ./pm3-flash-bootrom");
415 PrintAndLogEx(ERR
, " 2) ./pm3-flash-all");
416 PrintAndLogEx(ERR
, " 3) ./pm3");
417 PrintAndLogEx(INFO
, "--------------------------------------------------------");
418 g_printed_msg
= true;
421 static void flash_suggest_update_flasher(void) {
422 PrintAndLogEx(ERR
, _RED_("It is recommended that you first " _YELLOW_("update your flasher")));
425 // Go into flashing mode
426 int flash_start_flashing(int enable_bl_writes
, char *serial_port_name
, uint32_t *max_allowed
) {
428 uint32_t chipinfo
= 0;
431 ret
= enter_bootloader(serial_port_name
);
432 if (ret
!= PM3_SUCCESS
)
435 ret
= get_proxmark_state(&state
);
436 if (ret
!= PM3_SUCCESS
)
439 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_CHIP_INFO
) {
440 SendCommandBL(CMD_CHIP_INFO
, 0, 0, 0, NULL
, 0);
441 PacketResponseNG resp
;
442 WaitForResponse(CMD_CHIP_INFO
, &resp
);
443 chipinfo
= resp
.oldarg
[0];
446 int version
= BL_VERSION_INVALID
;
447 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_VERSION
) {
448 SendCommandBL(CMD_BL_VERSION
, 0, 0, 0, NULL
, 0);
449 PacketResponseNG resp
;
450 WaitForResponse(CMD_BL_VERSION
, &resp
);
451 version
= resp
.oldarg
[0];
452 if ((BL_VERSION_MAJOR(version
) < BL_VERSION_FIRST_MAJOR
) || (BL_VERSION_MAJOR(version
) > BL_VERSION_LAST_MAJOR
)) {
453 // version info seems fishy
454 version
= BL_VERSION_INVALID
;
455 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ==========================="));
456 PrintAndLogEx(ERR
, _RED_("Note: Your bootloader reported an invalid version number"));
457 flash_suggest_update_bootloader();
459 } else if (BL_VERSION_MAJOR(version
) < BL_VERSION_MAJOR(FLASHER_VERSION
)) {
460 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ==================================="));
461 PrintAndLogEx(ERR
, _RED_("Note: Your bootloader reported a version older than this flasher"));
462 flash_suggest_update_bootloader();
463 } else if (BL_VERSION_MAJOR(version
) > BL_VERSION_MAJOR(FLASHER_VERSION
)) {
464 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ========================="));
465 PrintAndLogEx(ERR
, _RED_("Note: Your bootloader is more recent than this flasher"));
466 flash_suggest_update_flasher();
469 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ==========================================="));
470 PrintAndLogEx(ERR
, _RED_("Note: Your bootloader does not understand the new" _YELLOW_(" CMD_BL_VERSION") _RED_(" command")));
471 flash_suggest_update_bootloader();
474 uint32_t flash_end
= FLASH_START
+ AT91C_IFLASH_PAGE_SIZE
* AT91C_IFLASH_NB_OF_PAGES
/ 2;
477 int mem_avail
= chipid_to_mem_avail(chipinfo
);
478 if (mem_avail
!= 0) {
479 PrintAndLogEx(INFO
, "Available memory on this board: "_YELLOW_("%uK") " bytes\n", mem_avail
);
480 if (mem_avail
> 256) {
481 if (BL_VERSION_MAJOR(version
) < BL_VERSION_MAJOR(BL_VERSION_1_0_0
)) {
482 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ======================"));
483 PrintAndLogEx(ERR
, _RED_("Your bootloader does not support writing above 256k"));
484 flash_suggest_update_bootloader();
486 flash_end
= FLASH_START
+ AT91C_IFLASH_PAGE_SIZE
* AT91C_IFLASH_NB_OF_PAGES
;
487 *max_allowed
= mem_avail
;
491 PrintAndLogEx(INFO
, "Available memory on this board: "_RED_("UNKNOWN")"\n");
492 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ======================================"));
493 PrintAndLogEx(ERR
, _RED_("Note: Your bootloader does not understand the new" _YELLOW_(" CHIP_INFO") _RED_(" command")));
494 flash_suggest_update_bootloader();
497 if (enable_bl_writes
) {
498 PrintAndLogEx(INFO
, "Permitted flash range: 0x%08x-0x%08x", FLASH_START
, flash_end
);
500 PrintAndLogEx(INFO
, "Permitted flash range: 0x%08x-0x%08x", BOOTLOADER_END
, flash_end
);
502 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
503 PacketResponseNG resp
;
505 if (enable_bl_writes
) {
506 SendCommandBL(CMD_START_FLASH
, FLASH_START
, flash_end
, START_FLASH_MAGIC
, NULL
, 0);
508 SendCommandBL(CMD_START_FLASH
, BOOTLOADER_END
, flash_end
, 0, NULL
, 0);
510 return wait_for_ack(&resp
);
512 PrintAndLogEx(ERR
, _RED_("====================== OBS ! ========================================"));
513 PrintAndLogEx(ERR
, _RED_("Note: Your bootloader does not understand the new" _YELLOW_(" START_FLASH") _RED_(" command")));
514 flash_suggest_update_bootloader();
519 static int write_block(uint32_t address
, uint8_t *data
, uint32_t length
) {
520 uint8_t block_buf
[BLOCK_SIZE
];
521 memset(block_buf
, 0xFF, BLOCK_SIZE
);
522 memcpy(block_buf
, data
, length
);
523 PacketResponseNG resp
;
524 SendCommandBL(CMD_FINISH_WRITE
, address
, 0, 0, block_buf
, length
);
525 int ret
= wait_for_ack(&resp
);
526 if (ret
&& resp
.oldarg
[0]) {
527 uint32_t lock_bits
= resp
.oldarg
[0] >> 16;
528 bool lock_error
= resp
.oldarg
[0] & AT91C_MC_LOCKE
;
529 bool prog_error
= resp
.oldarg
[0] & AT91C_MC_PROGE
;
530 bool security_bit
= resp
.oldarg
[0] & AT91C_MC_SECURITY
;
531 PrintAndLogEx(NORMAL
, "%s", lock_error
? " Lock Error" : "");
532 PrintAndLogEx(NORMAL
, "%s", prog_error
? " Invalid Command or bad Keyword" : "");
533 PrintAndLogEx(NORMAL
, "%s", security_bit
? " Security Bit is set!" : "");
534 PrintAndLogEx(NORMAL
, " Lock Bits: 0x%04x", lock_bits
);
540 "...................................................................\n @@@ @@@@@@@ @@@@@@@@ @@@@@@@@@@ @@@@@@ @@@ @@@\n"
541 " @@! !@@ @@! @@! @@! @@! @@! @@@ @@!@!@@@\n !!@ !@! @!!!:! @!! !!@ @!@ @!@!@!@! @!@@!!@!\n"
542 " !!: :!! !!: !!: !!: !!: !!! !!: !!!\n : :: :: : : :: ::: : : : : : :: : \n"
543 _RED_(" . .. .. . . .. ... . . . . . .. . ")
544 "\n...................................................................\n"
545 "...................................................................\n"
548 // Write a file's segments to Flash
549 int flash_write(flash_file_t
*ctx
) {
552 PrintAndLogEx(SUCCESS
, "Writing segments for file: %s", ctx
->filename
);
554 bool filter_ansi
= !session
.supports_colors
;
556 for (int i
= 0; i
< ctx
->num_segs
; i
++) {
557 flash_seg_t
*seg
= &ctx
->segments
[i
];
559 uint32_t length
= seg
->length
;
560 uint32_t blocks
= (length
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
561 uint32_t end
= seg
->start
+ length
;
563 PrintAndLogEx(SUCCESS
, " 0x%08x..0x%08x [0x%x / %u blocks]", seg
->start
, end
- 1, length
, blocks
);
566 uint8_t *data
= seg
->data
;
567 uint32_t baddr
= seg
->start
;
570 uint32_t block_size
= length
;
571 if (block_size
> BLOCK_SIZE
)
572 block_size
= BLOCK_SIZE
;
574 if (write_block(baddr
, data
, block_size
) < 0) {
575 PrintAndLogEx(ERR
, "Error writing block %d of %u", block
, blocks
);
581 length
-= block_size
;
583 if (len
< strlen(ice
)) {
584 if (filter_ansi
&& !isalpha(ice
[len
])) {
587 fprintf(stdout
, "%c", ice
[len
++]);
590 fprintf(stdout
, ".");
594 PrintAndLogEx(NORMAL
, " " _GREEN_("OK"));
600 // free a file context
601 void flash_free(flash_file_t
*ctx
) {
605 for (int i
= 0; i
< ctx
->num_segs
; i
++)
606 free(ctx
->segments
[i
].data
);
608 ctx
->segments
= NULL
;
613 // just reset the unit
614 int flash_stop_flashing(void) {
615 SendCommandBL(CMD_HARDWARE_RESET
, 0, 0, 0, NULL
, 0);