3 Copyright (c) 2006, 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"
25 EfiLibLocateProtocol (
26 IN EFI_GUID
*ProtocolGuid
,
33 Find the first instance of this Protocol
34 in the system and return it's interface
38 ProtocolGuid - Provides the protocol to search for
39 Interface - On return, a pointer to the first interface
40 that matches ProtocolGuid
44 EFI_SUCCESS - A protocol instance matching ProtocolGuid was found
46 EFI_NOT_FOUND - No protocol instances were found that match ProtocolGuid
52 Status
= gBS
->LocateProtocol (
62 IN EFI_HANDLE DeviceHandle
68 Function opens and returns a file handle to the root directory of a volume.
72 DeviceHandle - A handle for a device
76 A valid file handle or NULL is returned
81 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
87 // File the file system interface to the device
89 Status
= gBS
->HandleProtocol (
91 &gEfiSimpleFileSystemProtocolGuid
,
96 // Open the root directory of the volume
98 if (!EFI_ERROR (Status
)) {
99 Status
= Volume
->OpenVolume (
107 return EFI_ERROR (Status
) ? NULL
: File
;
112 IN OUT EFI_STATUS
*Status
,
113 IN OUT VOID
**Buffer
,
120 Helper function called as part of the code needed
121 to allocate the proper sized buffer for various
126 Status - Current status
128 Buffer - Current allocated buffer, or NULL
130 BufferSize - Current buffer size needed
134 TRUE - if the buffer was reallocated and the caller
135 should try the API again.
142 // If this is an initial request, buffer will be null with a new buffer size
144 if (!*Buffer
&& BufferSize
) {
145 *Status
= EFI_BUFFER_TOO_SMALL
;
148 // If the status code is "buffer too small", resize the buffer
151 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
153 SafeFreePool (*Buffer
);
155 *Buffer
= AllocateZeroPool (BufferSize
);
160 *Status
= EFI_OUT_OF_RESOURCES
;
164 // If there's an error, free the buffer
166 if (!TryAgain
&& EFI_ERROR (*Status
) && *Buffer
) {
167 SafeFreePool (*Buffer
);
177 IN EFI_GUID
*VendorGuid
182 Function returns the value of the specified variable.
185 Name - A Null-terminated Unicode string that is
186 the name of the vendor's variable.
188 VendorGuid - A unique identifier for the vendor.
198 return BdsLibGetVariableAndSize (Name
, VendorGuid
, &VarSize
);
202 EfiLibDeleteVariable (
209 Function deletes the variable specified by VarName and VarGuid.
212 VarName - A Null-terminated Unicode string that is
213 the name of the vendor's variable.
215 VendorGuid - A unique identifier for the vendor.
219 EFI_SUCCESS - The variable was found and removed
221 EFI_UNSUPPORTED - The variable store was inaccessible
223 EFI_OUT_OF_RESOURCES - The temporary buffer was not available
225 EFI_NOT_FOUND - The variable was not found
232 VarBuf
= EfiLibGetVariable (VarName
, VarGuid
);
233 Status
= EFI_NOT_FOUND
;
237 // Delete variable from Storage
239 Status
= gRT
->SetVariable (VarName
, VarGuid
, VAR_FLAG
, 0, NULL
);
240 ASSERT (!EFI_ERROR (Status
));
241 SafeFreePool (VarBuf
);
247 EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*
248 EfiLibFileSystemVolumeLabelInfo (
249 IN EFI_FILE_HANDLE FHand
255 Function gets the file system information from an open file descriptor,
256 and stores it in a buffer allocated from pool.
260 Fhand - A file handle
264 A pointer to a buffer with file information or NULL is returned
269 EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
*Buffer
;
272 // Initialize for GrowBuffer loop
275 BufferSize
= SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL_INFO
+ 200;
278 // Call the real function
280 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
281 Status
= FHand
->GetInfo (
283 &gEfiFileSystemVolumeLabelInfoIdGuid
,
300 Size
= StrSize (Src
);
301 Dest
= AllocateZeroPool (Size
);
302 ASSERT (Dest
!= NULL
);
304 CopyMem (Dest
, Src
, Size
);
312 IN EFI_FILE_HANDLE FHand
318 Function gets the file information from an open file descriptor, and stores it
319 in a buffer allocated from pool.
323 Fhand - A file handle
327 A pointer to a buffer with file information or NULL is returned
332 EFI_FILE_INFO
*Buffer
;
336 // Initialize for GrowBuffer loop
339 BufferSize
= SIZE_OF_EFI_FILE_INFO
+ 200;
342 // Call the real function
344 while (EfiGrowBuffer (&Status
, (VOID
**) &Buffer
, BufferSize
)) {
345 Status
= FHand
->GetInfo (
357 EfiDevicePathInstanceCount (
358 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
363 Function is used to determine the number of device path instances
364 that exist in a device path.
367 DevicePath - A pointer to a device path data structure.
371 This function counts and returns the number of device path instances
380 while (GetNextDevicePathInstance (&DevicePath
, &Size
)) {
396 Adjusts the size of a previously allocated buffer.
399 OldPool - A pointer to the buffer whose size is being adjusted.
400 OldSize - The size of the current buffer.
401 NewSize - The size of the new buffer.
405 EFI_SUCEESS - The requested number of bytes were allocated.
407 EFI_OUT_OF_RESOURCES - The pool requested could not be allocated.
409 EFI_INVALID_PARAMETER - The buffer was invalid.
417 NewPool
= AllocateZeroPool (NewSize
);
422 CopyMem (NewPool
, OldPool
, OldSize
< NewSize
? OldSize
: NewSize
);
425 SafeFreePool (OldPool
);
432 EfiLibGetStringFromToken (
433 IN EFI_GUID
*ProducerGuid
,
441 Acquire the string associated with the ProducerGuid and return it.
445 ProducerGuid - The Guid to search the HII database for
446 Token - The token value of the string to extract
447 String - The string that is extracted
451 EFI_SUCCESS - Buffer filled with the requested forms. BufferLength
453 EFI_BUFFER_TOO_SMALL - The buffer provided was not large enough to allow the form to be stored.
458 UINT16 HandleBufferLength
;
459 EFI_HII_HANDLE
*HiiHandleBuffer
;
460 UINTN StringBufferLength
;
461 UINTN NumberOfHiiHandles
;
465 EFI_HII_PROTOCOL
*Hii
;
468 // Initialize params.
470 HandleBufferLength
= 0;
471 HiiHandleBuffer
= NULL
;
473 Status
= gBS
->LocateProtocol (
474 &gEfiHiiProtocolGuid
,
478 if (EFI_ERROR (Status
)) {
483 // Get all the Hii handles
485 Status
= BdsLibGetHiiHandles (Hii
, &HandleBufferLength
, &HiiHandleBuffer
);
486 ASSERT_EFI_ERROR (Status
);
489 // Get the Hii Handle that matches the StructureNode->ProducerName
491 NumberOfHiiHandles
= HandleBufferLength
/ sizeof (EFI_HII_HANDLE
);
492 for (Index
= 0; Index
< NumberOfHiiHandles
; Index
++) {
494 Status
= ExtractDataFromHiiHandle (
495 HiiHandleBuffer
[Index
],
500 if (CompareGuid (ProducerGuid
, &HiiGuid
)) {
505 // Find the string based on the current language
507 StringBufferLength
= 0x100;
508 *String
= AllocateZeroPool (0x100);
509 ASSERT (*String
!= NULL
);
511 Status
= Hii
->GetString (
513 HiiHandleBuffer
[Index
],
521 gBS
->FreePool (HiiHandleBuffer
);
528 IN EFI_TIME
*FirstTime
,
529 IN EFI_TIME
*SecondTime
534 Compare two EFI_TIME data.
538 FirstTime - A pointer to the first EFI_TIME data.
539 SecondTime - A pointer to the second EFI_TIME data.
542 TRUE The FirstTime is not later than the SecondTime.
543 FALSE The FirstTime is later than the SecondTime.
547 if (FirstTime
->Year
!= SecondTime
->Year
) {
548 return (BOOLEAN
) (FirstTime
->Year
< SecondTime
->Year
);
549 } else if (FirstTime
->Month
!= SecondTime
->Month
) {
550 return (BOOLEAN
) (FirstTime
->Month
< SecondTime
->Month
);
551 } else if (FirstTime
->Day
!= SecondTime
->Day
) {
552 return (BOOLEAN
) (FirstTime
->Day
< SecondTime
->Day
);
553 } else if (FirstTime
->Hour
!= SecondTime
->Hour
) {
554 return (BOOLEAN
) (FirstTime
->Hour
< SecondTime
->Hour
);
555 } else if (FirstTime
->Minute
!= SecondTime
->Minute
) {
556 return (BOOLEAN
) (FirstTime
->Minute
< FirstTime
->Minute
);
557 } else if (FirstTime
->Second
!= SecondTime
->Second
) {
558 return (BOOLEAN
) (FirstTime
->Second
< SecondTime
->Second
);
561 return (BOOLEAN
) (FirstTime
->Nanosecond
<= SecondTime
->Nanosecond
);
565 EfiLibStrFromDatahub (
566 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
571 EFI_DATA_HUB_PROTOCOL
*Datahub
;
573 EFI_DATA_RECORD_HEADER
*Record
;
574 EFI_SUBCLASS_TYPE1_HEADER
*DataHdr
;
575 EFI_GUID MiscGuid
= EFI_MISC_SUBCLASS_GUID
;
576 EFI_MISC_ONBOARD_DEVICE_DATA
*ob
;
577 EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*Port
;
580 Status
= gBS
->LocateProtocol (
581 &gEfiDataHubProtocolGuid
,
585 if (EFI_ERROR (Status
)) {
589 Status
= gRT
->GetTime (&CurTime
, NULL
);
590 if (EFI_ERROR (Status
)) {
596 Status
= Datahub
->GetNextRecord (Datahub
, &Count
, NULL
, &Record
);
598 if (EFI_ERROR (Status
)) {
602 if (Record
->DataRecordClass
== EFI_DATA_RECORD_CLASS_DATA
&& CompareGuid (&Record
->DataRecordGuid
, &MiscGuid
)) {
604 // This record is what we need
606 DataHdr
= (EFI_SUBCLASS_TYPE1_HEADER
*) (Record
+ 1);
607 if (EFI_MISC_ONBOARD_DEVICE_RECORD_NUMBER
== DataHdr
->RecordType
) {
608 ob
= (EFI_MISC_ONBOARD_DEVICE_DATA
*) (DataHdr
+ 1);
609 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &ob
->OnBoardDevicePath
, DevPath
)) {
610 EfiLibGetStringFromToken (&Record
->ProducerName
, ob
->OnBoardDeviceDescription
, &Desc
);
615 if (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_RECORD_NUMBER
== DataHdr
->RecordType
) {
616 Port
= (EFI_MISC_PORT_INTERNAL_CONNECTOR_DESIGNATOR_DATA
*) (DataHdr
+ 1);
617 if (BdsLibMatchDevicePaths ((EFI_DEVICE_PATH_PROTOCOL
*) &Port
->PortPath
, DevPath
)) {
618 EfiLibGetStringFromToken (&Record
->ProducerName
, Port
->PortExternalConnectorDesignator
, &Desc
);
624 } while (TimeCompare (&Record
->LogTime
, &CurTime
) && Count
!= 0);