| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: ofw_subr.c,v 1.46 2021/01/24 17:44:16 thorpej Exp $ */ | | 1 | /* $NetBSD: ofw_subr.c,v 1.47 2021/01/24 19:38:37 thorpej Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright 1998 | | 4 | * Copyright 1998 |
5 | * Digital Equipment Corporation. All rights reserved. | | 5 | * Digital Equipment Corporation. All rights reserved. |
6 | * | | 6 | * |
7 | * This software is furnished under license and may be used and | | 7 | * This software is furnished under license and may be used and |
8 | * copied only in accordance with the following terms and conditions. | | 8 | * copied only in accordance with the following terms and conditions. |
9 | * Subject to these conditions, you may download, copy, install, | | 9 | * Subject to these conditions, you may download, copy, install, |
10 | * use, modify and distribute this software in source and/or binary | | 10 | * use, modify and distribute this software in source and/or binary |
11 | * form. No title or ownership is transferred hereby. | | 11 | * form. No title or ownership is transferred hereby. |
12 | * | | 12 | * |
13 | * 1) Any source code used, modified or distributed must reproduce | | 13 | * 1) Any source code used, modified or distributed must reproduce |
14 | * and retain this copyright notice and list of conditions as | | 14 | * and retain this copyright notice and list of conditions as |
| @@ -24,27 +24,27 @@ | | | @@ -24,27 +24,27 @@ |
24 | * 3) This software is provided "AS-IS" and any express or implied | | 24 | * 3) This software is provided "AS-IS" and any express or implied |
25 | * warranties, including but not limited to, any implied warranties | | 25 | * warranties, including but not limited to, any implied warranties |
26 | * of merchantability, fitness for a particular purpose, or | | 26 | * of merchantability, fitness for a particular purpose, or |
27 | * non-infringement are disclaimed. In no event shall DIGITAL be | | 27 | * non-infringement are disclaimed. In no event shall DIGITAL be |
28 | * liable for any damages whatsoever, and in particular, DIGITAL | | 28 | * liable for any damages whatsoever, and in particular, DIGITAL |
29 | * shall not be liable for special, indirect, consequential, or | | 29 | * shall not be liable for special, indirect, consequential, or |
30 | * incidental damages or damages for lost profits, loss of | | 30 | * incidental damages or damages for lost profits, loss of |
31 | * revenue or loss of use, whether such damages arise in contract, | | 31 | * revenue or loss of use, whether such damages arise in contract, |
32 | * negligence, tort, under statute, in equity, at law or otherwise, | | 32 | * negligence, tort, under statute, in equity, at law or otherwise, |
33 | * even if advised of the possibility of such damage. | | 33 | * even if advised of the possibility of such damage. |
34 | */ | | 34 | */ |
35 | | | 35 | |
36 | #include <sys/cdefs.h> | | 36 | #include <sys/cdefs.h> |
37 | __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.46 2021/01/24 17:44:16 thorpej Exp $"); | | 37 | __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.47 2021/01/24 19:38:37 thorpej Exp $"); |
38 | | | 38 | |
39 | #include <sys/param.h> | | 39 | #include <sys/param.h> |
40 | #include <sys/device.h> | | 40 | #include <sys/device.h> |
41 | #include <sys/kmem.h> | | 41 | #include <sys/kmem.h> |
42 | #include <sys/systm.h> | | 42 | #include <sys/systm.h> |
43 | #include <sys/malloc.h> | | 43 | #include <sys/malloc.h> |
44 | #include <dev/ofw/openfirm.h> | | 44 | #include <dev/ofw/openfirm.h> |
45 | | | 45 | |
46 | #define OFW_MAX_STACK_BUF_SIZE 256 | | 46 | #define OFW_MAX_STACK_BUF_SIZE 256 |
47 | #define OFW_PATH_BUF_SIZE 512 | | 47 | #define OFW_PATH_BUF_SIZE 512 |
48 | | | 48 | |
49 | /* | | 49 | /* |
50 | * int of_decode_int(p) | | 50 | * int of_decode_int(p) |
| @@ -91,80 +91,51 @@ of_decode_int(const unsigned char *p) | | | @@ -91,80 +91,51 @@ of_decode_int(const unsigned char *p) |
91 | * indicates compatibility. | | 91 | * indicates compatibility. |
92 | * | | 92 | * |
93 | * Return Value: | | 93 | * Return Value: |
94 | * -1 if none of the strings are found in phandle's "compatibility" | | 94 | * -1 if none of the strings are found in phandle's "compatibility" |
95 | * property, or the reverse index of the matching string in the | | 95 | * property, or the reverse index of the matching string in the |
96 | * phandle's "compatibility" property. | | 96 | * phandle's "compatibility" property. |
97 | * | | 97 | * |
98 | * Side Effects: | | 98 | * Side Effects: |
99 | * None. | | 99 | * None. |
100 | */ | | 100 | */ |
101 | int | | 101 | int |
102 | of_compatible(int phandle, const char * const *strings) | | 102 | of_compatible(int phandle, const char * const *strings) |
103 | { | | 103 | { |
| | | 104 | char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE]; |
| | | 105 | const char *cp; |
| | | 106 | int proplen, match, rv = -1; |
104 | | | 107 | |
105 | int len, olen, allocated, nstr, cstr, rv; | | 108 | proplen = OF_getproplen(phandle, "compatible"); |
106 | char *buf, sbuf[OFW_MAX_STACK_BUF_SIZE]; | | 109 | if (proplen <= 0) { |
107 | const char *sp, *nsp; | | 110 | return -1; |
108 | | | | |
109 | len = OF_getproplen(phandle, "compatible"); | | | |
110 | if (len <= 0) | | | |
111 | return (-1); | | | |
112 | | | | |
113 | if (len > sizeof(sbuf)) { | | | |
114 | buf = malloc(len, M_TEMP, M_WAITOK); | | | |
115 | allocated = 1; | | | |
116 | } else { | | | |
117 | buf = sbuf; | | | |
118 | allocated = 0; | | | |
119 | } | | 111 | } |
120 | | | 112 | |
121 | /* 'compatible' size should not change. */ | | 113 | prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP); |
122 | if (OF_getprop(phandle, "compatible", buf, len) != len) { | | 114 | |
123 | rv = -1; | | 115 | if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) { |
124 | goto out; | | 116 | goto out; |
125 | } | | 117 | } |
126 | | | 118 | |
127 | /* count 'compatible' strings */ | | 119 | for (; (cp = *strings) != NULL; strings++) { |
128 | sp = buf; | | 120 | if ((match = strlist_match(prop, proplen, cp)) != 0) { |
129 | nstr = 0; | | 121 | rv = match - 1; |
130 | olen = len; | | 122 | break; |
131 | while (len && (nsp = memchr(sp, 0, len)) != NULL) { | | 123 | } |
132 | nsp++; /* skip over NUL char */ | | 124 | } |
133 | len -= (nsp - sp); | | 125 | |
134 | sp = nsp; | | 126 | out: |
135 | nstr++; | | 127 | kmem_tmpbuf_free(prop, proplen, propbuf); |
136 | } | | 128 | return rv; |
137 | len = olen; | | | |
138 | | | | |
139 | sp = buf; | | | |
140 | rv = nstr; | | | |
141 | while (len && (nsp = memchr(sp, 0, len)) != NULL) { | | | |
142 | rv--; | | | |
143 | /* look for a match among the strings provided */ | | | |
144 | for (cstr = 0; strings[cstr] != NULL; cstr++) | | | |
145 | if (strcmp(sp, strings[cstr]) == 0) | | | |
146 | goto out; | | | |
147 | | | | |
148 | nsp++; /* skip over NUL char */ | | | |
149 | len -= (nsp - sp); | | | |
150 | sp = nsp; | | | |
151 | } | | | |
152 | rv = -1; | | | |
153 | | | | |
154 | out: | | | |
155 | if (allocated) | | | |
156 | free(buf, M_TEMP); | | | |
157 | return (rv); | | | |
158 | } | | 129 | } |
159 | | | 130 | |
160 | /* | | 131 | /* |
161 | * int of_match_compatible(phandle, strings) | | 132 | * int of_match_compatible(phandle, strings) |
162 | * | | 133 | * |
163 | * This routine checks an OFW node's "compatible" entry to see if | | 134 | * This routine checks an OFW node's "compatible" entry to see if |
164 | * it matches any of the provided strings. | | 135 | * it matches any of the provided strings. |
165 | * | | 136 | * |
166 | * It should be used when determining whether a driver can drive | | 137 | * It should be used when determining whether a driver can drive |
167 | * a particular device. | | 138 | * a particular device. |
168 | * | | 139 | * |
169 | * Arguments: | | 140 | * Arguments: |
170 | * phandle OFW phandle of device to be checked for | | 141 | * phandle OFW phandle of device to be checked for |