This is an automated email from Gerrit.

"Erhan Kurubas <erhan.kuru...@espressif.com>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/7078

-- gerrit

commit 6dc0dfa9d4fba9de7f885176e2dad72102b0fa7c
Author: Erhan Kurubas <erhan.kuru...@espressif.com>
Date:   Sun Jul 3 13:30:37 2022 +0300

    flash/nor/cfi: check available working area before try to allocate
    
    Signed-off-by: Erhan Kurubas <erhan.kuru...@espressif.com>
    Change-Id: I67b0201c37997c024974bf9f5a0af04ab9b6bd8a

diff --git a/src/flash/nor/cfi.c b/src/flash/nor/cfi.c
index 85115869d5..c847a24d64 100644
--- a/src/flash/nor/cfi.c
+++ b/src/flash/nor/cfi.c
@@ -1132,7 +1132,6 @@ static int cfi_intel_write_block(struct flash_bank *bank, 
const uint8_t *buffer,
        struct arm_algorithm arm_algo;
        struct working_area *write_algorithm;
        struct working_area *source = NULL;
-       uint32_t buffer_size = 32768;
        uint32_t write_command_val, busy_pattern_val, error_pattern_val;
 
        /* algorithm register usage:
@@ -1269,18 +1268,18 @@ static int cfi_intel_write_block(struct flash_bank 
*bank, const uint8_t *buffer,
                goto cleanup;
        }
 
-       /* Get a workspace buffer for the data to flash starting with 32k size.
-        * Half size until buffer would be smaller 256 Bytes then fail back */
+       /* Get the available workspace buffer for the data to flash.
+        * Available buffer must be bigger than 256 Bytes */
        /* FIXME Why 256 bytes, why not 32 bytes (smallest flash write page */
-       while (target_alloc_working_area_try(target, buffer_size, &source) != 
ERROR_OK) {
-               buffer_size /= 2;
-               if (buffer_size <= 256) {
-                       LOG_WARNING(
-                               "no large enough working area available, can't 
do block memory writes");
-                       retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
-                       goto cleanup;
-               }
+       uint32_t buffer_size = target_get_working_area_avail(target);
+       if (buffer_size <= 256) {
+               LOG_WARNING("no large enough working area available, can't do 
block memory writes");
+               retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
+               goto cleanup;
        }
+       retval = target_alloc_working_area(target, buffer_size, &source);
+       if (retval != ERROR_OK)
+               goto cleanup;
 
        /* setup algo registers */
        init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
@@ -1383,7 +1382,6 @@ static int cfi_spansion_write_block_mips(struct 
flash_bank *bank, const uint8_t
        struct mips32_algorithm mips32_info;
        struct working_area *write_algorithm;
        struct working_area *source;
-       uint32_t buffer_size = 32768;
        uint32_t status;
        int retval = ERROR_OK;
 
@@ -1509,18 +1507,17 @@ static int cfi_spansion_write_block_mips(struct 
flash_bank *bank, const uint8_t
 
        /* the following code still assumes target code is fixed 24*4 bytes */
 
-       while (target_alloc_working_area_try(target, buffer_size, &source) != 
ERROR_OK) {
-               buffer_size /= 2;
-               if (buffer_size <= 256) {
-                       /* we already allocated the writing code, but failed to 
get a
-                        * buffer, free the algorithm */
-                       target_free_working_area(target, write_algorithm);
-
-                       LOG_WARNING(
-                               "not enough working area available, can't do 
block memory writes");
-                       return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
-               }
+       uint32_t buffer_size = target_get_working_area_avail(target);
+       if (buffer_size <= 256) {
+               /* we already allocated the writing code, but failed to get a
+                       * buffer, free the algorithm */
+               target_free_working_area(target, write_algorithm);
+               LOG_WARNING("not enough working area available, can't do block 
memory writes");
+               return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
        }
+       retval = target_alloc_working_area(target, buffer_size, &source);
+       if (retval != ERROR_OK)
+               goto cleanup;
 
        init_reg_param(&reg_params[0], "r4", 32, PARAM_OUT);
        init_reg_param(&reg_params[1], "r5", 32, PARAM_OUT);
@@ -1569,6 +1566,7 @@ static int cfi_spansion_write_block_mips(struct 
flash_bank *bank, const uint8_t
                count -= thisrun_count;
        }
 
+cleanup:
        target_free_all_working_areas(target);
 
        destroy_reg_param(&reg_params[0]);
@@ -1597,7 +1595,6 @@ static int cfi_spansion_write_block(struct flash_bank 
*bank, const uint8_t *buff
        struct armv7m_algorithm armv7m_algo;
        struct working_area *write_algorithm;
        struct working_area *source;
-       uint32_t buffer_size = 32768;
        uint32_t status;
        int retval = ERROR_OK;
 
@@ -1888,18 +1885,17 @@ static int cfi_spansion_write_block(struct flash_bank 
*bank, const uint8_t *buff
 
        /* the following code still assumes target code is fixed 24*4 bytes */
 
-       while (target_alloc_working_area_try(target, buffer_size, &source) != 
ERROR_OK) {
-               buffer_size /= 2;
-               if (buffer_size <= 256) {
-                       /* we already allocated the writing code, but failed to 
get a
-                        * buffer, free the algorithm */
-                       target_free_working_area(target, write_algorithm);
-
-                       LOG_WARNING(
-                               "not enough working area available, can't do 
block memory writes");
-                       return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
-               }
+       uint32_t buffer_size = target_get_working_area_avail(target);
+       if (buffer_size <= 256) {
+               /* we already allocated the writing code, but failed to get a
+                       * buffer, free the algorithm */
+               target_free_working_area(target, write_algorithm);
+               LOG_WARNING("not enough working area available, can't do block 
memory writes");
+               return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
        }
+       retval = target_alloc_working_area(target, buffer_size, &source);
+       if (retval != ERROR_OK)
+               goto cleanup;
 
        init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
        init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
@@ -1948,6 +1944,7 @@ static int cfi_spansion_write_block(struct flash_bank 
*bank, const uint8_t *buff
                count -= thisrun_count;
        }
 
+cleanup:
        target_free_all_working_areas(target);
 
        destroy_reg_param(&reg_params[0]);

-- 

Reply via email to