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;
61 flash_driver_t *flash_drivers[] =
73 flash_bank_t *flash_banks;
74 static command_t *flash_cmd;
76 int flash_register_commands(struct command_context_s *cmd_ctx)
78 flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
80 register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
85 int flash_init(struct command_context_s *cmd_ctx)
89 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
90 "list configured flash banks ");
91 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
92 "print info about flash bank <num>");
93 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
94 "identify flash bank <num>");
95 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
96 "check erase state of sectors in flash bank <num>");
97 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
98 "check protection state of sectors in flash bank <num>");
99 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
100 "erase sectors at <bank> <first> <last>");
101 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
102 "write binary <bank> <file> <offset>");
103 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
104 "set protection of sectors at <bank> <first> <last> <on|off>");
110 flash_bank_t *get_flash_bank_by_num(int num)
115 for (p = flash_banks; p; p = p->next)
126 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
128 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
135 WARNING("incomplete flash_bank configuration");
139 for (i = 0; flash_drivers[i]; i++)
141 if (strcmp(args[0], flash_drivers[i]->name) == 0)
145 /* register flash specific commands */
146 if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
148 ERROR("couldn't register '%s' commands", args[0]);
152 c = malloc(sizeof(flash_bank_t));
153 c->driver = flash_drivers[i];
154 c->driver_priv = NULL;
155 c->base = strtoul(args[1], NULL, 0);
156 c->size = strtoul(args[2], NULL, 0);
157 c->chip_width = strtoul(args[3], NULL, 0);
158 c->bus_width = strtoul(args[4], NULL, 0);
161 if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
163 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
168 /* put flash bank in linked list */
171 /* find last flash bank */
172 for (p = flash_banks; p && p->next; p = p->next);
185 /* no matching flash driver found */
188 ERROR("flash driver '%s' not found", args[0]);
195 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
202 command_print(cmd_ctx, "no flash banks configured");
206 for (p = flash_banks; p; p = p->next)
208 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
209 i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
215 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
223 command_print(cmd_ctx, "usage: flash info <num>");
227 for (p = flash_banks; p; p = p->next)
229 if (i++ == strtoul(args[0], NULL, 0))
233 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
234 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
235 for (j = 0; j < p->num_sectors; j++)
237 char *erase_state, *protect_state;
239 if (p->sectors[j].is_erased == 0)
240 erase_state = "not erased";
241 else if (p->sectors[j].is_erased == 1)
242 erase_state = "erased";
244 erase_state = "erase state unknown";
246 if (p->sectors[j].is_protected == 0)
247 protect_state = "not protected";
248 else if (p->sectors[j].is_protected == 1)
249 protect_state = "protected";
251 protect_state = "protection state unknown";
253 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
254 j, p->sectors[j].offset, p->sectors[j].size,
255 erase_state, protect_state);
258 p->driver->info(p, buf, 1024);
259 command_print(cmd_ctx, "%s", buf);
266 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
273 command_print(cmd_ctx, "usage: flash probe <num>");
277 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
280 if ((retval = p->driver->probe(p)) == ERROR_OK)
282 command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
284 else if (retval == ERROR_FLASH_BANK_INVALID)
286 command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
291 command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
297 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
303 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
310 command_print(cmd_ctx, "usage: flash erase_check <num>");
314 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
317 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
319 command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
323 command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
329 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
335 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
342 command_print(cmd_ctx, "usage: flash protect_check <num>");
346 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
349 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
351 command_print(cmd_ctx, "successfully checked protect state");
353 else if (retval == ERROR_FLASH_OPERATION_FAILED)
355 command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
359 command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
364 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
370 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
374 int first = strtoul(args[1], NULL, 0);
375 int last = strtoul(args[2], NULL, 0);
377 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
381 duration_start_measure(&duration);
385 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
389 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
393 case ERROR_TARGET_NOT_HALTED:
394 command_print(cmd_ctx, "can't work with this flash while target is running");
396 case ERROR_INVALID_ARGUMENTS:
397 command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
399 case ERROR_FLASH_BANK_INVALID:
400 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
402 case ERROR_FLASH_OPERATION_FAILED:
403 command_print(cmd_ctx, "flash erase error");
405 case ERROR_FLASH_SECTOR_INVALID:
406 command_print(cmd_ctx, "sector number(s) invalid");
409 command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
412 command_print(cmd_ctx, "unknown error");
417 duration_stop_measure(&duration, &duration_text);
419 command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
425 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
431 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
435 int first = strtoul(args[1], NULL, 0);
436 int last = strtoul(args[2], NULL, 0);
439 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
442 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
446 if (strcmp(args[3], "on") == 0)
448 else if (strcmp(args[3], "off") == 0)
452 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
456 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
460 case ERROR_TARGET_NOT_HALTED:
461 command_print(cmd_ctx, "can't work with this flash while target is running");
463 case ERROR_INVALID_ARGUMENTS:
464 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
466 case ERROR_FLASH_BANK_INVALID:
467 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
469 case ERROR_FLASH_OPERATION_FAILED:
470 command_print(cmd_ctx, "flash program error");
472 case ERROR_FLASH_SECTOR_INVALID:
473 command_print(cmd_ctx, "sector number(s) invalid");
476 command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
479 command_print(cmd_ctx, "unknown error");
484 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));
489 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
495 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
513 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
517 duration_start_measure(&duration);
519 image.base_address_set = 1;
520 image.base_address = strtoul(args[1], NULL, 0);
522 image.start_address_set = 0;
524 offset = strtoul(args[2], NULL, 0);
525 p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
528 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
532 if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
534 command_print(cmd_ctx, "flash write error: %s", image.error_str);
539 for (i = 0; i < image.num_sections; i++)
541 buffer = malloc(image.sections[i].size);
542 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
544 ERROR("image_read_section failed with error code: %i", retval);
545 command_print(cmd_ctx, "image reading failed, flash write aborted");
551 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
553 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
554 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
557 case ERROR_TARGET_NOT_HALTED:
558 command_print(cmd_ctx, "can't work with this flash while target is running");
560 case ERROR_INVALID_ARGUMENTS:
561 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
563 case ERROR_FLASH_BANK_INVALID:
564 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
566 case ERROR_FLASH_OPERATION_FAILED:
567 command_print(cmd_ctx, "flash program error");
569 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
570 command_print(cmd_ctx, "offset breaks required alignment");
572 case ERROR_FLASH_DST_OUT_OF_BANK:
573 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
575 case ERROR_FLASH_SECTOR_NOT_ERASED:
576 command_print(cmd_ctx, "destination sector(s) not erased");
579 command_print(cmd_ctx, "unknown error");
582 image_size += buf_cnt;
588 duration_stop_measure(&duration, &duration_text);
589 command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
590 image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
591 (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));