2 Sample platform variable cleanup library implementation.
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "PlatVarCleanup.h"
11 VAR_ERROR_FLAG mLastVarErrorFlag
= VAR_ERROR_FLAG_NO_ERROR
;
12 EDKII_VAR_CHECK_PROTOCOL
*mVarCheck
= NULL
;
15 /// The flag to indicate whether the platform has left the DXE phase of execution.
17 BOOLEAN mEndOfDxe
= FALSE
;
19 EFI_EVENT mPlatVarCleanupLibEndOfDxeEvent
= NULL
;
21 LIST_ENTRY mUserVariableList
= INITIALIZE_LIST_HEAD_VARIABLE (mUserVariableList
);
22 UINT16 mUserVariableCount
= 0;
23 UINT16 mMarkedUserVariableCount
= 0;
25 EFI_GUID mVariableCleanupHiiGuid
= VARIABLE_CLEANUP_HII_GUID
;
26 CHAR16 mVarStoreName
[] = L
"VariableCleanup";
28 HII_VENDOR_DEVICE_PATH mVarCleanupHiiVendorDevicePath
= {
34 (UINT8
)(sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
)((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
38 VARIABLE_CLEANUP_HII_GUID
42 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
44 (UINT8
)(sizeof (EFI_DEVICE_PATH_PROTOCOL
)),
45 (UINT8
)((sizeof (EFI_DEVICE_PATH_PROTOCOL
)) >> 8)
51 Internal get variable error flag.
53 @return Variable error flag.
57 InternalGetVarErrorFlag (
63 VAR_ERROR_FLAG ErrorFlag
;
65 Size
= sizeof (ErrorFlag
);
66 Status
= gRT
->GetVariable (
68 &gEdkiiVarErrorFlagGuid
,
73 if (EFI_ERROR (Status
)) {
74 DEBUG ((DEBUG_INFO
, "%s - not found\n", VAR_ERROR_FLAG_NAME
));
75 return VAR_ERROR_FLAG_NO_ERROR
;
84 @param[in] Name Pointer to variable name.
85 @param[in] Guid Pointer to vendor guid.
87 @retval TRUE User variable.
88 @retval FALSE System variable.
98 VAR_CHECK_VARIABLE_PROPERTY Property
;
100 if (mVarCheck
== NULL
) {
101 gBS
->LocateProtocol (
102 &gEdkiiVarCheckProtocolGuid
,
108 ASSERT (mVarCheck
!= NULL
);
110 ZeroMem (&Property
, sizeof (Property
));
111 Status
= mVarCheck
->VariablePropertyGet (
116 if (EFI_ERROR (Status
)) {
118 // No property, it is user variable.
120 DEBUG ((DEBUG_INFO
, "PlatformVarCleanup - User variable: %g:%s\n", Guid
, Name
));
124 // DEBUG ((DEBUG_INFO, "PlatformVarCleanup - Variable Property: %g:%s\n", Guid, Name));
125 // DEBUG ((DEBUG_INFO, " Revision - 0x%04x\n", Property.Revision));
126 // DEBUG ((DEBUG_INFO, " Property - 0x%04x\n", Property.Property));
127 // DEBUG ((DEBUG_INFO, " Attribute - 0x%08x\n", Property.Attributes));
128 // DEBUG ((DEBUG_INFO, " MinSize - 0x%x\n", Property.MinSize));
129 // DEBUG ((DEBUG_INFO, " MaxSize - 0x%x\n", Property.MaxSize));
135 Find user variable node by variable GUID.
137 @param[in] Guid Pointer to vendor guid.
139 @return Pointer to user variable node.
143 FindUserVariableNodeByGuid (
147 USER_VARIABLE_NODE
*UserVariableNode
;
150 for (Link
= mUserVariableList
.ForwardLink
151 ; Link
!= &mUserVariableList
152 ; Link
= Link
->ForwardLink
)
154 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
156 if (CompareGuid (Guid
, &UserVariableNode
->Guid
)) {
160 return UserVariableNode
;
165 // Create new one if not found.
167 UserVariableNode
= AllocateZeroPool (sizeof (*UserVariableNode
));
168 ASSERT (UserVariableNode
!= NULL
);
169 UserVariableNode
->Signature
= USER_VARIABLE_NODE_SIGNATURE
;
170 CopyGuid (&UserVariableNode
->Guid
, Guid
);
172 // (36 chars of "########-####-####-####-############" + 1 space + 1 terminator) * sizeof (CHAR16).
174 UserVariableNode
->PromptString
= AllocatePool ((36 + 2) * sizeof (CHAR16
));
175 ASSERT (UserVariableNode
->PromptString
!= NULL
);
176 UnicodeSPrint (UserVariableNode
->PromptString
, (36 + 2) * sizeof (CHAR16
), L
" %g", &UserVariableNode
->Guid
);
177 InitializeListHead (&UserVariableNode
->NameLink
);
178 InsertTailList (&mUserVariableList
, &UserVariableNode
->Link
);
179 return UserVariableNode
;
183 Create user variable node.
187 CreateUserVariableNode (
192 EFI_STATUS GetVariableStatus
;
194 UINTN MaxVarNameSize
;
201 USER_VARIABLE_NODE
*UserVariableNode
;
202 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
207 // Initialize 128 * sizeof (CHAR16) variable name size.
209 MaxVarNameSize
= 128 * sizeof (CHAR16
);
210 VarName
= AllocateZeroPool (MaxVarNameSize
);
211 ASSERT (VarName
!= NULL
);
214 // Initialize 0x1000 variable data size.
216 MaxDataSize
= 0x1000;
217 Data
= AllocateZeroPool (MaxDataSize
);
218 ASSERT (Data
!= NULL
);
222 VarNameSize
= MaxVarNameSize
;
223 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
224 if (Status
== EFI_BUFFER_TOO_SMALL
) {
225 VarName
= ReallocatePool (MaxVarNameSize
, VarNameSize
, VarName
);
226 ASSERT (VarName
!= NULL
);
227 MaxVarNameSize
= VarNameSize
;
228 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
231 if (!EFI_ERROR (Status
)) {
232 if (IsUserVariable (VarName
, &Guid
)) {
233 DataSize
= MaxDataSize
;
234 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
235 if (GetVariableStatus
== EFI_BUFFER_TOO_SMALL
) {
236 Data
= ReallocatePool (MaxDataSize
, DataSize
, Data
);
237 ASSERT (Data
!= NULL
);
238 MaxDataSize
= DataSize
;
239 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
242 ASSERT_EFI_ERROR (GetVariableStatus
);
244 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
245 UserVariableNode
= FindUserVariableNodeByGuid (&Guid
);
246 ASSERT (UserVariableNode
!= NULL
);
249 // Different variables that have same variable GUID share same user variable node.
251 UserVariableNameNode
= AllocateZeroPool (sizeof (*UserVariableNameNode
));
252 ASSERT (UserVariableNameNode
!= NULL
);
253 UserVariableNameNode
->Signature
= USER_VARIABLE_NAME_NODE_SIGNATURE
;
254 UserVariableNameNode
->Name
= AllocateCopyPool (VarNameSize
, VarName
);
255 UserVariableNameNode
->Attributes
= Attributes
;
256 UserVariableNameNode
->DataSize
= DataSize
;
257 UserVariableNameNode
->Index
= Index
;
258 UserVariableNameNode
->QuestionId
= (EFI_QUESTION_ID
)(USER_VARIABLE_QUESTION_ID
+ Index
);
260 // 2 space * sizeof (CHAR16) + StrSize.
262 StringSize
= 2 * sizeof (CHAR16
) + StrSize (UserVariableNameNode
->Name
);
263 UserVariableNameNode
->PromptString
= AllocatePool (StringSize
);
264 ASSERT (UserVariableNameNode
->PromptString
!= NULL
);
265 UnicodeSPrint (UserVariableNameNode
->PromptString
, StringSize
, L
" %s", UserVariableNameNode
->Name
);
267 // (33 chars of "Attribtues = 0x and DataSize = 0x" + 1 terminator + (sizeof (UINT32) + sizeof (UINTN)) * 2) * sizeof (CHAR16).
269 StringSize
= (33 + 1 + (sizeof (UINT32
) + sizeof (UINTN
)) * 2) * sizeof (CHAR16
);
270 UserVariableNameNode
->HelpString
= AllocatePool (StringSize
);
271 ASSERT (UserVariableNameNode
->HelpString
!= NULL
);
272 UnicodeSPrint (UserVariableNameNode
->HelpString
, StringSize
, L
"Attribtues = 0x%08x and DataSize = 0x%x", UserVariableNameNode
->Attributes
, UserVariableNameNode
->DataSize
);
273 UserVariableNameNode
->Deleted
= FALSE
;
274 InsertTailList (&UserVariableNode
->NameLink
, &UserVariableNameNode
->Link
);
279 } while (Status
!= EFI_NOT_FOUND
);
281 mUserVariableCount
= Index
;
282 ASSERT (mUserVariableCount
<= MAX_USER_VARIABLE_COUNT
);
283 DEBUG ((DEBUG_INFO
, "PlatformVarCleanup - User variable count: 0x%04x\n", mUserVariableCount
));
290 Destroy user variable nodes.
294 DestroyUserVariableNode (
298 USER_VARIABLE_NODE
*UserVariableNode
;
300 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
301 LIST_ENTRY
*NameLink
;
303 while (mUserVariableList
.ForwardLink
!= &mUserVariableList
) {
304 Link
= mUserVariableList
.ForwardLink
;
305 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
307 RemoveEntryList (&UserVariableNode
->Link
);
309 while (UserVariableNode
->NameLink
.ForwardLink
!= &UserVariableNode
->NameLink
) {
310 NameLink
= UserVariableNode
->NameLink
.ForwardLink
;
311 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
313 RemoveEntryList (&UserVariableNameNode
->Link
);
315 FreePool (UserVariableNameNode
->Name
);
316 FreePool (UserVariableNameNode
->PromptString
);
317 FreePool (UserVariableNameNode
->HelpString
);
318 FreePool (UserVariableNameNode
);
321 FreePool (UserVariableNode
->PromptString
);
322 FreePool (UserVariableNode
);
327 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
328 descriptor with the input data. NO authentication is required in this function.
330 @param[in, out] DataSize On input, the size of Data buffer in bytes.
331 On output, the size of data returned in Data
333 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
334 pointer to NULL to wrap an empty payload.
335 On output, Pointer to the new payload date buffer allocated from pool,
336 it's caller's responsibility to free the memory after using it.
338 @retval EFI_SUCCESS Create time based payload successfully.
339 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
340 @retval EFI_INVALID_PARAMETER The parameter is invalid.
341 @retval Others Unexpected error happens.
345 CreateTimeBasedPayload (
346 IN OUT UINTN
*DataSize
,
354 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
355 UINTN DescriptorSize
;
358 if ((Data
== NULL
) || (DataSize
== NULL
)) {
359 return EFI_INVALID_PARAMETER
;
363 // At user physical presence, the variable does not need to be signed but the
364 // parameters to the SetVariable() call still need to be prepared as authenticated
365 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
369 PayloadSize
= *DataSize
;
371 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
372 NewData
= (UINT8
*)AllocateZeroPool (DescriptorSize
+ PayloadSize
);
373 if (NewData
== NULL
) {
374 return EFI_OUT_OF_RESOURCES
;
377 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
378 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
381 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*)(NewData
);
383 ZeroMem (&Time
, sizeof (EFI_TIME
));
384 Status
= gRT
->GetTime (&Time
, NULL
);
385 if (EFI_ERROR (Status
)) {
395 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
397 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
398 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
399 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
400 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
402 if (Payload
!= NULL
) {
406 *DataSize
= DescriptorSize
+ PayloadSize
;
412 Create a counter based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION
413 descriptor with the input data. NO authentication is required in this function.
415 @param[in, out] DataSize On input, the size of Data buffer in bytes.
416 On output, the size of data returned in Data
418 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
419 pointer to NULL to wrap an empty payload.
420 On output, Pointer to the new payload date buffer allocated from pool,
421 it's caller's responsibility to free the memory after using it.
423 @retval EFI_SUCCESS Create counter based payload successfully.
424 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
425 @retval EFI_INVALID_PARAMETER The parameter is invalid.
426 @retval Others Unexpected error happens.
430 CreateCounterBasedPayload (
431 IN OUT UINTN
*DataSize
,
439 EFI_VARIABLE_AUTHENTICATION
*DescriptorData
;
440 UINTN DescriptorSize
;
441 UINT64 MonotonicCount
;
443 if ((Data
== NULL
) || (DataSize
== NULL
)) {
444 return EFI_INVALID_PARAMETER
;
448 // At user physical presence, the variable does not need to be signed but the
449 // parameters to the SetVariable() call still need to be prepared as authenticated
450 // variable. So we create EFI_VARIABLE_AUTHENTICATED descriptor without certificate
454 PayloadSize
= *DataSize
;
456 DescriptorSize
= (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION
, AuthInfo
)) + \
457 (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) + \
458 sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
459 NewData
= (UINT8
*)AllocateZeroPool (DescriptorSize
+ PayloadSize
);
460 if (NewData
== NULL
) {
461 return EFI_OUT_OF_RESOURCES
;
464 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
465 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
468 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION
*)(NewData
);
470 Status
= gBS
->GetNextMonotonicCount (&MonotonicCount
);
471 if (EFI_ERROR (Status
)) {
476 DescriptorData
->MonotonicCount
= MonotonicCount
;
478 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
) + sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
479 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
480 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
481 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
);
483 if (Payload
!= NULL
) {
487 *DataSize
= DescriptorSize
+ PayloadSize
;
493 Delete user variable.
495 @param[in] DeleteAll Delete all user variables.
496 @param[in] VariableCleanupData Pointer to variable cleanup data.
501 IN BOOLEAN DeleteAll
,
502 IN VARIABLE_CLEANUP_DATA
*VariableCleanupData OPTIONAL
506 USER_VARIABLE_NODE
*UserVariableNode
;
508 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
509 LIST_ENTRY
*NameLink
;
513 for (Link
= mUserVariableList
.ForwardLink
514 ; Link
!= &mUserVariableList
515 ; Link
= Link
->ForwardLink
)
517 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
519 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
520 ; NameLink
!= &UserVariableNode
->NameLink
521 ; NameLink
= NameLink
->ForwardLink
)
523 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
525 if (!UserVariableNameNode
->Deleted
&& (DeleteAll
|| ((VariableCleanupData
!= NULL
) && (VariableCleanupData
->UserVariable
[UserVariableNameNode
->Index
] == TRUE
)))) {
526 DEBUG ((DEBUG_INFO
, "PlatformVarCleanup - Delete variable: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
527 if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
530 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
531 if (!EFI_ERROR (Status
)) {
532 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
535 } else if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
538 Status
= CreateCounterBasedPayload (&DataSize
, &Data
);
539 if (!EFI_ERROR (Status
)) {
540 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
544 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, 0, 0, NULL
);
547 if (!EFI_ERROR (Status
)) {
548 UserVariableNameNode
->Deleted
= TRUE
;
550 DEBUG ((DEBUG_INFO
, "PlatformVarCleanup - Delete variable fail: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
558 This function allows a caller to extract the current configuration for one
559 or more named elements from the target driver.
561 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
562 @param[in] Request A null-terminated Unicode string in <ConfigRequest> format.
563 @param[out] Progress On return, points to a character in the Request string.
564 Points to the string's null terminator if request was successful.
565 Points to the most recent '&' before the first failing name/value
566 pair (or the beginning of the string if the failure is in the
567 first name/value pair) if the request was not successful.
568 @param[out] Results A null-terminated Unicode string in <ConfigAltResp> format which
569 has all values filled in for the names in the Request string.
570 String to be allocated by the called function.
572 @retval EFI_SUCCESS The Results is filled with the requested values.
573 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
574 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
575 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
580 VariableCleanupHiiExtractConfig (
581 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
582 IN CONST EFI_STRING Request
,
583 OUT EFI_STRING
*Progress
,
584 OUT EFI_STRING
*Results
588 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
590 EFI_STRING ConfigRequestHdr
;
591 EFI_STRING ConfigRequest
;
592 BOOLEAN AllocatedRequest
;
595 if ((Progress
== NULL
) || (Results
== NULL
)) {
596 return EFI_INVALID_PARAMETER
;
600 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
601 return EFI_NOT_FOUND
;
604 ConfigRequestHdr
= NULL
;
605 ConfigRequest
= NULL
;
606 AllocatedRequest
= FALSE
;
609 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
611 // Convert buffer data to <ConfigResp> by helper function BlockToConfig().
613 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
614 ConfigRequest
= Request
;
615 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
617 // Request has no request element, construct full request string.
618 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
619 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator.
621 ConfigRequestHdr
= HiiConstructConfigHdr (
622 &mVariableCleanupHiiGuid
,
624 Private
->DriverHandle
626 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
627 ConfigRequest
= AllocateZeroPool (Size
);
628 ASSERT (ConfigRequest
!= NULL
);
629 AllocatedRequest
= TRUE
;
630 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
631 FreePool (ConfigRequestHdr
);
634 Status
= Private
->ConfigRouting
->BlockToConfig (
635 Private
->ConfigRouting
,
637 (UINT8
*)&Private
->VariableCleanupData
,
642 ASSERT_EFI_ERROR (Status
);
645 // Free the allocated config request string.
647 if (AllocatedRequest
) {
648 FreePool (ConfigRequest
);
649 ConfigRequest
= NULL
;
653 // Set Progress string to the original request string or the string's null terminator.
655 if (Request
== NULL
) {
657 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
658 *Progress
= Request
+ StrLen (Request
);
665 Update user variable form.
667 @param[in] Private Points to the VARIABLE_CLEANUP_HII_PRIVATE_DATA.
671 UpdateUserVariableForm (
672 IN VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
675 EFI_STRING_ID PromptStringToken
;
676 EFI_STRING_ID HelpStringToken
;
677 VOID
*StartOpCodeHandle
;
678 VOID
*EndOpCodeHandle
;
679 EFI_IFR_GUID_LABEL
*StartLabel
;
680 EFI_IFR_GUID_LABEL
*EndLabel
;
681 USER_VARIABLE_NODE
*UserVariableNode
;
683 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
684 LIST_ENTRY
*NameLink
;
688 // Init OpCode Handle.
690 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
691 ASSERT (StartOpCodeHandle
!= NULL
);
693 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
694 ASSERT (EndOpCodeHandle
!= NULL
);
697 // Create Hii Extend Label OpCode as the start opcode.
699 StartLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
700 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
701 StartLabel
->Number
= LABEL_START
;
704 // Create Hii Extend Label OpCode as the end opcode.
706 EndLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (EndOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
707 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
708 EndLabel
->Number
= LABEL_END
;
712 &mVariableCleanupHiiGuid
,
713 FORM_ID_VARIABLE_CLEANUP
,
714 StartOpCodeHandle
, // LABEL_START
715 EndOpCodeHandle
// LABEL_END
718 for (Link
= mUserVariableList
.ForwardLink
719 ; Link
!= &mUserVariableList
720 ; Link
= Link
->ForwardLink
)
722 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
725 // Create checkbox opcode for variables in the same variable GUID space.
728 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
729 ; NameLink
!= &UserVariableNode
->NameLink
730 ; NameLink
= NameLink
->ForwardLink
)
732 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
734 if (!UserVariableNameNode
->Deleted
) {
737 // Create subtitle opcode for variable GUID.
739 PromptStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNode
->PromptString
, NULL
);
740 HiiCreateSubTitleOpCode (StartOpCodeHandle
, PromptStringToken
, 0, 0, 0);
745 // Only create opcode for the non-deleted variables.
747 PromptStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNameNode
->PromptString
, NULL
);
748 HelpStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNameNode
->HelpString
, NULL
);
749 HiiCreateCheckBoxOpCode (
751 UserVariableNameNode
->QuestionId
,
752 VARIABLE_CLEANUP_VARSTORE_ID
,
753 (UINT16
)(USER_VARIABLE_VAR_OFFSET
+ UserVariableNameNode
->Index
),
756 EFI_IFR_FLAG_CALLBACK
,
757 Private
->VariableCleanupData
.UserVariable
[UserVariableNameNode
->Index
],
764 HiiCreateSubTitleOpCode (
766 STRING_TOKEN (STR_NULL_STRING
),
773 // Create the "Apply changes" and "Discard changes" tags.
775 HiiCreateActionOpCode (
777 SAVE_AND_EXIT_QUESTION_ID
,
778 STRING_TOKEN (STR_SAVE_AND_EXIT
),
779 STRING_TOKEN (STR_NULL_STRING
),
780 EFI_IFR_FLAG_CALLBACK
,
783 HiiCreateActionOpCode (
785 NO_SAVE_AND_EXIT_QUESTION_ID
,
786 STRING_TOKEN (STR_NO_SAVE_AND_EXIT
),
787 STRING_TOKEN (STR_NULL_STRING
),
788 EFI_IFR_FLAG_CALLBACK
,
794 &mVariableCleanupHiiGuid
,
795 FORM_ID_VARIABLE_CLEANUP
,
796 StartOpCodeHandle
, // LABEL_START
797 EndOpCodeHandle
// LABEL_END
800 HiiFreeOpCodeHandle (StartOpCodeHandle
);
801 HiiFreeOpCodeHandle (EndOpCodeHandle
);
805 This function applies changes in a driver's configuration.
806 Input is a Configuration, which has the routing data for this
807 driver followed by name / value configuration pairs. The driver
808 must apply those pairs to its configurable storage. If the
809 driver's configuration is stored in a linear block of data
810 and the driver's name / value pairs are in <BlockConfig>
811 format, it may use the ConfigToBlock helper function (above) to
812 simplify the job. Currently not implemented.
814 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
815 @param[in] Configuration A null-terminated Unicode string in
816 <ConfigString> format.
817 @param[out] Progress A pointer to a string filled in with the
818 offset of the most recent '&' before the
819 first failing name / value pair (or the
820 beginn ing of the string if the failure
821 is in the first name / value pair) or
822 the terminating NULL if all was
825 @retval EFI_SUCCESS The results have been distributed or are
826 awaiting distribution.
827 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
828 parts of the results that must be
829 stored awaiting possible future
831 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
832 Results parameter would result
833 in this type of error.
834 @retval EFI_NOT_FOUND Target for the specified routing data
840 VariableCleanupHiiRouteConfig (
841 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
842 IN CONST EFI_STRING Configuration
,
843 OUT EFI_STRING
*Progress
847 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
850 if (Progress
== NULL
) {
851 return EFI_INVALID_PARAMETER
;
854 *Progress
= Configuration
;
856 if (Configuration
== NULL
) {
857 return EFI_INVALID_PARAMETER
;
861 // Check routing data in <ConfigHdr>.
862 // Note: there is no name for Name/Value storage, only GUID will be checked.
864 if (!HiiIsConfigHdrMatch (Configuration
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
865 return EFI_NOT_FOUND
;
868 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
870 // Get Buffer Storage data.
872 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
874 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock().
876 Status
= Private
->ConfigRouting
->ConfigToBlock (
877 Private
->ConfigRouting
,
879 (UINT8
*)&Private
->VariableCleanupData
,
883 ASSERT_EFI_ERROR (Status
);
885 DeleteUserVariable (FALSE
, &Private
->VariableCleanupData
);
887 // For "F10" hotkey to refresh the form.
889 // UpdateUserVariableForm (Private);
895 This function is called to provide results data to the driver.
896 This data consists of a unique key that is used to identify
897 which data is either being passed back or being asked for.
899 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
900 @param[in] Action Specifies the type of action taken by the browser.
901 @param[in] QuestionId A unique value which is sent to the original
902 exporting driver so that it can identify the type
903 of data to expect. The format of the data tends to
904 vary based on the opcode that generated the callback.
905 @param[in] Type The type of value for the question.
906 @param[in] Value A pointer to the data being sent to the original
908 @param[out] ActionRequest On return, points to the action requested by the
911 @retval EFI_SUCCESS The callback successfully handled the action.
912 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
913 variable and its data.
914 @retval EFI_DEVICE_ERROR The variable could not be saved.
915 @retval EFI_UNSUPPORTED The specified Action is not supported by the
920 VariableCleanupHiiCallback (
921 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
922 IN EFI_BROWSER_ACTION Action
,
923 IN EFI_QUESTION_ID QuestionId
,
925 IN EFI_IFR_TYPE_VALUE
*Value
,
926 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
929 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
930 VARIABLE_CLEANUP_DATA
*VariableCleanupData
;
932 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
934 if ((Action
!= EFI_BROWSER_ACTION_CHANGING
) && (Action
!= EFI_BROWSER_ACTION_CHANGED
)) {
936 // All other action return unsupported.
938 return EFI_UNSUPPORTED
;
942 // Retrieve uncommitted data from Form Browser.
944 VariableCleanupData
= &Private
->VariableCleanupData
;
945 HiiGetBrowserData (&mVariableCleanupHiiGuid
, mVarStoreName
, sizeof (VARIABLE_CLEANUP_DATA
), (UINT8
*)VariableCleanupData
);
946 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
948 return EFI_INVALID_PARAMETER
;
950 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
951 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
952 return EFI_INVALID_PARAMETER
;
955 if ((QuestionId
>= USER_VARIABLE_QUESTION_ID
) && (QuestionId
< USER_VARIABLE_QUESTION_ID
+ MAX_USER_VARIABLE_COUNT
)) {
958 // Means one user variable checkbox is marked to delete but not press F10 or "Commit Changes and Exit" menu.
960 mMarkedUserVariableCount
++;
961 ASSERT (mMarkedUserVariableCount
<= mUserVariableCount
);
962 if (mMarkedUserVariableCount
== mUserVariableCount
) {
964 // All user variables have been marked, then also mark the SelectAll checkbox.
966 VariableCleanupData
->SelectAll
= TRUE
;
970 // Means one user variable checkbox is unmarked.
972 mMarkedUserVariableCount
--;
974 // Also unmark the SelectAll checkbox.
976 VariableCleanupData
->SelectAll
= FALSE
;
979 switch (QuestionId
) {
980 case SELECT_ALL_QUESTION_ID
:
983 // Means the SelectAll checkbox is marked to delete all user variables but not press F10 or "Commit Changes and Exit" menu.
985 SetMem (VariableCleanupData
->UserVariable
, sizeof (VariableCleanupData
->UserVariable
), TRUE
);
986 mMarkedUserVariableCount
= mUserVariableCount
;
989 // Means the SelectAll checkbox is unmarked.
991 SetMem (VariableCleanupData
->UserVariable
, sizeof (VariableCleanupData
->UserVariable
), FALSE
);
992 mMarkedUserVariableCount
= 0;
996 case SAVE_AND_EXIT_QUESTION_ID
:
997 DeleteUserVariable (FALSE
, VariableCleanupData
);
998 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT
;
1001 case NO_SAVE_AND_EXIT_QUESTION_ID
:
1003 // Restore local maintain data.
1005 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
1015 // Pass changed uncommitted data back to Form Browser.
1017 HiiSetBrowserData (&mVariableCleanupHiiGuid
, mVarStoreName
, sizeof (VARIABLE_CLEANUP_DATA
), (UINT8
*)VariableCleanupData
, NULL
);
1022 Platform variable cleanup.
1024 @param[in] Flag Variable error flag.
1025 @param[in] Type Variable cleanup type.
1026 If it is VarCleanupManually, the interface must be called after console connected.
1028 @retval EFI_SUCCESS No error or error processed.
1029 @retval EFI_UNSUPPORTED The specified Flag or Type is not supported.
1030 For example, system error may be not supported to process and Platform should have mechanism to reset system to manufacture mode.
1031 Another, if system and user variables are wanted to be distinguished to process, the interface must be called after EndOfDxe.
1032 @retval EFI_OUT_OF_RESOURCES Not enough resource to process the error.
1033 @retval EFI_INVALID_PARAMETER The specified Flag or Type is an invalid value.
1034 @retval Others Other failure occurs.
1039 PlatformVarCleanup (
1040 IN VAR_ERROR_FLAG Flag
,
1041 IN VAR_CLEANUP_TYPE Type
1045 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
1046 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
1050 // This implementation must be called after EndOfDxe.
1052 return EFI_UNSUPPORTED
;
1055 if ((Type
>= VarCleanupMax
) || ((Flag
& ((VAR_ERROR_FLAG
)(VAR_ERROR_FLAG_SYSTEM_ERROR
& VAR_ERROR_FLAG_USER_ERROR
))) == 0)) {
1056 return EFI_INVALID_PARAMETER
;
1059 if (Flag
== VAR_ERROR_FLAG_NO_ERROR
) {
1061 // Just return success if no error.
1066 if ((Flag
& (~((VAR_ERROR_FLAG
)VAR_ERROR_FLAG_SYSTEM_ERROR
))) == 0) {
1068 // This sample does not support system variables cleanup.
1070 DEBUG ((DEBUG_ERROR
, "NOTICE - VAR_ERROR_FLAG_SYSTEM_ERROR\n"));
1071 DEBUG ((DEBUG_ERROR
, "Platform should have mechanism to reset system to manufacture mode\n"));
1072 return EFI_UNSUPPORTED
;
1076 // Continue to process VAR_ERROR_FLAG_USER_ERROR.
1080 // Create user variable nodes for the following processing.
1082 CreateUserVariableNode ();
1086 DeleteUserVariable (TRUE
, NULL
);
1088 // Destroyed the created user variable nodes
1090 DestroyUserVariableNode ();
1094 case VarCleanupManually
:
1096 // Locate FormBrowser2 protocol.
1098 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**)&FormBrowser2
);
1099 if (EFI_ERROR (Status
)) {
1103 Private
= AllocateZeroPool (sizeof (VARIABLE_CLEANUP_HII_PRIVATE_DATA
));
1104 if (Private
== NULL
) {
1105 return EFI_OUT_OF_RESOURCES
;
1108 Private
->Signature
= VARIABLE_CLEANUP_HII_PRIVATE_SIGNATURE
;
1109 Private
->ConfigAccess
.ExtractConfig
= VariableCleanupHiiExtractConfig
;
1110 Private
->ConfigAccess
.RouteConfig
= VariableCleanupHiiRouteConfig
;
1111 Private
->ConfigAccess
.Callback
= VariableCleanupHiiCallback
;
1113 Status
= gBS
->LocateProtocol (
1114 &gEfiHiiConfigRoutingProtocolGuid
,
1116 (VOID
**)&Private
->ConfigRouting
1118 if (EFI_ERROR (Status
)) {
1123 // Install Device Path Protocol and Config Access protocol to driver handle.
1125 Status
= gBS
->InstallMultipleProtocolInterfaces (
1126 &Private
->DriverHandle
,
1127 &gEfiDevicePathProtocolGuid
,
1128 &mVarCleanupHiiVendorDevicePath
,
1129 &gEfiHiiConfigAccessProtocolGuid
,
1130 &Private
->ConfigAccess
,
1133 if (EFI_ERROR (Status
)) {
1138 // Publish our HII data.
1140 Private
->HiiHandle
= HiiAddPackages (
1141 &mVariableCleanupHiiGuid
,
1142 Private
->DriverHandle
,
1143 PlatformVarCleanupLibStrings
,
1147 if (Private
->HiiHandle
== NULL
) {
1148 Status
= EFI_OUT_OF_RESOURCES
;
1152 UpdateUserVariableForm (Private
);
1154 Status
= FormBrowser2
->SendForm (
1156 &Private
->HiiHandle
,
1166 return EFI_UNSUPPORTED
;
1171 if (Private
->DriverHandle
!= NULL
) {
1172 gBS
->UninstallMultipleProtocolInterfaces (
1173 Private
->DriverHandle
,
1174 &gEfiDevicePathProtocolGuid
,
1175 &mVarCleanupHiiVendorDevicePath
,
1176 &gEfiHiiConfigAccessProtocolGuid
,
1177 &Private
->ConfigAccess
,
1182 if (Private
->HiiHandle
!= NULL
) {
1183 HiiRemovePackages (Private
->HiiHandle
);
1189 // Destroyed the created user variable nodes
1191 DestroyUserVariableNode ();
1196 Get last boot variable error flag.
1198 @return Last boot variable error flag.
1203 GetLastBootVarErrorFlag (
1207 return mLastVarErrorFlag
;
1211 Notification function of END_OF_DXE.
1213 This is a notification function registered on END_OF_DXE event.
1215 @param[in] Event Event whose notification function is being invoked.
1216 @param[in] Context Pointer to the notification function's context.
1221 PlatformVarCleanupEndOfDxeEvent (
1230 The constructor function caches the pointer to VarCheck protocol and last boot variable error flag.
1232 The constructor function locates VarCheck protocol from protocol database.
1233 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
1235 @param ImageHandle The firmware allocated handle for the EFI image.
1236 @param SystemTable A pointer to the EFI System Table.
1238 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1243 PlatformVarCleanupLibConstructor (
1244 IN EFI_HANDLE ImageHandle
,
1245 IN EFI_SYSTEM_TABLE
*SystemTable
1250 mLastVarErrorFlag
= InternalGetVarErrorFlag ();
1251 DEBUG ((DEBUG_INFO
, "mLastVarErrorFlag - 0x%02x\n", mLastVarErrorFlag
));
1254 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
1256 Status
= gBS
->CreateEventEx (
1259 PlatformVarCleanupEndOfDxeEvent
,
1261 &gEfiEndOfDxeEventGroupGuid
,
1262 &mPlatVarCleanupLibEndOfDxeEvent
1264 ASSERT_EFI_ERROR (Status
);
1270 The destructor function closes the End of DXE event.
1272 @param ImageHandle The firmware allocated handle for the EFI image.
1273 @param SystemTable A pointer to the EFI System Table.
1275 @retval EFI_SUCCESS The destructor completed successfully.
1280 PlatformVarCleanupLibDestructor (
1281 IN EFI_HANDLE ImageHandle
,
1282 IN EFI_SYSTEM_TABLE
*SystemTable
1288 // Close the End of DXE event.
1290 Status
= gBS
->CloseEvent (mPlatVarCleanupLibEndOfDxeEvent
);
1291 ASSERT_EFI_ERROR (Status
);