Home | History | Annotate | Line # | Download | only in efi
devpath2.c revision 1.2
      1  1.2  christos /* $NetBSD: devpath2.c,v 1.2 2025/02/27 17:26:56 christos 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.2  christos __RCSID("$NetBSD: devpath2.c,v 1.2 2025/02/27 17:26:56 christos 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 
     53  1.1  christos #define PNP0A03		0x0a0341d0
     54  1.1  christos #define PNP0A08		0x0a0841d0
     55  1.1  christos 
     56  1.1  christos static const char *
     57  1.1  christos eisaid_to_str(uint32_t eisaid)
     58  1.1  christos {
     59  1.1  christos 	static const char hexdigits[] = "0123456789ABCDEF";
     60  1.1  christos 	static char text[8];
     61  1.1  christos 	union {
     62  1.1  christos 		uint32_t eisaid;
     63  1.1  christos 		uint8_t b[4];
     64  1.1  christos 	} u;
     65  1.1  christos 
     66  1.1  christos 	u.eisaid = eisaid;
     67  1.1  christos 
     68  1.1  christos 	text[0]  = (char)__SHIFTOUT(u.b[0], __BITS(4,0));
     69  1.1  christos 	text[1]  = (char)__SHIFTOUT(u.b[0], __BITS(7,5));
     70  1.1  christos 	text[1] |= (char)__SHIFTOUT(u.b[1], __BITS(1,0)) << 3;
     71  1.1  christos 	text[2]  = (char)__SHIFTOUT(u.b[1], __BITS(7,2));
     72  1.1  christos 
     73  1.1  christos 	text[0] += 0x40;	/* '@' */
     74  1.1  christos 	text[1] += 0x40;
     75  1.1  christos 	text[2] += 0x40;
     76  1.1  christos 
     77  1.1  christos #define hi_nib(v)	(((v) >> 4) & 0x0f)
     78  1.1  christos #define lo_nib(v)	(((v) >> 0) & 0x0f)
     79  1.1  christos 	text[3] = hexdigits[hi_nib(u.b[3])];
     80  1.1  christos 	text[4] = hexdigits[lo_nib(u.b[3])];
     81  1.1  christos 	text[5] = hexdigits[hi_nib(u.b[2])];
     82  1.1  christos 	text[6] = hexdigits[lo_nib(u.b[2])];
     83  1.1  christos 	text[7] = 0;
     84  1.1  christos #undef lo_nib
     85  1.1  christos #undef hi_nib
     86  1.1  christos 
     87  1.1  christos 	return text;
     88  1.1  christos }
     89  1.1  christos 
     90  1.1  christos static inline const char *
     91  1.1  christos devpath_acpi_acpi_eisaid(uint32_t eisaid)
     92  1.1  christos {
     93  1.1  christos 
     94  1.1  christos #define PNP(v)	(0x000041d0 | ((v) << 16))
     95  1.1  christos 	switch (eisaid) {
     96  1.1  christos //	case PNP(0x0f03):	return "PS2Mouse";	/* legacy? */
     97  1.1  christos //	case PNP(0x0303):	return "FloppyPNP";	/* legacy? */
     98  1.1  christos 	case PNP(0x0a03):	return "PciRoot";
     99  1.1  christos 	case PNP(0x0a08):	return "PcieRoot";
    100  1.1  christos 	case PNP(0x0604):	return "Floppy";
    101  1.1  christos 	case PNP(0x0301):	return "Keyboard";
    102  1.1  christos 	case PNP(0x0501):	return "Serial";
    103  1.1  christos 	case PNP(0x0401):	return "ParallelPort";
    104  1.1  christos 	default:		return NULL;
    105  1.1  christos 	}
    106  1.1  christos #undef PNP
    107  1.1  christos }
    108  1.1  christos 
    109  1.1  christos static inline void
    110  1.1  christos devpath_acpi_acpi(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    111  1.1  christos {	/* See 10.3.3 */
    112  1.1  christos 	struct {			/* Sub-Type 1 */
    113  1.1  christos 		devpath_t	hdr;	/* Length = 12 */
    114  1.1  christos 		uint32_t	_HID;
    115  1.1  christos 		uint32_t	_UID;
    116  1.1  christos 	} __packed *p = (void *)dp;
    117  1.1  christos 	__CTASSERT(sizeof(*p) == 12);
    118  1.1  christos 	const char *str;
    119  1.1  christos 
    120  1.1  christos 	str = devpath_acpi_acpi_eisaid(p->_HID);
    121  1.1  christos 	if (str == NULL)
    122  1.1  christos 		path->sz = easprintf(&path->cp, "ACPI(%s,0x%x)", eisaid_to_str(p->_HID), p->_UID);
    123  1.1  christos 	else
    124  1.1  christos 		path->sz = easprintf(&path->cp, "%s(0x%x)", str, p->_UID);
    125  1.1  christos 
    126  1.1  christos 	if (dbg != NULL) {
    127  1.1  christos 		dbg->sz = easprintf(&dbg->cp,
    128  1.1  christos 		    DEVPATH_FMT_HDR
    129  1.1  christos 		    DEVPATH_FMT(_HID: 0x%08x(%s)\n)
    130  1.1  christos 		    DEVPATH_FMT(_UID: 0x%08x\n),
    131  1.1  christos 		    DEVPATH_DAT_HDR(dp),
    132  1.1  christos 		    p->_HID,
    133  1.1  christos 		    eisaid_to_str(p->_HID),
    134  1.1  christos 		    p->_UID);
    135  1.1  christos 	}
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos static inline void
    139  1.1  christos devpath_acpi_acpiex(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    140  1.1  christos {	/* See 10.3.3 */
    141  1.1  christos 	struct {			/* Sub-Type 2 */
    142  1.1  christos 		devpath_t	hdr;	/* Length = 19 + n */
    143  1.1  christos 		uint32_t	_HID;
    144  1.1  christos 		uint32_t	_UID;
    145  1.1  christos 		uint32_t	_CID;
    146  1.1  christos 		char		_HIDSTR[];	/* at least NUL byte */
    147  1.1  christos //		char		_UIDSTR[];	/* at least NUL byte */
    148  1.1  christos //		char		_CIDSTR[];	/* at least NUL byte */
    149  1.1  christos 	} __packed *p = (void *)dp;
    150  1.1  christos 	__CTASSERT(sizeof(*p) == 16);
    151  1.1  christos 	char *hidstr = p->_HIDSTR;
    152  1.1  christos 	char *uidstr = hidstr + strlen(hidstr) + 1;
    153  1.1  christos 	char *cidstr = uidstr + strlen(uidstr) + 1;
    154  1.1  christos 
    155  1.1  christos #if 0
    156  1.1  christos 	There are 4 different subsubtypes depending on the ?ID, ?IDSTR values.
    157  1.1  christos 
    158  1.1  christos 	"AcpiEx"
    159  1.1  christos 	"AcpiExp"
    160  1.1  christos 	"PciRoot"
    161  1.1  christos 	"PcipRoot"
    162  1.1  christos #endif
    163  1.1  christos 	if (p->_HID == PNP0A08 || p->_CID == PNP0A08) {
    164  1.1  christos 		// PcieRoot(UID|UIDSTR)
    165  1.1  christos 		path->sz = easprintf(&path->cp, "PcieRoot(%s)",
    166  1.1  christos 		    *uidstr != '\0' ? uidstr : eisaid_to_str(p->_UID));
    167  1.1  christos 	}
    168  1.1  christos 	else if (p->_HID == PNP0A03 || p->_CID == PNP0A03) {
    169  1.1  christos 		assert(p->_HID != PNP0A08);
    170  1.1  christos 		// PciRoot(UID|UIDSTR)
    171  1.1  christos 		path->sz = easprintf(&path->cp, "PciRoot(%s)",
    172  1.1  christos 		    *uidstr != '\0' ? uidstr : eisaid_to_str(p->_UID));
    173  1.1  christos 	}
    174  1.1  christos 	else if (hidstr[0] == '\0' && cidstr[0] == '\0' && uidstr[0] != '\0') {
    175  1.1  christos 		assert(p->_HID != 0);
    176  1.1  christos 		path->sz = easprintf(&path->cp, "AcpiExp(%s,%s,%s)", eisaid_to_str(p->_HID),
    177  1.1  christos 		    eisaid_to_str(p->_CID), uidstr);
    178  1.1  christos 	}
    179  1.1  christos 	else {
    180  1.1  christos 		path->sz = easprintf(&path->cp, "ACPIEX(%s,%s,0x%x,%s,%s,%s)",
    181  1.1  christos 		    eisaid_to_str(p->_HID), eisaid_to_str(p->_CID),
    182  1.1  christos 		    p->_UID, hidstr, cidstr, uidstr);
    183  1.1  christos 	}
    184  1.1  christos 
    185  1.1  christos 	if (dbg != NULL) {
    186  1.1  christos 		dbg->sz = easprintf(&dbg->cp,
    187  1.1  christos 		    DEVPATH_FMT_HDR
    188  1.1  christos 		    DEVPATH_FMT(_HID: 0x%08x(%s)\n)
    189  1.1  christos 		    DEVPATH_FMT(_UID: 0x%08x\n)
    190  1.1  christos 		    DEVPATH_FMT(_CID: 0x%08x(%s)\n)
    191  1.1  christos 		    DEVPATH_FMT(_HIDSTR: %s\n)
    192  1.1  christos 		    DEVPATH_FMT(_UIDSTR: %s\n)
    193  1.1  christos 		    DEVPATH_FMT(_CIDSTR: %s\n),
    194  1.1  christos 		    DEVPATH_DAT_HDR(dp),
    195  1.1  christos 		    p->_HID,
    196  1.1  christos 		    eisaid_to_str(p->_HID),
    197  1.1  christos 		    p->_UID,
    198  1.1  christos 		    p->_CID,
    199  1.1  christos 		    eisaid_to_str(p->_CID),
    200  1.1  christos 		    hidstr,
    201  1.1  christos 		    uidstr,
    202  1.1  christos 		    cidstr);
    203  1.1  christos 	}
    204  1.1  christos }
    205  1.1  christos 
    206  1.1  christos static inline void
    207  1.1  christos devpath_acpi_adr(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    208  1.1  christos {	/* See 10.3.3.1 */
    209  1.1  christos 	struct {		/* Sub-Type 3 */
    210  1.1  christos 		devpath_t	hdr;	/* Length = 8; at least one _ADR */
    211  1.1  christos 		uint32_t	_ADR[];
    212  1.1  christos 	} __packed *p = (void *)dp;
    213  1.1  christos 	__CTASSERT(sizeof(*p) == 4);
    214  1.1  christos 	char *tp;
    215  1.1  christos 	size_t cnt;
    216  1.1  christos 
    217  1.1  christos 	cnt = (p->hdr.Length - sizeof(p->hdr)) / sizeof(p->_ADR[0]);
    218  1.1  christos 
    219  1.1  christos 	tp = estrdup("AcpiAdr(");
    220  1.1  christos 	for (size_t i = 0; i < cnt; i++) {
    221  1.1  christos 		path->sz = easprintf(&path->cp, "%s0x%08x%s", tp, p->_ADR[i],
    222  1.1  christos 		    i + 1 < cnt ? "," : "");
    223  1.1  christos 		free(tp);
    224  1.1  christos 		tp = path->cp;
    225  1.1  christos 	}
    226  1.1  christos 	path->sz = easprintf(&path->cp, "%s)", tp);
    227  1.1  christos 	free(tp);
    228  1.1  christos 
    229  1.1  christos 	if (dbg != NULL) {
    230  1.1  christos 		dbg->sz = easprintf(&dbg->cp,
    231  1.1  christos 		    DEVPATH_FMT_HDR,
    232  1.1  christos 		    DEVPATH_DAT_HDR(dp));
    233  1.1  christos 		tp = dbg->cp;
    234  1.1  christos 		for (size_t i = 0; i < cnt; i++) {
    235  1.1  christos 			dbg->sz = easprintf(&dbg->cp, "%s"
    236  1.1  christos 			    DEVPATH_FMT(_ADR[%zu]: 0x%08x\n),
    237  1.1  christos 			    tp, i, p->_ADR[i]);
    238  1.1  christos 			free(tp);
    239  1.1  christos 			tp = dbg->cp;
    240  1.1  christos 		}
    241  1.1  christos 	}
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos static inline void
    245  1.1  christos devpath_acpi_nvdimm(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    246  1.1  christos {	/* See 10.3.3.2 */
    247  1.1  christos 	struct {		/* Sub-Type 4 */
    248  1.1  christos 		devpath_t	hdr;	/* Length = 8 */
    249  1.1  christos 		uint32_t	NFIT_DevHdl;
    250  1.1  christos 	} __packed *p = (void *)dp;
    251  1.1  christos 	__CTASSERT(sizeof(*p) == 8);
    252  1.1  christos 
    253  1.1  christos 	path->sz = easprintf(&path->cp, "NvdimmAcpiAdr(0x%x)", p->NFIT_DevHdl);
    254  1.1  christos 
    255  1.1  christos 	if (dbg != NULL) {
    256  1.1  christos 		dbg->sz = easprintf(&dbg->cp,
    257  1.1  christos 		    DEVPATH_FMT_HDR
    258  1.1  christos 		    DEVPATH_FMT(NFIT_DevHdl: 0x%08x\n),
    259  1.1  christos 		    DEVPATH_DAT_HDR(dp),
    260  1.1  christos 		    p->NFIT_DevHdl);
    261  1.1  christos 	}
    262  1.1  christos }
    263  1.1  christos 
    264  1.2  christos #ifdef notdef
    265  1.1  christos static inline void
    266  1.1  christos devpath_acpi_unknown(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    267  1.1  christos {
    268  1.1  christos 
    269  1.1  christos 	path->sz = easprintf(&path->cp, "Msg(%d,%s)", dp->SubType,
    270  1.2  christos 	    encode_data(((uint8_t *)dp) + 4, dp->Length - 4));
    271  1.1  christos 
    272  1.1  christos 	if (dbg != NULL) {
    273  1.1  christos 		dbg->sz = easprintf(&dbg->cp,
    274  1.1  christos 		    DEVPATH_FMT_HDR,
    275  1.1  christos 		    DEVPATH_DAT_HDR(dp));
    276  1.1  christos 	}
    277  1.1  christos }
    278  1.2  christos #endif
    279  1.1  christos 
    280  1.1  christos PUBLIC void
    281  1.1  christos devpath_acpi(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    282  1.1  christos {
    283  1.1  christos 
    284  1.1  christos 	assert(dp->Type = 2);
    285  1.1  christos 
    286  1.1  christos 	switch (dp->SubType) {
    287  1.1  christos 	case 1:   devpath_acpi_acpi(dp, path, dbg);	return;
    288  1.1  christos 	case 2:   devpath_acpi_acpiex(dp, path, dbg);	return;
    289  1.1  christos 	case 3:   devpath_acpi_adr(dp, path, dbg);	return;
    290  1.1  christos 	case 4:   devpath_acpi_nvdimm(dp, path, dbg);	return;
    291  1.1  christos 	default:  devpath_unsupported(dp, path, dbg);	return;
    292  1.1  christos 	}
    293  1.1  christos }
    294