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 */
26int
27efi_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
39int
40efi_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
52int
53efi_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