2 Utility routines used by boot maintenance modules.
4 Copyright (c) 2004 - 2012, 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
,
181 ASSERT (!EFI_ERROR (Status
));
190 Function gets the file system information from an open file descriptor,
191 and stores it in a buffer allocated from pool.
194 @param FHand The file handle.
196 @return A pointer to a buffer with file information.
197 @retval NULL is returned if failed to get Vaolume Label Info.
200 EFI_FILE_SYSTEM_VOLUME_LABEL
*
201 EfiLibFileSystemVolumeLabelInfo (
202 IN EFI_FILE_HANDLE FHand
206 EFI_FILE_SYSTEM_VOLUME_LABEL
*Buffer
;
209 // Initialize for GrowBuffer loop
212 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL
+ 200;
215 // Call the real function
217 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
218 Status
= FHand
->GetInfo (
220 &gEfiFileSystemVolumeLabelInfoIdGuid
,
232 @param Src The source.
234 @return A new string which is duplicated copy of the source.
235 @retval NULL If there is not enough memory.
246 Size
= StrSize (Src
);
247 Dest
= AllocateZeroPool (Size
);
248 ASSERT (Dest
!= NULL
);
250 CopyMem (Dest
, Src
, Size
);
258 Function gets the file information from an open file descriptor, and stores it
259 in a buffer allocated from pool.
261 @param FHand File Handle.
263 @return A pointer to a buffer with file information or NULL is returned
268 IN EFI_FILE_HANDLE FHand
272 EFI_FILE_INFO
*Buffer
;
276 // Initialize for GrowBuffer loop
279 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
282 // Call the real function
284 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
285 Status
= FHand
->GetInfo (
297 Function is used to determine the number of device path instances
298 that exist in a device path.
301 @param DevicePath A pointer to a device path data structure.
303 @return This function counts and returns the number of device path instances
308 EfiDevicePathInstanceCount (
309 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
316 while (GetNextDevicePathInstance (&DevicePath
, &Size
) != NULL
) {
324 Adjusts the size of a previously allocated buffer.
327 @param OldPool - A pointer to the buffer whose size is being adjusted.
328 @param OldSize - The size of the current buffer.
329 @param NewSize - The size of the new buffer.
331 @return The newly allocated buffer.
332 @retval NULL Allocation failed.
346 NewPool
= AllocateZeroPool (NewSize
);
349 if (OldPool
!= NULL
) {
350 if (NewPool
!= NULL
) {
351 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
361 Get a string from the Data Hub record based on
364 @param DevPath The device Path.
366 @return A string located from the Data Hub records based on
368 @retval NULL If failed to get the String from Data Hub.
372 EfiLibStrFromDatahub (
373 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath