Add Block Protect (BP) and Top Bottom (TB) bits for write protect
cover Add Block Protect (BP) and Top Bottom (TB) bits for write protect
Hey everyone,
Adding the 3 block protection bits and top bottom bits which configure which parts of the flash are writable and read-only.
These patches are based on previous patches I just submitted:
hw: m25p80: fixing individual test failure when tests are running in isolation
hw: m25p80: add WP# pin and SRWD bit for write protection
hw: m25p80: add tests for write protect (WP# and SRWD bit)
The tests that were added iterates through every different memory protection case so it takes a little longer to run. Let me know what you think about that and if that's okay or not.
Thanks,
Iris
Iris Chen (2):
hw: m25p80: Add Block Protect and Top Bottom bits for write protect
hw: m25p80: add tests for BP and TB bit write protect
hw/block/m25p80.c | 74 +++++++++++++++++++---- tests/qtest/aspeed_smc-test.c | 111 ++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 12 deletions(-)
--
2.30.2
PATCH 1/2 hw: m25p80: Add Block Protect and Top Bottom bits for write protect
--- hw/block/m25p80.c | 74 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-)
hw/block/m25p80.c +62 -12
@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_nor_option_flags { +ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +SNOR_F_HAS_SR_TB =BIT (3 ), +SNOR_F_HAS_SR_BP3_BIT6 =BIT (4 ), +}; + typedef struct FlashPartInfo { const char *part_name; /*
@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | SNOR_F_HAS_SR_BP3_BIT6 | SNOR_F_HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },
@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;
@@ -630,6 +634,29 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)qemu_log_mask (LOG_GUEST_ERROR ,"M25P80: write with write protect!\n" );return ; } +uint32_t block_protect_value = (s->block_protect3 <<3 ) | + (s->block_protect2 <<2 ) | + (s->block_protect1 <<1 ) | + (s->block_protect0 <<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 (block_protect_value >0 && + s->pi->n_sectors <= sector + num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }else { +if (block_protect_value >0 && sector < num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }if ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data);
@@ -728,6 +755,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :
@@ -1213,6 +1249,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;
@@ -1553,6 +1598,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }
Signature · 1 line
2.30.2
Re: [PATCH 1/2] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
Even for a simple addition a quick intro is always nice to have.
Quoted 1 line · Iris Chen
Signed-off-by Iris Chen <[email protected]>
Too bad that we are modeling 5 bits with 5 bools but given the layout it makes the code easier to read. LGTM.
Thanks,
C.
Quoted 135 lines
--- hw/block/m25p80.c | 74 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-)hw/block/m25p80.c +62
-12@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_nor_option_flags { +ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +SNOR_F_HAS_SR_TB =BIT (3 ), +SNOR_F_HAS_SR_BP3_BIT6 =BIT (4 ), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | SNOR_F_HAS_SR_BP3_BIT6 | SNOR_F_HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;@@ -630,6 +634,29 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)qemu_log_mask (LOG_GUEST_ERROR ,"M25P80: write with write protect!\n" );return ; } +uint32_t block_protect_value = (s->block_protect3 <<3 ) | + (s->block_protect2 <<2 ) | + (s->block_protect1 <<1 ) | + (s->block_protect0 <<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 (block_protect_value >0 && + s->pi->n_sectors <= sector + num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }else { +if (block_protect_value >0 && sector < num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }if ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data);@@ -728,6 +755,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1249,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;@@ -1553,6 +1598,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }
Re: [PATCH 1/2] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
In reply to Iris ChenHi Iris,
Looks good, a couple of minor comments below!
Quoted 20 lines · Iris Chen
Signed-off-by Iris Chen <[email protected]>--- hw/block/m25p80.c | 74 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-)hw/block/m25p80.c +0
-7@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set
hw/block/m25p80.c line 46
Quoted 10 lines
hw/block/m25p80.c +2
-4-*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_nor_option_flags {
(A suggestion is to s/nor/flash/ above (and s/SNOR_F_// below) since there looks to be nand flashes as W25N01GV using the protocol to).
Quoted 35 lines
hw/block/m25p80.c +14
-1+ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +SNOR_F_HAS_SR_TB =BIT (3 ), + SNOR_F_HAS_SR_BP3_BIT6 = BIT(4), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | SNOR_F_HAS_SR_BP3_BIT6 | SNOR_F_HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;@@ -630,6 +634,29 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)qemu_log_mask (LOG_GUEST_ERROR ,"M25P80: write with write protect!\n" );return ;
hw/block/m25p80.c void flash_write8(Flash *s, uint32_t addr, uint8_t data) · line 645
Quoted 10 lines
hw/block/m25p80.c +9
-0} +uint32_t block_protect_value = (s->block_protect3 <<3 ) | + (s->block_protect2 <<2 ) | + (s->block_protect1 <<1 ) | + (s->block_protect0 <<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 */
Indentation needs minor fixing on above lines, also the declarations should be at the top of the function.
Quoted 50 lines
hw/block/m25p80.c +32
-0+if (!s->top_bottom_bit) { +if (block_protect_value >0 && + s->pi->n_sectors <= sector + num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }else { +if (block_protect_value >0 && sector < num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }if ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data);@@ -728,6 +755,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1249,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;
hw/block/m25p80.c static void m25p80_reset(DeviceState *d) · line 1605
Quoted 8 lines
hw/block/m25p80.c +5
-0@@ -1553,6 +1598,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;
We need to place above ones in a subsection in the vmstate (similar to the your previous patch).
Looks good to me otherwise!
Thanks!
Best regards,
Francisco Iglesias
Quoted 7 lines
hw/block/m25p80.c +0
-0reset_memory (s); }Signature · 1 line
2.30.2
Re: [PATCH 1/2] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
Quoted 71 lines · Francisco Iglesias
Hi Iris,
Looks good, a couple of minor comments below!
Quoted 20 lines · Iris Chen
Signed-off-by Iris Chen <[email protected]>--- hw/block/m25p80.c | 74 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-)hw/block/m25p80.c +0
-7@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set hw/block/m25p80.c line 46
Quoted 10 lines
hw/block/m25p80.c +2
-4-*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_nor_option_flags {(A suggestion is to s/nor/flash/ above (and s/SNOR_F_// below) since there looks to be nand flashes as W25N01GV using the protocol to).
Quoted 32 lines
hw/block/m25p80.c +14
-1+ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +SNOR_F_HAS_SR_TB =BIT (3 ), + SNOR_F_HAS_SR_BP3_BIT6 = BIT(4), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | SNOR_F_HAS_SR_BP3_BIT6 | SNOR_F_HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;
Quoted 16 lines
hw/block/m25p80.c void flash_write8(Flash *s, uint32_t addr, uint8_t data) · line 645
Quoted 13 lines
hw/block/m25p80.c +9
-0@@ -630,6 +634,29 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)qemu_log_mask (LOG_GUEST_ERROR ,"M25P80: write with write protect!\n" );return ; } +uint32_t block_protect_value = (s->block_protect3 <<3 ) | + (s->block_protect2 <<2 ) | + (s->block_protect1 <<1 ) | + (s->block_protect0 <<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 */ Indentation needs minor fixing on above lines, also the declarations should be at the top of the function.
I agree in that case it would be better to have at the top but checkpatch does not complain. What's the rule ?
For loop indexes, I do prefer to declare in the block statement.
Quoted 52 lines
Quoted 52 lines
hw/block/m25p80.c +32
-0+if (!s->top_bottom_bit) { +if (block_protect_value >0 && + s->pi->n_sectors <= sector + num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }else { +if (block_protect_value >0 && sector < num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }if ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data);@@ -728,6 +755,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1249,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;@@ -1553,6 +1598,11 @@ static void m25p80_reset(DeviceState *d)
Quoted 10 lines
hw/block/m25p80.c static void m25p80_reset(DeviceState *d) · line 1605
Quoted 7 lines
hw/block/m25p80.c +5
-0s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;We need to place above ones in a subsection in the vmstate (similar to the your previous patch).
Ah yes. I keep forgetting these ...
Thanks for the second look Francisco.
C.
Quoted 14 lines
Looks good to me otherwise!
Thanks!
Best regards,
Francisco IglesiasQuoted 7 lines
hw/block/m25p80.c +0
-0reset_memory (s); }Signature · 1 line
2.30.2
Re: [PATCH 1/2] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
Quoted 77 lines · Cédric Le Goater
Quoted 76 lines · Francisco Iglesias
Hi Iris,
Looks good, a couple of minor comments below!
Quoted 18 lines · Iris Chen
Signed-off-by Iris Chen <[email protected]>--- hw/block/m25p80.c | 74 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-)hw/block/m25p80.c +0
-6@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for hw/block/m25p80.c line 44
Quoted 10 lines
hw/block/m25p80.c +2
-5-* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_nor_option_flags {(A suggestion is to s/nor/flash/ above (and s/SNOR_F_// below) since there looks to be nand flashes as W25N01GV using the protocol to).
Quoted 39 lines
hw/block/m25p80.c +18
-1+ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +SNOR_F_HAS_SR_TB =BIT (3 ), + SNOR_F_HAS_SR_BP3_BIT6 = BIT(4), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | SNOR_F_HAS_SR_BP3_BIT6 | SNOR_F_HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;@@ -630,6 +634,29 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)qemu_log_mask (LOG_GUEST_ERROR ,"M25P80: write with write protect!\n" );return ; } +uint32_t block_protect_value = (s->block_protect3 <<3 ) | + (s->block_protect2 <<2 ) | + (s->block_protect1 <<1 ) | + (s->block_protect0 <<0 );
Quoted 9 lines
Quoted 8 lines
hw/block/m25p80.c void flash_write8(Flash *s, uint32_t addr, uint8_t data) · line 645
Quoted 5 lines
hw/block/m25p80.c +5
-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 */ Indentation needs minor fixing on above lines, also the declarations should be at the top of the function.
Hi Cédric,
Quoted 3 lines
I agree in that case it would be better to have at the top but checkpatch does not complain. What's the rule ?
Below is taken from docs/devel/style.rst:
"
Declarations
============
Mixed declarations (interleaving statements and declarations within blocks) are generally not allowed; declarations should be at the beginning of blocks.
Every now and then, an exception is made for declarations inside a #ifdef or #ifndef block: if the code looks nicer, such declarations can be placed at the top of the block even if there are statements above. On the other hand, however, it's often best to move that #ifdef/#ifndef block to a separate function altogether
"
I understand above as this is generally not allowed but in case you would prefer to keep the declarations as they are placed one can see this as 'code looks nicer' (to me the loop case is often cleaner in the way you are mentioning). I should though have been more clear that 'moving to the top' was a suggestion for not breaking this 'general rule' (also in case I've interpreted this wrongly), sorry for that!
Best regards,
Francisco Iglesias
Quoted 83 lines
For loop indexes, I do prefer to declare in the block statement.
Quoted 50 lines
Quoted 50 lines
hw/block/m25p80.c +32
-0+if (!s->top_bottom_bit) { +if (block_protect_value >0 && + s->pi->n_sectors <= sector + num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }else { +if (block_protect_value >0 && sector < num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }if ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data);@@ -728,6 +755,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1249,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;@@ -1553,6 +1598,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false;Quoted 8 lines
hw/block/m25p80.c static void m25p80_reset(DeviceState *d) · line 1605
Quoted 5 lines
hw/block/m25p80.c +5
-0+ s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;We need to place above ones in a subsection in the vmstate (similar to the your previous patch).
Ah yes. I keep forgetting these ...
Thanks for the second look Francisco.
C.
Quoted 13 lines
Looks good to me otherwise!
Thanks!
Best regards,
Francisco IglesiasQuoted 6 lines
hw/block/m25p80.c +0
-0reset_memory (s); }Signature · 1 line
2.30.2
Re: [PATCH 1/2] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
In reply to Iris ChenQuoted 63 lines · Iris Chen
Signed-off-by Iris Chen <[email protected]>--- hw/block/m25p80.c | 74 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-)hw/block/m25p80.c +16
-12@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_nor_option_flags { +ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +SNOR_F_HAS_SR_TB =BIT (3 ), +SNOR_F_HAS_SR_BP3_BIT6 =BIT (4 ), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | SNOR_F_HAS_SR_BP3_BIT6 | SNOR_F_HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;
hw/block/m25p80.c void flash_write8(Flash *s, uint32_t addr, uint8_t data) · line 642
Quoted 9 lines
hw/block/m25p80.c +6
-0@@ -630,6 +634,29 @@ void flash_write8(Flash *s, uint32_t addr, uint8_t data)qemu_log_mask (LOG_GUEST_ERROR ,"M25P80: write with write protect!\n" );return ; } +uint32_t block_protect_value = (s->block_protect3 <<3 ) | + (s->block_protect2 <<2 ) | + (s->block_protect1 <<1 ) | + (s->block_protect0 <<0 ); + +uint32_t num_protected_sectors =1 << (block_protect_value -1 );
Something wrong will occur if all block_protect[0123] are zeroes.
C.
Quoted 64 lines
hw/block/m25p80.c +40
-0+uint32_t sector = addr / s->pi->sector_size; + +/* top_bottom_bit == 0 means TOP */ +if (!s->top_bottom_bit) { +if (block_protect_value >0 && + s->pi->n_sectors <= sector + num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }else { +if (block_protect_value >0 && sector < num_protected_sectors) { +qemu_log_mask (LOG_GUEST_ERROR , +"M25P80: write with write protect!\n" ); +return ; + } + }if ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data);@@ -728,6 +755,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1249,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &SNOR_F_HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &SNOR_F_HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;@@ -1553,6 +1598,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }
PATCH 2/2 hw: m25p80: add tests for BP and TB bit write protect
In reply to Iris Chen--- tests/qtest/aspeed_smc-test.c | 111 ++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+)
tests/qtest/aspeed_smc-test.c +111 -0
@@ -192,6 +192,24 @@ static void read_page_mem(uint32_t addr, uint32_t *page) } } +static void write_page_mem (uint32_t addr,uint32_t write_value) +{ +spi_ctrl_setmode (CTRL_WRITEMODE ,PP ); + +for (int i =0 ; i <FLASH_PAGE_SIZE /4 ; i++) { +writel (ASPEED_FLASH_BASE + addr + i *4 , write_value); + } +} + +static void assert_page_mem (uint32_t addr,uint32_t expected_value) +{ +uint32_t page[FLASH_PAGE_SIZE /4 ]; +read_page_mem (addr, page); +for (int i =0 ; i <FLASH_PAGE_SIZE /4 ; i++) { + g_assert_cmphex(page[i], ==, expected_value); + } +} +static void test_erase_sector (void ) {uint32_t some_page_addr =0x600 *FLASH_PAGE_SIZE ;
@@ -501,6 +519,95 @@ static void test_status_reg_write_protection(void)flash_reset (); } +static void test_write_block_protect (void ) +{ +uint32_t sector_size =65536 ; +uint32_t n_sectors =512 ; + +spi_ce_ctrl (1 <<CRTL_EXTENDED0 ); +spi_conf (CONF_ENABLE_W0 ); + +uint32_t bp_bits =0b0 ; + +for (int i =0 ; i <16 ; i++) { + bp_bits = ((i &0b1000 ) <<3 ) | ((i &0b0111 ) <<2 ); + +spi_ctrl_start_user (); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,BULK_ERASE ); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,WRSR ); +writeb (ASPEED_FLASH_BASE , bp_bits); +writeb (ASPEED_FLASH_BASE ,EN_4BYTE_ADDR ); +writeb (ASPEED_FLASH_BASE ,WREN ); +spi_ctrl_stop_user (); + +uint32_t num_protected_sectors = i ?MIN (1 << (i -1 ), n_sectors) :0 ; +uint32_t protection_start = n_sectors - num_protected_sectors; +uint32_t protection_end = n_sectors; + +for (int sector =0 ; sector < n_sectors; sector++) { +uint32_t addr = sector * sector_size; + +assert_page_mem (addr,0xffffffff ); +write_page_mem (addr,make_be32 (0xabcdef12 )); + +uint32_t expected_value = protection_start <= sector + && sector < protection_end + ?0xffffffff :0xabcdef12 ; + +assert_page_mem (addr, expected_value); + } + } + +flash_reset (); +} + +static void test_write_block_protect_bottom_bit (void ) +{ +uint32_t sector_size =65536 ; +uint32_t n_sectors =512 ; + +spi_ce_ctrl (1 <<CRTL_EXTENDED0 ); +spi_conf (CONF_ENABLE_W0 ); + +/* top bottom bit is enabled */ +uint32_t bp_bits =0b00100 <<3 ; + +for (int i =0 ; i <16 ; i++) { + bp_bits = (((i &0b1000 ) |0b0100 ) <<3 ) | ((i &0b0111 ) <<2 ); + +spi_ctrl_start_user (); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,BULK_ERASE ); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,WRSR ); +writeb (ASPEED_FLASH_BASE , bp_bits); +writeb (ASPEED_FLASH_BASE ,EN_4BYTE_ADDR ); +writeb (ASPEED_FLASH_BASE ,WREN ); +spi_ctrl_stop_user (); + +uint32_t num_protected_sectors = i ?MIN (1 << (i -1 ), n_sectors) :0 ; +uint32_t protection_start =0 ; +uint32_t protection_end = num_protected_sectors; + +for (int sector =0 ; sector < n_sectors; sector++) { +uint32_t addr = sector * sector_size; + +assert_page_mem (addr,0xffffffff ); +write_page_mem (addr,make_be32 (0xabcdef12 )); + +uint32_t expected_value = protection_start <= sector + && sector < protection_end + ?0xffffffff :0xabcdef12 ; + +assert_page_mem (addr, expected_value); + } + } + +flash_reset (); +} +static char tmp_path[] ="/tmp/qtest.m25p80.XXXXXX" ;int main (int argc,char **argv)
@@ -529,6 +636,10 @@ int main(int argc, char **argv)qtest_add_func ("/ast2400/smc/read_status_reg" , test_read_status_reg);qtest_add_func ("/ast2400/smc/status_reg_write_protection" , test_status_reg_write_protection); +qtest_add_func ("/ast2400/smc/write_block_protect" , + test_write_block_protect); +qtest_add_func ("/ast2400/smc/write_block_protect_bottom_bit" , + test_write_block_protect_bottom_bit);flash_reset (); ret =g_test_run ();
Signature · 1 line
2.30.2
Re: [PATCH 2/2] hw: m25p80: add tests for BP and TB bit write protect
Quoted 1 line · Iris Chen
Signed-off-by Iris Chen <[email protected]>
Palmettos are getting old (2014). We might want to change the machine model to an ast2600 one.
Anyhow, these are very good tests for both m25p80 and aspeed-smc models.
Thanks,
C.
Quoted 140 lines
--- tests/qtest/aspeed_smc-test.c | 111 ++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+)tests/qtest/aspeed_smc-test.c +111
-0@@ -192,6 +192,24 @@ static void read_page_mem(uint32_t addr, uint32_t *page) } } +static void write_page_mem (uint32_t addr,uint32_t write_value) +{ +spi_ctrl_setmode (CTRL_WRITEMODE ,PP ); + +for (int i =0 ; i <FLASH_PAGE_SIZE /4 ; i++) { +writel (ASPEED_FLASH_BASE + addr + i *4 , write_value); + } +} + +static void assert_page_mem (uint32_t addr,uint32_t expected_value) +{ +uint32_t page[FLASH_PAGE_SIZE /4 ]; +read_page_mem (addr, page); +for (int i =0 ; i <FLASH_PAGE_SIZE /4 ; i++) { + g_assert_cmphex(page[i], ==, expected_value); + } +} +static void test_erase_sector (void ) {uint32_t some_page_addr =0x600 *FLASH_PAGE_SIZE ;@@ -501,6 +519,95 @@ static void test_status_reg_write_protection(void)flash_reset (); } +static void test_write_block_protect (void ) +{ +uint32_t sector_size =65536 ; +uint32_t n_sectors =512 ; + +spi_ce_ctrl (1 <<CRTL_EXTENDED0 ); +spi_conf (CONF_ENABLE_W0 ); + +uint32_t bp_bits =0b0 ; + +for (int i =0 ; i <16 ; i++) { + bp_bits = ((i &0b1000 ) <<3 ) | ((i &0b0111 ) <<2 ); + +spi_ctrl_start_user (); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,BULK_ERASE ); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,WRSR ); +writeb (ASPEED_FLASH_BASE , bp_bits); +writeb (ASPEED_FLASH_BASE ,EN_4BYTE_ADDR ); +writeb (ASPEED_FLASH_BASE ,WREN ); +spi_ctrl_stop_user (); + +uint32_t num_protected_sectors = i ?MIN (1 << (i -1 ), n_sectors) :0 ; +uint32_t protection_start = n_sectors - num_protected_sectors; +uint32_t protection_end = n_sectors; + +for (int sector =0 ; sector < n_sectors; sector++) { +uint32_t addr = sector * sector_size; + +assert_page_mem (addr,0xffffffff ); +write_page_mem (addr,make_be32 (0xabcdef12 )); + +uint32_t expected_value = protection_start <= sector + && sector < protection_end + ?0xffffffff :0xabcdef12 ; + +assert_page_mem (addr, expected_value); + } + } + +flash_reset (); +} + +static void test_write_block_protect_bottom_bit (void ) +{ +uint32_t sector_size =65536 ; +uint32_t n_sectors =512 ; + +spi_ce_ctrl (1 <<CRTL_EXTENDED0 ); +spi_conf (CONF_ENABLE_W0 ); + +/* top bottom bit is enabled */ +uint32_t bp_bits =0b00100 <<3 ; + +for (int i =0 ; i <16 ; i++) { + bp_bits = (((i &0b1000 ) |0b0100 ) <<3 ) | ((i &0b0111 ) <<2 ); + +spi_ctrl_start_user (); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,BULK_ERASE ); +writeb (ASPEED_FLASH_BASE ,WREN ); +writeb (ASPEED_FLASH_BASE ,WRSR ); +writeb (ASPEED_FLASH_BASE , bp_bits); +writeb (ASPEED_FLASH_BASE ,EN_4BYTE_ADDR ); +writeb (ASPEED_FLASH_BASE ,WREN ); +spi_ctrl_stop_user (); + +uint32_t num_protected_sectors = i ?MIN (1 << (i -1 ), n_sectors) :0 ; +uint32_t protection_start =0 ; +uint32_t protection_end = num_protected_sectors; + +for (int sector =0 ; sector < n_sectors; sector++) { +uint32_t addr = sector * sector_size; + +assert_page_mem (addr,0xffffffff ); +write_page_mem (addr,make_be32 (0xabcdef12 )); + +uint32_t expected_value = protection_start <= sector + && sector < protection_end + ?0xffffffff :0xabcdef12 ; + +assert_page_mem (addr, expected_value); + } + } + +flash_reset (); +} +static char tmp_path[] ="/tmp/qtest.m25p80.XXXXXX" ;int main (int argc,char **argv)@@ -529,6 +636,10 @@ int main(int argc, char **argv)qtest_add_func ("/ast2400/smc/read_status_reg" , test_read_status_reg);qtest_add_func ("/ast2400/smc/status_reg_write_protection" , test_status_reg_write_protection); +qtest_add_func ("/ast2400/smc/write_block_protect" , + test_write_block_protect); +qtest_add_func ("/ast2400/smc/write_block_protect_bottom_bit" , + test_write_block_protect_bottom_bit);flash_reset (); ret =g_test_run ();
PATCH v2 1/1 hw: m25p80: Add Block Protect and Top Bottom bits for write protect
In reply to Iris Chen---
Addressing all comments.
In reponse to this comment:
"Something wrong will occur if all block_protect[0123] are zeroes", the code actually ignores num_protected_sectors when block_protect_value = 0 which happens when block_protect[0123] are zeroes.
You can refer to the bottom block in v1, where we only look at cases when block_protect_value > 0 so it is actually handled. Regardless, since we were setting num_protected_sectors
in either case, I have refactored the code to make it more clear.
hw/block/m25p80.c | 103 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 12 deletions(-)
hw/block/m25p80.c +91 -12
@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_option_flags { +ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +HAS_SR_TB =BIT (3 ), +HAS_SR_BP3_BIT6 =BIT (4 ), +}; + typedef struct FlashPartInfo { const char *part_name; /*
@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | HAS_SR_BP3_BIT6 | HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },
@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;
@@ -626,11 +630,36 @@ 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 ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data); }
@@ -728,6 +757,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :
@@ -1213,6 +1251,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;
@@ -1553,6 +1600,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }
@@ -1639,6 +1691,32 @@ static const VMStateDescription vmstate_m25p80_write_protect = { } }; +static bool m25p80_block_protect_needed (void *opaque) +{ +Flash *s = (Flash *)opaque; + +return s->block_protect0 || + s->block_protect1 || + s->block_protect2 || + s->block_protect3 || + s->top_bottom_bit; +} + +static const VMStateDescription vmstate_m25p80_block_protect = { + .name ="m25p80/block_protect" , + .version_id =1 , + .minimum_version_id =1 , + .needed = m25p80_block_protect_needed, + .fields = (VMStateField []) { +VMSTATE_BOOL (block_protect0, Flash), +VMSTATE_BOOL (block_protect1, Flash), +VMSTATE_BOOL (block_protect2, Flash), +VMSTATE_BOOL (block_protect3, Flash), +VMSTATE_BOOL (top_bottom_bit, Flash), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_m25p80 = { .name ="m25p80" , .version_id =0 ,
@@ -1671,6 +1749,7 @@ static const VMStateDescription vmstate_m25p80 = { &vmstate_m25p80_data_read_loop, &vmstate_m25p80_aai_enable, &vmstate_m25p80_write_protect, + &vmstate_m25p80_block_protect, NULL } };
Signature · 1 line
2.30.2
Re: [PATCH v2] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
Hi Iris
Quoted 1 line · Iris Chen
Signed-off-by Iris Chen <[email protected]>
A couple of suggestions below if you would like to go for a v3 but otherwise:
Thanks,
Best regards,
Francisco Iglesias
Quoted 30 lines
---
Addressing all comments.
In reponse to this comment:
"Something wrong will occur if all block_protect[0123] are zeroes", the code actually ignoresnum_protected_sectorswhenblock_protect_value= 0 which happens when block_protect[0123] are zeroes.You can refer to the bottom block in v1, where we only look at cases when
block_protect_value> 0 so it is actually handled. Regardless, since we were settingnum_protected_sectors
in either case, I have refactored the code to make it more clear.hw/block/m25p80.c | 103 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 12 deletions(-)hw/block/m25p80.c +0
-7@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set
hw/block/m25p80.c line 46
Quoted 10 lines
hw/block/m25p80.c +2
-4-*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_option_flags {
A suggestion is to rename to spi_flash_option_flags (for being more clear that it is flash option and not a SPI option).
Quoted 33 lines
hw/block/m25p80.c +14
-1+ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +HAS_SR_TB =BIT (3 ), + HAS_SR_BP3_BIT6 = BIT(4), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | HAS_SR_BP3_BIT6 | HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;
hw/block/m25p80.c void flash_write8(Flash *s, uint32_t addr, uint8_t data) · line 632
Quoted 3 lines
hw/block/m25p80.c +0
-0@@ -626,11 +630,36 @@ 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];
A cosmetic suggestion is to remove above blank line to keep the declarations together.
Quoted 122 lines
hw/block/m25p80.c +75
-0+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 ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data); }@@ -728,6 +757,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1251,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;@@ -1553,6 +1600,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }@@ -1639,6 +1691,32 @@ static const VMStateDescription vmstate_m25p80_write_protect = { } }; +static bool m25p80_block_protect_needed (void *opaque) +{ +Flash *s = (Flash *)opaque; + +return s->block_protect0 || + s->block_protect1 || + s->block_protect2 || + s->block_protect3 || + s->top_bottom_bit; +} + +static const VMStateDescription vmstate_m25p80_block_protect = { + .name ="m25p80/block_protect" , + .version_id =1 , + .minimum_version_id =1 , + .needed = m25p80_block_protect_needed, + .fields = (VMStateField []) { +VMSTATE_BOOL (block_protect0, Flash), +VMSTATE_BOOL (block_protect1, Flash), +VMSTATE_BOOL (block_protect2, Flash), +VMSTATE_BOOL (block_protect3, Flash), +VMSTATE_BOOL (top_bottom_bit, Flash), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_m25p80 = { .name ="m25p80" , .version_id =0 ,@@ -1671,6 +1749,7 @@ static const VMStateDescription vmstate_m25p80 = { &vmstate_m25p80_data_read_loop, &vmstate_m25p80_aai_enable, &vmstate_m25p80_write_protect, + &vmstate_m25p80_block_protect, NULL } };Signature · 1 line
2.30.2
PATCH v3 1/1 hw: m25p80: Add Block Protect and Top Bottom bits for write protect
In reply to Iris Chen---
Cosmetic suggestions addressed.
hw/block/m25p80.c | 102 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 12 deletions(-)
hw/block/m25p80.c +90 -12
@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_flash_option_flags { +ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +HAS_SR_TB =BIT (3 ), +HAS_SR_BP3_BIT6 =BIT (4 ), +}; + typedef struct FlashPartInfo { const char *part_name; /*
@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | HAS_SR_BP3_BIT6 | HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },
@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;
@@ -625,12 +629,36 @@ 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 ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data); }
@@ -728,6 +756,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :
@@ -1213,6 +1250,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;
@@ -1553,6 +1599,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }
@@ -1639,6 +1690,32 @@ static const VMStateDescription vmstate_m25p80_write_protect = { } }; +static bool m25p80_block_protect_needed (void *opaque) +{ +Flash *s = (Flash *)opaque; + +return s->block_protect0 || + s->block_protect1 || + s->block_protect2 || + s->block_protect3 || + s->top_bottom_bit; +} + +static const VMStateDescription vmstate_m25p80_block_protect = { + .name ="m25p80/block_protect" , + .version_id =1 , + .minimum_version_id =1 , + .needed = m25p80_block_protect_needed, + .fields = (VMStateField []) { +VMSTATE_BOOL (block_protect0, Flash), +VMSTATE_BOOL (block_protect1, Flash), +VMSTATE_BOOL (block_protect2, Flash), +VMSTATE_BOOL (block_protect3, Flash), +VMSTATE_BOOL (top_bottom_bit, Flash), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_m25p80 = { .name ="m25p80" , .version_id =0 ,
@@ -1671,6 +1748,7 @@ static const VMStateDescription vmstate_m25p80 = { &vmstate_m25p80_data_read_loop, &vmstate_m25p80_aai_enable, &vmstate_m25p80_write_protect, + &vmstate_m25p80_block_protect, NULL } };
Signature · 1 line
2.30.2
Re: [PATCH v3] hw: m25p80: Add Block Protect and Top Bottom bits for write protect
Quoted 1 line · Iris Chen
Signed-off-by Iris Chen <[email protected]>
Quoted 189 lines
---
Cosmetic suggestions addressed.hw/block/m25p80.c | 102 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 12 deletions(-)hw/block/m25p80.c +90
-12@@ -38,21 +38,19 @@#include "trace.h" #include "qom/object.h" -/* Fields for FlashPartInfo->flags */ - -/* erase capabilities */ -#define ER_4K 1 -#define ER_32K 2 -/* set to allow the page program command to write 0s back to 1. Useful for -* modelling EEPROM with SPI flash command set -*/ -#define EEPROM 0x100 -/* 16 MiB max in 3 byte address mode */ #define MAX_3BYTES_SIZE 0x1000000 -#define SPI_NOR_MAX_ID_LEN 6 +/* Fields for FlashPartInfo->flags */ +enum spi_flash_option_flags { +ER_4K =BIT (0 ), +ER_32K =BIT (1 ), +EEPROM =BIT (2 ), +HAS_SR_TB =BIT (3 ), +HAS_SR_BP3_BIT6 =BIT (4 ), +}; + typedef struct FlashPartInfo { const char *part_name; /*@@ -253,7 +251,8 @@ static const FlashPartInfo known_devices[] = { { INFO("n25q512a11", 0x20bb20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512a13", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q128", 0x20ba18, 0, 64 << 10, 256, 0) }, - { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, ER_4K) }, + { INFO("n25q256a", 0x20ba19, 0, 64 << 10, 512, + ER_4K | HAS_SR_BP3_BIT6 | HAS_SR_TB) }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) },@@ -480,6 +479,11 @@ struct Flash {bool reset_enable;bool quad_enable;bool aai_enable; +bool block_protect0; +bool block_protect1; +bool block_protect2; +bool block_protect3; +bool top_bottom_bit;bool status_register_write_disabled;uint8_t ear;@@ -625,12 +629,36 @@ 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 ((prev ^ data) & data) {trace_m25p80_programming_zero_to_one (s, addr, prev, data); }@@ -728,6 +756,15 @@ static void complete_collecting_data(Flash *s)break ;case WRSR : s->status_register_write_disabled =extract32 (s->data[0 ],7 ,1 ); + s->block_protect0 =extract32 (s->data[0 ],2 ,1 ); + s->block_protect1 =extract32 (s->data[0 ],3 ,1 ); + s->block_protect2 =extract32 (s->data[0 ],4 ,1 ); +if (s->pi->flags &HAS_SR_TB ) { + s->top_bottom_bit =extract32 (s->data[0 ],5 ,1 ); + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->block_protect3 =extract32 (s->data[0 ],6 ,1 ); + }switch (get_man (s)) {case MAN_SPANSION :@@ -1213,6 +1250,15 @@ static void decode_new_cmd(Flash *s, uint32_t value)case RDSR : s->data[0 ] = (!!s->write_enable) <<1 ; s->data[0 ] |= (!!s->status_register_write_disabled) <<7 ; + s->data[0 ] |= (!!s->block_protect0) <<2 ; + s->data[0 ] |= (!!s->block_protect1) <<3 ; + s->data[0 ] |= (!!s->block_protect2) <<4 ; +if (s->pi->flags &HAS_SR_TB ) { + s->data[0 ] |= (!!s->top_bottom_bit) <<5 ; + } +if (s->pi->flags &HAS_SR_BP3_BIT6 ) { + s->data[0 ] |= (!!s->block_protect3) <<6 ; + }if (get_man (s) ==MAN_MACRONIX ||get_man (s) ==MAN_ISSI ) { s->data[0 ] |= (!!s->quad_enable) <<6 ;@@ -1553,6 +1599,11 @@ static void m25p80_reset(DeviceState *d) s->wp_level = true; s->status_register_write_disabled = false; + s->block_protect0 = false; + s->block_protect1 = false; + s->block_protect2 = false; + s->block_protect3 = false; + s->top_bottom_bit = false;reset_memory (s); }@@ -1639,6 +1690,32 @@ static const VMStateDescription vmstate_m25p80_write_protect = { } }; +static bool m25p80_block_protect_needed (void *opaque) +{ +Flash *s = (Flash *)opaque; + +return s->block_protect0 || + s->block_protect1 || + s->block_protect2 || + s->block_protect3 || + s->top_bottom_bit; +} + +static const VMStateDescription vmstate_m25p80_block_protect = { + .name ="m25p80/block_protect" , + .version_id =1 , + .minimum_version_id =1 , + .needed = m25p80_block_protect_needed, + .fields = (VMStateField []) { +VMSTATE_BOOL (block_protect0, Flash), +VMSTATE_BOOL (block_protect1, Flash), +VMSTATE_BOOL (block_protect2, Flash), +VMSTATE_BOOL (block_protect3, Flash), +VMSTATE_BOOL (top_bottom_bit, Flash), +VMSTATE_END_OF_LIST () + } +}; +static const VMStateDescription vmstate_m25p80 = { .name ="m25p80" , .version_id =0 ,@@ -1671,6 +1748,7 @@ static const VMStateDescription vmstate_m25p80 = { &vmstate_m25p80_data_read_loop, &vmstate_m25p80_aai_enable, &vmstate_m25p80_write_protect, + &vmstate_m25p80_block_protect, NULL } };Signature · 1 line
2.30.2