2 Utility routines used by boot maintenance modules.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BootMaint.h"
19 Find the first instance of this Protocol
20 in the system and return it's interface.
23 @param ProtocolGuid Provides the protocol to search for
24 @param Interface On return, a pointer to the first interface
25 that matches ProtocolGuid
27 @retval EFI_SUCCESS A protocol instance matching ProtocolGuid was found
28 @retval EFI_NOT_FOUND No protocol instances were found that match ProtocolGuid
32 EfiLibLocateProtocol (
33 IN EFI_GUID
*ProtocolGuid
,
39 Status
= gBS
->LocateProtocol (
49 Function opens and returns a file handle to the root directory of a volume.
51 @param DeviceHandle A handle for a device
53 @return A valid file handle or NULL is returned
58 IN EFI_HANDLE DeviceHandle
62 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
68 // File the file system interface to the device
70 Status
= gBS
->HandleProtocol (
72 &gEfiSimpleFileSystemProtocolGuid
,
77 // Open the root directory of the volume
79 if (!EFI_ERROR (Status
)) {
80 Status
= Volume
->OpenVolume (
88 return EFI_ERROR (Status
) ? NULL
: File
;
93 Helper function called as part of the code needed
94 to allocate the proper sized buffer for various
98 @param Status Current status
99 @param Buffer Current allocated buffer, or NULL
100 @param BufferSize Current buffer size needed
102 @retval TRUE if the buffer was reallocated and the caller
103 should try the API again.
104 @retval FALSE The caller should not call this function again.
109 IN OUT EFI_STATUS
*Status
,
110 IN OUT VOID
**Buffer
,
117 // If this is an initial request, buffer will be null with a new buffer size
119 if ((*Buffer
== NULL
) && (BufferSize
!= 0)) {
120 *Status
= EFI_BUFFER_TOO_SMALL
;
123 // If the status code is "buffer too small", resize the buffer
126 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
128 if (*Buffer
!= NULL
) {
132 *Buffer
= AllocateZeroPool (BufferSize
);
134 if (*Buffer
!= NULL
) {
137 *Status
= EFI_OUT_OF_RESOURCES
;
141 // If there's an error, free the buffer
143 if (!TryAgain
&& EFI_ERROR (*Status
) && (*Buffer
!= NULL
)) {
153 Function deletes the variable specified by VarName and VarGuid.
155 @param VarName A Null-terminated Unicode string that is
156 the name of the vendor's variable.
158 @param VarGuid A unique identifier for the vendor.
160 @retval EFI_SUCCESS The variable was found and removed
161 @retval EFI_UNSUPPORTED The variable store was inaccessible
162 @retval EFI_NOT_FOUND The variable was not found
166 EfiLibDeleteVariable (
171 return gRT
->SetVariable (
182 Function gets the file system information from an open file descriptor,
183 and stores it in a buffer allocated from pool.
186 @param FHand The file handle.
188 @return A pointer to a buffer with file information.
189 @retval NULL is returned if failed to get Vaolume Label Info.
192 EFI_FILE_SYSTEM_VOLUME_LABEL
*
193 EfiLibFileSystemVolumeLabelInfo (
194 IN EFI_FILE_HANDLE FHand
198 EFI_FILE_SYSTEM_VOLUME_LABEL
*Buffer
;
201 // Initialize for GrowBuffer loop
204 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL
+ 200;
207 // Call the real function
209 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
210 Status
= FHand
->GetInfo (
212 &gEfiFileSystemVolumeLabelInfoIdGuid
,
224 @param Src The source.
226 @return A new string which is duplicated copy of the source.
227 @retval NULL If there is not enough memory.
238 Size
= StrSize (Src
);
239 Dest
= AllocateZeroPool (Size
);
240 ASSERT (Dest
!= NULL
);
242 CopyMem (Dest
, Src
, Size
);
250 Function gets the file information from an open file descriptor, and stores it
251 in a buffer allocated from pool.
253 @param FHand File Handle.
255 @return A pointer to a buffer with file information or NULL is returned
260 IN EFI_FILE_HANDLE FHand
264 EFI_FILE_INFO
*Buffer
;
268 // Initialize for GrowBuffer loop
271 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
274 // Call the real function
276 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
277 Status
= FHand
->GetInfo (
289 Function is used to determine the number of device path instances
290 that exist in a device path.
293 @param DevicePath A pointer to a device path data structure.
295 @return This function counts and returns the number of device path instances
300 EfiDevicePathInstanceCount (
301 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
308 while (GetNextDevicePathInstance (&DevicePath
, &Size
) != NULL
) {
316 Adjusts the size of a previously allocated buffer.
319 @param OldPool - A pointer to the buffer whose size is being adjusted.
320 @param OldSize - The size of the current buffer.
321 @param NewSize - The size of the new buffer.
323 @return The newly allocated buffer.
324 @retval NULL Allocation failed.
338 NewPool
= AllocateZeroPool (NewSize
);
341 if (OldPool
!= NULL
) {
342 if (NewPool
!= NULL
) {
343 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
353 Get a string from the Data Hub record based on
356 @param DevPath The device Path.
358 @return A string located from the Data Hub records based on
360 @retval NULL If failed to get the String from Data Hub.
364 EfiLibStrFromDatahub (
365 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath