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