]> www.infradead.org Git - mtd-utils.git/commitdiff
mtd-utils: Add flash_otp_erase
authorMichael Walle <michael@walle.cc>
Tue, 16 Mar 2021 10:40:15 +0000 (11:40 +0100)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Sat, 12 Jun 2021 15:16:33 +0000 (17:16 +0200)
On some SPI NOR flashes you can actually erase the OTP region until its
fully locked. Add a small utility for that.

Signed-off-by: Michael Walle <michael@walle.cc>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
.gitignore
include/mtd/mtd-abi.h
misc-utils/Makemodule.am
misc-utils/flash_otp_erase.c [new file with mode: 0644]

index 4c04c1aa2ff9e398db25fdbd356ec3c124021fcb..1644ef09e5ad5eae7074b1c3f69a6698c9d8501b 100644 (file)
@@ -24,6 +24,7 @@ fectest
 flash_erase
 flash_lock
 flash_otp_dump
+flash_otp_erase
 flash_otp_info
 flash_otp_lock
 flash_otp_write
index bcd749692f51015d86de1dd8e74a259d08225519..a54c3862163241edf2581a20567705db06a21dff 100644 (file)
@@ -201,6 +201,8 @@ struct otp_info {
  * modes (see "struct mtd_write_req")
  */
 #define MEMWRITE               _IOWR('M', 24, struct mtd_write_req)
+/* Erase a given range of user data (must be in mode %MTD_FILE_MODE_OTP_USER) */
+#define OTPERASE               _IOW('M', 25, struct otp_info)
 
 /*
  * Obsolete legacy interface. Keep it in order not to break userspace
index b15c1194afffe6ae199795e4843a615864e19a38..bc69b1c591954c921482e0b832a77d87ef1c99a7 100644 (file)
@@ -32,6 +32,8 @@ flash_otp_dump_SOURCES = misc-utils/flash_otp_dump.c
 
 flash_otp_lock_SOURCES = misc-utils/flash_otp_lock.c
 
+flash_otp_erase_SOURCES = misc-utils/flash_otp_erase.c
+
 flash_otp_write_SOURCES = misc-utils/flash_otp_write.c
 
 flashcp_SOURCES = misc-utils/flashcp.c
@@ -43,7 +45,7 @@ sbin_PROGRAMS += \
        ftl_format doc_loadbios ftl_check mtd_debug docfdisk \
        serve_image recv_image fectest flash_erase flash_lock \
        flash_unlock flash_otp_info flash_otp_dump flash_otp_lock \
-       flash_otp_write flashcp mtdpart
+       flash_otp_erase flash_otp_write flashcp mtdpart
 
 MISC_SH = \
        misc-utils/flash_eraseall
diff --git a/misc-utils/flash_otp_erase.c b/misc-utils/flash_otp_erase.c
new file mode 100644 (file)
index 0000000..771e230
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * flash_otp_erase.c -- erase area of One-Time-Program data
+ */
+
+#define PROGRAM_NAME "flash_otp_erase"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+
+#include <mtd/mtd-user.h>
+#include "common.h"
+
+int main(int argc,char *argv[])
+{
+       int fd, val, ret, offset, size;
+       struct otp_info info;
+       char *p;
+
+       if (argc != 5 || strcmp(argv[1], "-u")) {
+               fprintf(stderr, "Usage: %s -u <device> <offset> <size>\n", PROGRAM_NAME);
+               fprintf(stderr, "offset and size must match on OTP region boundaries\n");
+               return EINVAL;
+       }
+
+       fd = open(argv[2], O_WRONLY);
+       if (fd < 0) {
+               perror(argv[2]);
+               return errno;
+       }
+
+       val = MTD_OTP_USER;
+       ret = ioctl(fd, OTPSELECT, &val);
+       if (ret < 0) {
+               perror("OTPSELECT");
+               return errno;
+       }
+
+       offset = strtoul(argv[3], &p, 0);
+       if (argv[3][0] == 0 || *p != 0) {
+               fprintf(stderr, "%s: bad offset value\n", PROGRAM_NAME);
+               return ERANGE;
+       }
+
+       size = strtoul(argv[4], &p, 0);
+       if (argv[4][0] == 0 || *p != 0) {
+               fprintf(stderr, "%s: bad size value\n", PROGRAM_NAME);
+               return ERANGE;
+       }
+
+       info.start = offset;
+       info.length = size;
+       ret = ioctl(fd, OTPERASE, &info);
+       if (ret < 0) {
+               perror("OTPERASE");
+               return errno;
+       }
+
+       return 0;
+}