flash/nor/at91sam7: fix flash bank allocation

at91sam7 flash driver allocates a flash bank based on detected flash
structure.
Use calloc() instead of malloc() - struct flash_bank has to be zeroed.

While on this:
Return error in case of struct flash_bank or driver_priv allocation fail.
Set default_padded_value and erased_value.
Use strdup() on bank->name, pointer is freed in flash_free_all_banks()

Signed-off-by: Tomas Vanek <vanekt@fbl.cz>
Change-Id: Id890496bfbadb7970ef583256aa4f30a7bff832f
Reviewed-on: https://review.openocd.org/c/openocd/+/7884
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Tomas Vanek 2023-09-05 18:32:57 +02:00 committed by Antonio Borneo
parent 870769b0ba
commit 040757b7e6
1 changed files with 26 additions and 4 deletions

View File

@ -560,11 +560,22 @@ static int at91sam7_read_part_info(struct flash_bank *bank)
if (bnk > 0) {
if (!t_bank->next) {
/* create a new flash bank element */
struct flash_bank *fb = malloc(sizeof(struct flash_bank));
struct flash_bank *fb = calloc(sizeof(struct flash_bank), 1);
if (!fb) {
LOG_ERROR("No memory for flash bank");
return ERROR_FAIL;
}
fb->target = target;
fb->driver = bank->driver;
fb->default_padded_value = 0xff;
fb->erased_value = 0xff;
fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
fb->name = "sam7_probed";
if (!fb->driver_priv) {
free(fb);
LOG_ERROR("No memory for flash driver priv");
return ERROR_FAIL;
}
fb->name = strdup("sam7_probed");
fb->next = NULL;
/* link created bank in 'flash_banks' list */
@ -738,11 +749,22 @@ FLASH_BANK_COMMAND_HANDLER(at91sam7_flash_bank_command)
if (bnk > 0) {
if (!t_bank->next) {
/* create a new bank element */
struct flash_bank *fb = malloc(sizeof(struct flash_bank));
struct flash_bank *fb = calloc(sizeof(struct flash_bank), 1);
if (!fb) {
LOG_ERROR("No memory for flash bank");
return ERROR_FAIL;
}
fb->target = target;
fb->driver = bank->driver;
fb->default_padded_value = 0xff;
fb->erased_value = 0xff;
fb->driver_priv = malloc(sizeof(struct at91sam7_flash_bank));
fb->name = "sam7_probed";
if (!fb->driver_priv) {
free(fb);
LOG_ERROR("No memory for flash driver priv");
return ERROR_FAIL;
}
fb->name = strdup("sam7_probed");
fb->next = NULL;
/* link created bank in 'flash_banks' list */