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>
39 /* command handlers */
40 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
41 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
42 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
43 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 extern flash_driver_t lpc2000_flash;
53 extern flash_driver_t cfi_flash;
54 extern flash_driver_t at91sam7_flash;
55 extern flash_driver_t str7x_flash;
56 extern flash_driver_t str9x_flash;
58 flash_driver_t *flash_drivers[] =
68 flash_bank_t *flash_banks;
69 static command_t *flash_cmd;
71 int flash_register_commands(struct command_context_s *cmd_ctx)
73 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
75 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
80 int flash_init(struct command_context_s *cmd_ctx)
84 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
85 "list configured flash banks ");
86 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
87 "print info about flash bank <num>");
88 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
89 "identify flash bank <num>");
90 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
91 "check erase state of sectors in flash bank <num>");
92 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
93 "check protection state of sectors in flash bank <num>");
94 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
95 "erase sectors at <bank> <first> <last>");
96 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
97 "write binary <bank> <file> <offset>");
98 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
99 "set protection of sectors at <bank> <first> <last> <on|off>");
105 flash_bank_t *get_flash_bank_by_num(int num)
110 for (p = flash_banks; p; p = p->next)
121 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
123 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
130 WARNING("incomplete flash_bank configuration");
134 for (i = 0; flash_drivers[i]; i++)
136 if (strcmp(args[0], flash_drivers[i]->name) == 0)
140 /* register flash specific commands */
141 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
143 ERROR("couldn't register '%s' commands", args[0]);
147 c = malloc(sizeof(flash_bank_t));
148 c->driver = flash_drivers[i];
149 c->driver_priv = NULL;
150 c->base = strtoul(args[1], NULL, 0);
151 c->size = strtoul(args[2], NULL, 0);
152 c->chip_width = strtoul(args[3], NULL, 0);
153 c->bus_width = strtoul(args[4], NULL, 0);
156 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
158 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
163 /* put flash bank in linked list */
166 /* find last flash bank */
167 for (p = flash_banks; p && p->next; p = p->next);
180 /* no matching flash driver found */
183 ERROR("flash driver '%s' not found", args[0]);
190 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
197 command_print(cmd_ctx, "no flash banks configured");
201 for (p = flash_banks; p; p = p->next)
203 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
204 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
210 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
218 command_print(cmd_ctx, "usage: flash info <num>");
222 for (p = flash_banks; p; p = p->next)
224 if (i++ == strtoul(args[0], NULL, 0))
228 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
229 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
230 for (j = 0; j < p->num_sectors; j++)
232 char *erase_state, *protect_state;
234 if (p->sectors[j].is_erased == 0)
235 erase_state = "not erased";
236 else if (p->sectors[j].is_erased == 1)
237 erase_state = "erased";
239 erase_state = "erase state unknown";
241 if (p->sectors[j].is_protected == 0)
242 protect_state = "not protected";
243 else if (p->sectors[j].is_protected == 1)
244 protect_state = "protected";
246 protect_state = "protection state unknown";
248 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
249 j, p->sectors[j].offset, p->sectors[j].size,
250 erase_state, protect_state);
253 p->driver->info(p, buf, 1024);
254 command_print(cmd_ctx, "%s", buf);
261 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
268 command_print(cmd_ctx, "usage: flash probe <num>");
272 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
275 if ((retval = p->driver->probe(p)) == ERROR_OK)
277 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
279 else if (retval == ERROR_FLASH_BANK_INVALID)
281 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
286 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
292 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
298 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
305 command_print(cmd_ctx, "usage: flash erase_check <num>");
309 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
312 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
314 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
318 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
324 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
330 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
337 command_print(cmd_ctx, "usage: flash protect_check <num>");
341 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
344 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
346 command_print(cmd_ctx, "successfully checked protect state");
348 else if (retval == ERROR_FLASH_OPERATION_FAILED)
350 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
354 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
359 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
365 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
369 int first = strtoul(args[1], NULL, 0);
370 int last = strtoul(args[2], NULL, 0);
372 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
373 struct timeval start, end, duration;
375 gettimeofday(&start, NULL);
379 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
383 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
387 case ERROR_TARGET_NOT_HALTED:
388 command_print(cmd_ctx, "can't work with this flash while target is running");
390 case ERROR_INVALID_ARGUMENTS:
391 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
393 case ERROR_FLASH_BANK_INVALID:
394 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
396 case ERROR_FLASH_OPERATION_FAILED:
397 command_print(cmd_ctx, "flash erase error");
399 case ERROR_FLASH_SECTOR_INVALID:
400 command_print(cmd_ctx, "sector number(s) invalid");
403 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
406 command_print(cmd_ctx, "unknown error");
411 gettimeofday(&end, NULL);
412 timeval_subtract(&duration, &end, &start);
414 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);
419 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
425 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
429 int first = strtoul(args[1], NULL, 0);
430 int last = strtoul(args[2], NULL, 0);
433 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
436 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
440 if (strcmp(args[3], "on") == 0)
442 else if (strcmp(args[3], "off") == 0)
446 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
450 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
454 case ERROR_TARGET_NOT_HALTED:
455 command_print(cmd_ctx, "can't work with this flash while target is running");
457 case ERROR_INVALID_ARGUMENTS:
458 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
460 case ERROR_FLASH_BANK_INVALID:
461 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
463 case ERROR_FLASH_OPERATION_FAILED:
464 command_print(cmd_ctx, "flash program error");
466 case ERROR_FLASH_SECTOR_INVALID:
467 command_print(cmd_ctx, "sector number(s) invalid");
470 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
473 command_print(cmd_ctx, "unknown error");
478 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));
483 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
489 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
497 fileio_image_t image_info;
498 enum fileio_sec_type sec_type;
508 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
512 duration_start_measure(&duration);
514 fileio_identify_image_type(&sec_type, (argc == 4) ? args[3] : NULL);
516 offset = strtoul(args[2], NULL, 0);
517 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
520 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
524 image_info.base_address = strtoul(args[2], NULL, 0);
525 image_info.has_start_address = 0;
527 if (fileio_open(&file, args[1], FILEIO_READ,
528 FILEIO_IMAGE, &image_info, sec_type) != ERROR_OK)
530 command_print(cmd_ctx, "flash write error: %s", file.error_str);
534 binary_size = file.size;
535 buffer = malloc(binary_size);
537 fileio_read(&file, binary_size, buffer, &buf_cnt);
539 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
541 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
542 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
545 case ERROR_TARGET_NOT_HALTED:
546 command_print(cmd_ctx, "can't work with this flash while target is running");
548 case ERROR_INVALID_ARGUMENTS:
549 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
551 case ERROR_FLASH_BANK_INVALID:
552 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
554 case ERROR_FLASH_OPERATION_FAILED:
555 command_print(cmd_ctx, "flash program error");
557 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
558 command_print(cmd_ctx, "offset breaks required alignment");
560 case ERROR_FLASH_DST_OUT_OF_BANK:
561 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
563 case ERROR_FLASH_SECTOR_NOT_ERASED:
564 command_print(cmd_ctx, "destination sector(s) not erased");
567 command_print(cmd_ctx, "unknown error");
572 duration_stop_measure(&duration, &duration_text);
573 command_print(cmd_ctx, "wrote file %s to flash bank %i at offset 0x%8.8x in %s",
574 file.url, strtoul(args[0], NULL, 0), offset, duration_text);