1 1.6 riastrad /* $NetBSD: devpath2.c,v 1.6 2025/03/02 14:18:04 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.6 riastrad __RCSID("$NetBSD: devpath2.c,v 1.6 2025/03/02 14:18:04 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.4 riastrad if (str == NULL) { 121 1.4 riastrad path->sz = easprintf(&path->cp, "ACPI(%s,0x%x)", 122 1.4 riastrad eisaid_to_str(p->_HID), p->_UID); 123 1.4 riastrad } else { 124 1.1 christos path->sz = easprintf(&path->cp, "%s(0x%x)", str, p->_UID); 125 1.4 riastrad } 126 1.1 christos 127 1.1 christos if (dbg != NULL) { 128 1.1 christos dbg->sz = easprintf(&dbg->cp, 129 1.1 christos DEVPATH_FMT_HDR 130 1.1 christos DEVPATH_FMT(_HID: 0x%08x(%s)\n) 131 1.1 christos DEVPATH_FMT(_UID: 0x%08x\n), 132 1.1 christos DEVPATH_DAT_HDR(dp), 133 1.1 christos p->_HID, 134 1.1 christos eisaid_to_str(p->_HID), 135 1.1 christos p->_UID); 136 1.1 christos } 137 1.1 christos } 138 1.1 christos 139 1.1 christos static inline void 140 1.1 christos devpath_acpi_acpiex(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 141 1.1 christos { /* See 10.3.3 */ 142 1.1 christos struct { /* Sub-Type 2 */ 143 1.1 christos devpath_t hdr; /* Length = 19 + n */ 144 1.1 christos uint32_t _HID; 145 1.1 christos uint32_t _UID; 146 1.1 christos uint32_t _CID; 147 1.1 christos char _HIDSTR[]; /* at least NUL byte */ 148 1.1 christos // char _UIDSTR[]; /* at least NUL byte */ 149 1.1 christos // char _CIDSTR[]; /* at least NUL byte */ 150 1.1 christos } __packed *p = (void *)dp; 151 1.1 christos __CTASSERT(sizeof(*p) == 16); 152 1.1 christos char *hidstr = p->_HIDSTR; 153 1.1 christos char *uidstr = hidstr + strlen(hidstr) + 1; 154 1.1 christos char *cidstr = uidstr + strlen(uidstr) + 1; 155 1.1 christos 156 1.1 christos #if 0 157 1.1 christos There are 4 different subsubtypes depending on the ?ID, ?IDSTR values. 158 1.1 christos 159 1.1 christos "AcpiEx" 160 1.1 christos "AcpiExp" 161 1.1 christos "PciRoot" 162 1.1 christos "PcipRoot" 163 1.1 christos #endif 164 1.1 christos if (p->_HID == PNP0A08 || p->_CID == PNP0A08) { 165 1.1 christos // PcieRoot(UID|UIDSTR) 166 1.1 christos path->sz = easprintf(&path->cp, "PcieRoot(%s)", 167 1.1 christos *uidstr != '\0' ? uidstr : eisaid_to_str(p->_UID)); 168 1.1 christos } 169 1.1 christos else if (p->_HID == PNP0A03 || p->_CID == PNP0A03) { 170 1.1 christos assert(p->_HID != PNP0A08); 171 1.1 christos // PciRoot(UID|UIDSTR) 172 1.1 christos path->sz = easprintf(&path->cp, "PciRoot(%s)", 173 1.1 christos *uidstr != '\0' ? uidstr : eisaid_to_str(p->_UID)); 174 1.1 christos } 175 1.1 christos else if (hidstr[0] == '\0' && cidstr[0] == '\0' && uidstr[0] != '\0') { 176 1.1 christos assert(p->_HID != 0); 177 1.5 riastrad path->sz = easprintf(&path->cp, "AcpiExp(%s,%s,%s)", 178 1.5 riastrad eisaid_to_str(p->_HID), 179 1.5 riastrad eisaid_to_str(p->_CID), 180 1.5 riastrad uidstr); 181 1.1 christos } 182 1.1 christos else { 183 1.1 christos path->sz = easprintf(&path->cp, "ACPIEX(%s,%s,0x%x,%s,%s,%s)", 184 1.1 christos eisaid_to_str(p->_HID), eisaid_to_str(p->_CID), 185 1.1 christos p->_UID, hidstr, cidstr, uidstr); 186 1.1 christos } 187 1.1 christos 188 1.1 christos if (dbg != NULL) { 189 1.1 christos dbg->sz = easprintf(&dbg->cp, 190 1.1 christos DEVPATH_FMT_HDR 191 1.1 christos DEVPATH_FMT(_HID: 0x%08x(%s)\n) 192 1.1 christos DEVPATH_FMT(_UID: 0x%08x\n) 193 1.1 christos DEVPATH_FMT(_CID: 0x%08x(%s)\n) 194 1.1 christos DEVPATH_FMT(_HIDSTR: %s\n) 195 1.1 christos DEVPATH_FMT(_UIDSTR: %s\n) 196 1.1 christos DEVPATH_FMT(_CIDSTR: %s\n), 197 1.1 christos DEVPATH_DAT_HDR(dp), 198 1.1 christos p->_HID, 199 1.1 christos eisaid_to_str(p->_HID), 200 1.1 christos p->_UID, 201 1.1 christos p->_CID, 202 1.1 christos eisaid_to_str(p->_CID), 203 1.1 christos hidstr, 204 1.1 christos uidstr, 205 1.1 christos cidstr); 206 1.1 christos } 207 1.1 christos } 208 1.1 christos 209 1.1 christos static inline void 210 1.1 christos devpath_acpi_adr(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 211 1.1 christos { /* See 10.3.3.1 */ 212 1.1 christos struct { /* Sub-Type 3 */ 213 1.1 christos devpath_t hdr; /* Length = 8; at least one _ADR */ 214 1.1 christos uint32_t _ADR[]; 215 1.1 christos } __packed *p = (void *)dp; 216 1.1 christos __CTASSERT(sizeof(*p) == 4); 217 1.1 christos char *tp; 218 1.1 christos size_t cnt; 219 1.1 christos 220 1.1 christos cnt = (p->hdr.Length - sizeof(p->hdr)) / sizeof(p->_ADR[0]); 221 1.1 christos 222 1.1 christos tp = estrdup("AcpiAdr("); 223 1.1 christos for (size_t i = 0; i < cnt; i++) { 224 1.1 christos path->sz = easprintf(&path->cp, "%s0x%08x%s", tp, p->_ADR[i], 225 1.1 christos i + 1 < cnt ? "," : ""); 226 1.1 christos free(tp); 227 1.1 christos tp = path->cp; 228 1.1 christos } 229 1.1 christos path->sz = easprintf(&path->cp, "%s)", tp); 230 1.1 christos free(tp); 231 1.1 christos 232 1.1 christos if (dbg != NULL) { 233 1.1 christos dbg->sz = easprintf(&dbg->cp, 234 1.1 christos DEVPATH_FMT_HDR, 235 1.1 christos DEVPATH_DAT_HDR(dp)); 236 1.1 christos tp = dbg->cp; 237 1.1 christos for (size_t i = 0; i < cnt; i++) { 238 1.1 christos dbg->sz = easprintf(&dbg->cp, "%s" 239 1.1 christos DEVPATH_FMT(_ADR[%zu]: 0x%08x\n), 240 1.1 christos tp, i, p->_ADR[i]); 241 1.1 christos free(tp); 242 1.1 christos tp = dbg->cp; 243 1.1 christos } 244 1.1 christos } 245 1.1 christos } 246 1.1 christos 247 1.1 christos static inline void 248 1.1 christos devpath_acpi_nvdimm(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 249 1.1 christos { /* See 10.3.3.2 */ 250 1.1 christos struct { /* Sub-Type 4 */ 251 1.1 christos devpath_t hdr; /* Length = 8 */ 252 1.1 christos uint32_t NFIT_DevHdl; 253 1.1 christos } __packed *p = (void *)dp; 254 1.1 christos __CTASSERT(sizeof(*p) == 8); 255 1.1 christos 256 1.1 christos path->sz = easprintf(&path->cp, "NvdimmAcpiAdr(0x%x)", p->NFIT_DevHdl); 257 1.1 christos 258 1.1 christos if (dbg != NULL) { 259 1.1 christos dbg->sz = easprintf(&dbg->cp, 260 1.1 christos DEVPATH_FMT_HDR 261 1.1 christos DEVPATH_FMT(NFIT_DevHdl: 0x%08x\n), 262 1.1 christos DEVPATH_DAT_HDR(dp), 263 1.1 christos p->NFIT_DevHdl); 264 1.1 christos } 265 1.1 christos } 266 1.1 christos 267 1.2 christos #ifdef notdef 268 1.1 christos static inline void 269 1.1 christos devpath_acpi_unknown(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 270 1.1 christos { 271 1.1 christos 272 1.1 christos path->sz = easprintf(&path->cp, "Msg(%d,%s)", dp->SubType, 273 1.6 riastrad encode_data((uint8_t *)(dp + 1), dp->Length - sizeof(*dp))); 274 1.3 riastrad 275 1.1 christos if (dbg != NULL) { 276 1.1 christos dbg->sz = easprintf(&dbg->cp, 277 1.1 christos DEVPATH_FMT_HDR, 278 1.1 christos DEVPATH_DAT_HDR(dp)); 279 1.1 christos } 280 1.1 christos } 281 1.2 christos #endif 282 1.1 christos 283 1.1 christos PUBLIC void 284 1.1 christos devpath_acpi(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 285 1.1 christos { 286 1.1 christos 287 1.1 christos assert(dp->Type = 2); 288 1.1 christos 289 1.1 christos switch (dp->SubType) { 290 1.1 christos case 1: devpath_acpi_acpi(dp, path, dbg); return; 291 1.1 christos case 2: devpath_acpi_acpiex(dp, path, dbg); return; 292 1.1 christos case 3: devpath_acpi_adr(dp, path, dbg); return; 293 1.1 christos case 4: devpath_acpi_nvdimm(dp, path, dbg); return; 294 1.1 christos default: devpath_unsupported(dp, path, dbg); return; 295 1.1 christos } 296 1.1 christos } 297