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