flash: add error handling to get_flash_by_addr/name
authorØyvind Harboe <oyvind.harboe@zylin.com>
Fri, 11 Jun 2010 06:10:39 +0000 (08:10 +0200)
committerØyvind Harboe <oyvind.harboe@zylin.com>
Fri, 11 Jun 2010 13:53:23 +0000 (15:53 +0200)
autoprobing can fail and this error has to be
reported up the call stack.

Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
src/flash/nor/core.c
src/flash/nor/core.h
src/flash/nor/tcl.c

index 429bad6762dcbe606cb776f5f39e1eceefc4cfd0..d2f2754d67ce714b94605f8211b7b50bd491cbb9 100644 (file)
@@ -197,7 +197,7 @@ struct flash_bank *get_flash_bank_by_name_noprobe(const char *name)
        return NULL;
 }
 
        return NULL;
 }
 
-struct flash_bank *get_flash_bank_by_name(const char *name)
+int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
 {
        struct flash_bank *bank;
        int retval;
 {
        struct flash_bank *bank;
        int retval;
@@ -210,11 +210,12 @@ struct flash_bank *get_flash_bank_by_name(const char *name)
                if (retval != ERROR_OK)
                {
                        LOG_ERROR("auto_probe failed %d\n", retval);
                if (retval != ERROR_OK)
                {
                        LOG_ERROR("auto_probe failed %d\n", retval);
-                       return NULL;
+                       return retval;
                }
        }
 
                }
        }
 
-       return bank;
+       *bank_result = bank;
+       return ERROR_OK;
 }
 
 int get_flash_bank_by_num(int num, struct flash_bank **bank)
 }
 
 int get_flash_bank_by_num(int num, struct flash_bank **bank)
@@ -238,8 +239,9 @@ int get_flash_bank_by_num(int num, struct flash_bank **bank)
        return ERROR_OK;
 }
 
        return ERROR_OK;
 }
 
-/* lookup flash bank by address */
-struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
+/* lookup flash bank by address, bank not found is success, but
+ * result_bank is set to NULL. */
+int get_flash_bank_by_addr(struct target *target, uint32_t addr, bool check, struct flash_bank **result_bank)
 {
        struct flash_bank *c;
 
 {
        struct flash_bank *c;
 
@@ -252,14 +254,22 @@ struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
                if (retval != ERROR_OK)
                {
                        LOG_ERROR("auto_probe failed %d\n", retval);
                if (retval != ERROR_OK)
                {
                        LOG_ERROR("auto_probe failed %d\n", retval);
-                       return NULL;
+                       return retval;
                }
                /* check whether address belongs to this flash bank */
                if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
                }
                /* check whether address belongs to this flash bank */
                if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
-                       return c;
+               {
+                       *result_bank = c;
+                       return ERROR_OK;
+               }
        }
        }
-       LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
-       return NULL;
+       *result_bank = NULL;
+       if (check)
+       {
+               LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
+               return ERROR_FAIL;
+       }
+       return ERROR_OK;
 }
 
 int default_flash_mem_blank_check(struct flash_bank *bank)
 }
 
 int default_flash_mem_blank_check(struct flash_bank *bank)
@@ -379,8 +389,9 @@ static int flash_iterate_address_range(struct target *target,
        int last = -1;
        int i;
 
        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 */
+       int retval = get_flash_bank_by_addr(target, addr, true, &c);
+       if (retval != ERROR_OK)
+               return retval;
 
        if (c->size == 0 || c->num_sectors == 0)
        {
 
        if (c->size == 0 || c->num_sectors == 0)
        {
@@ -588,7 +599,11 @@ int flash_write_unlock(struct target *target, struct image *image,
                }
 
                /* find the corresponding flash bank */
                }
 
                /* find the corresponding flash bank */
-               if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
+               int retval;
+               retval = get_flash_bank_by_addr(target, run_address, false, &c);
+               if (retval != ERROR_OK)
+                       return retval;
+               if (c == NULL)
                {
                        section++; /* and skip it */
                        section_offset = 0;
                {
                        section++; /* and skip it */
                        section_offset = 0;
index 17f1c53f9276da5ad4d9e6298a8c053a07736856..ff14df0a3184852058c7f91a5525ee48a50bdc41 100644 (file)
@@ -168,7 +168,7 @@ int default_flash_mem_blank_check(struct flash_bank *bank);
  * bank number: when two str9x banks are defined, then 'str9x.1' refers
  * to the second.
  */
  * bank number: when two str9x banks are defined, then 'str9x.1' refers
  * to the second.
  */
-struct flash_bank *get_flash_bank_by_name(const char *name);
+int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result);
 /**
  * Returns the flash bank specified by @a name, which matches the
  * driver name and a suffix (option) specify the driver-specific
 /**
  * Returns the flash bank specified by @a name, which matches the
  * driver name and a suffix (option) specify the driver-specific
@@ -206,8 +206,9 @@ struct flash_bank *get_flash_bank_by_num_noprobe(int num);
  * Returns the flash bank located at a specified address.
  * @param target The target, presumed to contain one or more banks.
  * @param addr An address that is within the range of the bank.
  * Returns the flash bank located at a specified address.
  * @param target The target, presumed to contain one or more banks.
  * @param addr An address that is within the range of the bank.
+ * @param check return ERROR_OK and result_bank NULL if the bank does not exist
  * @returns The struct flash_bank located at @a addr, or NULL.
  */
  * @returns The struct flash_bank located at @a addr, or NULL.
  */
-struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr);
+int get_flash_bank_by_addr(struct target *target, uint32_t addr, bool check, struct flash_bank **result_bank);
 
 #endif // FLASH_NOR_CORE_H
 
 #endif // FLASH_NOR_CORE_H
index cf1ca4af150b349bb08ed73f7551429cd19ac60a..3d67327fd3ca771145354eecd6b5530831891c8f 100644 (file)
@@ -35,7 +35,9 @@ COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
                struct flash_bank **bank)
 {
        const char *name = CMD_ARGV[name_index];
                struct flash_bank **bank)
 {
        const char *name = CMD_ARGV[name_index];
-       *bank = get_flash_bank_by_name(name);
+       int retval = get_flash_bank_by_name(name, bank);
+       if (retval != ERROR_OK)
+               return retval;
        if (*bank)
                return ERROR_OK;
 
        if (*bank)
                return ERROR_OK;
 
@@ -238,11 +240,9 @@ COMMAND_HANDLER(handle_flash_erase_address_command)
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
                return ERROR_COMMAND_SYNTAX_ERROR;
        }
 
-       p = get_flash_bank_by_addr(target, address);
-       if (p == NULL)
-       {
-               return ERROR_FAIL;
-       }
+       retval = get_flash_bank_by_addr(target, address, true, &p);
+       if (retval != ERROR_OK)
+               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();
 
        /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
        flash_set_dirty();
@@ -544,12 +544,9 @@ COMMAND_HANDLER(handle_flash_fill_command)
        {
                struct flash_bank *bank;
 
        {
                struct flash_bank *bank;
 
-               bank = get_flash_bank_by_addr(target, address);
-               if (bank == NULL)
-               {
-                       retval = ERROR_FAIL;
+               retval = get_flash_bank_by_addr(target, address, true, &bank );
+               if (retval != ERROR_OK)
                        goto done;
                        goto done;
-               }
 
                cur_size = MIN((count * wordsize - wrote), chunksize);
                err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
 
                cur_size = MIN((count * wordsize - wrote), chunksize);
                err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);

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)