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