2 BDS routines to handle capsules.
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 This routine is called to see if there are any capsules we need to process.
13 If the boot mode is not UPDATE, then we do nothing. Otherwise find the
14 capsule HOBS and produce firmware volumes for them via the DXE service.
15 Then call the dispatcher to dispatch drivers from them. Finally, check
16 the status of the updates.
18 This function should be called by BDS in case we need to do some
19 sort of processing even if there is no capsule to process. We
20 need to do this if an earlier update went away and we need to
21 clear the capsule variable so on the next reset PEI does not see it and
22 think there is a capsule available.
24 @param BootMode the current boot mode
26 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update
27 @retval EFI_SUCCESS There is no error when processing capsule
33 EFI_BOOT_MODE BootMode
37 EFI_PEI_HOB_POINTERS HobPointer
;
38 EFI_CAPSULE_HEADER
*CapsuleHeader
;
41 UINT32 CapsuleTotalNumber
;
42 EFI_CAPSULE_TABLE
*CapsuleTable
;
47 VOID
**CapsulePtrCache
;
48 EFI_GUID
*CapsuleGuidCache
;
52 CapsuleTotalNumber
= 0;
56 CapsulePtrCache
= NULL
;
57 CapsuleGuidCache
= NULL
;
61 // We don't do anything else if the boot mode is not flash-update
63 if (BootMode
!= BOOT_ON_FLASH_UPDATE
) {
64 DEBUG ((EFI_D_ERROR
, "Boot mode is not correct for capsule update.\n"));
65 return EFI_INVALID_PARAMETER
;
70 // Find all capsule images from hob
72 HobPointer
.Raw
= GetHobList ();
73 while ((HobPointer
.Raw
= GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE
, HobPointer
.Raw
)) != NULL
) {
74 CapsuleTotalNumber
++;
75 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
78 if (CapsuleTotalNumber
== 0) {
80 // We didn't find a hob, so had no errors.
82 DEBUG ((EFI_D_ERROR
, "We can not find capsule data in capsule update boot mode.\n"));
83 DEBUG ((EFI_D_ERROR
, "Please check the followings are correct if unexpected capsule update error happens.\n"));
84 DEBUG ((EFI_D_ERROR
, "1. CapsuleX64 is built as X64 module when PEI is IA32 and DXE is X64\n"));
85 DEBUG ((EFI_D_ERROR
, "2. Capsule data should persist in memory across a system reset.\n"));
86 PlatformBdsLockNonUpdatableFlash ();
91 // Init temp Capsule Data table.
93 CapsulePtr
= (VOID
**) AllocateZeroPool (sizeof (VOID
*) * CapsuleTotalNumber
);
94 ASSERT (CapsulePtr
!= NULL
);
95 CapsulePtrCache
= (VOID
**) AllocateZeroPool (sizeof (VOID
*) * CapsuleTotalNumber
);
96 ASSERT (CapsulePtrCache
!= NULL
);
97 CapsuleGuidCache
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
) * CapsuleTotalNumber
);
98 ASSERT (CapsuleGuidCache
!= NULL
);
101 // Find all capsule images from hob
103 HobPointer
.Raw
= GetHobList ();
104 while ((HobPointer
.Raw
= GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE
, HobPointer
.Raw
)) != NULL
) {
105 CapsulePtr
[CapsuleNumber
++] = (VOID
*) (UINTN
) HobPointer
.Capsule
->BaseAddress
;
106 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
110 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
111 //capsuleTable to configure table with EFI_CAPSULE_GUID
115 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
116 // System to have information persist across a system reset. EFI System Table must
117 // point to an array of capsules that contains the same CapsuleGuid value. And agents
118 // searching for this type capsule will look in EFI System Table and search for the
119 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
120 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
121 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
122 // array for later sorting capsules by CapsuleGuid.
124 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
125 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
126 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
128 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
129 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
130 // an additional one.
133 while (CacheIndex
< CacheNumber
) {
134 if (CompareGuid(&CapsuleGuidCache
[CacheIndex
],&CapsuleHeader
->CapsuleGuid
)) {
139 if (CacheIndex
== CacheNumber
) {
140 CopyMem(&CapsuleGuidCache
[CacheNumber
++],&CapsuleHeader
->CapsuleGuid
,sizeof(EFI_GUID
));
146 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
147 // whose guid is the same as it, and malloc memory for an array which preceding
148 // with UINT32. The array fills with entry point of capsules that have the same
149 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
150 // this array into EFI System Table, so that agents searching for this type capsule
151 // will look in EFI System Table and search for the capsule's Guid and associated
152 // pointer to retrieve the data.
155 while (CacheIndex
< CacheNumber
) {
157 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
158 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
159 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
160 if (CompareGuid (&CapsuleGuidCache
[CacheIndex
], &CapsuleHeader
->CapsuleGuid
)) {
162 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
164 CapsulePtrCache
[CapsuleNumber
++] = (VOID
*)CapsuleHeader
;
168 if (CapsuleNumber
!= 0) {
169 Size
= sizeof(EFI_CAPSULE_TABLE
) + (CapsuleNumber
- 1) * sizeof(VOID
*);
170 CapsuleTable
= AllocateRuntimePool (Size
);
171 ASSERT (CapsuleTable
!= NULL
);
172 CapsuleTable
->CapsuleArrayNumber
= CapsuleNumber
;
173 CopyMem(&CapsuleTable
->CapsulePtr
[0], CapsulePtrCache
, CapsuleNumber
* sizeof(VOID
*));
174 Status
= gBS
->InstallConfigurationTable (&CapsuleGuidCache
[CacheIndex
], (VOID
*)CapsuleTable
);
175 ASSERT_EFI_ERROR (Status
);
181 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
182 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
183 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
184 // type capsule transaction could be extended. It depends on platform policy.
186 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
187 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
188 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
190 // Always reset system after all capsule processed if FMP capsule exist
192 if (CompareGuid (&gEfiFmpCapsuleGuid
, &CapsuleHeader
->CapsuleGuid
)){
197 // Call capsule library to process capsule image.
199 ProcessCapsuleImage (CapsuleHeader
);
204 Print(L
"Capsule Request Cold Reboot.\n");
206 for (Index
= 5; Index
> 0; Index
--) {
207 Print(L
"\rResetting system in %d seconds ...", Index
);
208 gBS
->Stall (1000000);
211 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
216 PlatformBdsLockNonUpdatableFlash ();
219 // Free the allocated temp memory space.
221 FreePool (CapsuleGuidCache
);
222 FreePool (CapsulePtrCache
);
223 FreePool (CapsulePtr
);