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  retval = p->driver->erase_check(p);
191  if (retval == ERROR_OK)
192  command_print(CMD, "successfully checked erase state");
193  else {
195  "unknown error when checking erase state of flash bank #%s at "
197  CMD_ARGV[0],
198  p->base);
199  }
200 
201  for (unsigned int j = 0; j < p->num_sectors; j++) {
202  char *erase_state;
203 
204  if (p->sectors[j].is_erased == 0)
205  erase_state = "not erased";
206  else if (p->sectors[j].is_erased == 1)
207  continue;
208  else
209  erase_state = "erase state unknown";
210 
211  blank = false;
213  "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIu32 "kB) %s",
214  j,
215  p->sectors[j].offset,
216  p->sectors[j].size,
217  p->sectors[j].size >> 10,
218  erase_state);
219  }
220 
221  if (blank)
222  command_print(CMD, "\tBank is erased");
223  return retval;
224 }
225 
226 COMMAND_HANDLER(handle_flash_erase_address_command)
227 {
228  struct flash_bank *p;
229  int retval = ERROR_OK;
231  uint32_t length;
232  bool do_pad = false;
233  bool do_unlock = false;
235 
236  while (CMD_ARGC >= 3) {
237  /* Optionally pad out the address range to block/sector
238  * boundaries. We can't know if there's data in that part
239  * of the flash; only do padding if we're told to.
240  */
241  if (strcmp("pad", CMD_ARGV[0]) == 0)
242  do_pad = true;
243  else if (strcmp("unlock", CMD_ARGV[0]) == 0)
244  do_unlock = true;
245  else
247  CMD_ARGC--;
248  CMD_ARGV++;
249  }
250  if (CMD_ARGC != 2)
252 
255 
256  if (length <= 0) {
257  command_print(CMD, "Length must be >0");
259  }
260 
261  retval = get_flash_bank_by_addr(target, address, true, &p);
262  if (retval != ERROR_OK)
263  return retval;
264 
265  /* We can't know if we did a resume + halt, in which case we no longer know the erased state
266  **/
267  flash_set_dirty();
268 
269  struct duration bench;
270  duration_start(&bench);
271 
272  if (do_unlock)
274 
275  if (retval == ERROR_OK)
276  retval = flash_erase_address_range(target, do_pad, address, length);
277 
278  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
279  command_print(CMD, "erased address " TARGET_ADDR_FMT " (length %" PRIu32 ")"
280  " in %fs (%0.3f KiB/s)", address, length,
281  duration_elapsed(&bench), duration_kbps(&bench, length));
282  }
283 
284  return retval;
285 }
286 
287 COMMAND_HANDLER(handle_flash_erase_command)
288 {
289  if (CMD_ARGC != 3)
291 
292  uint32_t first;
293  uint32_t last;
294 
295  struct flash_bank *p;
296  int retval;
297 
298  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
299  if (retval != ERROR_OK)
300  return retval;
301 
302  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
303  if (strcmp(CMD_ARGV[2], "last") == 0)
304  last = p->num_sectors - 1;
305  else
306  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
307 
308  if (!(first <= last)) {
309  command_print(CMD, "ERROR: "
310  "first sector must be <= last");
311  return ERROR_FAIL;
312  }
313 
314  if (!(last <= (p->num_sectors - 1))) {
315  command_print(CMD, "ERROR: "
316  "last sector must be <= %u",
317  p->num_sectors - 1);
318  return ERROR_FAIL;
319  }
320 
321  struct duration bench;
322  duration_start(&bench);
323 
324  retval = flash_driver_erase(p, first, last);
325 
326  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
327  command_print(CMD, "erased sectors %" PRIu32 " "
328  "through %" PRIu32 " on flash bank %u "
329  "in %fs", first, last, p->bank_number, duration_elapsed(&bench));
330  }
331 
332  return retval;
333 }
334 
335 COMMAND_HANDLER(handle_flash_protect_command)
336 {
337  if (CMD_ARGC != 4)
339 
340  uint32_t first;
341  uint32_t last;
342 
343  struct flash_bank *p;
344  int retval;
345  int num_blocks;
346 
347  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
348  if (retval != ERROR_OK)
349  return retval;
350 
351  if (p->num_prot_blocks)
352  num_blocks = p->num_prot_blocks;
353  else
354  num_blocks = p->num_sectors;
355 
356  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
357  if (strcmp(CMD_ARGV[2], "last") == 0)
358  last = num_blocks - 1;
359  else
360  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
361 
362  bool set;
364 
365  if (!(first <= last)) {
366  command_print(CMD, "ERROR: "
367  "first %s must be <= last",
368  (p->num_prot_blocks) ? "block" : "sector");
369  return ERROR_FAIL;
370  }
371 
372  if (!(last <= (uint32_t)(num_blocks - 1))) {
373  command_print(CMD, "ERROR: "
374  "last %s must be <= %d",
375  (p->num_prot_blocks) ? "block" : "sector",
376  num_blocks - 1);
377  return ERROR_FAIL;
378  }
379 
380  retval = flash_driver_protect(p, set, first, last);
381  if (retval == ERROR_OK) {
382  command_print(CMD, "%s protection for %s %" PRIu32
383  " through %" PRIu32 " on flash bank %d",
384  (set) ? "set" : "cleared",
385  (p->num_prot_blocks) ? "blocks" : "sectors",
386  first, last, p->bank_number);
387  }
388 
389  return retval;
390 }
391 
392 COMMAND_HANDLER(handle_flash_write_image_command)
393 {
395 
396  struct image image;
397  uint32_t written;
398 
399  int retval;
400 
401  /* flash auto-erase is disabled by default*/
402  int auto_erase = 0;
403  bool auto_unlock = false;
404 
405  while (CMD_ARGC) {
406  if (strcmp(CMD_ARGV[0], "erase") == 0) {
407  auto_erase = 1;
408  CMD_ARGV++;
409  CMD_ARGC--;
410  command_print(CMD, "auto erase enabled");
411  } else if (strcmp(CMD_ARGV[0], "unlock") == 0) {
412  auto_unlock = true;
413  CMD_ARGV++;
414  CMD_ARGC--;
415  command_print(CMD, "auto unlock enabled");
416  } else
417  break;
418  }
419 
420  if (CMD_ARGC < 1)
422 
423  if (!target) {
424  LOG_ERROR("no target selected");
425  return ERROR_FAIL;
426  }
427 
428  struct duration bench;
429  duration_start(&bench);
430 
431  if (CMD_ARGC >= 2) {
432  image.base_address_set = true;
434  } else {
435  image.base_address_set = false;
436  image.base_address = 0x0;
437  }
438 
439  image.start_address_set = false;
440 
441  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
442  if (retval != ERROR_OK)
443  return retval;
444 
445  retval = flash_write_unlock_verify(target, &image, &written, auto_erase,
446  auto_unlock, true, false);
447  if (retval != ERROR_OK) {
448  image_close(&image);
449  return retval;
450  }
451 
452  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
453  command_print(CMD, "wrote %" PRIu32 " bytes from file %s "
454  "in %fs (%0.3f KiB/s)", written, CMD_ARGV[0],
455  duration_elapsed(&bench), duration_kbps(&bench, written));
456  }
457 
458  image_close(&image);
459 
460  return retval;
461 }
462 
463 COMMAND_HANDLER(handle_flash_verify_image_command)
464 {
466 
467  struct image image;
468  uint32_t verified;
469 
470  int retval;
471 
472  if (CMD_ARGC < 1)
474 
475  if (!target) {
476  LOG_ERROR("no target selected");
477  return ERROR_FAIL;
478  }
479 
480  struct duration bench;
481  duration_start(&bench);
482 
483  if (CMD_ARGC >= 2) {
486  } else {
488  image.base_address = 0x0;
489  }
490 
492 
493  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
494  if (retval != ERROR_OK)
495  return retval;
496 
497  retval = flash_write_unlock_verify(target, &image, &verified, false,
498  false, false, true);
499  if (retval != ERROR_OK) {
500  image_close(&image);
501  return retval;
502  }
503 
504  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
505  command_print(CMD, "verified %" PRIu32 " bytes from file %s "
506  "in %fs (%0.3f KiB/s)", verified, CMD_ARGV[0],
507  duration_elapsed(&bench), duration_kbps(&bench, verified));
508  }
509 
510  image_close(&image);
511 
512  return retval;
513 }
514 
515 COMMAND_HANDLER(handle_flash_fill_command)
516 {
518  uint64_t pattern;
519  uint32_t count;
521  unsigned int i;
522  uint32_t wordsize;
523  int retval;
524 
525  if (CMD_ARGC != 3)
527 
531 
532  struct flash_bank *bank;
533  retval = get_flash_bank_by_addr(target, address, true, &bank);
534  if (retval != ERROR_OK)
535  return retval;
536 
537  switch (CMD_NAME[4]) {
538  case 'd':
539  wordsize = 8;
540  break;
541  case 'w':
542  wordsize = 4;
543  break;
544  case 'h':
545  wordsize = 2;
546  break;
547  case 'b':
548  wordsize = 1;
549  break;
550  default:
552  }
553 
554  if ((wordsize < sizeof(pattern)) && (pattern >> (8 * wordsize) != 0)) {
555  command_print(CMD, "Fill pattern 0x%" PRIx64 " does not fit within %" PRIu32 "-byte word", pattern, wordsize);
556  return ERROR_FAIL;
557  }
558 
559  if (count == 0)
560  return ERROR_OK;
561 
562  if (address + count * wordsize > bank->base + bank->size) {
563  LOG_ERROR("Cannot cross flash bank borders");
564  return ERROR_FAIL;
565  }
566 
567  uint32_t size_bytes = count * wordsize;
569  target_addr_t end_addr = address + size_bytes - 1;
570  target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
571  uint32_t aligned_size = aligned_end + 1 - aligned_start;
572  uint32_t padding_at_start = address - aligned_start;
573  uint32_t padding_at_end = aligned_end - end_addr;
574 
575  uint8_t *buffer = malloc(aligned_size);
576  if (!buffer)
577  return ERROR_FAIL;
578 
579  if (padding_at_start) {
580  memset(buffer, bank->default_padded_value, padding_at_start);
581  LOG_WARNING("Start address " TARGET_ADDR_FMT
582  " breaks the required alignment of flash bank %s",
583  address, bank->name);
584  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
585  padding_at_start, aligned_start);
586  }
587 
588  uint8_t *ptr = buffer + padding_at_start;
589 
590  switch (wordsize) {
591  case 8:
592  for (i = 0; i < count; i++, ptr += wordsize)
594  break;
595  case 4:
596  for (i = 0; i < count; i++, ptr += wordsize)
598  break;
599  case 2:
600  for (i = 0; i < count; i++, ptr += wordsize)
602  break;
603  case 1:
604  memset(ptr, pattern, count);
605  ptr += count;
606  break;
607  default:
608  LOG_ERROR("BUG: can't happen");
609  exit(-1);
610  }
611 
612  if (padding_at_end) {
613  memset(ptr, bank->default_padded_value, padding_at_end);
614  LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRIu32
615  " bytes (bank write end alignment)",
616  end_addr + 1, padding_at_end);
617  }
618 
619  struct duration bench;
620  duration_start(&bench);
621 
622  retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
623  if (retval != ERROR_OK)
624  goto done;
625 
626  retval = flash_driver_read(bank, buffer, address - bank->base, size_bytes);
627  if (retval != ERROR_OK)
628  goto done;
629 
630  for (i = 0, ptr = buffer; i < count; i++) {
631  uint64_t readback = 0;
632 
633  switch (wordsize) {
634  case 8:
635  readback = target_buffer_get_u64(target, ptr);
636  break;
637  case 4:
638  readback = target_buffer_get_u32(target, ptr);
639  break;
640  case 2:
641  readback = target_buffer_get_u16(target, ptr);
642  break;
643  case 1:
644  readback = *ptr;
645  break;
646  }
647  if (readback != pattern) {
648  LOG_ERROR(
649  "Verification error address " TARGET_ADDR_FMT
650  ", read back 0x%02" PRIx64 ", expected 0x%02" PRIx64,
651  address + i * wordsize, readback, pattern);
652  retval = ERROR_FAIL;
653  goto done;
654  }
655  ptr += wordsize;
656  }
657 
658  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
659  command_print(CMD, "wrote %" PRIu32 " bytes to " TARGET_ADDR_FMT
660  " in %fs (%0.3f KiB/s)", size_bytes, address,
661  duration_elapsed(&bench), duration_kbps(&bench, size_bytes));
662  }
663 
664 done:
665  free(buffer);
666 
667  return retval;
668 }
669 
670 COMMAND_HANDLER(handle_flash_md_command)
671 {
672  int retval;
673 
674  if (CMD_ARGC < 1 || CMD_ARGC > 2)
676 
679 
680  uint32_t count = 1;
681  if (CMD_ARGC == 2)
683 
684  unsigned int wordsize;
685  switch (CMD_NAME[2]) {
686  case 'w':
687  wordsize = 4;
688  break;
689  case 'h':
690  wordsize = 2;
691  break;
692  case 'b':
693  wordsize = 1;
694  break;
695  default:
697  }
698 
699  if (count == 0)
700  return ERROR_OK;
701 
703  struct flash_bank *bank;
704  retval = get_flash_bank_by_addr(target, address, true, &bank);
705  if (retval != ERROR_OK)
706  return retval;
707 
708  uint32_t offset = address - bank->base;
709  uint32_t sizebytes = count * wordsize;
710  if (offset + sizebytes > bank->size) {
711  command_print(CMD, "Cannot cross flash bank borders");
712  return ERROR_FAIL;
713  }
714 
715  uint8_t *buffer = calloc(count, wordsize);
716  if (!buffer) {
717  command_print(CMD, "No memory for flash read buffer");
718  return ERROR_FAIL;
719  }
720 
721  retval = flash_driver_read(bank, buffer, offset, sizebytes);
722  if (retval == ERROR_OK)
724 
725  free(buffer);
726 
727  return retval;
728 }
729 
730 COMMAND_HANDLER(handle_flash_read_memory_command)
731 {
732  /*
733  * CMD_ARGV[0] = memory address
734  * CMD_ARGV[1] = desired element width in bits
735  * CMD_ARGV[2] = number of elements to read
736  */
737 
738  if (CMD_ARGC != 3)
740 
741  /* Arg 1: Memory address. */
744 
745  /* Arg 2: Bit width of one element. */
746  unsigned int width_bits;
747  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
748 
749  /* Arg 3: Number of elements to read. */
750  unsigned int count;
752 
753  switch (width_bits) {
754  case 8:
755  case 16:
756  case 32:
757  case 64:
758  break;
759  default:
760  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
762  }
763 
764  if (count > 65536) {
765  command_print(CMD, "too large read request, exceeds 64K elements");
767  }
768 
769  const unsigned int width = width_bits / 8;
770  /* -1 is needed to handle cases when (addr + count * width) results in zero
771  * due to overflow.
772  */
773  if ((addr + count * width - 1) < addr) {
774  command_print(CMD, "memory region wraps over address zero");
776  }
777 
779  struct flash_bank *bank;
780  int retval = get_flash_bank_by_addr(target, addr, true, &bank);
781  if (retval != ERROR_OK)
782  return retval;
783 
784  uint32_t offset = addr - bank->base;
785  uint32_t sizebytes = count * width_bits;
786  if (offset + sizebytes > bank->size) {
787  command_print(CMD, "cannot cross flash bank borders");
788  return ERROR_FAIL;
789  }
790 
791  const size_t buffer_size = 4096;
792  uint8_t *buffer = malloc(buffer_size);
793 
794  if (!buffer) {
795  command_print(CMD, "failed to allocate memory");
796  return ERROR_FAIL;
797  }
798 
799  char *separator = "";
800  while (count > 0) {
801  const unsigned int max_chunk_len = buffer_size / width;
802  const size_t chunk_len = MIN(count, max_chunk_len);
803 
804  retval = flash_driver_read(bank, buffer, offset, chunk_len * width);
805 
806  if (retval != ERROR_OK) {
807  LOG_DEBUG("read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
808  addr, width_bits, chunk_len);
809  /*
810  * FIXME: we append the errmsg to the list of value already read.
811  * Add a way to flush and replace old output, but LOG_DEBUG() it
812  */
813  command_print(CMD, "failed to read memory");
814  free(buffer);
815  return retval;
816  }
817 
818  for (size_t i = 0; i < chunk_len ; i++) {
819  uint64_t v = 0;
820 
821  switch (width) {
822  case 8:
824  break;
825  case 4:
827  break;
828  case 2:
830  break;
831  case 1:
832  v = buffer[i];
833  break;
834  }
835 
836  command_print_sameline(CMD, "%s0x%" PRIx64, separator, v);
837  separator = " ";
838  }
839 
840  count -= chunk_len;
841  offset += chunk_len * width;
842  }
843 
844  free(buffer);
845 
846  return ERROR_OK;
847 }
848 
849 COMMAND_HANDLER(handle_flash_write_bank_command)
850 {
851  uint32_t offset;
852  uint8_t *buffer;
853  size_t length;
854  struct fileio *fileio;
855 
856  if (CMD_ARGC < 2 || CMD_ARGC > 3)
858 
859  struct duration bench;
860  duration_start(&bench);
861 
862  struct flash_bank *bank;
863  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
864  if (retval != ERROR_OK)
865  return retval;
866 
867  offset = 0;
868 
869  if (CMD_ARGC > 2)
871 
872  if (offset > bank->size) {
873  LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
874  offset);
876  }
877 
879  return ERROR_FAIL;
880 
881  size_t filesize;
882  retval = fileio_size(fileio, &filesize);
883  if (retval != ERROR_OK) {
885  return retval;
886  }
887 
888  length = MIN(filesize, bank->size - offset);
889 
890  if (!length) {
891  LOG_INFO("Nothing to write to flash bank");
893  return ERROR_OK;
894  }
895 
896  if (length != filesize)
897  LOG_INFO("File content exceeds flash bank size. Only writing the "
898  "first %zu bytes of the file", length);
899 
900  target_addr_t start_addr = bank->base + offset;
901  target_addr_t aligned_start = flash_write_align_start(bank, start_addr);
902  target_addr_t end_addr = start_addr + length - 1;
903  target_addr_t aligned_end = flash_write_align_end(bank, end_addr);
904  uint32_t aligned_size = aligned_end + 1 - aligned_start;
905  uint32_t padding_at_start = start_addr - aligned_start;
906  uint32_t padding_at_end = aligned_end - end_addr;
907 
908  buffer = malloc(aligned_size);
909  if (!buffer) {
911  LOG_ERROR("Out of memory");
912  return ERROR_FAIL;
913  }
914 
915  if (padding_at_start) {
916  memset(buffer, bank->default_padded_value, padding_at_start);
917  LOG_WARNING("Start offset 0x%08" PRIx32
918  " breaks the required alignment of flash bank %s",
919  offset, bank->name);
920  LOG_WARNING("Padding %" PRIu32 " bytes from " TARGET_ADDR_FMT,
921  padding_at_start, aligned_start);
922  }
923 
924  uint8_t *ptr = buffer + padding_at_start;
925  size_t buf_cnt;
926  if (fileio_read(fileio, length, ptr, &buf_cnt) != ERROR_OK) {
927  free(buffer);
929  return ERROR_FAIL;
930  }
931 
932  if (buf_cnt != length) {
933  LOG_ERROR("Short read");
934  free(buffer);
936  return ERROR_FAIL;
937  }
938 
939  ptr += length;
940 
941  if (padding_at_end) {
942  memset(ptr, bank->default_padded_value, padding_at_end);
943  LOG_INFO("Padding at " TARGET_ADDR_FMT " with %" PRIu32
944  " bytes (bank write end alignment)",
945  end_addr + 1, padding_at_end);
946  }
947 
948  retval = flash_driver_write(bank, buffer, aligned_start - bank->base, aligned_size);
949 
950  free(buffer);
951 
952  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
953  command_print(CMD, "wrote %zu bytes from file %s to flash bank %u"
954  " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
955  length, CMD_ARGV[1], bank->bank_number, offset,
956  duration_elapsed(&bench), duration_kbps(&bench, length));
957  }
958 
960 
961  return retval;
962 }
963 
964 COMMAND_HANDLER(handle_flash_read_bank_command)
965 {
966  uint32_t offset;
967  uint8_t *buffer;
968  struct fileio *fileio;
969  uint32_t length;
970  size_t written;
971 
972  if (CMD_ARGC < 2 || CMD_ARGC > 4)
974 
975  struct duration bench;
976  duration_start(&bench);
977 
978  struct flash_bank *p;
979  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
980 
981  if (retval != ERROR_OK)
982  return retval;
983 
984  offset = 0;
985 
986  if (CMD_ARGC > 2)
988 
989  if (offset > p->size) {
990  LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
991  offset);
993  }
994 
995  length = p->size - offset;
996 
997  if (CMD_ARGC > 3)
999 
1000  if (offset + length > p->size) {
1001  LOG_ERROR("Length of %" PRIu32 " bytes with offset 0x%8.8" PRIx32
1002  " is out of range of the flash bank", length, offset);
1004  }
1005 
1006  buffer = malloc(length);
1007  if (!buffer) {
1008  LOG_ERROR("Out of memory");
1009  return ERROR_FAIL;
1010  }
1011 
1012  retval = flash_driver_read(p, buffer, offset, length);
1013  if (retval != ERROR_OK) {
1014  LOG_ERROR("Read error");
1015  free(buffer);
1016  return retval;
1017  }
1018 
1020  if (retval != ERROR_OK) {
1021  LOG_ERROR("Could not open file");
1022  free(buffer);
1023  return retval;
1024  }
1025 
1026  retval = fileio_write(fileio, length, buffer, &written);
1028  free(buffer);
1029  if (retval != ERROR_OK) {
1030  LOG_ERROR("Could not write file");
1031  return ERROR_FAIL;
1032  }
1033 
1034  if (duration_measure(&bench) == ERROR_OK)
1035  command_print(CMD, "wrote %zd bytes to file %s from flash bank %u"
1036  " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
1037  written, CMD_ARGV[1], p->bank_number, offset,
1038  duration_elapsed(&bench), duration_kbps(&bench, written));
1039 
1040  return retval;
1041 }
1042 
1043 
1044 COMMAND_HANDLER(handle_flash_verify_bank_command)
1045 {
1046  uint32_t offset;
1047  uint8_t *buffer_file, *buffer_flash;
1048  struct fileio *fileio;
1049  size_t read_cnt;
1050  size_t filesize;
1051  size_t length;
1052  int differ;
1053 
1054  if (CMD_ARGC < 2 || CMD_ARGC > 3)
1056 
1057  struct duration bench;
1058  duration_start(&bench);
1059 
1060  struct flash_bank *p;
1061  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
1062  if (retval != ERROR_OK)
1063  return retval;
1064 
1065  offset = 0;
1066 
1067  if (CMD_ARGC > 2)
1069 
1070  if (offset > p->size) {
1071  LOG_ERROR("Offset 0x%8.8" PRIx32 " is out of range of the flash bank",
1072  offset);
1074  }
1075 
1077  if (retval != ERROR_OK) {
1078  LOG_ERROR("Could not open file");
1079  return retval;
1080  }
1081 
1082  retval = fileio_size(fileio, &filesize);
1083  if (retval != ERROR_OK) {
1085  return retval;
1086  }
1087 
1088  length = MIN(filesize, p->size - offset);
1089 
1090  if (!length) {
1091  LOG_INFO("Nothing to compare with flash bank");
1093  return ERROR_OK;
1094  }
1095 
1096  if (length != filesize)
1097  LOG_INFO("File content exceeds flash bank size. Only comparing the "
1098  "first %zu bytes of the file", length);
1099 
1100  buffer_file = malloc(length);
1101  if (!buffer_file) {
1102  LOG_ERROR("Out of memory");
1104  return ERROR_FAIL;
1105  }
1106 
1107  retval = fileio_read(fileio, length, buffer_file, &read_cnt);
1109  if (retval != ERROR_OK) {
1110  LOG_ERROR("File read failure");
1111  free(buffer_file);
1112  return retval;
1113  }
1114 
1115  if (read_cnt != length) {
1116  LOG_ERROR("Short read");
1117  free(buffer_file);
1118  return ERROR_FAIL;
1119  }
1120 
1121  buffer_flash = malloc(length);
1122  if (!buffer_flash) {
1123  LOG_ERROR("Out of memory");
1124  free(buffer_file);
1125  return ERROR_FAIL;
1126  }
1127 
1128  retval = flash_driver_read(p, buffer_flash, offset, length);
1129  if (retval != ERROR_OK) {
1130  LOG_ERROR("Flash read error");
1131  free(buffer_flash);
1132  free(buffer_file);
1133  return retval;
1134  }
1135 
1136  if (duration_measure(&bench) == ERROR_OK)
1137  command_print(CMD, "read %zd bytes from file %s and flash bank %u"
1138  " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
1139  length, CMD_ARGV[1], p->bank_number, offset,
1140  duration_elapsed(&bench), duration_kbps(&bench, length));
1141 
1142  differ = memcmp(buffer_file, buffer_flash, length);
1143  command_print(CMD, "contents %s", differ ? "differ" : "match");
1144  if (differ) {
1145  uint32_t t;
1146  int diffs = 0;
1147  for (t = 0; t < length; t++) {
1148  if (buffer_flash[t] == buffer_file[t])
1149  continue;
1150  command_print(CMD, "diff %d address 0x%08" PRIx32 ". Was 0x%02x instead of 0x%02x",
1151  diffs, t + offset, buffer_flash[t], buffer_file[t]);
1152  if (diffs++ >= 127) {
1153  command_print(CMD, "More than 128 errors, the rest are not printed.");
1154  break;
1155  }
1156  keep_alive();
1157  }
1158  }
1159  free(buffer_flash);
1160  free(buffer_file);
1161 
1162  return differ ? ERROR_FAIL : ERROR_OK;
1163 }
1164 
1166 {
1167  struct flash_bank *c;
1168 
1169  /* set all flash to require erasing */
1170  for (c = flash_bank_list(); c; c = c->next) {
1171  for (unsigned int i = 0; i < c->num_sectors; i++)
1172  c->sectors[i].is_erased = 0;
1173  }
1174 }
1175 
1176 COMMAND_HANDLER(handle_flash_padded_value_command)
1177 {
1178  if (CMD_ARGC != 2)
1180 
1181  struct flash_bank *p;
1182  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
1183  if (retval != ERROR_OK)
1184  return retval;
1185 
1187 
1188  command_print(CMD, "Default padded value set to 0x%" PRIx8 " for flash bank %u",
1190 
1191  return retval;
1192 }
1193 
1194 static const struct command_registration flash_exec_command_handlers[] = {
1195  {
1196  .name = "probe",
1197  .handler = handle_flash_probe_command,
1198  .mode = COMMAND_EXEC,
1199  .usage = "bank_id",
1200  .help = "Identify a flash bank.",
1201  },
1202  {
1203  .name = "info",
1204  .handler = handle_flash_info_command,
1205  .mode = COMMAND_EXEC,
1206  .usage = "bank_id ['sectors']",
1207  .help = "Print information about a flash bank.",
1208  },
1209  {
1210  .name = "erase_check",
1211  .handler = handle_flash_erase_check_command,
1212  .mode = COMMAND_EXEC,
1213  .usage = "bank_id",
1214  .help = "Check erase state of all blocks in a "
1215  "flash bank.",
1216  },
1217  {
1218  .name = "erase_sector",
1219  .handler = handle_flash_erase_command,
1220  .mode = COMMAND_EXEC,
1221  .usage = "bank_id first_sector_num (last_sector_num|'last')",
1222  .help = "Erase a range of sectors in a flash bank.",
1223  },
1224  {
1225  .name = "erase_address",
1226  .handler = handle_flash_erase_address_command,
1227  .mode = COMMAND_EXEC,
1228  .usage = "['pad'] ['unlock'] address length",
1229  .help = "Erase flash sectors starting at address and "
1230  "continuing for length bytes. If 'pad' is specified, "
1231  "data outside that range may also be erased: the start "
1232  "address may be decreased, and length increased, so "
1233  "that all of the first and last sectors are erased. "
1234  "If 'unlock' is specified, then the flash is unprotected "
1235  "before erasing.",
1236 
1237  },
1238  {
1239  .name = "filld",
1240  .handler = handle_flash_fill_command,
1241  .mode = COMMAND_EXEC,
1242  .usage = "address value n",
1243  .help = "Fill n double-words with 64-bit value, starting at "
1244  "word address. (No autoerase.)",
1245  },
1246  {
1247  .name = "fillw",
1248  .handler = handle_flash_fill_command,
1249  .mode = COMMAND_EXEC,
1250  .usage = "address value n",
1251  .help = "Fill n words with 32-bit value, starting at "
1252  "word address. (No autoerase.)",
1253  },
1254  {
1255  .name = "fillh",
1256  .handler = handle_flash_fill_command,
1257  .mode = COMMAND_EXEC,
1258  .usage = "address value n",
1259  .help = "Fill n halfwords with 16-bit value, starting at "
1260  "word address. (No autoerase.)",
1261  },
1262  {
1263  .name = "fillb",
1264  .handler = handle_flash_fill_command,
1265  .mode = COMMAND_EXEC,
1266  .usage = "address value n",
1267  .help = "Fill n bytes with 8-bit value, starting at "
1268  "word address. (No autoerase.)",
1269  },
1270  {
1271  .name = "mdb",
1272  .handler = handle_flash_md_command,
1273  .mode = COMMAND_EXEC,
1274  .usage = "address [count]",
1275  .help = "Display bytes from flash.",
1276  },
1277  {
1278  .name = "mdh",
1279  .handler = handle_flash_md_command,
1280  .mode = COMMAND_EXEC,
1281  .usage = "address [count]",
1282  .help = "Display half-words from flash.",
1283  },
1284  {
1285  .name = "mdw",
1286  .handler = handle_flash_md_command,
1287  .mode = COMMAND_EXEC,
1288  .usage = "address [count]",
1289  .help = "Display words from flash.",
1290  },
1291  {
1292  .name = "read_memory",
1293  .mode = COMMAND_EXEC,
1294  .handler = handle_flash_read_memory_command,
1295  .help = "Read Tcl list of 8/16/32/64 bit numbers from flash memory",
1296  .usage = "address width count",
1297  },
1298  {
1299  .name = "write_bank",
1300  .handler = handle_flash_write_bank_command,
1301  .mode = COMMAND_EXEC,
1302  .usage = "bank_id filename [offset]",
1303  .help = "Write binary data from file to flash bank. Allow optional "
1304  "offset from beginning of the bank (defaults to zero).",
1305  },
1306  {
1307  .name = "write_image",
1308  .handler = handle_flash_write_image_command,
1309  .mode = COMMAND_EXEC,
1310  .usage = "[erase] [unlock] filename [offset [file_type]]",
1311  .help = "Write an image to flash. Optionally first unprotect "
1312  "and/or erase the region to be used. Allow optional "
1313  "offset from beginning of bank (defaults to zero)",
1314  },
1315  {
1316  .name = "verify_image",
1317  .handler = handle_flash_verify_image_command,
1318  .mode = COMMAND_EXEC,
1319  .usage = "filename [offset [file_type]]",
1320  .help = "Verify an image against flash. Allow optional "
1321  "offset from beginning of bank (defaults to zero)",
1322  },
1323  {
1324  .name = "read_bank",
1325  .handler = handle_flash_read_bank_command,
1326  .mode = COMMAND_EXEC,
1327  .usage = "bank_id filename [offset [length]]",
1328  .help = "Read binary data from flash bank to file. Allow optional "
1329  "offset from beginning of the bank (defaults to zero).",
1330  },
1331  {
1332  .name = "verify_bank",
1333  .handler = handle_flash_verify_bank_command,
1334  .mode = COMMAND_EXEC,
1335  .usage = "bank_id filename [offset]",
1336  .help = "Compare the contents of a file with the contents of the "
1337  "flash bank. Allow optional offset from beginning of the bank "
1338  "(defaults to zero).",
1339  },
1340  {
1341  .name = "protect",
1342  .handler = handle_flash_protect_command,
1343  .mode = COMMAND_EXEC,
1344  .usage = "bank_id first_block [last_block|'last'] "
1345  "('on'|'off')",
1346  .help = "Turn protection on or off for a range of protection "
1347  "blocks or sectors in a given flash bank. "
1348  "See 'flash info' output for a list of blocks.",
1349  },
1350  {
1351  .name = "padded_value",
1352  .handler = handle_flash_padded_value_command,
1353  .mode = COMMAND_EXEC,
1354  .usage = "bank_id value",
1355  .help = "Set default flash padded value",
1356  },
1358 };
1359 
1360 static int flash_init_drivers(struct command_context *cmd_ctx)
1361 {
1362  if (!flash_bank_list())
1363  return ERROR_OK;
1364 
1365  return register_commands(cmd_ctx, "flash", flash_exec_command_handlers);
1366 }
1367 
1368 COMMAND_HANDLER(handle_flash_bank_command)
1369 {
1370  if (CMD_ARGC < 7) {
1371  LOG_ERROR("usage: flash bank <name> <driver> "
1372  "<base> <size> <chip_width> <bus_width> <target>");
1374  }
1375  /* save bank name and advance arguments for compatibility */
1376  const char *bank_name = *CMD_ARGV++;
1377  CMD_ARGC--;
1378 
1379  struct target *target = get_target(CMD_ARGV[5]);
1380  if (!target) {
1381  LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
1382  return ERROR_FAIL;
1383  }
1384 
1385  const char *driver_name = CMD_ARGV[0];
1386  const struct flash_driver *driver = flash_driver_find_by_name(driver_name);
1387  if (!driver) {
1388  /* no matching flash driver found */
1389  LOG_ERROR("flash driver '%s' not found", driver_name);
1390  return ERROR_FAIL;
1391  }
1392 
1393  /* check the flash bank name is unique */
1394  if (get_flash_bank_by_name_noprobe(bank_name)) {
1395  /* flash bank name already exists */
1396  LOG_ERROR("flash bank name '%s' already exists", bank_name);
1397  return ERROR_FAIL;
1398  }
1399 
1400  /* register flash specific commands */
1401  if (driver->commands) {
1402  int retval = register_commands(CMD_CTX, NULL, driver->commands);
1403  if (retval != ERROR_OK) {
1404  LOG_ERROR("couldn't register '%s' commands",
1405  driver_name);
1406  return ERROR_FAIL;
1407  }
1408  }
1409 
1410  struct flash_bank *c = calloc(1, sizeof(*c));
1411  c->name = strdup(bank_name);
1412  c->target = target;
1413  c->driver = driver;
1414  COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[1], c->base);
1415  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
1417  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[4], c->bus_width);
1418  c->default_padded_value = c->erased_value = 0xff;
1420 
1421  int retval;
1422  retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
1423  if (retval != ERROR_OK) {
1424  LOG_ERROR("'%s' driver rejected flash bank at " TARGET_ADDR_FMT
1425  "; usage: %s", driver_name, c->base, driver->usage);
1426  free(c->name);
1427  free(c);
1428  return retval;
1429  }
1430 
1431  if (!driver->usage)
1432  LOG_DEBUG("'%s' driver usage field missing", driver_name);
1433 
1434  flash_bank_add(c);
1435 
1436  return ERROR_OK;
1437 }
1438 
1439 COMMAND_HANDLER(handle_flash_banks_command)
1440 {
1441  if (CMD_ARGC != 0)
1443 
1444  unsigned int n = 0;
1445  for (struct flash_bank *p = flash_bank_list(); p; p = p->next, n++) {
1446  command_print(CMD, "#%d : %s (%s) at " TARGET_ADDR_FMT ", size 0x%8.8" PRIx32 ", "
1447  "buswidth %u, chipwidth %u", p->bank_number,
1448  p->name, p->driver->name, p->base, p->size,
1449  p->bus_width, p->chip_width);
1450  }
1451  return ERROR_OK;
1452 }
1453 
1454 COMMAND_HANDLER(handle_flash_list)
1455 {
1456  if (CMD_ARGC != 0)
1458 
1459  for (struct flash_bank *p = flash_bank_list(); p; p = p->next) {
1461  "{\n"
1462  " name %s\n"
1463  " driver %s\n"
1464  " base " TARGET_ADDR_FMT "\n"
1465  " size 0x%" PRIx32 "\n"
1466  " bus_width %u\n"
1467  " chip_width %u\n"
1468  " target %s\n"
1469  "}",
1470  p->name, p->driver->name, p->base, p->size, p->bus_width, p->chip_width,
1471  target_name(p->target));
1472  }
1473 
1474  return ERROR_OK;
1475 }
1476 
1477 COMMAND_HANDLER(handle_flash_init_command)
1478 {
1479  if (CMD_ARGC != 0)
1481 
1482  static bool flash_initialized;
1483  if (flash_initialized) {
1484  LOG_INFO("'flash init' has already been called");
1485  return ERROR_OK;
1486  }
1487  flash_initialized = true;
1488 
1489  LOG_DEBUG("Initializing flash devices...");
1490  return flash_init_drivers(CMD_CTX);
1491 }
1492 
1493 static const struct command_registration flash_config_command_handlers[] = {
1494  {
1495  .name = "bank",
1496  .handler = handle_flash_bank_command,
1497  .mode = COMMAND_CONFIG,
1498  .usage = "bank_id driver_name base_address size_bytes "
1499  "chip_width_bytes bus_width_bytes target "
1500  "[driver_options ...]",
1501  .help = "Define a new bank with the given name, "
1502  "using the specified NOR flash driver.",
1503  },
1504  {
1505  .name = "init",
1506  .mode = COMMAND_CONFIG,
1507  .handler = handle_flash_init_command,
1508  .help = "Initialize flash devices.",
1509  .usage = "",
1510  },
1511  {
1512  .name = "banks",
1513  .mode = COMMAND_ANY,
1514  .handler = handle_flash_banks_command,
1515  .help = "Display table with information about flash banks.",
1516  .usage = "",
1517  },
1518  {
1519  .name = "list",
1520  .mode = COMMAND_ANY,
1521  .handler = handle_flash_list,
1522  .help = "Returns a list of details about the flash banks.",
1523  .usage = "",
1524  },
1526 };
1527 static const struct command_registration flash_command_handlers[] = {
1528  {
1529  .name = "flash",
1530  .mode = COMMAND_ANY,
1531  .help = "NOR flash command group",
1533  .usage = "",
1534  },
1536 };
1537 
1539 {
1540  return register_commands(cmd_ctx, NULL, flash_command_handlers);
1541 }
const char * name
Definition: armv4_5.c:76
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:352
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:375
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:166
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define COMMAND_PARSE_ADDRESS(in, out)
Definition: command.h:450
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:528
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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:440
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:402
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:272
@ 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)
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_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length)
Erases length bytes in the target flash, starting at addr.
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_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:1211
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:957
void keep_alive(void)
Definition: log.c:426
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
#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:94
#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:234
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:239
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:124
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
Definition: nor/core.h:116
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
Definition: nor/core.h:95
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:87
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:114
struct flash_sector * prot_blocks
Array of protection blocks, allocated and initialized by the flash driver.
Definition: nor/core.h:126
unsigned int bus_width
Maximum bus width, in bytes (1,2,4 bytes)
Definition: nor/core.h:88
struct flash_bank * next
The next flash bank on this chip.
Definition: nor/core.h:128
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:107
char * name
Definition: nor/core.h:76
uint8_t erased_value
Erased value.
Definition: nor/core.h:91
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:219
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:179
int(* info)(struct flash_bank *bank, struct command_invocation *cmd)
Display human-readable information about the flash bank.
Definition: nor/driver.h:204
int(* probe)(struct flash_bank *bank)
Probe to determine what kind of flash is present.
Definition: nor/driver.h:168
int(* protect_check)(struct flash_bank *bank)
Determine if the specific bank is "protected" or not.
Definition: nor/driver.h:193
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:316
struct target * get_target(const char *id)
Definition: target.c:442
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:379
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:361
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
Definition: target.c:352
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:343
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)
Definition: target.c:3354
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:325
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:236
float duration_elapsed(const struct duration *duration)
Definition: time_support.c:83
int duration_measure(struct duration *duration)
Update the duration->elapsed field to finish the duration measurement.
Definition: time_support.c:74
int duration_start(struct duration *duration)
Update the duration->start field to start the duration measurement.
Definition: time_support.c:69
float duration_kbps(const struct duration *duration, size_t count)
Definition: time_support.c:90
#define TARGET_ADDR_FMT
Definition: types.h:342
uint64_t target_addr_t
Definition: types.h:335
#define NULL
Definition: usb.h:16
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22