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