3 Copyright (c) 2006 - 2007, 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
23 // Include common header file for this module.
25 #include "CommonHeader.h"
27 #include "BootMaint.h"
30 EfiLibLocateProtocol (
31 IN EFI_GUID
*ProtocolGuid
,
38 Find the first instance of this Protocol
39 in the system and return it's interface
43 ProtocolGuid - Provides the protocol to search for
44 Interface - On return, a pointer to the first interface
45 that matches ProtocolGuid
49 EFI_SUCCESS - A protocol instance matching ProtocolGuid was found
51 EFI_NOT_FOUND - No protocol instances were found that match ProtocolGuid
57 Status
= gBS
->LocateProtocol (
67 IN EFI_HANDLE DeviceHandle
73 Function opens and returns a file handle to the root directory of a volume.
77 DeviceHandle - A handle for a device
81 A valid file handle or NULL is returned
86 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
92 // File the file system interface to the device
94 Status
= gBS
->HandleProtocol (
96 &gEfiSimpleFileSystemProtocolGuid
,
101 // Open the root directory of the volume
103 if (!EFI_ERROR (Status
)) {
104 Status
= Volume
->OpenVolume (
112 return EFI_ERROR (Status
) ? NULL
: File
;
117 IN OUT EFI_STATUS
*Status
,
118 IN OUT VOID
**Buffer
,
125 Helper function called as part of the code needed
126 to allocate the proper sized buffer for various
131 Status - Current status
133 Buffer - Current allocated buffer, or NULL
135 BufferSize - Current buffer size needed
139 TRUE - if the buffer was reallocated and the caller
140 should try the API again.
147 // If this is an initial request, buffer will be null with a new buffer size
149 if (!*Buffer
&& BufferSize
) {
150 *Status
= EFI_BUFFER_TOO_SMALL
;
153 // If the status code is "buffer too small", resize the buffer
156 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
158 SafeFreePool (*Buffer
);
160 *Buffer
= AllocateZeroPool (BufferSize
);
165 *Status
= EFI_OUT_OF_RESOURCES
;
169 // If there's an error, free the buffer
171 if (!TryAgain
&& EFI_ERROR (*Status
) && *Buffer
) {
172 SafeFreePool (*Buffer
);
182 IN EFI_GUID
*VendorGuid
187 Function returns the value of the specified variable.
190 Name - A Null-terminated Unicode string that is
191 the name of the vendor's variable.
193 VendorGuid - A unique identifier for the vendor.
203 return BdsLibGetVariableAndSize (Name
, VendorGuid
, &VarSize
);
207 EfiLibDeleteVariable (
214 Function deletes the variable specified by VarName and VarGuid.
217 VarName - A Null-terminated Unicode string that is
218 the name of the vendor's variable.
220 VendorGuid - A unique identifier for the vendor.
224 EFI_SUCCESS - The variable was found and removed
226 EFI_UNSUPPORTED - The variable store was inaccessible
228 EFI_OUT_OF_RESOURCES - The temporary buffer was not available
230 EFI_NOT_FOUND - The variable was not found
237 VarBuf
= EfiLibGetVariable (VarName
, VarGuid
);
238 Status
= EFI_NOT_FOUND
;
242 // Delete variable from Storage
244 Status
= gRT
->SetVariable (VarName
, VarGuid
, VAR_FLAG
, 0, NULL
);
245 ASSERT (!EFI_ERROR (Status
));
246 SafeFreePool (VarBuf
);
252 EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*
253 EfiLibFileSystemVolumeLabelInfo (
254 IN EFI_FILE_HANDLE FHand
260 Function gets the file system information from an open file descriptor,
261 and stores it in a buffer allocated from pool.
265 Fhand - A file handle
269 A pointer to a buffer with file information or NULL is returned
274 EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*Buffer
;
277 // Initialize for GrowBuffer loop
280 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
+ 200;
283 // Call the real function
285 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
286 Status
= FHand
->GetInfo (
288 &gEfiFileSystemVolumeLabelInfoIdGuid
,
305 Size
= StrSize (Src
);
306 Dest
= AllocateZeroPool (Size
);
307 ASSERT (Dest
!= NULL
);
309 CopyMem (Dest
, Src
, Size
);
317 IN EFI_FILE_HANDLE FHand
323 Function gets the file information from an open file descriptor, and stores it
324 in a buffer allocated from pool.
328 Fhand - A file handle
332 A pointer to a buffer with file information or NULL is returned
337 EFI_FILE_INFO
*Buffer
;
341 // Initialize for GrowBuffer loop
344 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
347 // Call the real function
349 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
350 Status
= FHand
->GetInfo (
362 EfiDevicePathInstanceCount (
363 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
368 Function is used to determine the number of device path instances
369 that exist in a device path.
372 DevicePath - A pointer to a device path data structure.
376 This function counts and returns the number of device path instances
385 while (GetNextDevicePathInstance (&DevicePath
, &Size
)) {
401 Adjusts the size of a previously allocated buffer.
404 OldPool - A pointer to the buffer whose size is being adjusted.
405 OldSize - The size of the current buffer.
406 NewSize - The size of the new buffer.
410 EFI_SUCEESS - The requested number of bytes were allocated.
412 EFI_OUT_OF_RESOURCES - The pool requested could not be allocated.
414 EFI_INVALID_PARAMETER - The buffer was invalid.
422 NewPool
= AllocateZeroPool (NewSize
);
427 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
430 SafeFreePool (OldPool
);
437 EfiLibGetStringFromToken (
438 IN EFI_GUID
*ProducerGuid
,
446 Acquire the string associated with the ProducerGuid and return it.
450 ProducerGuid - The Guid to search the HII database for
451 Token - The token value of the string to extract
452 String - The string that is extracted
456 EFI_SUCCESS - Buffer filled with the requested forms. BufferLength
458 EFI_BUFFER_TOO_SMALL - The buffer provided was not large enough to allow the form to be stored.
463 UINT16 HandleBufferLength
;
464 EFI_HII_HANDLE
*HiiHandleBuffer
;
465 UINTN StringBufferLength
;
466 UINTN NumberOfHiiHandles
;
470 EFI_HII_PROTOCOL
*Hii
;
473 // Initialize params.
475 HandleBufferLength
= 0;
476 HiiHandleBuffer
= NULL
;
478 Status
= gBS
->LocateProtocol (
479 &gEfiHiiProtocolGuid
,
483 if (EFI_ERROR (Status
)) {
488 // Get all the Hii handles
490 Status
= BdsLibGetHiiHandles (Hii
, &HandleBufferLength
, &HiiHandleBuffer
);
491 ASSERT_EFI_ERROR (Status
);
494 // Get the Hii Handle that matches the StructureNode->ProducerName
496 NumberOfHiiHandles
= HandleBufferLength
/ sizeof (EFI_HII_HANDLE
);
497 for (Index
= 0; Index
< NumberOfHiiHandles
; Index
++) {
499 Status
= ExtractDataFromHiiHandle (
500 HiiHandleBuffer
[Index
],
505 if (CompareGuid (ProducerGuid
, &HiiGuid
)) {
510 // Find the string based on the current language
512 StringBufferLength
= 0x100;
513 *String
= AllocateZeroPool (0x100);
514 ASSERT (*String
!= NULL
);
516 Status
= Hii
->GetString (
518 HiiHandleBuffer
[Index
],
526 FreePool (HiiHandleBuffer
);
533 IN EFI_TIME
*FirstTime
,
534 IN EFI_TIME
*SecondTime
539 Compare two EFI_TIME data.
543 FirstTime - A pointer to the first EFI_TIME data.
544 SecondTime - A pointer to the second EFI_TIME data.
547 TRUE The FirstTime is not later than the SecondTime.
548 FALSE The FirstTime is later than the SecondTime.
552 if (FirstTime
->Year
!= SecondTime
->Year
) {
553 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
554 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
555 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
556 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
557 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
558 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
559 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
560 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
561 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
562 } else if (FirstTime
->Second
!= SecondTime
->Second
) {
563 return (BOOLEAN
) (FirstTime
->Second
< SecondTime
->Second
);
566 return (BOOLEAN
) (FirstTime
->Nanosecond
<= SecondTime
->Nanosecond
);
570 EfiLibStrFromDatahub (
571 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
576 EFI_DATA_HUB_PROTOCOL
*Datahub
;
578 EFI_DATA_RECORD_HEADER
*Record
;
579 EFI_SUBCLASS_TYPE1_HEADER
*DataHdr
;
580 EFI_GUID MiscGuid
= EFI_MISC_SUBCLASS_GUID
;
581 EFI_MISC_ONBOARD_DEVICE_DATA
*ob
;
582 EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*Port
;
585 Status
= gBS
->LocateProtocol (
586 &gEfiDataHubProtocolGuid
,
590 if (EFI_ERROR (Status
)) {
594 Status
= gRT
->GetTime (&CurTime
, NULL
);
595 if (EFI_ERROR (Status
)) {
601 Status
= Datahub
->GetNextRecord (Datahub
, &Count
, NULL
, &Record
);
603 if (EFI_ERROR (Status
)) {
607 if (Record
->DataRecordClass
== EFI_DATA_RECORD_CLASS_DATA
&& CompareGuid (&Record
->DataRecordGuid
, &MiscGuid
)) {
609 // This record is what we need
611 DataHdr
= (EFI_SUBCLASS_TYPE1_HEADER
*) (Record
+ 1);
612 if (EFI_MISC_ONBOARD_DEVICE_RECORD_NUMBER
== DataHdr
->RecordType
) {
613 ob
= (EFI_MISC_ONBOARD_DEVICE_DATA
*) (DataHdr
+ 1);
614 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &ob
->OnBoardDevicePath
, DevPath
)) {
615 EfiLibGetStringFromToken (&Record
->ProducerName
, ob
->OnBoardDeviceDescription
, &Desc
);
620 if (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_RECORD_NUMBER
== DataHdr
->RecordType
) {
621 Port
= (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*) (DataHdr
+ 1);
622 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &Port
->PortPath
, DevPath
)) {
623 EfiLibGetStringFromToken (&Record
->ProducerName
, Port
->PortExternalConnectorDesignator
, &Desc
);
629 } while (TimeCompare (&Record
->LogTime
, &CurTime
) && Count
!= 0);