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 ((EFI_D_INFO
, "%s - not found\n", VAR_ERROR_FLAG_NAME
));
75 return VAR_ERROR_FLAG_NO_ERROR
;
83 @param[in] Name Pointer to variable name.
84 @param[in] Guid Pointer to vendor guid.
86 @retval TRUE User variable.
87 @retval FALSE System variable.
97 VAR_CHECK_VARIABLE_PROPERTY Property
;
99 if (mVarCheck
== NULL
) {
100 gBS
->LocateProtocol (
101 &gEdkiiVarCheckProtocolGuid
,
106 ASSERT (mVarCheck
!= NULL
);
108 ZeroMem (&Property
, sizeof (Property
));
109 Status
= mVarCheck
->VariablePropertyGet (
114 if (EFI_ERROR (Status
)) {
116 // No property, it is user variable.
118 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - User variable: %g:%s\n", Guid
, Name
));
122 // DEBUG ((EFI_D_INFO, "PlatformVarCleanup - Variable Property: %g:%s\n", Guid, Name));
123 // DEBUG ((EFI_D_INFO, " Revision - 0x%04x\n", Property.Revision));
124 // DEBUG ((EFI_D_INFO, " Property - 0x%04x\n", Property.Property));
125 // DEBUG ((EFI_D_INFO, " Attribute - 0x%08x\n", Property.Attributes));
126 // DEBUG ((EFI_D_INFO, " MinSize - 0x%x\n", Property.MinSize));
127 // DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", Property.MaxSize));
133 Find user variable node by variable GUID.
135 @param[in] Guid Pointer to vendor guid.
137 @return Pointer to user variable node.
141 FindUserVariableNodeByGuid (
145 USER_VARIABLE_NODE
*UserVariableNode
;
148 for (Link
= mUserVariableList
.ForwardLink
149 ;Link
!= &mUserVariableList
150 ;Link
= Link
->ForwardLink
) {
151 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
153 if (CompareGuid (Guid
, &UserVariableNode
->Guid
)) {
157 return UserVariableNode
;
162 // Create new one if not found.
164 UserVariableNode
= AllocateZeroPool (sizeof (*UserVariableNode
));
165 ASSERT (UserVariableNode
!= NULL
);
166 UserVariableNode
->Signature
= USER_VARIABLE_NODE_SIGNATURE
;
167 CopyGuid (&UserVariableNode
->Guid
, Guid
);
169 // (36 chars of "########-####-####-####-############" + 1 space + 1 terminator) * sizeof (CHAR16).
171 UserVariableNode
->PromptString
= AllocatePool ((36 + 2) * sizeof (CHAR16
));
172 ASSERT (UserVariableNode
->PromptString
!= NULL
);
173 UnicodeSPrint (UserVariableNode
->PromptString
, (36 + 2) * sizeof (CHAR16
), L
" %g", &UserVariableNode
->Guid
);
174 InitializeListHead (&UserVariableNode
->NameLink
);
175 InsertTailList (&mUserVariableList
, &UserVariableNode
->Link
);
176 return UserVariableNode
;
180 Create user variable node.
184 CreateUserVariableNode (
189 EFI_STATUS GetVariableStatus
;
191 UINTN MaxVarNameSize
;
198 USER_VARIABLE_NODE
*UserVariableNode
;
199 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
204 // Initialize 128 * sizeof (CHAR16) variable name size.
206 MaxVarNameSize
= 128 * sizeof (CHAR16
);
207 VarName
= AllocateZeroPool (MaxVarNameSize
);
208 ASSERT (VarName
!= NULL
);
211 // Initialize 0x1000 variable data size.
213 MaxDataSize
= 0x1000;
214 Data
= AllocateZeroPool (MaxDataSize
);
215 ASSERT (Data
!= NULL
);
219 VarNameSize
= MaxVarNameSize
;
220 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
221 if (Status
== EFI_BUFFER_TOO_SMALL
) {
222 VarName
= ReallocatePool (MaxVarNameSize
, VarNameSize
, VarName
);
223 ASSERT (VarName
!= NULL
);
224 MaxVarNameSize
= VarNameSize
;
225 Status
= gRT
->GetNextVariableName (&VarNameSize
, VarName
, &Guid
);
228 if (!EFI_ERROR (Status
)) {
229 if (IsUserVariable (VarName
, &Guid
)) {
230 DataSize
= MaxDataSize
;
231 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
232 if (GetVariableStatus
== EFI_BUFFER_TOO_SMALL
) {
233 Data
= ReallocatePool (MaxDataSize
, DataSize
, Data
);
234 ASSERT (Data
!= NULL
);
235 MaxDataSize
= DataSize
;
236 GetVariableStatus
= gRT
->GetVariable (VarName
, &Guid
, &Attributes
, &DataSize
, Data
);
238 ASSERT_EFI_ERROR (GetVariableStatus
);
240 if ((Attributes
& EFI_VARIABLE_NON_VOLATILE
) != 0) {
241 UserVariableNode
= FindUserVariableNodeByGuid (&Guid
);
242 ASSERT (UserVariableNode
!= NULL
);
245 // Different variables that have same variable GUID share same user variable node.
247 UserVariableNameNode
= AllocateZeroPool (sizeof (*UserVariableNameNode
));
248 ASSERT (UserVariableNameNode
!= NULL
);
249 UserVariableNameNode
->Signature
= USER_VARIABLE_NAME_NODE_SIGNATURE
;
250 UserVariableNameNode
->Name
= AllocateCopyPool (VarNameSize
, VarName
);
251 UserVariableNameNode
->Attributes
= Attributes
;
252 UserVariableNameNode
->DataSize
= DataSize
;
253 UserVariableNameNode
->Index
= Index
;
254 UserVariableNameNode
->QuestionId
= (EFI_QUESTION_ID
) (USER_VARIABLE_QUESTION_ID
+ Index
);
256 // 2 space * sizeof (CHAR16) + StrSize.
258 StringSize
= 2 * sizeof (CHAR16
) + StrSize (UserVariableNameNode
->Name
);
259 UserVariableNameNode
->PromptString
= AllocatePool (StringSize
);
260 ASSERT (UserVariableNameNode
->PromptString
!= NULL
);
261 UnicodeSPrint (UserVariableNameNode
->PromptString
, StringSize
, L
" %s", UserVariableNameNode
->Name
);
263 // (33 chars of "Attribtues = 0x and DataSize = 0x" + 1 terminator + (sizeof (UINT32) + sizeof (UINTN)) * 2) * sizeof (CHAR16).
265 StringSize
= (33 + 1 + (sizeof (UINT32
) + sizeof (UINTN
)) * 2) * sizeof (CHAR16
);
266 UserVariableNameNode
->HelpString
= AllocatePool (StringSize
);
267 ASSERT (UserVariableNameNode
->HelpString
!= NULL
);
268 UnicodeSPrint (UserVariableNameNode
->HelpString
, StringSize
, L
"Attribtues = 0x%08x and DataSize = 0x%x", UserVariableNameNode
->Attributes
, UserVariableNameNode
->DataSize
);
269 UserVariableNameNode
->Deleted
= FALSE
;
270 InsertTailList (&UserVariableNode
->NameLink
, &UserVariableNameNode
->Link
);
275 } while (Status
!= EFI_NOT_FOUND
);
277 mUserVariableCount
= Index
;
278 ASSERT (mUserVariableCount
<= MAX_USER_VARIABLE_COUNT
);
279 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - User variable count: 0x%04x\n", mUserVariableCount
));
286 Destroy user variable nodes.
290 DestroyUserVariableNode (
294 USER_VARIABLE_NODE
*UserVariableNode
;
296 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
297 LIST_ENTRY
*NameLink
;
299 while (mUserVariableList
.ForwardLink
!= &mUserVariableList
) {
300 Link
= mUserVariableList
.ForwardLink
;
301 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
303 RemoveEntryList (&UserVariableNode
->Link
);
305 while (UserVariableNode
->NameLink
.ForwardLink
!= &UserVariableNode
->NameLink
) {
306 NameLink
= UserVariableNode
->NameLink
.ForwardLink
;
307 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
309 RemoveEntryList (&UserVariableNameNode
->Link
);
311 FreePool (UserVariableNameNode
->Name
);
312 FreePool (UserVariableNameNode
->PromptString
);
313 FreePool (UserVariableNameNode
->HelpString
);
314 FreePool (UserVariableNameNode
);
317 FreePool (UserVariableNode
->PromptString
);
318 FreePool (UserVariableNode
);
323 Create a time based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION_2
324 descriptor with the input data. NO authentication is required in this function.
326 @param[in, out] DataSize On input, the size of Data buffer in bytes.
327 On output, the size of data returned in Data
329 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
330 pointer to NULL to wrap an empty payload.
331 On output, Pointer to the new payload date buffer allocated from pool,
332 it's caller's responsibility to free the memory after using it.
334 @retval EFI_SUCCESS Create time based payload successfully.
335 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
336 @retval EFI_INVALID_PARAMETER The parameter is invalid.
337 @retval Others Unexpected error happens.
341 CreateTimeBasedPayload (
342 IN OUT UINTN
*DataSize
,
350 EFI_VARIABLE_AUTHENTICATION_2
*DescriptorData
;
351 UINTN DescriptorSize
;
354 if (Data
== NULL
|| DataSize
== NULL
) {
355 return EFI_INVALID_PARAMETER
;
359 // At user physical presence, the variable does not need to be signed but the
360 // parameters to the SetVariable() call still need to be prepared as authenticated
361 // variable. So we create EFI_VARIABLE_AUTHENTICATED_2 descriptor without certificate
365 PayloadSize
= *DataSize
;
367 DescriptorSize
= OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2
, AuthInfo
) + OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
368 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
369 if (NewData
== NULL
) {
370 return EFI_OUT_OF_RESOURCES
;
373 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
374 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
377 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION_2
*) (NewData
);
379 ZeroMem (&Time
, sizeof (EFI_TIME
));
380 Status
= gRT
->GetTime (&Time
, NULL
);
381 if (EFI_ERROR (Status
)) {
390 CopyMem (&DescriptorData
->TimeStamp
, &Time
, sizeof (EFI_TIME
));
392 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
);
393 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
394 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
395 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertPkcs7Guid
);
397 if (Payload
!= NULL
) {
401 *DataSize
= DescriptorSize
+ PayloadSize
;
407 Create a counter based data payload by concatenating the EFI_VARIABLE_AUTHENTICATION
408 descriptor with the input data. NO authentication is required in this function.
410 @param[in, out] DataSize On input, the size of Data buffer in bytes.
411 On output, the size of data returned in Data
413 @param[in, out] Data On input, Pointer to data buffer to be wrapped or
414 pointer to NULL to wrap an empty payload.
415 On output, Pointer to the new payload date buffer allocated from pool,
416 it's caller's responsibility to free the memory after using it.
418 @retval EFI_SUCCESS Create counter based payload successfully.
419 @retval EFI_OUT_OF_RESOURCES There are not enough memory resourses to create time based payload.
420 @retval EFI_INVALID_PARAMETER The parameter is invalid.
421 @retval Others Unexpected error happens.
425 CreateCounterBasedPayload (
426 IN OUT UINTN
*DataSize
,
434 EFI_VARIABLE_AUTHENTICATION
*DescriptorData
;
435 UINTN DescriptorSize
;
436 UINT64 MonotonicCount
;
438 if (Data
== NULL
|| DataSize
== NULL
) {
439 return EFI_INVALID_PARAMETER
;
443 // At user physical presence, the variable does not need to be signed but the
444 // parameters to the SetVariable() call still need to be prepared as authenticated
445 // variable. So we create EFI_VARIABLE_AUTHENTICATED descriptor without certificate
449 PayloadSize
= *DataSize
;
451 DescriptorSize
= (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION
, AuthInfo
)) + \
452 (OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
)) + \
453 sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
454 NewData
= (UINT8
*) AllocateZeroPool (DescriptorSize
+ PayloadSize
);
455 if (NewData
== NULL
) {
456 return EFI_OUT_OF_RESOURCES
;
459 if ((Payload
!= NULL
) && (PayloadSize
!= 0)) {
460 CopyMem (NewData
+ DescriptorSize
, Payload
, PayloadSize
);
463 DescriptorData
= (EFI_VARIABLE_AUTHENTICATION
*) (NewData
);
465 Status
= gBS
->GetNextMonotonicCount (&MonotonicCount
);
466 if (EFI_ERROR (Status
)) {
470 DescriptorData
->MonotonicCount
= MonotonicCount
;
472 DescriptorData
->AuthInfo
.Hdr
.dwLength
= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID
, CertData
) + sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256
);
473 DescriptorData
->AuthInfo
.Hdr
.wRevision
= 0x0200;
474 DescriptorData
->AuthInfo
.Hdr
.wCertificateType
= WIN_CERT_TYPE_EFI_GUID
;
475 CopyGuid (&DescriptorData
->AuthInfo
.CertType
, &gEfiCertTypeRsa2048Sha256Guid
);
477 if (Payload
!= NULL
) {
481 *DataSize
= DescriptorSize
+ PayloadSize
;
487 Delete user variable.
489 @param[in] DeleteAll Delete all user variables.
490 @param[in] VariableCleanupData Pointer to variable cleanup data.
495 IN BOOLEAN DeleteAll
,
496 IN VARIABLE_CLEANUP_DATA
*VariableCleanupData OPTIONAL
500 USER_VARIABLE_NODE
*UserVariableNode
;
502 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
503 LIST_ENTRY
*NameLink
;
507 for (Link
= mUserVariableList
.ForwardLink
508 ;Link
!= &mUserVariableList
509 ;Link
= Link
->ForwardLink
) {
510 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
512 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
513 ;NameLink
!= &UserVariableNode
->NameLink
514 ;NameLink
= NameLink
->ForwardLink
) {
515 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
517 if (!UserVariableNameNode
->Deleted
&& (DeleteAll
|| ((VariableCleanupData
!= NULL
) && (VariableCleanupData
->UserVariable
[UserVariableNameNode
->Index
] == TRUE
)))) {
518 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - Delete variable: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
519 if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
) != 0) {
522 Status
= CreateTimeBasedPayload (&DataSize
, &Data
);
523 if (!EFI_ERROR (Status
)) {
524 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
527 } else if ((UserVariableNameNode
->Attributes
& EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
) != 0) {
530 Status
= CreateCounterBasedPayload (&DataSize
, &Data
);
531 if (!EFI_ERROR (Status
)) {
532 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, UserVariableNameNode
->Attributes
, DataSize
, Data
);
536 Status
= gRT
->SetVariable (UserVariableNameNode
->Name
, &UserVariableNode
->Guid
, 0, 0, NULL
);
538 if (!EFI_ERROR (Status
)) {
539 UserVariableNameNode
->Deleted
= TRUE
;
541 DEBUG ((EFI_D_INFO
, "PlatformVarCleanup - Delete variable fail: %g:%s\n", &UserVariableNode
->Guid
, UserVariableNameNode
->Name
));
549 This function allows a caller to extract the current configuration for one
550 or more named elements from the target driver.
552 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
553 @param[in] Request A null-terminated Unicode string in <ConfigRequest> format.
554 @param[out] Progress On return, points to a character in the Request string.
555 Points to the string's null terminator if request was successful.
556 Points to the most recent '&' before the first failing name/value
557 pair (or the beginning of the string if the failure is in the
558 first name/value pair) if the request was not successful.
559 @param[out] Results A null-terminated Unicode string in <ConfigAltResp> format which
560 has all values filled in for the names in the Request string.
561 String to be allocated by the called function.
563 @retval EFI_SUCCESS The Results is filled with the requested values.
564 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
565 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
566 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
571 VariableCleanupHiiExtractConfig (
572 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
573 IN CONST EFI_STRING Request
,
574 OUT EFI_STRING
*Progress
,
575 OUT EFI_STRING
*Results
579 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
581 EFI_STRING ConfigRequestHdr
;
582 EFI_STRING ConfigRequest
;
583 BOOLEAN AllocatedRequest
;
586 if (Progress
== NULL
|| Results
== NULL
) {
587 return EFI_INVALID_PARAMETER
;
591 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
592 return EFI_NOT_FOUND
;
595 ConfigRequestHdr
= NULL
;
596 ConfigRequest
= NULL
;
597 AllocatedRequest
= FALSE
;
600 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
602 // Convert buffer data to <ConfigResp> by helper function BlockToConfig().
604 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
605 ConfigRequest
= Request
;
606 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
608 // Request has no request element, construct full request string.
609 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
610 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator.
612 ConfigRequestHdr
= HiiConstructConfigHdr (&mVariableCleanupHiiGuid
, mVarStoreName
, Private
->HiiHandle
);
613 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
614 ConfigRequest
= AllocateZeroPool (Size
);
615 ASSERT (ConfigRequest
!= NULL
);
616 AllocatedRequest
= TRUE
;
617 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
618 FreePool (ConfigRequestHdr
);
621 Status
= Private
->ConfigRouting
->BlockToConfig (
622 Private
->ConfigRouting
,
624 (UINT8
*) &Private
->VariableCleanupData
,
629 ASSERT_EFI_ERROR (Status
);
632 // Free the allocated config request string.
634 if (AllocatedRequest
) {
635 FreePool (ConfigRequest
);
636 ConfigRequest
= NULL
;
639 // Set Progress string to the original request string or the string's null terminator.
641 if (Request
== NULL
) {
643 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
644 *Progress
= Request
+ StrLen (Request
);
651 Update user variable form.
653 @param[in] Private Points to the VARIABLE_CLEANUP_HII_PRIVATE_DATA.
657 UpdateUserVariableForm (
658 IN VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
661 EFI_STRING_ID PromptStringToken
;
662 EFI_STRING_ID HelpStringToken
;
663 VOID
*StartOpCodeHandle
;
664 VOID
*EndOpCodeHandle
;
665 EFI_IFR_GUID_LABEL
*StartLabel
;
666 EFI_IFR_GUID_LABEL
*EndLabel
;
667 USER_VARIABLE_NODE
*UserVariableNode
;
669 USER_VARIABLE_NAME_NODE
*UserVariableNameNode
;
670 LIST_ENTRY
*NameLink
;
674 // Init OpCode Handle.
676 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
677 ASSERT (StartOpCodeHandle
!= NULL
);
679 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
680 ASSERT (EndOpCodeHandle
!= NULL
);
683 // Create Hii Extend Label OpCode as the start opcode.
685 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
686 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
687 StartLabel
->Number
= LABEL_START
;
690 // Create Hii Extend Label OpCode as the end opcode.
692 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (EndOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
693 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
694 EndLabel
->Number
= LABEL_END
;
698 &mVariableCleanupHiiGuid
,
699 FORM_ID_VARIABLE_CLEANUP
,
700 StartOpCodeHandle
, // LABEL_START
701 EndOpCodeHandle
// LABEL_END
704 for (Link
= mUserVariableList
.ForwardLink
705 ;Link
!= &mUserVariableList
706 ;Link
= Link
->ForwardLink
) {
707 UserVariableNode
= USER_VARIABLE_FROM_LINK (Link
);
710 // Create checkbox opcode for variables in the same variable GUID space.
713 for (NameLink
= UserVariableNode
->NameLink
.ForwardLink
714 ;NameLink
!= &UserVariableNode
->NameLink
715 ;NameLink
= NameLink
->ForwardLink
) {
716 UserVariableNameNode
= USER_VARIABLE_NAME_FROM_LINK (NameLink
);
718 if (!UserVariableNameNode
->Deleted
) {
721 // Create subtitle opcode for variable GUID.
723 PromptStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNode
->PromptString
, NULL
);
724 HiiCreateSubTitleOpCode (StartOpCodeHandle
, PromptStringToken
, 0, 0, 0);
729 // Only create opcode for the non-deleted variables.
731 PromptStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNameNode
->PromptString
, NULL
);
732 HelpStringToken
= HiiSetString (Private
->HiiHandle
, 0, UserVariableNameNode
->HelpString
, NULL
);
733 HiiCreateCheckBoxOpCode (
735 UserVariableNameNode
->QuestionId
,
736 VARIABLE_CLEANUP_VARSTORE_ID
,
737 (UINT16
) (USER_VARIABLE_VAR_OFFSET
+ UserVariableNameNode
->Index
),
740 EFI_IFR_FLAG_CALLBACK
,
741 Private
->VariableCleanupData
.UserVariable
[UserVariableNameNode
->Index
],
748 HiiCreateSubTitleOpCode (
750 STRING_TOKEN (STR_NULL_STRING
),
757 // Create the "Apply changes" and "Discard changes" tags.
759 HiiCreateActionOpCode (
761 SAVE_AND_EXIT_QUESTION_ID
,
762 STRING_TOKEN (STR_SAVE_AND_EXIT
),
763 STRING_TOKEN (STR_NULL_STRING
),
764 EFI_IFR_FLAG_CALLBACK
,
767 HiiCreateActionOpCode (
769 NO_SAVE_AND_EXIT_QUESTION_ID
,
770 STRING_TOKEN (STR_NO_SAVE_AND_EXIT
),
771 STRING_TOKEN (STR_NULL_STRING
),
772 EFI_IFR_FLAG_CALLBACK
,
778 &mVariableCleanupHiiGuid
,
779 FORM_ID_VARIABLE_CLEANUP
,
780 StartOpCodeHandle
, // LABEL_START
781 EndOpCodeHandle
// LABEL_END
784 HiiFreeOpCodeHandle (StartOpCodeHandle
);
785 HiiFreeOpCodeHandle (EndOpCodeHandle
);
789 This function applies changes in a driver's configuration.
790 Input is a Configuration, which has the routing data for this
791 driver followed by name / value configuration pairs. The driver
792 must apply those pairs to its configurable storage. If the
793 driver's configuration is stored in a linear block of data
794 and the driver's name / value pairs are in <BlockConfig>
795 format, it may use the ConfigToBlock helper function (above) to
796 simplify the job. Currently not implemented.
798 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
799 @param[in] Configuration A null-terminated Unicode string in
800 <ConfigString> format.
801 @param[out] Progress A pointer to a string filled in with the
802 offset of the most recent '&' before the
803 first failing name / value pair (or the
804 beginn ing of the string if the failure
805 is in the first name / value pair) or
806 the terminating NULL if all was
809 @retval EFI_SUCCESS The results have been distributed or are
810 awaiting distribution.
811 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
812 parts of the results that must be
813 stored awaiting possible future
815 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
816 Results parameter would result
817 in this type of error.
818 @retval EFI_NOT_FOUND Target for the specified routing data
824 VariableCleanupHiiRouteConfig (
825 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
826 IN CONST EFI_STRING Configuration
,
827 OUT EFI_STRING
*Progress
831 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
834 if (Progress
== NULL
) {
835 return EFI_INVALID_PARAMETER
;
837 *Progress
= Configuration
;
839 if (Configuration
== NULL
) {
840 return EFI_INVALID_PARAMETER
;
844 // Check routing data in <ConfigHdr>.
845 // Note: there is no name for Name/Value storage, only GUID will be checked.
847 if (!HiiIsConfigHdrMatch (Configuration
, &mVariableCleanupHiiGuid
, mVarStoreName
)) {
848 return EFI_NOT_FOUND
;
851 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
853 // Get Buffer Storage data.
855 BufferSize
= sizeof (VARIABLE_CLEANUP_DATA
);
857 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock().
859 Status
= Private
->ConfigRouting
->ConfigToBlock (
860 Private
->ConfigRouting
,
862 (UINT8
*) &Private
->VariableCleanupData
,
866 ASSERT_EFI_ERROR (Status
);
868 DeleteUserVariable (FALSE
, &Private
->VariableCleanupData
);
870 // For "F10" hotkey to refresh the form.
872 // UpdateUserVariableForm (Private);
878 This function is called to provide results data to the driver.
879 This data consists of a unique key that is used to identify
880 which data is either being passed back or being asked for.
882 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
883 @param[in] Action Specifies the type of action taken by the browser.
884 @param[in] QuestionId A unique value which is sent to the original
885 exporting driver so that it can identify the type
886 of data to expect. The format of the data tends to
887 vary based on the opcode that generated the callback.
888 @param[in] Type The type of value for the question.
889 @param[in] Value A pointer to the data being sent to the original
891 @param[out] ActionRequest On return, points to the action requested by the
894 @retval EFI_SUCCESS The callback successfully handled the action.
895 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
896 variable and its data.
897 @retval EFI_DEVICE_ERROR The variable could not be saved.
898 @retval EFI_UNSUPPORTED The specified Action is not supported by the
903 VariableCleanupHiiCallback (
904 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
905 IN EFI_BROWSER_ACTION Action
,
906 IN EFI_QUESTION_ID QuestionId
,
908 IN EFI_IFR_TYPE_VALUE
*Value
,
909 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
912 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
913 VARIABLE_CLEANUP_DATA
*VariableCleanupData
;
915 Private
= VARIABLE_CLEANUP_HII_PRIVATE_FROM_THIS (This
);
917 if ((Action
!= EFI_BROWSER_ACTION_CHANGING
) && (Action
!= EFI_BROWSER_ACTION_CHANGED
)) {
919 // All other action return unsupported.
921 return EFI_UNSUPPORTED
;
925 // Retrieve uncommitted data from Form Browser.
927 VariableCleanupData
= &Private
->VariableCleanupData
;
928 HiiGetBrowserData (&mVariableCleanupHiiGuid
, mVarStoreName
, sizeof (VARIABLE_CLEANUP_DATA
), (UINT8
*) VariableCleanupData
);
929 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
931 return EFI_INVALID_PARAMETER
;
933 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
934 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
935 return EFI_INVALID_PARAMETER
;
937 if ((QuestionId
>= USER_VARIABLE_QUESTION_ID
) && (QuestionId
< USER_VARIABLE_QUESTION_ID
+ MAX_USER_VARIABLE_COUNT
)) {
940 // Means one user variable checkbox is marked to delete but not press F10 or "Commit Changes and Exit" menu.
942 mMarkedUserVariableCount
++;
943 ASSERT (mMarkedUserVariableCount
<= mUserVariableCount
);
944 if (mMarkedUserVariableCount
== mUserVariableCount
) {
946 // All user variables have been marked, then also mark the SelectAll checkbox.
948 VariableCleanupData
->SelectAll
= TRUE
;
952 // Means one user variable checkbox is unmarked.
954 mMarkedUserVariableCount
--;
956 // Also unmark the SelectAll checkbox.
958 VariableCleanupData
->SelectAll
= FALSE
;
961 switch (QuestionId
) {
962 case SELECT_ALL_QUESTION_ID
:
965 // Means the SelectAll checkbox is marked to delete all user variables but not press F10 or "Commit Changes and Exit" menu.
967 SetMem (VariableCleanupData
->UserVariable
, sizeof (VariableCleanupData
->UserVariable
), TRUE
);
968 mMarkedUserVariableCount
= mUserVariableCount
;
971 // Means the SelectAll checkbox is unmarked.
973 SetMem (VariableCleanupData
->UserVariable
, sizeof (VariableCleanupData
->UserVariable
), FALSE
);
974 mMarkedUserVariableCount
= 0;
977 case SAVE_AND_EXIT_QUESTION_ID
:
978 DeleteUserVariable (FALSE
, VariableCleanupData
);
979 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_SUBMIT_EXIT
;
982 case NO_SAVE_AND_EXIT_QUESTION_ID
:
984 // Restore local maintain data.
986 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
996 // Pass changed uncommitted data back to Form Browser.
998 HiiSetBrowserData (&mVariableCleanupHiiGuid
, mVarStoreName
, sizeof (VARIABLE_CLEANUP_DATA
), (UINT8
*) VariableCleanupData
, NULL
);
1003 Platform variable cleanup.
1005 @param[in] Flag Variable error flag.
1006 @param[in] Type Variable cleanup type.
1007 If it is VarCleanupManually, the interface must be called after console connected.
1009 @retval EFI_SUCCESS No error or error processed.
1010 @retval EFI_UNSUPPORTED The specified Flag or Type is not supported.
1011 For example, system error may be not supported to process and Platform should have mechanism to reset system to manufacture mode.
1012 Another, if system and user variables are wanted to be distinguished to process, the interface must be called after EndOfDxe.
1013 @retval EFI_OUT_OF_RESOURCES Not enough resource to process the error.
1014 @retval EFI_INVALID_PARAMETER The specified Flag or Type is an invalid value.
1015 @retval Others Other failure occurs.
1020 PlatformVarCleanup (
1021 IN VAR_ERROR_FLAG Flag
,
1022 IN VAR_CLEANUP_TYPE Type
1026 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
1027 VARIABLE_CLEANUP_HII_PRIVATE_DATA
*Private
;
1031 // This implementation must be called after EndOfDxe.
1033 return EFI_UNSUPPORTED
;
1036 if ((Type
>= VarCleanupMax
) || ((Flag
& ((VAR_ERROR_FLAG
) (VAR_ERROR_FLAG_SYSTEM_ERROR
& VAR_ERROR_FLAG_USER_ERROR
))) == 0)) {
1037 return EFI_INVALID_PARAMETER
;
1040 if (Flag
== VAR_ERROR_FLAG_NO_ERROR
) {
1042 // Just return success if no error.
1047 if ((Flag
& (~((VAR_ERROR_FLAG
) VAR_ERROR_FLAG_SYSTEM_ERROR
))) == 0) {
1049 // This sample does not support system variables cleanup.
1051 DEBUG ((EFI_D_ERROR
, "NOTICE - VAR_ERROR_FLAG_SYSTEM_ERROR\n"));
1052 DEBUG ((EFI_D_ERROR
, "Platform should have mechanism to reset system to manufacture mode\n"));
1053 return EFI_UNSUPPORTED
;
1057 // Continue to process VAR_ERROR_FLAG_USER_ERROR.
1061 // Create user variable nodes for the following processing.
1063 CreateUserVariableNode ();
1067 DeleteUserVariable (TRUE
, NULL
);
1069 // Destroyed the created user variable nodes
1071 DestroyUserVariableNode ();
1075 case VarCleanupManually
:
1077 // Locate FormBrowser2 protocol.
1079 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
1080 if (EFI_ERROR (Status
)) {
1084 Private
= AllocateZeroPool (sizeof (VARIABLE_CLEANUP_HII_PRIVATE_DATA
));
1085 if (Private
== NULL
) {
1086 return EFI_OUT_OF_RESOURCES
;
1089 Private
->Signature
= VARIABLE_CLEANUP_HII_PRIVATE_SIGNATURE
;
1090 Private
->ConfigAccess
.ExtractConfig
= VariableCleanupHiiExtractConfig
;
1091 Private
->ConfigAccess
.RouteConfig
= VariableCleanupHiiRouteConfig
;
1092 Private
->ConfigAccess
.Callback
= VariableCleanupHiiCallback
;
1094 Status
= gBS
->LocateProtocol (
1095 &gEfiHiiConfigRoutingProtocolGuid
,
1097 (VOID
**) &Private
->ConfigRouting
1099 if (EFI_ERROR (Status
)) {
1104 // Install Device Path Protocol and Config Access protocol to driver handle.
1106 Status
= gBS
->InstallMultipleProtocolInterfaces (
1107 &Private
->DriverHandle
,
1108 &gEfiDevicePathProtocolGuid
,
1109 &mVarCleanupHiiVendorDevicePath
,
1110 &gEfiHiiConfigAccessProtocolGuid
,
1111 &Private
->ConfigAccess
,
1114 if (EFI_ERROR (Status
)) {
1119 // Publish our HII data.
1121 Private
->HiiHandle
= HiiAddPackages (
1122 &mVariableCleanupHiiGuid
,
1123 Private
->DriverHandle
,
1124 PlatformVarCleanupLibStrings
,
1128 if (Private
->HiiHandle
== NULL
) {
1129 Status
= EFI_OUT_OF_RESOURCES
;
1133 UpdateUserVariableForm (Private
);
1135 Status
= FormBrowser2
->SendForm (
1137 &Private
->HiiHandle
,
1147 return EFI_UNSUPPORTED
;
1152 if (Private
->DriverHandle
!= NULL
) {
1153 gBS
->UninstallMultipleProtocolInterfaces (
1154 Private
->DriverHandle
,
1155 &gEfiDevicePathProtocolGuid
,
1156 &mVarCleanupHiiVendorDevicePath
,
1157 &gEfiHiiConfigAccessProtocolGuid
,
1158 &Private
->ConfigAccess
,
1162 if (Private
->HiiHandle
!= NULL
) {
1163 HiiRemovePackages (Private
->HiiHandle
);
1169 // Destroyed the created user variable nodes
1171 DestroyUserVariableNode ();
1176 Get last boot variable error flag.
1178 @return Last boot variable error flag.
1183 GetLastBootVarErrorFlag (
1187 return mLastVarErrorFlag
;
1191 Notification function of END_OF_DXE.
1193 This is a notification function registered on END_OF_DXE event.
1195 @param[in] Event Event whose notification function is being invoked.
1196 @param[in] Context Pointer to the notification function's context.
1201 PlatformVarCleanupEndOfDxeEvent (
1210 The constructor function caches the pointer to VarCheck protocol and last boot variable error flag.
1212 The constructor function locates VarCheck protocol from protocol database.
1213 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
1215 @param ImageHandle The firmware allocated handle for the EFI image.
1216 @param SystemTable A pointer to the EFI System Table.
1218 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1223 PlatformVarCleanupLibConstructor (
1224 IN EFI_HANDLE ImageHandle
,
1225 IN EFI_SYSTEM_TABLE
*SystemTable
1230 mLastVarErrorFlag
= InternalGetVarErrorFlag ();
1231 DEBUG ((EFI_D_INFO
, "mLastVarErrorFlag - 0x%02x\n", mLastVarErrorFlag
));
1234 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.
1236 Status
= gBS
->CreateEventEx (
1239 PlatformVarCleanupEndOfDxeEvent
,
1241 &gEfiEndOfDxeEventGroupGuid
,
1242 &mPlatVarCleanupLibEndOfDxeEvent
1244 ASSERT_EFI_ERROR (Status
);
1250 The destructor function closes the End of DXE event.
1252 @param ImageHandle The firmware allocated handle for the EFI image.
1253 @param SystemTable A pointer to the EFI System Table.
1255 @retval EFI_SUCCESS The destructor completed successfully.
1260 PlatformVarCleanupLibDestructor (
1261 IN EFI_HANDLE ImageHandle
,
1262 IN EFI_SYSTEM_TABLE
*SystemTable
1268 // Close the End of DXE event.
1270 Status
= gBS
->CloseEvent (mPlatVarCleanupLibEndOfDxeEvent
);
1271 ASSERT_EFI_ERROR (Status
);