+++ /dev/null
-/** @file\r
- Utility routines used by boot maintenance modules.\r
-\r
-Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-#include "BootMaint.h"\r
-\r
-/**\r
-\r
- Function opens and returns a file handle to the root directory of a volume.\r
-\r
- @param DeviceHandle A handle for a device\r
-\r
- @return A valid file handle or NULL is returned\r
-\r
-**/\r
-EFI_FILE_HANDLE\r
-EfiLibOpenRoot (\r
- IN EFI_HANDLE DeviceHandle\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
- EFI_FILE_HANDLE File;\r
-\r
- File = NULL;\r
-\r
- //\r
- // File the file system interface to the device\r
- //\r
- Status = gBS->HandleProtocol (\r
- DeviceHandle,\r
- &gEfiSimpleFileSystemProtocolGuid,\r
- (VOID *) &Volume\r
- );\r
-\r
- //\r
- // Open the root directory of the volume\r
- //\r
- if (!EFI_ERROR (Status)) {\r
- Status = Volume->OpenVolume (\r
- Volume,\r
- &File\r
- );\r
- }\r
- //\r
- // Done\r
- //\r
- return EFI_ERROR (Status) ? NULL : File;\r
-}\r
-\r
-/**\r
-\r
- Helper function called as part of the code needed\r
- to allocate the proper sized buffer for various\r
- EFI interfaces.\r
-\r
-\r
- @param Status Current status\r
- @param Buffer Current allocated buffer, or NULL\r
- @param BufferSize Current buffer size needed\r
-\r
- @retval TRUE if the buffer was reallocated and the caller\r
- should try the API again.\r
- @retval FALSE The caller should not call this function again.\r
-\r
-**/\r
-BOOLEAN\r
-EfiGrowBuffer (\r
- IN OUT EFI_STATUS *Status,\r
- IN OUT VOID **Buffer,\r
- IN UINTN BufferSize\r
- )\r
-{\r
- BOOLEAN TryAgain;\r
-\r
- //\r
- // If this is an initial request, buffer will be null with a new buffer size\r
- //\r
- if ((*Buffer == NULL) && (BufferSize != 0)) {\r
- *Status = EFI_BUFFER_TOO_SMALL;\r
- }\r
- //\r
- // If the status code is "buffer too small", resize the buffer\r
- //\r
- TryAgain = FALSE;\r
- if (*Status == EFI_BUFFER_TOO_SMALL) {\r
-\r
- if (*Buffer != NULL) {\r
- FreePool (*Buffer);\r
- }\r
-\r
- *Buffer = AllocateZeroPool (BufferSize);\r
-\r
- if (*Buffer != NULL) {\r
- TryAgain = TRUE;\r
- } else {\r
- *Status = EFI_OUT_OF_RESOURCES;\r
- }\r
- }\r
- //\r
- // If there's an error, free the buffer\r
- //\r
- if (!TryAgain && EFI_ERROR (*Status) && (*Buffer != NULL)) {\r
- FreePool (*Buffer);\r
- *Buffer = NULL;\r
- }\r
-\r
- return TryAgain;\r
-}\r
-\r
-/**\r
- Function returns the value of the specified variable.\r
-\r
-\r
- @param Name A Null-terminated Unicode string that is\r
- the name of the vendor's variable.\r
- @param VendorGuid A unique identifier for the vendor.\r
-\r
- @return The payload of the variable.\r
- @retval NULL If the variable can't be read.\r
-\r
-**/\r
-VOID *\r
-EfiLibGetVariable (\r
- IN CHAR16 *Name,\r
- IN EFI_GUID *VendorGuid\r
- )\r
-{\r
- UINTN VarSize;\r
-\r
- return BdsLibGetVariableAndSize (Name, VendorGuid, &VarSize);\r
-}\r
-\r
-/**\r
- Function deletes the variable specified by VarName and VarGuid.\r
-\r
- @param VarName A Null-terminated Unicode string that is\r
- the name of the vendor's variable.\r
-\r
- @param VarGuid A unique identifier for the vendor.\r
-\r
- @retval EFI_SUCCESS The variable was found and removed\r
- @retval EFI_UNSUPPORTED The variable store was inaccessible\r
- @retval EFI_OUT_OF_RESOURCES The temporary buffer was not available\r
- @retval EFI_NOT_FOUND The variable was not found\r
-\r
-**/\r
-EFI_STATUS\r
-EfiLibDeleteVariable (\r
- IN CHAR16 *VarName,\r
- IN EFI_GUID *VarGuid\r
- )\r
-{\r
- VOID *VarBuf;\r
- EFI_STATUS Status;\r
-\r
- VarBuf = EfiLibGetVariable (VarName, VarGuid);\r
- Status = EFI_NOT_FOUND;\r
-\r
- if (VarBuf != NULL) {\r
- //\r
- // Delete variable from Storage\r
- //\r
- Status = gRT->SetVariable (\r
- VarName,\r
- VarGuid,\r
- EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
- 0,\r
- NULL\r
- );\r
- //\r
- // Deleting variable with current variable implementation shouldn't fail.\r
- //\r
- ASSERT_EFI_ERROR (Status);\r
- FreePool (VarBuf);\r
- }\r
-\r
- return Status;\r
-}\r
-\r
-/**\r
-\r
- Function gets the file system information from an open file descriptor,\r
- and stores it in a buffer allocated from pool.\r
-\r
-\r
- @param FHand The file handle.\r
-\r
- @return A pointer to a buffer with file information.\r
- @retval NULL is returned if failed to get Vaolume Label Info.\r
-\r
-**/\r
-EFI_FILE_SYSTEM_VOLUME_LABEL *\r
-EfiLibFileSystemVolumeLabelInfo (\r
- IN EFI_FILE_HANDLE FHand\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_FILE_SYSTEM_VOLUME_LABEL *Buffer;\r
- UINTN BufferSize;\r
- //\r
- // Initialize for GrowBuffer loop\r
- //\r
- Buffer = NULL;\r
- BufferSize = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL + 200;\r
-\r
- //\r
- // Call the real function\r
- //\r
- while (EfiGrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
- Status = FHand->GetInfo (\r
- FHand,\r
- &gEfiFileSystemVolumeLabelInfoIdGuid,\r
- &BufferSize,\r
- Buffer\r
- );\r
- }\r
-\r
- return Buffer;\r
-}\r
-\r
-/**\r
- Duplicate a string.\r
-\r
- @param Src The source.\r
-\r
- @return A new string which is duplicated copy of the source.\r
- @retval NULL If there is not enough memory.\r
-\r
-**/\r
-CHAR16 *\r
-EfiStrDuplicate (\r
- IN CHAR16 *Src\r
- )\r
-{\r
- CHAR16 *Dest;\r
- UINTN Size;\r
-\r
- Size = StrSize (Src);\r
- Dest = AllocateZeroPool (Size);\r
- ASSERT (Dest != NULL);\r
- if (Dest != NULL) {\r
- CopyMem (Dest, Src, Size);\r
- }\r
-\r
- return Dest;\r
-}\r
-\r
-/**\r
-\r
- Function gets the file information from an open file descriptor, and stores it\r
- in a buffer allocated from pool.\r
-\r
- @param FHand File Handle.\r
-\r
- @return A pointer to a buffer with file information or NULL is returned\r
-\r
-**/\r
-EFI_FILE_INFO *\r
-EfiLibFileInfo (\r
- IN EFI_FILE_HANDLE FHand\r
- )\r
-{\r
- EFI_STATUS Status;\r
- EFI_FILE_INFO *Buffer;\r
- UINTN BufferSize;\r
-\r
- //\r
- // Initialize for GrowBuffer loop\r
- //\r
- Buffer = NULL;\r
- BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r
-\r
- //\r
- // Call the real function\r
- //\r
- while (EfiGrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {\r
- Status = FHand->GetInfo (\r
- FHand,\r
- &gEfiFileInfoGuid,\r
- &BufferSize,\r
- Buffer\r
- );\r
- }\r
-\r
- return Buffer;\r
-}\r
-\r
-/**\r
- Function is used to determine the number of device path instances\r
- that exist in a device path.\r
-\r
-\r
- @param DevicePath A pointer to a device path data structure.\r
-\r
- @return This function counts and returns the number of device path instances\r
- in DevicePath.\r
-\r
-**/\r
-UINTN\r
-EfiDevicePathInstanceCount (\r
- IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
- )\r
-{\r
- UINTN Count;\r
- UINTN Size;\r
-\r
- Count = 0;\r
- while (GetNextDevicePathInstance (&DevicePath, &Size) != NULL) {\r
- Count += 1;\r
- }\r
-\r
- return Count;\r
-}\r
-\r
-\r
-/**\r
- Get a string from the Data Hub record based on\r
- a device path.\r
-\r
- @param DevPath The device Path.\r
-\r
- @return A string located from the Data Hub records based on\r
- the device path.\r
- @retval NULL If failed to get the String from Data Hub.\r
-\r
-**/\r
-UINT16 *\r
-EfiLibStrFromDatahub (\r
- IN EFI_DEVICE_PATH_PROTOCOL *DevPath\r
- )\r
-{\r
- return NULL;\r
-}\r
-\r
-/**\r
-\r
- Find the first instance of this Protocol\r
- in the system and return it's interface.\r
-\r
-\r
- @param ProtocolGuid Provides the protocol to search for\r
- @param Interface On return, a pointer to the first interface\r
- that matches ProtocolGuid\r
-\r
- @retval EFI_SUCCESS A protocol instance matching ProtocolGuid was found\r
- @retval EFI_NOT_FOUND No protocol instances were found that match ProtocolGuid\r
-\r
-**/\r
-EFI_STATUS\r
-EfiLibLocateProtocol (\r
- IN EFI_GUID *ProtocolGuid,\r
- OUT VOID **Interface\r
- )\r
-{\r
- EFI_STATUS Status;\r
-\r
- Status = gBS->LocateProtocol (\r
- ProtocolGuid,\r
- NULL,\r
- (VOID **) Interface\r
- );\r
- return Status;\r
-}\r
-\r