]> git.gag.com Git - fw/openocd/blob - src/flash/flash.c
use COMMAND_PARSE_ON_OFF where appropriate
[fw/openocd] / src / flash / flash.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 by Spencer Oliver                                  *
9  *   spen@spen-soft.co.uk                                                  *
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program; if not, write to the                         *
23  *   Free Software Foundation, Inc.,                                       *
24  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25  ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "flash.h"
31 #include "image.h"
32 #include "time_support.h"
33
34 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock);
35
36 /* flash drivers
37  */
38 extern struct flash_driver lpc2000_flash;
39 extern struct flash_driver lpc288x_flash;
40 extern struct flash_driver lpc2900_flash;
41 extern struct flash_driver cfi_flash;
42 extern struct flash_driver at91sam3_flash;
43 extern struct flash_driver at91sam7_flash;
44 extern struct flash_driver str7x_flash;
45 extern struct flash_driver str9x_flash;
46 extern struct flash_driver aduc702x_flash;
47 extern struct flash_driver stellaris_flash;
48 extern struct flash_driver str9xpec_flash;
49 extern struct flash_driver stm32x_flash;
50 extern struct flash_driver tms470_flash;
51 extern struct flash_driver ecosflash_flash;
52 extern struct flash_driver ocl_flash;
53 extern struct flash_driver pic32mx_flash;
54 extern struct flash_driver avr_flash;
55 extern struct flash_driver faux_flash;
56
57 struct flash_driver *flash_drivers[] = {
58         &lpc2000_flash,
59         &lpc288x_flash,
60         &lpc2900_flash,
61         &cfi_flash,
62         &at91sam7_flash,
63         &at91sam3_flash,
64         &str7x_flash,
65         &str9x_flash,
66         &aduc702x_flash,
67         &stellaris_flash,
68         &str9xpec_flash,
69         &stm32x_flash,
70         &tms470_flash,
71         &ecosflash_flash,
72         &ocl_flash,
73         &pic32mx_flash,
74         &avr_flash,
75         &faux_flash,
76         NULL,
77 };
78
79 struct flash_bank *flash_banks;
80 static  struct command *flash_cmd;
81
82 /* wafer thin wrapper for invoking the flash driver */
83 static int flash_driver_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
84 {
85         int retval;
86
87         retval = bank->driver->write(bank, buffer, offset, count);
88         if (retval != ERROR_OK)
89         {
90                 LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
91                           bank->base, offset, retval);
92         }
93
94         return retval;
95 }
96
97 static int flash_driver_erase(struct flash_bank *bank, int first, int last)
98 {
99         int retval;
100
101         retval = bank->driver->erase(bank, first, last);
102         if (retval != ERROR_OK)
103         {
104                 LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
105         }
106
107         return retval;
108 }
109
110 int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
111 {
112         int retval;
113
114         retval = bank->driver->protect(bank, set, first, last);
115         if (retval != ERROR_OK)
116         {
117                 LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
118         }
119
120         return retval;
121 }
122
123 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
124 {
125         struct flash_bank *p;
126
127         if (argc != 1) {
128                 Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
129                 return JIM_ERR;
130         }
131
132         Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
133         for (p = flash_banks; p; p = p->next)
134         {
135                 Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
136
137                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
138                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
139                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
140                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
141                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
142                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
143                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
144                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
145                 Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
146                 Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
147
148                 Jim_ListAppendElement(interp, list, elem);
149         }
150
151         Jim_SetResult(interp, list);
152
153         return JIM_OK;
154 }
155
156 struct flash_bank *get_flash_bank_by_num_noprobe(int num)
157 {
158         struct flash_bank *p;
159         int i = 0;
160
161         for (p = flash_banks; p; p = p->next)
162         {
163                 if (i++ == num)
164                 {
165                         return p;
166                 }
167         }
168         LOG_ERROR("flash bank %d does not exist", num);
169         return NULL;
170 }
171
172 int flash_get_bank_count(void)
173 {
174         struct flash_bank *p;
175         int i = 0;
176         for (p = flash_banks; p; p = p->next)
177         {
178                 i++;
179         }
180         return i;
181 }
182
183 struct flash_bank *get_flash_bank_by_num(int num)
184 {
185         struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
186         int retval;
187
188         if (p == NULL)
189                 return NULL;
190
191         retval = p->driver->auto_probe(p);
192
193         if (retval != ERROR_OK)
194         {
195                 LOG_ERROR("auto_probe failed %d\n", retval);
196                 return NULL;
197         }
198         return p;
199 }
200
201 COMMAND_HELPER(flash_command_get_bank_by_num,
202         unsigned name_index, struct flash_bank **bank)
203 {
204         const char *name = CMD_ARGV[name_index];
205         unsigned bank_num;
206         COMMAND_PARSE_NUMBER(uint, name, bank_num);
207
208         *bank = get_flash_bank_by_num(bank_num);
209         if (!*bank)
210         {
211                 command_print(CMD_CTX, "flash bank '%s' not found", name);
212                 return ERROR_INVALID_ARGUMENTS;
213         }
214         return ERROR_OK;
215 }
216
217
218 COMMAND_HANDLER(handle_flash_bank_command)
219 {
220         int retval;
221         int i;
222         int found = 0;
223         struct target *target;
224
225         if (CMD_ARGC < 6)
226         {
227                 return ERROR_COMMAND_SYNTAX_ERROR;
228         }
229
230         if ((target = get_target(CMD_ARGV[5])) == NULL)
231         {
232                 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
233                 return ERROR_FAIL;
234         }
235
236         for (i = 0; flash_drivers[i]; i++)
237         {
238                 if (strcmp(CMD_ARGV[0], flash_drivers[i]->name) != 0)
239                         continue;
240
241                 struct flash_bank *p, *c;
242
243                 /* register flash specific commands */
244                 if (flash_drivers[i]->register_commands(CMD_CTX) != ERROR_OK)
245                 {
246                         LOG_ERROR("couldn't register '%s' commands", CMD_ARGV[0]);
247                         return ERROR_FAIL;
248                 }
249
250                 c = malloc(sizeof(struct flash_bank));
251                 c->target = target;
252                 c->driver = flash_drivers[i];
253                 c->driver_priv = NULL;
254                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
255                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
256                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
257                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
258                 c->num_sectors = 0;
259                 c->sectors = NULL;
260                 c->next = NULL;
261
262                 retval = CALL_COMMAND_HANDLER(flash_drivers[i]->flash_bank_command, c);
263                 if (ERROR_OK != retval)
264                 {
265                         LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 , CMD_ARGV[0], c->base);
266                         free(c);
267                         return retval;
268                 }
269
270                 /* put flash bank in linked list */
271                 if (flash_banks)
272                 {
273                         int     bank_num = 0;
274                         /* find last flash bank */
275                         for (p = flash_banks; p && p->next; p = p->next) bank_num++;
276                         if (p)
277                                 p->next = c;
278                         c->bank_number = bank_num + 1;
279                 }
280                 else
281                 {
282                         flash_banks = c;
283                         c->bank_number = 0;
284                 }
285
286                 found = 1;
287         }
288
289         /* no matching flash driver found */
290         if (!found)
291         {
292                 LOG_ERROR("flash driver '%s' not found", CMD_ARGV[0]);
293                 return ERROR_FAIL;
294         }
295
296         return ERROR_OK;
297 }
298
299 COMMAND_HANDLER(handle_flash_info_command)
300 {
301         struct flash_bank *p;
302         uint32_t i = 0;
303         int j = 0;
304         int retval;
305
306         if (CMD_ARGC != 1)
307                 return ERROR_COMMAND_SYNTAX_ERROR;
308
309         unsigned bank_nr;
310         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
311
312         for (p = flash_banks; p; p = p->next, i++)
313         {
314                 if (i != bank_nr)
315                         continue;
316
317                 char buf[1024];
318
319                 /* attempt auto probe */
320                 if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
321                         return retval;
322
323                 command_print(CMD_CTX,
324                               "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
325                               i,
326                               p->driver->name,
327                               p->base,
328                               p->size,
329                               p->bus_width,
330                               p->chip_width);
331                 for (j = 0; j < p->num_sectors; j++)
332                 {
333                         char *protect_state;
334
335                         if (p->sectors[j].is_protected == 0)
336                                 protect_state = "not protected";
337                         else if (p->sectors[j].is_protected == 1)
338                                 protect_state = "protected";
339                         else
340                                 protect_state = "protection state unknown";
341
342                         command_print(CMD_CTX,
343                                       "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
344                                       j,
345                                       p->sectors[j].offset,
346                                       p->sectors[j].size,
347                                       p->sectors[j].size >> 10,
348                                       protect_state);
349                 }
350
351                 *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
352                 retval = p->driver->info(p, buf, sizeof(buf));
353                 command_print(CMD_CTX, "%s", buf);
354                 if (retval != ERROR_OK)
355                         LOG_ERROR("error retrieving flash info (%d)", retval);
356         }
357
358         return ERROR_OK;
359 }
360
361 COMMAND_HANDLER(handle_flash_probe_command)
362 {
363         int retval;
364
365         if (CMD_ARGC != 1)
366         {
367                 return ERROR_COMMAND_SYNTAX_ERROR;
368         }
369
370         unsigned bank_nr;
371         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
372         struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
373         if (p)
374         {
375                 if ((retval = p->driver->probe(p)) == ERROR_OK)
376                 {
377                         command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
378                 }
379                 else if (retval == ERROR_FLASH_BANK_INVALID)
380                 {
381                         command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
382                                                   CMD_ARGV[0], p->base);
383                 }
384                 else
385                 {
386                         command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
387                                                   CMD_ARGV[0], p->base);
388                 }
389         }
390         else
391         {
392                 command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
393         }
394
395         return ERROR_OK;
396 }
397
398 COMMAND_HANDLER(handle_flash_erase_check_command)
399 {
400         if (CMD_ARGC != 1)
401         {
402                 return ERROR_COMMAND_SYNTAX_ERROR;
403         }
404
405         struct flash_bank *p;
406         int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
407         if (ERROR_OK != retval)
408                 return retval;
409
410         int j;
411         if ((retval = p->driver->erase_check(p)) == ERROR_OK)
412         {
413                 command_print(CMD_CTX, "successfully checked erase state");
414         }
415         else
416         {
417                 command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
418                         CMD_ARGV[0], p->base);
419         }
420
421         for (j = 0; j < p->num_sectors; j++)
422         {
423                 char *erase_state;
424
425                 if (p->sectors[j].is_erased == 0)
426                         erase_state = "not erased";
427                 else if (p->sectors[j].is_erased == 1)
428                         erase_state = "erased";
429                 else
430                         erase_state = "erase state unknown";
431
432                 command_print(CMD_CTX,
433                               "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
434                               j,
435                               p->sectors[j].offset,
436                               p->sectors[j].size,
437                               p->sectors[j].size >> 10,
438                               erase_state);
439         }
440
441         return ERROR_OK;
442 }
443
444 COMMAND_HANDLER(handle_flash_erase_address_command)
445 {
446         struct flash_bank *p;
447         int retval;
448         int address;
449         int length;
450
451         struct target *target = get_current_target(CMD_CTX);
452
453         if (CMD_ARGC != 2)
454                 return ERROR_COMMAND_SYNTAX_ERROR;
455
456         COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
457         COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], length);
458         if (length <= 0)
459         {
460                 command_print(CMD_CTX, "Length must be >0");
461                 return ERROR_COMMAND_SYNTAX_ERROR;
462         }
463
464         p = get_flash_bank_by_addr(target, address);
465         if (p == NULL)
466         {
467                 return ERROR_FAIL;
468         }
469
470         /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
471         flash_set_dirty();
472
473         struct duration bench;
474         duration_start(&bench);
475
476         retval = flash_erase_address_range(target, address, length);
477
478         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
479         {
480                 command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
481                                 " in %fs (%0.3f kb/s)", address, length,
482                                 duration_elapsed(&bench), duration_kbps(&bench, length));
483         }
484
485         return retval;
486 }
487
488 COMMAND_HANDLER(handle_flash_protect_check_command)
489 {
490         if (CMD_ARGC != 1)
491                 return ERROR_COMMAND_SYNTAX_ERROR;
492
493         struct flash_bank *p;
494         int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
495         if (ERROR_OK != retval)
496                 return retval;
497
498         if ((retval = p->driver->protect_check(p)) == ERROR_OK)
499         {
500                 command_print(CMD_CTX, "successfully checked protect state");
501         }
502         else if (retval == ERROR_FLASH_OPERATION_FAILED)
503         {
504                 command_print(CMD_CTX, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
505         }
506         else
507         {
508                 command_print(CMD_CTX, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
509         }
510
511         return ERROR_OK;
512 }
513
514 static int flash_check_sector_parameters(struct command_context *cmd_ctx,
515                 uint32_t first, uint32_t last, uint32_t num_sectors)
516 {
517         if (!(first <= last)) {
518                 command_print(cmd_ctx, "ERROR: "
519                                 "first sector must be <= last sector");
520                 return ERROR_FAIL;
521         }
522
523         if (!(last <= (num_sectors - 1))) {
524                 command_print(cmd_ctx, "ERROR: last sector must be <= %d",
525                                 (int) num_sectors - 1);
526                 return ERROR_FAIL;
527         }
528
529         return ERROR_OK;
530 }
531
532 COMMAND_HANDLER(handle_flash_erase_command)
533 {
534         if (CMD_ARGC != 2)
535                 return ERROR_COMMAND_SYNTAX_ERROR;
536
537         uint32_t bank_nr;
538         uint32_t first;
539         uint32_t last;
540
541         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
542         struct flash_bank *p = get_flash_bank_by_num(bank_nr);
543         if (!p)
544                 return ERROR_OK;
545
546         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
547         if (strcmp(CMD_ARGV[2], "last") == 0)
548                 last = p->num_sectors - 1;
549         else
550                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
551
552         int retval;
553         if ((retval = flash_check_sector_parameters(CMD_CTX,
554                         first, last, p->num_sectors)) != ERROR_OK)
555                 return retval;
556
557         struct duration bench;
558         duration_start(&bench);
559
560         retval = flash_driver_erase(p, first, last);
561
562         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
563         {
564                 command_print(CMD_CTX, "erased sectors %" PRIu32 " "
565                                 "through %" PRIu32" on flash bank %" PRIu32 " "
566                                 "in %fs", first, last, bank_nr, duration_elapsed(&bench));
567         }
568
569         return ERROR_OK;
570 }
571
572 COMMAND_HANDLER(handle_flash_protect_command)
573 {
574         if (CMD_ARGC != 3)
575                 return ERROR_COMMAND_SYNTAX_ERROR;
576
577         uint32_t bank_nr;
578         uint32_t first;
579         uint32_t last;
580
581         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
582         struct flash_bank *p = get_flash_bank_by_num(bank_nr);
583         if (!p)
584                 return ERROR_OK;
585
586         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
587         if (strcmp(CMD_ARGV[2], "last") == 0)
588                 last = p->num_sectors - 1;
589         else
590                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
591
592         bool set;
593         COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
594
595         int retval;
596         if ((retval = flash_check_sector_parameters(CMD_CTX,
597                         first, last, p->num_sectors)) != ERROR_OK)
598                 return retval;
599
600         retval = flash_driver_protect(p, set, first, last);
601         if (retval == ERROR_OK) {
602                 command_print(CMD_CTX, "%s protection for sectors %i "
603                                 "through %i on flash bank %i",
604                         (set) ? "set" : "cleared", (int) first,
605                         (int) last, (int) bank_nr);
606         }
607
608         return ERROR_OK;
609 }
610
611 COMMAND_HANDLER(handle_flash_write_image_command)
612 {
613         struct target *target = get_current_target(CMD_CTX);
614
615         struct image image;
616         uint32_t written;
617
618         int retval;
619
620         if (CMD_ARGC < 1)
621         {
622                 return ERROR_COMMAND_SYNTAX_ERROR;
623         }
624
625         /* flash auto-erase is disabled by default*/
626         int auto_erase = 0;
627         bool auto_unlock = false;
628
629         for (;;)
630         {
631                 if (strcmp(CMD_ARGV[0], "erase") == 0)
632                 {
633                         auto_erase = 1;
634                         CMD_ARGV++;
635                         CMD_ARGC--;
636                         command_print(CMD_CTX, "auto erase enabled");
637                 } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
638                 {
639                         auto_unlock = true;
640                         CMD_ARGV++;
641                         CMD_ARGC--;
642                         command_print(CMD_CTX, "auto unlock enabled");
643                 } else
644                 {
645                         break;
646                 }
647         }
648
649         if (CMD_ARGC < 1)
650         {
651                 return ERROR_COMMAND_SYNTAX_ERROR;
652         }
653
654         if (!target)
655         {
656                 LOG_ERROR("no target selected");
657                 return ERROR_FAIL;
658         }
659
660         struct duration bench;
661         duration_start(&bench);
662
663         if (CMD_ARGC >= 2)
664         {
665                 image.base_address_set = 1;
666                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], image.base_address);
667         }
668         else
669         {
670                 image.base_address_set = 0;
671                 image.base_address = 0x0;
672         }
673
674         image.start_address_set = 0;
675
676         retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
677         if (retval != ERROR_OK)
678         {
679                 return retval;
680         }
681
682         retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
683         if (retval != ERROR_OK)
684         {
685                 image_close(&image);
686                 return retval;
687         }
688
689         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
690         {
691                 command_print(CMD_CTX, "wrote %" PRIu32 " byte from file %s "
692                                 "in %fs (%0.3f kb/s)", written, CMD_ARGV[0],
693                                 duration_elapsed(&bench), duration_kbps(&bench, written));
694         }
695
696         image_close(&image);
697
698         return retval;
699 }
700
701 COMMAND_HANDLER(handle_flash_fill_command)
702 {
703         int err = ERROR_OK;
704         uint32_t address;
705         uint32_t pattern;
706         uint32_t count;
707         uint8_t chunk[1024];
708         uint8_t readback[1024];
709         uint32_t wrote = 0;
710         uint32_t cur_size = 0;
711         uint32_t chunk_count;
712         struct target *target = get_current_target(CMD_CTX);
713         uint32_t i;
714         uint32_t wordsize;
715
716         if (CMD_ARGC != 3)
717                 return ERROR_COMMAND_SYNTAX_ERROR;
718
719         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
720         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
721         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
722
723         if (count == 0)
724                 return ERROR_OK;
725
726         switch (CMD_NAME[4])
727         {
728         case 'w':
729                 wordsize = 4;
730                 break;
731         case 'h':
732                 wordsize = 2;
733                 break;
734         case 'b':
735                 wordsize = 1;
736                 break;
737         default:
738                 return ERROR_COMMAND_SYNTAX_ERROR;
739         }
740
741         chunk_count = MIN(count, (1024 / wordsize));
742         switch (wordsize)
743         {
744         case 4:
745                 for (i = 0; i < chunk_count; i++)
746                 {
747                         target_buffer_set_u32(target, chunk + i * wordsize, pattern);
748                 }
749                 break;
750         case 2:
751                 for (i = 0; i < chunk_count; i++)
752                 {
753                         target_buffer_set_u16(target, chunk + i * wordsize, pattern);
754                 }
755                 break;
756         case 1:
757                 memset(chunk, pattern, chunk_count);
758                 break;
759         default:
760                 LOG_ERROR("BUG: can't happen");
761                 exit(-1);
762         }
763
764         struct duration bench;
765         duration_start(&bench);
766
767         for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
768         {
769                 cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
770                 struct flash_bank *bank;
771                 bank = get_flash_bank_by_addr(target, address);
772                 if (bank == NULL)
773                 {
774                         return ERROR_FAIL;
775                 }
776                 err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
777                 if (err != ERROR_OK)
778                         return err;
779
780                 err = target_read_buffer(target, address + wrote, cur_size, readback);
781                 if (err != ERROR_OK)
782                         return err;
783
784                 unsigned i;
785                 for (i = 0; i < cur_size; i++)
786                 {
787                         if (readback[i]!=chunk[i])
788                         {
789                                 LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
790                                                   address + wrote + i, readback[i], chunk[i]);
791                                 return ERROR_FAIL;
792                         }
793                 }
794         }
795
796         if (duration_measure(&bench) == ERROR_OK)
797         {
798                 command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
799                                 " in %fs (%0.3f kb/s)", wrote, address,
800                                 duration_elapsed(&bench), duration_kbps(&bench, wrote));
801         }
802         return ERROR_OK;
803 }
804
805 COMMAND_HANDLER(handle_flash_write_bank_command)
806 {
807         uint32_t offset;
808         uint8_t *buffer;
809         struct fileio fileio;
810
811         if (CMD_ARGC != 3)
812                 return ERROR_COMMAND_SYNTAX_ERROR;
813
814         struct duration bench;
815         duration_start(&bench);
816
817         struct flash_bank *p;
818         int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_by_num, 0, &p);
819         if (ERROR_OK != retval)
820                 return retval;
821
822         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
823
824         if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
825         {
826                 return ERROR_OK;
827         }
828
829         buffer = malloc(fileio.size);
830         size_t buf_cnt;
831         if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
832         {
833                 free(buffer);
834                 fileio_close(&fileio);
835                 return ERROR_OK;
836         }
837
838         retval = flash_driver_write(p, buffer, offset, buf_cnt);
839
840         free(buffer);
841         buffer = NULL;
842
843         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
844         {
845                 command_print(CMD_CTX, "wrote %zu byte from file %s to flash bank %u"
846                                 " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
847                                 fileio.size, CMD_ARGV[1], p->bank_number, offset,
848                                 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
849         }
850
851         fileio_close(&fileio);
852
853         return retval;
854 }
855
856 void flash_set_dirty(void)
857 {
858         struct flash_bank *c;
859         int i;
860
861         /* set all flash to require erasing */
862         for (c = flash_banks; c; c = c->next)
863         {
864                 for (i = 0; i < c->num_sectors; i++)
865                 {
866                         c->sectors[i].is_erased = 0;
867                 }
868         }
869 }
870
871 /* lookup flash bank by address */
872 struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
873 {
874         struct flash_bank *c;
875
876         /* cycle through bank list */
877         for (c = flash_banks; c; c = c->next)
878         {
879                 int retval;
880                 retval = c->driver->auto_probe(c);
881
882                 if (retval != ERROR_OK)
883                 {
884                         LOG_ERROR("auto_probe failed %d\n", retval);
885                         return NULL;
886                 }
887                 /* check whether address belongs to this flash bank */
888                 if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
889                         return c;
890         }
891         LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
892         return NULL;
893 }
894
895 /* erase given flash region, selects proper bank according to target and address */
896 static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
897                 int (*callback)(struct flash_bank *bank, int first, int last))
898 {
899         struct flash_bank *c;
900         int first = -1;
901         int last = -1;
902         int i;
903
904         if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
905                 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
906
907         if (c->size == 0 || c->num_sectors == 0)
908         {
909                 LOG_ERROR("Bank is invalid");
910                 return ERROR_FLASH_BANK_INVALID;
911         }
912
913         if (length == 0)
914         {
915                 /* special case, erase whole bank when length is zero */
916                 if (addr != c->base)
917                         return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
918
919                 return callback(c, 0, c->num_sectors - 1);
920         }
921
922         /* check whether it fits */
923         if (addr + length - 1 > c->base + c->size - 1)
924                 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
925
926         addr -= c->base;
927
928         for (i = 0; i < c->num_sectors; i++)
929         {
930                 /* check whether sector overlaps with the given range and is not yet erased */
931                 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
932                         /* if first is not set yet then this is the first sector */
933                         if (first == -1)
934                                 first = i;
935                         last = i; /* and it is the last one so far in any case */
936                 }
937         }
938
939         if (first == -1 || last == -1)
940                 return ERROR_OK;
941
942         return callback(c, first, last);
943 }
944
945
946
947 int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
948 {
949         return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
950 }
951
952 static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
953 {
954         return flash_driver_protect(bank, 0, first, last);
955 }
956
957 static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
958 {
959         return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
960 }
961
962
963 /* write (optional verify) an image to flash memory of the given target */
964 static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock)
965 {
966         int retval = ERROR_OK;
967
968         int section;
969         uint32_t section_offset;
970         struct flash_bank *c;
971         int *padding;
972
973         section = 0;
974         section_offset = 0;
975
976         if (written)
977                 *written = 0;
978
979         if (erase)
980         {
981                 /* assume all sectors need erasing - stops any problems
982                  * when flash_write is called multiple times */
983
984                 flash_set_dirty();
985         }
986
987         /* allocate padding array */
988         padding = malloc(image->num_sections * sizeof(padding));
989
990         /* loop until we reach end of the image */
991         while (section < image->num_sections)
992         {
993                 uint32_t buffer_size;
994                 uint8_t *buffer;
995                 int section_first;
996                 int section_last;
997                 uint32_t run_address = image->sections[section].base_address + section_offset;
998                 uint32_t run_size = image->sections[section].size - section_offset;
999                 int pad_bytes = 0;
1000
1001                 if (image->sections[section].size ==  0)
1002                 {
1003                         LOG_WARNING("empty section %d", section);
1004                         section++;
1005                         section_offset = 0;
1006                         continue;
1007                 }
1008
1009                 /* find the corresponding flash bank */
1010                 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
1011                 {
1012                         section++; /* and skip it */
1013                         section_offset = 0;
1014                         continue;
1015                 }
1016
1017                 /* collect consecutive sections which fall into the same bank */
1018                 section_first = section;
1019                 section_last = section;
1020                 padding[section] = 0;
1021                 while ((run_address + run_size - 1 < c->base + c->size - 1)
1022                                 && (section_last + 1 < image->num_sections))
1023                 {
1024                         if (image->sections[section_last + 1].base_address < (run_address + run_size))
1025                         {
1026                                 LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
1027                                 break;
1028                         }
1029                         /* if we have multiple sections within our image, flash programming could fail due to alignment issues
1030                          * attempt to rebuild a consecutive buffer for the flash loader */
1031                         pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
1032                         if ((run_address + run_size + pad_bytes) > (c->base + c->size))
1033                                 break;
1034                         padding[section_last] = pad_bytes;
1035                         run_size += image->sections[++section_last].size;
1036                         run_size += pad_bytes;
1037                         padding[section_last] = 0;
1038
1039                         LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
1040                 }
1041
1042                 /* fit the run into bank constraints */
1043                 if (run_address + run_size - 1 > c->base + c->size - 1)
1044                 {
1045                         LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
1046                                     (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
1047                         run_size = c->base + c->size - run_address;
1048                 }
1049
1050                 /* allocate buffer */
1051                 buffer = malloc(run_size);
1052                 buffer_size = 0;
1053
1054                 /* read sections to the buffer */
1055                 while (buffer_size < run_size)
1056                 {
1057                         size_t size_read;
1058
1059                         size_read = run_size - buffer_size;
1060                         if (size_read > image->sections[section].size - section_offset)
1061                             size_read = image->sections[section].size - section_offset;
1062
1063                         if ((retval = image_read_section(image, section, section_offset,
1064                                         size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
1065                         {
1066                                 free(buffer);
1067                                 free(padding);
1068                                 return retval;
1069                         }
1070
1071                         /* see if we need to pad the section */
1072                         while (padding[section]--)
1073                                  (buffer + buffer_size)[size_read++] = 0xff;
1074
1075                         buffer_size += size_read;
1076                         section_offset += size_read;
1077
1078                         if (section_offset >= image->sections[section].size)
1079                         {
1080                                 section++;
1081                                 section_offset = 0;
1082                         }
1083                 }
1084
1085                 retval = ERROR_OK;
1086
1087                 if (unlock)
1088                 {
1089                         retval = flash_unlock_address_range(target, run_address, run_size);
1090                 }
1091                 if (retval == ERROR_OK)
1092                 {
1093                         if (erase)
1094                         {
1095                                 /* calculate and erase sectors */
1096                                 retval = flash_erase_address_range(target, run_address, run_size);
1097                         }
1098                 }
1099
1100                 if (retval == ERROR_OK)
1101                 {
1102                         /* write flash sectors */
1103                         retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
1104                 }
1105
1106                 free(buffer);
1107
1108                 if (retval != ERROR_OK)
1109                 {
1110                         free(padding);
1111                         return retval; /* abort operation */
1112                 }
1113
1114                 if (written != NULL)
1115                         *written += run_size; /* add run size to total written counter */
1116         }
1117
1118         free(padding);
1119
1120         return retval;
1121 }
1122
1123 int flash_write(struct target *target, struct image *image, uint32_t *written, int erase)
1124 {
1125         return flash_write_unlock(target, image, written, erase, false);
1126 }
1127
1128 int default_flash_mem_blank_check(struct flash_bank *bank)
1129 {
1130         struct target *target = bank->target;
1131         uint8_t buffer[1024];
1132         int buffer_size = sizeof(buffer);
1133         int i;
1134         uint32_t nBytes;
1135
1136         if (bank->target->state != TARGET_HALTED)
1137         {
1138                 LOG_ERROR("Target not halted");
1139                 return ERROR_TARGET_NOT_HALTED;
1140         }
1141
1142         for (i = 0; i < bank->num_sectors; i++)
1143         {
1144                 uint32_t j;
1145                 bank->sectors[i].is_erased = 1;
1146
1147                 for (j = 0; j < bank->sectors[i].size; j += buffer_size)
1148                 {
1149                         uint32_t chunk;
1150                         int retval;
1151                         chunk = buffer_size;
1152                         if (chunk > (j - bank->sectors[i].size))
1153                         {
1154                                 chunk = (j - bank->sectors[i].size);
1155                         }
1156
1157                         retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
1158                         if (retval != ERROR_OK)
1159                                 return retval;
1160
1161                         for (nBytes = 0; nBytes < chunk; nBytes++)
1162                         {
1163                                 if (buffer[nBytes] != 0xFF)
1164                                 {
1165                                         bank->sectors[i].is_erased = 0;
1166                                         break;
1167                                 }
1168                         }
1169                 }
1170         }
1171
1172         return ERROR_OK;
1173 }
1174
1175 int default_flash_blank_check(struct flash_bank *bank)
1176 {
1177         struct target *target = bank->target;
1178         int i;
1179         int retval;
1180         int fast_check = 0;
1181         uint32_t blank;
1182
1183         if (bank->target->state != TARGET_HALTED)
1184         {
1185                 LOG_ERROR("Target not halted");
1186                 return ERROR_TARGET_NOT_HALTED;
1187         }
1188
1189         for (i = 0; i < bank->num_sectors; i++)
1190         {
1191                 uint32_t address = bank->base + bank->sectors[i].offset;
1192                 uint32_t size = bank->sectors[i].size;
1193
1194                 if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
1195                 {
1196                         fast_check = 0;
1197                         break;
1198                 }
1199                 if (blank == 0xFF)
1200                         bank->sectors[i].is_erased = 1;
1201                 else
1202                         bank->sectors[i].is_erased = 0;
1203                 fast_check = 1;
1204         }
1205
1206         if (!fast_check)
1207         {
1208                 LOG_USER("Running slow fallback erase check - add working memory");
1209                 return default_flash_mem_blank_check(bank);
1210         }
1211
1212         return ERROR_OK;
1213 }
1214
1215 int flash_init_drivers(struct command_context *cmd_ctx)
1216 {
1217         register_jim(cmd_ctx, "ocd_flash_banks",
1218                         jim_flash_banks, "return information about the flash banks");
1219
1220         if (!flash_banks)
1221                 return ERROR_OK;
1222
1223         register_command(cmd_ctx, flash_cmd, "info",
1224                         handle_flash_info_command, COMMAND_EXEC,
1225                         "print info about flash bank <num>");
1226         register_command(cmd_ctx, flash_cmd, "probe",
1227                         handle_flash_probe_command, COMMAND_EXEC,
1228                         "identify flash bank <num>");
1229         register_command(cmd_ctx, flash_cmd, "erase_check",
1230                         handle_flash_erase_check_command, COMMAND_EXEC,
1231                         "check erase state of sectors in flash bank <num>");
1232         register_command(cmd_ctx, flash_cmd, "protect_check",
1233                         handle_flash_protect_check_command, COMMAND_EXEC,
1234                         "check protection state of sectors in flash bank <num>");
1235         register_command(cmd_ctx, flash_cmd, "erase_sector",
1236                         handle_flash_erase_command, COMMAND_EXEC,
1237                         "erase sectors at <bank> <first> <last>");
1238         register_command(cmd_ctx, flash_cmd, "erase_address",
1239                         handle_flash_erase_address_command, COMMAND_EXEC,
1240                         "erase address range <address> <length>");
1241
1242         register_command(cmd_ctx, flash_cmd, "fillw",
1243                         handle_flash_fill_command, COMMAND_EXEC,
1244                         "fill with pattern (no autoerase) <address> <word_pattern> <count>");
1245         register_command(cmd_ctx, flash_cmd, "fillh",
1246                         handle_flash_fill_command, COMMAND_EXEC,
1247                         "fill with pattern <address> <halfword_pattern> <count>");
1248         register_command(cmd_ctx, flash_cmd, "fillb",
1249                         handle_flash_fill_command, COMMAND_EXEC,
1250                         "fill with pattern <address> <byte_pattern> <count>");
1251
1252         register_command(cmd_ctx, flash_cmd, "write_bank",
1253                         handle_flash_write_bank_command, COMMAND_EXEC,
1254                         "write binary data to <bank> <file> <offset>");
1255         register_command(cmd_ctx, flash_cmd, "write_image",
1256                         handle_flash_write_image_command, COMMAND_EXEC,
1257                         "write_image [erase] [unlock] <file> [offset] [type]");
1258         register_command(cmd_ctx, flash_cmd, "protect",
1259                         handle_flash_protect_command, COMMAND_EXEC,
1260                         "set protection of sectors at <bank> <first> <last> <on | off>");
1261
1262         return ERROR_OK;
1263 }
1264
1265 int flash_register_commands(struct command_context *cmd_ctx)
1266 {
1267         flash_cmd = register_command(cmd_ctx, NULL, "flash",
1268                         NULL, COMMAND_ANY, NULL);
1269
1270         register_command(cmd_ctx, flash_cmd, "bank",
1271                         handle_flash_bank_command, COMMAND_CONFIG,
1272                         "flash bank <driver> <base> <size> "
1273                         "<chip_width> <bus_width> <target> [driver_options ...]");
1274         return ERROR_OK;
1275 }