flash/nor/rp2040: fix size of flash write buffer
authorTomas Vanek <vanekt@fbl.cz>
Sun, 11 Sep 2022 10:11:31 +0000 (12:11 +0200)
committerTomas Vanek <vanekt@fbl.cz>
Tue, 27 Sep 2022 08:39:58 +0000 (08:39 +0000)
The size of the flash write buffer should be rounded
down to the multiply of flash page size.
Using write chunks of unadjusted size results in write of chunks
unaligned to flash pages.

Change-Id: If7931362ee193dff4dc2df7ec78f13530658cf08
Signed-off-by: Tomas Vanek <vanekt@fbl.cz>
Reviewed-on: https://review.openocd.org/c/openocd/+/7187
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
src/flash/nor/rp2040.c

index 1c57424f914b0353ddd85671afc7a16cb3807fbe..ce09fea088286934c35263895b48395bf221ec5e 100644 (file)
@@ -217,7 +217,11 @@ static int rp2040_flash_write(struct flash_bank *bank, const uint8_t *buffer, ui
        if (err != ERROR_OK)
                goto cleanup;
 
-       const unsigned int chunk_size = target_get_working_area_avail(target);
+       unsigned int avail_pages = target_get_working_area_avail(target) / priv->dev->pagesize;
+       /* We try to allocate working area rounded down to device page size,
+        * al least 1 page, at most the write data size
+        */
+       unsigned int chunk_size = MIN(MAX(avail_pages, 1) * priv->dev->pagesize, count);
        err = target_alloc_working_area(target, chunk_size, &bounce);
        if (err != ERROR_OK) {
                LOG_ERROR("Could not allocate bounce buffer for flash programming. Can't continue");