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

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)