fix error handling in flash fill
[fw/openocd] / src / flash / flash.c
index 95a983b7433bee680ccac47736e6fd4e8792d72e..08f3aaff563f795426a5dcfb75e5841b48714e4f 100644 (file)
@@ -2,9 +2,12 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
- *   Copyright (C) 2007,2008 Øyvind Harboe                                      *
+ *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
  *   oyvind.harboe@zylin.com                                               *
  *                                                                         *
+ *   Copyright (C) 2008 by Spencer Oliver                                  *
+ *   spen@spen-soft.co.uk                                                  *
+ *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
@@ -64,8 +67,10 @@ flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
 extern flash_driver_t lpc2000_flash;
 extern flash_driver_t cfi_flash;
 extern flash_driver_t at91sam7_flash;
+extern flash_driver_t at91sam7_old_flash;
 extern flash_driver_t str7x_flash;
 extern flash_driver_t str9x_flash;
+extern flash_driver_t aduc702x_flash;
 extern flash_driver_t stellaris_flash;
 extern flash_driver_t str9xpec_flash;
 extern flash_driver_t stm32x_flash;
@@ -74,13 +79,14 @@ extern flash_driver_t ecosflash_flash;
 extern flash_driver_t lpc288x_flash;
 extern flash_driver_t ocl_flash;
 
-flash_driver_t *flash_drivers[] =
-{
+flash_driver_t *flash_drivers[] = {
        &lpc2000_flash,
        &cfi_flash,
        &at91sam7_flash,
+       &at91sam7_old_flash,
        &str7x_flash,
        &str9x_flash,
+       &aduc702x_flash,
        &stellaris_flash,
        &str9xpec_flash,
        &stm32x_flash,
@@ -145,7 +151,7 @@ int flash_register_commands(struct command_context_s *cmd_ctx)
 static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
        flash_bank_t *p;
-       
+
        if (argc != 1) {
                Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
                return JIM_ERR;
@@ -160,7 +166,7 @@ static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
        for (p = flash_banks; p; p = p->next)
        {
                Jim_Obj *elem=Jim_NewListObj(interp, NULL, 0);
-               
+
                Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
                Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
                Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
@@ -171,7 +177,7 @@ static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
                Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
                Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
                Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
-               
+
                Jim_ListAppendElement(interp, list, elem);
        }
 
@@ -185,7 +191,7 @@ int flash_init_drivers(struct command_context_s *cmd_ctx)
        if (flash_banks)
        {
                register_jim(cmd_ctx, "ocd_flash_banks", jim_flash_banks, "return information about the flash banks");
-               
+
                register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
                                                 "print info about flash bank <num>");
                register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
@@ -200,7 +206,7 @@ int flash_init_drivers(struct command_context_s *cmd_ctx)
                                                 "erase address range <address> <length>");
 
                register_command(cmd_ctx, flash_cmd, "fillw", handle_flash_fill_command, COMMAND_EXEC,
-                                                "fill with pattern <address> <word_pattern> <count>");
+                                                "fill with pattern (no autoerase) <address> <word_pattern> <count>");
                register_command(cmd_ctx, flash_cmd, "fillh", handle_flash_fill_command, COMMAND_EXEC,
                                                 "fill with pattern <address> <halfword_pattern> <count>");
                register_command(cmd_ctx, flash_cmd, "fillb", handle_flash_fill_command, COMMAND_EXEC,
@@ -233,7 +239,7 @@ flash_bank_t *get_flash_bank_by_num_noprobe(int num)
        return NULL;
 }
 
-int flash_get_bank_count()
+int flash_get_bank_count(void)
 {
        flash_bank_t *p;
        int i = 0;
@@ -264,6 +270,7 @@ flash_bank_t *get_flash_bank_by_num(int num)
 
 int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
+       int retval;
        int i;
        int found = 0;
        target_t *target;
@@ -276,7 +283,7 @@ int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char
        if ((target = get_target_by_num(strtoul(args[5], NULL, 0))) == NULL)
        {
                LOG_ERROR("target %lu not defined", strtoul(args[5], NULL, 0));
-               return ERROR_OK;
+               return ERROR_FAIL;
        }
 
        for (i = 0; flash_drivers[i]; i++)
@@ -289,7 +296,7 @@ int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char
                        if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
                        {
                                LOG_ERROR("couldn't register '%s' commands", args[0]);
-                               exit(-1);
+                               return ERROR_FAIL;
                        }
 
                        c = malloc(sizeof(flash_bank_t));
@@ -304,11 +311,11 @@ int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char
                        c->sectors = NULL;
                        c->next = NULL;
 
-                       if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
+                       if ((retval=flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
                        {
                                LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
                                free(c);
-                               return ERROR_OK;
+                               return retval;
                        }
 
                        /* put flash bank in linked list */
@@ -332,7 +339,7 @@ int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char
        if (!found)
        {
                LOG_ERROR("flash driver '%s' not found", args[0]);
-               exit(-1);
+               return ERROR_FAIL;
        }
 
        return ERROR_OK;
@@ -373,7 +380,7 @@ 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%x %ikB) %s",
+                               command_print(cmd_ctx, "\t#%3i: 0x%8.8x (0x%x %ikB) %s",
                                                        j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
                                                        protect_state);
                        }
@@ -448,7 +455,7 @@ int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cm
                        command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
                                args[0], p->base);
                }
-               
+
                for (j = 0; j < p->num_sectors; j++)
                {
                        char *erase_state;
@@ -460,11 +467,11 @@ int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cm
                        else
                                erase_state = "erase state unknown";
 
-                       command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s",
+                       command_print(cmd_ctx, "\t#%3i: 0x%8.8x (0x%x %ikB) %s",
                                                j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
                                                erase_state);
                }
-               
+
        }
 
        return ERROR_OK;
@@ -507,7 +514,10 @@ int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *
 
        if ((retval = flash_erase_address_range(target, address, length)) == ERROR_OK)
        {
-               duration_stop_measure(&duration, &duration_text);
+               if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+               {
+                       return retval;
+               }
                command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
                free(duration_text);
        }
@@ -569,7 +579,10 @@ int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, cha
 
                if ((retval = flash_driver_erase(p, first, last)) == ERROR_OK)
                {
-                       duration_stop_measure(&duration, &duration_text);
+                       if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+                       {
+                               return retval;
+                       }
 
                        command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
                        free(duration_text);
@@ -632,16 +645,16 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
        duration_t duration;
        char *duration_text;
 
-       int retval;
+       int retval, retvaltemp;
 
        if (argc < 1)
        {
                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;
@@ -649,13 +662,13 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
                argc--;
                command_print(cmd_ctx, "auto erase enabled");
        }
-       
+
 
        if (argc < 1)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
-       
+
        if (!target)
        {
                LOG_ERROR("no target selected");
@@ -690,7 +703,11 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
                return retval;
        }
 
-       duration_stop_measure(&duration, &duration_text);
+       if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+       {
+               image_close(&image);
+               return retvaltemp;
+       }
        if (retval == ERROR_OK)
        {
                command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
@@ -706,7 +723,7 @@ int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cm
 
 int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       int err = ERROR_OK;
+       int err = ERROR_OK, retval;
        u32 address;
        u32 pattern;
        u32 count;
@@ -718,16 +735,16 @@ int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char
        target_t *target = get_current_target(cmd_ctx);
        u32 i;
        int wordsize;
-       
+
        if (argc != 3)
        {
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
-       
+
        address = strtoul(args[0], NULL, 0);
        pattern = strtoul(args[1], NULL, 0);
        count   = strtoul(args[2], NULL, 0);
-       
+
        if(count == 0)
                return ERROR_OK;
 
@@ -746,7 +763,7 @@ int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char
        default:
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
-       
+
        chunk_count = MIN(count, (1024 / wordsize));
        switch(wordsize)
        {
@@ -769,34 +786,29 @@ int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char
                LOG_ERROR("BUG: can't happen");
                exit(-1);
        }
-       
+
        duration_start_measure(&duration);
 
-       flash_set_dirty();
-       err = flash_erase_address_range( target, address, count*wordsize );
-       if (err == ERROR_OK)
+       for (wrote=0; wrote<(count*wordsize); wrote+=sizeof(chunk))
        {
-               for (wrote=0; wrote<(count*wordsize); wrote+=sizeof(chunk))
-               { 
-                       int cur_size = MIN( (count*wordsize - wrote) , 1024 );
-                       if (err == ERROR_OK)
-                       {
-                               flash_bank_t *bank;
-                               bank = get_flash_bank_by_addr(target, address);
-                               if(bank == NULL)
-                               {
-                                       err = ERROR_FAIL;
-                                       break;
-                               }
-                               err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
-                               wrote += cur_size;
-                       }
-                       if (err!=ERROR_OK)
-                               break;
+               int cur_size = MIN( (count*wordsize - wrote) , 1024 );
+               flash_bank_t *bank;
+               bank = get_flash_bank_by_addr(target, address);
+               if(bank == NULL)
+               {
+                       return ERROR_FAIL;
                }
+               err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
+               if (err!=ERROR_OK)
+                       return err;
+               wrote += cur_size;
+       }
+
+       if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+       {
+               return retval;
        }
-       
-       duration_stop_measure(&duration, &duration_text);
+
 
        if(err == ERROR_OK)
        {
@@ -822,7 +834,7 @@ int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd
        duration_t duration;
        char *duration_text;
 
-       int retval;
+       int retval, retvaltemp;
        flash_bank_t *p;
 
        if (argc != 3)
@@ -858,7 +870,11 @@ int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd
        free(buffer);
        buffer = NULL;
 
-       duration_stop_measure(&duration, &duration_text);
+       if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
+       {
+               fileio_close(&fileio);
+               return retvaltemp;
+       }
        if (retval!=ERROR_OK)
        {
        command_print(cmd_ctx, "wrote  %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
@@ -904,7 +920,7 @@ flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
                        return NULL;
                }
                /* check whether address belongs to this flash bank */
-               if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
+               if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
                        return c;
        }
        LOG_ERROR("No flash at address 0x%08x\n", addr);
@@ -969,7 +985,7 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
        u32 section_offset;
        flash_bank_t *c;
        int *padding;
-       
+
        section = 0;
        section_offset = 0;
 
@@ -983,10 +999,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)
        {
@@ -997,7 +1013,7 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                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);
@@ -1035,7 +1051,7 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                        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 );
                }
 
@@ -1052,10 +1068,9 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                {
                        u32 size_read;
 
-                       if (buffer_size - run_size <= image->sections[section].size - section_offset)
-                               size_read = buffer_size - run_size;
-                       else
-                               size_read = image->sections[section].size - section_offset;
+                       size_read = run_size - buffer_size;
+                       if (size_read > image->sections[section].size - section_offset)
+                           size_read = image->sections[section].size - section_offset;
 
                        if ((retval = image_read_section(image, section, section_offset,
                                        size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
@@ -1064,11 +1079,11 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                                free(padding);
                                return retval;
                        }
-                       
+
                        /* see if we need to pad the section */
                        while (padding[section]--)
-                               buffer[size_read++] = 0xff;
-                       
+                                (buffer+buffer_size)[size_read++] = 0xff;
+
                        buffer_size += size_read;
                        section_offset += size_read;
 
@@ -1104,9 +1119,9 @@ int flash_write(target_t *target, image_t *image, u32 *written, int erase)
                if (written != NULL)
                        *written += run_size; /* add run size to total written counter */
        }
-       
+
        free(padding);
-       
+
        return retval;
 }
 
@@ -1117,17 +1132,18 @@ int default_flash_mem_blank_check(struct flash_bank_s *bank)
        int buffer_size = sizeof(buffer);
        int i;
        int nBytes;
-       
+
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
-       
+
        for (i = 0; i < bank->num_sectors; i++)
        {
                int j;
                bank->sectors[i].is_erased = 1;
-               
+
                for (j = 0; j < bank->sectors[i].size; j += buffer_size)
                {
                        int chunk;
@@ -1137,11 +1153,11 @@ int default_flash_mem_blank_check(struct flash_bank_s *bank)
                        {
                                chunk = (j - bank->sectors[i].size);
                        }
-                       
+
                        retval = target->type->read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
                        if (retval != ERROR_OK)
                                return retval;
-               
+
                        for (nBytes = 0; nBytes < chunk; nBytes++)
                        {
                                if (buffer[nBytes] != 0xFF)
@@ -1152,7 +1168,7 @@ int default_flash_mem_blank_check(struct flash_bank_s *bank)
                        }
                }
        }
-       
+
        return ERROR_OK;
 }
 
@@ -1163,17 +1179,18 @@ int default_flash_blank_check(struct flash_bank_s *bank)
        int retval;
        int fast_check = 0;
        int blank;
-       
+
        if (bank->target->state != TARGET_HALTED)
        {
+               LOG_ERROR("Target not halted");
                return ERROR_TARGET_NOT_HALTED;
        }
-               
+
        for (i = 0; i < bank->num_sectors; i++)
        {
                u32 address = bank->base + bank->sectors[i].offset;
                u32 size = bank->sectors[i].size;
-               
+
                if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
                {
                        fast_check = 0;
@@ -1185,12 +1202,12 @@ int default_flash_blank_check(struct flash_bank_s *bank)
                        bank->sectors[i].is_erased = 0;
                fast_check = 1;
        }
-               
+
        if (!fast_check)
        {
                LOG_USER("Running slow fallback erase check - add working memory");
                return default_flash_mem_blank_check(bank);
        }
-       
+
        return ERROR_OK;
 }