- added autoprobe functionality
[openocd.git] / src / flash / flash.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "flash.h"
25 #include "command.h"
26 #include "target.h"
27 #include "time_support.h"
28 #include "fileio.h"
29 #include "image.h"
30 #include "log.h"
31
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <inttypes.h>
39
40 /* command handlers */
41 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
42 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
43 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
55
56 /* flash drivers
57 */
58 extern flash_driver_t lpc2000_flash;
59 extern flash_driver_t cfi_flash;
60 extern flash_driver_t at91sam7_flash;
61 extern flash_driver_t str7x_flash;
62 extern flash_driver_t str9x_flash;
63 extern flash_driver_t stellaris_flash;
64 extern flash_driver_t str9xpec_flash;
65 extern flash_driver_t stm32x_flash;
66 extern flash_driver_t tms470_flash;
67
68 flash_driver_t *flash_drivers[] =
69 {
70 &lpc2000_flash,
71 &cfi_flash,
72 &at91sam7_flash,
73 &str7x_flash,
74 &str9x_flash,
75 &stellaris_flash,
76 &str9xpec_flash,
77 &stm32x_flash,
78 &tms470_flash,
79 NULL,
80 };
81
82 flash_bank_t *flash_banks;
83 static command_t *flash_cmd;
84 static int auto_erase = 0;
85
86 int flash_register_commands(struct command_context_s *cmd_ctx)
87 {
88 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
89
90 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
91
92 return ERROR_OK;
93 }
94
95 int flash_init_drivers(struct command_context_s *cmd_ctx)
96 {
97 if (flash_banks)
98 {
99 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
100 "list configured flash banks ");
101 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
102 "print info about flash bank <num>");
103 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
104 "identify flash bank <num>");
105 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
106 "check erase state of sectors in flash bank <num>");
107 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
108 "check protection state of sectors in flash bank <num>");
109 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
110 "DEPRECATED, use 'erase_sector' instead");
111 register_command(cmd_ctx, flash_cmd, "erase_sector", handle_flash_erase_command, COMMAND_EXEC,
112 "erase sectors at <bank> <first> <last>");
113 register_command(cmd_ctx, flash_cmd, "erase_address", handle_flash_erase_address_command, COMMAND_EXEC,
114 "erase address range <address> <length>");
115 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_binary_command, COMMAND_EXEC,
116 "DEPRECATED, use 'write_binary' instead");
117 register_command(cmd_ctx, flash_cmd, "write_binary", handle_flash_write_binary_command, COMMAND_EXEC,
118 "write binary <bank> <file> <offset>");
119 register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
120 "write_image <file> [offset] [type]");
121 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
122 "set protection of sectors at <bank> <first> <last> <on|off>");
123 register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_EXEC,
124 "auto erase flash sectors <on|off>");
125 }
126
127 return ERROR_OK;
128 }
129
130 flash_bank_t *get_flash_bank_by_num_noprobe(int num)
131 {
132 flash_bank_t *p;
133 int i = 0;
134
135 for (p = flash_banks; p; p = p->next)
136 {
137 if (i++ == num)
138 {
139 return p;
140 }
141 }
142
143 return NULL;
144 }
145
146 flash_bank_t *get_flash_bank_by_num(int num)
147 {
148 flash_bank_t *p = get_flash_bank_by_num_noprobe(num);
149 int retval;
150
151 if (p == NULL)
152 return NULL;
153
154 retval = p->driver->auto_probe(p);
155
156 if (retval != ERROR_OK)
157 {
158 ERROR("auto_probe failed %d\n", retval);
159 return NULL;
160 }
161 return p;
162 }
163
164 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]
165 */
166 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
167 {
168 int i;
169 int found = 0;
170 target_t *target;
171
172 if (argc < 6)
173 {
174 WARNING("incomplete flash_bank configuration");
175 WARNING("flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
176 return ERROR_OK;
177 }
178
179 if ((target = get_target_by_num(strtoul(args[5], NULL, 0))) == NULL)
180 {
181 ERROR("target %lu not defined", strtoul(args[5], NULL, 0));
182 return ERROR_OK;
183 }
184
185 for (i = 0; flash_drivers[i]; i++)
186 {
187 if (strcmp(args[0], flash_drivers[i]->name) == 0)
188 {
189 flash_bank_t *p, *c;
190
191 /* register flash specific commands */
192 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
193 {
194 ERROR("couldn't register '%s' commands", args[0]);
195 exit(-1);
196 }
197
198 c = malloc(sizeof(flash_bank_t));
199 c->target = target;
200 c->driver = flash_drivers[i];
201 c->driver_priv = NULL;
202 c->base = strtoul(args[1], NULL, 0);
203 c->size = strtoul(args[2], NULL, 0);
204 c->chip_width = strtoul(args[3], NULL, 0);
205 c->bus_width = strtoul(args[4], NULL, 0);
206 c->num_sectors = 0;
207 c->sectors = NULL;
208 c->next = NULL;
209
210 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
211 {
212 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
213 free(c);
214 return ERROR_OK;
215 }
216
217 /* put flash bank in linked list */
218 if (flash_banks)
219 {
220 /* find last flash bank */
221 for (p = flash_banks; p && p->next; p = p->next);
222 if (p)
223 p->next = c;
224 }
225 else
226 {
227 flash_banks = c;
228 }
229
230 found = 1;
231 }
232 }
233
234 /* no matching flash driver found */
235 if (!found)
236 {
237 ERROR("flash driver '%s' not found", args[0]);
238 exit(-1);
239 }
240
241 return ERROR_OK;
242 }
243
244 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
245 {
246 flash_bank_t *p;
247 int i = 0;
248
249 if (!flash_banks)
250 {
251 command_print(cmd_ctx, "no flash banks configured");
252 return ERROR_OK;
253 }
254
255 for (p = flash_banks; p; p = p->next)
256 {
257 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
258 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
259 }
260
261 return ERROR_OK;
262 }
263
264 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
265 {
266 flash_bank_t *p;
267 int i = 0;
268 int j = 0;
269
270 if (argc != 1)
271 {
272 command_print(cmd_ctx, "usage: flash info <num>");
273 return ERROR_OK;
274 }
275
276 for (p = flash_banks; p; p = p->next, i++)
277 {
278 if (i == strtoul(args[0], NULL, 0))
279 {
280 char buf[1024];
281
282 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
283 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
284 for (j = 0; j < p->num_sectors; j++)
285 {
286 char *erase_state, *protect_state;
287
288 if (p->sectors[j].is_erased == 0)
289 erase_state = "not erased";
290 else if (p->sectors[j].is_erased == 1)
291 erase_state = "erased";
292 else
293 erase_state = "erase state unknown";
294
295 if (p->sectors[j].is_protected == 0)
296 protect_state = "not protected";
297 else if (p->sectors[j].is_protected == 1)
298 protect_state = "protected";
299 else
300 protect_state = "protection state unknown";
301
302 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s, %s",
303 j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
304 erase_state, protect_state);
305 }
306
307 p->driver->info(p, buf, 1024);
308 command_print(cmd_ctx, "%s", buf);
309 }
310 }
311
312 return ERROR_OK;
313 }
314
315 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
316 {
317 flash_bank_t *p;
318 int retval;
319
320 if (argc != 1)
321 {
322 command_print(cmd_ctx, "usage: flash probe <num>");
323 return ERROR_OK;
324 }
325
326 p = get_flash_bank_by_num_noprobe(strtoul(args[0], NULL, 0));
327 if (p)
328 {
329 if ((retval = p->driver->probe(p)) == ERROR_OK)
330 {
331 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
332 }
333 else if (retval == ERROR_FLASH_BANK_INVALID)
334 {
335 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
336 args[0], p->base);
337 }
338 else
339 {
340 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
341 args[0], p->base);
342 }
343 }
344 else
345 {
346 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
347 }
348
349 return ERROR_OK;
350 }
351
352 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
353 {
354 flash_bank_t *p;
355 int retval;
356
357 if (argc != 1)
358 {
359 command_print(cmd_ctx, "usage: flash erase_check <num>");
360 return ERROR_OK;
361 }
362
363 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
364 if (p)
365 {
366 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
367 {
368 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
369 }
370 else
371 {
372 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
373 args[0], p->base);
374 }
375 }
376 else
377 {
378 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
379 }
380
381 return ERROR_OK;
382 }
383
384 int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
385 {
386 flash_bank_t *p;
387 int retval;
388 int address;
389 int length;
390 duration_t duration;
391 char *duration_text;
392
393 target_t *target = get_current_target(cmd_ctx);
394
395 if (argc != 2)
396 {
397 command_print(cmd_ctx, "usage: flash erase_address <address> <length>");
398 return ERROR_OK;
399 }
400
401 address = strtoul(args[0], NULL, 0);
402 length = strtoul(args[1], NULL, 0);
403 if (length <= 0)
404 {
405 command_print(cmd_ctx, "Length must be >0");
406 return ERROR_INVALID_ARGUMENTS;
407 }
408
409 p = get_flash_bank_by_addr(target, address);
410 if (p == NULL)
411 {
412 command_print(cmd_ctx, "No flash at that address");
413 return ERROR_INVALID_ARGUMENTS;
414 }
415
416 /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
417 flash_set_dirty();
418
419 duration_start_measure(&duration);
420
421 if ((retval = flash_erase_address_range(target, address, length)) != ERROR_OK)
422 {
423 switch (retval)
424 {
425 case ERROR_TARGET_NOT_HALTED:
426 command_print(cmd_ctx, "can't work with this flash while target is running");
427 break;
428 case ERROR_INVALID_ARGUMENTS:
429 command_print(cmd_ctx, "usage: flash erase_address <address> <length>");
430 break;
431 case ERROR_FLASH_BANK_INVALID:
432 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
433 break;
434 case ERROR_FLASH_OPERATION_FAILED:
435 command_print(cmd_ctx, "flash erase error");
436 break;
437 case ERROR_FLASH_SECTOR_INVALID:
438 command_print(cmd_ctx, "sector number(s) invalid");
439 break;
440 default:
441 command_print(cmd_ctx, "unknown error");
442 }
443 }
444 else
445 {
446 duration_stop_measure(&duration, &duration_text);
447 command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
448 free(duration_text);
449 }
450
451 return retval;
452 }
453
454 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
455 {
456 flash_bank_t *p;
457 int retval;
458
459 if (argc != 1)
460 {
461 command_print(cmd_ctx, "usage: flash protect_check <num>");
462 return ERROR_OK;
463 }
464
465 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
466 if (p)
467 {
468 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
469 {
470 command_print(cmd_ctx, "successfully checked protect state");
471 }
472 else if (retval == ERROR_FLASH_OPERATION_FAILED)
473 {
474 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
475 }
476 else
477 {
478 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
479 }
480 }
481 else
482 {
483 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
484 }
485
486 return ERROR_OK;
487 }
488
489 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
490 {
491 if (argc > 2)
492 {
493 int first = strtoul(args[1], NULL, 0);
494 int last = strtoul(args[2], NULL, 0);
495 int retval;
496 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
497 duration_t duration;
498 char *duration_text;
499
500 duration_start_measure(&duration);
501
502 if (!p)
503 {
504 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
505 return ERROR_OK;
506 }
507
508 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
509 {
510 switch (retval)
511 {
512 case ERROR_TARGET_NOT_HALTED:
513 command_print(cmd_ctx, "can't work with this flash while target is running");
514 break;
515 case ERROR_INVALID_ARGUMENTS:
516 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
517 break;
518 case ERROR_FLASH_BANK_INVALID:
519 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
520 break;
521 case ERROR_FLASH_OPERATION_FAILED:
522 command_print(cmd_ctx, "flash erase error");
523 break;
524 case ERROR_FLASH_SECTOR_INVALID:
525 command_print(cmd_ctx, "sector number(s) invalid");
526 break;
527 case ERROR_OK:
528 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
529 break;
530 default:
531 command_print(cmd_ctx, "unknown error");
532 }
533 }
534 else
535 {
536 duration_stop_measure(&duration, &duration_text);
537
538 command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
539 free(duration_text);
540 }
541 }
542 else
543 {
544 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
545 }
546
547 return ERROR_OK;
548 }
549
550 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
551 {
552 if (argc > 3)
553 {
554 int first = strtoul(args[1], NULL, 0);
555 int last = strtoul(args[2], NULL, 0);
556 int set;
557 int retval;
558 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
559 if (!p)
560 {
561 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
562 return ERROR_OK;
563 }
564
565 if (strcmp(args[3], "on") == 0)
566 set = 1;
567 else if (strcmp(args[3], "off") == 0)
568 set = 0;
569 else
570 {
571 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
572 return ERROR_OK;
573 }
574
575 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
576 {
577 switch (retval)
578 {
579 case ERROR_TARGET_NOT_HALTED:
580 command_print(cmd_ctx, "can't work with this flash while target is running");
581 break;
582 case ERROR_INVALID_ARGUMENTS:
583 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
584 break;
585 case ERROR_FLASH_BANK_INVALID:
586 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
587 break;
588 case ERROR_FLASH_OPERATION_FAILED:
589 command_print(cmd_ctx, "flash program error");
590 break;
591 case ERROR_FLASH_SECTOR_INVALID:
592 command_print(cmd_ctx, "sector number(s) invalid");
593 break;
594 case ERROR_OK:
595 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
596 break;
597 default:
598 command_print(cmd_ctx, "unknown error");
599 }
600 }
601 else
602 {
603 command_print(cmd_ctx, "%s protection for sectors %i through %i on flash bank %i", (set) ? "set" : "cleared", first, last, strtoul(args[0], 0, 0));
604 }
605 }
606 else
607 {
608 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
609 }
610
611 return ERROR_OK;
612 }
613
614 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
615 {
616 target_t *target = get_current_target(cmd_ctx);
617
618 image_t image;
619 u32 written;
620 char *error_str;
621 int *failed;
622
623 int i;
624
625 duration_t duration;
626 char *duration_text;
627
628 int retval;
629
630 if (argc < 1)
631 {
632 command_print(cmd_ctx, "usage: flash %s <file> [offset] [type]", cmd);
633 return ERROR_INVALID_ARGUMENTS;
634 }
635
636 if (!target)
637 {
638 ERROR("no target selected");
639 return ERROR_OK;
640 }
641
642 duration_start_measure(&duration);
643
644 if (argc >= 2)
645 {
646 image.base_address_set = 1;
647 image.base_address = strtoul(args[1], NULL, 0);
648 }
649 else
650 {
651 image.base_address_set = 0;
652 image.base_address = 0x0;
653 }
654
655 image.start_address_set = 0;
656
657 retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
658 if (retval != ERROR_OK)
659 {
660 command_print(cmd_ctx, "image_open error: %s", image.error_str);
661 return retval;
662 }
663
664 failed = malloc(sizeof(int) * image.num_sections);
665
666 error_str = NULL;
667
668 retval = flash_write(target, &image, &written, &error_str, failed, auto_erase);
669
670 if (retval != ERROR_OK)
671 {
672 if (error_str)
673 {
674 command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
675 free(error_str);
676 }
677 image_close(&image);
678 free(failed);
679 return retval;
680 }
681
682 for (i = 0; i < image.num_sections; i++)
683 {
684 if (failed[i])
685 {
686 command_print(cmd_ctx, "didn't write section at 0x%8.8x, size 0x%8.8x",
687 image.sections[i].base_address, image.sections[i].size);
688 }
689 }
690
691 duration_stop_measure(&duration, &duration_text);
692 command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
693 written, args[0], duration_text,
694 (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
695 free(duration_text);
696 free(failed);
697
698 image_close(&image);
699
700 return ERROR_OK;
701 }
702
703 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
704 {
705 u32 offset;
706 u8 *buffer;
707 u32 buf_cnt;
708
709 fileio_t fileio;
710
711 duration_t duration;
712 char *duration_text;
713
714 int retval;
715 flash_bank_t *p;
716
717 if (argc < 3)
718 {
719 command_print(cmd_ctx, "usage: flash write_binary <bank> <file> <offset>");
720 return ERROR_OK;
721 }
722
723 duration_start_measure(&duration);
724
725 offset = strtoul(args[2], NULL, 0);
726 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
727 if (!p)
728 {
729 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
730 return ERROR_OK;
731 }
732
733 if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
734 {
735 command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
736 return ERROR_OK;
737 }
738
739 buffer = malloc(fileio.size);
740 if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
741 {
742 command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
743 return ERROR_OK;
744 }
745
746 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
747 {
748 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
749 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
750
751 switch (retval)
752 {
753 case ERROR_TARGET_NOT_HALTED:
754 command_print(cmd_ctx, "can't work with this flash while target is running");
755 break;
756 case ERROR_INVALID_ARGUMENTS:
757 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
758 break;
759 case ERROR_FLASH_BANK_INVALID:
760 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
761 break;
762 case ERROR_FLASH_OPERATION_FAILED:
763 command_print(cmd_ctx, "flash program error");
764 break;
765 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
766 command_print(cmd_ctx, "offset breaks required alignment");
767 break;
768 case ERROR_FLASH_DST_OUT_OF_BANK:
769 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
770 break;
771 case ERROR_FLASH_SECTOR_NOT_ERASED:
772 command_print(cmd_ctx, "destination sector(s) not erased");
773 break;
774 default:
775 command_print(cmd_ctx, "unknown error");
776 }
777 }
778
779 free(buffer);
780
781 duration_stop_measure(&duration, &duration_text);
782 command_print(cmd_ctx, "wrote %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
783 fileio.size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
784 (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
785 free(duration_text);
786
787 fileio_close(&fileio);
788
789 return ERROR_OK;
790 }
791
792 void flash_set_dirty(void)
793 {
794 flash_bank_t *c;
795 int i;
796
797 /* set all flash to require erasing */
798 for (c = flash_banks; c; c = c->next)
799 {
800 for (i = 0; i < c->num_sectors; i++)
801 {
802 c->sectors[i].is_erased = 0;
803 }
804 }
805 }
806
807 /* lookup flash bank by address */
808 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
809 {
810 flash_bank_t *c;
811
812 /* cycle through bank list */
813 for (c = flash_banks; c; c = c->next)
814 {
815 int retval;
816 retval = c->driver->auto_probe(c);
817
818 if (retval != ERROR_OK)
819 {
820 ERROR("auto_probe failed %d\n", retval);
821 return NULL;
822 }
823 /* check whether address belongs to this flash bank */
824 if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
825 return c;
826 }
827
828 return NULL;
829 }
830
831 /* erase given flash region, selects proper bank according to target and address */
832 int flash_erase_address_range(target_t *target, u32 addr, u32 length)
833 {
834 flash_bank_t *c;
835 int first = -1;
836 int last = -1;
837 int i;
838
839 if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
840 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
841
842 if (c->size == 0 || c->num_sectors == 0)
843 return ERROR_FLASH_BANK_INVALID;
844
845 if (length == 0)
846 {
847 /* special case, erase whole bank when length is zero */
848 if (addr != c->base)
849 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
850
851 return c->driver->erase(c, 0, c->num_sectors - 1);
852 }
853
854 /* check whether it fits */
855 if (addr + length > c->base + c->size)
856 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
857
858 addr -= c->base;
859
860 for (i = 0; i < c->num_sectors; i++)
861 {
862 /* check whether sector overlaps with the given range and is not yet erased */
863 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
864 /* if first is not set yet then this is the first sector */
865 if (first == -1)
866 first = i;
867 last = i; /* and it is the last one so far in any case */
868 }
869 }
870
871 if( first == -1 || last == -1 )
872 return ERROR_OK;
873
874 return c->driver->erase(c, first, last);
875 }
876
877 /* write (optional verify) an image to flash memory of the given target */
878 int flash_write(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, int erase)
879 {
880 int retval;
881 int i;
882
883 int section;
884 u32 section_offset;
885 flash_bank_t *c;
886
887 section = 0;
888 section_offset = 0;
889
890 if (written)
891 *written = 0;
892
893 if (failed != NULL)
894 for (i = 0; i < image->num_sections; i++)
895 failed[i] = 0;
896
897 if (erase)
898 {
899 /* assume all sectors need erasing - stops any problems
900 * when flash_write is called multiple times */
901
902 flash_set_dirty();
903 }
904
905 /* loop until we reach end of the image */
906 while (section < image->num_sections)
907 {
908 u32 buffer_size;
909 u8 *buffer;
910 int section_first;
911 int section_last;
912 u32 run_address = image->sections[section].base_address + section_offset;
913 u32 run_size = image->sections[section].size - section_offset;
914
915 if (image->sections[section].size == 0)
916 {
917 WARNING("empty section %d", section);
918 section++;
919 section_offset = 0;
920 continue;
921 }
922
923 /* find the corresponding flash bank */
924 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
925 {
926 if (failed == NULL)
927 {
928 if (error_str == NULL)
929 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
930 *error_str = malloc(FLASH_MAX_ERROR_STR);
931 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
932 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
933 }
934 failed[section] = ERROR_FLASH_DST_OUT_OF_BANK; /* mark the section as failed */
935 section++; /* and skip it */
936 section_offset = 0;
937 continue;
938 }
939
940 /* collect consecutive sections which fall into the same bank */
941 section_first = section;
942 section_last = section;
943 while ((run_address + run_size < c->base + c->size)
944 && (section_last + 1 < image->num_sections))
945 {
946 if (image->sections[section_last + 1].base_address < (run_address + run_size))
947 {
948 DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
949 break;
950 }
951 if (image->sections[section_last + 1].base_address != (run_address + run_size))
952 break;
953 run_size += image->sections[++section_last].size;
954 }
955
956 /* fit the run into bank constraints */
957 if (run_address + run_size > c->base + c->size)
958 run_size = c->base + c->size - run_address;
959
960 /* allocate buffer */
961 buffer = malloc(run_size);
962 buffer_size = 0;
963
964 /* read sections to the buffer */
965 while (buffer_size < run_size)
966 {
967 u32 size_read;
968
969 if (buffer_size - run_size <= image->sections[section].size - section_offset)
970 size_read = buffer_size - run_size;
971 else
972 size_read = image->sections[section].size - section_offset;
973
974 if ((retval = image_read_section(image, section, section_offset,
975 size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
976 {
977 free(buffer);
978
979 if (error_str == NULL)
980 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
981
982 *error_str = malloc(FLASH_MAX_ERROR_STR);
983
984 /* if image_read_section returned an error there's an error string we can pass on */
985 if (retval != ERROR_OK)
986 snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image: %s", image->error_str);
987 else
988 snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image");
989
990 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
991 }
992
993 buffer_size += size_read;
994 section_offset += size_read;
995
996 if (section_offset >= image->sections[section].size)
997 {
998 section++;
999 section_offset = 0;
1000 }
1001 }
1002
1003 retval = ERROR_OK;
1004
1005 if (erase)
1006 {
1007 /* calculate and erase sectors */
1008 retval = flash_erase_address_range( target, run_address, run_size );
1009 }
1010
1011 if (retval == ERROR_OK)
1012 {
1013 /* write flash sectors */
1014 retval = c->driver->write(c, buffer, run_address - c->base, run_size);
1015 }
1016
1017 free(buffer);
1018
1019 if (retval != ERROR_OK)
1020 {
1021 if (error_str == NULL)
1022 return retval; /* abort operation */
1023
1024 *error_str = malloc(FLASH_MAX_ERROR_STR);
1025 switch (retval)
1026 {
1027 case ERROR_TARGET_NOT_HALTED:
1028 snprintf(*error_str, FLASH_MAX_ERROR_STR, "can't flash image while target is running");
1029 break;
1030 case ERROR_INVALID_ARGUMENTS:
1031 snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash driver can't fulfill request");
1032 break;
1033 case ERROR_FLASH_OPERATION_FAILED:
1034 snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash program error");
1035 break;
1036 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
1037 snprintf(*error_str, FLASH_MAX_ERROR_STR, "offset breaks required alignment");
1038 break;
1039 case ERROR_FLASH_DST_OUT_OF_BANK:
1040 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
1041 break;
1042 case ERROR_FLASH_SECTOR_NOT_ERASED:
1043 snprintf(*error_str, FLASH_MAX_ERROR_STR, "destination sector(s) not erased");
1044 break;
1045 default:
1046 snprintf(*error_str, FLASH_MAX_ERROR_STR, "unknown error: %i", retval);
1047 }
1048
1049 return retval; /* abort operation */
1050 }
1051
1052 if (written != NULL)
1053 *written += run_size; /* add run size to total written counter */
1054 }
1055
1056 return ERROR_OK;
1057 }
1058
1059 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1060 {
1061 if (argc != 1)
1062 {
1063 command_print(cmd_ctx, "usage: flash auto_erase <on|off>");
1064 return ERROR_OK;
1065 }
1066
1067 if (strcmp(args[0], "on") == 0)
1068 auto_erase = 1;
1069 else if (strcmp(args[0], "off") == 0)
1070 auto_erase = 0;
1071
1072 return ERROR_OK;
1073 }

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)