Skip to contentSkip to conversation

sf: Add support for status register protect

PATCH New George McCollister 3 participants 13 messages Oct 10, 2016 – Oct 28, 2016 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

cover sf: Add support for status register protect

Many SPI NOR flash devices support status register protection through one or two status register protection bits. Protection of the status register is essential in defending the device from rogue software which may attempt to modify block protection bits in order to make malicious modifications to the data stored in flash. This patch series adds status register protect support for STMICRO, SST, Winbond and Spansion devices and also implements an sf sub-command to set the used protection method.

George McCollister (5):
sf: Add status register protection mechanism
sf: Add status register protect for STMICRO, SST
sf: Use stm_lock/unlock for Spansion and Winbond
sf: Add status register protect for Winbond
sf: Add sr-protect sub-command

 cmd/sf.c                      |  28 ++++++++++++
 drivers/mtd/spi/sf_internal.h |   2 +
 drivers/mtd/spi/spi_flash.c   | 103 +++++++++++++++++++++++++++++++++++++++++-
 include/spi_flash.h           |  17 +++++++
 4 files changed, 149 insertions(+), 1 deletion(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/5 sf: Add status register protection mechanism

Many SPI NOR flash devices support status register protection through one or two status register protection bits. Status register protection enables protection of block protect and other bits from manipulation.

So far, four different status register protection methods have been observed:

Software - Writes to the status register are blocked until
a write enable bit is set by software.
Hardware - Writes to the status register are blocked while
a pin is in a certain state.
Power - Writes to the status register are blocked until
the next power-down.
One Time Program - Writes to the status register are permanently
blocked.

---
 include/spi_flash.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/spi_flash.h +17 -0
index be2fe3f..d1f63c7 100644
@@ -26,6 +26,13 @@
 # define CONFIG_SF_DEFAULT_BUS		0
 #endif
 
+enum srp_method {
+	SRP_SOFTWARE,
+	SRP_HARDWARE,
+	SRP_POWER,
+	SRP_OTP,
+};
+
 struct spi_slave;
 
 /**
@@ -89,6 +96,7 @@ struct spi_flash {
 	int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
 	int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
 	int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
+	int (*sr_protect)(struct spi_flash *flash, enum srp_method method);
 #ifndef CONFIG_DM_SPI_FLASH
 	/*
 	 * These are not strictly needed for driver model, but keep them here
@@ -239,4 +247,13 @@ static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
 		return flash->flash_unlock(flash, ofs, len);
 }
 
+static inline int spi_flash_sr_protect(struct spi_flash *flash,
+		enum srp_method method)
+{
+	if (!flash->sr_protect)
+		return -EOPNOTSUPP;
+
+	return flash->sr_protect(flash, method);
+}
+
 #endif /* _SPI_FLASH_H_ */
Signature · 1 line
2.9.3
#3 lore raw

PATCH 2/5 sf: Add status register protect for STMICRO, SST

In reply to George McCollister

STMICRO parts such as M25PX64 and SST parts such as SST26VF032B support a single status register protect bit. When set this bit cause writes to the status register to be blocked when the write protect signal is low.

Implement sr_protect for these devices.

---
 drivers/mtd/spi/sf_internal.h |  1 +
 drivers/mtd/spi/spi_flash.c   | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
drivers/mtd/spi/sf_internal.h +1 -0
index cde4cfb..1b576e8 100644
@@ -81,6 +81,7 @@ enum spi_nor_option_flags {
 #define SR_BP0				BIT(2)  /* Block protect 0 */
 #define SR_BP1				BIT(3)  /* Block protect 1 */
 #define SR_BP2				BIT(4)  /* Block protect 2 */
+#define SR_SRP0				BIT(7)	/* Status register protect 0 */
 
 /* Flash timeout values */
 #define SPI_FLASH_PROG_TIMEOUT		(2 * CONFIG_SYS_HZ)
drivers/mtd/spi/spi_flash.c +35 -0
index 7f6e9ae..0e33901 100644
@@ -837,6 +837,40 @@ int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
 
 	return 0;
 }
+
+/*
+ * Set status register protection method for parts with one protection bit
+ *
+ * Returns negative on errors, 0 on success.
+ */
+int stm_sr_protect(struct spi_flash *flash, enum srp_method method)
+{
+	uint8_t status_old, status_new;
+	u8 mask = SR_SRP0;
+	u8 val;
+	int ret;
+
+	ret = read_sr(flash, &status_old);
+	if (ret < 0)
+		return ret;
+
+	switch (method) {
+	case SRP_SOFTWARE:
+		val = 0;
+		break;
+	case SRP_HARDWARE:
+		val = SR_SRP0;
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	status_new = (status_old & ~mask) | val;
+
+	write_sr(flash, status_new);
+
+	return 0;
+}
 #endif
 
 
@@ -1125,6 +1159,7 @@ int spi_flash_scan(struct spi_flash *flash)
 		flash->flash_lock = stm_lock;
 		flash->flash_unlock = stm_unlock;
 		flash->flash_is_locked = stm_is_locked;
+		flash->sr_protect = stm_sr_protect;
 #endif
 		break;
 	default:
Signature · 1 line
2.9.3
#5 lore raw

PATCH 3/5 sf: Use stm_lock/unlock for Spansion and Winbond

In reply to George McCollister

Spansion parts such as S25FL128S and Winbond parts such as W25Q64FV support the block protection mechanism implemented as stm_lock() and stm_unlock(). Use stm_lock() and stm_unlock() for Spansion and Winbond devices.

---
 drivers/mtd/spi/spi_flash.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/mtd/spi/spi_flash.c +10 -1
index 0e33901..392146b 100644
@@ -664,7 +664,8 @@ int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
 }
 #endif
 
-#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
+#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST) || \
+	defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
 static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
 				 u64 *len)
 {
@@ -1160,8 +1161,16 @@ int spi_flash_scan(struct spi_flash *flash)
 		flash->flash_unlock = stm_unlock;
 		flash->flash_is_locked = stm_is_locked;
 		flash->sr_protect = stm_sr_protect;
+		break;
 #endif
+#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
+	case SPI_FLASH_CFI_MFR_SPANSION:
+	case SPI_FLASH_CFI_MFR_WINBOND:
+		flash->flash_lock = stm_lock;
+		flash->flash_unlock = stm_unlock;
+		flash->flash_is_locked = stm_is_locked;
 		break;
+#endif
 	default:
 		debug("SF: Lock ops not supported for %02x flash\n", idcode[0]);
 	}
Signature · 1 line
2.9.3
#7 lore raw

PATCH 4/5 sf: Add status register protect for Winbond

In reply to George McCollister

Winbond parts such as W25Q64FV and Spansion parts such as S25FL128S support two status register protection bits. Implement sr_protect for Winbond and Spansion devices.

-------------------------------------------------------

 | SRP1 | SRP0 | Method   | Description                |
 -------------------------|-----------------------------
 | 0    | 0    | Software | Blocked until write enable |
 | 0    | 1    | Hardware | Blocked if WP pin is low   |
 | 1    | 0    | Power    | Blocked until power down   |
 | 1    | 1    | OTP      | Blocked permanently        |

-------------------------------------------------------

Not all devices support OTP.

---
 drivers/mtd/spi/sf_internal.h |  1 +
 drivers/mtd/spi/spi_flash.c   | 57 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
drivers/mtd/spi/sf_internal.h +1 -0
index 1b576e8..7a97fc4 100644
@@ -82,6 +82,7 @@ enum spi_nor_option_flags {
 #define SR_BP1				BIT(3)  /* Block protect 1 */
 #define SR_BP2				BIT(4)  /* Block protect 2 */
 #define SR_SRP0				BIT(7)	/* Status register protect 0 */
+#define SR_SRP1				BIT(0)	/* Status register protect 1 */
 
 /* Flash timeout values */
 #define SPI_FLASH_PROG_TIMEOUT		(2 * CONFIG_SYS_HZ)
drivers/mtd/spi/spi_flash.c +57 -0

69 lines not on this page · raw message

Signature · 1 line
2.9.3
#9 lore raw

PATCH 5/5 sf: Add sr-protect sub-command

In reply to George McCollister

Add an sf sub-command named sr-protect which allows the status register protection method to be changed.

Valid settings are software, hardware, power and otp.

---
 cmd/sf.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
cmd/sf.c +28 -0
index 65b117f..1a42bf6 100644
@@ -380,6 +380,30 @@ static int do_spi_protect(int argc, char * const argv[])
 	return ret == 0 ? 0 : 1;
 }
 
+static int do_spi_sr_protect(int argc, char * const argv[])
+{
+	int ret;
+	enum srp_method method;
+
+	if (argc != 2)
+		return -1;
+
+	if (strcmp(argv[1], "software") == 0)
+		method = SRP_SOFTWARE;
+	else if (strcmp(argv[1], "hardware") == 0)
+		method = SRP_HARDWARE;
+	else if (strcmp(argv[1], "power") == 0)
+		method = SRP_POWER;
+	else if (strcmp(argv[1], "otp") == 0)
+		method = SRP_OTP;
+	else
+		return -1;  /* Unknown parameter */
+
+	ret = spi_flash_sr_protect(flash, method);
+
+	return ret == 0 ? 0 : 1;
+}
+
 #ifdef CONFIG_CMD_SF_TEST
 enum {
 	STAGE_ERASE,
@@ -574,6 +598,8 @@ static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc,
 		ret = do_spi_flash_erase(argc, argv);
 	else if (strcmp(cmd, "protect") == 0)
 		ret = do_spi_protect(argc, argv);
+	else if (strcmp(cmd, "sr-protect") == 0)
+		ret = do_spi_sr_protect(argc, argv);
 #ifdef CONFIG_CMD_SF_TEST
 	else if (!strcmp(cmd, "test"))
 		ret = do_spi_flash_test(argc, argv);
@@ -615,5 +641,7 @@ U_BOOT_CMD(
 	"					  or to start of mtd `partition'\n"
 	"sf protect lock/unlock sector len	- protect/unprotect 'len' bytes starting\n"
 	"					  at address 'sector'\n"
+	"sf sr-protect method			- set status register protect method to\n"
+	"					  software, hardware, power or otp\n"
 	SF_TEST_HELP
 );
Signature · 1 line
2.9.3
#2 lore raw

PATCH 1/5 sf: Add status register protection mechanism

In reply to George McCollister
Quoted 16 lines · George McCollister

Many SPI NOR flash devices support status register protection through one or two status register protection bits. Status register protection enables protection of block protect and other bits from manipulation.

So far, four different status register protection methods have been observed:

Software - Writes to the status register are blocked until
a write enable bit is set by software.
Hardware - Writes to the status register are blocked while
a pin is in a certain state.
Power - Writes to the status register are blocked until
the next power-down.
One Time Program - Writes to the status register are permanently
blocked.

Quoted 5 lines
---
 include/spi_flash.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
#4 lore raw

PATCH 2/5 sf: Add status register protect for STMICRO, SST

In reply to George McCollister
Quoted 4 lines · George McCollister

STMICRO parts such as M25PX64 and SST parts such as SST26VF032B support a single status register protect bit. When set this bit cause writes to the status register to be blocked when the write protect signal is low.

Quoted 8 lines

Implement sr_protect for these devices.

---
 drivers/mtd/spi/sf_internal.h |  1 +
 drivers/mtd/spi/spi_flash.c   | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
#6 lore raw

PATCH 3/5 sf: Use stm_lock/unlock for Spansion and Winbond

In reply to George McCollister
Quoted 10 lines · George McCollister

Spansion parts such as S25FL128S and Winbond parts such as W25Q64FV support the block protection mechanism implemented as stm_lock() and stm_unlock(). Use stm_lock() and stm_unlock() for Spansion and Winbond devices.

---
 drivers/mtd/spi/spi_flash.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
#8 lore raw

PATCH 4/5 sf: Add status register protect for Winbond

In reply to George McCollister
Quoted 13 lines · George McCollister

Winbond parts such as W25Q64FV and Spansion parts such as S25FL128S support two status register protection bits. Implement sr_protect for Winbond and Spansion devices.

-------------------------------------------------------

 | SRP1 | SRP0 | Method   | Description                |
 -------------------------|-----------------------------
 | 0    | 0    | Software | Blocked until write enable |
 | 0    | 1    | Hardware | Blocked if WP pin is low   |
 | 1    | 0    | Power    | Blocked until power down   |
 | 1    | 1    | OTP      | Blocked permanently        |

-------------------------------------------------------

Quoted 8 lines

Not all devices support OTP.

---
 drivers/mtd/spi/sf_internal.h |  1 +
 drivers/mtd/spi/spi_flash.c   | 57 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
#11 lore raw

cover sf: Add support for status register protect

In reply to George McCollister
Quoted 16 lines · George McCollister

Many SPI NOR flash devices support status register protection through one or two status register protection bits. Protection of the status register is essential in defending the device from rogue software which may attempt to modify block protection bits in order to make malicious modifications to the data stored in flash. This patch series adds status register protect support for STMICRO, SST, Winbond and Spansion devices and also implements an sf sub-command to set the used protection method.

George McCollister (5):
sf: Add status register protection mechanism
sf: Add status register protect for STMICRO, SST
sf: Use stm_lock/unlock for Spansion and Winbond
sf: Add status register protect for Winbond
sf: Add sr-protect sub-command

Quoted 9 lines
 cmd/sf.c                      |  28 ++++++++++++
 drivers/mtd/spi/sf_internal.h |   2 +
 drivers/mtd/spi/spi_flash.c   | 103 +++++++++++++++++++++++++++++++++++++++++-
 include/spi_flash.h           |  17 +++++++
 4 files changed, 149 insertions(+), 1 deletion(-)

--
2.9.3

Jagan,

I think get_maintainer.pl may have given me an old email address for you and you may have missed it on the mailing list. Either way if you get a chance I'd like hear what you think. I'm not sure which tree/branch you're working with (u-boot-spi
master?) but if you need it rebased just let me know.

Thanks,
George

#12 lore raw

cover sf: Add support for status register protect

Quoted 28 lines · George McCollister
Quoted 16 lines · George McCollister

Many SPI NOR flash devices support status register protection through one or two status register protection bits. Protection of the status register is essential in defending the device from rogue software which may attempt to modify block protection bits in order to make malicious modifications to the data stored in flash. This patch series adds status register protect support for STMICRO, SST, Winbond and Spansion devices and also implements an sf sub-command to set the used protection method.

George McCollister (5):
sf: Add status register protection mechanism
sf: Add status register protect for STMICRO, SST
sf: Use stm_lock/unlock for Spansion and Winbond
sf: Add status register protect for Winbond
sf: Add sr-protect sub-command

Quoted 9 lines
 cmd/sf.c                      |  28 ++++++++++++
 drivers/mtd/spi/sf_internal.h |   2 +
 drivers/mtd/spi/spi_flash.c   | 103 +++++++++++++++++++++++++++++++++++++++++-
 include/spi_flash.h           |  17 +++++++
 4 files changed, 149 insertions(+), 1 deletion(-)

--
2.9.3

Quoted 7 lines

Jagan,

I think get_maintainer.pl may have given me an old email address for you and you may have missed it on the mailing list. Either way if you get a chance I'd like hear what you think. I'm not sure which tree/branch you're working with (u-boot-spi
master?) but if you need it rebased just let me know.

But this still active. about the patches please wait for some time will get back to you.

thanks!

Signature · 4 lines
Jagan Teki
Free Software Engineer | www.openedev.com
U-Boot, Linux | Upstream Maintainer
Hyderabad, India.