xref: /netbsd-src/sys/stand/efiboot/efidev.c (revision 3fc11eecb7a487925583126e01a8180572a7e884)
1 /*	$NetBSD: efidev.c,v 1.2 2020/07/15 00:51:40 jmcneill Exp $	*/
2 /*	$OpenBSD: efiboot.c,v 1.28 2017/11/25 19:02:07 patrick Exp $	*/
3 
4 /*
5  * Copyright (c) 2015 YASUOKA Masahiko <yasuoka@yasuoka.net>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "efiboot.h"
21 
22 /*
23  * Determine the number of nodes up to, but not including, the first
24  * node of the specified type.
25  */
26 int
efi_device_path_depth(EFI_DEVICE_PATH * dp,int dptype)27 efi_device_path_depth(EFI_DEVICE_PATH *dp, int dptype)
28 {
29 	int	i;
30 
31 	for (i = 0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp), i++) {
32 		if (DevicePathType(dp) == dptype)
33 			return (i);
34 	}
35 
36 	return (-1);
37 }
38 
39 int
efi_device_path_count(EFI_DEVICE_PATH * dp)40 efi_device_path_count(EFI_DEVICE_PATH *dp)
41 {
42 	int	count;
43 
44 	for (count = 0; ; dp = NextDevicePathNode(dp), count++) {
45 		if (IsDevicePathEnd(dp))
46 			break;
47 	}
48 
49 	return (count);
50 }
51 
52 int
efi_device_path_ncmp(EFI_DEVICE_PATH * dpa,EFI_DEVICE_PATH * dpb,int deptn)53 efi_device_path_ncmp(EFI_DEVICE_PATH *dpa, EFI_DEVICE_PATH *dpb, int deptn)
54 {
55 	int	 i, cmp;
56 
57 	for (i = 0; i < deptn; i++) {
58 		if (IsDevicePathEnd(dpa) || IsDevicePathEnd(dpb))
59 			return ((IsDevicePathEnd(dpa) && IsDevicePathEnd(dpb))
60 			    ? 0 : (IsDevicePathEnd(dpa))? -1 : 1);
61 		cmp = DevicePathNodeLength(dpa) - DevicePathNodeLength(dpb);
62 		if (cmp)
63 			return (cmp);
64 		cmp = memcmp(dpa, dpb, DevicePathNodeLength(dpa));
65 		if (cmp)
66 			return (cmp);
67 		dpa = NextDevicePathNode(dpa);
68 		dpb = NextDevicePathNode(dpb);
69 	}
70 
71 	return (0);
72 }
73