- merged support for Cortex-M3 from cortex-m3 branch (thanks to Magnus Lundin)
[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 "log.h"
27 #include "target.h"
28 #include "time_support.h"
29
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36
37 #include <fileio.h>
38 #include <image.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_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50
51 /* flash drivers
52 */
53 extern flash_driver_t lpc2000_flash;
54 extern flash_driver_t cfi_flash;
55 extern flash_driver_t at91sam7_flash;
56 extern flash_driver_t str7x_flash;
57 extern flash_driver_t str9x_flash;
58 extern flash_driver_t stellaris_flash;
59
60 flash_driver_t *flash_drivers[] =
61 {
62 &lpc2000_flash,
63 &cfi_flash,
64 &at91sam7_flash,
65 &str7x_flash,
66 &str9x_flash,
67 &stellaris_flash,
68 NULL,
69 };
70
71 flash_bank_t *flash_banks;
72 static command_t *flash_cmd;
73
74 int flash_register_commands(struct command_context_s *cmd_ctx)
75 {
76 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
77
78 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
79
80 return ERROR_OK;
81 }
82
83 int flash_init(struct command_context_s *cmd_ctx)
84 {
85 if (flash_banks)
86 {
87 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
88 "list configured flash banks ");
89 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
90 "print info about flash bank <num>");
91 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
92 "identify flash bank <num>");
93 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
94 "check erase state of sectors in flash bank <num>");
95 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
96 "check protection state of sectors in flash bank <num>");
97 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
98 "erase sectors at <bank> <first> <last>");
99 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
100 "write binary <bank> <file> <offset>");
101 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
102 "set protection of sectors at <bank> <first> <last> <on|off>");
103 }
104
105 return ERROR_OK;
106 }
107
108 flash_bank_t *get_flash_bank_by_num(int num)
109 {
110 flash_bank_t *p;
111 int i = 0;
112
113 for (p = flash_banks; p; p = p->next)
114 {
115 if (i++ == num)
116 {
117 return p;
118 }
119 }
120
121 return NULL;
122 }
123
124 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
125 */
126 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
127 {
128 int i;
129 int found = 0;
130
131 if (argc < 5)
132 {
133 WARNING("incomplete flash_bank configuration");
134 return ERROR_OK;
135 }
136
137 for (i = 0; flash_drivers[i]; i++)
138 {
139 if (strcmp(args[0], flash_drivers[i]->name) == 0)
140 {
141 flash_bank_t *p, *c;
142
143 /* register flash specific commands */
144 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
145 {
146 ERROR("couldn't register '%s' commands", args[0]);
147 exit(-1);
148 }
149
150 c = malloc(sizeof(flash_bank_t));
151 c->driver = flash_drivers[i];
152 c->driver_priv = NULL;
153 c->base = strtoul(args[1], NULL, 0);
154 c->size = strtoul(args[2], NULL, 0);
155 c->chip_width = strtoul(args[3], NULL, 0);
156 c->bus_width = strtoul(args[4], NULL, 0);
157 c->next = NULL;
158
159 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
160 {
161 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
162 free(c);
163 return ERROR_OK;
164 }
165
166 /* put flash bank in linked list */
167 if (flash_banks)
168 {
169 /* find last flash bank */
170 for (p = flash_banks; p && p->next; p = p->next);
171 if (p)
172 p->next = c;
173 }
174 else
175 {
176 flash_banks = c;
177 }
178
179 found = 1;
180 }
181 }
182
183 /* no matching flash driver found */
184 if (!found)
185 {
186 ERROR("flash driver '%s' not found", args[0]);
187 exit(-1);
188 }
189
190 return ERROR_OK;
191 }
192
193 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
194 {
195 flash_bank_t *p;
196 int i = 0;
197
198 if (!flash_banks)
199 {
200 command_print(cmd_ctx, "no flash banks configured");
201 return ERROR_OK;
202 }
203
204 for (p = flash_banks; p; p = p->next)
205 {
206 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
207 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
208 }
209
210 return ERROR_OK;
211 }
212
213 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
214 {
215 flash_bank_t *p;
216 int i = 0;
217 int j = 0;
218
219 if (argc != 1)
220 {
221 command_print(cmd_ctx, "usage: flash info <num>");
222 return ERROR_OK;
223 }
224
225 for (p = flash_banks; p; p = p->next)
226 {
227 if (i++ == strtoul(args[0], NULL, 0))
228 {
229 char buf[1024];
230
231 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
232 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
233 for (j = 0; j < p->num_sectors; j++)
234 {
235 char *erase_state, *protect_state;
236
237 if (p->sectors[j].is_erased == 0)
238 erase_state = "not erased";
239 else if (p->sectors[j].is_erased == 1)
240 erase_state = "erased";
241 else
242 erase_state = "erase state unknown";
243
244 if (p->sectors[j].is_protected == 0)
245 protect_state = "not protected";
246 else if (p->sectors[j].is_protected == 1)
247 protect_state = "protected";
248 else
249 protect_state = "protection state unknown";
250
251 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
252 j, p->sectors[j].offset, p->sectors[j].size,
253 erase_state, protect_state);
254 }
255
256 p->driver->info(p, buf, 1024);
257 command_print(cmd_ctx, "%s", buf);
258 }
259 }
260
261 return ERROR_OK;
262 }
263
264 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
265 {
266 flash_bank_t *p;
267 int retval;
268
269 if (argc != 1)
270 {
271 command_print(cmd_ctx, "usage: flash probe <num>");
272 return ERROR_OK;
273 }
274
275 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
276 if (p)
277 {
278 if ((retval = p->driver->probe(p)) == ERROR_OK)
279 {
280 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
281 }
282 else if (retval == ERROR_FLASH_BANK_INVALID)
283 {
284 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
285 args[0], p->base);
286 }
287 else
288 {
289 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
290 args[0], p->base);
291 }
292 }
293 else
294 {
295 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
296 }
297
298 return ERROR_OK;
299 }
300
301 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
302 {
303 flash_bank_t *p;
304 int retval;
305
306 if (argc != 1)
307 {
308 command_print(cmd_ctx, "usage: flash erase_check <num>");
309 return ERROR_OK;
310 }
311
312 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
313 if (p)
314 {
315 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
316 {
317 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
318 }
319 else
320 {
321 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
322 args[0], p->base);
323 }
324 }
325 else
326 {
327 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
328 }
329
330 return ERROR_OK;
331 }
332
333 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
334 {
335 flash_bank_t *p;
336 int retval;
337
338 if (argc != 1)
339 {
340 command_print(cmd_ctx, "usage: flash protect_check <num>");
341 return ERROR_OK;
342 }
343
344 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
345 if (p)
346 {
347 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
348 {
349 command_print(cmd_ctx, "successfully checked protect state");
350 }
351 else if (retval == ERROR_FLASH_OPERATION_FAILED)
352 {
353 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
354 }
355 else
356 {
357 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
358 }
359 }
360 else
361 {
362 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
363 }
364
365 return ERROR_OK;
366 }
367
368 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
369 {
370 if (argc > 2)
371 {
372 int first = strtoul(args[1], NULL, 0);
373 int last = strtoul(args[2], NULL, 0);
374 int retval;
375 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
376 struct timeval start, end, duration;
377
378 gettimeofday(&start, NULL);
379
380 if (!p)
381 {
382 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
383 return ERROR_OK;
384 }
385
386 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
387 {
388 switch (retval)
389 {
390 case ERROR_TARGET_NOT_HALTED:
391 command_print(cmd_ctx, "can't work with this flash while target is running");
392 break;
393 case ERROR_INVALID_ARGUMENTS:
394 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
395 break;
396 case ERROR_FLASH_BANK_INVALID:
397 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
398 break;
399 case ERROR_FLASH_OPERATION_FAILED:
400 command_print(cmd_ctx, "flash erase error");
401 break;
402 case ERROR_FLASH_SECTOR_INVALID:
403 command_print(cmd_ctx, "sector number(s) invalid");
404 break;
405 case ERROR_OK:
406 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
407 break;
408 default:
409 command_print(cmd_ctx, "unknown error");
410 }
411 }
412 else
413 {
414 gettimeofday(&end, NULL);
415 timeval_subtract(&duration, &end, &start);
416
417 command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %is %ius", first, last, strtoul(args[0], 0, 0), duration.tv_sec, duration.tv_usec);
418 }
419 }
420 else
421 {
422 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
423 }
424
425 return ERROR_OK;
426 }
427
428 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
429 {
430 if (argc > 3)
431 {
432 int first = strtoul(args[1], NULL, 0);
433 int last = strtoul(args[2], NULL, 0);
434 int set;
435 int retval;
436 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
437 if (!p)
438 {
439 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
440 return ERROR_OK;
441 }
442
443 if (strcmp(args[3], "on") == 0)
444 set = 1;
445 else if (strcmp(args[3], "off") == 0)
446 set = 0;
447 else
448 {
449 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
450 return ERROR_OK;
451 }
452
453 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
454 {
455 switch (retval)
456 {
457 case ERROR_TARGET_NOT_HALTED:
458 command_print(cmd_ctx, "can't work with this flash while target is running");
459 break;
460 case ERROR_INVALID_ARGUMENTS:
461 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
462 break;
463 case ERROR_FLASH_BANK_INVALID:
464 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
465 break;
466 case ERROR_FLASH_OPERATION_FAILED:
467 command_print(cmd_ctx, "flash program error");
468 break;
469 case ERROR_FLASH_SECTOR_INVALID:
470 command_print(cmd_ctx, "sector number(s) invalid");
471 break;
472 case ERROR_OK:
473 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
474 break;
475 default:
476 command_print(cmd_ctx, "unknown error");
477 }
478 }
479 else
480 {
481 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));
482 }
483 }
484 else
485 {
486 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
487 }
488
489 return ERROR_OK;
490 }
491
492 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
493 {
494 u32 offset;
495 u8 *buffer;
496 u32 buf_cnt;
497 u32 image_size;
498 int i;
499
500 image_t image;
501
502 duration_t duration;
503 char *duration_text;
504
505 int retval;
506 flash_bank_t *p;
507
508 if (argc < 3)
509 {
510 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
511 return ERROR_OK;
512 }
513
514 duration_start_measure(&duration);
515
516 image.base_address_set = 1;
517 image.base_address = strtoul(args[1], NULL, 0);
518
519 image.start_address_set = 0;
520
521 offset = strtoul(args[2], NULL, 0);
522 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
523 if (!p)
524 {
525 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
526 return ERROR_OK;
527 }
528
529 if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
530 {
531 command_print(cmd_ctx, "flash write error: %s", image.error_str);
532 return ERROR_OK;
533 }
534
535 image_size = 0x0;
536 for (i = 0; i < image.num_sections; i++)
537 {
538 buffer = malloc(image.sections[i].size);
539 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
540 {
541 ERROR("image_read_section failed with error code: %i", retval);
542 command_print(cmd_ctx, "image reading failed, flash write aborted");
543 free(buffer);
544 image_close(&image);
545 return ERROR_OK;
546 }
547
548 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
549 {
550 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
551 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
552 switch (retval)
553 {
554 case ERROR_TARGET_NOT_HALTED:
555 command_print(cmd_ctx, "can't work with this flash while target is running");
556 break;
557 case ERROR_INVALID_ARGUMENTS:
558 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
559 break;
560 case ERROR_FLASH_BANK_INVALID:
561 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
562 break;
563 case ERROR_FLASH_OPERATION_FAILED:
564 command_print(cmd_ctx, "flash program error");
565 break;
566 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
567 command_print(cmd_ctx, "offset breaks required alignment");
568 break;
569 case ERROR_FLASH_DST_OUT_OF_BANK:
570 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
571 break;
572 case ERROR_FLASH_SECTOR_NOT_ERASED:
573 command_print(cmd_ctx, "destination sector(s) not erased");
574 break;
575 default:
576 command_print(cmd_ctx, "unknown error");
577 }
578 }
579 image_size += buf_cnt;
580
581 free(buffer);
582 }
583
584
585 duration_stop_measure(&duration, &duration_text);
586 command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
587 image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
588 (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
589 free(duration_text);
590
591 image_close(&image);
592
593 return ERROR_OK;
594 }

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)