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
30 /*
31 * cfi [-f device] op
32 * (default device is /dev/cfi0).
33 */
34 #include <sys/types.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/cfictl.h>
38
39 #include <err.h>
40 #include <getopt.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 const char *progname;
47 const char *dvname;
48
49 static void
usage(void)50 usage(void)
51 {
52 fprintf(stderr, "usage: %s [-f device] op...\n", progname);
53 fprintf(stderr, "where op's are:\n");
54 fprintf(stderr, "fact\t\tread factory PR segment\n");
55 fprintf(stderr, "oem\t\tread OEM segment\n");
56 fprintf(stderr, "woem value\twrite OEM segment\n");
57 fprintf(stderr, "plr\t\tread PLR\n");
58 fprintf(stderr, "wplr\t\twrite PLR\n");
59 exit(-1);
60 }
61
62 static int
getfd(int mode)63 getfd(int mode)
64 {
65 int fd = open(dvname, mode, 0);
66 if (fd < 0)
67 err(-1, "open");
68 return fd;
69 }
70
71 static uint64_t
getfactorypr(void)72 getfactorypr(void)
73 {
74 uint64_t v;
75 int fd = getfd(O_RDONLY);
76 if (ioctl(fd, CFIOCGFACTORYPR, &v) < 0)
77 err(-1, "ioctl(CFIOCGFACTORYPR)");
78 close(fd);
79 return v;
80 }
81
82 static uint64_t
getoempr(void)83 getoempr(void)
84 {
85 uint64_t v;
86 int fd = getfd(O_RDONLY);
87 if (ioctl(fd, CFIOCGOEMPR, &v) < 0)
88 err(-1, "ioctl(CFIOCGOEMPR)");
89 close(fd);
90 return v;
91 }
92
93 static void
setoempr(uint64_t v)94 setoempr(uint64_t v)
95 {
96 int fd = getfd(O_WRONLY);
97 if (ioctl(fd, CFIOCSOEMPR, &v) < 0)
98 err(-1, "ioctl(CFIOCGOEMPR)");
99 close(fd);
100 }
101
102 static uint32_t
getplr(void)103 getplr(void)
104 {
105 uint32_t plr;
106 int fd = getfd(O_RDONLY);
107 if (ioctl(fd, CFIOCGPLR, &plr) < 0)
108 err(-1, "ioctl(CFIOCGPLR)");
109 close(fd);
110 return plr;
111 }
112
113 static void
setplr(void)114 setplr(void)
115 {
116 int fd = getfd(O_WRONLY);
117 if (ioctl(fd, CFIOCSPLR, 0) < 0)
118 err(-1, "ioctl(CFIOCPLR)");
119 close(fd);
120 }
121
122 int
main(int argc,char * argv[])123 main(int argc, char *argv[])
124 {
125 dvname = getenv("CFI");
126 if (dvname == NULL)
127 dvname = "/dev/cfi0";
128 progname = argv[0];
129 if (argc > 1) {
130 if (strcmp(argv[1], "-f") == 0) {
131 if (argc < 2)
132 errx(1, "missing device name for -f option");
133 dvname = argv[2];
134 argc -= 2, argv += 2;
135 } else if (strcmp(argv[1], "-?") == 0)
136 usage();
137 }
138 for (; argc > 1; argc--, argv++) {
139 if (strcasecmp(argv[1], "fact") == 0) {
140 printf("0x%llx\n", (unsigned long long) getfactorypr());
141 } else if (strcasecmp(argv[1], "oem") == 0) {
142 printf("0x%llx\n", (unsigned long long) getoempr());
143 } else if (strcasecmp(argv[1], "woem") == 0) {
144 if (argc < 2)
145 errx(1, "missing value for woem");
146 setoempr((uint64_t) strtoull(argv[2], NULL, 0));
147 argc--, argv++;
148 } else if (strcasecmp(argv[1], "plr") == 0) {
149 printf("0x%x\n", getplr());
150 } else if (strcasecmp(argv[1], "wplr") == 0) {
151 setplr();
152 } else
153 usage();
154 }
155 }
156