devpath2.c revision 1.3 1 1.3 riastrad /* $NetBSD: devpath2.c,v 1.3 2025/03/02 00:03:41 riastradh Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Redistribution and use in source and binary forms, with or without
5 1.1 christos * modification, are permitted provided that the following conditions
6 1.1 christos * are met:
7 1.1 christos * 1. Redistributions of source code must retain the above copyright
8 1.1 christos * notice, this list of conditions and the following disclaimer.
9 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer in the
11 1.1 christos * documentation and/or other materials provided with the distribution.
12 1.1 christos *
13 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 1.1 christos * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 1.1 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 1.1 christos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 1.1 christos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 1.1 christos * SUCH DAMAGE.
24 1.1 christos */
25 1.1 christos
26 1.1 christos #include <sys/cdefs.h>
27 1.1 christos #ifndef lint
28 1.3 riastrad __RCSID("$NetBSD: devpath2.c,v 1.3 2025/03/02 00:03:41 riastradh Exp $");
29 1.1 christos #endif /* not lint */
30 1.1 christos
31 1.1 christos #include <assert.h>
32 1.1 christos #include <stdio.h>
33 1.1 christos #include <string.h>
34 1.1 christos #include <util.h>
35 1.1 christos
36 1.1 christos #include "defs.h"
37 1.1 christos #include "devpath.h"
38 1.1 christos #include "devpath2.h"
39 1.1 christos #include "utils.h"
40 1.1 christos
41 1.1 christos #define easprintf (size_t)easprintf
42 1.1 christos
43 1.1 christos /************************************************************************
44 1.1 christos * Type 2 - ACPI Device Path
45 1.1 christos ************************************************************************/
46 1.1 christos
47 1.1 christos /*
48 1.1 christos * See 19.3.4 of ACPI Specification, Release 6.5 for compressed EISAID
49 1.1 christos * algorithm.
50 1.1 christos */
51 1.1 christos
52 1.1 christos #define PNP0A03 0x0a0341d0
53 1.1 christos #define PNP0A08 0x0a0841d0
54 1.1 christos
55 1.1 christos static const char *
56 1.1 christos eisaid_to_str(uint32_t eisaid)
57 1.1 christos {
58 1.1 christos static const char hexdigits[] = "0123456789ABCDEF";
59 1.1 christos static char text[8];
60 1.1 christos union {
61 1.1 christos uint32_t eisaid;
62 1.1 christos uint8_t b[4];
63 1.1 christos } u;
64 1.1 christos
65 1.1 christos u.eisaid = eisaid;
66 1.1 christos
67 1.1 christos text[0] = (char)__SHIFTOUT(u.b[0], __BITS(4,0));
68 1.1 christos text[1] = (char)__SHIFTOUT(u.b[0], __BITS(7,5));
69 1.1 christos text[1] |= (char)__SHIFTOUT(u.b[1], __BITS(1,0)) << 3;
70 1.1 christos text[2] = (char)__SHIFTOUT(u.b[1], __BITS(7,2));
71 1.1 christos
72 1.1 christos text[0] += 0x40; /* '@' */
73 1.1 christos text[1] += 0x40;
74 1.1 christos text[2] += 0x40;
75 1.1 christos
76 1.1 christos #define hi_nib(v) (((v) >> 4) & 0x0f)
77 1.1 christos #define lo_nib(v) (((v) >> 0) & 0x0f)
78 1.1 christos text[3] = hexdigits[hi_nib(u.b[3])];
79 1.1 christos text[4] = hexdigits[lo_nib(u.b[3])];
80 1.1 christos text[5] = hexdigits[hi_nib(u.b[2])];
81 1.1 christos text[6] = hexdigits[lo_nib(u.b[2])];
82 1.1 christos text[7] = 0;
83 1.1 christos #undef lo_nib
84 1.1 christos #undef hi_nib
85 1.1 christos
86 1.1 christos return text;
87 1.1 christos }
88 1.1 christos
89 1.1 christos static inline const char *
90 1.1 christos devpath_acpi_acpi_eisaid(uint32_t eisaid)
91 1.1 christos {
92 1.1 christos
93 1.1 christos #define PNP(v) (0x000041d0 | ((v) << 16))
94 1.1 christos switch (eisaid) {
95 1.1 christos // case PNP(0x0f03): return "PS2Mouse"; /* legacy? */
96 1.1 christos // case PNP(0x0303): return "FloppyPNP"; /* legacy? */
97 1.1 christos case PNP(0x0a03): return "PciRoot";
98 1.1 christos case PNP(0x0a08): return "PcieRoot";
99 1.1 christos case PNP(0x0604): return "Floppy";
100 1.1 christos case PNP(0x0301): return "Keyboard";
101 1.1 christos case PNP(0x0501): return "Serial";
102 1.1 christos case PNP(0x0401): return "ParallelPort";
103 1.1 christos default: return NULL;
104 1.1 christos }
105 1.1 christos #undef PNP
106 1.1 christos }
107 1.1 christos
108 1.1 christos static inline void
109 1.1 christos devpath_acpi_acpi(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
110 1.1 christos { /* See 10.3.3 */
111 1.1 christos struct { /* Sub-Type 1 */
112 1.1 christos devpath_t hdr; /* Length = 12 */
113 1.1 christos uint32_t _HID;
114 1.1 christos uint32_t _UID;
115 1.1 christos } __packed *p = (void *)dp;
116 1.1 christos __CTASSERT(sizeof(*p) == 12);
117 1.1 christos const char *str;
118 1.1 christos
119 1.1 christos str = devpath_acpi_acpi_eisaid(p->_HID);
120 1.1 christos if (str == NULL)
121 1.1 christos path->sz = easprintf(&path->cp, "ACPI(%s,0x%x)", eisaid_to_str(p->_HID), p->_UID);
122 1.1 christos else
123 1.1 christos path->sz = easprintf(&path->cp, "%s(0x%x)", str, p->_UID);
124 1.1 christos
125 1.1 christos if (dbg != NULL) {
126 1.1 christos dbg->sz = easprintf(&dbg->cp,
127 1.1 christos DEVPATH_FMT_HDR
128 1.1 christos DEVPATH_FMT(_HID: 0x%08x(%s)\n)
129 1.1 christos DEVPATH_FMT(_UID: 0x%08x\n),
130 1.1 christos DEVPATH_DAT_HDR(dp),
131 1.1 christos p->_HID,
132 1.1 christos eisaid_to_str(p->_HID),
133 1.1 christos p->_UID);
134 1.1 christos }
135 1.1 christos }
136 1.1 christos
137 1.1 christos static inline void
138 1.1 christos devpath_acpi_acpiex(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
139 1.1 christos { /* See 10.3.3 */
140 1.1 christos struct { /* Sub-Type 2 */
141 1.1 christos devpath_t hdr; /* Length = 19 + n */
142 1.1 christos uint32_t _HID;
143 1.1 christos uint32_t _UID;
144 1.1 christos uint32_t _CID;
145 1.1 christos char _HIDSTR[]; /* at least NUL byte */
146 1.1 christos // char _UIDSTR[]; /* at least NUL byte */
147 1.1 christos // char _CIDSTR[]; /* at least NUL byte */
148 1.1 christos } __packed *p = (void *)dp;
149 1.1 christos __CTASSERT(sizeof(*p) == 16);
150 1.1 christos char *hidstr = p->_HIDSTR;
151 1.1 christos char *uidstr = hidstr + strlen(hidstr) + 1;
152 1.1 christos char *cidstr = uidstr + strlen(uidstr) + 1;
153 1.1 christos
154 1.1 christos #if 0
155 1.1 christos There are 4 different subsubtypes depending on the ?ID, ?IDSTR values.
156 1.1 christos
157 1.1 christos "AcpiEx"
158 1.1 christos "AcpiExp"
159 1.1 christos "PciRoot"
160 1.1 christos "PcipRoot"
161 1.1 christos #endif
162 1.1 christos if (p->_HID == PNP0A08 || p->_CID == PNP0A08) {
163 1.1 christos // PcieRoot(UID|UIDSTR)
164 1.1 christos path->sz = easprintf(&path->cp, "PcieRoot(%s)",
165 1.1 christos *uidstr != '\0' ? uidstr : eisaid_to_str(p->_UID));
166 1.1 christos }
167 1.1 christos else if (p->_HID == PNP0A03 || p->_CID == PNP0A03) {
168 1.1 christos assert(p->_HID != PNP0A08);
169 1.1 christos // PciRoot(UID|UIDSTR)
170 1.1 christos path->sz = easprintf(&path->cp, "PciRoot(%s)",
171 1.1 christos *uidstr != '\0' ? uidstr : eisaid_to_str(p->_UID));
172 1.1 christos }
173 1.1 christos else if (hidstr[0] == '\0' && cidstr[0] == '\0' && uidstr[0] != '\0') {
174 1.1 christos assert(p->_HID != 0);
175 1.1 christos path->sz = easprintf(&path->cp, "AcpiExp(%s,%s,%s)", eisaid_to_str(p->_HID),
176 1.1 christos eisaid_to_str(p->_CID), uidstr);
177 1.1 christos }
178 1.1 christos else {
179 1.1 christos path->sz = easprintf(&path->cp, "ACPIEX(%s,%s,0x%x,%s,%s,%s)",
180 1.1 christos eisaid_to_str(p->_HID), eisaid_to_str(p->_CID),
181 1.1 christos p->_UID, hidstr, cidstr, uidstr);
182 1.1 christos }
183 1.1 christos
184 1.1 christos if (dbg != NULL) {
185 1.1 christos dbg->sz = easprintf(&dbg->cp,
186 1.1 christos DEVPATH_FMT_HDR
187 1.1 christos DEVPATH_FMT(_HID: 0x%08x(%s)\n)
188 1.1 christos DEVPATH_FMT(_UID: 0x%08x\n)
189 1.1 christos DEVPATH_FMT(_CID: 0x%08x(%s)\n)
190 1.1 christos DEVPATH_FMT(_HIDSTR: %s\n)
191 1.1 christos DEVPATH_FMT(_UIDSTR: %s\n)
192 1.1 christos DEVPATH_FMT(_CIDSTR: %s\n),
193 1.1 christos DEVPATH_DAT_HDR(dp),
194 1.1 christos p->_HID,
195 1.1 christos eisaid_to_str(p->_HID),
196 1.1 christos p->_UID,
197 1.1 christos p->_CID,
198 1.1 christos eisaid_to_str(p->_CID),
199 1.1 christos hidstr,
200 1.1 christos uidstr,
201 1.1 christos cidstr);
202 1.1 christos }
203 1.1 christos }
204 1.1 christos
205 1.1 christos static inline void
206 1.1 christos devpath_acpi_adr(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
207 1.1 christos { /* See 10.3.3.1 */
208 1.1 christos struct { /* Sub-Type 3 */
209 1.1 christos devpath_t hdr; /* Length = 8; at least one _ADR */
210 1.1 christos uint32_t _ADR[];
211 1.1 christos } __packed *p = (void *)dp;
212 1.1 christos __CTASSERT(sizeof(*p) == 4);
213 1.1 christos char *tp;
214 1.1 christos size_t cnt;
215 1.1 christos
216 1.1 christos cnt = (p->hdr.Length - sizeof(p->hdr)) / sizeof(p->_ADR[0]);
217 1.1 christos
218 1.1 christos tp = estrdup("AcpiAdr(");
219 1.1 christos for (size_t i = 0; i < cnt; i++) {
220 1.1 christos path->sz = easprintf(&path->cp, "%s0x%08x%s", tp, p->_ADR[i],
221 1.1 christos i + 1 < cnt ? "," : "");
222 1.1 christos free(tp);
223 1.1 christos tp = path->cp;
224 1.1 christos }
225 1.1 christos path->sz = easprintf(&path->cp, "%s)", tp);
226 1.1 christos free(tp);
227 1.1 christos
228 1.1 christos if (dbg != NULL) {
229 1.1 christos dbg->sz = easprintf(&dbg->cp,
230 1.1 christos DEVPATH_FMT_HDR,
231 1.1 christos DEVPATH_DAT_HDR(dp));
232 1.1 christos tp = dbg->cp;
233 1.1 christos for (size_t i = 0; i < cnt; i++) {
234 1.1 christos dbg->sz = easprintf(&dbg->cp, "%s"
235 1.1 christos DEVPATH_FMT(_ADR[%zu]: 0x%08x\n),
236 1.1 christos tp, i, p->_ADR[i]);
237 1.1 christos free(tp);
238 1.1 christos tp = dbg->cp;
239 1.1 christos }
240 1.1 christos }
241 1.1 christos }
242 1.1 christos
243 1.1 christos static inline void
244 1.1 christos devpath_acpi_nvdimm(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
245 1.1 christos { /* See 10.3.3.2 */
246 1.1 christos struct { /* Sub-Type 4 */
247 1.1 christos devpath_t hdr; /* Length = 8 */
248 1.1 christos uint32_t NFIT_DevHdl;
249 1.1 christos } __packed *p = (void *)dp;
250 1.1 christos __CTASSERT(sizeof(*p) == 8);
251 1.1 christos
252 1.1 christos path->sz = easprintf(&path->cp, "NvdimmAcpiAdr(0x%x)", p->NFIT_DevHdl);
253 1.1 christos
254 1.1 christos if (dbg != NULL) {
255 1.1 christos dbg->sz = easprintf(&dbg->cp,
256 1.1 christos DEVPATH_FMT_HDR
257 1.1 christos DEVPATH_FMT(NFIT_DevHdl: 0x%08x\n),
258 1.1 christos DEVPATH_DAT_HDR(dp),
259 1.1 christos p->NFIT_DevHdl);
260 1.1 christos }
261 1.1 christos }
262 1.1 christos
263 1.2 christos #ifdef notdef
264 1.1 christos static inline void
265 1.1 christos devpath_acpi_unknown(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
266 1.1 christos {
267 1.1 christos
268 1.1 christos path->sz = easprintf(&path->cp, "Msg(%d,%s)", dp->SubType,
269 1.2 christos encode_data(((uint8_t *)dp) + 4, dp->Length - 4));
270 1.3 riastrad
271 1.1 christos if (dbg != NULL) {
272 1.1 christos dbg->sz = easprintf(&dbg->cp,
273 1.1 christos DEVPATH_FMT_HDR,
274 1.1 christos DEVPATH_DAT_HDR(dp));
275 1.1 christos }
276 1.1 christos }
277 1.2 christos #endif
278 1.1 christos
279 1.1 christos PUBLIC void
280 1.1 christos devpath_acpi(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
281 1.1 christos {
282 1.1 christos
283 1.1 christos assert(dp->Type = 2);
284 1.1 christos
285 1.1 christos switch (dp->SubType) {
286 1.1 christos case 1: devpath_acpi_acpi(dp, path, dbg); return;
287 1.1 christos case 2: devpath_acpi_acpiex(dp, path, dbg); return;
288 1.1 christos case 3: devpath_acpi_adr(dp, path, dbg); return;
289 1.1 christos case 4: devpath_acpi_nvdimm(dp, path, dbg); return;
290 1.1 christos default: devpath_unsupported(dp, path, dbg); return;
291 1.1 christos }
292 1.1 christos }
293