Skip to contentSkip to conversation

hw/block/m25p80: Fix erase and page program behavior

PATCH In review Yifan Wang 2 participants 6 messages Nov 13 – Nov 16 Arm/Aspeed machine support

lore
Share & tools
lore

Yifan Wang

author
#0 lore raw

cover hw/block/m25p80: Fix erase and page program behavior

Hi,

This series fixes erase alignment, write-enable latch handling, block protection on erase, and page-program address wrapping in the M25P80 flash model.

Patch 1 aligns erase addresses to the containing erase unit and checks the device bounds. Patch 2 clears the write-enable latch after WRSR2. Patch 3 checks block protection before erase operations and updates the ASPEED SMC tests to clear protection before their setup erases. Patch 4 wraps page-program addresses within the page and corrects the page sizes of affected devices.

Comments and suggestions are welcome.

Thanks,
Yifan

Chao Liu (4):
hw/block/m25p80: fix erase past the end of the flash device hw/block/m25p80: fix write enable latch after WRSR2 hw/block/m25p80: fix block protection on erase hw/block/m25p80: fix page program address wrap

 hw/block/m25p80.c              | 118 +++++++++++++++++++++++++--------
 tests/qtest/aspeed-smc-utils.c |  19 ++++++
 2 files changed, 109 insertions(+), 28 deletions(-)

base-commit: 49559567804523cb28cad002aa8e97f444e23ad7

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/4 hw/block/m25p80: fix erase past the end of the flash device

From: Chao Liu <[email protected]>

Erase commands are addressed at the containing erase unit; the flash ignores the low address bits, so an erase in the last erase unit of the device must not extend past the end of the backing store.

Fixes 82a2499011a7 ("m25p80: Initial implementation of SPI flash device")
---
 hw/block/m25p80.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/block/m25p80.c +13 -0
index baf798c6b5..2a882c392c 100644
@@ -639,6 +639,19 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
         abort();
     }
 
+    /*
+     * The flash ignores the low address bits of erase commands and erases
+     * the containing erase unit. Reject requests whose erase unit is not
+     * entirely within the device.
+     */
+    offset &= ~(len - 1);
+    if (offset + len > s->size) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "M25P80: erase at 0x%x for %u bytes is outside the "
+                      "device (size 0x%x)\n", offset, len, s->size);
+        return;
+    }
+
     trace_m25p80_flash_erase(s, offset, len);
 
     if ((s->pi->flags & capa_to_assert) != capa_to_assert) {
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH 2/4 hw/block/m25p80: fix write enable latch after WRSR2

In reply to Yifan Wang

From: Chao Liu <[email protected]>

The flash clears the write enable latch when a WRSR2 command completes, just like a WRSR write; otherwise a following program or erase could proceed without a new WREN.

Fixes 9785731ec4f7 ("hw/block:m25p80: Support write status register 2 command (0x31) for w25q01jvq")
---
 hw/block/m25p80.c | 3 +++
 1 file changed, 3 insertions(+)
hw/block/m25p80.c +3 -0
index 2a882c392c..d0a8ecf1b8 100644
@@ -853,6 +853,9 @@ static void complete_collecting_data(Flash *s)
         default:
             break;
         }
+        if (s->write_enable) {
+            s->write_enable = false;
+        }
         break;
     case BRWR:
     case EXTEND_ADDR_WRITE:
Signature · 1 line
2.53.0
Reviewed-by on v1 2/4
#3 lore raw

Re: [PATCH 2/4] hw/block/m25p80: fix write enable latch after WRSR2

Quoted 18 lines · Yifan Wang

From: Chao Liu <[email protected]>

The flash clears the write enable latch when a WRSR2 command completes, just like a WRSR write; otherwise a following program or erase could proceed without a new WREN.

Fixes 9785731ec4f7 ("hw/block:m25p80: Support write status register 2 command (0x31) for w25q01jvq")
---
  hw/block/m25p80.c | 3 +++
  1 file changed, 3 insertions(+)
hw/block/m25p80.c +0 -0
index 2a882c392c..d0a8ecf1b8 100644

hw/block/m25p80.c static void complete_collecting_data(Flash *s) · line 857

Quoted 5 lines
hw/block/m25p80.c +2 -0
@@ -853,6 +853,9 @@ static void complete_collecting_data(Flash *s)
          default:
              break;
          }
+        if (s->write_enable) {
+            s->write_enable = false;

Directly assign? Otherwise:

Quoted 4 lines
hw/block/m25p80.c +1 -0
+        }
          break;
      case BRWR:
      case EXTEND_ADDR_WRITE:
author
#4 lore raw

PATCH 3/4 hw/block/m25p80: fix block protection on erase

In reply to Yifan Wang

From: Chao Liu <[email protected]>

The flash ignores erase commands that overlap a block-protected region, including chip erase; otherwise protected data can be erased.

Fixes 2113a128976f ("hw: m25p80: Add Block Protect and Top Bottom bits for write protect")
---
 hw/block/m25p80.c              | 77 ++++++++++++++++++++++++----------
 tests/qtest/aspeed-smc-utils.c | 19 +++++++++
 2 files changed, 74 insertions(+), 22 deletions(-)
hw/block/m25p80.c +55 -22
index d0a8ecf1b8..15aba33bce 100644
@@ -602,6 +602,52 @@ static inline void flash_sync_area(Flash *s, int64_t off, int64_t len)
     blk_aio_pwritev(s->blk, off, iov, 0, blk_sync_complete, iov);
 }
 
+static uint32_t flash_protected_sectors(const Flash *s)
+{
+    uint32_t block_protect_value = (s->block_protect3 << 3) |
+                                   (s->block_protect2 << 2) |
+                                   (s->block_protect1 << 1) |
+                                   (s->block_protect0 << 0);
+
+    if (!block_protect_value) {
+        return 0;
+    }
+
+    return MIN(1U << (block_protect_value - 1), s->pi->n_sectors);
+}
+
+static bool flash_sector_protected(const Flash *s, uint32_t sector)
+{
+    uint32_t num_protected = flash_protected_sectors(s);
+
+    if (!num_protected) {
+        return false;
+    }
+
+    if (!s->top_bottom_bit) {
+        return sector + num_protected >= s->pi->n_sectors;
+    }
+
+    return sector < num_protected;
+}
+
+static bool flash_erase_protected(const Flash *s, uint32_t offset, uint32_t len)
+{
+    uint32_t first_sector = offset / s->pi->sector_size;
+    uint64_t last = ((uint64_t)offset + len - 1) / s->pi->sector_size;
+    uint32_t num_protected = flash_protected_sectors(s);
+
+    if (!num_protected) {
+        return false;
+    }
+
+    if (!s->top_bottom_bit) {
+        return last >= s->pi->n_sectors - num_protected;
+    }
+
+    return first_sector < num_protected;
+}
+
 static void flash_erase(Flash *s, int offset, FlashCMD cmd)
 {
     uint32_t len;
@@ -663,6 +709,12 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
         qemu_log_mask(LOG_GUEST_ERROR, "M25P80: erase with write protect!\n");
         return;
     }
+
+    if (flash_erase_protected(s, offset, len)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "M25P80: erase with write protect!\n");
+        return;
+    }
+
     memset(s->storage + offset, 0xff, len);
     flash_sync_area(s, offset, len);
 }
@@ -680,34 +732,15 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)
 {
     uint32_t page = addr / s->pi->page_size;
     uint8_t prev = s->storage[s->cur_addr];
-    uint32_t block_protect_value = (s->block_protect3 << 3) |
-                                   (s->block_protect2 << 2) |
-                                   (s->block_protect1 << 1) |
-                                   (s->block_protect0 << 0);
 
     if (!s->write_enable) {
         qemu_log_mask(LOG_GUEST_ERROR, "M25P80: write with write protect!\n");
         return;
     }
 
-    if (block_protect_value > 0) {
-        uint32_t num_protected_sectors = 1 << (block_protect_value - 1);
-        uint32_t sector = addr / s->pi->sector_size;
-
-        /* top_bottom_bit == 0 means TOP */
-        if (!s->top_bottom_bit) {
-            if (s->pi->n_sectors <= sector + num_protected_sectors) {
-                qemu_log_mask(LOG_GUEST_ERROR,
-                              "M25P80: write with write protect!\n");
-                return;
-            }
-        } else {
-            if (sector < num_protected_sectors) {
-                qemu_log_mask(LOG_GUEST_ERROR,
-                              "M25P80: write with write protect!\n");
-                return;
-            }
-        }
+    if (flash_sector_protected(s, addr / s->pi->sector_size)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "M25P80: write with write protect!\n");
+        return;
     }
 
     if ((prev ^ data) & data) {
tests/qtest/aspeed-smc-utils.c +19 -0
index 146332240d..ac54385958 100644
@@ -173,6 +173,13 @@ static void flash_reset(const AspeedSMCTestData *data)
 {
     spi_conf(data, 1 << (CONF_ENABLE_W0 + data->cs));
 
+    /* Chip erase is ignored while any memory region is protected. */
+    spi_ctrl_start_user(data);
+    flash_writeb(data, 0, WREN);
+    flash_writeb(data, 0, WRSR);
+    flash_writeb(data, 0, 0);
+    spi_ctrl_stop_user(data);
+
     spi_ctrl_start_user(data);
     flash_writeb(data, 0, RESET_ENABLE);
     flash_writeb(data, 0, RESET_MEMORY);
@@ -595,6 +602,12 @@ void aspeed_smc_test_write_block_protect(const void *data)
     for (int i = 0; i < 16; i++) {
         bp_bits = ((i & 0b1000) << 3) | ((i & 0b0111) << 2);
 
+        spi_ctrl_start_user(test_data);
+        flash_writeb(test_data, 0, WREN);
+        flash_writeb(test_data, 0, WRSR);
+        flash_writeb(test_data, 0, 0);
+        spi_ctrl_stop_user(test_data);
+
         spi_ctrl_start_user(test_data);
         flash_writeb(test_data, 0, WREN);
         flash_writeb(test_data, 0, BULK_ERASE);
@@ -641,6 +654,12 @@ void aspeed_smc_test_write_block_protect_bottom_bit(const void *data)
     for (int i = 0; i < 16; i++) {
         bp_bits = (((i & 0b1000) | 0b0100) << 3) | ((i & 0b0111) << 2);
 
+        spi_ctrl_start_user(test_data);
+        flash_writeb(test_data, 0, WREN);
+        flash_writeb(test_data, 0, WRSR);
+        flash_writeb(test_data, 0, 0);
+        spi_ctrl_stop_user(test_data);
+
         spi_ctrl_start_user(test_data);
         flash_writeb(test_data, 0, WREN);
         flash_writeb(test_data, 0, BULK_ERASE);
Signature · 1 line
2.53.0
author
#5 lore raw

PATCH 4/4 hw/block/m25p80: fix page program address wrap

In reply to Yifan Wang

From: Chao Liu <[email protected]>

The flash wraps page program addressing within the page when the write crosses the page boundary; otherwise a page-crossing program can corrupt the following page. Correct the affected devices' page sizes to ensure wrapping at the correct boundary.

Fixes 82a2499011a7 ("m25p80: Initial implementation of SPI flash device")
---
 hw/block/m25p80.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
hw/block/m25p80.c +19 -6
index 15aba33bce..7205c48b1a 100644
@@ -194,8 +194,10 @@ static const FlashPartInfo known_devices[] = {
      * Atmel EEPROMS - it is assumed, that don't care bit in command
      * is set to 0. Block protection is not supported.
      */
-    { INFO("at25128a-nonjedec", 0x0,     0,         1, 131072, EEPROM) },
-    { INFO("at25256a-nonjedec", 0x0,     0,         1, 262144, EEPROM) },
+    { INFO("at25128a-nonjedec", 0x0,     0,         1, 131072, EEPROM),
+      .page_size = 64 },
+    { INFO("at25256a-nonjedec", 0x0,     0,         1, 262144, EEPROM),
+      .page_size = 64 },
 
     /* EON -- en25xxx */
     { INFO("en25f32",     0x1c3116,      0,  64 << 10,  64, ER_4K) },
@@ -289,10 +291,13 @@ static const FlashPartInfo known_devices[] = {
      */
     { INFO("s25sl032p",   0x010215, 0x4d00,  64 << 10,  64, ER_4K) },
     { INFO("s25sl064p",   0x010216, 0x4d00,  64 << 10, 128, ER_4K) },
-    { INFO("s25fl256s0",  0x010219, 0x4d00, 256 << 10, 128, 0) },
+    { INFO("s25fl256s0",  0x010219, 0x4d00, 256 << 10, 128, 0),
+      .page_size = 512 },
     { INFO("s25fl256s1",  0x010219, 0x4d01,  64 << 10, 512, 0) },
-    { INFO6("s25fl512s",  0x010220, 0x4d0080, 256 << 10, 256, 0) },
-    { INFO6("s70fl01gs",  0x010221, 0x4d0080, 256 << 10, 512, 0) },
+    { INFO6("s25fl512s",  0x010220, 0x4d0080, 256 << 10, 256, 0),
+      .page_size = 512 },
+    { INFO6("s70fl01gs",  0x010221, 0x4d0080, 256 << 10, 512, 0),
+      .page_size = 512 },
     { INFO("s25sl12800",  0x012018, 0x0300, 256 << 10,  64, 0) },
     { INFO("s25sl12801",  0x012018, 0x0301,  64 << 10, 256, 0) },
     { INFO("s25fl129p0",  0x012018, 0x4d00, 256 << 10,  64, 0) },
@@ -1675,7 +1680,15 @@ static uint32_t m25p80_transfer8(SSIPeripheral *ss, uint32_t tx)
     case STATE_PAGE_PROGRAM:
         trace_m25p80_page_program(s, s->cur_addr, (uint8_t)tx);
         flash_write8(s, s->cur_addr, (uint8_t)tx);
-        s->cur_addr = (s->cur_addr + 1) & (s->size - 1);
+        if (s->cmd_in_progress == AAI_WP) {
+            s->cur_addr = (s->cur_addr + 1) & (s->size - 1);
+        } else {
+            uint32_t page_size = s->pi->page_size;
+            uint32_t page_offset = (s->cur_addr + 1) % page_size;
+
+            s->cur_addr -= s->cur_addr % page_size;
+            s->cur_addr += page_offset;
+        }
 
         if (get_man(s) == MAN_SST && s->aai_enable && s->cur_addr == 0) {
             /*
Signature · 1 line
2.53.0