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 ***************************************************************************/
32 #include <sys/types.h>
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);
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;
54 flash_driver_t *flash_drivers[] =
63 flash_bank_t *flash_banks;
64 static command_t *flash_cmd;
66 int flash_register_commands(struct command_context_s *cmd_ctx)
68 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
70 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
75 int flash_init(struct command_context_s *cmd_ctx)
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>");
100 flash_bank_t *get_flash_bank_by_num(int num)
105 for (p = flash_banks; p; p = p->next)
116 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
118 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
125 WARNING("incomplete flash_bank configuration");
129 for (i = 0; flash_drivers[i]; i++)
131 if (strcmp(args[0], flash_drivers[i]->name) == 0)
135 /* register flash specific commands */
136 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
138 ERROR("couldn't register '%s' commands", args[0]);
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);
151 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
153 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
158 /* put flash bank in linked list */
161 /* find last flash bank */
162 for (p = flash_banks; p && p->next; p = p->next);
175 /* no matching flash driver found */
178 ERROR("flash driver '%s' not found", args[0]);
185 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
192 command_print(cmd_ctx, "no flash banks configured");
196 for (p = flash_banks; p; p = p->next)
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);
205 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
213 command_print(cmd_ctx, "usage: flash info <num>");
217 for (p = flash_banks; p; p = p->next)
219 if (i++ == strtoul(args[0], NULL, 0))
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++)
227 char *erase_state, *protect_state;
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";
234 erase_state = "erase state unknown";
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";
241 protect_state = "protection state unknown";
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);
248 p->driver->info(p, buf, 1024);
249 command_print(cmd_ctx, "%s", buf);
256 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
263 command_print(cmd_ctx, "usage: flash probe <num>");
267 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
270 if ((retval = p->driver->probe(p)) == ERROR_OK)
272 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
274 else if (retval == ERROR_FLASH_BANK_INVALID)
276 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
281 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
287 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
293 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
300 command_print(cmd_ctx, "usage: flash erase_check <num>");
304 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
307 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
309 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
313 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
319 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
325 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
332 command_print(cmd_ctx, "usage: flash protect_check <num>");
336 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
339 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
341 command_print(cmd_ctx, "successfully checked protect state");
343 else if (retval == ERROR_FLASH_OPERATION_FAILED)
345 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
349 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
354 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
360 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
364 int first = strtoul(args[1], NULL, 0);
365 int last = strtoul(args[2], NULL, 0);
367 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
370 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
374 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
378 case ERROR_TARGET_NOT_HALTED:
379 command_print(cmd_ctx, "can't work with this flash while target is running");
381 case ERROR_INVALID_ARGUMENTS:
382 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
384 case ERROR_FLASH_BANK_INVALID:
385 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
387 case ERROR_FLASH_OPERATION_FAILED:
388 command_print(cmd_ctx, "flash erase error");
390 case ERROR_FLASH_SECTOR_INVALID:
391 command_print(cmd_ctx, "sector number(s) invalid");
394 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
397 command_print(cmd_ctx, "unknown error");
403 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
409 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
413 int first = strtoul(args[1], NULL, 0);
414 int last = strtoul(args[2], NULL, 0);
417 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
420 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
424 if (strcmp(args[3], "on") == 0)
426 else if (strcmp(args[3], "off") == 0)
430 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
434 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
438 case ERROR_TARGET_NOT_HALTED:
439 command_print(cmd_ctx, "can't work with this flash while target is running");
441 case ERROR_INVALID_ARGUMENTS:
442 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
444 case ERROR_FLASH_BANK_INVALID:
445 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
447 case ERROR_FLASH_OPERATION_FAILED:
448 command_print(cmd_ctx, "flash program error");
450 case ERROR_FLASH_SECTOR_INVALID:
451 command_print(cmd_ctx, "sector number(s) invalid");
454 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
457 command_print(cmd_ctx, "unknown error");
463 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
469 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
473 struct stat binary_stat;
482 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
486 offset = strtoul(args[2], NULL, 0);
487 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
490 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
494 if (stat(args[1], &binary_stat) == -1)
496 ERROR("couldn't stat() %s: %s", args[1], strerror(errno));
500 if (S_ISDIR(binary_stat.st_mode))
502 ERROR("%s is a directory", args[1]);
503 command_print(cmd_ctx,"%s is a directory", args[1]);
507 if (binary_stat.st_size == 0){
508 ERROR("Empty file %s", args[1]);
509 command_print(cmd_ctx,"Empty file %s", args[1]);
513 if (!(binary = fopen(args[1], "rb")))
515 ERROR("couldn't open %s: %s", args[1], strerror(errno));
516 command_print(cmd_ctx, "couldn't open %s", args[1]);
520 binary_size = binary_stat.st_size;
521 buffer = malloc(binary_size);
522 buf_cnt = fread(buffer, 1, binary_size, binary);
524 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
528 case ERROR_TARGET_NOT_HALTED:
529 command_print(cmd_ctx, "can't work with this flash while target is running");
531 case ERROR_INVALID_ARGUMENTS:
532 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
534 case ERROR_FLASH_BANK_INVALID:
535 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
537 case ERROR_FLASH_OPERATION_FAILED:
538 command_print(cmd_ctx, "flash program error");
540 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
541 command_print(cmd_ctx, "offset breaks required alignment");
543 case ERROR_FLASH_DST_OUT_OF_BANK:
544 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
546 case ERROR_FLASH_SECTOR_NOT_ERASED:
547 command_print(cmd_ctx, "destination sector(s) not erased");
550 command_print(cmd_ctx, "unknown error");
556 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));