From 8c7bc280bfae00561ef35bde14597ee372d24d17 Mon Sep 17 00:00:00 2001 From: Download-Fritz Date: Mon, 15 Jul 2019 09:16:53 +0200 Subject: [PATCH] OcDevicePathLib: Add deduplicate DP instance append API --- Include/Library/OcDevicePathLib.h | 6 +++ Library/OcDevicePathLib/OcDevicePathLib.c | 45 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/Include/Library/OcDevicePathLib.h b/Include/Library/OcDevicePathLib.h index 956d0447..3d0a1c78 100755 --- a/Include/Library/OcDevicePathLib.h +++ b/Include/Library/OcDevicePathLib.h @@ -157,6 +157,12 @@ OcFileDevicePathNameLen ( IN CONST FILEPATH_DEVICE_PATH *FilePath ); +EFI_DEVICE_PATH_PROTOCOL * +OcAppendDevicePathInstanceDedupe ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL, + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL + ); + /** Fix Apple Boot Device Path to be compatible with conventional UEFI implementations. diff --git a/Library/OcDevicePathLib/OcDevicePathLib.c b/Library/OcDevicePathLib/OcDevicePathLib.c index f6122a0c..3a9ec4d5 100755 --- a/Library/OcDevicePathLib/OcDevicePathLib.c +++ b/Library/OcDevicePathLib/OcDevicePathLib.c @@ -735,3 +735,48 @@ OcFileDevicePathNameLen ( return Len; } + +EFI_DEVICE_PATH_PROTOCOL * +OcAppendDevicePathInstanceDedupe ( + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL, + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL + ) +{ + INTN CmpResult; + + EFI_DEVICE_PATH_PROTOCOL *DevPathWalker; + CONST EFI_DEVICE_PATH_PROTOCOL *CurrentInstance; + + UINTN AppendInstanceSize; + UINTN CurrentInstanceSize; + + if (DevicePath != NULL && DevicePathInstance != NULL) { + AppendInstanceSize = GetDevicePathSize (DevicePathInstance); + DevPathWalker = DevicePath; + + while (TRUE) { + CurrentInstance = GetNextDevicePathInstance ( + &DevPathWalker, + &CurrentInstanceSize + ); + if (CurrentInstance == NULL) { + break; + } + + if (CurrentInstanceSize != AppendInstanceSize) { + continue; + } + + CmpResult = CompareMem ( + CurrentInstance, + DevicePathInstance, + CurrentInstanceSize + ); + if (CmpResult == 0) { + return DuplicateDevicePath (DevicePath); + } + } + } + + return AppendDevicePathInstance (DevicePath, DevicePathInstance); +}