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

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)