fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / flash.c
blob2f5f037c3b56adcbf0b20f65af9b9dfc5ef48743
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 //-----------------------------------------------------------------------------
11 #include "flash.h"
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <ctype.h>
18 #include "ui.h"
19 #include "elf.h"
20 #include "proxendian.h"
21 #include "at91sam7s512.h"
22 #include "util_posix.h"
23 #include "comms.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[] = {
35 0x7f, 'E', 'L', 'F',
36 ELFCLASS32,
37 ELFDATA2LSB,
38 EV_CURRENT
41 static int chipid_to_mem_avail(uint32_t iChipID) {
42 int mem_avail = 0;
43 switch ((iChipID & 0xF00) >> 8) {
44 case 0:
45 mem_avail = 0;
46 break;
47 case 1:
48 mem_avail = 8;
49 break;
50 case 2:
51 mem_avail = 16;
52 break;
53 case 3:
54 mem_avail = 32;
55 break;
56 case 5:
57 mem_avail = 64;
58 break;
59 case 7:
60 mem_avail = 128;
61 break;
62 case 9:
63 mem_avail = 256;
64 break;
65 case 10:
66 mem_avail = 512;
67 break;
68 case 12:
69 mem_avail = 1024;
70 break;
71 case 14:
72 mem_avail = 2048;
74 return mem_avail;
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;
81 flash_seg_t *seg;
82 uint32_t last_end = 0;
84 ctx->segments = calloc(sizeof(flash_seg_t) * num_phdrs, sizeof(uint8_t));
85 if (!ctx->segments) {
86 PrintAndLogEx(ERR, "Out of memory");
87 return PM3_EMALLOC;
89 ctx->num_segs = 0;
90 seg = ctx->segments;
92 PrintAndLogEx(SUCCESS, "Loading usable ELF segments:");
93 for (int i = 0; i < num_phdrs; i++) {
94 if (le32(phdr->p_type) != PT_LOAD) {
95 phdr++;
96 continue;
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);
104 if (!filesz) {
105 phdr++;
106 continue;
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' : ' ',
113 offset);
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!)");
117 return PM3_EFILE;
119 if (paddr < last_end) {
120 PrintAndLogEx(ERR, "Error: PHDRs not sorted or overlap");
121 return PM3_EFILE;
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");
128 return PM3_EFILE;
130 if (vaddr >= FLASH_START && vaddr < flash_end && (flags & PF_W)) {
131 PrintAndLogEx(ERR, "Error: Flash VMA segment is writable");
132 return PM3_EFILE;
135 uint8_t *data;
136 // make extra space if we need to move the data forward
137 data = calloc(filesz + BLOCK_SIZE, sizeof(uint8_t));
138 if (!data) {
139 PrintAndLogEx(ERR, "Error: Out of memory");
140 return PM3_EMALLOC;
142 if (fseek(fd, offset, SEEK_SET) < 0 || fread(data, 1, filesz, fd) != filesz) {
143 PrintAndLogEx(ERR, "Error while reading PHDR payload");
144 free(data);
145 return PM3_EFILE;
148 uint32_t block_offset = paddr & (BLOCK_SIZE - 1);
149 if (block_offset) {
150 if (ctx->num_segs) {
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));
161 if (!new_data) {
162 PrintAndLogEx(ERR, "Error: Out of memory");
163 free(data);
164 return PM3_EMALLOC;
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);
171 if (hole)
172 PrintAndLogEx(INFO, "Note: 0x%x-byte hole created", hole);
173 free(data);
174 free(prev_seg->data);
175 prev_seg->data = new_data;
176 prev_seg->length = new_length;
177 last_end = this_end;
178 phdr++;
179 continue;
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;
189 seg->data = data;
190 seg->start = paddr;
191 seg->length = filesz;
192 seg++;
193 ctx->num_segs++;
195 last_end = paddr + filesz;
196 phdr++;
198 return PM3_SUCCESS;
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");
208 return PM3_EFILE;
210 if (seg->start < FLASH_START) {
211 PrintAndLogEx(ERR, "Error: Segment is outside of flash bounds");
212 return PM3_EFILE;
214 if (seg->start + seg->length > flash_end) {
215 PrintAndLogEx(ERR, "Error: Segment is outside of flash bounds");
216 return PM3_EFILE;
218 if (!can_write_bl && seg->start < BOOTLOADER_END) {
219 PrintAndLogEx(ERR, "Attempted to write bootloader but bootloader writes are not enabled");
220 return PM3_EINVARG;
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");
224 return PM3_EFILE;
227 return PM3_SUCCESS;
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) {
232 FILE *fd;
233 Elf32_Ehdr ehdr;
234 Elf32_Phdr *phdrs = NULL;
235 uint16_t num_phdrs;
236 uint32_t flash_end = FLASH_START + flash_size;
237 int res = PM3_EUNDEF;
239 fd = fopen(name, "rb");
240 if (!fd) {
241 PrintAndLogEx(ERR, _RED_("Could not open file") " %s >>> ", name);
242 res = PM3_EFILE;
243 goto fail;
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");
250 res = PM3_EFILE;
251 goto fail;
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");
256 res = PM3_EFILE;
257 goto fail;
259 if (le16(ehdr.e_type) != ET_EXEC) {
260 PrintAndLogEx(ERR, "ELF is not executable");
261 res = PM3_EFILE;
262 goto fail;
264 if (le16(ehdr.e_machine) != EM_ARM) {
265 PrintAndLogEx(ERR, "Wrong ELF architecture");
266 res = PM3_EFILE;
267 goto fail;
269 if (!ehdr.e_phnum || !ehdr.e_phoff) {
270 PrintAndLogEx(ERR, "ELF has no PHDRs");
271 res = PM3_EFILE;
272 goto fail;
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");
277 res = PM3_EFILE;
278 goto fail;
280 num_phdrs = le16(ehdr.e_phnum);
282 phdrs = calloc(le16(ehdr.e_phnum) * sizeof(Elf32_Phdr), sizeof(uint8_t));
283 if (!phdrs) {
284 PrintAndLogEx(ERR, "Out of memory");
285 res = PM3_EMALLOC;
286 goto fail;
288 if (fseek(fd, le32(ehdr.e_phoff), SEEK_SET) < 0) {
289 PrintAndLogEx(ERR, "Error while reading ELF PHDRs");
290 res = PM3_EFILE;
291 goto fail;
293 if (fread(phdrs, sizeof(Elf32_Phdr), num_phdrs, fd) != num_phdrs) {
294 res = PM3_EFILE;
295 PrintAndLogEx(ERR, "Error while reading ELF PHDRs");
296 goto fail;
299 res = build_segs_from_phdrs(ctx, fd, phdrs, num_phdrs, flash_end);
300 if (res != PM3_SUCCESS)
301 goto fail;
302 res = check_segs(ctx, can_write_bl, flash_end);
303 if (res != PM3_SUCCESS)
304 goto fail;
306 free(phdrs);
307 fclose(fd);
308 ctx->filename = name;
309 return PM3_SUCCESS;
311 fail:
312 if (phdrs)
313 free(phdrs);
314 if (fd)
315 fclose(fd);
316 flash_free(ctx);
317 return res;
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.
326 // Three outcomes:
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
331 switch (resp.cmd) {
332 case CMD_ACK:
333 *state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM;
334 break;
335 case CMD_DEBUG_PRINT_STRING:
336 *state = DEVICE_INFO_FLAG_CURRENT_MODE_OS;
337 break;
338 case CMD_DEVICE_INFO:
339 *state = resp.oldarg[0];
340 break;
341 default:
342 PrintAndLogEx(ERR, _RED_("Error:") " Couldn't get Proxmark3 state, bad response type: 0x%04x", resp.cmd);
343 return PM3_EFATAL;
344 break;
346 return PM3_SUCCESS;
349 // Enter the bootloader to be able to start flashing
350 static int enter_bootloader(char *serial_port_name) {
351 uint32_t state;
352 int ret;
354 if ((ret = get_proxmark_state(&state)) != PM3_SUCCESS)
355 return ret;
357 /* Already in flash state, we're done. */
358 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM)
359 return PM3_SUCCESS;
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") ")");
370 } else {
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.");
375 msleep(100);
376 CloseProxmark(session.current_device);
377 // Let time to OS to make the port disappear
378 msleep(1000);
380 if (OpenProxmark(&session.current_device, serial_port_name, true, 60, true, FLASHMODE_SPEED)) {
381 PrintAndLogEx(NORMAL, _GREEN_(" found"));
382 return PM3_SUCCESS;
383 } else {
384 PrintAndLogEx(ERR, _RED_("Error:") " Proxmark3 not found.");
385 return PM3_ETIMEOUT;
389 PrintAndLogEx(ERR, _RED_("Error:") " Unknown Proxmark3 mode");
390 return PM3_EFATAL;
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)",
398 ack->cmd,
399 (ack->cmd == CMD_NACK) ? "NACK" : ""
401 return PM3_ESOFT;
403 return PM3_SUCCESS;
406 static bool g_printed_msg = false;
407 static void flash_suggest_update_bootloader(void) {
408 if (g_printed_msg)
409 return;
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) {
427 uint32_t state;
428 uint32_t chipinfo = 0;
429 int ret;
431 ret = enter_bootloader(serial_port_name);
432 if (ret != PM3_SUCCESS)
433 return ret;
435 ret = get_proxmark_state(&state);
436 if (ret != PM3_SUCCESS)
437 return ret;
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();
468 } else {
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;
475 *max_allowed = 256;
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();
485 } else {
486 flash_end = FLASH_START + AT91C_IFLASH_PAGE_SIZE * AT91C_IFLASH_NB_OF_PAGES;
487 *max_allowed = mem_avail;
490 } else {
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);
499 } else {
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);
507 } else {
508 SendCommandBL(CMD_START_FLASH, BOOTLOADER_END, flash_end, 0, NULL, 0);
510 return wait_for_ack(&resp);
511 } else {
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();
516 return PM3_SUCCESS;
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);
536 return ret;
539 const char ice[] =
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) {
550 int len = 0;
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);
564 fflush(stdout);
565 int block = 0;
566 uint8_t *data = seg->data;
567 uint32_t baddr = seg->start;
569 while (length) {
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);
576 return PM3_EFATAL;
579 data += block_size;
580 baddr += block_size;
581 length -= block_size;
582 block++;
583 if (len < strlen(ice)) {
584 if (filter_ansi && !isalpha(ice[len])) {
585 len++;
586 } else {
587 fprintf(stdout, "%c", ice[len++]);
589 } else {
590 fprintf(stdout, ".");
592 fflush(stdout);
594 PrintAndLogEx(NORMAL, " " _GREEN_("OK"));
595 fflush(stdout);
597 return PM3_SUCCESS;
600 // free a file context
601 void flash_free(flash_file_t *ctx) {
602 if (!ctx)
603 return;
604 if (ctx->segments) {
605 for (int i = 0; i < ctx->num_segs; i++)
606 free(ctx->segments[i].data);
607 free(ctx->segments);
608 ctx->segments = NULL;
609 ctx->num_segs = 0;
613 // just reset the unit
614 int flash_stop_flashing(void) {
615 SendCommandBL(CMD_HARDWARE_RESET, 0, 0, 0, NULL, 0);
616 msleep(100);
617 return PM3_SUCCESS;