mtd/util flash_otp_lock.c,NONE,1.1 flash_otp_write.c,NONE,1.1
Nicolas Pitre
nico at infradead.org
Thu Feb 17 15:38:39 EST 2005
Update of /home/cvs/mtd/util
In directory phoenix.infradead.org:/tmp/cvs-serv8751
Added Files:
flash_otp_lock.c flash_otp_write.c
Log Message:
More OTP tools: one to write OTP data and another to lock it.
NOTE: I'm _not_ adding them to the Makefile on purpose.
This way it should be one more obstacle to misusing them.
--- NEW FILE flash_otp_lock.c ---
/*
* flash_otp_lock.c -- lock area of One-Time-Program data
*/
#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>
int main(int argc,char *argv[])
{
int fd, val, ret, offset, size;
char *p, buf[8];
if (argc != 5 || strcmp(argv[1], "-u")) {
fprintf(stderr, "Usage: %s -u <device> <offset> <size>\n", argv[0]);
fprintf(stderr, "offset and size must match on OTP region boundaries\n");
fprintf(stderr, "CAUTION! ONCE LOCKED, OTP REGIONS CAN'T BE UNLOCKED!\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", argv[0]);
return ERANGE;
}
size = strtoul(argv[4], &p, 0);
if (argv[4][0] == 0 || *p != 0) {
fprintf(stderr, "%s: bad size value\n", argv[0]);
return ERANGE;
}
printf("About to lock OTP user data on %s from 0x%x to 0x%x\n",
argv[2], offset, offset + size);
printf("Are you sure (yes|no)? ");
if (fgets(buf, sizeof(buf), stdin) && strcmp(buf, "yes\n") == 0) {
struct otp_info info;
info.start = offset;
info.length = size;
ret = ioctl(fd, OTPLOCK, &info);
if (ret < 0) {
perror("OTPLOCK");
return errno;
}
printf("Done.\n");
} else {
printf("Aborted\n");
}
return 0;
}
--- NEW FILE flash_otp_write.c ---
/*
* 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;
off_t offset;
char *p, buf[256];
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;
}
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);
wrote = 0;
while ((size = read(0, buf, sizeof(buf)))) {
if (size < 0) {
perror("read()");
return errno;
}
p = buf;
while (size > 0) {
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;
}
More information about the linux-mtd-cvs
mailing list