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