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