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 ***************************************************************************/
28 #include <sys/types.h>
32 /* command handlers */
33 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
34 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
35 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
36 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
37 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
38 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
39 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
40 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
41 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 extern flash_driver_t lpc2000_flash;
46 extern flash_driver_t cfi_flash;
47 extern flash_driver_t at91sam7_flash;
48 extern flash_driver_t str7x_flash;
50 flash_driver_t *flash_drivers[] =
59 flash_bank_t *flash_banks;
60 static command_t *flash_cmd;
62 int flash_register_commands(struct command_context_s *cmd_ctx)
64 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
66 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
71 int flash_init(struct command_context_s *cmd_ctx)
75 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
76 "list configured flash banks ");
77 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
78 "print info about flash bank <num>");
79 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
80 "identify flash bank <num>");
81 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
82 "check erase state of sectors in flash bank <num>");
83 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
84 "check protection state of sectors in flash bank <num>");
85 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
86 "erase sectors at <bank> <first> <last>");
87 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
88 "write binary <bank> <file> <offset>");
89 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
90 "set protection of sectors at <bank> <first> <last> <on|off>");
96 flash_bank_t *get_flash_bank_by_num(int num)
101 for (p = flash_banks; p; p = p->next)
112 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
114 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
121 WARNING("incomplete flash_bank configuration");
125 for (i = 0; flash_drivers[i]; i++)
127 if (strcmp(args[0], flash_drivers[i]->name) == 0)
131 /* register flash specific commands */
132 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
134 ERROR("couldn't register '%s' commands", args[0]);
138 c = malloc(sizeof(flash_bank_t));
139 c->driver = flash_drivers[i];
140 c->driver_priv = NULL;
141 c->base = strtoul(args[1], NULL, 0);
142 c->size = strtoul(args[2], NULL, 0);
143 c->chip_width = strtoul(args[3], NULL, 0);
144 c->bus_width = strtoul(args[4], NULL, 0);
147 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
149 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
154 /* put flash bank in linked list */
157 /* find last flash bank */
158 for (p = flash_banks; p && p->next; p = p->next);
171 /* no matching flash driver found */
174 ERROR("flash driver '%s' not found", args[0]);
181 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
188 command_print(cmd_ctx, "no flash banks configured");
192 for (p = flash_banks; p; p = p->next)
194 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
195 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
201 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
209 command_print(cmd_ctx, "usage: flash info <num>");
213 for (p = flash_banks; p; p = p->next)
215 if (i++ == strtoul(args[0], NULL, 0))
219 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
220 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
221 for (j = 0; j < p->num_sectors; j++)
223 char *erase_state, *protect_state;
225 if (p->sectors[j].is_erased == 0)
226 erase_state = "not erased";
227 else if (p->sectors[j].is_erased == 1)
228 erase_state = "erased";
230 erase_state = "erase state unknown";
232 if (p->sectors[j].is_protected == 0)
233 protect_state = "not protected";
234 else if (p->sectors[j].is_protected == 1)
235 protect_state = "protected";
237 protect_state = "protection state unknown";
239 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
240 j, p->sectors[j].offset, p->sectors[j].size,
241 erase_state, protect_state);
244 p->driver->info(p, buf, 1024);
245 command_print(cmd_ctx, "%s", buf);
252 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
259 command_print(cmd_ctx, "usage: flash probe <num>");
263 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
266 if ((retval = p->driver->probe(p)) == ERROR_OK)
268 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
270 else if (retval == ERROR_FLASH_BANK_INVALID)
272 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
277 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
283 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
289 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
296 command_print(cmd_ctx, "usage: flash erase_check <num>");
300 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
303 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
305 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
309 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
315 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
321 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
328 command_print(cmd_ctx, "usage: flash protect_check <num>");
332 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
335 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
337 command_print(cmd_ctx, "successfully checked protect state");
339 else if (retval == ERROR_FLASH_OPERATION_FAILED)
341 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
345 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
350 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
356 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
360 int first = strtoul(args[1], NULL, 0);
361 int last = strtoul(args[2], NULL, 0);
363 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
366 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
370 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
374 case ERROR_TARGET_NOT_HALTED:
375 command_print(cmd_ctx, "can't work with this flash while target is running");
377 case ERROR_INVALID_ARGUMENTS:
378 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
380 case ERROR_FLASH_BANK_INVALID:
381 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
383 case ERROR_FLASH_OPERATION_FAILED:
384 command_print(cmd_ctx, "flash erase error");
386 case ERROR_FLASH_SECTOR_INVALID:
387 command_print(cmd_ctx, "sector number(s) invalid");
390 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
393 command_print(cmd_ctx, "unknown error");
399 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
405 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
409 int first = strtoul(args[1], NULL, 0);
410 int last = strtoul(args[2], NULL, 0);
413 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
416 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
420 if (strcmp(args[3], "on") == 0)
422 else if (strcmp(args[3], "off") == 0)
426 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
430 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
434 case ERROR_TARGET_NOT_HALTED:
435 command_print(cmd_ctx, "can't work with this flash while target is running");
437 case ERROR_INVALID_ARGUMENTS:
438 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
440 case ERROR_FLASH_BANK_INVALID:
441 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
443 case ERROR_FLASH_OPERATION_FAILED:
444 command_print(cmd_ctx, "flash program error");
446 case ERROR_FLASH_SECTOR_INVALID:
447 command_print(cmd_ctx, "sector number(s) invalid");
450 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
453 command_print(cmd_ctx, "unknown error");
459 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
465 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
469 struct stat binary_stat;
478 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
482 offset = strtoul(args[2], NULL, 0);
483 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
486 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
490 if (stat(args[1], &binary_stat) == -1)
492 ERROR("couldn't stat() %s: %s", args[1], strerror(errno));
496 if (S_ISDIR(binary_stat.st_mode))
498 ERROR("%s is a directory", args[1]);
499 command_print(cmd_ctx,"%s is a directory", args[1]);
503 if (binary_stat.st_size == 0){
504 ERROR("Empty file %s", args[1]);
505 command_print(cmd_ctx,"Empty file %s", args[1]);
509 if (!(binary = fopen(args[1], "r")))
511 ERROR("couldn't open %s: %s", args[1], strerror(errno));
512 command_print(cmd_ctx, "couldn't open %s", args[1]);
516 binary_size = binary_stat.st_size;
517 buffer = malloc(binary_size);
518 buf_cnt = fread(buffer, 1, binary_size, binary);
520 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
524 case ERROR_TARGET_NOT_HALTED:
525 command_print(cmd_ctx, "can't work with this flash while target is running");
527 case ERROR_INVALID_ARGUMENTS:
528 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
530 case ERROR_FLASH_BANK_INVALID:
531 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
533 case ERROR_FLASH_OPERATION_FAILED:
534 command_print(cmd_ctx, "flash program error");
536 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
537 command_print(cmd_ctx, "offset breaks required alignment");
539 case ERROR_FLASH_DST_OUT_OF_BANK:
540 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
542 case ERROR_FLASH_SECTOR_NOT_ERASED:
543 command_print(cmd_ctx, "destination sector(s) not erased");
546 command_print(cmd_ctx, "unknown error");
552 command_print(cmd_ctx, "wrote file %s to flash bank %i at offset 0x%8.8x", args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));