git: f2b3bf5c4d4a - main - libefi: add efi_devpath_next_instance()

From: Toomas Soome <tsoome_at_FreeBSD.org>
Date: Sat, 13 May 2023 12:20:22 UTC
The branch main has been updated by tsoome:

URL: https://cgit.FreeBSD.org/src/commit/?id=f2b3bf5c4d4a391648e5b2740e81bccb85752ae5

commit f2b3bf5c4d4a391648e5b2740e81bccb85752ae5
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2023-05-12 15:41:14 +0000
Commit:     Toomas Soome <tsoome@FreeBSD.org>
CommitDate: 2023-05-13 12:16:10 +0000

    libefi: add efi_devpath_next_instance()
    
    UEFI device path may be path to one device, or concatenated list of instances
    to different devices (textually represented as comma separated list).
    
    Provide generic function to get next instance from device path.
    Returns next instance or end node.
    
    The use case is like:
    
    EFI_DEVICE_PATH *node = (EFI_DEVICE_PATH *)buf;
    while (!IsDevicePathEnd(node)) {
            process(node);
            node = efi_devpath_next_instance(node);
    }
    
    Where buf is pointing to either single device path or
    concatenated list of device paths (such as from ConIn or ConOut).
    
    Reviewers: imp
    Differential Revision: https://reviews.freebsd.org/D40081
---
 stand/efi/include/efilib.h |  1 +
 stand/efi/libefi/devpath.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/stand/efi/include/efilib.h b/stand/efi/include/efilib.h
index 1673ac97fdf2..df4ae6503760 100644
--- a/stand/efi/include/efilib.h
+++ b/stand/efi/include/efilib.h
@@ -97,6 +97,7 @@ void efi_close_devpath(EFI_HANDLE);
 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 *);
+EFI_DEVICE_PATH *efi_devpath_next_instance(EFI_DEVICE_PATH *);
 bool efi_devpath_match(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
 bool efi_devpath_match_node(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
 bool efi_devpath_is_prefix(EFI_DEVICE_PATH *, EFI_DEVICE_PATH *);
diff --git a/stand/efi/libefi/devpath.c b/stand/efi/libefi/devpath.c
index c25ae73e463c..94bb07540376 100644
--- a/stand/efi/libefi/devpath.c
+++ b/stand/efi/libefi/devpath.c
@@ -575,6 +575,23 @@ efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
 	return (devpath);
 }
 
+/*
+ * Walk device path nodes, return next instance or end node.
+ */
+EFI_DEVICE_PATH *
+efi_devpath_next_instance(EFI_DEVICE_PATH *devpath)
+{
+	while (!IsDevicePathEnd(devpath)) {
+		devpath = NextDevicePathNode(devpath);
+		if (IsDevicePathEndType(devpath) &&
+		    devpath->SubType == END_INSTANCE_DEVICE_PATH_SUBTYPE) {
+			devpath = NextDevicePathNode(devpath);
+			break;
+		}
+	}
+	return (devpath);
+}
+
 EFI_DEVICE_PATH *
 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
 {