1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
27 #include "time_support.h"
32 #include <sys/types.h>
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);
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 extern flash_driver_t stm32x_flash;
62 flash_driver_t *flash_drivers[] =
75 flash_bank_t *flash_banks;
76 static command_t *flash_cmd;
78 int flash_register_commands(struct command_context_s *cmd_ctx)
80 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
82 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
87 int flash_init(struct command_context_s *cmd_ctx)
91 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
92 "list configured flash banks ");
93 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
94 "print info about flash bank <num>");
95 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
96 "identify flash bank <num>");
97 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
98 "check erase state of sectors in flash bank <num>");
99 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
100 "check protection state of sectors in flash bank <num>");
101 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
102 "erase sectors at <bank> <first> <last>");
103 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
104 "write binary <bank> <file> <offset>");
105 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
106 "set protection of sectors at <bank> <first> <last> <on|off>");
112 flash_bank_t *get_flash_bank_by_num(int num)
117 for (p = flash_banks; p; p = p->next)
128 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
130 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
137 WARNING("incomplete flash_bank configuration");
141 for (i = 0; flash_drivers[i]; i++)
143 if (strcmp(args[0], flash_drivers[i]->name) == 0)
147 /* register flash specific commands */
148 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
150 ERROR("couldn't register '%s' commands", args[0]);
154 c = malloc(sizeof(flash_bank_t));
155 c->driver = flash_drivers[i];
156 c->driver_priv = NULL;
157 c->base = strtoul(args[1], NULL, 0);
158 c->size = strtoul(args[2], NULL, 0);
159 c->chip_width = strtoul(args[3], NULL, 0);
160 c->bus_width = strtoul(args[4], NULL, 0);
163 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
165 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
170 /* put flash bank in linked list */
173 /* find last flash bank */
174 for (p = flash_banks; p && p->next; p = p->next);
187 /* no matching flash driver found */
190 ERROR("flash driver '%s' not found", args[0]);
197 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
204 command_print(cmd_ctx, "no flash banks configured");
208 for (p = flash_banks; p; p = p->next)
210 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
211 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
217 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
225 command_print(cmd_ctx, "usage: flash info <num>");
229 for (p = flash_banks; p; p = p->next)
231 if (i++ == strtoul(args[0], NULL, 0))
235 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
236 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
237 for (j = 0; j < p->num_sectors; j++)
239 char *erase_state, *protect_state;
241 if (p->sectors[j].is_erased == 0)
242 erase_state = "not erased";
243 else if (p->sectors[j].is_erased == 1)
244 erase_state = "erased";
246 erase_state = "erase state unknown";
248 if (p->sectors[j].is_protected == 0)
249 protect_state = "not protected";
250 else if (p->sectors[j].is_protected == 1)
251 protect_state = "protected";
253 protect_state = "protection state unknown";
255 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
256 j, p->sectors[j].offset, p->sectors[j].size,
257 erase_state, protect_state);
260 p->driver->info(p, buf, 1024);
261 command_print(cmd_ctx, "%s", buf);
268 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
275 command_print(cmd_ctx, "usage: flash probe <num>");
279 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
282 if ((retval = p->driver->probe(p)) == ERROR_OK)
284 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
286 else if (retval == ERROR_FLASH_BANK_INVALID)
288 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
293 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
299 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
305 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
312 command_print(cmd_ctx, "usage: flash erase_check <num>");
316 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
319 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
321 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
325 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
331 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
337 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
344 command_print(cmd_ctx, "usage: flash protect_check <num>");
348 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
351 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
353 command_print(cmd_ctx, "successfully checked protect state");
355 else if (retval == ERROR_FLASH_OPERATION_FAILED)
357 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
361 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
366 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
372 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
376 int first = strtoul(args[1], NULL, 0);
377 int last = strtoul(args[2], NULL, 0);
379 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
383 duration_start_measure(&duration);
387 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
391 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
395 case ERROR_TARGET_NOT_HALTED:
396 command_print(cmd_ctx, "can't work with this flash while target is running");
398 case ERROR_INVALID_ARGUMENTS:
399 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
401 case ERROR_FLASH_BANK_INVALID:
402 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
404 case ERROR_FLASH_OPERATION_FAILED:
405 command_print(cmd_ctx, "flash erase error");
407 case ERROR_FLASH_SECTOR_INVALID:
408 command_print(cmd_ctx, "sector number(s) invalid");
411 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
414 command_print(cmd_ctx, "unknown error");
419 duration_stop_measure(&duration, &duration_text);
421 command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
427 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
433 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
437 int first = strtoul(args[1], NULL, 0);
438 int last = strtoul(args[2], NULL, 0);
441 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
444 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
448 if (strcmp(args[3], "on") == 0)
450 else if (strcmp(args[3], "off") == 0)
454 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
458 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
462 case ERROR_TARGET_NOT_HALTED:
463 command_print(cmd_ctx, "can't work with this flash while target is running");
465 case ERROR_INVALID_ARGUMENTS:
466 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
468 case ERROR_FLASH_BANK_INVALID:
469 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
471 case ERROR_FLASH_OPERATION_FAILED:
472 command_print(cmd_ctx, "flash program error");
474 case ERROR_FLASH_SECTOR_INVALID:
475 command_print(cmd_ctx, "sector number(s) invalid");
478 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
481 command_print(cmd_ctx, "unknown error");
486 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));
491 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
497 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
515 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
519 duration_start_measure(&duration);
521 image.base_address_set = 1;
522 image.base_address = strtoul(args[1], NULL, 0);
524 image.start_address_set = 0;
526 offset = strtoul(args[2], NULL, 0);
527 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
530 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
534 if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
536 command_print(cmd_ctx, "flash write error: %s", image.error_str);
541 for (i = 0; i < image.num_sections; i++)
543 buffer = malloc(image.sections[i].size);
544 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
546 ERROR("image_read_section failed with error code: %i", retval);
547 command_print(cmd_ctx, "image reading failed, flash write aborted");
553 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
555 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
556 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
559 case ERROR_TARGET_NOT_HALTED:
560 command_print(cmd_ctx, "can't work with this flash while target is running");
562 case ERROR_INVALID_ARGUMENTS:
563 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
565 case ERROR_FLASH_BANK_INVALID:
566 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
568 case ERROR_FLASH_OPERATION_FAILED:
569 command_print(cmd_ctx, "flash program error");
571 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
572 command_print(cmd_ctx, "offset breaks required alignment");
574 case ERROR_FLASH_DST_OUT_OF_BANK:
575 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
577 case ERROR_FLASH_SECTOR_NOT_ERASED:
578 command_print(cmd_ctx, "destination sector(s) not erased");
581 command_print(cmd_ctx, "unknown error");
584 image_size += buf_cnt;
590 duration_stop_measure(&duration, &duration_text);
591 command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
592 image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
593 (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));