xref: /freebsd/tools/tools/cfi/cfi.c (revision 81ad6265)
1 /*-
2  * Copyright (c) 2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * cfi [-f device] op
34  * (default device is /dev/cfi0).
35  */
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/cfictl.h>
40 
41 #include <err.h>
42 #include <getopt.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 const char *progname;
49 const char *dvname;
50 
51 static void
52 usage(void)
53 {
54 	fprintf(stderr, "usage: %s [-f device] op...\n", progname);
55 	fprintf(stderr, "where op's are:\n");
56 	fprintf(stderr, "fact\t\tread factory PR segment\n");
57 	fprintf(stderr, "oem\t\tread OEM segment\n");
58 	fprintf(stderr, "woem value\twrite OEM segment\n");
59 	fprintf(stderr, "plr\t\tread PLR\n");
60 	fprintf(stderr, "wplr\t\twrite PLR\n");
61 	exit(-1);
62 }
63 
64 static int
65 getfd(int mode)
66 {
67 	int fd = open(dvname, mode, 0);
68 	if (fd < 0)
69 		err(-1, "open");
70 	return fd;
71 }
72 
73 static uint64_t
74 getfactorypr(void)
75 {
76 	uint64_t v;
77 	int fd = getfd(O_RDONLY);
78 	if (ioctl(fd, CFIOCGFACTORYPR, &v) < 0)
79 		err(-1, "ioctl(CFIOCGFACTORYPR)");
80 	close(fd);
81 	return v;
82 }
83 
84 static uint64_t
85 getoempr(void)
86 {
87 	uint64_t v;
88 	int fd = getfd(O_RDONLY);
89 	if (ioctl(fd, CFIOCGOEMPR, &v) < 0)
90 		err(-1, "ioctl(CFIOCGOEMPR)");
91 	close(fd);
92 	return v;
93 }
94 
95 static void
96 setoempr(uint64_t v)
97 {
98 	int fd = getfd(O_WRONLY);
99 	if (ioctl(fd, CFIOCSOEMPR, &v) < 0)
100 		err(-1, "ioctl(CFIOCGOEMPR)");
101 	close(fd);
102 }
103 
104 static uint32_t
105 getplr(void)
106 {
107 	uint32_t plr;
108 	int fd = getfd(O_RDONLY);
109 	if (ioctl(fd, CFIOCGPLR, &plr) < 0)
110 		err(-1, "ioctl(CFIOCGPLR)");
111 	close(fd);
112 	return plr;
113 }
114 
115 static void
116 setplr(void)
117 {
118 	int fd = getfd(O_WRONLY);
119 	if (ioctl(fd, CFIOCSPLR, 0) < 0)
120 		err(-1, "ioctl(CFIOCPLR)");
121 	close(fd);
122 }
123 
124 int
125 main(int argc, char *argv[])
126 {
127 	dvname = getenv("CFI");
128 	if (dvname == NULL)
129 		dvname = "/dev/cfi0";
130 	progname = argv[0];
131 	if (argc > 1) {
132 		if (strcmp(argv[1], "-f") == 0) {
133 			if (argc < 2)
134 				errx(1, "missing device name for -f option");
135 			dvname = argv[2];
136 			argc -= 2, argv += 2;
137 		} else if (strcmp(argv[1], "-?") == 0)
138 			usage();
139 	}
140 	for (; argc > 1; argc--, argv++) {
141 		if (strcasecmp(argv[1], "fact") == 0) {
142 			printf("0x%llx\n", (unsigned long long) getfactorypr());
143 		} else if (strcasecmp(argv[1], "oem") == 0) {
144 			printf("0x%llx\n", (unsigned long long) getoempr());
145 		} else if (strcasecmp(argv[1], "woem") == 0) {
146 			if (argc < 2)
147 				errx(1, "missing value for woem");
148 			setoempr((uint64_t) strtoull(argv[2], NULL, 0));
149 			argc--, argv++;
150 		} else if (strcasecmp(argv[1], "plr") == 0) {
151 			printf("0x%x\n", getplr());
152 		} else if (strcasecmp(argv[1], "wplr") == 0) {
153 			setplr();
154 		} else
155 			usage();
156 	}
157 }
158