xref: /netbsd-src/usr.sbin/eeprom/prephandlers.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: prephandlers.c,v 1.1 2007/03/01 16:49:48 garbled Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tim Rightnour.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <string.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include <machine/nvram.h>
50 
51 #include "defs.h"
52 
53 extern char *path_prepnvram;
54 extern int eval;
55 extern int verbose;
56 
57 static char err_str[BUFSIZE];
58 
59 static void prep_notsupp(struct extabent *, struct pnviocdesc *, char *);
60 
61 /*
62  * XXX
63  * This file needs a recalc checksum routine, and a routine to call a
64  * write back to nvram.  The prep NVRAM is stored in RAM after boot, to
65  * prevent horrific bitbanging on the NVRAM chip, we read and write into RAM
66  * until a save operation is called.  At that time we write the ram-based
67  * NVRAM back to the physical NVRAM.  This is a fairly expensive operation.
68  */
69 
70 
71 /*
72  * There are several known fields that I either don't know how to
73  * deal with or require special treatment.
74  */
75 static struct extabent prepextab[] = {
76 	{NULL, prep_notsupp},
77 };
78 #define BARF(str1, str2) {						\
79 	snprintf(err_str, sizeof err_str, "%s: %s", (str1), (str2));	\
80 	++eval;								\
81 	return (err_str);						\
82 };
83 
84 void
85 prep_action(char *keyword, char *arg)
86 {
87 	char *cp;
88 
89 	if ((cp = prep_handler(keyword, arg)) != NULL)
90 		warnx("%s", cp);
91 	return;
92 }
93 
94 char *
95 prep_handler(char *keyword, char *arg)
96 {
97 	struct pnviocdesc nvio;
98 	struct extabent *ex;
99 	char nvio_buf[BUFSIZE];
100 	int fd;
101 
102 	if ((fd = open(path_prepnvram, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
103 		BARF(path_prepnvram, strerror(errno));
104 
105 	/* Check to see if it's a special-case keyword. */
106 	for (ex = prepextab; ex->ex_keyword != NULL; ++ex)
107 		if (strcmp(ex->ex_keyword, keyword) == 0)
108 			break;
109 
110 	memset(&nvio_buf[0], 0, sizeof(nvio_buf));
111 	memset(&nvio, 0, sizeof(nvio));
112 	nvio.pnv_name = keyword;
113 	nvio.pnv_namelen = strlen(nvio.pnv_name);
114 
115 	if (arg) {
116 		if (verbose) {
117 			printf("old: ");
118 
119 			nvio.pnv_buf = &nvio_buf[0];
120 			nvio.pnv_buflen = sizeof(nvio_buf);
121 			if (ioctl(fd, PNVIOCGET, (char *) &nvio) < 0)
122 				BARF("PNVIOCGET", strerror(errno));
123 
124 			if (nvio.pnv_buflen <= 0) {
125 				printf("nothing available for %s\n", keyword);
126 				goto out;
127 			}
128 			if (ex->ex_keyword != NULL)
129 				(*ex->ex_handler) (ex, &nvio, NULL);
130 			else
131 				printf("%s\n", nvio.pnv_buf);
132 		}
133 out:
134 		if (ex->ex_keyword != NULL)
135 			(*ex->ex_handler) (ex, &nvio, arg);
136 		else {
137 			nvio.pnv_buf = arg;
138 			nvio.pnv_buflen = strlen(arg);
139 		}
140 
141 		if (ioctl(fd, PNVIOCSET, (char *) &nvio) < 0)
142 			BARF("invalid keyword", keyword);
143 
144 		if (verbose) {
145 			printf("new: ");
146 			if (ex->ex_keyword != NULL)
147 				(*ex->ex_handler) (ex, &nvio, NULL);
148 			else
149 				printf("%s\n", nvio.pnv_buf);
150 		}
151 	} else {
152 		nvio.pnv_buf = &nvio_buf[0];
153 		nvio.pnv_buflen = sizeof(nvio_buf);
154 		if (ioctl(fd, PNVIOCGET, (char *) &nvio) < 0)
155 			BARF("PNVIOCGET", strerror(errno));
156 
157 		if (nvio.pnv_buflen <= 0) {
158 			(void) snprintf(err_str, sizeof err_str,
159 			    "nothing available for %s", keyword);
160 			return (err_str);
161 		}
162 		if (ex->ex_keyword != NULL)
163 			(*ex->ex_handler) (ex, &nvio, NULL);
164 		else
165 			printf("%s=%s\n", keyword, nvio.pnv_buf);
166 	}
167 
168 	(void) close(fd);
169 	return (NULL);
170 }
171 /* ARGSUSED */
172 static void
173 prep_notsupp(struct extabent * exent, struct pnviocdesc * nviop, char *arg)
174 {
175 
176 	warnx("property `%s' not yet supported", exent->ex_keyword);
177 }
178 /*
179  * Derrived from op_dump().  This should dump the contents of the PReP
180  * NVRAM chip.
181  */
182 
183 void
184 prep_dump()
185 {
186 	struct pnviocdesc nvio1, nvio2;
187 	char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
188 	int fd, optnode, nrofvars;
189 
190 	if ((fd = open(path_prepnvram, O_RDONLY, 0640)) < 0)
191 		err(1, "open: %s", path_prepnvram);
192 
193 	memset(&nvio1, 0, sizeof(nvio1));
194 
195 	if (ioctl(fd, PNVIOCGETNUMGE, (char *) &nvio1) < 0)
196 		err(1, "PNVIOCGETNUMGE");
197 
198 	nrofvars = nvio1.pnv_num;
199 	if (nrofvars < 1)
200 		err(1, "PNVIOCGETNUMGE");
201 
202 	memset(&nvio1, 0, sizeof(nvio1));
203 	memset(buf1, 0, sizeof(buf1));
204 	memset(buf2, 0, sizeof(buf2));
205 	memset(buf3, 0, sizeof(buf3));
206 	memset(buf4, 0, sizeof(buf4));
207 	nvio1.pnv_name = NULL;
208 	nvio1.pnv_buf = buf2;
209 	nvio2.pnv_name = buf3;
210 	nvio2.pnv_buf = buf4;
211 	for (optnode = 0; optnode < nrofvars; optnode++) {
212 		nvio1.pnv_buflen = sizeof(buf2);
213 
214 		if (ioctl(fd, PNVIOCGETNEXTNAME, (char *) &nvio1) < 0)
215 			err(1, "PNVIOCGETNEXTNAME");
216 
217 		if (optnode == 0)
218 			nvio1.pnv_name = buf1;
219 
220 		/* the property name is now stored in nvio1.pnv_buf */
221 		strcpy(nvio2.pnv_name, nvio1.pnv_buf);	/* safe */
222 		nvio2.pnv_namelen = strlen(nvio2.pnv_name);
223 
224 		if (nvio2.pnv_namelen == 0) {
225 			(void) close(fd);
226 			return;
227 		}
228 		memset(nvio2.pnv_buf, 0, sizeof(buf4));
229 		nvio2.pnv_buflen = sizeof(buf4);
230 
231 		if (ioctl(fd, PNVIOCGET, (char *) &nvio2) < 0)
232 			err(1, "PNVIOCGET");
233 
234 		printf("%s=%s\n", nvio1.pnv_buf, nvio2.pnv_buf);
235 
236 		/* now clean out the buffers for the next loop */
237 
238 		memset(nvio1.pnv_name, 0, sizeof(buf1));
239 		memset(nvio1.pnv_buf, 0, sizeof(buf2));
240 		strcpy(nvio1.pnv_name, nvio2.pnv_name);	/* safe */
241 		nvio1.pnv_namelen = strlen(nvio1.pnv_name);
242 	}
243 	close(fd);
244 }
245