]> git.sur5r.net Git - openocd/blob - src/flash/flash.c
- ST STM32x cortex support added
[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
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35
36 #include <fileio.h>
37 #include <image.h>
38 #include "log.h"
39
40 /* command handlers */
41 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
42 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
43 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50
51 /* flash drivers
52  */
53 extern flash_driver_t lpc2000_flash;
54 extern flash_driver_t cfi_flash;
55 extern flash_driver_t at91sam7_flash;
56 extern flash_driver_t str7x_flash;
57 extern flash_driver_t str9x_flash;
58 extern flash_driver_t stellaris_flash;
59 extern flash_driver_t str9xpec_flash;
60 extern flash_driver_t stm32x_flash;
61
62 flash_driver_t *flash_drivers[] =
63 {
64         &lpc2000_flash,
65         &cfi_flash,
66         &at91sam7_flash,
67         &str7x_flash,
68         &str9x_flash,
69         &stellaris_flash,
70         &str9xpec_flash,
71         &stm32x_flash,
72         NULL,
73 };
74
75 flash_bank_t *flash_banks;
76 static  command_t *flash_cmd;
77
78 int flash_register_commands(struct command_context_s *cmd_ctx)
79 {
80         flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
81         
82         register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
83         
84         return ERROR_OK;
85 }
86
87 int flash_init(struct command_context_s *cmd_ctx)
88 {
89         if (flash_banks)
90         {
91                 register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
92                                                  "list configured flash banks ");
93                 register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
94                                                  "print info about flash bank <num>");
95                 register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
96                                                  "identify flash bank <num>");
97                 register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
98                                                  "check erase state of sectors in flash bank <num>");
99                 register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
100                                                  "check protection state of sectors in flash bank <num>");
101                 register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
102                                                  "erase sectors at <bank> <first> <last>");
103                 register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
104                                                  "write binary <bank> <file> <offset>");
105                 register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
106                                                  "set protection of sectors at <bank> <first> <last> <on|off>");
107         }
108         
109         return ERROR_OK;
110 }
111
112 flash_bank_t *get_flash_bank_by_num(int num)
113 {
114         flash_bank_t *p;
115         int i = 0;
116
117         for (p = flash_banks; p; p = p->next)
118         {
119                 if (i++ == num)
120                 {
121                         return p;
122                 }
123         }
124         
125         return NULL;
126 }
127
128 /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
129  */
130 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
131 {
132         int i;
133         int found = 0;
134                 
135         if (argc < 5)
136         {
137                 WARNING("incomplete flash_bank configuration");
138                 return ERROR_OK;
139         }
140         
141         for (i = 0; flash_drivers[i]; i++)
142         {
143                 if (strcmp(args[0], flash_drivers[i]->name) == 0)
144                 {
145                         flash_bank_t *p, *c;
146                         
147                         /* register flash specific commands */
148                         if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
149                         {
150                                 ERROR("couldn't register '%s' commands", args[0]);
151                                 exit(-1);
152                         }
153                         
154                         c = malloc(sizeof(flash_bank_t));
155                         c->driver = flash_drivers[i];
156                         c->driver_priv = NULL;
157                         c->base = strtoul(args[1], NULL, 0);
158                         c->size = strtoul(args[2], NULL, 0);
159                         c->chip_width = strtoul(args[3], NULL, 0);
160                         c->bus_width = strtoul(args[4], NULL, 0);
161                         c->next = NULL;
162                         
163                         if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
164                         {
165                                 ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
166                                 free(c);
167                                 return ERROR_OK;
168                         }
169                         
170                         /* put flash bank in linked list */
171                         if (flash_banks)
172                         {
173                                 /* find last flash bank */
174                                 for (p = flash_banks; p && p->next; p = p->next);
175                                 if (p)
176                                         p->next = c;
177                         }
178                         else
179                         {
180                                 flash_banks = c;
181                         }
182                         
183                         found = 1;
184                 }
185         }
186                 
187         /* no matching flash driver found */
188         if (!found)
189         {
190                 ERROR("flash driver '%s' not found", args[0]);
191                 exit(-1);
192         }
193         
194         return ERROR_OK;
195 }
196
197 int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
198 {
199         flash_bank_t *p;
200         int i = 0;
201         
202         if (!flash_banks)
203         {
204                 command_print(cmd_ctx, "no flash banks configured");
205                 return ERROR_OK;
206         }
207         
208         for (p = flash_banks; p; p = p->next)
209         {
210                 command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
211                                           i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
212         }
213         
214         return ERROR_OK;
215 }
216
217 int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
218 {
219         flash_bank_t *p;
220         int i = 0;
221         int j = 0;
222                 
223         if (argc != 1)
224         {
225                 command_print(cmd_ctx, "usage: flash info <num>");
226                 return ERROR_OK;
227         }
228         
229         for (p = flash_banks; p; p = p->next)
230         {
231                 if (i++ == strtoul(args[0], NULL, 0))
232                 {
233                         char buf[1024];
234                         
235                         command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
236                                                 i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
237                         for (j = 0; j < p->num_sectors; j++)
238                         {
239                                 char *erase_state, *protect_state;
240                                 
241                                 if (p->sectors[j].is_erased == 0)
242                                         erase_state = "not erased";
243                                 else if (p->sectors[j].is_erased == 1)
244                                         erase_state = "erased";
245                                 else
246                                         erase_state = "erase state unknown";
247                                 
248                                 if (p->sectors[j].is_protected == 0)
249                                         protect_state = "not protected";
250                                 else if (p->sectors[j].is_protected == 1)
251                                         protect_state = "protected";
252                                 else
253                                         protect_state = "protection state unknown";
254
255                                 command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
256                                                         j, p->sectors[j].offset, p->sectors[j].size,
257                                                         erase_state, protect_state);
258                         }
259                         
260                         p->driver->info(p, buf, 1024);
261                         command_print(cmd_ctx, "%s", buf);
262                 }
263         }
264         
265         return ERROR_OK;
266 }
267
268 int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
269 {
270         flash_bank_t *p;
271         int retval;
272                 
273         if (argc != 1)
274         {
275                 command_print(cmd_ctx, "usage: flash probe <num>");
276                 return ERROR_OK;
277         }
278         
279         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
280         if (p)
281         {
282                 if ((retval = p->driver->probe(p)) == ERROR_OK)
283                 {
284                         command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
285                 }
286                 else if (retval == ERROR_FLASH_BANK_INVALID)
287                 {
288                         command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
289                                                   args[0], p->base);
290                 }
291                 else
292                 {
293                         command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
294                                                   args[0], p->base);
295                 }
296         }
297         else
298         {
299                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
300         }
301         
302         return ERROR_OK;
303 }
304
305 int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
306 {
307         flash_bank_t *p;
308         int retval;
309                 
310         if (argc != 1)
311         {
312                 command_print(cmd_ctx, "usage: flash erase_check <num>");
313                 return ERROR_OK;
314         }
315         
316         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
317         if (p)
318         {
319                 if ((retval = p->driver->erase_check(p)) == ERROR_OK)
320                 {
321                         command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
322                 }
323                 else
324                 {
325                         command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
326                                 args[0], p->base);
327                 }
328         }
329         else
330         {
331                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
332         }
333         
334         return ERROR_OK;
335 }
336
337 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
338 {
339         flash_bank_t *p;
340         int retval;
341                 
342         if (argc != 1)
343         {
344                 command_print(cmd_ctx, "usage: flash protect_check <num>");
345                 return ERROR_OK;
346         }
347         
348         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
349         if (p)
350         {
351                 if ((retval = p->driver->protect_check(p)) == ERROR_OK)
352                 {
353                         command_print(cmd_ctx, "successfully checked protect state");
354                 }
355                 else if (retval == ERROR_FLASH_OPERATION_FAILED)
356                 {
357                         command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
358                 }
359                 else
360                 {
361                         command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
362                 }
363         }
364         else
365         {
366                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
367         }
368         
369         return ERROR_OK;
370 }
371
372 int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
373 {
374         if (argc > 2)
375         {
376                 int first = strtoul(args[1], NULL, 0);
377                 int last = strtoul(args[2], NULL, 0);
378                 int retval;
379                 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
380                 duration_t duration;
381                 char *duration_text;
382         
383                 duration_start_measure(&duration);
384         
385                 if (!p)
386                 {
387                         command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
388                         return ERROR_OK;
389                 }
390                 
391                 if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
392                 {
393                         switch (retval)
394                         {
395                                 case ERROR_TARGET_NOT_HALTED:
396                                         command_print(cmd_ctx, "can't work with this flash while target is running");
397                                         break;
398                                 case ERROR_INVALID_ARGUMENTS:
399                                         command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
400                                         break;
401                                 case ERROR_FLASH_BANK_INVALID:
402                                         command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
403                                         break;
404                                 case ERROR_FLASH_OPERATION_FAILED:
405                                         command_print(cmd_ctx, "flash erase error");
406                                         break;
407                                 case ERROR_FLASH_SECTOR_INVALID:
408                                         command_print(cmd_ctx, "sector number(s) invalid");
409                                         break;
410                                 case ERROR_OK:
411                                         command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
412                                         break;
413                                 default:
414                                         command_print(cmd_ctx, "unknown error");
415                         }
416                 }
417                 else
418                 {
419                         duration_stop_measure(&duration, &duration_text);       
420                         
421                         command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
422                         free(duration_text);
423                 }
424         }
425         else
426         {
427                 command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
428         }
429
430         return ERROR_OK;
431 }
432
433 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
434 {
435         if (argc > 3)
436         {
437                 int first = strtoul(args[1], NULL, 0);
438                 int last = strtoul(args[2], NULL, 0);
439                 int set;
440                 int retval;
441                 flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
442                 if (!p)
443                 {
444                         command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
445                         return ERROR_OK;
446                 }
447                 
448                 if (strcmp(args[3], "on") == 0)
449                         set = 1;
450                 else if (strcmp(args[3], "off") == 0)
451                         set = 0;
452                 else
453                 {
454                         command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
455                         return ERROR_OK;
456                 }
457                 
458                 if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
459                 {
460                         switch (retval)
461                         {
462                                 case ERROR_TARGET_NOT_HALTED:
463                                         command_print(cmd_ctx, "can't work with this flash while target is running");
464                                         break;
465                                 case ERROR_INVALID_ARGUMENTS:
466                                         command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
467                                         break;
468                                 case ERROR_FLASH_BANK_INVALID:
469                                         command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
470                                         break;
471                                 case ERROR_FLASH_OPERATION_FAILED:
472                                         command_print(cmd_ctx, "flash program error");
473                                         break;
474                                 case ERROR_FLASH_SECTOR_INVALID:
475                                         command_print(cmd_ctx, "sector number(s) invalid");
476                                         break;
477                                 case ERROR_OK:
478                                         command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
479                                         break;
480                                 default:
481                                         command_print(cmd_ctx, "unknown error");
482                         }
483                 }
484                 else
485                 {
486                         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));
487                 }
488         }
489         else
490         {
491                 command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
492         }
493
494         return ERROR_OK;
495 }
496
497 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
498 {
499         u32 offset;
500         u8 *buffer;
501         u32 buf_cnt;
502         u32 image_size;
503         int i;
504
505         image_t image;
506         
507         duration_t duration;
508         char *duration_text;
509         
510         int retval;
511         flash_bank_t *p;
512
513         if (argc < 3)
514         {
515                 command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
516                 return ERROR_OK;
517         }
518         
519         duration_start_measure(&duration);
520         
521         image.base_address_set = 1;
522         image.base_address = strtoul(args[1], NULL, 0);
523         
524         image.start_address_set = 0;
525         
526         offset = strtoul(args[2], NULL, 0);
527         p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
528         if (!p)
529         {
530                 command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
531                 return ERROR_OK;
532         }
533         
534         if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
535         {
536                 command_print(cmd_ctx, "flash write error: %s", image.error_str);
537                 return ERROR_OK;
538         }
539         
540         image_size = 0x0;
541         for (i = 0; i < image.num_sections; i++)
542         {
543                 buffer = malloc(image.sections[i].size);
544                 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
545                 {
546                         ERROR("image_read_section failed with error code: %i", retval);
547                         command_print(cmd_ctx, "image reading failed, flash write aborted");
548                         free(buffer);
549                         image_close(&image);
550                         return ERROR_OK;
551                 }
552                 
553                 if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
554                 {
555                         command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
556                                 args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
557                         switch (retval)
558                         {
559                                 case ERROR_TARGET_NOT_HALTED:
560                                         command_print(cmd_ctx, "can't work with this flash while target is running");
561                                         break;
562                                 case ERROR_INVALID_ARGUMENTS:
563                                         command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
564                                         break;
565                                 case ERROR_FLASH_BANK_INVALID:
566                                         command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
567                                         break;
568                                 case ERROR_FLASH_OPERATION_FAILED:
569                                         command_print(cmd_ctx, "flash program error");
570                                         break;
571                                 case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
572                                         command_print(cmd_ctx, "offset breaks required alignment");
573                                         break;
574                                 case ERROR_FLASH_DST_OUT_OF_BANK:
575                                         command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
576                                         break;
577                                 case ERROR_FLASH_SECTOR_NOT_ERASED:
578                                         command_print(cmd_ctx, "destination sector(s) not erased");
579                                         break;
580                                 default:
581                                         command_print(cmd_ctx, "unknown error");
582                         }
583                 }
584                 image_size += buf_cnt;
585
586                 free(buffer);
587         }
588
589         
590         duration_stop_measure(&duration, &duration_text);
591         command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
592                 image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
593                 (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
594         free(duration_text);
595
596         image_close(&image);
597         
598         return ERROR_OK;
599 }