aboutsummaryrefslogtreecommitdiff
path: root/flash_otp_write.c
blob: 71d3168e7d8616bc2fb5c513759780d6bd5a49e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * flash_otp_write.c -- write One-Time-Program data
*/

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include <mtd/mtd-user.h>

int main(int argc,char *argv[])
{
	int fd, val, ret, size, wrote, len;
	mtd_info_t mtdInfo;
	off_t offset;
	char *p, buf[2048];

	if (argc != 4 || strcmp(argv[1], "-u")) {
		fprintf(stderr, "Usage: %s -u <device> <offset>\n", argv[0]);
		fprintf(stderr, "the raw data to write should be provided on stdin\n");
		fprintf(stderr, "CAUTION! ONCE SET TO 0, OTP DATA BITS CAN'T BE ERASED!\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;
	}

	if (ioctl(fd, MEMGETINFO, &mtdInfo)) {
		perror("MEMGETINFO");
		return errno;
	}

	offset = strtoul(argv[3], &p, 0);
	if (argv[3][0] == 0 || *p != 0) {
		fprintf(stderr, "%s: bad offset value\n", argv[0]);
		return ERANGE;
	}

	if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
		perror("lseek()");
		return errno;
	}

	printf("Writing OTP user data on %s at offset 0x%lx\n", argv[2], offset);

	if (mtdInfo.type == MTD_NANDFLASH)
		len = mtdInfo.oobblock;
	else
		len = 256;

	wrote = 0;
	while ((size = read(0, buf, len))) {
		if (size < 0) {
			perror("read()");
			return errno;
		}
		p = buf;
		while (size > 0) {
			if (mtdInfo.type == MTD_NANDFLASH) {
				/* Fill remain buffers with 0xff */
				memset(buf + size, 0xff, mtdInfo.oobblock - size);
				size = mtdInfo.oobblock;
			}
			ret = write(fd, p, size);
			if (ret < 0) {
				perror("write()");
				return errno;
			}
			if (ret == 0) {
				printf("write() returned 0 after writing %d bytes\n", wrote);
				return 0;
			}
			p += ret;
			wrote += ret;
			size -= ret;
		}
	}

	printf("Wrote %d bytes of OTP user data\n", wrote);
	return 0;
}