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

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)