Home | History | Annotate | Line # | Download | only in efi
      1  1.3  riastrad /* $NetBSD: devpath.h,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 #ifndef _DEVPATH_H_
     27  1.1  christos #define _DEVPATH_H_
     28  1.1  christos 
     29  1.1  christos #ifndef lint
     30  1.3  riastrad __RCSID("$NetBSD: devpath.h,v 1.3 2025/03/02 00:03:41 riastradh Exp $");
     31  1.1  christos #endif /* not lint */
     32  1.1  christos 
     33  1.1  christos #include <stdlib.h>
     34  1.1  christos #include <util.h>
     35  1.1  christos 
     36  1.1  christos typedef struct EFI_DEVICE_PATH_PROTOCOL {
     37  1.1  christos 	uint8_t Type;
     38  1.1  christos 	uint8_t SubType;
     39  1.1  christos //	uint8_t Length[2];
     40  1.1  christos 	uint16_t Length;
     41  1.2  christos //	uint8_t	Data[];
     42  1.1  christos } EFI_DEVICE_PATH_PROTOCOL;
     43  1.1  christos 
     44  1.1  christos typedef EFI_DEVICE_PATH_PROTOCOL devpath_t;
     45  1.1  christos 
     46  1.1  christos typedef struct devpath_elm {
     47  1.1  christos 	size_t sz;
     48  1.1  christos 	char *cp;
     49  1.1  christos } devpath_elm_t;
     50  1.1  christos 
     51  1.1  christos enum {
     52  1.1  christos 	DEVPATH_TYPE_HW    = 1,
     53  1.1  christos 	DEVPATH_TYPE_ACPI  = 2,
     54  1.1  christos 	DEVPATH_TYPE_MSG   = 3,
     55  1.1  christos 	DEVPATH_TYPE_MEDIA = 4,
     56  1.1  christos 	DEVPATH_TYPE_BIOS  = 5,
     57  1.1  christos 	DEVPATH_TYPE_END   = 0x7f,
     58  1.3  riastrad };
     59  1.1  christos 
     60  1.1  christos #define DEVPATH_END_ALL \
     61  1.1  christos   ((EFI_DEVICE_PATH_PROTOCOL){.Type = 0x7f, .SubType = 0xff, .Length = 0x04})
     62  1.1  christos #define DEVPATH_END_SEG \
     63  1.1  christos   ((EFI_DEVICE_PATH_PROTOCOL){.Type = 0x7f, .SubType = 0x01, .Length = 0x04})
     64  1.1  christos 
     65  1.1  christos /*
     66  1.1  christos  * For debugging
     67  1.1  christos  */
     68  1.1  christos #define DEVPATH_FMT_HDR	"  DevPath_Hdr: Type %u, SubType: %u, Length: %u\n"
     69  1.1  christos #define DEVPATH_FMT_PFX	"    "
     70  1.1  christos #define DEVPATH_FMT(s)	DEVPATH_FMT_PFX #s
     71  1.1  christos #define DEVPATH_DAT_HDR(dp)	(dp)->Type, (dp)->SubType, (dp)->Length
     72  1.1  christos 
     73  1.1  christos static inline char *
     74  1.1  christos aconcat(char *bp1, const char *sep, char *bp2)
     75  1.1  christos {
     76  1.1  christos 	char *bp;
     77  1.1  christos 
     78  1.1  christos 	easprintf(&bp, "%s%s%s", bp1, sep, bp2);
     79  1.1  christos 	free(bp1);
     80  1.1  christos 	free(bp2);
     81  1.1  christos 	return bp;
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos static inline const char *
     85  1.1  christos devpath_type_name(size_t type)
     86  1.1  christos {
     87  1.1  christos 	static const char *type_tbl[] = {
     88  1.1  christos 		[DEVPATH_TYPE_HW]    = "HW",
     89  1.1  christos 		[DEVPATH_TYPE_ACPI]  = "ACPI",
     90  1.1  christos 		[DEVPATH_TYPE_MSG]   = "MSG",
     91  1.1  christos 		[DEVPATH_TYPE_MEDIA] = "MEDIA",
     92  1.1  christos 		[DEVPATH_TYPE_BIOS]  = "BIOS",
     93  1.1  christos 	};
     94  1.1  christos 
     95  1.1  christos 	if (type == DEVPATH_TYPE_END)
     96  1.1  christos 		return "END";
     97  1.1  christos 
     98  1.1  christos 	if (type > __arraycount(type_tbl) || type == 0)
     99  1.1  christos 		return "UNKNOWN";
    100  1.1  christos 
    101  1.1  christos 	return type_tbl[type];
    102  1.1  christos 
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos static inline void
    106  1.1  christos devpath_hdr(devpath_t *dp, devpath_elm_t *elm)
    107  1.1  christos {
    108  1.3  riastrad 
    109  1.1  christos 	elm->sz = (size_t)easprintf(&elm->cp,
    110  1.1  christos 	    DEVPATH_FMT_HDR, DEVPATH_DAT_HDR(dp));
    111  1.1  christos }
    112  1.1  christos 
    113  1.1  christos static inline void
    114  1.1  christos devpath_unsupported(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
    115  1.1  christos {
    116  1.1  christos 
    117  1.1  christos 	path->sz = (size_t)easprintf(&path->cp,
    118  1.1  christos 	    "[unsupported devpath type: %s(%u), subtype: %u]",
    119  1.1  christos 	    devpath_type_name(dp->Type), dp->Type, dp->SubType);
    120  1.1  christos 
    121  1.1  christos 	if (dbg != NULL)
    122  1.1  christos 		devpath_hdr(dp, dbg);
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos char *devpath_parse(devpath_t *, size_t, char **);
    126  1.1  christos 
    127  1.1  christos #endif /* _DEVPATH_H_ */
    128