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

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)