2 Utility routines used by boot maintenance modules.
4 Copyright (c) 2004 - 2018, 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 Function opens and returns a file handle to the root directory of a volume.
21 @param DeviceHandle A handle for a device
23 @return A valid file handle or NULL is returned
28 IN EFI_HANDLE DeviceHandle
32 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
38 // File the file system interface to the device
40 Status
= gBS
->HandleProtocol (
42 &gEfiSimpleFileSystemProtocolGuid
,
47 // Open the root directory of the volume
49 if (!EFI_ERROR (Status
)) {
50 Status
= Volume
->OpenVolume (
58 return EFI_ERROR (Status
) ? NULL
: File
;
63 Helper function called as part of the code needed
64 to allocate the proper sized buffer for various
68 @param Status Current status
69 @param Buffer Current allocated buffer, or NULL
70 @param BufferSize Current buffer size needed
72 @retval TRUE if the buffer was reallocated and the caller
73 should try the API again.
74 @retval FALSE The caller should not call this function again.
79 IN OUT EFI_STATUS
*Status
,
87 // If this is an initial request, buffer will be null with a new buffer size
89 if ((*Buffer
== NULL
) && (BufferSize
!= 0)) {
90 *Status
= EFI_BUFFER_TOO_SMALL
;
93 // If the status code is "buffer too small", resize the buffer
96 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
98 if (*Buffer
!= NULL
) {
102 *Buffer
= AllocateZeroPool (BufferSize
);
104 if (*Buffer
!= NULL
) {
107 *Status
= EFI_OUT_OF_RESOURCES
;
111 // If there's an error, free the buffer
113 if (!TryAgain
&& EFI_ERROR (*Status
) && (*Buffer
!= NULL
)) {
122 Function returns the value of the specified variable.
125 @param Name A Null-terminated Unicode string that is
126 the name of the vendor's variable.
127 @param VendorGuid A unique identifier for the vendor.
129 @return The payload of the variable.
130 @retval NULL If the variable can't be read.
136 IN EFI_GUID
*VendorGuid
141 return BdsLibGetVariableAndSize (Name
, VendorGuid
, &VarSize
);
145 Function deletes the variable specified by VarName and VarGuid.
147 @param VarName A Null-terminated Unicode string that is
148 the name of the vendor's variable.
150 @param VarGuid A unique identifier for the vendor.
152 @retval EFI_SUCCESS The variable was found and removed
153 @retval EFI_UNSUPPORTED The variable store was inaccessible
154 @retval EFI_OUT_OF_RESOURCES The temporary buffer was not available
155 @retval EFI_NOT_FOUND The variable was not found
159 EfiLibDeleteVariable (
167 VarBuf
= EfiLibGetVariable (VarName
, VarGuid
);
168 Status
= EFI_NOT_FOUND
;
170 if (VarBuf
!= NULL
) {
172 // Delete variable from Storage
174 Status
= gRT
->SetVariable (
177 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
182 // Deleting variable with current variable implementation shouldn't fail.
184 ASSERT_EFI_ERROR (Status
);
193 Function gets the file system information from an open file descriptor,
194 and stores it in a buffer allocated from pool.
197 @param FHand The file handle.
199 @return A pointer to a buffer with file information.
200 @retval NULL is returned if failed to get Vaolume Label Info.
203 EFI_FILE_SYSTEM_VOLUME_LABEL
*
204 EfiLibFileSystemVolumeLabelInfo (
205 IN EFI_FILE_HANDLE FHand
209 EFI_FILE_SYSTEM_VOLUME_LABEL
*Buffer
;
212 // Initialize for GrowBuffer loop
215 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL
+ 200;
218 // Call the real function
220 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
221 Status
= FHand
->GetInfo (
223 &gEfiFileSystemVolumeLabelInfoIdGuid
,
235 @param Src The source.
237 @return A new string which is duplicated copy of the source.
238 @retval NULL If there is not enough memory.
249 Size
= StrSize (Src
);
250 Dest
= AllocateZeroPool (Size
);
251 ASSERT (Dest
!= NULL
);
253 CopyMem (Dest
, Src
, Size
);
261 Function gets the file information from an open file descriptor, and stores it
262 in a buffer allocated from pool.
264 @param FHand File Handle.
266 @return A pointer to a buffer with file information or NULL is returned
271 IN EFI_FILE_HANDLE FHand
275 EFI_FILE_INFO
*Buffer
;
279 // Initialize for GrowBuffer loop
282 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
285 // Call the real function
287 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
288 Status
= FHand
->GetInfo (
300 Function is used to determine the number of device path instances
301 that exist in a device path.
304 @param DevicePath A pointer to a device path data structure.
306 @return This function counts and returns the number of device path instances
311 EfiDevicePathInstanceCount (
312 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
319 while (GetNextDevicePathInstance (&DevicePath
, &Size
) != NULL
) {
327 Adjusts the size of a previously allocated buffer.
330 @param OldPool - A pointer to the buffer whose size is being adjusted.
331 @param OldSize - The size of the current buffer.
332 @param NewSize - The size of the new buffer.
334 @return The newly allocated buffer.
335 @retval NULL Allocation failed.
349 NewPool
= AllocateZeroPool (NewSize
);
352 if (OldPool
!= NULL
) {
353 if (NewPool
!= NULL
) {
354 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
364 Get a string from the Data Hub record based on
367 @param DevPath The device Path.
369 @return A string located from the Data Hub records based on
371 @retval NULL If failed to get the String from Data Hub.
375 EfiLibStrFromDatahub (
376 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
384 Find the first instance of this Protocol
385 in the system and return it's interface.
388 @param ProtocolGuid Provides the protocol to search for
389 @param Interface On return, a pointer to the first interface
390 that matches ProtocolGuid
392 @retval EFI_SUCCESS A protocol instance matching ProtocolGuid was found
393 @retval EFI_NOT_FOUND No protocol instances were found that match ProtocolGuid
397 EfiLibLocateProtocol (
398 IN EFI_GUID
*ProtocolGuid
,
404 Status
= gBS
->LocateProtocol (