devpath1.c revision 1.1 1 1.1 christos /* $NetBSD: devpath1.c,v 1.1 2025/02/24 13:47: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.1 christos __RCSID("$NetBSD: devpath1.c,v 1.1 2025/02/24 13:47:56 christos Exp $");
29 1.1 christos #endif /* not lint */
30 1.1 christos
31 1.1 christos #include <sys/uuid.h>
32 1.1 christos
33 1.1 christos #include <assert.h>
34 1.1 christos #include <stdio.h>
35 1.1 christos #include <util.h>
36 1.1 christos
37 1.1 christos #include "defs.h"
38 1.1 christos #include "devpath.h"
39 1.1 christos #include "devpath1.h"
40 1.1 christos #include "utils.h"
41 1.1 christos
42 1.1 christos #define easprintf (size_t)easprintf
43 1.1 christos
44 1.1 christos /************************************************************************
45 1.1 christos * Type 1 - PCI Device Path
46 1.1 christos ************************************************************************/
47 1.1 christos
48 1.1 christos /*
49 1.1 christos * XXX: I can't find this GUID documented anywhere online. I snarfed
50 1.1 christos * it from
51 1.1 christos * https://github.com/rhboot/efivar::efivar-main/src/include/efivar/efivar-dp.h
52 1.1 christos * Comment out the define if you don't want to use it.
53 1.1 christos */
54 1.1 christos /* Used in subtype 4 */
55 1.1 christos #if 1
56 1.1 christos #define EFI_EDD10_PATH_GUID\
57 1.1 christos ((uuid_t){0xcf31fac5,0xc24e,0x11d2,0x85,0xf3,{0x00,0xa0,0xc9,0x3e,0xc9,0x3b}})
58 1.1 christos #endif
59 1.1 christos
60 1.1 christos #define EFI_MEMORY_TYPE \
61 1.1 christos _X(ReservedMemoryType) \
62 1.1 christos _X(LoaderCode) \
63 1.1 christos _X(LoaderData) \
64 1.1 christos _X(BootServicesCode) \
65 1.1 christos _X(BootServicesData) \
66 1.1 christos _X(RuntimeServicesCode) \
67 1.1 christos _X(RuntimeServicesData) \
68 1.1 christos _X(ConventionalMemory) \
69 1.1 christos _X(UnusableMemory) \
70 1.1 christos _X(ACPIReclaimMemory) \
71 1.1 christos _X(ACPIMemoryNVS) \
72 1.1 christos _X(MemoryMappedIO) \
73 1.1 christos _X(MemoryMappedIOPortSpace) \
74 1.1 christos _X(PalCode) \
75 1.1 christos _X(PersistentMemory) \
76 1.1 christos _X(UnacceptedMemoryType) \
77 1.1 christos _X(MaxMemoryTyp)
78 1.1 christos
79 1.1 christos typedef enum {
80 1.1 christos #define _X(n) Efi ## n,
81 1.1 christos EFI_MEMORY_TYPE
82 1.1 christos #undef _X
83 1.1 christos } EfiMemoryType_t;
84 1.1 christos
85 1.1 christos static const char *
86 1.1 christos efi_memory_type_name(EfiMemoryType_t t)
87 1.1 christos {
88 1.1 christos static const char *memtype[] = {
89 1.1 christos #define _X(n) #n,
90 1.1 christos EFI_MEMORY_TYPE
91 1.1 christos #undef _X
92 1.1 christos };
93 1.1 christos
94 1.1 christos if (t >= __arraycount(memtype))
95 1.1 christos return "Unknown";
96 1.1 christos
97 1.1 christos return memtype[t];
98 1.1 christos }
99 1.1 christos
100 1.1 christos /****************************************/
101 1.1 christos
102 1.1 christos static inline void
103 1.1 christos devpath_hw_pci(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
104 1.1 christos { /* See 10.3.2.2 */
105 1.1 christos struct { /* Sub-Type 1 */
106 1.1 christos devpath_t hdr; /* Length = 6 */
107 1.1 christos uint8_t FunctionNum;
108 1.1 christos uint8_t DeviceNum;
109 1.1 christos } __packed *p = (void *)dp;
110 1.1 christos __CTASSERT(sizeof(*p) == 6);
111 1.1 christos
112 1.1 christos path->sz = easprintf(&path->cp, "Pci(0x%x,0x%x)",
113 1.1 christos p->DeviceNum, p->FunctionNum);
114 1.1 christos
115 1.1 christos if (dbg != NULL) {
116 1.1 christos dbg->sz = easprintf(&dbg->cp,
117 1.1 christos DEVPATH_FMT_HDR
118 1.1 christos DEVPATH_FMT(FunctionNum: %u\n)
119 1.1 christos DEVPATH_FMT(DeviceNum: %u\n),
120 1.1 christos DEVPATH_DAT_HDR(dp),
121 1.1 christos p->FunctionNum,
122 1.1 christos p->DeviceNum);
123 1.1 christos }
124 1.1 christos }
125 1.1 christos
126 1.1 christos static inline void
127 1.1 christos devpath_hw_pccard(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
128 1.1 christos { /* See 10.3.2.2 */
129 1.1 christos struct { /* Sub-Type 2 */
130 1.1 christos devpath_t hdr; /* Length = 5 */
131 1.1 christos uint8_t FunctionNum;
132 1.1 christos } __packed *p = (void *)dp;
133 1.1 christos __CTASSERT(sizeof(*p) == 5);
134 1.1 christos
135 1.1 christos path->sz = easprintf(&path->cp, "PcCard(0x%x)", p->FunctionNum);
136 1.1 christos
137 1.1 christos if (dbg != NULL) {
138 1.1 christos dbg->sz = easprintf(&dbg->cp,
139 1.1 christos DEVPATH_FMT_HDR
140 1.1 christos DEVPATH_FMT(FunctionNum: %u\n),
141 1.1 christos DEVPATH_DAT_HDR(dp),
142 1.1 christos p->FunctionNum);
143 1.1 christos }
144 1.1 christos }
145 1.1 christos
146 1.1 christos static inline void
147 1.1 christos devpath_hw_memmap(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
148 1.1 christos { /* See 10.3.2.3 */
149 1.1 christos struct { /* Sub-Type 3 */
150 1.1 christos devpath_t hdr; /* Length = 24 */
151 1.1 christos uint32_t MemoryType;
152 1.1 christos uint64_t StartAddress;
153 1.1 christos uint64_t EndAddress;
154 1.1 christos } __packed *p = (void *)dp;
155 1.1 christos __CTASSERT(sizeof(*p) == 24);
156 1.1 christos const char *typename;
157 1.1 christos
158 1.1 christos typename = efi_memory_type_name(p->MemoryType);
159 1.1 christos
160 1.1 christos path->sz = easprintf(&path->cp, "MemMap(%s,0x%016tx,0x%016tx)", typename,
161 1.1 christos p->StartAddress, p->EndAddress);
162 1.1 christos
163 1.1 christos if (dbg != NULL) {
164 1.1 christos dbg->sz = easprintf(&dbg->cp,
165 1.1 christos DEVPATH_FMT_HDR
166 1.1 christos DEVPATH_FMT(MemoryType: 0x%08x(%s)\n)
167 1.1 christos DEVPATH_FMT(StartAddress: 0x%016tx\n)
168 1.1 christos DEVPATH_FMT(EndAddress: 0x%016tx\n),
169 1.1 christos DEVPATH_DAT_HDR(dp),
170 1.1 christos p->MemoryType, typename,
171 1.1 christos p->StartAddress,
172 1.1 christos p->EndAddress);
173 1.1 christos
174 1.1 christos }
175 1.1 christos }
176 1.1 christos
177 1.1 christos #ifdef EFI_EDD10_PATH_GUID
178 1.1 christos static inline void
179 1.1 christos devpath_hw_edd10(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
180 1.1 christos { /* See unknown - snarfed from efivar-main.zip */
181 1.1 christos struct { /* Sub-Type 4 */
182 1.1 christos devpath_t hdr; /* Length = 24 */
183 1.1 christos uuid_t GUID;
184 1.1 christos uint32_t devnum;
185 1.1 christos } __packed *p = (void *)dp;
186 1.1 christos __CTASSERT(sizeof(*p) == 24);
187 1.1 christos
188 1.1 christos assert(p->hdr.Length == 24);
189 1.1 christos
190 1.1 christos /*
191 1.1 christos * p->data will be printed by debug
192 1.1 christos */
193 1.1 christos path->sz = easprintf(&path->cp, "EDD10(0x%02x)", p->devnum);
194 1.1 christos
195 1.1 christos if (dbg != NULL) {
196 1.1 christos char uuid_str[UUID_STR_LEN];
197 1.1 christos
198 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->GUID);
199 1.1 christos dbg->sz = easprintf(&dbg->cp,
200 1.1 christos DEVPATH_FMT_HDR
201 1.1 christos DEVPATH_FMT(GUID: %s\n)
202 1.1 christos DEVPATH_FMT(devnum: 0x%08x\n),
203 1.1 christos DEVPATH_DAT_HDR(dp),
204 1.1 christos uuid_str,
205 1.1 christos p->devnum);
206 1.1 christos }
207 1.1 christos }
208 1.1 christos #endif /* EFI_EDD10_PATH_GUID */
209 1.1 christos
210 1.1 christos static inline void
211 1.1 christos devpath_hw_vendor(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
212 1.1 christos { /* See 10.3.2.4 */
213 1.1 christos struct { /* Sub-Type 4 */
214 1.1 christos devpath_t hdr; /* Length = 20 + n */
215 1.1 christos uuid_t GUID;
216 1.1 christos uint8_t data[];
217 1.1 christos } __packed *p = (void *)dp;
218 1.1 christos __CTASSERT(sizeof(*p) == 20);
219 1.1 christos char uuid_str[UUID_STR_LEN];
220 1.1 christos
221 1.1 christos #ifdef EFI_EDD10_PATH_GUID
222 1.1 christos if (memcmp(&p->GUID, &EFI_EDD10_PATH_GUID, sizeof(uuid_t)) == 0) {
223 1.1 christos devpath_hw_edd10(dp, path, dbg);
224 1.1 christos return;
225 1.1 christos }
226 1.1 christos #endif /* EFI_EDD10_PATH_GUID */
227 1.1 christos
228 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->GUID);
229 1.1 christos
230 1.1 christos /*
231 1.1 christos * p->data will be printed by debug
232 1.1 christos */
233 1.1 christos path->sz = easprintf(&path->cp, "VenHw(%s)", uuid_str);
234 1.1 christos
235 1.1 christos if (dbg != NULL) {
236 1.1 christos dbg->sz = easprintf(&dbg->cp,
237 1.1 christos DEVPATH_FMT_HDR
238 1.1 christos DEVPATH_FMT(GUID: %s\n),
239 1.1 christos DEVPATH_DAT_HDR(dp),
240 1.1 christos uuid_str);
241 1.1 christos }
242 1.1 christos }
243 1.1 christos
244 1.1 christos static inline void
245 1.1 christos devpath_hw_controller(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
246 1.1 christos { /* See 10.3.2.5 */
247 1.1 christos struct { /* Sub-Type 5 */
248 1.1 christos devpath_t hdr; /* Length = 8 */
249 1.1 christos uint32_t CtrlNum;
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, "Ctrl(0x%x)", p->CtrlNum);
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(CtrlNum: 0x%x\n),
259 1.1 christos DEVPATH_DAT_HDR(dp),
260 1.1 christos p->CtrlNum);
261 1.1 christos }
262 1.1 christos }
263 1.1 christos
264 1.1 christos static inline const char *
265 1.1 christos devpath_hw_bmc_iftype(uint type)
266 1.1 christos {
267 1.1 christos static const char *tbl[] = {
268 1.1 christos [0] = "Unknown",
269 1.1 christos [1] = "KCS", // Keyboard Controller Style
270 1.1 christos [2] = "SMIC", // Server Management Interface Chip
271 1.1 christos [3] = "BT", // Block Transfer
272 1.1 christos };
273 1.1 christos
274 1.1 christos if (type >= __arraycount(tbl))
275 1.1 christos type = 0;
276 1.1 christos
277 1.1 christos return tbl[type];
278 1.1 christos }
279 1.1 christos
280 1.1 christos /* Baseboard Management Controller */
281 1.1 christos static inline void
282 1.1 christos devpath_hw_bmc(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
283 1.1 christos { /* See 10.3.2.6 */
284 1.1 christos struct { /* Sub-Type 6 */
285 1.1 christos devpath_t hdr; /* Length = 13 */
286 1.1 christos uint8_t IfaceType;
287 1.1 christos uint64_t BaseAddress;
288 1.1 christos } __packed *p = (void *)dp;
289 1.1 christos __CTASSERT(sizeof(*p) == 13);
290 1.1 christos const char *iftype;
291 1.1 christos
292 1.1 christos iftype = devpath_hw_bmc_iftype(p->IfaceType);
293 1.1 christos
294 1.1 christos path->sz = easprintf(&path->cp, "(%s,0x%016tx)", iftype, p->BaseAddress);
295 1.1 christos
296 1.1 christos if (dbg != NULL) {
297 1.1 christos dbg->sz = easprintf(&dbg->cp,
298 1.1 christos DEVPATH_FMT_HDR
299 1.1 christos DEVPATH_FMT(IfaceType: %u(%s)\n)
300 1.1 christos DEVPATH_FMT(BaseAddress: 0x%016tx\n),
301 1.1 christos DEVPATH_DAT_HDR(dp),
302 1.1 christos p->IfaceType, iftype,
303 1.1 christos p->BaseAddress);
304 1.1 christos }
305 1.1 christos }
306 1.1 christos
307 1.1 christos PUBLIC void
308 1.1 christos devpath_hw(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg)
309 1.1 christos {
310 1.1 christos
311 1.1 christos assert(dp->Type = 1);
312 1.1 christos
313 1.1 christos switch (dp->SubType) {
314 1.1 christos case 1: devpath_hw_pci(dp, path, dbg); return;
315 1.1 christos case 2: devpath_hw_pccard(dp, path, dbg); return;
316 1.1 christos case 3: devpath_hw_memmap(dp, path, dbg); return;
317 1.1 christos case 4: devpath_hw_vendor(dp, path, dbg); return;
318 1.1 christos case 5: devpath_hw_controller(dp, path, dbg); return;
319 1.1 christos case 6: devpath_hw_bmc(dp, path, dbg); return;
320 1.1 christos default: devpath_unsupported(dp, path, dbg); return;
321 1.1 christos }
322 1.1 christos }
323