From 2e1b4c6feef0ca074bfdb2f007fd08b9037212b1 Mon Sep 17 00:00:00 2001 From: Warner Losh Date: Fri, 6 Oct 2017 12:20:24 +0000 Subject: [PATCH] Add efi_devpath_is_prefix efi_devpath_is_prefix determines if a path matches a passed-in prefix. Differential Revision: https://reviews.freebsd.org/D12564 Submitted by: Eric McCorkle --- sys/boot/efi/include/efilib.h | 1 + sys/boot/efi/libefi/devpath.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/sys/boot/efi/include/efilib.h b/sys/boot/efi/include/efilib.h index ff88bc70454..72752c575ac 100644 --- a/sys/boot/efi/include/efilib.h +++ b/sys/boot/efi/include/efilib.h @@ -82,6 +82,7 @@ EFI_HANDLE efi_devpath_handle(EFI_DEVICE_PATH *); EFI_DEVICE_PATH *efi_devpath_last_node(EFI_DEVICE_PATH *); EFI_DEVICE_PATH *efi_devpath_trim(EFI_DEVICE_PATH *); bool efi_devpath_match(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *); +int efi_devpath_is_prefix(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *); CHAR16 *efi_devpath_name(EFI_DEVICE_PATH *); void efi_free_devpath_name(CHAR16 *); diff --git a/sys/boot/efi/libefi/devpath.c b/sys/boot/efi/libefi/devpath.c index 15f2a5aab41..fd863019fd3 100644 --- a/sys/boot/efi/libefi/devpath.c +++ b/sys/boot/efi/libefi/devpath.c @@ -166,3 +166,32 @@ efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2) } return (true); } + +int +efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path) +{ + int len; + + if (prefix == NULL || path == NULL) + return (0); + + while (1) { + if (IsDevicePathEnd(prefix)) + break; + + if (DevicePathType(prefix) != DevicePathType(path) || + DevicePathSubType(prefix) != DevicePathSubType(path)) + return (0); + + len = DevicePathNodeLength(prefix); + if (len != DevicePathNodeLength(path)) + return (0); + + if (memcmp(prefix, path, (size_t)len) != 0) + return (0); + + prefix = NextDevicePathNode(prefix); + path = NextDevicePathNode(path); + } + return (1); +}