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