summaryrefslogtreecommitdiff
path: root/misc-utils/flash_otp_erase.c
diff options
context:
space:
mode:
authorMichael Walle <michael@walle.cc>2021-03-16 11:40:15 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-06-12 17:16:33 +0200
commit51d6820f49ca4a203cf30dd99bc83f25d569eb4b (patch)
treef7955ee2d8d27fb2b1325ad583ffce6f409a4fab /misc-utils/flash_otp_erase.c
parent2ca0bbf296d6f7ce51b8255347c7fd08afa33651 (diff)
mtd-utils: Add flash_otp_erase
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>
Diffstat (limited to 'misc-utils/flash_otp_erase.c')
-rw-r--r--misc-utils/flash_otp_erase.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/misc-utils/flash_otp_erase.c b/misc-utils/flash_otp_erase.c
new file mode 100644
index 0000000..771e230
--- /dev/null
+++ b/misc-utils/flash_otp_erase.c
@@ -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;
+}