1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2007,2008 Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
5 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
6 * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
25 #include <helper/time_support.h>
26 #include <target/image.h>
30 * Implements Tcl commands used to access NOR flash facilities.
33 COMMAND_HELPER(flash_command_get_bank_maybe_probe, unsigned name_index,
34 struct flash_bank **bank, bool do_probe)
36 const char *name = CMD_ARGV[name_index];
39 retval = get_flash_bank_by_name(name, bank);
41 *bank = get_flash_bank_by_name_noprobe(name);
45 if (retval != ERROR_OK)
51 COMMAND_PARSE_NUMBER(uint, name, bank_num);
54 return get_flash_bank_by_num(bank_num, bank);
56 *bank = get_flash_bank_by_num_noprobe(bank_num);
57 retval = (bank) ? ERROR_OK : ERROR_FAIL;
62 COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
63 struct flash_bank **bank)
65 return CALL_COMMAND_HANDLER(flash_command_get_bank_maybe_probe,
66 name_index, bank, true);
69 COMMAND_HANDLER(handle_flash_info_command)
74 bool show_sectors = false;
75 bool prot_block_available;
77 if (CMD_ARGC < 1 || CMD_ARGC > 2)
78 return ERROR_COMMAND_SYNTAX_ERROR;
81 if (strcmp("sectors", CMD_ARGV[1]) == 0)
84 return ERROR_COMMAND_SYNTAX_ERROR;
87 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
88 if (retval != ERROR_OK)
94 struct flash_sector *block_array;
96 /* attempt auto probe */
97 retval = p->driver->auto_probe(p);
98 if (retval != ERROR_OK)
101 /* If the driver does not implement protection, we show the default
102 * state of is_protected array - usually protection state unknown */
103 if (p->driver->protect_check == NULL) {
104 retval = ERROR_FLASH_OPER_UNSUPPORTED;
106 /* We must query the hardware to avoid printing stale information! */
107 retval = p->driver->protect_check(p);
108 if (retval != ERROR_OK && retval != ERROR_FLASH_OPER_UNSUPPORTED)
111 if (retval == ERROR_FLASH_OPER_UNSUPPORTED)
112 LOG_INFO("Flash protection check is not implemented.");
115 "#%d : %s at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32
116 ", buswidth %i, chipwidth %i",
124 prot_block_available = p->num_prot_blocks && p->prot_blocks;
125 if (!show_sectors && prot_block_available) {
126 block_array = p->prot_blocks;
127 num_blocks = p->num_prot_blocks;
129 block_array = p->sectors;
130 num_blocks = p->num_sectors;
133 for (j = 0; j < num_blocks; j++) {
134 char *protect_state = "";
136 if (block_array[j].is_protected == 0)
137 protect_state = "not protected";
138 else if (block_array[j].is_protected == 1)
139 protect_state = "protected";
140 else if (!show_sectors || !prot_block_available)
141 protect_state = "protection state unknown";
144 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
146 block_array[j].offset,
148 block_array[j].size >> 10,
152 if (p->driver->info != NULL) {
153 retval = p->driver->info(p, buf, sizeof(buf));
154 if (retval == ERROR_OK)
155 command_print(CMD, "%s", buf);
157 LOG_ERROR("error retrieving flash info");
164 COMMAND_HANDLER(handle_flash_probe_command)
166 struct flash_bank *p;
170 return ERROR_COMMAND_SYNTAX_ERROR;
172 retval = CALL_COMMAND_HANDLER(flash_command_get_bank_maybe_probe, 0, &p, false);
173 if (retval != ERROR_OK)
177 retval = p->driver->probe(p);
178 if (retval == ERROR_OK)
180 "flash '%s' found at " TARGET_ADDR_FMT,
184 command_print(CMD, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
191 COMMAND_HANDLER(handle_flash_erase_check_command)
195 return ERROR_COMMAND_SYNTAX_ERROR;
197 struct flash_bank *p;
198 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
199 if (ERROR_OK != retval)
203 retval = p->driver->erase_check(p);
204 if (retval == ERROR_OK)
205 command_print(CMD, "successfully checked erase state");
208 "unknown error when checking erase state of flash bank #%s at "
214 for (j = 0; j < p->num_sectors; j++) {
217 if (p->sectors[j].is_erased == 0)
218 erase_state = "not erased";
219 else if (p->sectors[j].is_erased == 1)
222 erase_state = "erase state unknown";
226 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
228 p->sectors[j].offset,
230 p->sectors[j].size >> 10,
235 command_print(CMD, "\tBank is erased");
239 COMMAND_HANDLER(handle_flash_erase_address_command)
241 struct flash_bank *p;
242 int retval = ERROR_OK;
243 target_addr_t address;
246 bool do_unlock = false;
247 struct target *target = get_current_target(CMD_CTX);
249 while (CMD_ARGC >= 3) {
250 /* Optionally pad out the address range to block/sector
251 * boundaries. We can't know if there's data in that part
252 * of the flash; only do padding if we're told to.
254 if (strcmp("pad", CMD_ARGV[0]) == 0)
256 else if (strcmp("unlock", CMD_ARGV[0]) == 0)
259 return ERROR_COMMAND_SYNTAX_ERROR;
264 return ERROR_COMMAND_SYNTAX_ERROR;
266 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
267 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
270 command_print(CMD, "Length must be >0");
271 return ERROR_COMMAND_SYNTAX_ERROR;
274 retval = get_flash_bank_by_addr(target, address, true, &p);
275 if (retval != ERROR_OK)
278 /* We can't know if we did a resume + halt, in which case we no longer know the erased state
282 struct duration bench;
283 duration_start(&bench);
286 retval = flash_unlock_address_range(target, address, length);
288 if (retval == ERROR_OK)
289 retval = flash_erase_address_range(target, do_pad, address, length);
291 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
292 command_print(CMD, "erased address " TARGET_ADDR_FMT " (length %"
294 " in %fs (%0.3f KiB/s)", address, length,
295 duration_elapsed(&bench), duration_kbps(&bench, length));
301 COMMAND_HANDLER(handle_flash_erase_command)
304 return ERROR_COMMAND_SYNTAX_ERROR;
309 struct flash_bank *p;
312 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
313 if (retval != ERROR_OK)
316 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
317 if (strcmp(CMD_ARGV[2], "last") == 0)
318 last = p->num_sectors - 1;
320 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
322 if (!(first <= last)) {
323 command_print(CMD, "ERROR: "
324 "first sector must be <= last");
328 if (!(last <= (uint32_t)(p->num_sectors - 1))) {
329 command_print(CMD, "ERROR: "
330 "last sector must be <= %" PRIu32,
335 struct duration bench;
336 duration_start(&bench);
338 retval = flash_driver_erase(p, first, last);
340 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
341 command_print(CMD, "erased sectors %" PRIu32 " "
342 "through %" PRIu32 " on flash bank %d "
343 "in %fs", first, last, p->bank_number, duration_elapsed(&bench));
349 COMMAND_HANDLER(handle_flash_protect_command)
352 return ERROR_COMMAND_SYNTAX_ERROR;
357 struct flash_bank *p;
361 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
362 if (retval != ERROR_OK)
365 if (p->num_prot_blocks)
366 num_blocks = p->num_prot_blocks;
368 num_blocks = p->num_sectors;
370 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
371 if (strcmp(CMD_ARGV[2], "last") == 0)
372 last = num_blocks - 1;
374 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
377 COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
379 if (!(first <= last)) {
380 command_print(CMD, "ERROR: "
381 "first %s must be <= last",
382 (p->num_prot_blocks) ? "block" : "sector");
386 if (!(last <= (uint32_t)(num_blocks - 1))) {
387 command_print(CMD, "ERROR: "
388 "last %s must be <= %" PRIu32,
389 (p->num_prot_blocks) ? "block" : "sector",
394 retval = flash_driver_protect(p, set, first, last);
395 if (retval == ERROR_OK) {
396 command_print(CMD, "%s protection for %s %" PRIu32
397 " through %" PRIu32 " on flash bank %d",
398 (set) ? "set" : "cleared",
399 (p->num_prot_blocks) ? "blocks" : "sectors",
400 first, last, p->bank_number);
406 COMMAND_HANDLER(handle_flash_write_image_command)
408 struct target *target = get_current_target(CMD_CTX);
415 /* flash auto-erase is disabled by default*/
417 bool auto_unlock = false;
420 if (strcmp(CMD_ARGV[0], "erase") == 0) {
424 command_print(CMD, "auto erase enabled");
425 } else if (strcmp(CMD_ARGV[0], "unlock") == 0) {
429 command_print(CMD, "auto unlock enabled");
435 return ERROR_COMMAND_SYNTAX_ERROR;
438 LOG_ERROR("no target selected");
442 struct duration bench;
443 duration_start(&bench);
446 image.base_address_set = 1;
447 COMMAND_PARSE_NUMBER(llong, CMD_ARGV[1], image.base_address);
449 image.base_address_set = 0;
450 image.base_address = 0x0;
453 image.start_address_set = 0;
455 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
456 if (retval != ERROR_OK)
459 retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
460 if (retval != ERROR_OK) {
465 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
466 command_print(CMD, "wrote %" PRIu32 " bytes from file %s "
467 "in %fs (%0.3f KiB/s)", written, CMD_ARGV[0],
468 duration_elapsed(&bench), duration_kbps(&bench, written));
476 COMMAND_HANDLER(handle_flash_fill_command)
478 target_addr_t address;
481 struct target *target = get_current_target(CMD_CTX);
487 return ERROR_COMMAND_SYNTAX_ERROR;
489 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
490 COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], pattern);
491 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
493 struct flash_bank *bank;
494 retval = get_flash_bank_by_addr(target, address, true, &bank);
495 if (retval != ERROR_OK)
498 switch (CMD_NAME[4]) {
512 return ERROR_COMMAND_SYNTAX_ERROR;
515 if ((wordsize < sizeof(pattern)) && (pattern >> (8 * wordsize) != 0)) {
516 command_print(CMD, "Fill pattern 0x%" PRIx64 " does not fit within %" PRIu32 "-byte word", pattern, wordsize);
523 if (address + count * wordsize > bank->base + bank->size) {
524 LOG_ERROR("Cannot cross flash bank borders");
528 uint32_t size_bytes = count * wordsize;
529 target_addr_t aligned_start = flash_write_align_start(bank, address);
530 target_addr_t end_addr = address + size_bytes - 1;
531 target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
532 uint32_t aligned_size = aligned_end + 1 - aligned_start;
533 uint32_t padding_at_start = address - aligned_start;
534 uint32_t padding_at_end = aligned_end - end_addr;
536 uint8_t *buffer = malloc(aligned_size);
540 if (padding_at_start) {
541 memset(buffer, bank->default_padded_value, padding_at_start);
542 LOG_WARNING("Start address " TARGET_ADDR_FMT
543 " breaks the required alignment of flash bank %s",
544 address, bank->name);
545 LOG_WARNING("Padding %" PRId32 " bytes from " TARGET_ADDR_FMT,
546 padding_at_start, aligned_start);
549 uint8_t *ptr = buffer + padding_at_start;
553 for (i = 0; i < count; i++, ptr += wordsize)
554 target_buffer_set_u64(target, ptr, pattern);
557 for (i = 0; i < count; i++, ptr += wordsize)
558 target_buffer_set_u32(target, ptr, pattern);
561 for (i = 0; i < count; i++, ptr += wordsize)
562 target_buffer_set_u16(target, ptr, pattern);
565 memset(ptr, pattern, count);
569 LOG_ERROR("BUG: can't happen");
573 if (padding_at_end) {
574 memset(ptr, bank->default_padded_value, padding_at_end);
575 LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRId32
576 " bytes (bank write end alignment)",
577 end_addr + 1, padding_at_end);
580 struct duration bench;
581 duration_start(&bench);
583 retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
584 if (retval != ERROR_OK)
587 retval = flash_driver_read(bank, buffer, address - bank->base, size_bytes);
588 if (retval != ERROR_OK)
591 for (i = 0, ptr = buffer; i < count; i++) {
592 uint64_t readback = 0;
596 readback = target_buffer_get_u64(target, ptr);
599 readback = target_buffer_get_u32(target, ptr);
602 readback = target_buffer_get_u16(target, ptr);
608 if (readback != pattern) {
610 "Verification error address " TARGET_ADDR_FMT
611 ", read back 0x%02" PRIx64 ", expected 0x%02" PRIx64,
612 address + i * wordsize, readback, pattern);
619 if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
620 command_print(CMD, "wrote %" PRIu32 " bytes to " TARGET_ADDR_FMT
621 " in %fs (%0.3f KiB/s)", size_bytes, address,
622 duration_elapsed(&bench), duration_kbps(&bench, size_bytes));
631 COMMAND_HANDLER(handle_flash_write_bank_command)
636 struct fileio *fileio;
638 if (CMD_ARGC < 2 || CMD_ARGC > 3)
639 return ERROR_COMMAND_SYNTAX_ERROR;
641 struct duration bench;
642 duration_start(&bench);
644 struct flash_bank *bank;
645 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
646 if (ERROR_OK != retval)
652 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
654 if (offset > bank->size) {
655 LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
657 return ERROR_COMMAND_ARGUMENT_INVALID;
660 if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
664 retval = fileio_size(fileio, &filesize);
665 if (retval != ERROR_OK) {
666 fileio_close(fileio);
670 length = MIN(filesize, bank->size - offset);
673 LOG_INFO("Nothing to write to flash bank");
674 fileio_close(fileio);
678 if (length != filesize)
679 LOG_INFO("File content exceeds flash bank size. Only writing the "
680 "first %zu bytes of the file", length);
682 target_addr_t start_addr = bank->base + offset;
683 target_addr_t aligned_start = flash_write_align_start(bank, start_addr);
684 target_addr_t end_addr = start_addr + length - 1;
685 target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
686 uint32_t aligned_size = aligned_end + 1 - aligned_start;
687 uint32_t padding_at_start = start_addr - aligned_start;
688 uint32_t padding_at_end = aligned_end - end_addr;
690 buffer = malloc(aligned_size);
691 if (buffer == NULL) {
692 fileio_close(fileio);
693 LOG_ERROR("Out of memory");
697 if (padding_at_start) {
698 memset(buffer, bank->default_padded_value, padding_at_start);
699 LOG_WARNING("Start offset 0x%08" PRIx32
700 " breaks the required alignment of flash bank %s",
702 LOG_WARNING("Padding %" PRId32 " bytes from " TARGET_ADDR_FMT,
703 padding_at_start, aligned_start);
706 uint8_t *ptr = buffer + padding_at_start;
708 if (fileio_read(fileio, length, ptr, &buf_cnt) != ERROR_OK) {
710 fileio_close(fileio);
714 if (buf_cnt != length) {
715 LOG_ERROR("Short read");
722 if (padding_at_end) {
723 memset(ptr, bank->default_padded_value, padding_at_end);
724 LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRId32
725 " bytes (bank write end alignment)",
726 end_addr + 1, padding_at_end);
729 retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
733 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
734 command_print(CMD, "wrote %zu bytes from file %s to flash bank %u"
735 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
736 length, CMD_ARGV[1], bank->bank_number, offset,
737 duration_elapsed(&bench), duration_kbps(&bench, length));
740 fileio_close(fileio);
745 COMMAND_HANDLER(handle_flash_read_bank_command)
749 struct fileio *fileio;
753 if (CMD_ARGC < 2 || CMD_ARGC > 4)
754 return ERROR_COMMAND_SYNTAX_ERROR;
756 struct duration bench;
757 duration_start(&bench);
759 struct flash_bank *p;
760 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
762 if (ERROR_OK != retval)
768 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
770 if (offset > p->size) {
771 LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
773 return ERROR_COMMAND_ARGUMENT_INVALID;
776 length = p->size - offset;
779 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], length);
781 if (offset + length > p->size) {
782 LOG_ERROR("Length of %" PRIu32 " bytes with offset 0x%8.8" PRIx32
783 " is out of range of the flash bank", length, offset);
784 return ERROR_COMMAND_ARGUMENT_INVALID;
787 buffer = malloc(length);
788 if (buffer == NULL) {
789 LOG_ERROR("Out of memory");
793 retval = flash_driver_read(p, buffer, offset, length);
794 if (retval != ERROR_OK) {
795 LOG_ERROR("Read error");
800 retval = fileio_open(&fileio, CMD_ARGV[1], FILEIO_WRITE, FILEIO_BINARY);
801 if (retval != ERROR_OK) {
802 LOG_ERROR("Could not open file");
807 retval = fileio_write(fileio, length, buffer, &written);
808 fileio_close(fileio);
810 if (retval != ERROR_OK) {
811 LOG_ERROR("Could not write file");
815 if (duration_measure(&bench) == ERROR_OK)
816 command_print(CMD, "wrote %zd bytes to file %s from flash bank %u"
817 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
818 written, CMD_ARGV[1], p->bank_number, offset,
819 duration_elapsed(&bench), duration_kbps(&bench, written));
825 COMMAND_HANDLER(handle_flash_verify_bank_command)
828 uint8_t *buffer_file, *buffer_flash;
829 struct fileio *fileio;
835 if (CMD_ARGC < 2 || CMD_ARGC > 3)
836 return ERROR_COMMAND_SYNTAX_ERROR;
838 struct duration bench;
839 duration_start(&bench);
841 struct flash_bank *p;
842 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
843 if (ERROR_OK != retval)
849 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
851 if (offset > p->size) {
852 LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
854 return ERROR_COMMAND_ARGUMENT_INVALID;
857 retval = fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY);
858 if (retval != ERROR_OK) {
859 LOG_ERROR("Could not open file");
863 retval = fileio_size(fileio, &filesize);
864 if (retval != ERROR_OK) {
865 fileio_close(fileio);
869 length = MIN(filesize, p->size - offset);
872 LOG_INFO("Nothing to compare with flash bank");
873 fileio_close(fileio);
877 if (length != filesize)
878 LOG_INFO("File content exceeds flash bank size. Only comparing the "
879 "first %zu bytes of the file", length);
881 buffer_file = malloc(length);
882 if (buffer_file == NULL) {
883 LOG_ERROR("Out of memory");
884 fileio_close(fileio);
888 retval = fileio_read(fileio, length, buffer_file, &read_cnt);
889 fileio_close(fileio);
890 if (retval != ERROR_OK) {
891 LOG_ERROR("File read failure");
896 if (read_cnt != length) {
897 LOG_ERROR("Short read");
902 buffer_flash = malloc(length);
903 if (buffer_flash == NULL) {
904 LOG_ERROR("Out of memory");
909 retval = flash_driver_read(p, buffer_flash, offset, length);
910 if (retval != ERROR_OK) {
911 LOG_ERROR("Flash read error");
917 if (duration_measure(&bench) == ERROR_OK)
918 command_print(CMD, "read %zd bytes from file %s and flash bank %u"
919 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
920 length, CMD_ARGV[1], p->bank_number, offset,
921 duration_elapsed(&bench), duration_kbps(&bench, length));
923 differ = memcmp(buffer_file, buffer_flash, length);
924 command_print(CMD, "contents %s", differ ? "differ" : "match");
928 for (t = 0; t < length; t++) {
929 if (buffer_flash[t] == buffer_file[t])
931 command_print(CMD, "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
932 diffs, t + offset, buffer_flash[t], buffer_file[t]);
933 if (diffs++ >= 127) {
934 command_print(CMD, "More than 128 errors, the rest are not printed.");
943 return differ ? ERROR_FAIL : ERROR_OK;
946 void flash_set_dirty(void)
948 struct flash_bank *c;
951 /* set all flash to require erasing */
952 for (c = flash_bank_list(); c; c = c->next) {
953 for (i = 0; i < c->num_sectors; i++)
954 c->sectors[i].is_erased = 0;
958 COMMAND_HANDLER(handle_flash_padded_value_command)
961 return ERROR_COMMAND_SYNTAX_ERROR;
963 struct flash_bank *p;
964 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
965 if (ERROR_OK != retval)
968 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], p->default_padded_value);
970 command_print(CMD, "Default padded value set to 0x%" PRIx8 " for flash bank %u", \
971 p->default_padded_value, p->bank_number);
976 static const struct command_registration flash_exec_command_handlers[] = {
979 .handler = handle_flash_probe_command,
980 .mode = COMMAND_EXEC,
982 .help = "Identify a flash bank.",
986 .handler = handle_flash_info_command,
987 .mode = COMMAND_EXEC,
988 .usage = "bank_id ['sectors']",
989 .help = "Print information about a flash bank.",
992 .name = "erase_check",
993 .handler = handle_flash_erase_check_command,
994 .mode = COMMAND_EXEC,
996 .help = "Check erase state of all blocks in a "
1000 .name = "erase_sector",
1001 .handler = handle_flash_erase_command,
1002 .mode = COMMAND_EXEC,
1003 .usage = "bank_id first_sector_num (last_sector_num|'last')",
1004 .help = "Erase a range of sectors in a flash bank.",
1007 .name = "erase_address",
1008 .handler = handle_flash_erase_address_command,
1009 .mode = COMMAND_EXEC,
1010 .usage = "['pad'] ['unlock'] address length",
1011 .help = "Erase flash sectors starting at address and "
1012 "continuing for length bytes. If 'pad' is specified, "
1013 "data outside that range may also be erased: the start "
1014 "address may be decreased, and length increased, so "
1015 "that all of the first and last sectors are erased. "
1016 "If 'unlock' is specified, then the flash is unprotected "
1022 .handler = handle_flash_fill_command,
1023 .mode = COMMAND_EXEC,
1024 .usage = "address value n",
1025 .help = "Fill n double-words with 64-bit value, starting at "
1026 "word address. (No autoerase.)",
1030 .handler = handle_flash_fill_command,
1031 .mode = COMMAND_EXEC,
1032 .usage = "address value n",
1033 .help = "Fill n words with 32-bit value, starting at "
1034 "word address. (No autoerase.)",
1038 .handler = handle_flash_fill_command,
1039 .mode = COMMAND_EXEC,
1040 .usage = "address value n",
1041 .help = "Fill n halfwords with 16-bit value, starting at "
1042 "word address. (No autoerase.)",
1046 .handler = handle_flash_fill_command,
1047 .mode = COMMAND_EXEC,
1048 .usage = "address value n",
1049 .help = "Fill n bytes with 8-bit value, starting at "
1050 "word address. (No autoerase.)",
1053 .name = "write_bank",
1054 .handler = handle_flash_write_bank_command,
1055 .mode = COMMAND_EXEC,
1056 .usage = "bank_id filename [offset]",
1057 .help = "Write binary data from file to flash bank. Allow optional "
1058 "offset from beginning of the bank (defaults to zero).",
1061 .name = "write_image",
1062 .handler = handle_flash_write_image_command,
1063 .mode = COMMAND_EXEC,
1064 .usage = "[erase] [unlock] filename [offset [file_type]]",
1065 .help = "Write an image to flash. Optionally first unprotect "
1066 "and/or erase the region to be used. Allow optional "
1067 "offset from beginning of bank (defaults to zero)",
1070 .name = "read_bank",
1071 .handler = handle_flash_read_bank_command,
1072 .mode = COMMAND_EXEC,
1073 .usage = "bank_id filename [offset [length]]",
1074 .help = "Read binary data from flash bank to file. Allow optional "
1075 "offset from beginning of the bank (defaults to zero).",
1078 .name = "verify_bank",
1079 .handler = handle_flash_verify_bank_command,
1080 .mode = COMMAND_EXEC,
1081 .usage = "bank_id filename [offset]",
1082 .help = "Compare the contents of a file with the contents of the "
1083 "flash bank. Allow optional offset from beginning of the bank "
1084 "(defaults to zero).",
1088 .handler = handle_flash_protect_command,
1089 .mode = COMMAND_EXEC,
1090 .usage = "bank_id first_block [last_block|'last'] "
1092 .help = "Turn protection on or off for a range of protection "
1093 "blocks or sectors in a given flash bank. "
1094 "See 'flash info' output for a list of blocks.",
1097 .name = "padded_value",
1098 .handler = handle_flash_padded_value_command,
1099 .mode = COMMAND_EXEC,
1100 .usage = "bank_id value",
1101 .help = "Set default flash padded value",
1103 COMMAND_REGISTRATION_DONE
1106 static int flash_init_drivers(struct command_context *cmd_ctx)
1108 if (!flash_bank_list())
1111 struct command *parent = command_find_in_context(cmd_ctx, "flash");
1112 return register_commands(cmd_ctx, parent, flash_exec_command_handlers);
1115 COMMAND_HANDLER(handle_flash_bank_command)
1118 LOG_ERROR("usage: flash bank <name> <driver> "
1119 "<base> <size> <chip_width> <bus_width> <target>");
1120 return ERROR_COMMAND_SYNTAX_ERROR;
1122 /* save bank name and advance arguments for compatibility */
1123 const char *bank_name = *CMD_ARGV++;
1126 struct target *target = get_target(CMD_ARGV[5]);
1127 if (target == NULL) {
1128 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
1132 const char *driver_name = CMD_ARGV[0];
1133 const struct flash_driver *driver = flash_driver_find_by_name(driver_name);
1134 if (NULL == driver) {
1135 /* no matching flash driver found */
1136 LOG_ERROR("flash driver '%s' not found", driver_name);
1140 /* check the flash bank name is unique */
1141 if (get_flash_bank_by_name_noprobe(bank_name) != NULL) {
1142 /* flash bank name already exists */
1143 LOG_ERROR("flash bank name '%s' already exists", bank_name);
1147 /* register flash specific commands */
1148 if (NULL != driver->commands) {
1149 int retval = register_commands(CMD_CTX, NULL,
1151 if (ERROR_OK != retval) {
1152 LOG_ERROR("couldn't register '%s' commands",
1158 struct flash_bank *c = calloc(1, sizeof(*c));
1159 c->name = strdup(bank_name);
1162 COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[1], c->base);
1163 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
1164 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
1165 COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
1166 c->default_padded_value = c->erased_value = 0xff;
1167 c->minimal_write_gap = FLASH_WRITE_GAP_SECTOR;
1170 retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
1171 if (ERROR_OK != retval) {
1172 LOG_ERROR("'%s' driver rejected flash bank at " TARGET_ADDR_FMT
1173 "; usage: %s", driver_name, c->base, driver->usage);
1178 if (driver->usage == NULL)
1179 LOG_DEBUG("'%s' driver usage field missing", driver_name);
1186 COMMAND_HANDLER(handle_flash_banks_command)
1189 return ERROR_COMMAND_SYNTAX_ERROR;
1192 for (struct flash_bank *p = flash_bank_list(); p; p = p->next, n++) {
1193 command_print(CMD, "#%d : %s (%s) at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32 ", "
1194 "buswidth %u, chipwidth %u", p->bank_number,
1195 p->name, p->driver->name, p->base, p->size,
1196 p->bus_width, p->chip_width);
1201 static int jim_flash_list(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
1204 Jim_WrongNumArgs(interp, 1, argv,
1205 "no arguments to 'flash list' command");
1209 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1211 for (struct flash_bank *p = flash_bank_list(); p; p = p->next) {
1212 Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
1214 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
1215 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
1216 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
1217 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
1218 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
1219 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
1220 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
1221 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
1222 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
1223 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
1225 Jim_ListAppendElement(interp, list, elem);
1228 Jim_SetResult(interp, list);
1233 COMMAND_HANDLER(handle_flash_init_command)
1236 return ERROR_COMMAND_SYNTAX_ERROR;
1238 static bool flash_initialized;
1239 if (flash_initialized) {
1240 LOG_INFO("'flash init' has already been called");
1243 flash_initialized = true;
1245 LOG_DEBUG("Initializing flash devices...");
1246 return flash_init_drivers(CMD_CTX);
1249 static const struct command_registration flash_config_command_handlers[] = {
1252 .handler = handle_flash_bank_command,
1253 .mode = COMMAND_CONFIG,
1254 .usage = "bank_id driver_name base_address size_bytes "
1255 "chip_width_bytes bus_width_bytes target "
1256 "[driver_options ...]",
1257 .help = "Define a new bank with the given name, "
1258 "using the specified NOR flash driver.",
1262 .mode = COMMAND_CONFIG,
1263 .handler = handle_flash_init_command,
1264 .help = "Initialize flash devices.",
1269 .mode = COMMAND_ANY,
1270 .handler = handle_flash_banks_command,
1271 .help = "Display table with information about flash banks.",
1276 .mode = COMMAND_ANY,
1277 .jim_handler = jim_flash_list,
1278 .help = "Returns a list of details about the flash banks.",
1280 COMMAND_REGISTRATION_DONE
1282 static const struct command_registration flash_command_handlers[] = {
1285 .mode = COMMAND_ANY,
1286 .help = "NOR flash command group",
1287 .chain = flash_config_command_handlers,
1290 COMMAND_REGISTRATION_DONE
1293 int flash_register_commands(struct command_context *cmd_ctx)
1295 return register_commands(cmd_ctx, NULL, flash_command_handlers);