- change error message for image_open error
[openocd.git] / src / flash / flash.c
index 9ca5f157be9aa1449aa8f1c66d2e2cd2e4dc58c4..2271f603cc74bf92eaf81d6ffca8886822898e79 100644 (file)
@@ -49,6 +49,7 @@ int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, cha
 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
+int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 
 /* flash drivers
  */
@@ -76,6 +77,7 @@ flash_driver_t *flash_drivers[] =
 
 flash_bank_t *flash_banks;
 static         command_t *flash_cmd;
+static int auto_erase = 0;
 
 int flash_register_commands(struct command_context_s *cmd_ctx)
 {
@@ -110,6 +112,8 @@ int flash_init(struct command_context_s *cmd_ctx)
                                                 "write image <file> [offset] [type]");
                register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
                                                 "set protection of sectors at <bank> <first> <last> <on|off>");
+               register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_EXEC,
+                                                "auto erase flash sectors <on|off>");
        }
        
        return ERROR_OK;
@@ -172,7 +176,9 @@ int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char
                        c->base = strtoul(args[1], NULL, 0);
                        c->size = strtoul(args[2], NULL, 0);
                        c->chip_width = strtoul(args[3], NULL, 0);
-                       c->bus_width = strtoul(args[4], NULL, 0);
+                       c->bus_width = strtoul(args[4], NULL, 0);\r
+                       c->num_sectors = 0;\r
+                       c->sectors = NULL;
                        c->next = NULL;
                        
                        if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
@@ -267,8 +273,8 @@ int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char
                                else
                                        protect_state = "protection state unknown";
 
-                               command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
-                                                       j, p->sectors[j].offset, p->sectors[j].size,
+                               command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s, %s",
+                                                       j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
                                                        erase_state, protect_state);
                        }
                        
@@ -557,19 +563,29 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
        }
        
        image.start_address_set = 0;
-
-       if (image_open(&image, args[0], (argc == 4) ? args[2] : NULL) != ERROR_OK)
+\r
+       retval = image_open(&image, args[0], (argc == 4) ? args[2] : NULL);
+       if (retval != ERROR_OK)
        {
-               command_print(cmd_ctx, "flash write error: %s", image.error_str);
-               return ERROR_OK;
+               command_print(cmd_ctx, "image_open error: %s", image.error_str);
+               return retval;
        }
        
        failed = malloc(sizeof(int) * image.num_sections);
 
-       if ((retval = flash_write(target, &image, &written, &error_str, failed)) != ERROR_OK)
+       error_str=NULL;
+       retval = flash_write(target, &image, &written, &error_str, failed, auto_erase);
+       
+       if (retval != ERROR_OK)
        {
-               command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
-               free(error_str);
+               if (error_str)
+               {
+                       command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
+                       free(error_str);
+               }
+               image_close(&image);
+               free(failed);
+               return retval;
        }
        
        for (i = 0; i < image.num_sections; i++)
@@ -585,7 +601,9 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
        command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
                written, args[0], duration_text,
                (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
+
        free(duration_text);
+       free(failed);
 
        image_close(&image);
        
@@ -700,17 +718,16 @@ flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
 int flash_erase(target_t *target, u32 addr, u32 length)
 {
        flash_bank_t *c;
-       unsigned long sector_size;
-       int first;
-       int last;
-
+       int first = -1;
+       int last = -1;
+       int i;
+       
        if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
                return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
-       /* sanity checks */
-       if (c->size == 0 || c->num_sectors == 0 || c->size % c->num_sectors)
-               return ERROR_FLASH_BANK_INVALID;
 
+       if (c->size == 0 || c->num_sectors == 0)
+               return ERROR_FLASH_BANK_INVALID;
+       
        if (length == 0)
        {
                /* special case, erase whole bank when length is zero */
@@ -722,22 +739,29 @@ int flash_erase(target_t *target, u32 addr, u32 length)
 
        /* check whether it fits */
        if (addr + length > c->base + c->size)
-         return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
-
-       /* calculate sector size */
-       sector_size = c->size / c->num_sectors;
-
-       /* check alignment */
-       if ((addr - c->base) % sector_size || length % sector_size)
                return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
-
-       first = (addr - c->base) / sector_size;
-       last = first + length / sector_size - 1;
+       
+       addr -= c->base;
+       
+       for (i = 0; i < c->num_sectors; i++)
+       {               
+               /* check whether sector overlaps with the given range and is not yet erased */
+               if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
+                       /* if first is not set yet then this is the first sector */
+                       if (first == -1)
+                               first = i;
+                       last = i; /* and it is the last one so far in any case */
+               }
+       }
+       
+       if( first == -1 || last == -1 )
+               return ERROR_OK;
+       
        return c->driver->erase(c, first, last);
 }
 
 /* write an image to flash memory of the given target */
-int flash_write(target_t *target, image_t *image, u32 *written, char **error_str, int *failed)
+int flash_write(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, int erase)
 {
        int retval;
        int i;
@@ -854,7 +878,20 @@ int flash_write(target_t *target, image_t *image, u32 *written, char **error_str
                        }
                }
 
-               retval = c->driver->write(c, buffer, run_address - c->base, run_size);
+               retval = ERROR_OK;
+               
+               if (erase)
+               {
+                       /* calculate and erase sectors */
+                       retval = flash_erase( target, run_address, run_size );
+               }
+               
+               if (retval == ERROR_OK)
+               {
+                       /* write flash sectors */
+                       retval = c->driver->write(c, buffer, run_address - c->base, run_size);
+               }
+               
                free(buffer);
 
                if (retval != ERROR_OK)
@@ -896,3 +933,21 @@ int flash_write(target_t *target, image_t *image, u32 *written, char **error_str
 
        return ERROR_OK;
 }
+
+int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       if (argc != 1)
+       {
+               command_print(cmd_ctx, "usage: flash auto_erase <on|off>");
+               return ERROR_OK;
+       }
+       
+       if (strcmp(args[0], "on") == 0)
+               auto_erase = 1;
+       else if (strcmp(args[0], "off") == 0)
+               auto_erase = 0;
+       
+       return ERROR_OK;
+}
+
+

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)