From 771729c77fa49cf0ff17491f371003c4f5d66f85 Mon Sep 17 00:00:00 2001 From: niruiyu Date: Mon, 24 Sep 2012 03:20:35 +0000 Subject: Add new API IsDevicePathValid() to UefiDevicePathLib. Signed-off-by: Ruiyu Ni Reviewed-by: Liming Gao Reviewed-by: Kinney Michael D git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13737 6f19259b-4bc3-4df7-8a09-765794883524 --- .../UefiDevicePathLib.c | 95 ++++++++++++++++++---- .../UefiDevicePathLibDevicePathProtocol.inf | 7 +- 2 files changed, 85 insertions(+), 17 deletions(-) (limited to 'MdePkg/Library/UefiDevicePathLibDevicePathProtocol') diff --git a/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c b/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c index 8275dd611a..1052988e73 100644 --- a/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c +++ b/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c @@ -2,7 +2,7 @@ Library instance that implement UEFI Device Path Library class based on protocol gEfiDevicePathUtilitiesProtocolGuid. - Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -24,6 +24,7 @@ #include #include #include +#include EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathUtilities = NULL; @@ -71,6 +72,61 @@ DevicePathLibConstructor ( return Status; } +/** + Determine whether a given device path is valid. + If DevicePath is NULL, then ASSERT(). + + @param DevicePath A pointer to a device path data structure. + @param MaxSize The maximum size of the device path data structure. + + @retval TRUE DevicePath is valid. + @retval FALSE The length of any node node in the DevicePath is less + than sizeof (EFI_DEVICE_PATH_PROTOCOL). + @retval FALSE If MaxSize is not zero, the size of the DevicePath + exceeds MaxSize. + @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node + count of the DevicePath exceeds PcdMaximumDevicePathNodeCount. +**/ +BOOLEAN +EFIAPI +IsDevicePathValid ( + IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN UINTN MaxSize + ) +{ + UINTN Count; + UINTN Size; + UINTN NodeLength; + + ASSERT (DevicePath != NULL); + + for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) { + NodeLength = DevicePathNodeLength (DevicePath); + if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) { + return FALSE; + } + + if (MaxSize > 0) { + Size += NodeLength; + if (Size + END_DEVICE_PATH_LENGTH > MaxSize) { + return FALSE; + } + } + + if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) { + Count++; + if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) { + return FALSE; + } + } + } + + // + // Only return TRUE when the End Device Path node is valid. + // + return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH); +} + /** Returns the Type field of a device path node. @@ -136,8 +192,12 @@ DevicePathNodeLength ( IN CONST VOID *Node ) { + UINTN Length; + ASSERT (Node != NULL); - return ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]); + Length = ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0]); + ASSERT (Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)); + return Length; } /** @@ -256,7 +316,8 @@ IsDevicePathEndInstance ( be used to set the contents of the Length field. If Node is NULL, then ASSERT(). - If NodeLength >= 0x10000, then ASSERT(). + If NodeLength >= SIZE_64KB, then ASSERT(). + If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT(). @param Node A pointer to a device path node data structure. @param Length The length, in bytes, of the device path node. @@ -272,7 +333,7 @@ SetDevicePathNodeLength ( ) { ASSERT (Node != NULL); - ASSERT (Length < 0x10000); + ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB)); return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length)); } @@ -305,13 +366,14 @@ SetDevicePathEndNode ( /** Returns the size of a device path in bytes. - This function returns the size, in bytes, of the device path data structure specified by - DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned. + This function returns the size, in bytes, of the device path data structure + specified by DevicePath including the end of device path node. + If DevicePath is NULL or invalid, then 0 is returned. - @param DevicePath A pointer to a device path data structure. - - @retval 0 If DevicePath is NULL. - @retval Others The size of a device path in bytes. + @param DevicePath A pointer to a device path data structure. + + @retval 0 If DevicePath is NULL or invalid. + @retval Others The size of a device path in bytes. **/ UINTN @@ -336,7 +398,7 @@ GetDevicePathSize ( @param DevicePath A pointer to a device path data structure. - @retval NULL If DevicePath is NULL. + @retval NULL If DevicePath is NULL or invalid. @retval Others A pointer to the duplicated device path. **/ @@ -367,6 +429,7 @@ DuplicateDevicePath ( @param SecondDevicePath A pointer to a device path data structure. @retval NULL If there is not enough memory for the newly allocated buffer. + @retval NULL If FirstDevicePath or SecondDevicePath is invalid. @retval Others A pointer to the new device path if success. Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL. @@ -431,6 +494,7 @@ AppendDevicePathNode ( path instance and a new end-of-device-path-instance node is inserted between. If DevicePath is NULL, then a copy if DevicePathInstance is returned. If DevicePathInstance is NULL, then NULL is returned. + If DevicePath or DevicePathInstance is invalid, then NULL is returned. If there is not enough memory to allocate space for the new device path, then NULL is returned. The memory is allocated from EFI boot services memory. It is the responsibility @@ -524,15 +588,16 @@ CreateDeviceNode ( /** Determines if a device path is single or multi-instance. - This function returns TRUE if the device path specified by DevicePath is + This function returns TRUE if the device path specified by DevicePath is multi-instance. - Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned. + Otherwise, FALSE is returned. + If DevicePath is NULL or invalid, then FALSE is returned. @param DevicePath A pointer to a device path data structure. @retval TRUE DevicePath is multi-instance. - @retval FALSE DevicePath is not multi-instance or DevicePath - is NULL. + @retval FALSE DevicePath is not multi-instance, or DevicePath + is NULL or invalid. **/ BOOLEAN diff --git a/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf b/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf index 3393830d60..16b47e3883 100644 --- a/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf +++ b/MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf @@ -4,7 +4,7 @@ # Device Path Library that layers on top of the UEFI 2.0 Device Path Protocol. # This library is not available for EFI 1.10 modules. # -# Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.
# # This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License @@ -44,11 +44,14 @@ MemoryAllocationLib BaseLib DebugLib - + PcdLib [Protocols] gEfiDevicePathProtocolGuid ## CONSUMES gEfiDevicePathUtilitiesProtocolGuid ## CONSUMES +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdMaximumDevicePathNodeCount + [Depex.common.DXE_DRIVER, Depex.common.DXE_RUNTIME_DRIVER, Depex.common.DXE_SAL_DRIVER, Depex.common.DXE_SMM_DRIVER] gEfiDevicePathUtilitiesProtocolGuid -- cgit v1.2.3