- initialize num_sectors and sectors in handle_flash_bank_command.
[fw/openocd] / src / flash / flash.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "flash.h"
25 #include "command.h"
26 #include "target.h"
27 #include "time_support.h"
28 #include "fileio.h"
29 #include "image.h"
30 #include "log.h"
31
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <inttypes.h>
39
40 /* command handlers */
41 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
42 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
43 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53
54 /* flash drivers
55  */
56 extern flash_driver_t lpc2000_flash;
57 extern flash_driver_t cfi_flash;
58 extern flash_driver_t at91sam7_flash;
59 extern flash_driver_t str7x_flash;
60 extern flash_driver_t str9x_flash;
61 extern flash_driver_t stellaris_flash;
62 extern flash_driver_t str9xpec_flash;
63 extern flash_driver_t stm32x_flash;
64
65 flash_driver_t *flash_drivers[] =
66 {
67         &lpc2000_flash,
68         &cfi_flash,
69         &at91sam7_flash,
70         &str7x_flash,
71         &str9x_flash,
72         &stellaris_flash,
73         &str9xpec_flash,
74         &stm32x_flash,
75         NULL,
76 };
77
78 flash_bank_t *flash_banks;
79 static  command_t *flash_cmd;
80 static int auto_erase = 0;
81
82 int flash_register_commands(struct command_context_s *cmd_ctx)
83 {
84         flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
85         
86         register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
87         
88         return ERROR_OK;
89 }
90
91 int flash_init(struct command_context_s *cmd_ctx)
92 {
93         if (flash_banks)
94         {
95                 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
96                                                  "list configured flash banks ");
97                 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
98                                                  "print info about flash bank <num>");
99                 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
100                                                  "identify flash bank <num>");
101                 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
102                                                  "check erase state of sectors in flash bank <num>");
103                 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
104                                                  "check protection state of sectors in flash bank <num>");
105                 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
106                                                  "erase sectors at <bank> <first> <last>");
107                 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_binary_command, COMMAND_EXEC,
108                                                  "DEPRECATED, use 'write_binary' or 'write_image' instead");
109                 register_command(cmd_ctx, flash_cmd, "write_binary", handle_flash_write_binary_command, COMMAND_EXEC,
110                                                  "write binary <bank> <file> <offset>");
111                 register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
112                                                  "write image <file> [offset] [type]");
113                 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
114                                                  "set protection of sectors at <bank> <first> <last> <on|off>");
115                 register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_EXEC,
116                                                  "auto erase flash sectors <on|off>");
117         }
118         
119         return ERROR_OK;
120 }
121
122 flash_bank_t *get_flash_bank_by_num(int num)
123 {
124         flash_bank_t *p;
125         int i = 0;
126
127         for (p = flash_banks; p; p = p->next)
128         {
129                 if (i++ == num)
130                 {
131                         return p;
132                 }
133         }
134         
135         return NULL;
136 }
137
138 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]
139  */
140 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
141 {
142         int i;
143         int found = 0;
144         target_t *target;
145                 
146         if (argc < 6)
147         {
148                 WARNING("incomplete flash_bank configuration");
149                 WARNING("flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
150                 return ERROR_OK;
151         }
152         
153         if ((target = get_target_by_num(strtoul(args[5], NULL, 0))) == NULL)
154         {
155                 ERROR("target %lu not defined", strtoul(args[5], NULL, 0));
156                 return ERROR_OK;
157         }
158         
159         for (i = 0; flash_drivers[i]; i++)
160         {
161                 if (strcmp(args[0], flash_drivers[i]->name) == 0)
162                 {
163                         flash_bank_t *p, *c;
164                         
165                         /* register flash specific commands */
166                         if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
167                         {
168                                 ERROR("couldn't register '%s' commands", args[0]);
169                                 exit(-1);
170                         }
171                         
172                         c = malloc(sizeof(flash_bank_t));
173                         c->target = target;
174                         c->driver = flash_drivers[i];
175                         c->driver_priv = NULL;
176                         c->base = strtoul(args[1], NULL, 0);
177                         c->size = strtoul(args[2], NULL, 0);
178                         c->chip_width = strtoul(args[3], NULL, 0);
179                         c->bus_width = strtoul(args[4], NULL, 0);\r
180                         c->num_sectors = 0;\r
181                         c->sectors = NULL;
182                         c->next = NULL;
183                         
184                         if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
185                         {
186                                 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
187                                 free(c);
188                                 return ERROR_OK;
189                         }
190                         
191                         /* put flash bank in linked list */
192                         if (flash_banks)
193                         {
194                                 /* find last flash bank */
195                                 for (p = flash_banks; p && p->next; p = p->next);
196                                 if (p)
197                                         p->next = c;
198                         }
199                         else
200                         {
201                                 flash_banks = c;
202                         }
203                         
204                         found = 1;
205                 }
206         }
207                 
208         /* no matching flash driver found */
209         if (!found)
210         {
211                 ERROR("flash driver '%s' not found", args[0]);
212                 exit(-1);
213         }
214         
215         return ERROR_OK;
216 }
217
218 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
219 {
220         flash_bank_t *p;
221         int i = 0;
222         
223         if (!flash_banks)
224         {
225                 command_print(cmd_ctx, "no flash banks configured");
226                 return ERROR_OK;
227         }
228         
229         for (p = flash_banks; p; p = p->next)
230         {
231                 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
232                                           i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
233         }
234         
235         return ERROR_OK;
236 }
237
238 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
239 {
240         flash_bank_t *p;
241         int i = 0;
242         int j = 0;
243                 
244         if (argc != 1)
245         {
246                 command_print(cmd_ctx, "usage: flash info <num>");
247                 return ERROR_OK;
248         }
249         
250         for (p = flash_banks; p; p = p->next, i++)
251         {
252                 if (i == strtoul(args[0], NULL, 0))
253                 {
254                         char buf[1024];
255                         
256                         command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
257                                                 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
258                         for (j = 0; j < p->num_sectors; j++)
259                         {
260                                 char *erase_state, *protect_state;
261                                 
262                                 if (p->sectors[j].is_erased == 0)
263                                         erase_state = "not erased";
264                                 else if (p->sectors[j].is_erased == 1)
265                                         erase_state = "erased";
266                                 else
267                                         erase_state = "erase state unknown";
268                                 
269                                 if (p->sectors[j].is_protected == 0)
270                                         protect_state = "not protected";
271                                 else if (p->sectors[j].is_protected == 1)
272                                         protect_state = "protected";
273                                 else
274                                         protect_state = "protection state unknown";
275
276                                 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s, %s",
277                                                         j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
278                                                         erase_state, protect_state);
279                         }
280                         
281                         p->driver->info(p, buf, 1024);
282                         command_print(cmd_ctx, "%s", buf);
283                 }
284         }
285         
286         return ERROR_OK;
287 }
288
289 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
290 {
291         flash_bank_t *p;
292         int retval;
293                 
294         if (argc != 1)
295         {
296                 command_print(cmd_ctx, "usage: flash probe <num>");
297                 return ERROR_OK;
298         }
299         
300         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
301         if (p)
302         {
303                 if ((retval = p->driver->probe(p)) == ERROR_OK)
304                 {
305                         command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
306                 }
307                 else if (retval == ERROR_FLASH_BANK_INVALID)
308                 {
309                         command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
310                                                   args[0], p->base);
311                 }
312                 else
313                 {
314                         command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
315                                                   args[0], p->base);
316                 }
317         }
318         else
319         {
320                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
321         }
322         
323         return ERROR_OK;
324 }
325
326 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
327 {
328         flash_bank_t *p;
329         int retval;
330                 
331         if (argc != 1)
332         {
333                 command_print(cmd_ctx, "usage: flash erase_check <num>");
334                 return ERROR_OK;
335         }
336         
337         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
338         if (p)
339         {
340                 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
341                 {
342                         command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
343                 }
344                 else
345                 {
346                         command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
347                                 args[0], p->base);
348                 }
349         }
350         else
351         {
352                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
353         }
354         
355         return ERROR_OK;
356 }
357
358 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
359 {
360         flash_bank_t *p;
361         int retval;
362                 
363         if (argc != 1)
364         {
365                 command_print(cmd_ctx, "usage: flash protect_check <num>");
366                 return ERROR_OK;
367         }
368         
369         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
370         if (p)
371         {
372                 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
373                 {
374                         command_print(cmd_ctx, "successfully checked protect state");
375                 }
376                 else if (retval == ERROR_FLASH_OPERATION_FAILED)
377                 {
378                         command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
379                 }
380                 else
381                 {
382                         command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
383                 }
384         }
385         else
386         {
387                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
388         }
389         
390         return ERROR_OK;
391 }
392
393 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
394 {
395         if (argc > 2)
396         {
397                 int first = strtoul(args[1], NULL, 0);
398                 int last = strtoul(args[2], NULL, 0);
399                 int retval;
400                 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
401                 duration_t duration;
402                 char *duration_text;
403         
404                 duration_start_measure(&duration);
405         
406                 if (!p)
407                 {
408                         command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
409                         return ERROR_OK;
410                 }
411                 
412                 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
413                 {
414                         switch (retval)
415                         {
416                                 case ERROR_TARGET_NOT_HALTED:
417                                         command_print(cmd_ctx, "can't work with this flash while target is running");
418                                         break;
419                                 case ERROR_INVALID_ARGUMENTS:
420                                         command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
421                                         break;
422                                 case ERROR_FLASH_BANK_INVALID:
423                                         command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
424                                         break;
425                                 case ERROR_FLASH_OPERATION_FAILED:
426                                         command_print(cmd_ctx, "flash erase error");
427                                         break;
428                                 case ERROR_FLASH_SECTOR_INVALID:
429                                         command_print(cmd_ctx, "sector number(s) invalid");
430                                         break;
431                                 case ERROR_OK:
432                                         command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
433                                         break;
434                                 default:
435                                         command_print(cmd_ctx, "unknown error");
436                         }
437                 }
438                 else
439                 {
440                         duration_stop_measure(&duration, &duration_text);       
441                         
442                         command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
443                         free(duration_text);
444                 }
445         }
446         else
447         {
448                 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
449         }
450
451         return ERROR_OK;
452 }
453
454 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
455 {
456         if (argc > 3)
457         {
458                 int first = strtoul(args[1], NULL, 0);
459                 int last = strtoul(args[2], NULL, 0);
460                 int set;
461                 int retval;
462                 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
463                 if (!p)
464                 {
465                         command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
466                         return ERROR_OK;
467                 }
468                 
469                 if (strcmp(args[3], "on") == 0)
470                         set = 1;
471                 else if (strcmp(args[3], "off") == 0)
472                         set = 0;
473                 else
474                 {
475                         command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
476                         return ERROR_OK;
477                 }
478                 
479                 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
480                 {
481                         switch (retval)
482                         {
483                                 case ERROR_TARGET_NOT_HALTED:
484                                         command_print(cmd_ctx, "can't work with this flash while target is running");
485                                         break;
486                                 case ERROR_INVALID_ARGUMENTS:
487                                         command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
488                                         break;
489                                 case ERROR_FLASH_BANK_INVALID:
490                                         command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
491                                         break;
492                                 case ERROR_FLASH_OPERATION_FAILED:
493                                         command_print(cmd_ctx, "flash program error");
494                                         break;
495                                 case ERROR_FLASH_SECTOR_INVALID:
496                                         command_print(cmd_ctx, "sector number(s) invalid");
497                                         break;
498                                 case ERROR_OK:
499                                         command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
500                                         break;
501                                 default:
502                                         command_print(cmd_ctx, "unknown error");
503                         }
504                 }
505                 else
506                 {
507                         command_print(cmd_ctx, "%s protection for sectors %i through %i on flash bank %i", (set) ? "set" : "cleared", first, last, strtoul(args[0], 0, 0));
508                 }
509         }
510         else
511         {
512                 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
513         }
514
515         return ERROR_OK;
516 }
517
518 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
519 {
520         target_t *target = get_current_target(cmd_ctx);
521         
522         image_t image;
523         u32 written;
524         char *error_str;
525         int *failed;
526         
527         int i;
528         
529         duration_t duration;
530         char *duration_text;
531         
532         int retval;
533         
534         if (!strcmp(cmd, "write"))
535         {
536                 command_print(cmd_ctx, "'flash write' has been deprecated in favor of 'flash write_binary' and 'flash write_image'");
537                 DEBUG("'flash write' has been deprecated in favor of 'flash write_binary' and 'flash write_image'");
538         }
539
540         if (argc < 1)
541         {
542                 command_print(cmd_ctx, "usage: flash write <file> [offset] [type]");
543                 return ERROR_OK;
544         }
545         
546         if (!target)
547         {
548                 ERROR("no target selected");
549                 return ERROR_OK;
550         }
551         
552         duration_start_measure(&duration);
553         
554         if (argc >= 2)
555         {
556                 image.base_address_set = 1;
557                 image.base_address = strtoul(args[1], NULL, 0);
558         }
559         else
560         {
561                 image.base_address_set = 0;
562                 image.base_address = 0x0;
563         }
564         
565         image.start_address_set = 0;
566
567         if (image_open(&image, args[0], (argc == 4) ? args[2] : NULL) != ERROR_OK)
568         {
569                 command_print(cmd_ctx, "flash write error: %s", image.error_str);
570                 return ERROR_OK;
571         }
572         
573         failed = malloc(sizeof(int) * image.num_sections);
574
575         error_str=NULL;
576         retval = flash_write(target, &image, &written, &error_str, failed, auto_erase);
577         
578         if (retval != ERROR_OK)
579         {
580                 if (error_str)
581                 {
582                         command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
583                         free(error_str);
584                 }
585                 image_close(&image);
586                 free(failed);
587                 return retval;
588         }
589         
590         for (i = 0; i < image.num_sections; i++)
591         {
592                 if (failed[i])
593                 {
594                         command_print(cmd_ctx, "didn't write section at 0x%8.8x, size 0x%8.8x",
595                                         image.sections[i].base_address, image.sections[i].size);
596                 }
597         }
598         
599         duration_stop_measure(&duration, &duration_text);
600         command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
601                 written, args[0], duration_text,
602                 (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
603
604         free(duration_text);
605         free(failed);
606
607         image_close(&image);
608         
609         return ERROR_OK;
610 }
611
612 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
613 {
614         u32 offset;
615         u8 *buffer;
616         u32 buf_cnt;
617
618         fileio_t fileio;
619         
620         duration_t duration;
621         char *duration_text;
622         
623         int retval;
624         flash_bank_t *p;
625
626         if (argc < 3)
627         {
628                 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
629                 return ERROR_OK;
630         }
631         
632         duration_start_measure(&duration);
633         
634         offset = strtoul(args[2], NULL, 0);
635         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
636         if (!p)
637         {
638                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
639                 return ERROR_OK;
640         }
641         
642         if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
643         {
644                 command_print(cmd_ctx, "flash write error: %s", fileio.error_str);
645                 return ERROR_OK;
646         }
647         
648         buffer = malloc(fileio.size);
649         if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
650         {
651                 command_print(cmd_ctx, "flash write error: %s", fileio.error_str);
652                 return ERROR_OK;
653         }
654         
655         if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
656         {
657                 command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
658                         args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
659                 switch (retval)
660                 {
661                         case ERROR_TARGET_NOT_HALTED:
662                                 command_print(cmd_ctx, "can't work with this flash while target is running");
663                                 break;
664                         case ERROR_INVALID_ARGUMENTS:
665                                 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
666                                 break;
667                         case ERROR_FLASH_BANK_INVALID:
668                                 command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
669                                 break;
670                         case ERROR_FLASH_OPERATION_FAILED:
671                                 command_print(cmd_ctx, "flash program error");
672                                 break;
673                         case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
674                                 command_print(cmd_ctx, "offset breaks required alignment");
675                                 break;
676                         case ERROR_FLASH_DST_OUT_OF_BANK:
677                                 command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
678                                 break;
679                         case ERROR_FLASH_SECTOR_NOT_ERASED:
680                                 command_print(cmd_ctx, "destination sector(s) not erased");
681                                 break;
682                         default:
683                                 command_print(cmd_ctx, "unknown error");
684                 }
685         }
686
687         free(buffer);
688         
689         duration_stop_measure(&duration, &duration_text);
690         command_print(cmd_ctx, "wrote  %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
691                 fileio.size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
692                 (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
693         free(duration_text);
694
695         fileio_close(&fileio);
696         
697         return ERROR_OK;
698 }
699
700 /* lookup flash bank by address */
701 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
702 {
703         flash_bank_t *c;
704
705         /* cycle through bank list */
706         for (c = flash_banks; c; c = c->next)
707         {
708                 /* check whether address belongs to this flash bank */
709                 if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
710                         return c;
711         }
712
713         return NULL;
714 }
715
716 /* erase given flash region, selects proper bank according to target and address */
717 int flash_erase(target_t *target, u32 addr, u32 length)
718 {
719         flash_bank_t *c;
720         int first = -1;
721         int last = -1;
722         int i;
723         
724         if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
725                 return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
726
727         if (c->size == 0 || c->num_sectors == 0)
728                 return ERROR_FLASH_BANK_INVALID;
729         
730         if (length == 0)
731         {
732                 /* special case, erase whole bank when length is zero */
733                 if (addr != c->base)
734                         return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
735                 
736                 return c->driver->erase(c, 0, c->num_sectors - 1);
737         }
738
739         /* check whether it fits */
740         if (addr + length > c->base + c->size)
741                 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
742         
743         addr -= c->base;
744         
745         for (i = 0; i < c->num_sectors; i++)
746         {               
747                 /* check whether sector overlaps with the given range and is not yet erased */
748                 if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
749                         /* if first is not set yet then this is the first sector */
750                         if (first == -1)
751                                 first = i;
752                         last = i; /* and it is the last one so far in any case */
753                 }
754         }
755         
756         if( first == -1 || last == -1 )
757                 return ERROR_OK;
758         
759         return c->driver->erase(c, first, last);
760 }
761
762 /* write an image to flash memory of the given target */
763 int flash_write(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, int erase)
764 {
765         int retval;
766         int i;
767
768         int section;
769         u32 section_offset;
770
771         section = 0;
772         section_offset = 0;
773
774         if (written)
775                 *written = 0;
776         
777         if (failed != NULL)
778                 for (i = 0; i < image->num_sections; i++)
779                         failed[i] = 0;
780
781         /* loop until we reach end of the image */
782         while (section < image->num_sections)
783         {
784                 flash_bank_t *c;
785                 u32 buffer_size;
786                 u8 *buffer;
787                 int section_first;
788                 int section_last;
789                 u32 run_address = image->sections[section].base_address+section_offset;
790                 u32 run_size = image->sections[section].size-section_offset;
791
792                 if (image->sections[section].size ==  0)
793                 {
794                         WARNING("empty section %d", section);
795                         section++;
796                         section_offset = 0;
797                         continue;
798                 }
799
800                 /* find the corresponding flash bank */
801                 if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
802                 {
803                         if (failed == NULL)
804                         {
805                                 if (error_str == NULL)
806                                         return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
807                                 *error_str = malloc(FLASH_MAX_ERROR_STR);
808                                 snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
809                                 return ERROR_FLASH_DST_OUT_OF_BANK; /* abort operation */
810                         }
811                         failed[section] = ERROR_FLASH_DST_OUT_OF_BANK; /* mark the section as failed */
812                         section++; /* and skip it */
813                         section_offset = 0;
814                         continue;
815                 }
816
817                 /* collect consecutive sections which fall into the same bank */
818                 section_first = section;
819                 section_last = section;
820                 while ((run_address + run_size < c->base + c->size)
821                                 && (section_last + 1 < image->num_sections))
822                 {
823                         if (image->sections[section_last + 1].base_address < (run_address + run_size))
824                         {
825                                 WARNING("section %d out of order", section_last + 1);
826                                 break;
827                         }
828                         if (image->sections[section_last + 1].base_address != (run_address + run_size))
829                                 break;
830                         run_size += image->sections[++section_last].size;
831                 }
832
833                 /* fit the run into bank constraints */
834                 if (run_address + run_size > c->base + c->size)
835                         run_size = c->base + c->size - run_address;
836
837                 /* allocate buffer */
838                 buffer = malloc(run_size);
839                 buffer_size = 0;
840
841                 /* read sections to the buffer */
842                 while (buffer_size < run_size)
843                 {
844                         u32 size_read;
845                         
846                         if (buffer_size - run_size <= image->sections[section].size - section_offset)
847                                 size_read = buffer_size - run_size;
848                         else
849                                 size_read = image->sections[section].size - section_offset;
850                         
851                         if ((retval = image_read_section(image, section, section_offset,
852                                         run_size, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
853                         {
854                                 free(buffer);
855                                 
856                                 if (error_str == NULL)
857                                         return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
858                                 
859                                 *error_str = malloc(FLASH_MAX_ERROR_STR);
860                                 
861                                 /* if image_read_section returned an error there's an error string we can pass on */
862                                 if (retval != ERROR_OK)
863                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image: %s", image->error_str);
864                                 else
865                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "error reading from image");
866                                 
867                                 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
868                         }
869
870                         buffer_size += size_read;
871                         section_offset += size_read;
872
873                         if (section_offset >= image->sections[section].size)
874                         {
875                                 section++;
876                                 section_offset = 0;
877                         }
878                 }
879
880                 retval = ERROR_OK;
881                 
882                 if (erase)
883                 {
884                         /* calculate and erase sectors */
885                         retval = flash_erase( target, run_address, run_size );
886                 }
887                 
888                 if (retval == ERROR_OK)
889                 {
890                         /* write flash sectors */
891                         retval = c->driver->write(c, buffer, run_address - c->base, run_size);
892                 }
893                 
894                 free(buffer);
895
896                 if (retval != ERROR_OK)
897                 {
898                         if (error_str == NULL)
899                                 return retval; /* abort operation */
900
901                         *error_str = malloc(FLASH_MAX_ERROR_STR);
902                         switch (retval)
903                         {
904                                 case ERROR_TARGET_NOT_HALTED:
905                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "can't flash image while target is running");
906                                         break;
907                                 case ERROR_INVALID_ARGUMENTS:
908                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash driver can't fulfill request");
909                                         break;
910                                 case ERROR_FLASH_OPERATION_FAILED:
911                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "flash program error");
912                                         break;
913                                 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
914                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "offset breaks required alignment");
915                                         break;
916                                 case ERROR_FLASH_DST_OUT_OF_BANK:
917                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "no flash mapped at requested address");
918                                         break;
919                                 case ERROR_FLASH_SECTOR_NOT_ERASED:
920                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "destination sector(s) not erased");
921                                         break;
922                                 default:
923                                         snprintf(*error_str, FLASH_MAX_ERROR_STR, "unknown error: %i", retval);
924                         }
925
926                         return retval; /* abort operation */
927                 }
928
929                 if (written != NULL)
930                         *written += run_size; /* add run size to total written counter */
931         }
932
933         return ERROR_OK;
934 }
935
936 int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
937 {
938         if (argc != 1)
939         {
940                 command_print(cmd_ctx, "usage: flash auto_erase <on|off>");
941                 return ERROR_OK;
942         }
943         
944         if (strcmp(args[0], "on") == 0)
945                 auto_erase = 1;
946         else if (strcmp(args[0], "off") == 0)
947                 auto_erase = 0;
948         
949         return ERROR_OK;
950 }
951
952