add flash/nor/{tcl.c,imp.h} from flash/flash.c
[openocd.git] / src / flash / flash.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "flash.h"
31 #include "common.h"
32 #include <target/image.h>
33 #include <helper/time_support.h>
34
35 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock);
36
37 /* flash drivers
38 */
39 extern struct flash_driver lpc2000_flash;
40 extern struct flash_driver lpc288x_flash;
41 extern struct flash_driver lpc2900_flash;
42 extern struct flash_driver cfi_flash;
43 extern struct flash_driver at91sam3_flash;
44 extern struct flash_driver at91sam7_flash;
45 extern struct flash_driver str7x_flash;
46 extern struct flash_driver str9x_flash;
47 extern struct flash_driver aduc702x_flash;
48 extern struct flash_driver stellaris_flash;
49 extern struct flash_driver str9xpec_flash;
50 extern struct flash_driver stm32x_flash;
51 extern struct flash_driver tms470_flash;
52 extern struct flash_driver ecosflash_flash;
53 extern struct flash_driver ocl_flash;
54 extern struct flash_driver pic32mx_flash;
55 extern struct flash_driver avr_flash;
56 extern struct flash_driver faux_flash;
57
58 struct flash_driver *flash_drivers[] = {
59 &lpc2000_flash,
60 &lpc288x_flash,
61 &lpc2900_flash,
62 &cfi_flash,
63 &at91sam7_flash,
64 &at91sam3_flash,
65 &str7x_flash,
66 &str9x_flash,
67 &aduc702x_flash,
68 &stellaris_flash,
69 &str9xpec_flash,
70 &stm32x_flash,
71 &tms470_flash,
72 &ecosflash_flash,
73 &ocl_flash,
74 &pic32mx_flash,
75 &avr_flash,
76 &faux_flash,
77 NULL,
78 };
79
80 struct flash_bank *flash_banks;
81
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
84 {
85 int retval;
86
87 retval = bank->driver->write(bank, buffer, offset, count);
88 if (retval != ERROR_OK)
89 {
90 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
91 bank->base, offset, retval);
92 }
93
94 return retval;
95 }
96
97 static int flash_driver_erase(struct flash_bank *bank, int first, int last)
98 {
99 int retval;
100
101 retval = bank->driver->erase(bank, first, last);
102 if (retval != ERROR_OK)
103 {
104 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
105 }
106
107 return retval;
108 }
109
110 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
111 {
112 int retval;
113
114 retval = bank->driver->protect(bank, set, first, last);
115 if (retval != ERROR_OK)
116 {
117 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
118 }
119
120 return retval;
121 }
122
123 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
124 {
125 struct flash_bank *p;
126 int i = 0;
127
128 for (p = flash_banks; p; p = p->next)
129 {
130 if (i++ == num)
131 {
132 return p;
133 }
134 }
135 LOG_ERROR("flash bank %d does not exist", num);
136 return NULL;
137 }
138
139 int flash_get_bank_count(void)
140 {
141 struct flash_bank *p;
142 int i = 0;
143 for (p = flash_banks; p; p = p->next)
144 {
145 i++;
146 }
147 return i;
148 }
149
150 struct flash_bank *get_flash_bank_by_name(const char *name)
151 {
152 unsigned requested = get_flash_name_index(name);
153 unsigned found = 0;
154
155 struct flash_bank *bank;
156 for (bank = flash_banks; NULL != bank; bank = bank->next)
157 {
158 if (strcmp(bank->name, name) == 0)
159 return bank;
160 if (!flash_driver_name_matches(bank->driver->name, name))
161 continue;
162 if (++found < requested)
163 continue;
164 return bank;
165 }
166 return NULL;
167 }
168
169 struct flash_bank *get_flash_bank_by_num(int num)
170 {
171 struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
172 int retval;
173
174 if (p == NULL)
175 return NULL;
176
177 retval = p->driver->auto_probe(p);
178
179 if (retval != ERROR_OK)
180 {
181 LOG_ERROR("auto_probe failed %d\n", retval);
182 return NULL;
183 }
184 return p;
185 }
186
187 COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
188 struct flash_bank **bank)
189 {
190 const char *name = CMD_ARGV[name_index];
191 *bank = get_flash_bank_by_name(name);
192 if (*bank)
193 return ERROR_OK;
194
195 unsigned bank_num;
196 COMMAND_PARSE_NUMBER(uint, name, bank_num);
197
198 *bank = get_flash_bank_by_num(bank_num);
199 if (!*bank)
200 {
201 command_print(CMD_CTX, "flash bank '%s' not found", name);
202 return ERROR_INVALID_ARGUMENTS;
203 }
204 return ERROR_OK;
205 }
206
207
208 COMMAND_HANDLER(handle_flash_info_command)
209 {
210 struct flash_bank *p;
211 uint32_t i = 0;
212 int j = 0;
213 int retval;
214
215 if (CMD_ARGC != 1)
216 return ERROR_COMMAND_SYNTAX_ERROR;
217
218 unsigned bank_nr;
219 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
220
221 for (p = flash_banks; p; p = p->next, i++)
222 {
223 if (i != bank_nr)
224 continue;
225
226 char buf[1024];
227
228 /* attempt auto probe */
229 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
230 return retval;
231
232 command_print(CMD_CTX,
233 "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
234 i,
235 p->driver->name,
236 p->base,
237 p->size,
238 p->bus_width,
239 p->chip_width);
240 for (j = 0; j < p->num_sectors; j++)
241 {
242 char *protect_state;
243
244 if (p->sectors[j].is_protected == 0)
245 protect_state = "not protected";
246 else if (p->sectors[j].is_protected == 1)
247 protect_state = "protected";
248 else
249 protect_state = "protection state unknown";
250
251 command_print(CMD_CTX,
252 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
253 j,
254 p->sectors[j].offset,
255 p->sectors[j].size,
256 p->sectors[j].size >> 10,
257 protect_state);
258 }
259
260 *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
261 retval = p->driver->info(p, buf, sizeof(buf));
262 command_print(CMD_CTX, "%s", buf);
263 if (retval != ERROR_OK)
264 LOG_ERROR("error retrieving flash info (%d)", retval);
265 }
266
267 return ERROR_OK;
268 }
269
270 COMMAND_HANDLER(handle_flash_probe_command)
271 {
272 int retval;
273
274 if (CMD_ARGC != 1)
275 {
276 return ERROR_COMMAND_SYNTAX_ERROR;
277 }
278
279 unsigned bank_nr;
280 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
281 struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
282 if (p)
283 {
284 if ((retval = p->driver->probe(p)) == ERROR_OK)
285 {
286 command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
287 }
288 else if (retval == ERROR_FLASH_BANK_INVALID)
289 {
290 command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
291 CMD_ARGV[0], p->base);
292 }
293 else
294 {
295 command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
296 CMD_ARGV[0], p->base);
297 }
298 }
299 else
300 {
301 command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
302 }
303
304 return ERROR_OK;
305 }
306
307 COMMAND_HANDLER(handle_flash_erase_check_command)
308 {
309 if (CMD_ARGC != 1)
310 {
311 return ERROR_COMMAND_SYNTAX_ERROR;
312 }
313
314 struct flash_bank *p;
315 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
316 if (ERROR_OK != retval)
317 return retval;
318
319 int j;
320 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
321 {
322 command_print(CMD_CTX, "successfully checked erase state");
323 }
324 else
325 {
326 command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
327 CMD_ARGV[0], p->base);
328 }
329
330 for (j = 0; j < p->num_sectors; j++)
331 {
332 char *erase_state;
333
334 if (p->sectors[j].is_erased == 0)
335 erase_state = "not erased";
336 else if (p->sectors[j].is_erased == 1)
337 erase_state = "erased";
338 else
339 erase_state = "erase state unknown";
340
341 command_print(CMD_CTX,
342 "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
343 j,
344 p->sectors[j].offset,
345 p->sectors[j].size,
346 p->sectors[j].size >> 10,
347 erase_state);
348 }
349
350 return ERROR_OK;
351 }
352
353 COMMAND_HANDLER(handle_flash_erase_address_command)
354 {
355 struct flash_bank *p;
356 int retval;
357 int address;
358 int length;
359
360 struct target *target = get_current_target(CMD_CTX);
361
362 if (CMD_ARGC != 2)
363 return ERROR_COMMAND_SYNTAX_ERROR;
364
365 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
366 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], length);
367 if (length <= 0)
368 {
369 command_print(CMD_CTX, "Length must be >0");
370 return ERROR_COMMAND_SYNTAX_ERROR;
371 }
372
373 p = get_flash_bank_by_addr(target, address);
374 if (p == NULL)
375 {
376 return ERROR_FAIL;
377 }
378
379 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
380 flash_set_dirty();
381
382 struct duration bench;
383 duration_start(&bench);
384
385 retval = flash_erase_address_range(target, address, length);
386
387 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
388 {
389 command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
390 " in %fs (%0.3f kb/s)", address, length,
391 duration_elapsed(&bench), duration_kbps(&bench, length));
392 }
393
394 return retval;
395 }
396
397 COMMAND_HANDLER(handle_flash_protect_check_command)
398 {
399 if (CMD_ARGC != 1)
400 return ERROR_COMMAND_SYNTAX_ERROR;
401
402 struct flash_bank *p;
403 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
404 if (ERROR_OK != retval)
405 return retval;
406
407 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
408 {
409 command_print(CMD_CTX, "successfully checked protect state");
410 }
411 else if (retval == ERROR_FLASH_OPERATION_FAILED)
412 {
413 command_print(CMD_CTX, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
414 }
415 else
416 {
417 command_print(CMD_CTX, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
418 }
419
420 return ERROR_OK;
421 }
422
423 static int flash_check_sector_parameters(struct command_context *cmd_ctx,
424 uint32_t first, uint32_t last, uint32_t num_sectors)
425 {
426 if (!(first <= last)) {
427 command_print(cmd_ctx, "ERROR: "
428 "first sector must be <= last sector");
429 return ERROR_FAIL;
430 }
431
432 if (!(last <= (num_sectors - 1))) {
433 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
434 (int) num_sectors - 1);
435 return ERROR_FAIL;
436 }
437
438 return ERROR_OK;
439 }
440
441 COMMAND_HANDLER(handle_flash_erase_command)
442 {
443 if (CMD_ARGC != 3)
444 return ERROR_COMMAND_SYNTAX_ERROR;
445
446 uint32_t bank_nr;
447 uint32_t first;
448 uint32_t last;
449
450 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
451 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
452 if (!p)
453 return ERROR_OK;
454
455 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
456 if (strcmp(CMD_ARGV[2], "last") == 0)
457 last = p->num_sectors - 1;
458 else
459 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
460
461 int retval;
462 if ((retval = flash_check_sector_parameters(CMD_CTX,
463 first, last, p->num_sectors)) != ERROR_OK)
464 return retval;
465
466 struct duration bench;
467 duration_start(&bench);
468
469 retval = flash_driver_erase(p, first, last);
470
471 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
472 {
473 command_print(CMD_CTX, "erased sectors %" PRIu32 " "
474 "through %" PRIu32" on flash bank %" PRIu32 " "
475 "in %fs", first, last, bank_nr, duration_elapsed(&bench));
476 }
477
478 return ERROR_OK;
479 }
480
481 COMMAND_HANDLER(handle_flash_protect_command)
482 {
483 if (CMD_ARGC != 4)
484 return ERROR_COMMAND_SYNTAX_ERROR;
485
486 uint32_t bank_nr;
487 uint32_t first;
488 uint32_t last;
489
490 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
491 struct flash_bank *p = get_flash_bank_by_num(bank_nr);
492 if (!p)
493 return ERROR_OK;
494
495 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
496 if (strcmp(CMD_ARGV[2], "last") == 0)
497 last = p->num_sectors - 1;
498 else
499 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
500
501 bool set;
502 COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
503
504 int retval;
505 if ((retval = flash_check_sector_parameters(CMD_CTX,
506 first, last, p->num_sectors)) != ERROR_OK)
507 return retval;
508
509 retval = flash_driver_protect(p, set, first, last);
510 if (retval == ERROR_OK) {
511 command_print(CMD_CTX, "%s protection for sectors %i "
512 "through %i on flash bank %i",
513 (set) ? "set" : "cleared", (int) first,
514 (int) last, (int) bank_nr);
515 }
516
517 return ERROR_OK;
518 }
519
520 COMMAND_HANDLER(handle_flash_write_image_command)
521 {
522 struct target *target = get_current_target(CMD_CTX);
523
524 struct image image;
525 uint32_t written;
526
527 int retval;
528
529 if (CMD_ARGC < 1)
530 {
531 return ERROR_COMMAND_SYNTAX_ERROR;
532 }
533
534 /* flash auto-erase is disabled by default*/
535 int auto_erase = 0;
536 bool auto_unlock = false;
537
538 for (;;)
539 {
540 if (strcmp(CMD_ARGV[0], "erase") == 0)
541 {
542 auto_erase = 1;
543 CMD_ARGV++;
544 CMD_ARGC--;
545 command_print(CMD_CTX, "auto erase enabled");
546 } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
547 {
548 auto_unlock = true;
549 CMD_ARGV++;
550 CMD_ARGC--;
551 command_print(CMD_CTX, "auto unlock enabled");
552 } else
553 {
554 break;
555 }
556 }
557
558 if (CMD_ARGC < 1)
559 {
560 return ERROR_COMMAND_SYNTAX_ERROR;
561 }
562
563 if (!target)
564 {
565 LOG_ERROR("no target selected");
566 return ERROR_FAIL;
567 }
568
569 struct duration bench;
570 duration_start(&bench);
571
572 if (CMD_ARGC >= 2)
573 {
574 image.base_address_set = 1;
575 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], image.base_address);
576 }
577 else
578 {
579 image.base_address_set = 0;
580 image.base_address = 0x0;
581 }
582
583 image.start_address_set = 0;
584
585 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
586 if (retval != ERROR_OK)
587 {
588 return retval;
589 }
590
591 retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
592 if (retval != ERROR_OK)
593 {
594 image_close(&image);
595 return retval;
596 }
597
598 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
599 {
600 command_print(CMD_CTX, "wrote %" PRIu32 " byte from file %s "
601 "in %fs (%0.3f kb/s)", written, CMD_ARGV[0],
602 duration_elapsed(&bench), duration_kbps(&bench, written));
603 }
604
605 image_close(&image);
606
607 return retval;
608 }
609
610 COMMAND_HANDLER(handle_flash_fill_command)
611 {
612 int err = ERROR_OK;
613 uint32_t address;
614 uint32_t pattern;
615 uint32_t count;
616 uint32_t wrote = 0;
617 uint32_t cur_size = 0;
618 uint32_t chunk_count;
619 struct target *target = get_current_target(CMD_CTX);
620 uint32_t i;
621 uint32_t wordsize;
622 int retval = ERROR_OK;
623
624 static size_t const chunksize = 1024;
625 uint8_t *chunk = malloc(chunksize);
626 if (chunk == NULL)
627 return ERROR_FAIL;
628
629 uint8_t *readback = malloc(chunksize);
630 if (readback == NULL)
631 {
632 free(chunk);
633 return ERROR_FAIL;
634 }
635
636
637 if (CMD_ARGC != 3)
638 {
639 retval = ERROR_COMMAND_SYNTAX_ERROR;
640 goto done;
641 }
642
643
644 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
645 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
646 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
647
648 if (count == 0)
649 goto done;
650
651 switch (CMD_NAME[4])
652 {
653 case 'w':
654 wordsize = 4;
655 break;
656 case 'h':
657 wordsize = 2;
658 break;
659 case 'b':
660 wordsize = 1;
661 break;
662 default:
663 retval = ERROR_COMMAND_SYNTAX_ERROR;
664 goto done;
665 }
666
667 chunk_count = MIN(count, (chunksize / wordsize));
668 switch (wordsize)
669 {
670 case 4:
671 for (i = 0; i < chunk_count; i++)
672 {
673 target_buffer_set_u32(target, chunk + i * wordsize, pattern);
674 }
675 break;
676 case 2:
677 for (i = 0; i < chunk_count; i++)
678 {
679 target_buffer_set_u16(target, chunk + i * wordsize, pattern);
680 }
681 break;
682 case 1:
683 memset(chunk, pattern, chunk_count);
684 break;
685 default:
686 LOG_ERROR("BUG: can't happen");
687 exit(-1);
688 }
689
690 struct duration bench;
691 duration_start(&bench);
692
693 for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
694 {
695 cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
696 struct flash_bank *bank;
697 bank = get_flash_bank_by_addr(target, address);
698 if (bank == NULL)
699 {
700 retval = ERROR_FAIL;
701 goto done;
702 }
703 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
704 if (err != ERROR_OK)
705 {
706 retval = err;
707 goto done;
708 }
709
710 err = target_read_buffer(target, address + wrote, cur_size, readback);
711 if (err != ERROR_OK)
712 {
713 retval = err;
714 goto done;
715 }
716
717 unsigned i;
718 for (i = 0; i < cur_size; i++)
719 {
720 if (readback[i]!=chunk[i])
721 {
722 LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
723 address + wrote + i, readback[i], chunk[i]);
724 retval = ERROR_FAIL;
725 goto done;
726 }
727 }
728 }
729
730 if (duration_measure(&bench) == ERROR_OK)
731 {
732 command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
733 " in %fs (%0.3f kb/s)", wrote, address,
734 duration_elapsed(&bench), duration_kbps(&bench, wrote));
735 }
736
737 done:
738 free(readback);
739 free(chunk);
740
741 return retval;
742 }
743
744 COMMAND_HANDLER(handle_flash_write_bank_command)
745 {
746 uint32_t offset;
747 uint8_t *buffer;
748 struct fileio fileio;
749
750 if (CMD_ARGC != 3)
751 return ERROR_COMMAND_SYNTAX_ERROR;
752
753 struct duration bench;
754 duration_start(&bench);
755
756 struct flash_bank *p;
757 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
758 if (ERROR_OK != retval)
759 return retval;
760
761 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
762
763 if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
764 {
765 return ERROR_OK;
766 }
767
768 buffer = malloc(fileio.size);
769 size_t buf_cnt;
770 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
771 {
772 free(buffer);
773 fileio_close(&fileio);
774 return ERROR_OK;
775 }
776
777 retval = flash_driver_write(p, buffer, offset, buf_cnt);
778
779 free(buffer);
780 buffer = NULL;
781
782 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
783 {
784 command_print(CMD_CTX, "wrote %zu byte from file %s to flash bank %u"
785 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
786 fileio.size, CMD_ARGV[1], p->bank_number, offset,
787 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
788 }
789
790 fileio_close(&fileio);
791
792 return retval;
793 }
794
795 void flash_set_dirty(void)
796 {
797 struct flash_bank *c;
798 int i;
799
800 /* set all flash to require erasing */
801 for (c = flash_banks; c; c = c->next)
802 {
803 for (i = 0; i < c->num_sectors; i++)
804 {
805 c->sectors[i].is_erased = 0;
806 }
807 }
808 }
809
810 /* lookup flash bank by address */
811 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
812 {
813 struct flash_bank *c;
814
815 /* cycle through bank list */
816 for (c = flash_banks; c; c = c->next)
817 {
818 int retval;
819 retval = c->driver->auto_probe(c);
820
821 if (retval != ERROR_OK)
822 {
823 LOG_ERROR("auto_probe failed %d\n", retval);
824 return NULL;
825 }
826 /* check whether address belongs to this flash bank */
827 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
828 return c;
829 }
830 LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
831 return NULL;
832 }
833
834 /* erase given flash region, selects proper bank according to target and address */
835 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
836 int (*callback)(struct flash_bank *bank, int first, int last))
837 {
838 struct flash_bank *c;
839 int first = -1;
840 int last = -1;
841 int i;
842
843 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
844 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
845
846 if (c->size == 0 || c->num_sectors == 0)
847 {
848 LOG_ERROR("Bank is invalid");
849 return ERROR_FLASH_BANK_INVALID;
850 }
851
852 if (length == 0)
853 {
854 /* special case, erase whole bank when length is zero */
855 if (addr != c->base)
856 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
857
858 return callback(c, 0, c->num_sectors - 1);
859 }
860
861 /* check whether it fits */
862 if (addr + length - 1 > c->base + c->size - 1)
863 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
864
865 addr -= c->base;
866
867 for (i = 0; i < c->num_sectors; i++)
868 {
869 /* check whether sector overlaps with the given range and is not yet erased */
870 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
871 /* if first is not set yet then this is the first sector */
872 if (first == -1)
873 first = i;
874 last = i; /* and it is the last one so far in any case */
875 }
876 }
877
878 if (first == -1 || last == -1)
879 return ERROR_OK;
880
881 return callback(c, first, last);
882 }
883
884
885
886 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
887 {
888 return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
889 }
890
891 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
892 {
893 return flash_driver_protect(bank, 0, first, last);
894 }
895
896 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
897 {
898 return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
899 }
900
901
902 /* write (optional verify) an image to flash memory of the given target */
903 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock)
904 {
905 int retval = ERROR_OK;
906
907 int section;
908 uint32_t section_offset;
909 struct flash_bank *c;
910 int *padding;
911
912 section = 0;
913 section_offset = 0;
914
915 if (written)
916 *written = 0;
917
918 if (erase)
919 {
920 /* assume all sectors need erasing - stops any problems
921 * when flash_write is called multiple times */
922
923 flash_set_dirty();
924 }
925
926 /* allocate padding array */
927 padding = malloc(image->num_sections * sizeof(padding));
928
929 /* loop until we reach end of the image */
930 while (section < image->num_sections)
931 {
932 uint32_t buffer_size;
933 uint8_t *buffer;
934 int section_first;
935 int section_last;
936 uint32_t run_address = image->sections[section].base_address + section_offset;
937 uint32_t run_size = image->sections[section].size - section_offset;
938 int pad_bytes = 0;
939
940 if (image->sections[section].size == 0)
941 {
942 LOG_WARNING("empty section %d", section);
943 section++;
944 section_offset = 0;
945 continue;
946 }
947
948 /* find the corresponding flash bank */
949 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
950 {
951 section++; /* and skip it */
952 section_offset = 0;
953 continue;
954 }
955
956 /* collect consecutive sections which fall into the same bank */
957 section_first = section;
958 section_last = section;
959 padding[section] = 0;
960 while ((run_address + run_size - 1 < c->base + c->size - 1)
961 && (section_last + 1 < image->num_sections))
962 {
963 if (image->sections[section_last + 1].base_address < (run_address + run_size))
964 {
965 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
966 break;
967 }
968 /* if we have multiple sections within our image, flash programming could fail due to alignment issues
969 * attempt to rebuild a consecutive buffer for the flash loader */
970 pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
971 if ((run_address + run_size + pad_bytes) > (c->base + c->size))
972 break;
973 padding[section_last] = pad_bytes;
974 run_size += image->sections[++section_last].size;
975 run_size += pad_bytes;
976 padding[section_last] = 0;
977
978 LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
979 }
980
981 /* fit the run into bank constraints */
982 if (run_address + run_size - 1 > c->base + c->size - 1)
983 {
984 LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
985 (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
986 run_size = c->base + c->size - run_address;
987 }
988
989 /* allocate buffer */
990 buffer = malloc(run_size);
991 buffer_size = 0;
992
993 /* read sections to the buffer */
994 while (buffer_size < run_size)
995 {
996 size_t size_read;
997
998 size_read = run_size - buffer_size;
999 if (size_read > image->sections[section].size - section_offset)
1000 size_read = image->sections[section].size - section_offset;
1001
1002 if ((retval = image_read_section(image, section, section_offset,
1003 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
1004 {
1005 free(buffer);
1006 free(padding);
1007 return retval;
1008 }
1009
1010 /* see if we need to pad the section */
1011 while (padding[section]--)
1012 (buffer + buffer_size)[size_read++] = 0xff;
1013
1014 buffer_size += size_read;
1015 section_offset += size_read;
1016
1017 if (section_offset >= image->sections[section].size)
1018 {
1019 section++;
1020 section_offset = 0;
1021 }
1022 }
1023
1024 retval = ERROR_OK;
1025
1026 if (unlock)
1027 {
1028 retval = flash_unlock_address_range(target, run_address, run_size);
1029 }
1030 if (retval == ERROR_OK)
1031 {
1032 if (erase)
1033 {
1034 /* calculate and erase sectors */
1035 retval = flash_erase_address_range(target, run_address, run_size);
1036 }
1037 }
1038
1039 if (retval == ERROR_OK)
1040 {
1041 /* write flash sectors */
1042 retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1043 }
1044
1045 free(buffer);
1046
1047 if (retval != ERROR_OK)
1048 {
1049 free(padding);
1050 return retval; /* abort operation */
1051 }
1052
1053 if (written != NULL)
1054 *written += run_size; /* add run size to total written counter */
1055 }
1056
1057 free(padding);
1058
1059 return retval;
1060 }
1061
1062 int flash_write(struct target *target, struct image *image, uint32_t *written, int erase)
1063 {
1064 return flash_write_unlock(target, image, written, erase, false);
1065 }
1066
1067 int default_flash_mem_blank_check(struct flash_bank *bank)
1068 {
1069 struct target *target = bank->target;
1070 const int buffer_size = 1024;
1071 int i;
1072 uint32_t nBytes;
1073 int retval = ERROR_OK;
1074
1075 if (bank->target->state != TARGET_HALTED)
1076 {
1077 LOG_ERROR("Target not halted");
1078 return ERROR_TARGET_NOT_HALTED;
1079 }
1080
1081 uint8_t *buffer = malloc(buffer_size);
1082
1083 for (i = 0; i < bank->num_sectors; i++)
1084 {
1085 uint32_t j;
1086 bank->sectors[i].is_erased = 1;
1087
1088 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
1089 {
1090 uint32_t chunk;
1091 chunk = buffer_size;
1092 if (chunk > (j - bank->sectors[i].size))
1093 {
1094 chunk = (j - bank->sectors[i].size);
1095 }
1096
1097 retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
1098 if (retval != ERROR_OK)
1099 {
1100 goto done;
1101 }
1102
1103 for (nBytes = 0; nBytes < chunk; nBytes++)
1104 {
1105 if (buffer[nBytes] != 0xFF)
1106 {
1107 bank->sectors[i].is_erased = 0;
1108 break;
1109 }
1110 }
1111 }
1112 }
1113
1114 done:
1115 free(buffer);
1116
1117 return retval;
1118 }
1119
1120 int default_flash_blank_check(struct flash_bank *bank)
1121 {
1122 struct target *target = bank->target;
1123 int i;
1124 int retval;
1125 int fast_check = 0;
1126 uint32_t blank;
1127
1128 if (bank->target->state != TARGET_HALTED)
1129 {
1130 LOG_ERROR("Target not halted");
1131 return ERROR_TARGET_NOT_HALTED;
1132 }
1133
1134 for (i = 0; i < bank->num_sectors; i++)
1135 {
1136 uint32_t address = bank->base + bank->sectors[i].offset;
1137 uint32_t size = bank->sectors[i].size;
1138
1139 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
1140 {
1141 fast_check = 0;
1142 break;
1143 }
1144 if (blank == 0xFF)
1145 bank->sectors[i].is_erased = 1;
1146 else
1147 bank->sectors[i].is_erased = 0;
1148 fast_check = 1;
1149 }
1150
1151 if (!fast_check)
1152 {
1153 LOG_USER("Running slow fallback erase check - add working memory");
1154 return default_flash_mem_blank_check(bank);
1155 }
1156
1157 return ERROR_OK;
1158 }
1159
1160 static const struct command_registration flash_exec_command_handlers[] = {
1161 {
1162 .name = "probe",
1163 .handler = &handle_flash_probe_command,
1164 .mode = COMMAND_EXEC,
1165 .usage = "<bank>",
1166 .help = "identify flash bank",
1167 },
1168 {
1169 .name = "info",
1170 .handler = &handle_flash_info_command,
1171 .mode = COMMAND_EXEC,
1172 .usage = "<bank>",
1173 .help = "print bank information",
1174 },
1175 {
1176 .name = "erase_check",
1177 .handler = &handle_flash_erase_check_command,
1178 .mode = COMMAND_EXEC,
1179 .usage = "<bank>",
1180 .help = "check erase state of sectors",
1181 },
1182 {
1183 .name = "protect_check",
1184 .handler = &handle_flash_protect_check_command,
1185 .mode = COMMAND_EXEC,
1186 .usage = "<bank>",
1187 .help = "check protection state of sectors",
1188 },
1189 {
1190 .name = "erase_sector",
1191 .handler = &handle_flash_erase_command,
1192 .mode = COMMAND_EXEC,
1193 .usage = "<bank> <first> <last>",
1194 .help = "erase sectors",
1195 },
1196 {
1197 .name = "erase_address",
1198 .handler = &handle_flash_erase_address_command,
1199 .mode = COMMAND_EXEC,
1200 .usage = "<address> <length>",
1201 .help = "erase address range",
1202
1203 },
1204 {
1205 .name = "fillw",
1206 .handler = &handle_flash_fill_command,
1207 .mode = COMMAND_EXEC,
1208 .usage = "<bank> <address> <word_pattern> <count>",
1209 .help = "fill with pattern (no autoerase)",
1210 },
1211 {
1212 .name = "fillh",
1213 .handler = &handle_flash_fill_command,
1214 .mode = COMMAND_EXEC,
1215 .usage = "<bank> <address> <halfword_pattern> <count>",
1216 .help = "fill with pattern",
1217 },
1218 {
1219 .name = "fillb",
1220 .handler = &handle_flash_fill_command,
1221 .mode = COMMAND_EXEC,
1222 .usage = "<bank> <address> <byte_pattern> <count>",
1223 .help = "fill with pattern",
1224
1225 },
1226 {
1227 .name = "write_bank",
1228 .handler = &handle_flash_write_bank_command,
1229 .mode = COMMAND_EXEC,
1230 .usage = "<bank> <file> <offset>",
1231 .help = "write binary data",
1232 },
1233 {
1234 .name = "write_image",
1235 .handler = &handle_flash_write_image_command,
1236 .mode = COMMAND_EXEC,
1237 .usage = "<bank> [erase] [unlock] <file> [offset] [type]",
1238 .help = "write an image to flash"
1239 },
1240 {
1241 .name = "protect",
1242 .handler = &handle_flash_protect_command,
1243 .mode = COMMAND_EXEC,
1244 .usage = "<bank> <first> <last> <on | off>",
1245 .help = "set protection of sectors",
1246 },
1247 COMMAND_REGISTRATION_DONE
1248 };
1249
1250 int flash_init_drivers(struct command_context *cmd_ctx)
1251 {
1252 if (!flash_banks)
1253 return ERROR_OK;
1254
1255 struct command *parent = command_find_in_context(cmd_ctx, "flash");
1256 return register_commands(cmd_ctx, parent, flash_exec_command_handlers);
1257 }
1258

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)