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 "time_support.h"
33 #include <sys/types.h>
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);
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 extern flash_driver_t str9x_flash;
56 flash_driver_t *flash_drivers[] =
66 flash_bank_t *flash_banks;
67 static command_t *flash_cmd;
69 int flash_register_commands(struct command_context_s *cmd_ctx)
71 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
73 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
78 int flash_init(struct command_context_s *cmd_ctx)
82 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
83 "list configured flash banks ");
84 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
85 "print info about flash bank <num>");
86 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
87 "identify flash bank <num>");
88 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
89 "check erase state of sectors in flash bank <num>");
90 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
91 "check protection state of sectors in flash bank <num>");
92 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
93 "erase sectors at <bank> <first> <last>");
94 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
95 "write binary <bank> <file> <offset>");
96 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
97 "set protection of sectors at <bank> <first> <last> <on|off>");
103 flash_bank_t *get_flash_bank_by_num(int num)
108 for (p = flash_banks; p; p = p->next)
119 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
121 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
128 WARNING("incomplete flash_bank configuration");
132 for (i = 0; flash_drivers[i]; i++)
134 if (strcmp(args[0], flash_drivers[i]->name) == 0)
138 /* register flash specific commands */
139 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
141 ERROR("couldn't register '%s' commands", args[0]);
145 c = malloc(sizeof(flash_bank_t));
146 c->driver = flash_drivers[i];
147 c->driver_priv = NULL;
148 c->base = strtoul(args[1], NULL, 0);
149 c->size = strtoul(args[2], NULL, 0);
150 c->chip_width = strtoul(args[3], NULL, 0);
151 c->bus_width = strtoul(args[4], NULL, 0);
154 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
156 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
161 /* put flash bank in linked list */
164 /* find last flash bank */
165 for (p = flash_banks; p && p->next; p = p->next);
178 /* no matching flash driver found */
181 ERROR("flash driver '%s' not found", args[0]);
188 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
195 command_print(cmd_ctx, "no flash banks configured");
199 for (p = flash_banks; p; p = p->next)
201 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
202 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
208 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
216 command_print(cmd_ctx, "usage: flash info <num>");
220 for (p = flash_banks; p; p = p->next)
222 if (i++ == strtoul(args[0], NULL, 0))
226 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
227 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
228 for (j = 0; j < p->num_sectors; j++)
230 char *erase_state, *protect_state;
232 if (p->sectors[j].is_erased == 0)
233 erase_state = "not erased";
234 else if (p->sectors[j].is_erased == 1)
235 erase_state = "erased";
237 erase_state = "erase state unknown";
239 if (p->sectors[j].is_protected == 0)
240 protect_state = "not protected";
241 else if (p->sectors[j].is_protected == 1)
242 protect_state = "protected";
244 protect_state = "protection state unknown";
246 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
247 j, p->sectors[j].offset, p->sectors[j].size,
248 erase_state, protect_state);
251 p->driver->info(p, buf, 1024);
252 command_print(cmd_ctx, "%s", buf);
259 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
266 command_print(cmd_ctx, "usage: flash probe <num>");
270 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
273 if ((retval = p->driver->probe(p)) == ERROR_OK)
275 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
277 else if (retval == ERROR_FLASH_BANK_INVALID)
279 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
284 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
290 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
296 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
303 command_print(cmd_ctx, "usage: flash erase_check <num>");
307 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
310 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
312 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
316 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
322 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
328 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
335 command_print(cmd_ctx, "usage: flash protect_check <num>");
339 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
342 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
344 command_print(cmd_ctx, "successfully checked protect state");
346 else if (retval == ERROR_FLASH_OPERATION_FAILED)
348 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
352 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
357 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
363 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
367 int first = strtoul(args[1], NULL, 0);
368 int last = strtoul(args[2], NULL, 0);
370 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
371 struct timeval start, end, duration;
373 gettimeofday(&start, NULL);
377 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
381 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
385 case ERROR_TARGET_NOT_HALTED:
386 command_print(cmd_ctx, "can't work with this flash while target is running");
388 case ERROR_INVALID_ARGUMENTS:
389 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
391 case ERROR_FLASH_BANK_INVALID:
392 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
394 case ERROR_FLASH_OPERATION_FAILED:
395 command_print(cmd_ctx, "flash erase error");
397 case ERROR_FLASH_SECTOR_INVALID:
398 command_print(cmd_ctx, "sector number(s) invalid");
401 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
404 command_print(cmd_ctx, "unknown error");
409 gettimeofday(&end, NULL);
410 timeval_subtract(&duration, &end, &start);
412 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);
417 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
423 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
427 int first = strtoul(args[1], NULL, 0);
428 int last = strtoul(args[2], NULL, 0);
431 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
434 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
438 if (strcmp(args[3], "on") == 0)
440 else if (strcmp(args[3], "off") == 0)
444 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
448 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
452 case ERROR_TARGET_NOT_HALTED:
453 command_print(cmd_ctx, "can't work with this flash while target is running");
455 case ERROR_INVALID_ARGUMENTS:
456 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
458 case ERROR_FLASH_BANK_INVALID:
459 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
461 case ERROR_FLASH_OPERATION_FAILED:
462 command_print(cmd_ctx, "flash program error");
464 case ERROR_FLASH_SECTOR_INVALID:
465 command_print(cmd_ctx, "sector number(s) invalid");
468 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
471 command_print(cmd_ctx, "unknown error");
477 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
483 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
487 struct stat binary_stat;
493 struct timeval start, end, duration;
495 gettimeofday(&start, NULL);
499 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
503 offset = strtoul(args[2], NULL, 0);
504 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
507 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
511 if (stat(args[1], &binary_stat) == -1)
513 ERROR("couldn't stat() %s: %s", args[1], strerror(errno));
517 if (S_ISDIR(binary_stat.st_mode))
519 ERROR("%s is a directory", args[1]);
520 command_print(cmd_ctx,"%s is a directory", args[1]);
524 if (binary_stat.st_size == 0){
525 ERROR("Empty file %s", args[1]);
526 command_print(cmd_ctx,"Empty file %s", args[1]);
530 if (!(binary = fopen(args[1], "rb")))
532 ERROR("couldn't open %s: %s", args[1], strerror(errno));
533 command_print(cmd_ctx, "couldn't open %s", args[1]);
537 binary_size = binary_stat.st_size;
538 buffer = malloc(binary_size);
539 buf_cnt = fread(buffer, 1, binary_size, binary);
541 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
543 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
544 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
547 case ERROR_TARGET_NOT_HALTED:
548 command_print(cmd_ctx, "can't work with this flash while target is running");
550 case ERROR_INVALID_ARGUMENTS:
551 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
553 case ERROR_FLASH_BANK_INVALID:
554 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
556 case ERROR_FLASH_OPERATION_FAILED:
557 command_print(cmd_ctx, "flash program error");
559 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
560 command_print(cmd_ctx, "offset breaks required alignment");
562 case ERROR_FLASH_DST_OUT_OF_BANK:
563 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
565 case ERROR_FLASH_SECTOR_NOT_ERASED:
566 command_print(cmd_ctx, "destination sector(s) not erased");
569 command_print(cmd_ctx, "unknown error");
574 gettimeofday(&end, NULL);
575 timeval_subtract(&duration, &end, &start);
577 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);