2 * Copyright (c) 2004 Marius Strobl
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * Handlers for Open Firmware /options node.
33 #include <sys/types.h>
35 #include <dev/ofw/openfirm.h>
39 #include <readpassphrase.h>
46 #include "ofw_options.h"
50 #define OFWO_MAXPROP 31
53 struct ofwo_extabent {
55 int (*ex_handler)(struct ofwo_extabent *, int, const void *,
59 static int ofwo_oemlogo(struct ofwo_extabent *, int, const void *, int,
61 static int ofwo_secmode(struct ofwo_extabent *, int, const void *, int,
63 static int ofwo_secpwd(struct ofwo_extabent *, int, const void *, int,
66 static struct ofwo_extabent ofwo_extab[] = {
67 { "oem-logo", ofwo_oemlogo },
68 { "security-mode", ofwo_secmode },
69 { "security-password", ofwo_secpwd },
73 static int ofwo_setpass(int);
74 static int ofwo_setstr(int, const void *, int, const char *,
78 ofwo_printprop(const char *prop, const char* buf, int buflen)
81 printf("%s: %.*s\n", prop, buflen, buf);
85 ofwo_oemlogo(struct ofwo_extabent *exent, int fd, const void *buf, int buflen,
89 char logo[OFWO_LOGO + 1];
93 ofw_setprop(fd, ofw_optnode(fd), exent->ex_prop, "", 1);
95 if ((lfd = open(val, O_RDONLY)) == -1) {
96 warn("could not open '%s'", val);
99 if (read(lfd, logo, OFWO_LOGO) != OFWO_LOGO ||
100 lseek(lfd, 0, SEEK_END) != OFWO_LOGO) {
102 warnx("logo '%s' has wrong size.", val);
106 logo[OFWO_LOGO] = '\0';
107 if (ofw_setprop(fd, ofw_optnode(fd), exent->ex_prop,
108 logo, OFWO_LOGO + 1) != OFWO_LOGO)
109 errx(EX_IOERR, "writing logo failed.");
113 printf("%s: <logo data>\n", exent->ex_prop);
115 ofwo_printprop(exent->ex_prop, (const char *)buf,
121 ofwo_secmode(struct ofwo_extabent *exent, int fd, const void *buf, int buflen,
127 if (strcmp(val, "full") == 0 || strcmp(val, "command") == 0) {
128 if ((res = ofwo_setpass(fd)) != EX_OK)
130 if ((res = ofwo_setstr(fd, buf, buflen, exent->ex_prop,
132 ofw_setprop(fd, ofw_optnode(fd),
133 "security-password", "", 1);
136 if (strcmp(val, "none") == 0) {
137 ofw_setprop(fd, ofw_optnode(fd), "security-password",
139 return (ofwo_setstr(fd, buf, buflen, exent->ex_prop,
144 ofwo_printprop(exent->ex_prop, (const char *)buf, buflen);
149 ofwo_secpwd(struct ofwo_extabent *exent, int fd __unused,
150 const void *buf __unused, __unused int buflen, const char *val)
159 len = ofw_getprop_alloc(fd, ofw_optnode(fd), "security-mode",
161 if (len <= 0 || strncmp("none", (char *)pbuf, len) == 0) {
163 warnx("no security mode set.");
164 } else if (strncmp("command", (char *)pbuf, len) == 0 ||
165 strncmp("full", (char *)pbuf, len) == 0) {
166 rv = ofwo_setpass(fd);
169 warnx("invalid security mode.");
172 ofwo_printprop(exent->ex_prop, (const char *)buf, buflen);
181 char pwd1[OFWO_MAXPWD + 1], pwd2[OFWO_MAXPWD + 1];
183 if (readpassphrase("New password:", pwd1, sizeof(pwd1),
184 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL ||
185 readpassphrase("Retype new password:", pwd2, sizeof(pwd2),
186 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL)
187 errx(EX_USAGE, "failed to get password.");
188 if (strlen(pwd1) == 0) {
189 printf("Password unchanged.\n");
192 if (strcmp(pwd1, pwd2) != 0) {
193 printf("Mismatch - password unchanged.\n");
196 ofw_setprop(fd, ofw_optnode(fd), "security-password", pwd1,
202 ofwo_setstr(int fd, const void *buf, int buflen, const char *prop,
213 optnode = ofw_optnode(fd);
214 ofw_setprop(fd, optnode, prop, val, strlen(val) + 1);
215 len = ofw_getprop_alloc(fd, optnode, prop, &pbuf, &pblen, 1);
216 if (len < 0 || strncmp(val, (char *)pbuf, len) != 0) {
218 * The value is too long for this property and the OFW has
219 * truncated it to fit or the value is illegal and a legal
220 * one has been written instead (e.g. attempted to write
221 * "foobar" to a "true"/"false"-property) - try to recover
225 if ((oval = malloc(buflen + 1)) == NULL)
226 err(EX_OSERR, "malloc() failed.");
227 strncpy(oval, buf, buflen);
229 len = ofw_setprop(fd, optnode, prop, oval, buflen + 1);
231 errx(EX_IOERR, "recovery of old value failed.");
235 printf("%s: %.*s%s->%s%.*s\n", prop, buflen, (const char *)buf,
236 buflen > 0 ? " " : "", len > 0 ? " " : "", len, (char *)pbuf);
247 int fd, len, nlen, pblen;
249 char prop[OFWO_MAXPROP + 1];
250 struct ofwo_extabent *ex;
254 fd = ofw_open(O_RDONLY);
255 optnode = ofw_optnode(fd);
256 for (nlen = ofw_firstprop(fd, optnode, prop, sizeof(prop)); nlen != 0;
257 nlen = ofw_nextprop(fd, optnode, prop, prop, sizeof(prop))) {
258 len = ofw_getprop_alloc(fd, optnode, prop, &pbuf, &pblen, 1);
261 if (strcmp(prop, "name") == 0)
263 for (ex = ofwo_extab; ex->ex_prop != NULL; ++ex)
264 if (strcmp(ex->ex_prop, prop) == 0)
266 if (ex->ex_prop != NULL)
267 (*ex->ex_handler)(ex, fd, pbuf, len, NULL);
269 ofwo_printprop(prop, (char *)pbuf, len);
277 ofwo_action(const char *prop, const char *val)
280 int fd, len, pblen, rv;
281 struct ofwo_extabent *ex;
286 if (strcmp(prop, "name") == 0)
287 return (EX_UNAVAILABLE);
289 fd = ofw_open(O_RDWR);
291 fd = ofw_open(O_RDONLY);
292 len = ofw_getprop_alloc(fd, ofw_optnode(fd), prop, &pbuf, &pblen, 1);
297 for (ex = ofwo_extab; ex->ex_prop != NULL; ++ex)
298 if (strcmp(ex->ex_prop, prop) == 0)
300 if (ex->ex_prop != NULL)
301 rv = (*ex->ex_handler)(ex, fd, pbuf, len, val);
303 rv = ofwo_setstr(fd, pbuf, len, prop, val);
305 ofwo_printprop(prop, (char *)pbuf, len);