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
;
55 flash_driver_t
*flash_drivers
[] =
64 flash_bank_t
*flash_banks
;
65 static command_t
*flash_cmd
;
67 int flash_register_commands(struct command_context_s
*cmd_ctx
)
69 flash_cmd
= register_command(cmd_ctx
, NULL
, "flash", NULL
, COMMAND_ANY
, NULL
);
71 register_command(cmd_ctx
, flash_cmd
, "bank", handle_flash_bank_command
, COMMAND_CONFIG
, NULL
);
76 int flash_init(struct command_context_s
*cmd_ctx
)
80 register_command(cmd_ctx
, flash_cmd
, "banks", handle_flash_banks_command
, COMMAND_EXEC
,
81 "list configured flash banks ");
82 register_command(cmd_ctx
, flash_cmd
, "info", handle_flash_info_command
, COMMAND_EXEC
,
83 "print info about flash bank <num>");
84 register_command(cmd_ctx
, flash_cmd
, "probe", handle_flash_probe_command
, COMMAND_EXEC
,
85 "identify flash bank <num>");
86 register_command(cmd_ctx
, flash_cmd
, "erase_check", handle_flash_erase_check_command
, COMMAND_EXEC
,
87 "check erase state of sectors in flash bank <num>");
88 register_command(cmd_ctx
, flash_cmd
, "protect_check", handle_flash_protect_check_command
, COMMAND_EXEC
,
89 "check protection state of sectors in flash bank <num>");
90 register_command(cmd_ctx
, flash_cmd
, "erase", handle_flash_erase_command
, COMMAND_EXEC
,
91 "erase sectors at <bank> <first> <last>");
92 register_command(cmd_ctx
, flash_cmd
, "write", handle_flash_write_command
, COMMAND_EXEC
,
93 "write binary <bank> <file> <offset>");
94 register_command(cmd_ctx
, flash_cmd
, "protect", handle_flash_protect_command
, COMMAND_EXEC
,
95 "set protection of sectors at <bank> <first> <last> <on|off>");
101 flash_bank_t
*get_flash_bank_by_num(int num
)
106 for (p
= flash_banks
; p
; p
= p
->next
)
117 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
119 int handle_flash_bank_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
126 WARNING("incomplete flash_bank configuration");
130 for (i
= 0; flash_drivers
[i
]; i
++)
132 if (strcmp(args
[0], flash_drivers
[i
]->name
) == 0)
136 /* register flash specific commands */
137 if (flash_drivers
[i
]->register_commands(cmd_ctx
) != ERROR_OK
)
139 ERROR("couldn't register '%s' commands", args
[0]);
143 c
= malloc(sizeof(flash_bank_t
));
144 c
->driver
= flash_drivers
[i
];
145 c
->driver_priv
= NULL
;
146 c
->base
= strtoul(args
[1], NULL
, 0);
147 c
->size
= strtoul(args
[2], NULL
, 0);
148 c
->chip_width
= strtoul(args
[3], NULL
, 0);
149 c
->bus_width
= strtoul(args
[4], NULL
, 0);
152 if (flash_drivers
[i
]->flash_bank_command(cmd_ctx
, cmd
, args
, argc
, c
) != ERROR_OK
)
154 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args
[0], c
->base
);
159 /* put flash bank in linked list */
162 /* find last flash bank */
163 for (p
= flash_banks
; p
&& p
->next
; p
= p
->next
);
176 /* no matching flash driver found */
179 ERROR("flash driver '%s' not found", args
[0]);
186 int handle_flash_banks_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
193 command_print(cmd_ctx
, "no flash banks configured");
197 for (p
= flash_banks
; p
; p
= p
->next
)
199 command_print(cmd_ctx
, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
200 i
++, p
->driver
->name
, p
->base
, p
->size
, p
->bus_width
, p
->chip_width
);
206 int handle_flash_info_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
214 command_print(cmd_ctx
, "usage: flash info <num>");
218 for (p
= flash_banks
; p
; p
= p
->next
)
220 if (i
++ == strtoul(args
[0], NULL
, 0))
224 command_print(cmd_ctx
, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
225 i
, p
->driver
->name
, p
->base
, p
->size
, p
->bus_width
, p
->chip_width
);
226 for (j
= 0; j
< p
->num_sectors
; j
++)
228 char *erase_state
, *protect_state
;
230 if (p
->sectors
[j
].is_erased
== 0)
231 erase_state
= "not erased";
232 else if (p
->sectors
[j
].is_erased
== 1)
233 erase_state
= "erased";
235 erase_state
= "erase state unknown";
237 if (p
->sectors
[j
].is_protected
== 0)
238 protect_state
= "not protected";
239 else if (p
->sectors
[j
].is_protected
== 1)
240 protect_state
= "protected";
242 protect_state
= "protection state unknown";
244 command_print(cmd_ctx
, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
245 j
, p
->sectors
[j
].offset
, p
->sectors
[j
].size
,
246 erase_state
, protect_state
);
249 p
->driver
->info(p
, buf
, 1024);
250 command_print(cmd_ctx
, "%s", buf
);
257 int handle_flash_probe_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
264 command_print(cmd_ctx
, "usage: flash probe <num>");
268 p
= get_flash_bank_by_num(strtoul(args
[0], NULL
, 0));
271 if ((retval
= p
->driver
->probe(p
)) == ERROR_OK
)
273 command_print(cmd_ctx
, "flash '%s' found at 0x%8.8x", p
->driver
->name
, p
->base
);
275 else if (retval
== ERROR_FLASH_BANK_INVALID
)
277 command_print(cmd_ctx
, "probing failed for flash bank '#%s' at 0x%8.8x",
282 command_print(cmd_ctx
, "unknown error when probing flash bank '#%s' at 0x%8.8x",
288 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
294 int handle_flash_erase_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
301 command_print(cmd_ctx
, "usage: flash erase_check <num>");
305 p
= get_flash_bank_by_num(strtoul(args
[0], NULL
, 0));
308 if ((retval
= p
->driver
->erase_check(p
)) == ERROR_OK
)
310 command_print(cmd_ctx
, "successfully checked erase state", p
->driver
->name
, p
->base
);
314 command_print(cmd_ctx
, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
320 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
326 int handle_flash_protect_check_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
333 command_print(cmd_ctx
, "usage: flash protect_check <num>");
337 p
= get_flash_bank_by_num(strtoul(args
[0], NULL
, 0));
340 if ((retval
= p
->driver
->protect_check(p
)) == ERROR_OK
)
342 command_print(cmd_ctx
, "successfully checked protect state");
344 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
346 command_print(cmd_ctx
, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args
[0], p
->base
);
350 command_print(cmd_ctx
, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args
[0], p
->base
);
355 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
361 int handle_flash_erase_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
365 int first
= strtoul(args
[1], NULL
, 0);
366 int last
= strtoul(args
[2], NULL
, 0);
368 flash_bank_t
*p
= get_flash_bank_by_num(strtoul(args
[0], NULL
, 0));
371 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
375 if ((retval
= p
->driver
->erase(p
, first
, last
)) != ERROR_OK
)
379 case ERROR_TARGET_NOT_HALTED
:
380 command_print(cmd_ctx
, "can't work with this flash while target is running");
382 case ERROR_INVALID_ARGUMENTS
:
383 command_print(cmd_ctx
, "usage: flash_erase <bank> <first> <last>");
385 case ERROR_FLASH_BANK_INVALID
:
386 command_print(cmd_ctx
, "no '%s' flash found at 0x%8.8x", p
->driver
->name
, p
->base
);
388 case ERROR_FLASH_OPERATION_FAILED
:
389 command_print(cmd_ctx
, "flash erase error");
391 case ERROR_FLASH_SECTOR_INVALID
:
392 command_print(cmd_ctx
, "sector number(s) invalid");
395 command_print(cmd_ctx
, "erased flash sectors %i to %i", first
, last
);
398 command_print(cmd_ctx
, "unknown error");
404 command_print(cmd_ctx
, "usage: flash erase <bank> <first> <last>");
410 int handle_flash_protect_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
414 int first
= strtoul(args
[1], NULL
, 0);
415 int last
= strtoul(args
[2], NULL
, 0);
418 flash_bank_t
*p
= get_flash_bank_by_num(strtoul(args
[0], NULL
, 0));
421 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
425 if (strcmp(args
[3], "on") == 0)
427 else if (strcmp(args
[3], "off") == 0)
431 command_print(cmd_ctx
, "usage: flash protect <bank> <first> <last> <on|off>");
435 if ((retval
= p
->driver
->protect(p
, set
, first
, last
)) != ERROR_OK
)
439 case ERROR_TARGET_NOT_HALTED
:
440 command_print(cmd_ctx
, "can't work with this flash while target is running");
442 case ERROR_INVALID_ARGUMENTS
:
443 command_print(cmd_ctx
, "usage: flash protect <bank> <first> <last> <on|off>");
445 case ERROR_FLASH_BANK_INVALID
:
446 command_print(cmd_ctx
, "no '%s' flash found at 0x%8.8x", p
->driver
->name
, p
->base
);
448 case ERROR_FLASH_OPERATION_FAILED
:
449 command_print(cmd_ctx
, "flash program error");
451 case ERROR_FLASH_SECTOR_INVALID
:
452 command_print(cmd_ctx
, "sector number(s) invalid");
455 command_print(cmd_ctx
, "protection of flash sectors %i to %i turned %s", first
, last
, args
[3]);
458 command_print(cmd_ctx
, "unknown error");
464 command_print(cmd_ctx
, "usage: flash protect <bank> <first> <last> <on|off>");
470 int handle_flash_write_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
474 struct stat binary_stat
;
480 struct timeval start
, end
, duration
;
482 gettimeofday(&start
, NULL
);
486 command_print(cmd_ctx
, "usage: flash write <bank> <file> <offset>");
490 offset
= strtoul(args
[2], NULL
, 0);
491 p
= get_flash_bank_by_num(strtoul(args
[0], NULL
, 0));
494 command_print(cmd_ctx
, "flash bank '#%s' is out of bounds", args
[0]);
498 if (stat(args
[1], &binary_stat
) == -1)
500 ERROR("couldn't stat() %s: %s", args
[1], strerror(errno
));
504 if (S_ISDIR(binary_stat
.st_mode
))
506 ERROR("%s is a directory", args
[1]);
507 command_print(cmd_ctx
,"%s is a directory", args
[1]);
511 if (binary_stat
.st_size
== 0){
512 ERROR("Empty file %s", args
[1]);
513 command_print(cmd_ctx
,"Empty file %s", args
[1]);
517 if (!(binary
= fopen(args
[1], "rb")))
519 ERROR("couldn't open %s: %s", args
[1], strerror(errno
));
520 command_print(cmd_ctx
, "couldn't open %s", args
[1]);
524 binary_size
= binary_stat
.st_size
;
525 buffer
= malloc(binary_size
);
526 buf_cnt
= fread(buffer
, 1, binary_size
, binary
);
528 if ((retval
= p
->driver
->write(p
, buffer
, offset
, buf_cnt
)) != ERROR_OK
)
530 command_print(cmd_ctx
, "failed writing file %s to flash bank %i at offset 0x%8.8x",
531 args
[1], strtoul(args
[0], NULL
, 0), strtoul(args
[2], NULL
, 0));
534 case ERROR_TARGET_NOT_HALTED
:
535 command_print(cmd_ctx
, "can't work with this flash while target is running");
537 case ERROR_INVALID_ARGUMENTS
:
538 command_print(cmd_ctx
, "usage: flash write <bank> <file> <offset>");
540 case ERROR_FLASH_BANK_INVALID
:
541 command_print(cmd_ctx
, "no '%s' flash found at 0x%8.8x", p
->driver
->name
, p
->base
);
543 case ERROR_FLASH_OPERATION_FAILED
:
544 command_print(cmd_ctx
, "flash program error");
546 case ERROR_FLASH_DST_BREAKS_ALIGNMENT
:
547 command_print(cmd_ctx
, "offset breaks required alignment");
549 case ERROR_FLASH_DST_OUT_OF_BANK
:
550 command_print(cmd_ctx
, "destination is out of flash bank (offset and/or file too large)");
552 case ERROR_FLASH_SECTOR_NOT_ERASED
:
553 command_print(cmd_ctx
, "destination sector(s) not erased");
556 command_print(cmd_ctx
, "unknown error");
561 gettimeofday(&end
, NULL
);
562 timeval_subtract(&duration
, &end
, &start
);
564 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
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)