diff --git a/sys/dev/ofw/openfirmio.c b/sys/dev/ofw/openfirmio.c index 691d0dd80d04..2112d45d4dd9 100644 --- a/sys/dev/ofw/openfirmio.c +++ b/sys/dev/ofw/openfirmio.c @@ -1,294 +1,305 @@ /* $NetBSD: openfirmio.c,v 1.4 2002/09/06 13:23:19 gehenna Exp $ */ #include __FBSDID("$FreeBSD$"); /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and * contributed to Berkeley. * * All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Lawrence Berkeley Laboratory. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)openfirm.c 8.1 (Berkeley) 6/11/93 * */ #include #include #include #include #include #include #include #include #include #include static struct cdev *openfirm_dev; static d_ioctl_t openfirm_ioctl; #define OPENFIRM_MINOR 0 static struct cdevsw openfirm_cdevsw = { .d_version = D_VERSION, .d_flags = D_NEEDGIANT, .d_ioctl = openfirm_ioctl, .d_name = "openfirm", }; static phandle_t lastnode; /* speed hack */ static int openfirm_checkid(phandle_t, phandle_t); static int openfirm_getstr(int, const char *, char **); /* * Verify target ID is valid (exists in the OPENPROM tree), as * listed from node ID sid forward. */ static int openfirm_checkid(phandle_t sid, phandle_t tid) { for (; sid != 0; sid = OF_peer(sid)) if (sid == tid || openfirm_checkid(OF_child(sid), tid)) return (1); return (0); } static int openfirm_getstr(int len, const char *user, char **cpp) { int error; char *cp; /* Reject obvious bogus requests. */ if ((u_int)len > OFIOCMAXNAME) return (ENAMETOOLONG); *cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK); error = copyin(user, cp, len); cp[len] = '\0'; return (error); } int openfirm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) { struct ofiocdesc *of; phandle_t node; int len, ok, error; char *name, *value; - char newname[32]; + char newname[OFIOCSUGGPROPNAMELEN]; if ((flags & FREAD) == 0) return (EBADF); of = (struct ofiocdesc *)data; switch (cmd) { case OFIOCGETOPTNODE: *(phandle_t *) data = OF_finddevice("/options"); return (0); case OFIOCGET: case OFIOCSET: case OFIOCNEXTPROP: case OFIOCFINDDEVICE: case OFIOCGETPROPLEN: node = of->of_nodeid; break; case OFIOCGETNEXT: case OFIOCGETCHILD: node = *(phandle_t *)data; break; default: return (ENOIOCTL); } if (node != 0 && node != lastnode) { /* Not an easy one, we must search for it. */ ok = openfirm_checkid(OF_peer(0), node); if (!ok) return (EINVAL); lastnode = node; } name = value = NULL; error = 0; switch (cmd) { case OFIOCGET: case OFIOCGETPROPLEN: if (node == 0) return (EINVAL); error = openfirm_getstr(of->of_namelen, of->of_name, &name); if (error) break; len = OF_getproplen(node, name); if (cmd == OFIOCGETPROPLEN) { of->of_buflen = len; break; } if (len > of->of_buflen) { error = ENOMEM; break; } of->of_buflen = len; /* -1 means no entry; 0 means no value. */ if (len <= 0) break; value = malloc(len, M_TEMP, M_WAITOK); len = OF_getprop(node, name, (void *)value, len); error = copyout(value, of->of_buf, len); break; case OFIOCSET: /* * Note: Text string values for at least the /options node * have to be null-terminated and the length parameter must * include this terminating null. However, like OF_getprop(), * OF_setprop() will return the actual length of the text * string, i.e. omitting the terminating null. */ if ((flags & FWRITE) == 0) return (EBADF); if (node == 0) return (EINVAL); if ((u_int)of->of_buflen > OFIOCMAXVALUE) return (ENAMETOOLONG); error = openfirm_getstr(of->of_namelen, of->of_name, &name); if (error) break; value = malloc(of->of_buflen, M_TEMP, M_WAITOK); error = copyin(of->of_buf, value, of->of_buflen); if (error) break; len = OF_setprop(node, name, value, of->of_buflen); if (len < 0) error = EINVAL; of->of_buflen = len; break; case OFIOCNEXTPROP: if (node == 0 || of->of_buflen < 0) return (EINVAL); if (of->of_namelen != 0) { error = openfirm_getstr(of->of_namelen, of->of_name, &name); if (error) break; } ok = OF_nextprop(node, name, newname, sizeof(newname)); if (ok == 0) { error = ENOENT; break; } if (ok == -1) { error = EINVAL; break; } len = strlen(newname) + 1; - if (len > of->of_buflen) + if (len > of->of_buflen) { + /* + * Passed buffer was insufficient. + * + * Instead of returning an error here, truncate the + * property name to fit the buffer. + * + * This allows us to retain compatibility with old + * tools which always pass a 32 character buffer. + */ len = of->of_buflen; + newname[len - 1] = '\0'; + } else of->of_buflen = len; error = copyout(newname, of->of_buf, len); break; case OFIOCGETNEXT: node = OF_peer(node); *(phandle_t *)data = lastnode = node; break; case OFIOCGETCHILD: if (node == 0) return (EINVAL); node = OF_child(node); *(phandle_t *)data = lastnode = node; break; case OFIOCFINDDEVICE: error = openfirm_getstr(of->of_namelen, of->of_name, &name); if (error) break; node = OF_finddevice(name); if (node == -1) { error = ENOENT; break; } of->of_nodeid = lastnode = node; break; } if (name != NULL) free(name, M_TEMP); if (value != NULL) free(value, M_TEMP); return (error); } static int openfirm_modevent(module_t mod, int type, void *data) { switch(type) { case MOD_LOAD: if (bootverbose) printf("openfirm: \n"); /* * Allow only root access by default; this device may allow * users to peek into firmware passwords, and likely to crash * the machine on some boxen due to firmware quirks. */ openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR, UID_ROOT, GID_WHEEL, 0600, "openfirm"); return 0; case MOD_UNLOAD: destroy_dev(openfirm_dev); return 0; case MOD_SHUTDOWN: return 0; default: return EOPNOTSUPP; } } DEV_MODULE(openfirm, openfirm_modevent, NULL); diff --git a/sys/dev/ofw/openfirmio.h b/sys/dev/ofw/openfirmio.h index 7ba7b907e892..e892c50c672a 100644 --- a/sys/dev/ofw/openfirmio.h +++ b/sys/dev/ofw/openfirmio.h @@ -1,79 +1,93 @@ /* $NetBSD: openfirmio.h,v 1.4 2002/09/06 13:23:19 gehenna Exp $ */ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and * contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)openpromio.h 8.1 (Berkeley) 6/11/93 * * $FreeBSD$ */ #ifndef _DEV_OFW_OPENFIRMIO_H_ #define _DEV_OFW_OPENFIRMIO_H_ #include struct ofiocdesc { phandle_t of_nodeid; /* passed or returned node id */ int of_namelen; /* length of of_name */ const char *of_name; /* pointer to field name */ int of_buflen; /* length of of_buf (value-result) */ char *of_buf; /* pointer to field value */ }; #define OFIOC_BASE 'O' /* Get openprom field. */ #define OFIOCGET _IOWR(OFIOC_BASE, 1, struct ofiocdesc) /* Set openprom field. */ #define OFIOCSET _IOWR(OFIOC_BASE, 2, struct ofiocdesc) /* Get next property. */ #define OFIOCNEXTPROP _IOWR(OFIOC_BASE, 3, struct ofiocdesc) /* Get options node. */ #define OFIOCGETOPTNODE _IOR(OFIOC_BASE, 4, phandle_t) /* Get next node of node. */ #define OFIOCGETNEXT _IOWR(OFIOC_BASE, 5, phandle_t) /* Get first child of node. */ #define OFIOCGETCHILD _IOWR(OFIOC_BASE, 6, phandle_t) /* Find a specific device. */ #define OFIOCFINDDEVICE _IOWR(OFIOC_BASE, 7, struct ofiocdesc) /* Retrieve the size of a property. */ #define OFIOCGETPROPLEN _IOWR(OFIOC_BASE, 8, struct ofiocdesc) /* Maximum accepted name length. */ #define OFIOCMAXNAME 8191 /* Maximum accepted value length (maximum of nvramrc property). */ #define OFIOCMAXVALUE 8192 +/* + * While IEEE 1275-1994 states in 3.2.2.1.1 that property names are 1-31 + * printable characters, in practice, this limit has been ignored. + * Noncompliant properties have been codified in standards such as LoPAPR. + * + * This is a suggested buffer length that should be large enough to hold + * any property name currently seen in device trees, without being overly + * wasteful of memory. + * + * If a future version of the Devicetree specification updates the property + * names length requirement, this value will be updated to match. + */ +#define OFIOCSUGGPROPNAMELEN 64 + #endif /* _DEV_OFW_OPENFIRMIO_H_ */ diff --git a/usr.sbin/ofwdump/ofwdump.c b/usr.sbin/ofwdump/ofwdump.c index 9a356f48d01b..6bca1ac839ab 100644 --- a/usr.sbin/ofwdump/ofwdump.c +++ b/usr.sbin/ofwdump/ofwdump.c @@ -1,250 +1,250 @@ /*- * Copyright (c) 2002 by Thomas Moestl . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include "ofw_util.h" /* Constants controlling the layout of the output. */ #define LVLINDENT 2 #define NAMEINDENT 2 #define DUMPINDENT 4 #define CHARSPERLINE 60 #define BYTESPERLINE (CHARSPERLINE / 3) static void usage(void); static void ofw_indent(int); static void ofw_dump_properties(int, phandle_t, int, int, int); static void ofw_dump_property(int fd, phandle_t n, int level, const char *prop, int raw, int str); static void ofw_dump(int, const char *, int, int, const char *, int, int); static void usage(void) { fprintf(stderr, "usage: ofwdump -a [-p | -P property] [-R | -S]\n" " ofwdump [-p | -P property] [-r] [-R | -S] [--] nodes\n"); exit(EX_USAGE); } int main(int argc, char *argv[]) { int opt, i, fd; int aflag, pflag, rflag, Rflag, Sflag; char *Parg; aflag = pflag = rflag = Rflag = Sflag = 0; Parg = NULL; while ((opt = getopt(argc, argv, "-aprP:RS")) != -1) { if (opt == '-') break; switch (opt) { case 'a': aflag = 1; rflag = 1; break; case 'p': if (Parg != NULL) usage(); pflag = 1; break; case 'r': rflag = 1; break; case 'P': if (pflag) usage(); pflag = 1; Parg = optarg; break; case 'R': if (Sflag) usage(); Rflag = 1; break; case 'S': if (Rflag) usage(); Sflag = 1; break; case '?': default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; fd = ofw_open(O_RDONLY); if (aflag) { if (argc != 0) usage(); ofw_dump(fd, NULL, rflag, pflag, Parg, Rflag, Sflag); } else { /* * For the sake of scripts, usage() is not called here if * argc == 0. */ for (i = 0; i < argc; i++) ofw_dump(fd, argv[i], rflag, pflag, Parg, Rflag, Sflag); } ofw_close(fd); return (EX_OK); } static void ofw_indent(int level) { int i; for (i = 0; i < level; i++) putchar(' '); } static void ofw_dump_properties(int fd, phandle_t n, int level, int raw, int str) { int nlen; - char prop[32]; + char prop[OFIOCSUGGPROPNAMELEN]; for (nlen = ofw_firstprop(fd, n, prop, sizeof(prop)); nlen != 0; nlen = ofw_nextprop(fd, n, prop, prop, sizeof(prop))) ofw_dump_property(fd, n, level, prop, raw, str); } static void ofw_dump_property(int fd, phandle_t n, int level, const char *prop, int raw, int str) { static void *pbuf = NULL; static char *visbuf = NULL; static char printbuf[CHARSPERLINE + 1]; static int pblen = 0, vblen = 0; int len, i, j, max, vlen; len = ofw_getprop_alloc(fd, n, prop, &pbuf, &pblen, 1); if (len < 0) return; if (raw) write(STDOUT_FILENO, pbuf, len); else if (str) printf("%.*s\n", len, (char *)pbuf); else { ofw_indent(level * LVLINDENT + NAMEINDENT); printf("%s:\n", prop); /* Print in hex. */ for (i = 0; i < len; i += BYTESPERLINE) { max = len - i; max = max > BYTESPERLINE ? BYTESPERLINE : max; ofw_indent(level * LVLINDENT + DUMPINDENT); for (j = 0; j < max; j++) printf("%02x ", ((unsigned char *)pbuf)[i + j]); printf("\n"); } /* * strvis() and print if it looks like it is * zero-terminated. */ if (((char *)pbuf)[len - 1] == '\0' && strlen(pbuf) == (unsigned)len - 1) { if (vblen < (len - 1) * 4 + 1) { if (visbuf != NULL) free(visbuf); vblen = (OFIOCMAXVALUE + len) * 4 + 1; if ((visbuf = malloc(vblen)) == NULL) err(EX_OSERR, "malloc() failed"); } vlen = strvis(visbuf, pbuf, VIS_TAB | VIS_NL); for (i = 0; i < vlen; i += CHARSPERLINE) { ofw_indent(level * LVLINDENT + DUMPINDENT); strlcpy(printbuf, &visbuf[i], sizeof(printbuf)); printf("'%s'\n", printbuf); } } } } static void ofw_dump_node(int fd, phandle_t n, int level, int rec, int prop, const char *pmatch, int raw, int str) { static void *nbuf = NULL; static int nblen = 0; int plen; phandle_t c; if (!(raw || str)) { ofw_indent(level * LVLINDENT); printf("Node %#lx", (unsigned long)n); plen = ofw_getprop_alloc(fd, n, "name", &nbuf, &nblen, 1); if (plen > 0) printf(": %.*s\n", (int)plen, (char *)nbuf); else putchar('\n'); } if (prop) { if (pmatch) ofw_dump_property(fd, n, level, pmatch, raw, str); else ofw_dump_properties(fd, n, level, raw, str); } if (rec) { for (c = ofw_child(fd, n); c != 0; c = ofw_peer(fd, c)) { ofw_dump_node(fd, c, level + 1, rec, prop, pmatch, raw, str); } } } static void ofw_dump(int fd, const char *start, int rec, int prop, const char *pmatch, int raw, int str) { phandle_t n; n = start == NULL ? ofw_root(fd) : ofw_finddevice(fd, start); ofw_dump_node(fd, n, 0, rec, prop, pmatch, raw, str); }