flash_write will attempt to pad image sections
[openocd.git] / src / flash / flash.c
index 12f844181a5a0fe03c02fab1a98f4b0ca0cec2d8..1e4edd622cffe60ceccb6f09a5b8989c4a746786 100644 (file)
@@ -55,7 +55,6 @@ int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd
 int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_flash_fill_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_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
 
 /* flash drivers
@@ -70,6 +69,8 @@ extern flash_driver_t str9xpec_flash;
 extern flash_driver_t stm32x_flash;
 extern flash_driver_t tms470_flash;
 extern flash_driver_t ecosflash_flash;
+extern flash_driver_t lpc288x_flash;
+extern flash_driver_t ocl_flash;
 
 flash_driver_t *flash_drivers[] =
 {
@@ -83,13 +84,13 @@ flash_driver_t *flash_drivers[] =
        &stm32x_flash,
        &tms470_flash,
        &ecosflash_flash,
+       &lpc288x_flash,
+       &ocl_flash,
        NULL,
 };
 
 flash_bank_t *flash_banks;
 static         command_t *flash_cmd;
-/* enabled by default*/
-static int auto_erase = 1;
 
 /* wafer thin wrapper for invoking the flash driver */
 static int flash_driver_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
@@ -131,14 +132,11 @@ int flash_driver_protect(struct flash_bank_s *bank, int set, int first, int last
        return retval;
 }
 
-
 int flash_register_commands(struct command_context_s *cmd_ctx)
 {
        flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
 
        register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, "flash_bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
-       register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_ANY,
-                                                "auto erase flash sectors <on|off>");
        return ERROR_OK;
 }
 
@@ -171,7 +169,7 @@ int flash_init_drivers(struct command_context_s *cmd_ctx)
                register_command(cmd_ctx, flash_cmd, "write_bank", handle_flash_write_bank_command, COMMAND_EXEC,
                                                 "write binary data to <bank> <file> <offset>");
                register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
-                                                "write_image <file> [offset] [type]");
+                                                "write_image [erase] <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>");
        }
@@ -479,7 +477,7 @@ int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *
        p = get_flash_bank_by_addr(target, address);
        if (p == NULL)
        {
-               return ERROR_COMMAND_SYNTAX_ERROR;
+               return ERROR_FAIL;
        }
 
        /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
@@ -620,7 +618,24 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
+       
+       /* flash auto-erase is disabled by default*/
+       int auto_erase = 0;
+       
+       if (strcmp(args[0], "erase")==0)
+       {
+               auto_erase = 1;
+               args++;
+               argc--;
+               command_print(cmd_ctx, "auto erase enabled");
+       }
+       
 
+       if (argc < 1)
+       {
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       }
+       
        if (!target)
        {
                LOG_ERROR("no target selected");
@@ -813,12 +828,15 @@ int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd
        buffer = malloc(fileio.size);
        if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
        {
+               free(buffer);
+               fileio_close(&fileio);
                return ERROR_OK;
        }
 
        retval = flash_driver_write(p, buffer, offset, buf_cnt);
 
        free(buffer);
+       buffer = NULL;
 
        duration_stop_measure(&duration, &duration_text);
        if (retval!=ERROR_OK)
@@ -930,7 +948,8 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
        int section;
        u32 section_offset;
        flash_bank_t *c;
-
+       int *padding;
+       
        section = 0;
        section_offset = 0;
 
@@ -944,7 +963,10 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
 
                flash_set_dirty();
        }
-
+       
+       /* allocate padding array */
+       padding = malloc(image->num_sections * sizeof(padding));
+       
        /* loop until we reach end of the image */
        while (section < image->num_sections)
        {
@@ -954,7 +976,8 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                int section_last;
                u32 run_address = image->sections[section].base_address + section_offset;
                u32 run_size = image->sections[section].size - section_offset;
-
+               int pad_bytes = 0;
+               
                if (image->sections[section].size ==  0)
                {
                        LOG_WARNING("empty section %d", section);
@@ -974,6 +997,7 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                /* collect consecutive sections which fall into the same bank */
                section_first = section;
                section_last = section;
+               padding[section] = 0;
                while ((run_address + run_size < c->base + c->size)
                                && (section_last + 1 < image->num_sections))
                {
@@ -982,9 +1006,17 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                                LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
                                break;
                        }
-                       if (image->sections[section_last + 1].base_address != (run_address + run_size))
+                       /* if we have multiple sections within our image, flash programming could fail due to alignment issues
+                        * attempt to rebuild a consecutive buffer for the flash loader */
+                       pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
+                       if ((run_address + run_size + pad_bytes) > (c->base + c->size))
                                break;
+                       padding[section_last] = pad_bytes;
                        run_size += image->sections[++section_last].size;
+                       run_size += pad_bytes;
+                       padding[section_last] = 0;
+                       
+                       LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes );
                }
 
                /* fit the run into bank constraints */
@@ -1009,10 +1041,14 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                                        size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
                        {
                                free(buffer);
-
+                               free(padding);
                                return retval;
                        }
-
+                       
+                       /* see if we need to pad the section */
+                       while (padding[section]--)
+                               buffer[size_read++] = 0xff;
+                       
                        buffer_size += size_read;
                        section_offset += size_read;
 
@@ -1041,34 +1077,19 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
 
                if (retval != ERROR_OK)
                {
+                       free(padding);
                        return retval; /* abort operation */
                }
 
                if (written != NULL)
                        *written += run_size; /* add run size to total written counter */
        }
-
+       
+       free(padding);
+       
        return retval;
 }
 
-int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       if (argc != 1)
-       {
-               return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       if (strcmp(args[0], "on") == 0)
-               auto_erase = 1;
-       else if (strcmp(args[0], "off") == 0)
-               auto_erase = 0;
-       else
-               return ERROR_COMMAND_SYNTAX_ERROR;
-
-       return ERROR_OK;
-}
-
-
 int default_flash_blank_check(struct flash_bank_s *bank)
 {
        target_t *target = bank->target;

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)