diff mbox series

[v6,2/2] sf: Query write-protection status before operating the flash

Message ID 5a760b8f30a392e82a2ea0cc4d3a158ea5880cfe.1646229716.git.jan.kiszka@siemens.com
State Accepted
Commit f3a56dda880af44dac87b1a60c44ebb7c079c329
Delegated to: Jagannadha Sutradharudu Teki
Headers show
Series sf: Check protection before writing/erasing flash | expand

Commit Message

Jan Kiszka March 2, 2022, 2:01 p.m. UTC
From: Jan Kiszka <jan.kiszka@siemens.com>

Do not suggest successful operation if a flash area to be changed is
actually locked, thus will not execute the request. Rather report an
error and bail out. That's way more user-friendly than asking them to
manually check for this case.

Derived from original patch by Chao Zeng.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 cmd/sf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff mbox series

Patch

diff --git a/cmd/sf.c b/cmd/sf.c
index 8bdebd9fd8f..c97d0e28bb8 100644
--- a/cmd/sf.c
+++ b/cmd/sf.c
@@ -287,6 +287,12 @@  static int do_spi_flash_read_write(int argc, char *const argv[])
 		return 1;
 	}
 
+	if (strncmp(argv[0], "read", 4) != 0 && flash->flash_is_unlocked &&
+	    !flash->flash_is_unlocked(flash, offset, len)) {
+		printf("ERROR: flash area is locked\n");
+		return 1;
+	}
+
 	buf = map_physmem(addr, len, MAP_WRBACK);
 	if (!buf && addr) {
 		puts("Failed to map physical memory\n");
@@ -343,6 +349,12 @@  static int do_spi_flash_erase(int argc, char *const argv[])
 		return 1;
 	}
 
+	if (flash->flash_is_unlocked &&
+	    !flash->flash_is_unlocked(flash, offset, len)) {
+		printf("ERROR: flash area is locked\n");
+		return 1;
+	}
+
 	ret = spi_flash_erase(flash, offset, size);
 	printf("SF: %zu bytes @ %#x Erased: ", (size_t)size, (u32)offset);
 	if (ret)