flash/nor: improved API of flash_driver.info & fixed buffer overruns
[openocd.git] / src / flash / nor / efm32.c
index 9cdc325735f0c63349ebb3fe33dba72341ea09d8..6461e4c7257521494ba7e9b2fa9c12c5d40959e1 100644 (file)
@@ -99,7 +99,7 @@ struct efm32_family_data {
 };
 
 struct efm32x_flash_bank {
-       int probed;
+       bool probed;
        uint32_t lb_page[LOCKBITS_PAGE_SZ/4];
        uint32_t reg_base;
        uint32_t reg_lock;
@@ -324,23 +324,7 @@ static int efm32x_read_info(struct flash_bank *bank,
        return ERROR_OK;
 }
 
-/*
- * Helper to create a human friendly string describing a part
- */
-static int efm32x_decode_info(struct efm32_info *info, char *buf, int buf_size)
-{
-       int printed = 0;
-       printed = snprintf(buf, buf_size, "%s Gecko, rev %d",
-                       info->family_data->name, info->prod_rev);
-
-       if (printed >= buf_size)
-               return ERROR_BUF_TOO_SMALL;
-
-       return ERROR_OK;
-}
-
-/* flash bank efm32 <base> <size> 0 0 <target#>
- */
+/* flash bank efm32 <base> <size> 0 0 <target#> */
 FLASH_BANK_COMMAND_HANDLER(efm32x_flash_bank_command)
 {
        struct efm32x_flash_bank *efm32x_info;
@@ -351,7 +335,7 @@ FLASH_BANK_COMMAND_HANDLER(efm32x_flash_bank_command)
        efm32x_info = malloc(sizeof(struct efm32x_flash_bank));
 
        bank->driver_priv = efm32x_info;
-       efm32x_info->probed = 0;
+       efm32x_info->probed = false;
        memset(efm32x_info->lb_page, 0xff, LOCKBITS_PAGE_SZ);
 
        return ERROR_OK;
@@ -467,10 +451,10 @@ static int efm32x_erase_page(struct flash_bank *bank, uint32_t addr)
                EFM32_MSC_STATUS_BUSY_MASK, 0);
 }
 
-static int efm32x_erase(struct flash_bank *bank, int first, int last)
+static int efm32x_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct target *target = bank->target;
-       int i = 0;
        int ret = 0;
 
        if (TARGET_HALTED != target->state) {
@@ -485,7 +469,7 @@ static int efm32x_erase(struct flash_bank *bank, int first, int last)
                return ret;
        }
 
-       for (i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
                ret = efm32x_erase_page(bank, bank->sectors[i].offset);
                if (ERROR_OK != ret)
                        LOG_ERROR("Failed to erase page %d", i);
@@ -501,7 +485,6 @@ static int efm32x_read_lock_data(struct flash_bank *bank)
 {
        struct efm32x_flash_bank *efm32x_info = bank->driver_priv;
        struct target *target = bank->target;
-       int i = 0;
        int data_size = 0;
        uint32_t *ptr = NULL;
        int ret = 0;
@@ -513,7 +496,7 @@ static int efm32x_read_lock_data(struct flash_bank *bank)
 
        ptr = efm32x_info->lb_page;
 
-       for (i = 0; i < data_size; i++, ptr++) {
+       for (int i = 0; i < data_size; i++, ptr++) {
                ret = target_read_u32(target, EFM32_MSC_LOCK_BITS+i*4, ptr);
                if (ERROR_OK != ret) {
                        LOG_ERROR("Failed to read PLW %d", i);
@@ -616,10 +599,10 @@ static int efm32x_set_page_lock(struct flash_bank *bank, size_t page, int set)
        return ERROR_OK;
 }
 
-static int efm32x_protect(struct flash_bank *bank, int set, int first, int last)
+static int efm32x_protect(struct flash_bank *bank, int set, unsigned int first,
+               unsigned int last)
 {
        struct target *target = bank->target;
-       int i = 0;
        int ret = 0;
 
        if (!set) {
@@ -632,7 +615,7 @@ static int efm32x_protect(struct flash_bank *bank, int set, int first, int last)
                return ERROR_TARGET_NOT_HALTED;
        }
 
-       for (i = first; i <= last; i++) {
+       for (unsigned int i = first; i <= last; i++) {
                ret = efm32x_set_page_lock(bank, i, set);
                if (ERROR_OK != ret) {
                        LOG_ERROR("Failed to set lock on page %d", i);
@@ -952,9 +935,7 @@ reset_pg_and_lock:
                retval = retval2;
 
 cleanup:
-       if (new_buffer)
-               free(new_buffer);
-
+       free(new_buffer);
        return retval;
 }
 
@@ -963,22 +944,17 @@ static int efm32x_probe(struct flash_bank *bank)
        struct efm32x_flash_bank *efm32x_info = bank->driver_priv;
        struct efm32_info efm32_mcu_info;
        int ret;
-       int i;
        uint32_t base_address = 0x00000000;
-       char buf[256];
 
-       efm32x_info->probed = 0;
+       efm32x_info->probed = false;
        memset(efm32x_info->lb_page, 0xff, LOCKBITS_PAGE_SZ);
 
        ret = efm32x_read_info(bank, &efm32_mcu_info);
        if (ERROR_OK != ret)
                return ret;
 
-       ret = efm32x_decode_info(&efm32_mcu_info, buf, sizeof(buf));
-       if (ERROR_OK != ret)
-               return ret;
-
-       LOG_INFO("detected part: %s", buf);
+       LOG_INFO("detected part: %s Gecko, rev %d",
+                       efm32_mcu_info.family_data->name, efm32_mcu_info.prod_rev);
        LOG_INFO("flash size = %dkbytes", efm32_mcu_info.flash_sz_kib);
        LOG_INFO("flash page size = %dbytes", efm32_mcu_info.page_size);
 
@@ -989,10 +965,8 @@ static int efm32x_probe(struct flash_bank *bank)
 
        assert(num_pages > 0);
 
-       if (bank->sectors) {
-               free(bank->sectors);
-               bank->sectors = NULL;
-       }
+       free(bank->sectors);
+       bank->sectors = NULL;
 
        bank->base = base_address;
        bank->size = (num_pages * efm32_mcu_info.page_size);
@@ -1006,14 +980,14 @@ static int efm32x_probe(struct flash_bank *bank)
 
        bank->sectors = malloc(sizeof(struct flash_sector) * num_pages);
 
-       for (i = 0; i < num_pages; i++) {
+       for (int i = 0; i < num_pages; i++) {
                bank->sectors[i].offset = i * efm32_mcu_info.page_size;
                bank->sectors[i].size = efm32_mcu_info.page_size;
                bank->sectors[i].is_erased = -1;
                bank->sectors[i].is_protected = 1;
        }
 
-       efm32x_info->probed = 1;
+       efm32x_info->probed = true;
 
        return ERROR_OK;
 }
@@ -1030,7 +1004,6 @@ static int efm32x_protect_check(struct flash_bank *bank)
 {
        struct target *target = bank->target;
        int ret = 0;
-       int i = 0;
 
        if (target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
@@ -1045,16 +1018,16 @@ static int efm32x_protect_check(struct flash_bank *bank)
 
        assert(NULL != bank->sectors);
 
-       for (i = 0; i < bank->num_sectors; i++)
+       for (unsigned int i = 0; i < bank->num_sectors; i++)
                bank->sectors[i].is_protected = efm32x_get_page_lock(bank, i);
 
        return ERROR_OK;
 }
 
-static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_efm32x_info(struct flash_bank *bank, struct command_invocation *cmd)
 {
        struct efm32_info info;
-       int ret = 0;
+       int ret;
 
        ret = efm32x_read_info(bank, &info);
        if (ERROR_OK != ret) {
@@ -1062,7 +1035,8 @@ static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size)
                return ret;
        }
 
-       return efm32x_decode_info(&info, buf, buf_size);
+       command_print_sameline(cmd, "%s Gecko, rev %d", info.family_data->name, info.prod_rev);
+       return ERROR_OK;
 }
 
 COMMAND_HANDLER(efm32x_handle_debuglock_command)

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)