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