- add missing files from previous commit (tms470 flash driver)
[openocd.git] / src / flash / flash.c
index 734decb3f0a9b03df78ced3e07aa5a5c8e7d53b5..2e7cc4537e2f7e41421bbe6b8536d71d22ef5217 100644 (file)
@@ -49,7 +49,6 @@ int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, cha
 int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 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_verify_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_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
@@ -64,6 +63,7 @@ extern flash_driver_t str9x_flash;
 extern flash_driver_t stellaris_flash;
 extern flash_driver_t str9xpec_flash;
 extern flash_driver_t stm32x_flash;
+extern flash_driver_t tms470_flash;
 
 flash_driver_t *flash_drivers[] =
 {
@@ -75,6 +75,7 @@ flash_driver_t *flash_drivers[] =
        &stellaris_flash,
        &str9xpec_flash,
        &stm32x_flash,
+       &tms470_flash,
        NULL,
 };
 
@@ -91,7 +92,7 @@ int flash_register_commands(struct command_context_s *cmd_ctx)
        return ERROR_OK;
 }
 
-int flash_init(struct command_context_s *cmd_ctx)
+int flash_init_drivers(struct command_context_s *cmd_ctx)
 {
        if (flash_banks)
        {
@@ -117,8 +118,6 @@ int flash_init(struct command_context_s *cmd_ctx)
                                                 "write binary <bank> <file> <offset>");
                register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
                                                 "write_image <file> [offset] [type]");
-               register_command(cmd_ctx, flash_cmd, "verify_image", handle_flash_verify_image_command, COMMAND_EXEC,
-                                                "verify_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,
@@ -364,69 +363,74 @@ int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cm
        return ERROR_OK;
 }
 
-static void printError(struct command_context_s *cmd_ctx, flash_bank_t *p, int retval)
-{
-       if (retval==ERROR_OK)
-               return;
-       switch (retval)
-       {
-               case ERROR_TARGET_NOT_HALTED:
-                       command_print(cmd_ctx, "can't work with this flash while target is running");
-                       break;
-               case ERROR_INVALID_ARGUMENTS:
-                       command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
-                       break;
-               case ERROR_FLASH_BANK_INVALID:
-                       command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
-                       break;
-               case ERROR_FLASH_OPERATION_FAILED:
-                       command_print(cmd_ctx, "flash program error");
-                       break;
-               case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
-                       command_print(cmd_ctx, "offset breaks required alignment");
-                       break;
-               case ERROR_FLASH_DST_OUT_OF_BANK:
-                       command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
-                       break;
-               case ERROR_FLASH_SECTOR_NOT_ERASED:
-                       command_print(cmd_ctx, "destination sector(s) not erased");
-                       break;
-               default:
-                       command_print(cmd_ctx, "unknown error");
-}
-}
-
-
 int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        flash_bank_t *p;
-               
+       int retval;
+       int address;
+       int length;
+       duration_t duration;
+       char *duration_text;
+       
        target_t *target = get_current_target(cmd_ctx);
 
        if (argc != 2)
        {
-               command_print(cmd_ctx, "usage: flash erase_address <address> <range>");
+               command_print(cmd_ctx, "usage: flash erase_address <address> <length>");
                return ERROR_OK;
        }
        
-       int address=strtoul(args[0], NULL, 0);
-       int length=strtoul(args[1], NULL, 0);
-       if (length<=0)
+       address = strtoul(args[0], NULL, 0);
+       length = strtoul(args[1], NULL, 0);
+       if (length <= 0)
        {
                command_print(cmd_ctx, "Length must be >0");
                return ERROR_INVALID_ARGUMENTS;
        }
 
        p = get_flash_bank_by_addr(target, address);
-       if (p==NULL)
+       if (p == NULL)
        {
                command_print(cmd_ctx, "No flash at that address");
                return ERROR_INVALID_ARGUMENTS;
        }
-       int retval=flash_erase(target, address, length);
-       printError(cmd_ctx, p, retval);
-       return retval;
        
+       /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
+       flash_set_dirty();
+       
+       duration_start_measure(&duration);
+       
+       if ((retval = flash_erase_address_range(target, address, length)) != ERROR_OK)
+       {
+               switch (retval)
+               {
+                       case ERROR_TARGET_NOT_HALTED:
+                               command_print(cmd_ctx, "can't work with this flash while target is running");
+                               break;
+                       case ERROR_INVALID_ARGUMENTS:
+                               command_print(cmd_ctx, "usage: flash erase_address <address> <length>");
+                               break;
+                       case ERROR_FLASH_BANK_INVALID:
+                               command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
+                               break;
+                       case ERROR_FLASH_OPERATION_FAILED:
+                               command_print(cmd_ctx, "flash erase error");
+                               break;
+                       case ERROR_FLASH_SECTOR_INVALID:
+                               command_print(cmd_ctx, "sector number(s) invalid");
+                               break;
+                       default:
+                               command_print(cmd_ctx, "unknown error");
+               }
+       }
+       else
+       {
+               duration_stop_measure(&duration, &duration_text);       
+               command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
+               free(duration_text);
+       }
+       
+       return retval;
 }
 
 int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
@@ -589,7 +593,7 @@ int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, c
        return ERROR_OK;
 }
 
-int handle_flash_write_image_command_core(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, enum flash_image_op op)
+int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        target_t *target = get_current_target(cmd_ctx);
        
@@ -641,17 +645,9 @@ int handle_flash_write_image_command_core(struct command_context_s *cmd_ctx, cha
        
        failed = malloc(sizeof(int) * image.num_sections);
 
-       error_str=NULL;
-
-       retval=ERROR_OK;
-       if ((op==flash_image_op_write)&&auto_erase)
-       {
-               retval = flash_image_operation(target, &image, &written, &error_str, failed, flash_image_op_erase);
-       }
-       if (retval == ERROR_OK)
-       {
-               retval = flash_image_operation(target, &image, &written, &error_str, failed, op);
-       }
+       error_str = NULL;
+               
+       retval = flash_write(target, &image, &written, &error_str, failed, auto_erase);
        
        if (retval != ERROR_OK)
        {
@@ -675,11 +671,9 @@ int handle_flash_write_image_command_core(struct command_context_s *cmd_ctx, cha
        }
        
        duration_stop_measure(&duration, &duration_text);
-       command_print(cmd_ctx, "%s %u byte from file %s in %s (%f kb/s)",
-                       (op==flash_image_op_write)?"wrote":"verified",
+       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);
 
@@ -688,16 +682,6 @@ int handle_flash_write_image_command_core(struct command_context_s *cmd_ctx, cha
        return ERROR_OK;
 }
 
-
-int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       return handle_flash_write_image_command_core(cmd_ctx, cmd, args, argc, flash_image_op_write);
-}
-int handle_flash_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
-       return handle_flash_write_image_command_core(cmd_ctx, cmd, args, argc, flash_image_op_verify);
-}
-
 int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        u32 offset;
@@ -745,7 +729,33 @@ int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *c
        {
                command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
                        args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
-               printError(cmd_ctx, p, retval);
+               
+               switch (retval)
+               {
+                       case ERROR_TARGET_NOT_HALTED:
+                               command_print(cmd_ctx, "can't work with this flash while target is running");
+                               break;
+                       case ERROR_INVALID_ARGUMENTS:
+                               command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
+                               break;
+                       case ERROR_FLASH_BANK_INVALID:
+                               command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
+                               break;
+                       case ERROR_FLASH_OPERATION_FAILED:
+                               command_print(cmd_ctx, "flash program error");
+                               break;
+                       case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
+                               command_print(cmd_ctx, "offset breaks required alignment");
+                               break;
+                       case ERROR_FLASH_DST_OUT_OF_BANK:
+                               command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
+                               break;
+                       case ERROR_FLASH_SECTOR_NOT_ERASED:
+                               command_print(cmd_ctx, "destination sector(s) not erased");
+                               break;
+                       default:
+                               command_print(cmd_ctx, "unknown error");
+               }
        }
 
        free(buffer);
@@ -761,6 +771,21 @@ int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *c
        return ERROR_OK;
 }
 
+void flash_set_dirty(void)
+{
+       flash_bank_t *c;
+       int i;
+       
+       /* set all flash to require erasing */
+       for (c = flash_banks; c; c = c->next)
+       {
+               for (i = 0; i < c->num_sectors; i++)
+               {
+                       c->sectors[i].is_erased = 0; 
+               }
+       }
+}
+
 /* lookup flash bank by address */
 flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
 {
@@ -778,7 +803,7 @@ flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
 }
 
 /* erase given flash region, selects proper bank according to target and address */
-int flash_erase(target_t *target, u32 addr, u32 length)
+int flash_erase_address_range(target_t *target, u32 addr, u32 length)
 {
        flash_bank_t *c;
        int first = -1;
@@ -823,15 +848,16 @@ int flash_erase(target_t *target, u32 addr, u32 length)
        return c->driver->erase(c, first, last);
 }
 
-/* perform an operation on flash using an image: verify, erase or write. */
-int flash_image_operation(target_t *target, image_t *image, u32 *written, char **error_str, int *failed, enum flash_image_op op)
+/* write (optional verify) 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 erase)
 {
        int retval;
        int i;
 
        int section;
        u32 section_offset;
-
+       flash_bank_t *c;
+       
        section = 0;
        section_offset = 0;
 
@@ -841,17 +867,24 @@ int flash_image_operation(target_t *target, image_t *image, u32 *written, char *
        if (failed != NULL)
                for (i = 0; i < image->num_sections; i++)
                        failed[i] = 0;
-
+       
+       if (erase)
+       {
+               /* assume all sectors need erasing - stops any problems
+                * when flash_write is called multiple times */
+               
+               flash_set_dirty();
+       }
+       
        /* loop until we reach end of the image */
        while (section < image->num_sections)
        {
-               flash_bank_t *c;
                u32 buffer_size;
                u8 *buffer;
                int section_first;
                int section_last;
-               u32 run_address = image->sections[section].base_address+section_offset;
-               u32 run_size = image->sections[section].size-section_offset;
+               u32 run_address = image->sections[section].base_address + section_offset;
+               u32 run_size = image->sections[section].size - section_offset;
 
                if (image->sections[section].size ==  0)
                {
@@ -943,57 +976,16 @@ int flash_image_operation(target_t *target, image_t *image, u32 *written, char *
 
                retval = ERROR_OK;
                
-               switch (op)
+               if (erase)
                {
-                       case flash_image_op_erase:
-                               /* calculate and erase sectors */
-                               retval = flash_erase( target, run_address, run_size );
-                               break;
-               
-                       case flash_image_op_write:
-                               /* write flash sectors */
-                               retval = c->driver->write(c, buffer, run_address - c->base, run_size);
-                               break;
-                       
-                       case flash_image_op_verify:
-                       {
-                               // Verify
-                               u8 *data;
-                               data=(u8 *)malloc(run_size);
-                               if (data==NULL)
-                                       retval = ERROR_INVALID_ARGUMENTS; // exception handling would be nice...
-                               
-                               // Can we use long word accesses?
-                               int size=1;
-                               int count=run_size;
-                               if ((count%4)==0)
-                               {
-                                       size*=4;
-                                       count/=4;
-                               }
-                               retval = target->type->read_memory(target, run_address, size, count, data);
+                       /* calculate and erase sectors */
+                       retval = flash_erase_address_range( target, run_address, run_size );
+               }
                
-                               if (retval == ERROR_OK)
-                               {
-                                       int i;
-                                       for (i=0; i<run_size; i++)
-                                       {
-                                               if (data[i]!=buffer[i])
-                                               {
-                                                       ERROR("Verify operation failed address 0x%08x. Was %02x instead of %02x\n", i+run_address, data[i], buffer[i]);
-                                                       retval=ERROR_FLASH_OPERATION_FAILED;
-                                                       break;
-                                               }
-                                       }
-                               }
-                               free(data);
-        
-                               break;
-                       }
-      
-                       default:
-                               // can't happen
-                               exit(-1);
+               if (retval == ERROR_OK)
+               {
+                       /* write flash sectors */
+                       retval = c->driver->write(c, buffer, run_address - c->base, run_size);
                }
                
                free(buffer);
@@ -1053,6 +1045,3 @@ int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd
        
        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)