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)(const struct ofwo_extabent *, int,
56 const void *, int, const char *);
59 static int ofwo_oemlogo(const struct ofwo_extabent *, int, const void *,
61 static int ofwo_secmode(const struct ofwo_extabent *, int, const void *,
63 static int ofwo_secpwd(const struct ofwo_extabent *, int, const void *,
66 static const 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 *, const char *);
77 ofwo_printprop(const char *prop, const char* buf, int buflen)
80 printf("%s: %.*s\n", prop, buflen, buf);
84 ofwo_oemlogo(const struct ofwo_extabent *exent, int fd, const void *buf,
85 int buflen, const char *val)
88 char logo[OFWO_LOGO + 1];
92 ofw_setprop(fd, ofw_optnode(fd), exent->ex_prop, "", 1);
94 if ((lfd = open(val, O_RDONLY)) == -1) {
95 warn("could not open '%s'", val);
98 if (read(lfd, logo, OFWO_LOGO) != OFWO_LOGO ||
99 lseek(lfd, 0, SEEK_END) != OFWO_LOGO) {
101 warnx("logo '%s' has wrong size.", val);
105 logo[OFWO_LOGO] = '\0';
106 if (ofw_setprop(fd, ofw_optnode(fd), exent->ex_prop,
107 logo, OFWO_LOGO + 1) != OFWO_LOGO)
108 errx(EX_IOERR, "writing logo failed.");
112 printf("%s: <logo data>\n", exent->ex_prop);
114 ofwo_printprop(exent->ex_prop, (const char *)buf,
120 ofwo_secmode(const struct ofwo_extabent *exent, int fd, const void *buf,
121 int buflen, const char *val)
126 if (strcmp(val, "full") == 0 || strcmp(val, "command") == 0) {
127 if ((res = ofwo_setpass(fd)) != EX_OK)
129 if ((res = ofwo_setstr(fd, buf, buflen, exent->ex_prop,
131 ofw_setprop(fd, ofw_optnode(fd),
132 "security-password", "", 1);
135 if (strcmp(val, "none") == 0) {
136 ofw_setprop(fd, ofw_optnode(fd), "security-password",
138 return (ofwo_setstr(fd, buf, buflen, exent->ex_prop,
143 ofwo_printprop(exent->ex_prop, (const char *)buf, buflen);
148 ofwo_secpwd(const struct ofwo_extabent *exent, int fd, const void *buf,
149 int buflen, const char *val)
158 len = ofw_getprop_alloc(fd, ofw_optnode(fd), "security-mode",
160 if (len <= 0 || strncmp("none", (char *)pbuf, len) == 0) {
162 warnx("no security mode set.");
163 } else if (strncmp("command", (char *)pbuf, len) == 0 ||
164 strncmp("full", (char *)pbuf, len) == 0) {
165 rv = ofwo_setpass(fd);
168 warnx("invalid security mode.");
171 ofwo_printprop(exent->ex_prop, (const char *)buf, buflen);
180 char pwd1[OFWO_MAXPWD + 1], pwd2[OFWO_MAXPWD + 1];
182 if (readpassphrase("New password:", pwd1, sizeof(pwd1),
183 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL ||
184 readpassphrase("Retype new password:", pwd2, sizeof(pwd2),
185 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL)
186 errx(EX_USAGE, "failed to get password.");
187 if (strlen(pwd1) == 0) {
188 printf("Password unchanged.\n");
191 if (strcmp(pwd1, pwd2) != 0) {
192 printf("Mismatch - password unchanged.\n");
195 ofw_setprop(fd, ofw_optnode(fd), "security-password", pwd1,
201 ofwo_setstr(int fd, const void *buf, int buflen, const char *prop,
212 optnode = ofw_optnode(fd);
213 ofw_setprop(fd, optnode, prop, val, strlen(val) + 1);
214 len = ofw_getprop_alloc(fd, optnode, prop, &pbuf, &pblen, 1);
215 if (len < 0 || strncmp(val, (char *)pbuf, len) != 0) {
217 * The value is too long for this property and the OFW has
218 * truncated it to fit or the value is illegal and a legal
219 * one has been written instead (e.g. attempted to write
220 * "foobar" to a "true"/"false"-property) - try to recover
224 if ((oval = malloc(buflen + 1)) == NULL)
225 err(EX_OSERR, "malloc() failed.");
226 strncpy(oval, buf, buflen);
228 len = ofw_setprop(fd, optnode, prop, oval, buflen + 1);
230 errx(EX_IOERR, "recovery of old value failed.");
234 printf("%s: %.*s%s->%s%.*s\n", prop, buflen, (const char *)buf,
235 buflen > 0 ? " " : "", len > 0 ? " " : "", len, (char *)pbuf);
246 int fd, len, nlen, pblen;
248 char prop[OFWO_MAXPROP + 1];
249 const struct ofwo_extabent *ex;
253 fd = ofw_open(O_RDONLY);
254 optnode = ofw_optnode(fd);
255 for (nlen = ofw_firstprop(fd, optnode, prop, sizeof(prop)); nlen != 0;
256 nlen = ofw_nextprop(fd, optnode, prop, prop, sizeof(prop))) {
257 len = ofw_getprop_alloc(fd, optnode, prop, &pbuf, &pblen, 1);
260 if (strcmp(prop, "name") == 0)
262 for (ex = ofwo_extab; ex->ex_prop != NULL; ++ex)
263 if (strcmp(ex->ex_prop, prop) == 0)
265 if (ex->ex_prop != NULL)
266 (*ex->ex_handler)(ex, fd, pbuf, len, NULL);
268 ofwo_printprop(prop, (char *)pbuf, len);
276 ofwo_action(const char *prop, const char *val)
279 int fd, len, pblen, rv;
280 const struct ofwo_extabent *ex;
285 if (strcmp(prop, "name") == 0)
286 return (EX_UNAVAILABLE);
288 fd = ofw_open(O_RDWR);
290 fd = ofw_open(O_RDONLY);
291 len = ofw_getprop_alloc(fd, ofw_optnode(fd), prop, &pbuf, &pblen, 1);
296 for (ex = ofwo_extab; ex->ex_prop != NULL; ++ex)
297 if (strcmp(ex->ex_prop, prop) == 0)
299 if (ex->ex_prop != NULL)
300 rv = (*ex->ex_handler)(ex, fd, pbuf, len, val);
302 rv = ofwo_setstr(fd, pbuf, len, prop, val);
304 ofwo_printprop(prop, (char *)pbuf, len);