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

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)