OpenOCD
flash/nor/tcl.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
5  * Copyright (C) 2007,2008 Øyvind Harboe <oyvind.harboe@zylin.com> *
6  * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
7  * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
8  * Copyright (C) 2017-2018 Tomas Vanek <vanekt@fbl.cz> *
9  ***************************************************************************/
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 #include "imp.h"
14 #include <helper/time_support.h>
15 #include <target/image.h>
16 
22 COMMAND_HELPER(flash_command_get_bank_probe_optional, unsigned int name_index,
23  struct flash_bank **bank, bool do_probe)
24 {
25  const char *name = CMD_ARGV[name_index];
26  int retval;
27  if (do_probe) {
28  retval = get_flash_bank_by_name(name, bank);
29  } else {
31  retval = ERROR_OK;
32  }
33 
34  if (retval != ERROR_OK)
35  return retval;
36  if (*bank)
37  return ERROR_OK;
38 
39  unsigned int bank_num;
40  COMMAND_PARSE_NUMBER(uint, name, bank_num);
41 
42  if (do_probe) {
43  return get_flash_bank_by_num(bank_num, bank);
44  } else {
46  retval = (bank) ? ERROR_OK : ERROR_FAIL;
47  return retval;
48  }
49 }
50 
51 COMMAND_HELPER(flash_command_get_bank, unsigned int name_index,
52  struct flash_bank **bank)
53 {
54  return CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional,
55  name_index, bank, true);
56 }
57 
58 COMMAND_HANDLER(handle_flash_info_command)
59 {
60  struct flash_bank *p;
61  int j = 0;
62  int retval;
63  bool show_sectors = false;
64  bool prot_block_available;
65 
66  if (CMD_ARGC < 1 || CMD_ARGC > 2)
68 
69  if (CMD_ARGC == 2) {
70  if (strcmp("sectors", CMD_ARGV[1]) == 0)
71  show_sectors = true;
72  else
74  }
75 
76  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
77  if (retval != ERROR_OK)
78  return retval;
79 
80  if (p) {
81  int num_blocks;
82  struct flash_sector *block_array;
83 
84  /* attempt auto probe */
85  retval = p->driver->auto_probe(p);
86  if (retval != ERROR_OK)
87  return retval;
88 
89  /* If the driver does not implement protection, we show the default
90  * state of is_protected array - usually protection state unknown */
91  if (!p->driver->protect_check) {
93  } else {
94  /* We must query the hardware to avoid printing stale information! */
95  retval = p->driver->protect_check(p);
96  if (retval != ERROR_OK && retval != ERROR_FLASH_OPER_UNSUPPORTED)
97  return retval;
98  }
99  if (retval == ERROR_FLASH_OPER_UNSUPPORTED)
100  LOG_INFO("Flash protection check is not implemented.");
101 
103  "#%u : %s at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32
104  ", buswidth %u, chipwidth %u",
105  p->bank_number,
106  p->driver->name,
107  p->base,
108  p->size,
109  p->bus_width,
110  p->chip_width);
111 
112  prot_block_available = p->num_prot_blocks && p->prot_blocks;
113  if (!show_sectors && prot_block_available) {
114  block_array = p->prot_blocks;
115  num_blocks = p->num_prot_blocks;
116  } else {
117  block_array = p->sectors;
118  num_blocks = p->num_sectors;
119  }
120 
121  for (j = 0; j < num_blocks; j++) {
122  char *protect_state = "";
123 
124  if (block_array[j].is_protected == 0)
125  protect_state = "not protected";
126  else if (block_array[j].is_protected == 1)
127  protect_state = "protected";
128  else if (!show_sectors || !prot_block_available)
129  protect_state = "protection state unknown";
130 
132  "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIu32 "kB) %s",
133  j,
134  block_array[j].offset,
135  block_array[j].size,
136  block_array[j].size >> 10,
137  protect_state);
138  }
139 
140  if (p->driver->info) {
141  /* Let the flash driver print extra custom info */
142  retval = p->driver->info(p, CMD);
144  if (retval != ERROR_OK)
145  LOG_ERROR("error retrieving flash info");
146  }
147  }
148 
149  return retval;
150 }
151 
152 COMMAND_HANDLER(handle_flash_probe_command)
153 {
154  struct flash_bank *p;
155  int retval;
156 
157  if (CMD_ARGC != 1)
159 
160  retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0, &p, false);
161  if (retval != ERROR_OK)
162  return retval;
163 
164  if (p) {
165  retval = p->driver->probe(p);
166  if (retval == ERROR_OK)
168  "flash '%s' found at " TARGET_ADDR_FMT,
169  p->driver->name,
170  p->base);
171  } else {
172  command_print(CMD, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
173  retval = ERROR_FAIL;
174  }
175 
176  return retval;
177 }
178 
179 COMMAND_HANDLER(handle_flash_erase_check_command)
180 {
181  bool blank = true;
182  if (CMD_ARGC != 1)
184 
185  struct flash_bank *p;
186  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
187  if (retval != ERROR_OK)
188  return retval;
189 
190  if (p->driver->erase_check)
191  retval = p->driver->erase_check(p);
192  else
193  retval = default_flash_blank_check(p);
194  if (retval == ERROR_OK) {
195  command_print(CMD, "successfully checked erase state");
196  } else {
198  "Error: checking erase state of flash bank #%s at "
200  CMD_ARGV[0],
201  p->base);
202  }
203 
204  for (unsigned int j = 0; j < p->num_sectors; j++) {
205  char *erase_state;
206 
207  if (p->sectors[j].is_erased == 0)
208  erase_state = "not erased";
209  else if (p->sectors[j].is_erased == 1)
210  continue;
211  else
212  erase_state = "erase state unknown";
213 
214  blank = false;
216  "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIu32 "kB) %s",
217  j,
218  p->sectors[j].offset,
219  p->sectors[j].size,
220  p->sectors[j].size >> 10,
221  erase_state);
222  }
223 
224  if (p->num_sectors == 0)
225  blank = (p->is_erased == 1);
226 
227  if (blank)
228  command_print(CMD, "\tBank is erased");
229  return retval;
230 }
231 
232 COMMAND_HANDLER(handle_flash_erase_address_command)
233 {
234  struct flash_bank *p;
235  int retval = ERROR_OK;
237  uint32_t length;
238  bool do_pad = false;
239  bool do_unlock = false;
241 
242  while (CMD_ARGC >= 3) {
243  /* Optionally pad out the address range to block/sector
244  * boundaries. We can't know if there's data in that part
245  * of the flash; only do padding if we're told to.
246  */
247  if (strcmp("pad", CMD_ARGV[0]) == 0)
248  do_pad = true;
249  else if (strcmp("unlock", CMD_ARGV[0]) == 0)
250  do_unlock = true;
251  else
253  CMD_ARGC--;
254  CMD_ARGV++;
255  }
256  if (CMD_ARGC != 2)
258 
261 
262  if (length <= 0) {
263  command_print(CMD, "Length must be >0");
265  }
266 
267  retval = get_flash_bank_by_addr(target, address, true, &p);
268  if (retval != ERROR_OK)
269  return retval;
270 
271  /* We can't know if we did a resume + halt, in which case we no longer know the erased state
272  **/
273  flash_set_dirty();
274 
275  struct duration bench;
276  duration_start(&bench);
277 
278  if (do_unlock)
280 
281  if (retval == ERROR_OK)
282  retval = flash_erase_address_range(target, do_pad, address, length,
283  false /* skip_banks_without_erase */);
284 
285  if (retval == ERROR_FLASH_OPER_UNSUPPORTED) {
286  command_print(CMD, "erase operation is not supported");
287  return retval;
288  } else if (retval != ERROR_OK) {
289  command_print(CMD, "erase operation failed");
290  return retval;
291  }
292 
293  if (duration_measure(&bench) == ERROR_OK) {
294  command_print(CMD, "erased address " TARGET_ADDR_FMT " (length %" PRIu32 ")"
295  " in %fs (%0.3f KiB/s)", address, length,
296  duration_elapsed(&bench), duration_kbps(&bench, length));
297  }
298 
299  return ERROR_OK;
300 }
301 
302 COMMAND_HANDLER(handle_flash_erase_command)
303 {
304  if (CMD_ARGC != 3)
306 
307  uint32_t first;
308  uint32_t last;
309 
310  struct flash_bank *p;
311  int retval;
312 
313  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
314  if (retval != ERROR_OK)
315  return retval;
316 
317  if (p->num_sectors == 0) {
318  LOG_INFO("bank has no sectors, ignoring specified sectors");
319  first = 0;
320  last = 0;
321  } else {
322  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
323  if (strcmp(CMD_ARGV[2], "last") == 0)
324  last = p->num_sectors - 1;
325  else
326  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
327 
328  if (!(first <= last)) {
329  command_print(CMD, "ERROR: "
330  "first sector must be <= last");
331  return ERROR_FAIL;
332  }
333 
334  if (!(last <= (p->num_sectors - 1))) {
335  command_print(CMD, "ERROR: "
336  "last sector must be <= %u",
337  p->num_sectors - 1);
338  return ERROR_FAIL;
339  }
340  }
341 
342  struct duration bench;
343  duration_start(&bench);
344 
345  retval = flash_driver_erase(p, first, last);
346 
347  if (retval == ERROR_FLASH_OPER_UNSUPPORTED) {
348  command_print(CMD, "erase operation is not supported");
349  return retval;
350  } else if (retval != ERROR_OK) {
351  command_print(CMD, "failed to erase sectors");
352  return retval;
353  }
354 
355  if (duration_measure(&bench) == ERROR_OK) {
356  command_print(CMD, "erased sectors %" PRIu32 " "
357  "through %" PRIu32 " on flash bank %u "
358  "in %fs", first, last, p->bank_number, duration_elapsed(&bench));
359  }
360 
361  return ERROR_OK;
362 }
363 
364 COMMAND_HANDLER(handle_flash_protect_command)
365 {
366  if (CMD_ARGC != 4)
368 
369  uint32_t first;
370  uint32_t last;
371 
372  struct flash_bank *p;
373  int retval;
374  int num_blocks;
375 
376  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
377  if (retval != ERROR_OK)
378  return retval;
379 
380  if (p->num_prot_blocks)
381  num_blocks = p->num_prot_blocks;
382  else
383  num_blocks = p->num_sectors;
384 
385  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
386  if (strcmp(CMD_ARGV[2], "last") == 0)
387  last = num_blocks - 1;
388  else
389  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
390 
391  bool set;
393 
394  if (!(first <= last)) {
395  command_print(CMD, "ERROR: "
396  "first %s must be <= last",
397  (p->num_prot_blocks) ? "block" : "sector");
398  return ERROR_FAIL;
399  }
400 
401  if (!(last <= (uint32_t)(num_blocks - 1))) {
402  command_print(CMD, "ERROR: "
403  "last %s must be <= %d",
404  (p->num_prot_blocks) ? "block" : "sector",
405  num_blocks - 1);
406  return ERROR_FAIL;
407  }
408 
409  retval = flash_driver_protect(p, set, first, last);
410  if (retval != ERROR_OK) {
411  command_print(CMD, "failed to %s protection", set ? "set" : "clear");
412  return retval;
413  }
414 
415  command_print(CMD, "%s protection for %s %" PRIu32
416  " through %" PRIu32 " on flash bank %d",
417  set ? "set" : "cleared",
418  p->num_prot_blocks ? "blocks" : "sectors",
419  first, last, p->bank_number);
420 
421  return ERROR_OK;
422 }
423 
424 COMMAND_HANDLER(handle_flash_write_image_command)
425 {
427 
428  struct image image;
429  uint32_t written;
430 
431  int retval;
432 
433  /* flash auto-erase is disabled by default*/
434  int auto_erase = 0;
435  bool auto_unlock = false;
436 
437  while (CMD_ARGC) {
438  if (strcmp(CMD_ARGV[0], "erase") == 0) {
439  auto_erase = 1;
440  CMD_ARGV++;
441  CMD_ARGC--;
442  command_print(CMD, "auto erase enabled");
443  } else if (strcmp(CMD_ARGV[0], "unlock") == 0) {
444  auto_unlock = true;
445  CMD_ARGV++;
446  CMD_ARGC--;
447  command_print(CMD, "auto unlock enabled");
448  } else
449  break;
450  }
451 
452  if (CMD_ARGC < 1)
454 
455  if (!target) {
456  LOG_ERROR("no target selected");
457  return ERROR_FAIL;
458  }
459 
460  struct duration bench;
461  duration_start(&bench);
462 
463  if (CMD_ARGC >= 2) {
464  image.base_address_set = true;
466  } else {
467  image.base_address_set = false;
468  image.base_address = 0x0;
469  }
470 
471  image.start_address_set = false;
472 
473  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
474  if (retval != ERROR_OK)
475  return retval;
476 
477  retval = flash_write_unlock_verify(target, &image, &written, auto_erase,
478  auto_unlock, true, false);
479  if (retval != ERROR_OK) {
480  image_close(&image);
481  return retval;
482  }
483 
484  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
485  command_print(CMD, "wrote %" PRIu32 " bytes from file %s "
486  "in %fs (%0.3f KiB/s)", written, CMD_ARGV[0],
487  duration_elapsed(&bench), duration_kbps(&bench, written));
488  }
489 
490  image_close(&image);
491 
492  return retval;
493 }
494 
495 COMMAND_HANDLER(handle_flash_verify_image_command)
496 {
498 
499  struct image image;
500  uint32_t verified;
501 
502  int retval;
503 
504  if (CMD_ARGC < 1)
506 
507  if (!target) {
508  LOG_ERROR("no target selected");
509  return ERROR_FAIL;
510  }
511 
512  struct duration bench;
513  duration_start(&bench);
514 
515  if (CMD_ARGC >= 2) {
518  } else {
520  image.base_address = 0x0;
521  }
522 
524 
525  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
526  if (retval != ERROR_OK)
527  return retval;
528 
529  retval = flash_write_unlock_verify(target, &image, &verified, false,
530  false, false, true);
531  if (retval != ERROR_OK) {
532  image_close(&image);
533  return retval;
534  }
535 
536  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
537  command_print(CMD, "verified %" PRIu32 " bytes from file %s "
538  "in %fs (%0.3f KiB/s)", verified, CMD_ARGV[0],
539  duration_elapsed(&bench), duration_kbps(&bench, verified));
540  }
541 
542  image_close(&image);
543 
544  return retval;
545 }
546 
547 COMMAND_HANDLER(handle_flash_fill_command)
548 {
550  uint64_t pattern;
551  uint32_t count;
553  unsigned int i;
554  uint32_t wordsize;
555  int retval;
556 
557  if (CMD_ARGC != 3)
559 
563 
564  struct flash_bank *bank;
565  retval = get_flash_bank_by_addr(target, address, true, &bank);
566  if (retval != ERROR_OK)
567  return retval;
568 
569  switch (CMD_NAME[4]) {
570  case 'd':
571  wordsize = 8;
572  break;
573  case 'w':
574  wordsize = 4;
575  break;
576  case 'h':
577  wordsize = 2;
578  break;
579  case 'b':
580  wordsize = 1;
581  break;
582  default:
584  }
585 
586  if ((wordsize < sizeof(pattern)) && (pattern >> (8 * wordsize) != 0)) {
587  command_print(CMD, "Fill pattern 0x%" PRIx64 " does not fit within %" PRIu32 "-byte word", pattern, wordsize);
588  return ERROR_FAIL;
589  }
590 
591  if (count == 0)
592  return ERROR_OK;
593 
594  if (address + count * wordsize > bank->base + bank->size) {
595  LOG_ERROR("Cannot cross flash bank borders");
596  return ERROR_FAIL;
597  }
598 
599  uint32_t size_bytes = count * wordsize;
601  target_addr_t end_addr = address + size_bytes - 1;
602  target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
603  uint32_t aligned_size = aligned_end + 1 - aligned_start;
604  uint32_t padding_at_start = address - aligned_start;
605  uint32_t padding_at_end = aligned_end - end_addr;
606 
607  uint8_t *buffer = malloc(aligned_size);
608  if (!buffer)
609  return ERROR_FAIL;
610 
611  if (padding_at_start) {
612  memset(buffer, bank->default_padded_value, padding_at_start);
613  LOG_WARNING("Start address " TARGET_ADDR_FMT
614  " breaks the required alignment of flash bank %s",
615  address, bank->name);
616  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
617  padding_at_start, aligned_start);
618  }
619 
620  uint8_t *ptr = buffer + padding_at_start;
621 
622  switch (wordsize) {
623  case 8:
624  for (i = 0; i < count; i++, ptr += wordsize)
626  break;
627  case 4:
628  for (i = 0; i < count; i++, ptr += wordsize)
630  break;
631  case 2:
632  for (i = 0; i < count; i++, ptr += wordsize)
634  break;
635  case 1:
636  memset(ptr, pattern, count);
637  ptr += count;
638  break;
639  default:
640  LOG_ERROR("BUG: can't happen");
641  retval = ERROR_FAIL;
642  goto done;
643  }
644 
645  if (padding_at_end) {
646  memset(ptr, bank->default_padded_value, padding_at_end);
647  LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRIu32
648  " bytes (bank write end alignment)",
649  end_addr + 1, padding_at_end);
650  }
651 
652  struct duration bench;
653  duration_start(&bench);
654 
655  retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
656  if (retval != ERROR_OK)
657  goto done;
658 
659  retval = flash_driver_read(bank, buffer, address - bank->base, size_bytes);
660  if (retval != ERROR_OK)
661  goto done;
662 
663  for (i = 0, ptr = buffer; i < count; i++) {
664  uint64_t readback = 0;
665 
666  switch (wordsize) {
667  case 8:
668  readback = target_buffer_get_u64(target, ptr);
669  break;
670  case 4:
671  readback = target_buffer_get_u32(target, ptr);
672  break;
673  case 2:
674  readback = target_buffer_get_u16(target, ptr);
675  break;
676  case 1:
677  readback = *ptr;
678  break;
679  }
680  if (readback != pattern) {
681  LOG_ERROR(
682  "Verification error address " TARGET_ADDR_FMT
683  ", read back 0x%02" PRIx64 ", expected 0x%02" PRIx64,
684  address + i * wordsize, readback, pattern);
685  retval = ERROR_FAIL;
686  goto done;
687  }
688  ptr += wordsize;
689  }
690 
691  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
692  command_print(CMD, "wrote %" PRIu32 " bytes to " TARGET_ADDR_FMT
693  " in %fs (%0.3f KiB/s)", size_bytes, address,
694  duration_elapsed(&bench), duration_kbps(&bench, size_bytes));
695  }
696 
697 done:
698  free(buffer);
699 
700  return retval;
701 }
702 
703 COMMAND_HANDLER(handle_flash_md_command)
704 {
705  int retval;
706 
707  if (CMD_ARGC < 1 || CMD_ARGC > 2)
709 
712 
713  uint32_t count = 1;
714  if (CMD_ARGC == 2)
716 
717  unsigned int wordsize;
718  switch (CMD_NAME[2]) {
719  case 'w':
720  wordsize = 4;
721  break;
722  case 'h':
723  wordsize = 2;
724  break;
725  case 'b':
726  wordsize = 1;
727  break;
728  default:
730  }
731 
732  if (count == 0)
733  return ERROR_OK;
734 
736  struct flash_bank *bank;
737  retval = get_flash_bank_by_addr(target, address, true, &bank);
738  if (retval != ERROR_OK)
739  return retval;
740 
741  uint32_t offset = address - bank->base;
742  uint32_t sizebytes = count * wordsize;
743  if (offset + sizebytes > bank->size) {
744  command_print(CMD, "Cannot cross flash bank borders");
745  return ERROR_FAIL;
746  }
747 
748  uint8_t *buffer = calloc(count, wordsize);
749  if (!buffer) {
750  command_print(CMD, "No memory for flash read buffer");
751  return ERROR_FAIL;
752  }
753 
754  retval = flash_driver_read(bank, buffer, offset, sizebytes);
755  if (retval == ERROR_OK)
757  buffer, true);
758 
759  free(buffer);
760 
761  return retval;
762 }
763 
764 COMMAND_HANDLER(handle_flash_read_memory_command)
765 {
766  /*
767  * CMD_ARGV[0] = memory address
768  * CMD_ARGV[1] = desired element width in bits
769  * CMD_ARGV[2] = number of elements to read
770  */
771 
772  if (CMD_ARGC != 3)
774 
775  /* Arg 1: Memory address. */
778 
779  /* Arg 2: Bit width of one element. */
780  unsigned int width_bits;
781  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
782 
783  /* Arg 3: Number of elements to read. */
784  unsigned int count;
786 
787  switch (width_bits) {
788  case 8:
789  case 16:
790  case 32:
791  case 64:
792  break;
793  default:
794  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
796  }
797 
798  if (count > 65536) {
799  command_print(CMD, "too large read request, exceeds 64K elements");
801  }
802 
803  const unsigned int width = width_bits / 8;
804  /* -1 is needed to handle cases when (addr + count * width) results in zero
805  * due to overflow.
806  */
807  if ((addr + count * width - 1) < addr) {
808  command_print(CMD, "memory region wraps over address zero");
810  }
811 
813  struct flash_bank *bank;
814  int retval = get_flash_bank_by_addr(target, addr, true, &bank);
815  if (retval != ERROR_OK)
816  return retval;
817 
818  uint32_t offset = addr - bank->base;
819  uint32_t sizebytes = count * width_bits;
820  if (offset + sizebytes > bank->size) {
821  command_print(CMD, "cannot cross flash bank borders");
822  return ERROR_FAIL;
823  }
824 
825  const size_t buffer_size = 4096;
826  uint8_t *buffer = malloc(buffer_size);
827 
828  if (!buffer) {
829  command_print(CMD, "failed to allocate memory");
830  return ERROR_FAIL;
831  }
832 
833  char *separator = "";
834  while (count > 0) {
835  const unsigned int max_chunk_len = buffer_size / width;
836  const size_t chunk_len = MIN(count, max_chunk_len);
837 
838  retval = flash_driver_read(bank, buffer, offset, chunk_len * width);
839 
840  if (retval != ERROR_OK) {
841  LOG_DEBUG("read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
842  addr, width_bits, chunk_len);
843  /*
844  * FIXME: we append the errmsg to the list of value already read.
845  * Add a way to flush and replace old output, but LOG_DEBUG() it
846  */
847  command_print(CMD, "failed to read memory");
848  free(buffer);
849  return retval;
850  }
851 
852  for (size_t i = 0; i < chunk_len ; i++) {
853  uint64_t v = 0;
854 
855  switch (width) {
856  case 8:
858  break;
859  case 4:
861  break;
862  case 2:
864  break;
865  case 1:
866  v = buffer[i];
867  break;
868  }
869 
870  command_print_sameline(CMD, "%s0x%" PRIx64, separator, v);
871  separator = " ";
872  }
873 
874  count -= chunk_len;
875  offset += chunk_len * width;
876  }
877 
878  free(buffer);
879 
880  return ERROR_OK;
881 }
882 
883 COMMAND_HANDLER(handle_flash_write_bank_command)
884 {
885  uint32_t offset;
886  uint8_t *buffer;
887  size_t length;
888  struct fileio *fileio;
889 
890  if (CMD_ARGC < 2 || CMD_ARGC > 3)
892 
893  struct duration bench;
894  duration_start(&bench);
895 
896  struct flash_bank *bank;
897  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
898  if (retval != ERROR_OK)
899  return retval;
900 
901  offset = 0;
902 
903  if (CMD_ARGC > 2)
905 
906  if (offset > bank->size) {
907  LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
908  offset);
910  }
911 
913  return ERROR_FAIL;
914 
915  size_t filesize;
916  retval = fileio_size(fileio, &filesize);
917  if (retval != ERROR_OK) {
919  return retval;
920  }
921 
922  length = MIN(filesize, bank->size - offset);
923 
924  if (!length) {
925  LOG_INFO("Nothing to write to flash bank");
927  return ERROR_OK;
928  }
929 
930  if (length != filesize)
931  LOG_INFO("File content exceeds flash bank size. Only writing the "
932  "first %zu bytes of the file", length);
933 
934  target_addr_t start_addr = bank->base + offset;
935  target_addr_t aligned_start = flash_write_align_start(bank, start_addr);
936  target_addr_t end_addr = start_addr + length - 1;
937  target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
938  uint32_t aligned_size = aligned_end + 1 - aligned_start;
939  uint32_t padding_at_start = start_addr - aligned_start;
940  uint32_t padding_at_end = aligned_end - end_addr;
941 
942  buffer = malloc(aligned_size);
943  if (!buffer) {
945  LOG_ERROR("Out of memory");
946  return ERROR_FAIL;
947  }
948 
949  if (padding_at_start) {
950  memset(buffer, bank->default_padded_value, padding_at_start);
951  LOG_WARNING("Start offset 0x%08" PRIx32
952  " breaks the required alignment of flash bank %s",
953  offset, bank->name);
954  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
955  padding_at_start, aligned_start);
956  }
957 
958  uint8_t *ptr = buffer + padding_at_start;
959  size_t buf_cnt;
960  if (fileio_read(fileio, length, ptr, &buf_cnt) != ERROR_OK) {
961  free(buffer);
963  return ERROR_FAIL;
964  }
965 
966  if (buf_cnt != length) {
967  LOG_ERROR("Short read");
968  free(buffer);
970  return ERROR_FAIL;
971  }
972 
973  ptr += length;
974 
975  if (padding_at_end) {
976  memset(ptr, bank->default_padded_value, padding_at_end);
977  LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRIu32
978  " bytes (bank write end alignment)",
979  end_addr + 1, padding_at_end);
980  }
981 
982  retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
983 
984  free(buffer);
985 
986  if (retval == ERROR_OK && duration_measure(&bench) == ERROR_OK) {
987  command_print(CMD, "wrote %zu bytes from file %s to flash bank %u"
988  " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
989  length, CMD_ARGV[1], bank->bank_number, offset,
990  duration_elapsed(&bench), duration_kbps(&bench, length));
991  }
992 
994 
995  return retval;
996 }
997 
998 COMMAND_HANDLER(handle_flash_read_bank_command)
999 {
1000  uint32_t offset;
1001  uint8_t *buffer;
1002  struct fileio *fileio;
1003  uint32_t length;
1004  size_t written;
1005 
1006  if (CMD_ARGC < 2 || CMD_ARGC > 4)
1008 
1009  struct duration bench;
1010  duration_start(&bench);
1011 
1012  struct flash_bank *p;
1013  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
1014 
1015  if (retval != ERROR_OK)
1016  return retval;
1017 
1018  offset = 0;
1019 
1020  if (CMD_ARGC > 2)
1022 
1023  if (offset > p->size) {
1024  LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
1025  offset);
1027  }
1028 
1029  length = p->size - offset;
1030 
1031  if (CMD_ARGC > 3)
1033 
1034  if (offset + length > p->size) {
1035  LOG_ERROR("Length of %" PRIu32 " bytes with offset 0x%8.8" PRIx32
1036  " is out of range of the flash bank", length, offset);
1038  }
1039 
1040  buffer = malloc(length);
1041  if (!buffer) {
1042  LOG_ERROR("Out of memory");
1043  return ERROR_FAIL;
1044  }
1045 
1046  retval = flash_driver_read(p, buffer, offset, length);
1047  if (retval != ERROR_OK) {
1048  LOG_ERROR("Read error");
1049  free(buffer);
1050  return retval;
1051  }
1052 
1054  if (retval != ERROR_OK) {
1055  LOG_ERROR("Could not open file");
1056  free(buffer);
1057  return retval;
1058  }
1059 
1060  retval = fileio_write(fileio, length, buffer, &written);
1062  free(buffer);
1063  if (retval != ERROR_OK) {
1064  LOG_ERROR("Could not write file");
1065  return ERROR_FAIL;
1066  }
1067 
1068  if (duration_measure(&bench) == ERROR_OK)
1069  command_print(CMD, "wrote %zd bytes to file %s from flash bank %u"
1070  " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
1071  written, CMD_ARGV[1], p->bank_number, offset,
1072  duration_elapsed(&bench), duration_kbps(&bench, written));
1073 
1074  return retval;
1075 }
1076 
1077 
1078 COMMAND_HANDLER(handle_flash_verify_bank_command)
1079 {
1080  uint32_t offset;
1081  uint8_t *buffer_file, *buffer_flash;
1082  struct fileio *fileio;
1083  size_t read_cnt;
1084  size_t filesize;
1085  size_t length;
1086  int differ;
1087 
1088  if (CMD_ARGC < 2 || CMD_ARGC > 3)
1090 
1091  struct duration bench;
1092  duration_start(&bench);
1093 
1094  struct flash_bank *p;
1095  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
1096  if (retval != ERROR_OK)
1097  return retval;
1098 
1099  offset = 0;
1100 
1101  if (CMD_ARGC > 2)
1103 
1104  if (offset > p->size) {
1105  LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
1106  offset);
1108  }
1109 
1111  if (retval != ERROR_OK) {
1112  LOG_ERROR("Could not open file");
1113  return retval;
1114  }
1115 
1116  retval = fileio_size(fileio, &filesize);
1117  if (retval != ERROR_OK) {
1119  return retval;
1120  }
1121 
1122  length = MIN(filesize, p->size - offset);
1123 
1124  if (!length) {
1125  LOG_INFO("Nothing to compare with flash bank");
1127  return ERROR_OK;
1128  }
1129 
1130  if (length != filesize)
1131  LOG_INFO("File content exceeds flash bank size. Only comparing the "
1132  "first %zu bytes of the file", length);
1133 
1134  buffer_file = malloc(length);
1135  if (!buffer_file) {
1136  LOG_ERROR("Out of memory");
1138  return ERROR_FAIL;
1139  }
1140 
1141  retval = fileio_read(fileio, length, buffer_file, &read_cnt);
1143  if (retval != ERROR_OK) {
1144  LOG_ERROR("File read failure");
1145  free(buffer_file);
1146  return retval;
1147  }
1148 
1149  if (read_cnt != length) {
1150  LOG_ERROR("Short read");
1151  free(buffer_file);
1152  return ERROR_FAIL;
1153  }
1154 
1155  buffer_flash = malloc(length);
1156  if (!buffer_flash) {
1157  LOG_ERROR("Out of memory");
1158  free(buffer_file);
1159  return ERROR_FAIL;
1160  }
1161 
1162  retval = flash_driver_read(p, buffer_flash, offset, length);
1163  if (retval != ERROR_OK) {
1164  LOG_ERROR("Flash read error");
1165  free(buffer_flash);
1166  free(buffer_file);
1167  return retval;
1168  }
1169 
1170  if (duration_measure(&bench) == ERROR_OK)
1171  command_print(CMD, "read %zd bytes from file %s and flash bank %u"
1172  " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
1173  length, CMD_ARGV[1], p->bank_number, offset,
1174  duration_elapsed(&bench), duration_kbps(&bench, length));
1175 
1176  differ = memcmp(buffer_file, buffer_flash, length);
1177  command_print(CMD, "contents %s", differ ? "differ" : "match");
1178  if (differ) {
1179  uint32_t t;
1180  int diffs = 0;
1181  for (t = 0; t < length; t++) {
1182  if (buffer_flash[t] == buffer_file[t])
1183  continue;
1184  command_print(CMD, "diff %d address 0x%08" PRIx32 ". Was 0x%02x instead of 0x%02x",
1185  diffs, t + offset, buffer_flash[t], buffer_file[t]);
1186  if (diffs++ >= 127) {
1187  command_print(CMD, "More than 128 errors, the rest are not printed.");
1188  break;
1189  }
1190  keep_alive();
1191  }
1192  }
1193  free(buffer_flash);
1194  free(buffer_file);
1195 
1196  return differ ? ERROR_FAIL : ERROR_OK;
1197 }
1198 
1200 {
1201  struct flash_bank *c;
1202 
1203  /* set all flash to require erasing */
1204  for (c = flash_bank_list(); c; c = c->next) {
1205  for (unsigned int i = 0; i < c->num_sectors; i++)
1206  c->sectors[i].is_erased = 0;
1207  }
1208 }
1209 
1210 COMMAND_HANDLER(handle_flash_padded_value_command)
1211 {
1212  if (CMD_ARGC != 2)
1214 
1215  struct flash_bank *p;
1216  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
1217  if (retval != ERROR_OK)
1218  return retval;
1219 
1221 
1222  command_print(CMD, "Default padded value set to 0x%" PRIx8 " for flash bank %u",
1224 
1225  return retval;
1226 }
1227 
1228 static const struct command_registration flash_exec_command_handlers[] = {
1229  {
1230  .name = "probe",
1231  .handler = handle_flash_probe_command,
1232  .mode = COMMAND_EXEC,
1233  .usage = "bank_id",
1234  .help = "Identify a flash bank.",
1235  },
1236  {
1237  .name = "info",
1238  .handler = handle_flash_info_command,
1239  .mode = COMMAND_EXEC,
1240  .usage = "bank_id ['sectors']",
1241  .help = "Print information about a flash bank.",
1242  },
1243  {
1244  .name = "erase_check",
1245  .handler = handle_flash_erase_check_command,
1246  .mode = COMMAND_EXEC,
1247  .usage = "bank_id",
1248  .help = "Check erase state of all blocks in a "
1249  "flash bank.",
1250  },
1251  {
1252  .name = "erase_sector",
1253  .handler = handle_flash_erase_command,
1254  .mode = COMMAND_EXEC,
1255  .usage = "bank_id first_sector_num (last_sector_num|'last')",
1256  .help = "Erase a range of sectors in a flash bank.",
1257  },
1258  {
1259  .name = "erase_address",
1260  .handler = handle_flash_erase_address_command,
1261  .mode = COMMAND_EXEC,
1262  .usage = "['pad'] ['unlock'] address length",
1263  .help = "Erase flash sectors starting at address and "
1264  "continuing for length bytes. If 'pad' is specified, "
1265  "data outside that range may also be erased: the start "
1266  "address may be decreased, and length increased, so "
1267  "that all of the first and last sectors are erased. "
1268  "If 'unlock' is specified, then the flash is unprotected "
1269  "before erasing.",
1270 
1271  },
1272  {
1273  .name = "filld",
1274  .handler = handle_flash_fill_command,
1275  .mode = COMMAND_EXEC,
1276  .usage = "address value n",
1277  .help = "Fill n double-words with 64-bit value, starting at "
1278  "word address. (No autoerase.)",
1279  },
1280  {
1281  .name = "fillw",
1282  .handler = handle_flash_fill_command,
1283  .mode = COMMAND_EXEC,
1284  .usage = "address value n",
1285  .help = "Fill n words with 32-bit value, starting at "
1286  "word address. (No autoerase.)",
1287  },
1288  {
1289  .name = "fillh",
1290  .handler = handle_flash_fill_command,
1291  .mode = COMMAND_EXEC,
1292  .usage = "address value n",
1293  .help = "Fill n halfwords with 16-bit value, starting at "
1294  "word address. (No autoerase.)",
1295  },
1296  {
1297  .name = "fillb",
1298  .handler = handle_flash_fill_command,
1299  .mode = COMMAND_EXEC,
1300  .usage = "address value n",
1301  .help = "Fill n bytes with 8-bit value, starting at "
1302  "word address. (No autoerase.)",
1303  },
1304  {
1305  .name = "mdb",
1306  .handler = handle_flash_md_command,
1307  .mode = COMMAND_EXEC,
1308  .usage = "address [count]",
1309  .help = "Display bytes from flash.",
1310  },
1311  {
1312  .name = "mdh",
1313  .handler = handle_flash_md_command,
1314  .mode = COMMAND_EXEC,
1315  .usage = "address [count]",
1316  .help = "Display half-words from flash.",
1317  },
1318  {
1319  .name = "mdw",
1320  .handler = handle_flash_md_command,
1321  .mode = COMMAND_EXEC,
1322  .usage = "address [count]",
1323  .help = "Display words from flash.",
1324  },
1325  {
1326  .name = "read_memory",
1327  .mode = COMMAND_EXEC,
1328  .handler = handle_flash_read_memory_command,
1329  .help = "Read Tcl list of 8/16/32/64 bit numbers from flash memory",
1330  .usage = "address width count",
1331  },
1332  {
1333  .name = "write_bank",
1334  .handler = handle_flash_write_bank_command,
1335  .mode = COMMAND_EXEC,
1336  .usage = "bank_id filename [offset]",
1337  .help = "Write binary data from file to flash bank. Allow optional "
1338  "offset from beginning of the bank (defaults to zero).",
1339  },
1340  {
1341  .name = "write_image",
1342  .handler = handle_flash_write_image_command,
1343  .mode = COMMAND_EXEC,
1344  .usage = "[erase] [unlock] filename [offset [file_type]]",
1345  .help = "Write an image to flash. Optionally first unprotect "
1346  "and/or erase the region to be used. Allow optional "
1347  "offset from beginning of bank (defaults to zero)",
1348  },
1349  {
1350  .name = "verify_image",
1351  .handler = handle_flash_verify_image_command,
1352  .mode = COMMAND_EXEC,
1353  .usage = "filename [offset [file_type]]",
1354  .help = "Verify an image against flash. Allow optional "
1355  "offset from beginning of bank (defaults to zero)",
1356  },
1357  {
1358  .name = "read_bank",
1359  .handler = handle_flash_read_bank_command,
1360  .mode = COMMAND_EXEC,
1361  .usage = "bank_id filename [offset [length]]",
1362  .help = "Read binary data from flash bank to file. Allow optional "
1363  "offset from beginning of the bank (defaults to zero).",
1364  },
1365  {
1366  .name = "verify_bank",
1367  .handler = handle_flash_verify_bank_command,
1368  .mode = COMMAND_EXEC,
1369  .usage = "bank_id filename [offset]",
1370  .help = "Compare the contents of a file with the contents of the "
1371  "flash bank. Allow optional offset from beginning of the bank "
1372  "(defaults to zero).",
1373  },
1374  {
1375  .name = "protect",
1376  .handler = handle_flash_protect_command,
1377  .mode = COMMAND_EXEC,
1378  .usage = "bank_id first_block [last_block|'last'] "
1379  "('on'|'off')",
1380  .help = "Turn protection on or off for a range of protection "
1381  "blocks or sectors in a given flash bank. "
1382  "See 'flash info' output for a list of blocks.",
1383  },
1384  {
1385  .name = "padded_value",
1386  .handler = handle_flash_padded_value_command,
1387  .mode = COMMAND_EXEC,
1388  .usage = "bank_id value",
1389  .help = "Set default flash padded value",
1390  },
1392 };
1393 
1394 static int flash_init_drivers(struct command_context *cmd_ctx)
1395 {
1396  if (!flash_bank_list())
1397  return ERROR_OK;
1398 
1399  return register_commands(cmd_ctx, "flash", flash_exec_command_handlers);
1400 }
1401 
1402 COMMAND_HANDLER(handle_flash_bank_command)
1403 {
1404  if (CMD_ARGC < 7) {
1405  LOG_ERROR("usage: flash bank <name> <driver> "
1406  "<base> <size> <chip_width> <bus_width> <target>");
1408  }
1409  /* save bank name and advance arguments for compatibility */
1410  const char *bank_name = *CMD_ARGV++;
1411  CMD_ARGC--;
1412 
1413  struct target *target = get_target(CMD_ARGV[5]);
1414  if (!target) {
1415  LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
1416  return ERROR_FAIL;
1417  }
1418 
1419  const char *driver_name = CMD_ARGV[0];
1420  const struct flash_driver *driver = flash_driver_find_by_name(driver_name);
1421  if (!driver) {
1422  /* no matching flash driver found */
1423  LOG_ERROR("flash driver '%s' not found", driver_name);
1424  return ERROR_FAIL;
1425  }
1426 
1427  /* check the flash bank name is unique */
1428  if (get_flash_bank_by_name_noprobe(bank_name)) {
1429  /* flash bank name already exists */
1430  LOG_ERROR("flash bank name '%s' already exists", bank_name);
1431  return ERROR_FAIL;
1432  }
1433 
1434  /* register flash specific commands */
1435  if (driver->commands) {
1436  int retval = register_commands(CMD_CTX, NULL, driver->commands);
1437  if (retval != ERROR_OK) {
1438  LOG_ERROR("couldn't register '%s' commands",
1439  driver_name);
1440  return ERROR_FAIL;
1441  }
1442  }
1443 
1444  struct flash_bank *c = calloc(1, sizeof(*c));
1445  c->name = strdup(bank_name);
1446  c->target = target;
1447  c->driver = driver;
1448  COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[1], c->base);
1449  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
1451  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[4], c->bus_width);
1452  c->default_padded_value = c->erased_value = 0xff;
1454 
1455  int retval;
1456  retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
1457  if (retval != ERROR_OK) {
1458  LOG_ERROR("'%s' driver rejected flash bank at " TARGET_ADDR_FMT
1459  "; usage: %s", driver_name, c->base, driver->usage);
1460  free(c->name);
1461  free(c);
1462  return retval;
1463  }
1464 
1465  if (!driver->usage)
1466  LOG_DEBUG("'%s' driver usage field missing", driver_name);
1467 
1468  flash_bank_add(c);
1469 
1470  return ERROR_OK;
1471 }
1472 
1473 COMMAND_HANDLER(handle_flash_banks_command)
1474 {
1475  if (CMD_ARGC != 0)
1477 
1478  for (struct flash_bank *p = flash_bank_list(); p; p = p->next) {
1479  command_print(CMD, "#%d : %s (%s) at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32 ", "
1480  "buswidth %u, chipwidth %u", p->bank_number,
1481  p->name, p->driver->name, p->base, p->size,
1482  p->bus_width, p->chip_width);
1483  }
1484  return ERROR_OK;
1485 }
1486 
1487 COMMAND_HANDLER(handle_flash_list)
1488 {
1489  if (CMD_ARGC != 0)
1491 
1492  for (struct flash_bank *p = flash_bank_list(); p; p = p->next) {
1494  "{\n"
1495  " name {%s}\n"
1496  " driver %s\n"
1497  " base " TARGET_ADDR_FMT "\n"
1498  " size 0x%" PRIx32 "\n"
1499  " bus_width %u\n"
1500  " chip_width %u\n"
1501  " target %s\n"
1502  "}",
1503  p->name, p->driver->name, p->base, p->size, p->bus_width, p->chip_width,
1504  target_name(p->target));
1505  }
1506 
1507  return ERROR_OK;
1508 }
1509 
1510 COMMAND_HANDLER(handle_flash_init_command)
1511 {
1512  if (CMD_ARGC != 0)
1514 
1515  static bool flash_initialized;
1516  if (flash_initialized) {
1517  LOG_INFO("'flash init' has already been called");
1518  return ERROR_OK;
1519  }
1520  flash_initialized = true;
1521 
1522  LOG_DEBUG("Initializing flash devices...");
1523  return flash_init_drivers(CMD_CTX);
1524 }
1525 
1526 static const struct command_registration flash_config_command_handlers[] = {
1527  {
1528  .name = "bank",
1529  .handler = handle_flash_bank_command,
1530  .mode = COMMAND_CONFIG,
1531  .usage = "bank_id driver_name base_address size_bytes "
1532  "chip_width_bytes bus_width_bytes target "
1533  "[driver_options ...]",
1534  .help = "Define a new bank with the given name, "
1535  "using the specified NOR flash driver.",
1536  },
1537  {
1538  .name = "init",
1539  .mode = COMMAND_CONFIG,
1540  .handler = handle_flash_init_command,
1541  .help = "Initialize flash devices.",
1542  .usage = "",
1543  },
1544  {
1545  .name = "banks",
1546  .mode = COMMAND_ANY,
1547  .handler = handle_flash_banks_command,
1548  .help = "Display table with information about flash banks.",
1549  .usage = "",
1550  },
1551  {
1552  .name = "list",
1553  .mode = COMMAND_ANY,
1554  .handler = handle_flash_list,
1555  .help = "Returns a list of details about the flash banks.",
1556  .usage = "",
1557  },
1559 };
1560 static const struct command_registration flash_command_handlers[] = {
1561  {
1562  .name = "flash",
1563  .mode = COMMAND_ANY,
1564  .help = "NOR flash command group",
1566  .usage = "",
1567  },
1569 };
1570 
1572 {
1573  return register_commands(cmd_ctx, NULL, flash_command_handlers);
1574 }
const char * name
Definition: am13e230x.c:167
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:171
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ADDRESS(in, out)
Definition: command.h:455
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint8_t pattern
Fill pattern.
Definition: dw-spi-helper.h:8
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
unsigned short width
Definition: embeddedice.c:47
uint8_t bank
Definition: esirisc.c:135
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FLASH_OPER_UNSUPPORTED
Definition: flash/common.h:36
target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
Get aligned start address of a flash write region.
int flash_driver_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
struct flash_bank * get_flash_bank_by_name_noprobe(const char *name)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
Get aligned end address of a flash write region.
int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
int flash_driver_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
void flash_bank_add(struct flash_bank *bank)
Adds a new NOR bank to the global list of banks.
int flash_unlock_address_range(struct target *target, target_addr_t addr, uint32_t length)
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
int get_flash_bank_by_addr(struct target *target, target_addr_t addr, bool check, struct flash_bank **result_bank)
Returns the flash bank located at a specified address.
int flash_driver_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
struct flash_bank * flash_bank_list(void)
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length, bool skip_banks_without_erase)
Erases length bytes in the target flash, starting at addr.
int flash_write_unlock_verify(struct target *target, struct image *image, uint32_t *written, bool erase, bool unlock, bool write, bool verify)
static const struct command_registration flash_config_command_handlers[]
static const struct command_registration flash_command_handlers[]
void flash_set_dirty(void)
Forces targets to re-examine their erase/protection state.
COMMAND_HELPER(flash_command_get_bank_probe_optional, unsigned int name_index, struct flash_bank **bank, bool do_probe)
Retrieves bank from a command argument, reporting errors parsing the bank identifier or retrieving th...
Definition: flash/nor/tcl.c:22
static const struct command_registration flash_exec_command_handlers[]
static int flash_init_drivers(struct command_context *cmd_ctx)
COMMAND_HANDLER(handle_flash_info_command)
Definition: flash/nor/tcl.c:58
int flash_register_commands(struct command_context *cmd_ctx)
Registers the 'flash' subsystem commands.
int fileio_write(struct fileio *fileio, size_t size, const void *buffer, size_t *size_written)
int fileio_read(struct fileio *fileio, size_t size, void *buffer, size_t *size_read)
int fileio_close(struct fileio *fileio)
int fileio_size(struct fileio *fileio, size_t *size)
FIX!!!!
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
@ FILEIO_WRITE
Definition: helper/fileio.h:29
@ FILEIO_READ
Definition: helper/fileio.h:28
@ FILEIO_BINARY
Definition: helper/fileio.h:23
void image_close(struct image *image)
Definition: image.c:1210
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:956
void keep_alive(void)
Definition: log.c:437
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define FLASH_WRITE_GAP_SECTOR
Definition: nor/core.h:63
const struct flash_driver * flash_driver_find_by_name(const char *name)
Find a NOR flash driver by its name.
Definition: drivers.c:105
#define MIN(a, b)
Definition: replacements.h:22
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
unsigned int num_prot_blocks
The number of protection blocks in this bank.
Definition: nor/core.h:137
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
Definition: nor/core.h:118
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
Definition: nor/core.h:97
const struct flash_driver * driver
Driver for this bank.
Definition: nor/core.h:80
unsigned int chip_width
Width of the chip in bytes (1,2,4 bytes)
Definition: nor/core.h:89
target_addr_t base
The base address of this bank.
Definition: nor/core.h:84
uint32_t size
The size of this chip bank, in bytes.
Definition: nor/core.h:85
unsigned int num_sectors
The number of sectors on this chip.
Definition: nor/core.h:116
struct flash_sector * prot_blocks
Array of protection blocks, allocated and initialized by the flash driver.
Definition: nor/core.h:139
unsigned int bus_width
Maximum bus width, in bytes (1,2,4 bytes)
Definition: nor/core.h:90
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
Definition: nor/core.h:129
struct flash_bank * next
The next flash bank on this chip.
Definition: nor/core.h:141
struct target * target
Target to which this bank belongs.
Definition: nor/core.h:78
uint32_t minimal_write_gap
Minimal gap between sections to discontinue flash write Default FLASH_WRITE_GAP_SECTOR splits the wri...
Definition: nor/core.h:109
char * name
Definition: nor/core.h:76
uint8_t erased_value
Erased value.
Definition: nor/core.h:93
unsigned int bank_number
The 'bank' (or chip number) of this instance.
Definition: nor/core.h:83
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const struct command_registration * commands
An array of driver-specific commands to register.
Definition: nor/driver.h:56
int(* auto_probe)(struct flash_bank *bank)
A more gentle flavor of flash_driver::probe, performing setup with less noise.
Definition: nor/driver.h:231
const char * usage
Gives a human-readable description of arguments.
Definition: nor/driver.h:49
int(* erase_check)(struct flash_bank *bank)
Check the erasure status of a flash bank.
Definition: nor/driver.h:191
int(* info)(struct flash_bank *bank, struct command_invocation *cmd)
Display human-readable information about the flash bank.
Definition: nor/driver.h:216
int(* probe)(struct flash_bank *bank)
Probe to determine what kind of flash is present.
Definition: nor/driver.h:177
int(* protect_check)(struct flash_bank *bank)
Determine if the specific bank is "protected" or not.
Definition: nor/driver.h:205
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
Definition: nor/core.h:42
uint32_t offset
Bus offset from start of the flash chip (in bytes).
Definition: nor/core.h:30
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
Definition: nor/core.h:55
uint32_t size
Number of bytes in this flash sector.
Definition: nor/core.h:32
Definition: image.h:48
bool start_address_set
Definition: image.h:55
long long base_address
Definition: image.h:54
bool base_address_set
Definition: image.h:53
Definition: target.h:119
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
Definition: target.c:318
struct target * get_target(const char *id)
Definition: target.c:444
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:381
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:363
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
Definition: target.c:354
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:345
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:469
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:327
void target_handle_md_output(struct command_invocation *cmd, struct target *target, target_addr_t address, unsigned int size, unsigned int count, const uint8_t *buffer, bool include_address)
Definition: target.c:3368
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:247
float duration_elapsed(const struct duration *duration)
Definition: time_support.c:46
int duration_measure(struct duration *duration)
Update the duration->elapsed field to finish the duration measurement.
Definition: time_support.c:34
int duration_start(struct duration *duration)
Update the duration->start field to start the duration measurement.
Definition: time_support.c:22
float duration_kbps(const struct duration *duration, size_t count)
Definition: time_support.c:51
#define TARGET_ADDR_FMT
Definition: types.h:286
uint64_t target_addr_t
Definition: types.h:279
#define NULL
Definition: usb.h:16
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22