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

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)