3 Copyright (c) 2004 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Boot Maintainence Helper functions
22 #include "BootMaint.h"
31 Wrap original AllocatePool gBS call
32 and ZeroMem gBS call into a single
33 function in order to decrease code length
38 Valid pointer to the allocated buffer
45 Status
= gBS
->AllocatePool (EfiBootServicesData
, Size
, &Ptr
);
46 if (EFI_ERROR (Status
)) {
56 EfiLibLocateProtocol (
57 IN EFI_GUID
*ProtocolGuid
,
64 Find the first instance of this Protocol
65 in the system and return it's interface
69 ProtocolGuid - Provides the protocol to search for
70 Interface - On return, a pointer to the first interface
71 that matches ProtocolGuid
75 EFI_SUCCESS - A protocol instance matching ProtocolGuid was found
77 EFI_NOT_FOUND - No protocol instances were found that match ProtocolGuid
83 Status
= gBS
->LocateProtocol (
93 IN EFI_HANDLE DeviceHandle
99 Function opens and returns a file handle to the root directory of a volume.
103 DeviceHandle - A handle for a device
107 A valid file handle or NULL is returned
112 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
113 EFI_FILE_HANDLE File
;
118 // File the file system interface to the device
120 Status
= gBS
->HandleProtocol (
122 &gEfiSimpleFileSystemProtocolGuid
,
127 // Open the root directory of the volume
129 if (!EFI_ERROR (Status
)) {
130 Status
= Volume
->OpenVolume (
138 return EFI_ERROR (Status
) ? NULL
: File
;
143 IN OUT EFI_STATUS
*Status
,
144 IN OUT VOID
**Buffer
,
151 Helper function called as part of the code needed
152 to allocate the proper sized buffer for various
157 Status - Current status
159 Buffer - Current allocated buffer, or NULL
161 BufferSize - Current buffer size needed
165 TRUE - if the buffer was reallocated and the caller
166 should try the API again.
173 // If this is an initial request, buffer will be null with a new buffer size
175 if (!*Buffer
&& BufferSize
) {
176 *Status
= EFI_BUFFER_TOO_SMALL
;
179 // If the status code is "buffer too small", resize the buffer
182 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
184 SafeFreePool (*Buffer
);
186 *Buffer
= EfiAllocateZeroPool (BufferSize
);
191 *Status
= EFI_OUT_OF_RESOURCES
;
195 // If there's an error, free the buffer
197 if (!TryAgain
&& EFI_ERROR (*Status
) && *Buffer
) {
198 SafeFreePool (*Buffer
);
208 IN EFI_GUID
*VendorGuid
213 Function returns the value of the specified variable.
216 Name - A Null-terminated Unicode string that is
217 the name of the vendor's variable.
219 VendorGuid - A unique identifier for the vendor.
229 return BdsLibGetVariableAndSize (Name
, VendorGuid
, &VarSize
);
233 EfiLibDeleteVariable (
240 Function deletes the variable specified by VarName and VarGuid.
243 VarName - A Null-terminated Unicode string that is
244 the name of the vendor's variable.
246 VendorGuid - A unique identifier for the vendor.
250 EFI_SUCCESS - The variable was found and removed
252 EFI_UNSUPPORTED - The variable store was inaccessible
254 EFI_OUT_OF_RESOURCES - The temporary buffer was not available
256 EFI_NOT_FOUND - The variable was not found
263 VarBuf
= EfiLibGetVariable (VarName
, VarGuid
);
264 Status
= EFI_NOT_FOUND
;
268 // Delete variable from Storage
270 Status
= gRT
->SetVariable (VarName
, VarGuid
, VAR_FLAG
, 0, NULL
);
271 ASSERT (!EFI_ERROR (Status
));
272 SafeFreePool (VarBuf
);
278 EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*
279 EfiLibFileSystemVolumeLabelInfo (
280 IN EFI_FILE_HANDLE FHand
286 Function gets the file system information from an open file descriptor,
287 and stores it in a buffer allocated from pool.
291 Fhand - A file handle
295 A pointer to a buffer with file information or NULL is returned
300 EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*Buffer
;
303 // Initialize for GrowBuffer loop
306 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
+ 200;
309 // Call the real function
311 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
312 Status
= FHand
->GetInfo (
314 &gEfiFileSystemVolumeLabelInfoIdGuid
,
331 Size
= StrSize (Src
);
332 Dest
= EfiAllocateZeroPool (Size
);
333 ASSERT (Dest
!= NULL
);
335 CopyMem (Dest
, Src
, Size
);
343 IN EFI_FILE_HANDLE FHand
349 Function gets the file information from an open file descriptor, and stores it
350 in a buffer allocated from pool.
354 Fhand - A file handle
358 A pointer to a buffer with file information or NULL is returned
363 EFI_FILE_INFO
*Buffer
;
367 // Initialize for GrowBuffer loop
370 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
373 // Call the real function
375 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
376 Status
= FHand
->GetInfo (
388 EfiDevicePathInstanceCount (
389 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
394 Function is used to determine the number of device path instances
395 that exist in a device path.
398 DevicePath - A pointer to a device path data structure.
402 This function counts and returns the number of device path instances
411 while (GetNextDevicePathInstance (&DevicePath
, &Size
)) {
427 Adjusts the size of a previously allocated buffer.
430 OldPool - A pointer to the buffer whose size is being adjusted.
431 OldSize - The size of the current buffer.
432 NewSize - The size of the new buffer.
436 EFI_SUCEESS - The requested number of bytes were allocated.
438 EFI_OUT_OF_RESOURCES - The pool requested could not be allocated.
440 EFI_INVALID_PARAMETER - The buffer was invalid.
448 NewPool
= EfiAllocateZeroPool (NewSize
);
453 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
456 SafeFreePool (OldPool
);
464 IN EFI_TIME
*FirstTime
,
465 IN EFI_TIME
*SecondTime
470 Compare two EFI_TIME data.
474 FirstTime - A pointer to the first EFI_TIME data.
475 SecondTime - A pointer to the second EFI_TIME data.
478 TRUE The FirstTime is not later than the SecondTime.
479 FALSE The FirstTime is later than the SecondTime.
483 if (FirstTime
->Year
!= SecondTime
->Year
) {
484 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
485 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
486 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
487 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
488 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
489 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
490 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
491 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
492 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
493 } else if (FirstTime
->Second
!= SecondTime
->Second
) {
494 return (BOOLEAN
) (FirstTime
->Second
< SecondTime
->Second
);
497 return (BOOLEAN
) (FirstTime
->Nanosecond
<= SecondTime
->Nanosecond
);
501 EfiLibStrFromDatahub (
502 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
507 EFI_DATA_HUB_PROTOCOL
*Datahub
;
509 EFI_DATA_RECORD_HEADER
*Record
;
510 EFI_SUBCLASS_TYPE1_HEADER
*DataHdr
;
511 EFI_GUID MiscGuid
= EFI_MISC_SUBCLASS_GUID
;
512 EFI_MISC_ONBOARD_DEVICE_DATA
*ob
;
513 EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*Port
;
516 Status
= gBS
->LocateProtocol (
517 &gEfiDataHubProtocolGuid
,
521 if (EFI_ERROR (Status
)) {
525 Status
= gRT
->GetTime (&CurTime
, NULL
);
526 if (EFI_ERROR (Status
)) {
532 Status
= Datahub
->GetNextRecord (Datahub
, &Count
, NULL
, &Record
);
534 if (EFI_ERROR (Status
)) {
538 if (Record
->DataRecordClass
== EFI_DATA_RECORD_CLASS_DATA
&& CompareGuid (&Record
->DataRecordGuid
, &MiscGuid
)) {
540 // This record is what we need
542 DataHdr
= (EFI_SUBCLASS_TYPE1_HEADER
*) (Record
+ 1);
543 if (EFI_MISC_ONBOARD_DEVICE_RECORD_NUMBER
== DataHdr
->RecordType
) {
544 ob
= (EFI_MISC_ONBOARD_DEVICE_DATA
*) (DataHdr
+ 1);
545 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &ob
->OnBoardDevicePath
, DevPath
)) {
546 GetProducerString (&Record
->ProducerName
, ob
->OnBoardDeviceDescription
, &Desc
);
551 if (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_RECORD_NUMBER
== DataHdr
->RecordType
) {
552 Port
= (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*) (DataHdr
+ 1);
553 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &Port
->PortPath
, DevPath
)) {
554 GetProducerString (&Record
->ProducerName
, Port
->PortExternalConnectorDesignator
, &Desc
);
560 } while (TimeCompare (&Record
->LogTime
, &CurTime
) && Count
!= 0);