flash: flash erase_address takes unsigned arguments
[fw/openocd] / src / flash / nor / tcl.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>              *
3  *   Copyright (C) 2007,2008 Ã˜yvind Harboe <oyvind.harboe@zylin.com>       *
4  *   Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk>           *
5  *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "imp.h"
26 #include <helper/time_support.h>
27 #include <target/image.h>
28
29 /**
30  * @file
31  * Implements Tcl commands used to access NOR flash facilities.
32  */
33
34 COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
35                 struct flash_bank **bank)
36 {
37         const char *name = CMD_ARGV[name_index];
38         *bank = get_flash_bank_by_name(name);
39         if (*bank)
40                 return ERROR_OK;
41
42         unsigned bank_num;
43         COMMAND_PARSE_NUMBER(uint, name, bank_num);
44
45         return get_flash_bank_by_num(bank_num, bank);
46 }
47
48
49 COMMAND_HANDLER(handle_flash_info_command)
50 {
51         struct flash_bank *p;
52         int j = 0;
53         int retval;
54
55         if (CMD_ARGC != 1)
56                 return ERROR_COMMAND_SYNTAX_ERROR;
57
58         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
59         if (retval != ERROR_OK)
60                 return retval;
61
62         if (p != NULL)
63         {
64                 char buf[1024];
65
66                 /* attempt auto probe */
67                 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
68                         return retval;
69
70                 /* We must query the hardware to avoid printing stale information! */
71                 retval = p->driver->protect_check(p);
72                 if (retval != ERROR_OK)
73                         return retval;
74
75                 command_print(CMD_CTX,
76                               "#%" PRIu32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
77                               p->bank_number,
78                               p->driver->name,
79                               p->base,
80                               p->size,
81                               p->bus_width,
82                               p->chip_width);
83                 for (j = 0; j < p->num_sectors; j++)
84                 {
85                         char *protect_state;
86
87                         if (p->sectors[j].is_protected == 0)
88                                 protect_state = "not protected";
89                         else if (p->sectors[j].is_protected == 1)
90                                 protect_state = "protected";
91                         else
92                                 protect_state = "protection state unknown";
93
94                         command_print(CMD_CTX,
95                                       "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
96                                       j,
97                                       p->sectors[j].offset,
98                                       p->sectors[j].size,
99                                       p->sectors[j].size >> 10,
100                                       protect_state);
101                 }
102
103                 *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
104                 retval = p->driver->info(p, buf, sizeof(buf));
105                 command_print(CMD_CTX, "%s", buf);
106                 if (retval != ERROR_OK)
107                         LOG_ERROR("error retrieving flash info (%d)", retval);
108         }
109
110         return ERROR_OK;
111 }
112
113 COMMAND_HANDLER(handle_flash_probe_command)
114 {
115         struct flash_bank *p;
116         int retval;
117
118         if (CMD_ARGC != 1)
119         {
120                 return ERROR_COMMAND_SYNTAX_ERROR;
121         }
122
123         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
124         if (retval != ERROR_OK)
125                 return retval;
126
127         if (p)
128         {
129                 if ((retval = p->driver->probe(p)) == ERROR_OK)
130                 {
131                         command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
132                 }
133                 else if (retval == ERROR_FLASH_BANK_INVALID)
134                 {
135                         command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
136                                                   CMD_ARGV[0], p->base);
137                 }
138                 else
139                 {
140                         command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
141                                                   CMD_ARGV[0], p->base);
142                 }
143         }
144         else
145         {
146                 command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
147         }
148
149         return ERROR_OK;
150 }
151
152 COMMAND_HANDLER(handle_flash_erase_check_command)
153 {
154         if (CMD_ARGC != 1)
155         {
156                 return ERROR_COMMAND_SYNTAX_ERROR;
157         }
158
159         struct flash_bank *p;
160         int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
161         if (ERROR_OK != retval)
162                 return retval;
163
164         int j;
165         if ((retval = p->driver->erase_check(p)) == ERROR_OK)
166         {
167                 command_print(CMD_CTX, "successfully checked erase state");
168         }
169         else
170         {
171                 command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
172                         CMD_ARGV[0], p->base);
173         }
174
175         for (j = 0; j < p->num_sectors; j++)
176         {
177                 char *erase_state;
178
179                 if (p->sectors[j].is_erased == 0)
180                         erase_state = "not erased";
181                 else if (p->sectors[j].is_erased == 1)
182                         erase_state = "erased";
183                 else
184                         erase_state = "erase state unknown";
185
186                 command_print(CMD_CTX,
187                               "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
188                               j,
189                               p->sectors[j].offset,
190                               p->sectors[j].size,
191                               p->sectors[j].size >> 10,
192                               erase_state);
193         }
194
195         return ERROR_OK;
196 }
197
198 COMMAND_HANDLER(handle_flash_erase_address_command)
199 {
200         struct flash_bank *p;
201         int retval = ERROR_OK;
202         uint32_t address;
203         uint32_t length;
204         bool do_pad = false;
205         bool do_unlock = false;
206         struct target *target = get_current_target(CMD_CTX);
207
208         while (CMD_ARGC >= 3)
209         {
210                 /* Optionally pad out the address range to block/sector
211                  * boundaries.  We can't know if there's data in that part
212                  * of the flash; only do padding if we're told to.
213                  */
214                 if (strcmp("pad", CMD_ARGV[0]) == 0)
215                 {
216                         do_pad = true;
217                 } else if (strcmp("unlock", CMD_ARGV[0]) == 0)
218                 {
219                         do_unlock = true;
220                 } else
221                 {
222                         return ERROR_COMMAND_SYNTAX_ERROR;
223                 }
224                 CMD_ARGC--;
225                 CMD_ARGV++;
226         }
227         if (CMD_ARGC != 2)
228         {
229                 return ERROR_COMMAND_SYNTAX_ERROR;
230         }
231
232         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
233         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
234
235         if (length <= 0)
236         {
237                 command_print(CMD_CTX, "Length must be >0");
238                 return ERROR_COMMAND_SYNTAX_ERROR;
239         }
240
241         p = get_flash_bank_by_addr(target, address);
242         if (p == NULL)
243         {
244                 return ERROR_FAIL;
245         }
246
247         /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
248         flash_set_dirty();
249
250         struct duration bench;
251         duration_start(&bench);
252
253         if (do_unlock)
254         {
255                 retval = flash_unlock_address_range(target, address, length);
256         }
257
258         if (retval == ERROR_OK)
259         {
260                 retval = flash_erase_address_range(target, do_pad, address, length);
261         }
262
263         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
264         {
265                 command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
266                                 " in %fs (%0.3f KiB/s)", address, length,
267                                 duration_elapsed(&bench), duration_kbps(&bench, length));
268         }
269
270         return retval;
271 }
272
273 static int flash_check_sector_parameters(struct command_context *cmd_ctx,
274                 uint32_t first, uint32_t last, uint32_t num_sectors)
275 {
276         if (!(first <= last)) {
277                 command_print(cmd_ctx, "ERROR: "
278                                 "first sector must be <= last sector");
279                 return ERROR_FAIL;
280         }
281
282         if (!(last <= (num_sectors - 1))) {
283                 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
284                                 (int) num_sectors - 1);
285                 return ERROR_FAIL;
286         }
287
288         return ERROR_OK;
289 }
290
291 COMMAND_HANDLER(handle_flash_erase_command)
292 {
293         if (CMD_ARGC != 3)
294                 return ERROR_COMMAND_SYNTAX_ERROR;
295
296         uint32_t first;
297         uint32_t last;
298
299         struct flash_bank *p;
300         int retval;
301
302         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
303         if (retval != ERROR_OK)
304                 return retval;
305
306         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
307         if (strcmp(CMD_ARGV[2], "last") == 0)
308                 last = p->num_sectors - 1;
309         else
310                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
311
312         if ((retval = flash_check_sector_parameters(CMD_CTX,
313                         first, last, p->num_sectors)) != ERROR_OK)
314                 return retval;
315
316         struct duration bench;
317         duration_start(&bench);
318
319         retval = flash_driver_erase(p, first, last);
320
321         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
322         {
323                 command_print(CMD_CTX, "erased sectors %" PRIu32 " "
324                                 "through %" PRIu32" on flash bank %" PRIu32 " "
325                                 "in %fs", first, last, p->bank_number, duration_elapsed(&bench));
326         }
327
328         return ERROR_OK;
329 }
330
331 COMMAND_HANDLER(handle_flash_protect_command)
332 {
333         if (CMD_ARGC != 4)
334                 return ERROR_COMMAND_SYNTAX_ERROR;
335
336         uint32_t first;
337         uint32_t last;
338
339         struct flash_bank *p;
340         int retval;
341
342         retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
343         if (retval != ERROR_OK)
344                 return retval;
345
346         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
347         if (strcmp(CMD_ARGV[2], "last") == 0)
348                 last = p->num_sectors - 1;
349         else
350                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
351
352         bool set;
353         COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
354
355         if ((retval = flash_check_sector_parameters(CMD_CTX,
356                         first, last, p->num_sectors)) != ERROR_OK)
357                 return retval;
358
359         retval = flash_driver_protect(p, set, first, last);
360         if (retval == ERROR_OK) {
361                 command_print(CMD_CTX, "%s protection for sectors %i "
362                                 "through %i on flash bank %" PRIu32 "",
363                         (set) ? "set" : "cleared", (int) first,
364                         (int) last, p->bank_number);
365         }
366
367         return ERROR_OK;
368 }
369
370 COMMAND_HANDLER(handle_flash_write_image_command)
371 {
372         struct target *target = get_current_target(CMD_CTX);
373
374         struct image image;
375         uint32_t written;
376
377         int retval;
378
379         if (CMD_ARGC < 1)
380         {
381                 return ERROR_COMMAND_SYNTAX_ERROR;
382         }
383
384         /* flash auto-erase is disabled by default*/
385         int auto_erase = 0;
386         bool auto_unlock = false;
387
388         for (;;)
389         {
390                 if (strcmp(CMD_ARGV[0], "erase") == 0)
391                 {
392                         auto_erase = 1;
393                         CMD_ARGV++;
394                         CMD_ARGC--;
395                         command_print(CMD_CTX, "auto erase enabled");
396                 } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
397                 {
398                         auto_unlock = true;
399                         CMD_ARGV++;
400                         CMD_ARGC--;
401                         command_print(CMD_CTX, "auto unlock enabled");
402                 } else
403                 {
404                         break;
405                 }
406         }
407
408         if (CMD_ARGC < 1)
409         {
410                 return ERROR_COMMAND_SYNTAX_ERROR;
411         }
412
413         if (!target)
414         {
415                 LOG_ERROR("no target selected");
416                 return ERROR_FAIL;
417         }
418
419         struct duration bench;
420         duration_start(&bench);
421
422         if (CMD_ARGC >= 2)
423         {
424                 image.base_address_set = 1;
425                 COMMAND_PARSE_NUMBER(llong, CMD_ARGV[1], image.base_address);
426         }
427         else
428         {
429                 image.base_address_set = 0;
430                 image.base_address = 0x0;
431         }
432
433         image.start_address_set = 0;
434
435         retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
436         if (retval != ERROR_OK)
437         {
438                 return retval;
439         }
440
441         retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
442         if (retval != ERROR_OK)
443         {
444                 image_close(&image);
445                 return retval;
446         }
447
448         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
449         {
450                 command_print(CMD_CTX, "wrote %" PRIu32 " bytes from file %s "
451                                 "in %fs (%0.3f KiB/s)", written, CMD_ARGV[0],
452                                 duration_elapsed(&bench), duration_kbps(&bench, written));
453         }
454
455         image_close(&image);
456
457         return retval;
458 }
459
460 COMMAND_HANDLER(handle_flash_fill_command)
461 {
462         int err = ERROR_OK;
463         uint32_t address;
464         uint32_t pattern;
465         uint32_t count;
466         uint32_t wrote = 0;
467         uint32_t cur_size = 0;
468         uint32_t chunk_count;
469         struct target *target = get_current_target(CMD_CTX);
470         uint32_t i;
471         uint32_t wordsize;
472         int retval = ERROR_OK;
473
474         static size_t const chunksize = 1024;
475         uint8_t *chunk = malloc(chunksize);
476         if (chunk == NULL)
477                 return ERROR_FAIL;
478
479         uint8_t *readback = malloc(chunksize);
480         if (readback == NULL)
481         {
482                 free(chunk);
483                 return ERROR_FAIL;
484         }
485
486
487         if (CMD_ARGC != 3)
488         {
489                 retval = ERROR_COMMAND_SYNTAX_ERROR;
490                 goto done;
491         }
492
493
494         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
495         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
496         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
497
498         if (count == 0)
499                 goto done;
500
501         switch (CMD_NAME[4])
502         {
503         case 'w':
504                 wordsize = 4;
505                 break;
506         case 'h':
507                 wordsize = 2;
508                 break;
509         case 'b':
510                 wordsize = 1;
511                 break;
512         default:
513                 retval = ERROR_COMMAND_SYNTAX_ERROR;
514                 goto done;
515         }
516
517         chunk_count = MIN(count, (chunksize / wordsize));
518         switch (wordsize)
519         {
520         case 4:
521                 for (i = 0; i < chunk_count; i++)
522                 {
523                         target_buffer_set_u32(target, chunk + i * wordsize, pattern);
524                 }
525                 break;
526         case 2:
527                 for (i = 0; i < chunk_count; i++)
528                 {
529                         target_buffer_set_u16(target, chunk + i * wordsize, pattern);
530                 }
531                 break;
532         case 1:
533                 memset(chunk, pattern, chunk_count);
534                 break;
535         default:
536                 LOG_ERROR("BUG: can't happen");
537                 exit(-1);
538         }
539
540         struct duration bench;
541         duration_start(&bench);
542
543         for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
544         {
545                 struct flash_bank *bank;
546
547                 bank = get_flash_bank_by_addr(target, address);
548                 if (bank == NULL)
549                 {
550                         retval = ERROR_FAIL;
551                         goto done;
552                 }
553
554                 cur_size = MIN((count * wordsize - wrote), chunksize);
555                 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
556                 if (err != ERROR_OK)
557                 {
558                         retval = err;
559                         goto done;
560                 }
561
562                 err = flash_driver_read(bank, readback, address - bank->base + wrote, cur_size);
563                 if (err != ERROR_OK)
564                 {
565                         retval = err;
566                         goto done;
567                 }
568
569                 unsigned i;
570                 for (i = 0; i < cur_size; i++)
571                 {
572                         if (readback[i]!=chunk[i])
573                         {
574                                 LOG_ERROR("Verification error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
575                                                   address + wrote + i, readback[i], chunk[i]);
576                                 retval = ERROR_FAIL;
577                                 goto done;
578                         }
579                 }
580         }
581
582         if (duration_measure(&bench) == ERROR_OK)
583         {
584                 command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
585                                 " in %fs (%0.3f KiB/s)", wrote, address,
586                                 duration_elapsed(&bench), duration_kbps(&bench, wrote));
587         }
588
589 done:
590         free(readback);
591         free(chunk);
592
593         return retval;
594 }
595
596 COMMAND_HANDLER(handle_flash_write_bank_command)
597 {
598         uint32_t offset;
599         uint8_t *buffer;
600         struct fileio fileio;
601
602         if (CMD_ARGC != 3)
603                 return ERROR_COMMAND_SYNTAX_ERROR;
604
605         struct duration bench;
606         duration_start(&bench);
607
608         struct flash_bank *p;
609         int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
610         if (ERROR_OK != retval)
611                 return retval;
612
613         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
614
615         if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
616         {
617                 return ERROR_OK;
618         }
619
620         buffer = malloc(fileio.size);
621         size_t buf_cnt;
622         if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
623         {
624                 free(buffer);
625                 fileio_close(&fileio);
626                 return ERROR_OK;
627         }
628
629         retval = flash_driver_write(p, buffer, offset, buf_cnt);
630
631         free(buffer);
632         buffer = NULL;
633
634         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
635         {
636                 command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u"
637                                 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
638                                 (long)fileio.size, CMD_ARGV[1], p->bank_number, offset,
639                                 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
640         }
641
642         fileio_close(&fileio);
643
644         return retval;
645 }
646
647 void flash_set_dirty(void)
648 {
649         struct flash_bank *c;
650         int i;
651
652         /* set all flash to require erasing */
653         for (c = flash_bank_list(); c; c = c->next)
654         {
655                 for (i = 0; i < c->num_sectors; i++)
656                 {
657                         c->sectors[i].is_erased = 0;
658                 }
659         }
660 }
661
662 static const struct command_registration flash_exec_command_handlers[] = {
663         {
664                 .name = "probe",
665                 .handler = handle_flash_probe_command,
666                 .mode = COMMAND_EXEC,
667                 .usage = "bank_id",
668                 .help = "Identify a flash bank.",
669         },
670         {
671                 .name = "info",
672                 .handler = handle_flash_info_command,
673                 .mode = COMMAND_EXEC,
674                 .usage = "bank_id",
675                 .help = "Print information about a flash bank.",
676         },
677         {
678                 .name = "erase_check",
679                 .handler = handle_flash_erase_check_command,
680                 .mode = COMMAND_EXEC,
681                 .usage = "bank_id",
682                 .help = "Check erase state of all blocks in a "
683                         "flash bank.",
684         },
685         {
686                 .name = "erase_sector",
687                 .handler = handle_flash_erase_command,
688                 .mode = COMMAND_EXEC,
689                 .usage = "bank_id first_sector_num last_sector_num",
690                 .help = "Erase a range of sectors in a flash bank.",
691         },
692         {
693                 .name = "erase_address",
694                 .handler = handle_flash_erase_address_command,
695                 .mode = COMMAND_EXEC,
696                 .usage = "['pad'] ['unlock'] address length",
697                 .help = "Erase flash sectors starting at address and "
698                         "continuing for length bytes.  If 'pad' is specified, "
699                         "data outside that range may also be erased: the start "
700                         "address may be decreased, and length increased, so "
701                         "that all of the first and last sectors are erased. "
702                         "If 'unlock' is specified, then the flash is unprotected "
703                         "before erasing.",
704
705         },
706         {
707                 .name = "fillw",
708                 .handler = handle_flash_fill_command,
709                 .mode = COMMAND_EXEC,
710                 .usage = "address value n",
711                 .help = "Fill n words with 32-bit value, starting at "
712                         "word address.  (No autoerase.)",
713         },
714         {
715                 .name = "fillh",
716                 .handler = handle_flash_fill_command,
717                 .mode = COMMAND_EXEC,
718                 .usage = "address value n",
719                 .help = "Fill n halfwords with 16-bit value, starting at "
720                         "word address.  (No autoerase.)",
721         },
722         {
723                 .name = "fillb",
724                 .handler = handle_flash_fill_command,
725                 .mode = COMMAND_EXEC,
726                 .usage = "address value n",
727                 .help = "Fill n bytes with 8-bit value, starting at "
728                         "word address.  (No autoerase.)",
729         },
730         {
731                 .name = "write_bank",
732                 .handler = handle_flash_write_bank_command,
733                 .mode = COMMAND_EXEC,
734                 .usage = "bank_id filename offset",
735                 .help = "Write binary data from file to flash bank, "
736                         "starting at specified byte offset from the "
737                         "beginning of the bank.",
738         },
739         {
740                 .name = "write_image",
741                 .handler = handle_flash_write_image_command,
742                 .mode = COMMAND_EXEC,
743                 .usage = "[erase] [unlock] filename [offset [file_type]]",
744                 .help = "Write an image to flash.  Optionally first unprotect "
745                         "and/or erase the region to be used.  Allow optional "
746                         "offset from beginning of bank (defaults to zero)",
747         },
748         {
749                 .name = "protect",
750                 .handler = handle_flash_protect_command,
751                 .mode = COMMAND_EXEC,
752                 .usage = "bank_id first_sector [last_sector|'last'] "
753                         "('on'|'off')",
754                 .help = "Turn protection on or off for a range of sectors "
755                         "in a given flash bank.",
756         },
757         COMMAND_REGISTRATION_DONE
758 };
759
760 static int flash_init_drivers(struct command_context *cmd_ctx)
761 {
762         if (!flash_bank_list())
763                 return ERROR_OK;
764
765         struct command *parent = command_find_in_context(cmd_ctx, "flash");
766         return register_commands(cmd_ctx, parent, flash_exec_command_handlers);
767 }
768
769
770 COMMAND_HANDLER(handle_flash_bank_command)
771 {
772         if (CMD_ARGC < 7)
773         {
774                 LOG_ERROR("usage: flash bank <name> <driver> "
775                                 "<base> <size> <chip_width> <bus_width> <target>");
776                 return ERROR_COMMAND_SYNTAX_ERROR;
777         }
778         // save bank name and advance arguments for compatibility
779         const char *bank_name = *CMD_ARGV++;
780         CMD_ARGC--;
781
782         struct target *target;
783         if ((target = get_target(CMD_ARGV[5])) == NULL)
784         {
785                 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
786                 return ERROR_FAIL;
787         }
788
789         const char *driver_name = CMD_ARGV[0];
790         struct flash_driver *driver = flash_driver_find_by_name(driver_name);
791         if (NULL == driver)
792         {
793                 /* no matching flash driver found */
794                 LOG_ERROR("flash driver '%s' not found", driver_name);
795                 return ERROR_FAIL;
796         }
797
798         /* check the flash bank name is unique */
799         if (get_flash_bank_by_name_noprobe(bank_name) != NULL)
800         {
801                 /* flash bank name already exists  */
802                 LOG_ERROR("flash bank name '%s' already exists", bank_name);
803                 return ERROR_FAIL;
804         }
805
806         /* register flash specific commands */
807         if (NULL != driver->commands)
808         {
809                 int retval = register_commands(CMD_CTX, NULL,
810                                 driver->commands);
811                 if (ERROR_OK != retval)
812                 {
813                         LOG_ERROR("couldn't register '%s' commands",
814                                         driver_name);
815                         return ERROR_FAIL;
816                 }
817         }
818
819         struct flash_bank *c = malloc(sizeof(*c));
820         c->name = strdup(bank_name);
821         c->target = target;
822         c->driver = driver;
823         c->driver_priv = NULL;
824         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
825         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
826         COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
827         COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
828         c->num_sectors = 0;
829         c->sectors = NULL;
830         c->next = NULL;
831
832         int retval;
833         retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
834         if (ERROR_OK != retval)
835         {
836                 LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32,
837                                 driver_name, c->base);
838                 free(c);
839                 return retval;
840         }
841
842         flash_bank_add(c);
843
844         return ERROR_OK;
845 }
846
847 COMMAND_HANDLER(handle_flash_banks_command)
848 {
849         if (CMD_ARGC != 0)
850                 return ERROR_INVALID_ARGUMENTS;
851
852         unsigned n = 0;
853         for (struct flash_bank *p = flash_bank_list(); p; p = p->next, n++)
854         {
855                 LOG_USER("#%" PRIu32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", "
856                         "buswidth %u, chipwidth %u", p->bank_number,
857                         p->driver->name, p->base, p->size,
858                         p->bus_width, p->chip_width);
859         }
860         return ERROR_OK;
861 }
862
863 static int jim_flash_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
864 {
865         if (argc != 1)
866         {
867                 Jim_WrongNumArgs(interp, 1, argv,
868                                 "no arguments to 'flash list' command");
869                 return JIM_ERR;
870         }
871
872         Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
873
874         for (struct flash_bank *p = flash_bank_list(); p; p = p->next)
875         {
876                 Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
877
878                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
879                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
880                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
881                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
882                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
883                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
884                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
885                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
886                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
887                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
888
889                 Jim_ListAppendElement(interp, list, elem);
890         }
891
892         Jim_SetResult(interp, list);
893
894         return JIM_OK;
895 }
896
897
898 COMMAND_HANDLER(handle_flash_init_command)
899 {
900         if (CMD_ARGC != 0)
901                 return ERROR_COMMAND_SYNTAX_ERROR;
902
903         static bool flash_initialized = false;
904         if (flash_initialized)
905         {
906                 LOG_INFO("'flash init' has already been called");
907                 return ERROR_OK;
908         }
909         flash_initialized = true;
910
911         LOG_DEBUG("Initializing flash devices...");
912         return flash_init_drivers(CMD_CTX);
913 }
914
915 static const struct command_registration flash_config_command_handlers[] = {
916         {
917                 .name = "bank",
918                 .handler = handle_flash_bank_command,
919                 .mode = COMMAND_CONFIG,
920                 .usage = "bank_id driver_name base_address size_bytes "
921                         "chip_width_bytes bus_width_bytes target "
922                         "[driver_options ...]",
923                 .help = "Define a new bank with the given name, "
924                         "using the specified NOR flash driver.",
925         },
926         {
927                 .name = "init",
928                 .mode = COMMAND_CONFIG,
929                 .handler = handle_flash_init_command,
930                 .help = "Initialize flash devices.",
931         },
932         {
933                 .name = "banks",
934                 .mode = COMMAND_ANY,
935                 .handler = handle_flash_banks_command,
936                 .help = "Display table with information about flash banks.",
937         },
938         {
939                 .name = "list",
940                 .mode = COMMAND_ANY,
941                 .jim_handler = jim_flash_list,
942                 .help = "Returns a list of details about the flash banks.",
943         },
944         COMMAND_REGISTRATION_DONE
945 };
946 static const struct command_registration flash_command_handlers[] = {
947         {
948                 .name = "flash",
949                 .mode = COMMAND_ANY,
950                 .help = "NOR flash command group",
951                 .chain = flash_config_command_handlers,
952         },
953         COMMAND_REGISTRATION_DONE
954 };
955
956 int flash_register_commands(struct command_context *cmd_ctx)
957 {
958         return register_commands(cmd_ctx, NULL, flash_command_handlers);
959 }