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