1 1.5 riastrad /* $NetBSD: devpath4.c,v 1.5 2025/03/02 00:23:59 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.5 riastrad __RCSID("$NetBSD: devpath4.c,v 1.5 2025/03/02 00:23:59 riastradh 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 <stdlib.h> 36 1.1 christos #include <util.h> 37 1.1 christos 38 1.1 christos #include "defs.h" 39 1.1 christos #include "devpath.h" 40 1.1 christos #include "devpath4.h" 41 1.1 christos #include "utils.h" 42 1.1 christos 43 1.1 christos #define easprintf (size_t)easprintf 44 1.1 christos 45 1.1 christos #define EFI_VIRTUAL_DISK_GUID \ 46 1.3 christos {0x77AB535A,0x45FC,0x624B,0x55,0x60,{0xF7,0xB2,0x81,0xD1,0xF9,0x6E}} 47 1.1 christos 48 1.1 christos #define EFI_VIRTUAL_CD_GUID \ 49 1.3 christos {0x3D5ABD30,0x4175,0x87CE,0x6D,0x64,{0xD2,0xAD,0xE5,0x23,0xC4,0xBB}} 50 1.1 christos 51 1.1 christos #define EFI_PERSISTENT_VIRTUAL_DISK_GUID \ 52 1.3 christos {0x5CEA02C9,0x4D07,0x69D3,0x26,0x9F,{0x44,0x96,0xFB,0xE0,0x96,0xF9}} 53 1.1 christos 54 1.1 christos #define EFI_PERSISTENT_VIRTUAL_CD_GUID \ 55 1.3 christos {0x08018188,0x42CD,0xBB48,0x10,0x0F,{0x53,0x87,0xD5,0x3D,0xED,0x3D}} 56 1.1 christos 57 1.1 christos /************************************************************************ 58 1.1 christos * Type 4 - Media Device Path 59 1.1 christos ************************************************************************/ 60 1.1 christos 61 1.1 christos static void 62 1.1 christos devpath_media_harddisk(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 63 1.1 christos { /* See 10.3.5.1 */ 64 1.1 christos struct { /* Sub-Type 1 */ 65 1.1 christos devpath_t hdr; /* Length 42 */ 66 1.1 christos uint32_t PartitionNumber; 67 1.1 christos uint64_t PartitionStart; 68 1.1 christos uint64_t PartitionSize; 69 1.1 christos uuid_t PartitionSignature; 70 1.1 christos uint8_t PartitionFormat; 71 1.1 christos #define PARTITION_FORMAT_MBR 0x01 72 1.1 christos #define PARTITION_FORMAT_GPT 0x02 73 1.1 christos uint8_t SignatureType; 74 1.1 christos #define SIGNATURE_TYPE_NONE 0x00 75 1.1 christos #define SIGNATURE_TYPE_MBR 0x01 76 1.1 christos #define SIGNATURE_TYPE_GUID 0x02 77 1.1 christos } __packed *p = (void *)dp; 78 1.1 christos __CTASSERT(sizeof(*p) == 42); 79 1.1 christos char uuid_str[UUID_STR_LEN]; 80 1.1 christos const char *name_PartitionFormat; 81 1.1 christos 82 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), 83 1.1 christos &p->PartitionSignature); 84 1.1 christos 85 1.1 christos switch (p->PartitionFormat) { 86 1.1 christos case PARTITION_FORMAT_MBR: name_PartitionFormat = "MBR"; break; 87 1.1 christos case PARTITION_FORMAT_GPT: name_PartitionFormat = "GPT"; break; 88 1.1 christos default: name_PartitionFormat = "unknown"; break; 89 1.1 christos } 90 1.1 christos 91 1.2 martin path->sz = easprintf(&path->cp, "HD(%u,%s,%s,0x%" PRIx64 ",0x%" 92 1.2 martin PRIx64 ")", 93 1.1 christos p->PartitionNumber, 94 1.1 christos name_PartitionFormat, 95 1.1 christos uuid_str, 96 1.1 christos p->PartitionStart, 97 1.1 christos p->PartitionSize); 98 1.4 riastrad 99 1.1 christos if (dbg != NULL) { 100 1.1 christos const char *name_SignatureType; 101 1.1 christos 102 1.1 christos switch (p->SignatureType) { 103 1.1 christos case SIGNATURE_TYPE_NONE: name_SignatureType = "NONE"; break; 104 1.1 christos case SIGNATURE_TYPE_MBR: name_SignatureType = "MBR"; break; 105 1.1 christos case SIGNATURE_TYPE_GUID: name_SignatureType = "GUID"; break; 106 1.1 christos default: name_SignatureType = "unknown"; break; 107 1.1 christos } 108 1.4 riastrad 109 1.1 christos dbg->sz = easprintf(&dbg->cp, 110 1.1 christos DEVPATH_FMT_HDR 111 1.1 christos DEVPATH_FMT(PartitionNumber: %u\n) 112 1.2 martin DEVPATH_FMT(PartitionStart:) " 0x%" PRIx64 "\n" 113 1.2 martin DEVPATH_FMT(PartitionSize:) " 0x%" PRIx64 "\n" 114 1.1 christos DEVPATH_FMT(PartitionSignature: %s\n) 115 1.1 christos DEVPATH_FMT(PartitionFormat: %s(%u)\n) 116 1.1 christos DEVPATH_FMT(SignatureType: %s(%u)\n), 117 1.1 christos DEVPATH_DAT_HDR(dp), 118 1.1 christos p->PartitionNumber, 119 1.1 christos p->PartitionStart, 120 1.1 christos p->PartitionSize, 121 1.1 christos uuid_str, 122 1.1 christos name_PartitionFormat, 123 1.1 christos p->PartitionFormat, 124 1.1 christos name_SignatureType, 125 1.1 christos p->SignatureType); 126 1.1 christos } 127 1.1 christos } 128 1.1 christos 129 1.1 christos static void 130 1.1 christos devpath_media_cdrom(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 131 1.1 christos { /* See 10.3.5.2 */ 132 1.1 christos struct { /* Sub-Type 2 */ 133 1.1 christos devpath_t hdr; /* Length 24 */ 134 1.1 christos uint32_t BootEntry; 135 1.1 christos uint64_t PartitionStart; 136 1.1 christos uint64_t PartitionSize; 137 1.1 christos } __packed *p = (void *)dp; 138 1.1 christos __CTASSERT(sizeof(*p) == 24); 139 1.1 christos 140 1.2 martin path->sz = easprintf(&path->cp, "CDROM(0x%x,0x%016" PRIx64 ",0x%016" 141 1.2 martin PRIx64 ")", p->BootEntry, 142 1.1 christos p->PartitionStart, p->PartitionSize); 143 1.4 riastrad 144 1.1 christos if (dbg != NULL) { 145 1.1 christos dbg->sz = easprintf(&dbg->cp, 146 1.1 christos DEVPATH_FMT_HDR 147 1.1 christos DEVPATH_FMT(BootEntry: 0x%04x\n) 148 1.2 martin DEVPATH_FMT(PartitionStart:) " 0x%016" PRIx64 "\n" 149 1.2 martin DEVPATH_FMT(PartitionSize:) " 0x%016" PRIx64 "\n", 150 1.1 christos DEVPATH_DAT_HDR(dp), 151 1.1 christos p->BootEntry, 152 1.1 christos p->PartitionStart, 153 1.1 christos p->PartitionSize); 154 1.1 christos } 155 1.1 christos } 156 1.1 christos 157 1.1 christos static void 158 1.1 christos devpath_media_vendor(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 159 1.1 christos { /* See 10.3.5.3 */ 160 1.1 christos 161 1.1 christos struct { /* Sub-Type 3 */ 162 1.1 christos devpath_t hdr; /* Length = 20 + n */ 163 1.1 christos uuid_t VendorGUID; 164 1.1 christos uint8_t vendorData[]; 165 1.1 christos } __packed *p = (void *)dp; 166 1.1 christos __CTASSERT(sizeof(*p) == 20); 167 1.1 christos char uuid_str[UUID_STR_LEN]; 168 1.1 christos 169 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->VendorGUID); 170 1.1 christos 171 1.1 christos path->sz = easprintf(&path->cp, "VenMedia(%s)", uuid_str); 172 1.4 riastrad 173 1.1 christos if (dbg != NULL) { 174 1.1 christos dbg->sz = easprintf(&dbg->cp, 175 1.1 christos DEVPATH_FMT_HDR 176 1.1 christos DEVPATH_FMT(VendorGUID: %s\n), 177 1.1 christos DEVPATH_DAT_HDR(dp), 178 1.1 christos uuid_str); 179 1.1 christos } 180 1.1 christos } 181 1.1 christos 182 1.1 christos static void 183 1.1 christos devpath_media_filepath(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 184 1.1 christos { /* See 10.3.5.4 */ 185 1.1 christos struct { /* Sub-Type 4 */ 186 1.1 christos devpath_t hdr; /* Length = 4 + n */ 187 1.1 christos uint16_t PathName[]; 188 1.1 christos } __packed *p = (void *)dp; 189 1.1 christos __CTASSERT(sizeof(*p) == 4); 190 1.1 christos char *PathName; 191 1.1 christos size_t sz = p->hdr.Length - sizeof(*p); 192 1.1 christos 193 1.1 christos PathName = ucs2_to_utf8(p->PathName, sz, NULL, NULL); 194 1.1 christos path->sz = easprintf(&path->cp, "File(%s)", PathName); 195 1.4 riastrad 196 1.1 christos if (dbg != NULL) { 197 1.1 christos dbg->sz = easprintf(&dbg->cp, 198 1.1 christos DEVPATH_FMT_HDR 199 1.1 christos DEVPATH_FMT(PathName: %s\n), 200 1.1 christos DEVPATH_DAT_HDR(dp), 201 1.1 christos PathName); 202 1.1 christos } 203 1.1 christos free(PathName); 204 1.1 christos } 205 1.1 christos 206 1.1 christos static void 207 1.1 christos devpath_media_protocol(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 208 1.1 christos { /* See 10.3.5.5 */ 209 1.1 christos struct { /* Sub-Type 5 */ 210 1.1 christos devpath_t hdr; /* Length 20 */ 211 1.1 christos uuid_t ProtocolGUID; 212 1.1 christos } __packed *p = (void *)dp; 213 1.1 christos __CTASSERT(sizeof(*p) == 20); 214 1.1 christos char uuid_str[UUID_STR_LEN]; 215 1.1 christos 216 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->ProtocolGUID); 217 1.1 christos path->sz = easprintf(&path->cp, "Media(%s)", uuid_str); 218 1.1 christos 219 1.1 christos if (dbg != NULL) { 220 1.1 christos dbg->sz = easprintf(&dbg->cp, 221 1.1 christos DEVPATH_FMT_HDR 222 1.1 christos DEVPATH_FMT(ProtocolGUID: %s\n), 223 1.1 christos DEVPATH_DAT_HDR(dp), 224 1.1 christos uuid_str); 225 1.1 christos } 226 1.1 christos } 227 1.1 christos 228 1.1 christos static void 229 1.5 riastrad devpath_media_PIWGfwfile(devpath_t *dp, devpath_elm_t *path, 230 1.5 riastrad devpath_elm_t *dbg) 231 1.1 christos { /* See 10.3.5.6 */ 232 1.1 christos /* See UEFI PI Version 1.8 Errata A (March 5, 2024) II-8.3 */ 233 1.1 christos struct { /* Sub-Type 6 */ 234 1.1 christos devpath_t hdr; /* Length 20 */ 235 1.1 christos uuid_t FWFileName; 236 1.1 christos } __packed *p = (void *)dp; 237 1.1 christos __CTASSERT(sizeof(*p) == 20); 238 1.1 christos char uuid_str[UUID_STR_LEN]; 239 1.1 christos 240 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->FWFileName); 241 1.1 christos path->sz = easprintf(&path->cp, "FvFile(%s)", uuid_str); 242 1.1 christos 243 1.1 christos if (dbg != NULL) { 244 1.1 christos dbg->sz = easprintf(&dbg->cp, 245 1.1 christos DEVPATH_FMT_HDR 246 1.1 christos DEVPATH_FMT(FirmwareFileName: %s\n), 247 1.1 christos DEVPATH_DAT_HDR(dp), 248 1.1 christos uuid_str); 249 1.1 christos } 250 1.1 christos } 251 1.1 christos 252 1.1 christos static void 253 1.1 christos devpath_media_PIWGfwvol(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 254 1.1 christos { /* See 10.3.5.7 */ 255 1.1 christos /* See UEFI PI Version 1.8 Errata A (March 5, 2024) II-8.2 */ 256 1.1 christos struct { /* Sub-Type 7 */ 257 1.1 christos devpath_t hdr; /* Length 20 */ 258 1.1 christos uuid_t GUID; 259 1.1 christos } __packed *p = (void *)dp; 260 1.1 christos __CTASSERT(sizeof(*p) == 20); 261 1.1 christos char uuid_str[UUID_STR_LEN]; 262 1.1 christos 263 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->GUID); 264 1.1 christos path->sz = easprintf(&path->cp, "Fv(%s)", uuid_str); 265 1.1 christos 266 1.1 christos if (dbg != NULL) { 267 1.1 christos dbg->sz = easprintf(&dbg->cp, 268 1.1 christos DEVPATH_FMT_HDR 269 1.1 christos DEVPATH_FMT(GUID: %s\n), 270 1.1 christos DEVPATH_DAT_HDR(dp), 271 1.1 christos uuid_str); 272 1.1 christos } 273 1.1 christos } 274 1.1 christos 275 1.1 christos static void 276 1.1 christos devpath_media_reloff(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 277 1.1 christos { /* See 10.3.5.8 */ 278 1.1 christos struct { /* Sub-Type 8 */ 279 1.1 christos devpath_t hdr; /* Length 24 */ 280 1.1 christos uint32_t Reserved; 281 1.1 christos uint64_t StartingOffset; 282 1.1 christos uint64_t EndingOffset; 283 1.1 christos } __packed *p = (void *)dp; 284 1.1 christos __CTASSERT(sizeof(*p) == 24); 285 1.1 christos 286 1.2 martin path->sz = easprintf(&path->cp, "Offset(0x%016" PRIx64 ",0x%016" 287 1.2 martin PRIx64 ")", 288 1.1 christos p->StartingOffset, p->EndingOffset); 289 1.1 christos 290 1.1 christos if (dbg != NULL) { 291 1.1 christos dbg->sz = easprintf(&dbg->cp, 292 1.1 christos DEVPATH_FMT_HDR 293 1.1 christos DEVPATH_FMT(Reserved: 0x%08x\n) 294 1.2 martin DEVPATH_FMT(StartingOffset:) " 0x%016" PRIx64 "\n" 295 1.2 martin DEVPATH_FMT(EndingOffset:) " 0x%016" PRIx64 "\n", 296 1.1 christos DEVPATH_DAT_HDR(dp), 297 1.1 christos p->Reserved, 298 1.1 christos p->StartingOffset, 299 1.1 christos p->EndingOffset); 300 1.1 christos } 301 1.1 christos } 302 1.1 christos 303 1.1 christos static const char * 304 1.1 christos devpath_media_ramdisk_type(uuid_t *uuid) 305 1.1 christos { 306 1.1 christos static struct { 307 1.1 christos uuid_t GUID; 308 1.1 christos const char *type; 309 1.1 christos } tbl[] = { 310 1.1 christos { .GUID = EFI_VIRTUAL_DISK_GUID, 311 1.1 christos .type = "VirtualDisk", 312 1.1 christos }, 313 1.1 christos { .GUID = EFI_VIRTUAL_CD_GUID, 314 1.1 christos .type = "VirtualCD", 315 1.1 christos }, 316 1.1 christos { .GUID = EFI_PERSISTENT_VIRTUAL_DISK_GUID, 317 1.1 christos .type = "PersistentVirtualDisk", 318 1.1 christos }, 319 1.1 christos { .GUID =EFI_PERSISTENT_VIRTUAL_CD_GUID, 320 1.1 christos .type = "PersistentVirtualCD", 321 1.1 christos }, 322 1.1 christos }; 323 1.1 christos 324 1.1 christos for (size_t i = 0; i < __arraycount(tbl); i++) { 325 1.1 christos if (memcmp(uuid, &tbl[i].GUID, sizeof(*uuid)) == 0) { 326 1.1 christos return tbl[i].type; 327 1.1 christos } 328 1.1 christos } 329 1.1 christos return NULL; 330 1.1 christos } 331 1.1 christos 332 1.1 christos static void 333 1.1 christos devpath_media_ramdisk(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 334 1.1 christos { /* See 10.3.5.9 */ 335 1.1 christos struct { /* Sub-Type 9 */ 336 1.1 christos devpath_t hdr; /* Length 38 */ 337 1.1 christos uint64_t StartingAddress; 338 1.1 christos uint64_t EndingAddress; 339 1.1 christos uuid_t GUID; 340 1.1 christos uint16_t Instance; 341 1.1 christos } __packed *p = (void *)dp; 342 1.1 christos __CTASSERT(sizeof(*p) == 38); 343 1.1 christos char uuid_str[UUID_STR_LEN]; 344 1.1 christos const char *type; 345 1.1 christos 346 1.1 christos uuid_snprintf(uuid_str, sizeof(uuid_str), &p->GUID); 347 1.1 christos 348 1.1 christos type = devpath_media_ramdisk_type(&p->GUID); 349 1.1 christos if (type != NULL) 350 1.2 martin path->sz = easprintf(&path->cp, "%s(0x%016" PRIx64 ",0x%016" 351 1.2 martin PRIx64 ",%u)", type, 352 1.1 christos p->StartingAddress, p->EndingAddress, p->Instance); 353 1.1 christos else 354 1.2 martin path->sz = easprintf(&path->cp, "RamDisk(0x%016" PRIx64 355 1.2 martin ",0x%016" PRIx64 ",%u,%s)", 356 1.1 christos p->StartingAddress, p->EndingAddress, p->Instance, 357 1.1 christos uuid_str); 358 1.1 christos 359 1.1 christos if (dbg != NULL) { 360 1.1 christos dbg->sz = easprintf(&dbg->cp, 361 1.1 christos DEVPATH_FMT_HDR 362 1.2 martin DEVPATH_FMT(StartingAddress:) " 0x%016" PRIx64 "\n" 363 1.2 martin DEVPATH_FMT(EndingAddress:) " 0x%016" PRIx64 "\n" 364 1.1 christos DEVPATH_FMT(Instance: 0x%08x\n) 365 1.1 christos DEVPATH_FMT(GUID: %s\n), 366 1.1 christos DEVPATH_DAT_HDR(dp), 367 1.1 christos p->StartingAddress, 368 1.1 christos p->EndingAddress, 369 1.1 christos p->Instance, 370 1.1 christos uuid_str); 371 1.1 christos } 372 1.1 christos } 373 1.1 christos 374 1.1 christos PUBLIC void 375 1.1 christos devpath_media(devpath_t *dp, devpath_elm_t *path, devpath_elm_t *dbg) 376 1.1 christos { 377 1.1 christos 378 1.1 christos switch (dp->SubType) { 379 1.1 christos case 1: devpath_media_harddisk(dp, path, dbg); return; 380 1.1 christos case 2: devpath_media_cdrom(dp, path, dbg); return; 381 1.1 christos case 3: devpath_media_vendor(dp, path, dbg); return; 382 1.1 christos case 4: devpath_media_filepath(dp, path, dbg); return; 383 1.1 christos case 5: devpath_media_protocol(dp, path, dbg); return; 384 1.1 christos case 6: devpath_media_PIWGfwfile(dp, path, dbg); return; 385 1.1 christos case 7: devpath_media_PIWGfwvol(dp, path, dbg); return; 386 1.1 christos case 8: devpath_media_reloff(dp, path, dbg); return; 387 1.1 christos case 9: devpath_media_ramdisk(dp, path, dbg); return; 388 1.1 christos default: devpath_unsupported(dp, path, dbg); return; 389 1.1 christos } 390 1.1 christos } 391