2 Utility routines used by boot maintenance modules.
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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
)) {
152 Function returns the value of the specified variable.
155 @param Name A Null-terminated Unicode string that is
156 the name of the vendor's variable.
157 @param VendorGuid A unique identifier for the vendor.
159 @return The payload of the variable.
160 @retval NULL If the variable can't be read.
166 IN EFI_GUID
*VendorGuid
171 return BdsLibGetVariableAndSize (Name
, VendorGuid
, &VarSize
);
175 Function deletes the variable specified by VarName and VarGuid.
177 @param VarName A Null-terminated Unicode string that is
178 the name of the vendor's variable.
180 @param VarGuid A unique identifier for the vendor.
182 @retval EFI_SUCCESS The variable was found and removed
183 @retval EFI_UNSUPPORTED The variable store was inaccessible
184 @retval EFI_OUT_OF_RESOURCES The temporary buffer was not available
185 @retval EFI_NOT_FOUND The variable was not found
189 EfiLibDeleteVariable (
197 VarBuf
= EfiLibGetVariable (VarName
, VarGuid
);
198 Status
= EFI_NOT_FOUND
;
200 if (VarBuf
!= NULL
) {
202 // Delete variable from Storage
204 Status
= gRT
->SetVariable (VarName
, VarGuid
, VAR_FLAG
, 0, NULL
);
205 ASSERT (!EFI_ERROR (Status
));
214 Function gets the file system information from an open file descriptor,
215 and stores it in a buffer allocated from pool.
218 @param FHand The file handle.
220 @return A pointer to a buffer with file information.
221 @retval NULL is returned if failed to get Vaolume Label Info.
224 EFI_FILE_SYSTEM_VOLUME_LABEL
*
225 EfiLibFileSystemVolumeLabelInfo (
226 IN EFI_FILE_HANDLE FHand
230 EFI_FILE_SYSTEM_VOLUME_LABEL
*Buffer
;
233 // Initialize for GrowBuffer loop
236 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL
+ 200;
239 // Call the real function
241 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
242 Status
= FHand
->GetInfo (
244 &gEfiFileSystemVolumeLabelInfoIdGuid
,
256 @param Src The source.
258 @return A new string which is duplicated copy of the source.
259 @retval NULL If there is not enough memory.
270 Size
= StrSize (Src
);
271 Dest
= AllocateZeroPool (Size
);
272 ASSERT (Dest
!= NULL
);
274 CopyMem (Dest
, Src
, Size
);
282 Function gets the file information from an open file descriptor, and stores it
283 in a buffer allocated from pool.
285 @param FHand File Handle.
287 @return A pointer to a buffer with file information or NULL is returned
292 IN EFI_FILE_HANDLE FHand
296 EFI_FILE_INFO
*Buffer
;
300 // Initialize for GrowBuffer loop
303 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
306 // Call the real function
308 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
309 Status
= FHand
->GetInfo (
321 Function is used to determine the number of device path instances
322 that exist in a device path.
325 @param DevicePath A pointer to a device path data structure.
327 @return This function counts and returns the number of device path instances
332 EfiDevicePathInstanceCount (
333 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
340 while (GetNextDevicePathInstance (&DevicePath
, &Size
) != NULL
) {
348 Adjusts the size of a previously allocated buffer.
351 @param OldPool - A pointer to the buffer whose size is being adjusted.
352 @param OldSize - The size of the current buffer.
353 @param NewSize - The size of the new buffer.
355 @return The newly allocated buffer.
356 @retval NULL Allocation failed.
370 NewPool
= AllocateZeroPool (NewSize
);
373 if (OldPool
!= NULL
) {
374 if (NewPool
!= NULL
) {
375 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
385 Compare two EFI_TIME data.
388 @param FirstTime - A pointer to the first EFI_TIME data.
389 @param SecondTime - A pointer to the second EFI_TIME data.
391 @retval TRUE The FirstTime is not later than the SecondTime.
392 @retval FALSE The FirstTime is later than the SecondTime.
397 IN EFI_TIME
*FirstTime
,
398 IN EFI_TIME
*SecondTime
401 if (FirstTime
->Year
!= SecondTime
->Year
) {
402 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
403 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
404 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
405 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
406 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
407 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
408 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
409 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
410 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
411 } else if (FirstTime
->Second
!= SecondTime
->Second
) {
412 return (BOOLEAN
) (FirstTime
->Second
< SecondTime
->Second
);
415 return (BOOLEAN
) (FirstTime
->Nanosecond
<= SecondTime
->Nanosecond
);
419 Get a string from the Data Hub record based on
422 @param DevPath The device Path.
424 @return A string located from the Data Hub records based on
426 @retval NULL If failed to get the String from Data Hub.
430 EfiLibStrFromDatahub (
431 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
436 EFI_DATA_HUB_PROTOCOL
*Datahub
;
438 EFI_DATA_RECORD_HEADER
*Record
;
439 EFI_SUBCLASS_TYPE1_HEADER
*DataHdr
;
441 EFI_MISC_ONBOARD_DEVICE_DATA
*Ob
;
442 EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*Port
;
445 CopyGuid (&MiscGuid
, &gEfiMiscSubClassGuid
);
447 Status
= gBS
->LocateProtocol (
448 &gEfiDataHubProtocolGuid
,
452 if (EFI_ERROR (Status
)) {
456 Status
= gRT
->GetTime (&CurTime
, NULL
);
457 if (EFI_ERROR (Status
)) {
463 Status
= Datahub
->GetNextRecord (Datahub
, &Count
, NULL
, &Record
);
465 if (EFI_ERROR (Status
)) {
469 if (Record
->DataRecordClass
== EFI_DATA_RECORD_CLASS_DATA
&& CompareGuid (&Record
->DataRecordGuid
, &MiscGuid
)) {
471 // This record is what we need
473 DataHdr
= (EFI_SUBCLASS_TYPE1_HEADER
*) (Record
+ 1);
474 if (EFI_MISC_ONBOARD_DEVICE_RECORD_NUMBER
== DataHdr
->RecordType
) {
475 Ob
= (EFI_MISC_ONBOARD_DEVICE_DATA
*) (DataHdr
+ 1);
476 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &Ob
->OnBoardDevicePath
, DevPath
)) {
477 GetProducerString (&Record
->ProducerName
, Ob
->OnBoardDeviceDescription
, &Desc
);
482 if (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_RECORD_NUMBER
== DataHdr
->RecordType
) {
483 Port
= (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*) (DataHdr
+ 1);
484 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &Port
->PortPath
, DevPath
)) {
485 GetProducerString (&Record
->ProducerName
, Port
->PortExternalConnectorDesignator
, &Desc
);
491 } while (TimeCompare (&Record
->LogTime
, &CurTime
) && Count
!= 0);