aboutsummaryrefslogtreecommitdiff
path: root/stand/efi/libefi/devpath.c
blob: b56c583b5fa740d914dd4495571f625d4c4546af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*-
 * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <efi.h>
#include <efilib.h>
#include <efichar.h>
#include <uuid.h>
#include <machine/_inttypes.h>

static EFI_GUID ImageDevicePathGUID =
    EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *toTextProtocol;
static EFI_GUID DevicePathFromTextGUID =
    EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID;
static EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *fromTextProtocol;

EFI_DEVICE_PATH *
efi_lookup_image_devpath(EFI_HANDLE handle)
{
	EFI_DEVICE_PATH *devpath;
	EFI_STATUS status;

	status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
	    (void **)&devpath);
	if (EFI_ERROR(status))
		devpath = NULL;
	return (devpath);
}

EFI_DEVICE_PATH *
efi_lookup_devpath(EFI_HANDLE handle)
{
	EFI_DEVICE_PATH *devpath;
	EFI_STATUS status;

	status = OpenProtocolByHandle(handle, &DevicePathGUID,
	    (void **)&devpath);
	if (EFI_ERROR(status))
		devpath = NULL;
	return (devpath);
}

void
efi_close_devpath(EFI_HANDLE handle)
{
	EFI_STATUS status;

	status = BS->CloseProtocol(handle, &DevicePathGUID, IH, NULL);
	if (EFI_ERROR(status))
		printf("CloseProtocol error: %lu\n", EFI_ERROR_CODE(status));
}

static char *
efi_make_tail(char *suffix)
{
	char *tail;

	tail = NULL;
	if (suffix != NULL)
		(void)asprintf(&tail, "/%s", suffix);
	else
		tail = strdup("");
	return (tail);
}

typedef struct {
	EFI_DEVICE_PATH	Header;
	EFI_GUID	Guid;
	UINT8		VendorDefinedData[1];
} __packed VENDOR_DEVICE_PATH_WITH_DATA;

static char *
efi_vendor_path(const char *type, VENDOR_DEVICE_PATH *node, char *suffix)
{
	uint32_t size = DevicePathNodeLength(&node->Header) - sizeof(*node);
	VENDOR_DEVICE_PATH_WITH_DATA *dp = (VENDOR_DEVICE_PATH_WITH_DATA *)node;
	char *name, *tail, *head;
	char *uuid;
	int rv;

	uuid_to_string((const uuid_t *)(void *)&node->Guid, &uuid, &rv);
	if (rv != uuid_s_ok)
		return (NULL);

	tail = efi_make_tail(suffix);
	rv = asprintf(&head, "%sVendor(%s)[%x:", type, uuid, size);
	free(uuid);
	if (rv < 0)
		return (NULL);

	if (DevicePathNodeLength(&node->Header) > sizeof(*node)) {
		for (uint32_t i = 0; i < size; i++) {
			rv = asprintf(&name, "%s%02x", head,
			    dp->VendorDefinedData[i]);
			if (rv < 0) {
				free(tail);
				free(head);
				return (NULL);
			}
			free(head);
			head = name;
		}
	}

	if (asprintf(&name, "%s]%s", head, tail) < 0)
		name = NULL;
	free(head);
	free(tail);
	return (name);
}

static char *
efi_hw_dev_path(EFI_DEVICE_PATH *node, char *suffix)
{
	uint8_t subtype = DevicePathSubType(node);
	char *name, *tail;

	tail = efi_make_tail(suffix);
	switch (subtype) {
	case HW_PCI_DP:
		if (asprintf(&name, "Pci(%x,%x)%s",
		    ((PCI_DEVICE_PATH *)node)->Function,
		    ((PCI_DEVICE_PATH *)node)->Device, tail) < 0)
			name = NULL;
		break;
	case HW_PCCARD_DP:
		if (asprintf(&name, "PCCARD(%x)%s",
		    ((PCCARD_DEVICE_PATH *)node)->FunctionNumber, tail) < 0)
			name = NULL;
		break;
	case HW_MEMMAP_DP:
		if (asprintf(&name, "MMap(%x,%" PRIx64 ",%" PRIx64 ")%s",
		    ((MEMMAP_DEVICE_PATH *)node)->MemoryType,
		    ((MEMMAP_DEVICE_PATH *)node)->StartingAddress,
		    ((MEMMAP_DEVICE_PATH *)node)->EndingAddress, tail) < 0)
			name = NULL;
		break;
	case HW_VENDOR_DP:
		name = efi_vendor_path("Hardware",
		    (VENDOR_DEVICE_PATH *)node, tail);
		break;
	case HW_CONTROLLER_DP:
		if (asprintf(&name, "Ctrl(%x)%s",
		    ((CONTROLLER_DEVICE_PATH *)node)->Controller, tail) < 0)
			name = NULL;
		break;
	default:
		if (asprintf(&name, "UnknownHW(%x)%s", subtype, tail) < 0)
			name = NULL;
		break;
	}
	free(tail);
	return (name);
}

static char *
efi_acpi_dev_path(EFI_DEVICE_PATH *node, char *suffix)
{
	uint8_t subtype = DevicePathSubType(node);
	ACPI_HID_DEVICE_PATH *acpi = (ACPI_HID_DEVICE_PATH *)node;
	char *name, *tail;

	tail = efi_make_tail(suffix);
	switch (subtype) {
	case ACPI_DP:
		if ((acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
			switch (EISA_ID_TO_NUM (acpi->HID)) {
			case 0x0a03:
				if (asprintf(&name, "PciRoot(%x)%s",
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			case 0x0a08:
				if (asprintf(&name, "PcieRoot(%x)%s",
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			case 0x0604:
				if (asprintf(&name, "Floppy(%x)%s",
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			case 0x0301:
				if (asprintf(&name, "Keyboard(%x)%s",
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			case 0x0501:
				if (asprintf(&name, "Serial(%x)%s",
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			case 0x0401:
				if (asprintf(&name, "ParallelPort(%x)%s",
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			default:
				if (asprintf(&name, "Acpi(PNP%04x,%x)%s",
				    EISA_ID_TO_NUM(acpi->HID),
				    acpi->UID, tail) < 0)
					name = NULL;
				break;
			}
		} else {
			if (asprintf(&name, "Acpi(%08x,%x)%s",
			    acpi->HID, acpi->UID, tail) < 0)
				name = NULL;
		}
		break;
	case ACPI_EXTENDED_DP:
	default:
		if (asprintf(&name, "UnknownACPI(%x)%s", subtype, tail) < 0)
			name = NULL;
		break;
	}
	free(tail);
	return (name);
}

static char *
efi_messaging_dev_path(EFI_DEVICE_PATH *node, char *suffix)
{
	uint8_t subtype = DevicePathSubType(node);
	char *name;
	char *tail;

	tail = efi_make_tail(suffix);
	switch (subtype) {
	case MSG_ATAPI_DP:
		if (asprintf(&name, "ATA(%s,%s,%x)%s",
		    ((ATAPI_DEVICE_PATH *)node)->PrimarySecondary == 1 ?
		    "Secondary" : "Primary",
		    ((ATAPI_DEVICE_PATH *)node)->SlaveMaster == 1 ?
		    "Slave" : "Master",
		    ((ATAPI_DEVICE_PATH *)node)->Lun, tail) < 0)
			name = NULL;
		break;
	case MSG_SCSI_DP:
		if (asprintf(&name, "SCSI(%x,%x)%s",
		    ((SCSI_DEVICE_PATH *)node)->Pun,
		    ((SCSI_DEVICE_PATH *)node)->Lun, tail) < 0)
			name = NULL;
		break;
	case MSG_FIBRECHANNEL_DP:
		if (asprintf(&name, "Fibre(%" PRIx64 ",%" PRIx64 ")%s",
		    ((FIBRECHANNEL_DEVICE_PATH *)node)->WWN,
		    ((FIBRECHANNEL_DEVICE_PATH *)node)->Lun, tail) < 0)
			name = NULL;
		break;
	case MSG_1394_DP:
		if (asprintf(&name, "I1394(%016" PRIx64 ")%s",
		    ((F1394_DEVICE_PATH *)node)->Guid, tail) < 0)
			name = NULL;
		break;
	case MSG_USB_DP:
		if (asprintf(&name, "USB(%x,%x)%s",
		    ((USB_DEVICE_PATH *)node)->ParentPortNumber,
		    ((USB_DEVICE_PATH *)node)->InterfaceNumber, tail) < 0)
			name = NULL;
		break;
	case MSG_USB_CLASS_DP:
		if (asprintf(&name, "UsbClass(%x,%x,%x,%x,%x)%s",
		    ((USB_CLASS_DEVICE_PATH *)node)->VendorId,
		    ((USB_CLASS_DEVICE_PATH *)node)->ProductId,
		    ((USB_CLASS_DEVICE_PATH *)node)->DeviceClass,
		    ((USB_CLASS_DEVICE_PATH *)node)->DeviceSubClass,
		    ((USB_CLASS_DEVICE_PATH *)node)->DeviceProtocol, tail) < 0)
			name = NULL;
		break;
	case MSG_MAC_ADDR_DP:
		if (asprintf(&name, "MAC(%02x:%02x:%02x:%02x:%02x:%02x,%x)%s",
		    ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[0],
		    ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[1],
		    ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[2],
		    ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[3],
		    ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[4],
		    ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[5],
		    ((MAC_ADDR_DEVICE_PATH *)node)->IfType, tail) < 0)
			name = NULL;
		break;
	case MSG_VENDOR_DP:
		name = efi_vendor_path("Messaging",
		    (VENDOR_DEVICE_PATH *)node, tail);
		break;
	case MSG_UART_DP:
		if (asprintf(&name, "UART(%" PRIu64 ",%u,%x,%x)%s",
		    ((UART_DEVICE_PATH *)node)->BaudRate,
		    ((UART_DEVICE_PATH *)node)->DataBits,
		    ((UART_DEVICE_PATH *)node)->Parity,
		    ((UART_DEVICE_PATH *)node)->StopBits, tail) < 0)
			name = NULL;
		break;
	case MSG_SATA_DP:
		if (asprintf(&name, "Sata(%x,%x,%x)%s",
		    ((SATA_DEVICE_PATH *)node)->HBAPortNumber,
		    ((SATA_DEVICE_PATH *)node)->PortMultiplierPortNumber,
		    ((SATA_DEVICE_PATH *)node)->Lun, tail) < 0)
			name = NULL;
		break;
	default:
		if (asprintf(&name, "UnknownMessaging(%x)%s",
		    subtype, tail) < 0)
			name = NULL;
		break;
	}
	free(tail);
	return (name);
}

static char *
efi_media_dev_path(EFI_DEVICE_PATH *node, char *suffix)
{
	uint8_t subtype = DevicePathSubType(node);
	HARDDRIVE_DEVICE_PATH *hd;
	char *name;
	char *str;
	char *tail;
	int rv;

	tail = efi_make_tail(suffix);
	name = NULL;
	switch (subtype) {
	case MEDIA_HARDDRIVE_DP:
		hd = (HARDDRIVE_DEVICE_PATH *)node;
		switch (hd->SignatureType) {
		case SIGNATURE_TYPE_MBR:
			if (asprintf(&name, "HD(%d,MBR,%08x,%" PRIx64
			    ",%" PRIx64 ")%s",
			    hd->PartitionNumber,
			    *((uint32_t *)(uintptr_t)&hd->Signature[0]),
			    hd->PartitionStart,
			    hd->PartitionSize, tail) < 0)
				name = NULL;
			break;
		case SIGNATURE_TYPE_GUID:
			name = NULL;
			uuid_to_string((const uuid_t *)(void *)
			    &hd->Signature[0], &str, &rv);
			if (rv != uuid_s_ok)
				break;
			rv = asprintf(&name, "HD(%d,GPT,%s,%" PRIx64 ",%"
			    PRIx64 ")%s",
			    hd->PartitionNumber, str,
			    hd->PartitionStart, hd->PartitionSize, tail);
			free(str);
			break;
		default:
			if (asprintf(&name, "HD(%d,%d,0)%s",
			    hd->PartitionNumber,
			    hd->SignatureType, tail) < 0) {
				name = NULL;
			}
			break;
		}
		break;
	case MEDIA_CDROM_DP:
		if (asprintf(&name, "CD(%x,%" PRIx64 ",%" PRIx64 ")%s",
		    ((CDROM_DEVICE_PATH *)node)->BootEntry,
		    ((CDROM_DEVICE_PATH *)node)->PartitionStart,
		    ((CDROM_DEVICE_PATH *)node)->PartitionSize, tail) < 0) {
			name = NULL;
		}
		break;
	case MEDIA_VENDOR_DP:
		name = efi_vendor_path("Media",
		    (VENDOR_DEVICE_PATH *)node, tail);
		break;
	case MEDIA_FILEPATH_DP:
		name = NULL;
		str = NULL;
		if (ucs2_to_utf8(((FILEPATH_DEVICE_PATH *)node)->PathName,
		    &str) == 0) {
			(void)asprintf(&name, "%s%s", str, tail);
			free(str);
		}
		break;
	case MEDIA_PROTOCOL_DP:
		name = NULL;
		uuid_to_string((const uuid_t *)(void *)
		    &((MEDIA_PROTOCOL_DEVICE_PATH *)node)->Protocol,
		    &str, &rv);
		if (rv != uuid_s_ok)
			break;
		rv = asprintf(&name, "Protocol(%s)%s", str, tail);
		free(str);
		break;
	default:
		if (asprintf(&name, "UnknownMedia(%x)%s",
		    subtype, tail) < 0)
			name = NULL;
	}
	free(tail);
	return (name);
}

static char *
efi_translate_devpath(EFI_DEVICE_PATH *devpath)
{
	EFI_DEVICE_PATH *dp = NextDevicePathNode(devpath);
	char *name, *ptr;
	uint8_t type;

	if (!IsDevicePathEnd(devpath))
		name = efi_translate_devpath(dp);
	else
		return (NULL);

	ptr = NULL;
	type = DevicePathType(devpath);
	switch (type) {
	case HARDWARE_DEVICE_PATH:
		ptr = efi_hw_dev_path(devpath, name);
		break;
	case ACPI_DEVICE_PATH:
		ptr = efi_acpi_dev_path(devpath, name);
		break;
	case MESSAGING_DEVICE_PATH:
		ptr = efi_messaging_dev_path(devpath, name);
		break;
	case MEDIA_DEVICE_PATH:
		ptr = efi_media_dev_path(devpath, name);
		break;
	case BBS_DEVICE_PATH:
	default:
		if (asprintf(&ptr, "UnknownPath(%x)%s", type,
		    name? name : "") < 0)
			ptr = NULL;
		break;
	}

	if (ptr != NULL) {
		free(name);
		name = ptr;
	}
	return (name);
}

static CHAR16 *
efi_devpath_to_name(EFI_DEVICE_PATH *devpath)
{
	char *name = NULL;
	CHAR16 *ptr = NULL;
	size_t len;
	int rv;

	name = efi_translate_devpath(devpath);
	if (name == NULL)
		return (NULL);

	/*
	 * We need to return memory from AllocatePool, so it can be freed
	 * with FreePool() in efi_free_devpath_name().
	 */
	rv = utf8_to_ucs2(name, &ptr, &len);
	free(name);
	if (rv == 0) {
		CHAR16 *out = NULL;
		EFI_STATUS status;

		status = BS->AllocatePool(EfiLoaderData, len, (void **)&out);
		if (EFI_ERROR(status)) {
			free(ptr);
                	return (out);
		}
		memcpy(out, ptr, len);
		free(ptr);
		ptr = out;
	}
	
	return (ptr);
}

CHAR16 *
efi_devpath_name(EFI_DEVICE_PATH *devpath)
{
	EFI_STATUS status;

	if (devpath == NULL)
		return (NULL);
	if (toTextProtocol == NULL) {
		status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
		    (VOID **)&toTextProtocol);
		if (EFI_ERROR(status))
			toTextProtocol = NULL;
	}
	if (toTextProtocol == NULL)
		return (efi_devpath_to_name(devpath));

	return (toTextProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
}

void
efi_free_devpath_name(CHAR16 *text)
{
	if (text != NULL)
		BS->FreePool(text);
}

EFI_DEVICE_PATH *
efi_name_to_devpath(const char *path)
{
	EFI_DEVICE_PATH *devpath;
	CHAR16 *uv;
	size_t ul;

	uv = NULL;
	if (utf8_to_ucs2(path, &uv, &ul) != 0)
		return (NULL);
	devpath = efi_name_to_devpath16(uv);
	free(uv);
	return (devpath);
}

EFI_DEVICE_PATH *
efi_name_to_devpath16(CHAR16 *path)
{
	EFI_STATUS status;

	if (path == NULL)
		return (NULL);
	if (fromTextProtocol == NULL) {
		status = BS->LocateProtocol(&DevicePathFromTextGUID, NULL,
		    (VOID **)&fromTextProtocol);
		if (EFI_ERROR(status))
			fromTextProtocol = NULL;
	}
	if (fromTextProtocol == NULL)
		return (NULL);

	return (fromTextProtocol->ConvertTextToDevicePath(path));
}

void efi_devpath_free(EFI_DEVICE_PATH *devpath)
{

	BS->FreePool(devpath);
}

EFI_DEVICE_PATH *
efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
{

	if (IsDevicePathEnd(devpath))
		return (NULL);
	while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
		devpath = NextDevicePathNode(devpath);
	return (devpath);
}

EFI_DEVICE_PATH *
efi_devpath_trim(EFI_DEVICE_PATH *devpath)
{
	EFI_DEVICE_PATH *node, *copy;
	size_t prefix, len;

	if ((node = efi_devpath_last_node(devpath)) == NULL)
		return (NULL);
	prefix = (UINT8 *)node - (UINT8 *)devpath;
	if (prefix == 0)
		return (NULL);
	len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
	copy = malloc(len);
	if (copy != NULL) {
		memcpy(copy, devpath, prefix);
		node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
		SetDevicePathEndNode(node);
	}
	return (copy);
}

EFI_HANDLE
efi_devpath_handle(EFI_DEVICE_PATH *devpath)
{
	EFI_STATUS status;
	EFI_HANDLE h;

	/*
	 * There isn't a standard way to locate a handle for a given
	 * device path.  However, querying the EFI_DEVICE_PATH protocol
	 * for a given device path should give us a handle for the
	 * closest node in the path to the end that is valid.
	 */
	status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
	if (EFI_ERROR(status))
		return (NULL);
	return (h);
}

bool
efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
{
	size_t len;

	if (devpath1 == NULL || devpath2 == NULL)
		return (false);
	if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
	    DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
		return (false);
	len = DevicePathNodeLength(devpath1);
	if (len != DevicePathNodeLength(devpath2))
		return (false);
	if (memcmp(devpath1, devpath2, len) != 0)
		return (false);
	return (true);
}

static bool
_efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2,
    bool ignore_media)
{

	if (devpath1 == NULL || devpath2 == NULL)
		return (false);

	while (true) {
		if (ignore_media &&
		    IsDevicePathType(devpath1, MEDIA_DEVICE_PATH) &&
		    IsDevicePathType(devpath2, MEDIA_DEVICE_PATH))
			return (true);
		if (!efi_devpath_match_node(devpath1, devpath2))
			return false;
		if (IsDevicePathEnd(devpath1))
			break;
		devpath1 = NextDevicePathNode(devpath1);
		devpath2 = NextDevicePathNode(devpath2);
	}
	return (true);
}
/*
 * Are two devpaths identical?
 */
bool
efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
{
	return _efi_devpath_match(devpath1, devpath2, false);
}

/*
 * Like efi_devpath_match, but stops at when we hit the media device
 * path node that specifies the partition information. If we match
 * up to that point, then we're on the same disk.
 */
bool
efi_devpath_same_disk(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
{
	return _efi_devpath_match(devpath1, devpath2, true);
}

bool
efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
{
	size_t len;

	if (prefix == NULL || path == NULL)
		return (false);

	while (1) {
		if (IsDevicePathEnd(prefix))
			break;

		if (DevicePathType(prefix) != DevicePathType(path) ||
		    DevicePathSubType(prefix) != DevicePathSubType(path))
			return (false);

		len = DevicePathNodeLength(prefix);
		if (len != DevicePathNodeLength(path))
			return (false);

		if (memcmp(prefix, path, len) != 0)
			return (false);

		prefix = NextDevicePathNode(prefix);
		path = NextDevicePathNode(path);
	}
	return (true);
}

/*
 * Skip over the 'prefix' part of path and return the part of the path
 * that starts with the first node that's a MEDIA_DEVICE_PATH.
 */
EFI_DEVICE_PATH *
efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
{

	while (!IsDevicePathEnd(path)) {
		if (DevicePathType(path) == MEDIA_DEVICE_PATH)
			return (path);
		path = NextDevicePathNode(path);
	}
	return (NULL);
}

UINTN
efi_devpath_length(EFI_DEVICE_PATH  *path)
{
	EFI_DEVICE_PATH *start = path;

	while (!IsDevicePathEnd(path))
		path = NextDevicePathNode(path);
	return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);
}

EFI_HANDLE
efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles)
{
	unsigned i;
	EFI_DEVICE_PATH *media, *devpath;
	EFI_HANDLE h;

	media = efi_devpath_to_media_path(path);
	if (media == NULL)
		return (NULL);
	for (i = 0; i < nhandles; i++) {
		h = handles[i];
		devpath = efi_lookup_devpath(h);
		if (devpath == NULL)
			continue;
		if (!efi_devpath_match_node(media, efi_devpath_to_media_path(devpath)))
			continue;
		return (h);
	}
	return (NULL);
}