2 BDS routines to handle capsules.
4 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 This routine is called to see if there are any capsules we need to process.
19 If the boot mode is not UPDATE, then we do nothing. Otherwise find the
20 capsule HOBS and produce firmware volumes for them via the DXE service.
21 Then call the dispatcher to dispatch drivers from them. Finally, check
22 the status of the updates.
24 This function should be called by BDS in case we need to do some
25 sort of processing even if there is no capsule to process. We
26 need to do this if an earlier update went away and we need to
27 clear the capsule variable so on the next reset PEI does not see it and
28 think there is a capsule available.
30 @param BootMode the current boot mode
32 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update
33 @retval EFI_SUCCESS There is no error when processing capsule
39 EFI_BOOT_MODE BootMode
43 EFI_PEI_HOB_POINTERS HobPointer
;
44 EFI_CAPSULE_HEADER
*CapsuleHeader
;
47 UINT32 CapsuleTotalNumber
;
48 EFI_CAPSULE_TABLE
*CapsuleTable
;
53 VOID
**CapsulePtrCache
;
54 EFI_GUID
*CapsuleGuidCache
;
57 CapsuleTotalNumber
= 0;
61 CapsulePtrCache
= NULL
;
62 CapsuleGuidCache
= NULL
;
65 // We don't do anything else if the boot mode is not flash-update
67 if (BootMode
!= BOOT_ON_FLASH_UPDATE
) {
68 DEBUG ((EFI_D_ERROR
, "Boot mode is not correct for capsule update.\n"));
69 return EFI_INVALID_PARAMETER
;
74 // Find all capsule images from hob
76 HobPointer
.Raw
= GetHobList ();
77 while ((HobPointer
.Raw
= GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE
, HobPointer
.Raw
)) != NULL
) {
78 CapsuleTotalNumber
++;
79 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
82 if (CapsuleTotalNumber
== 0) {
84 // We didn't find a hob, so had no errors.
86 DEBUG ((EFI_D_ERROR
, "We can not find capsule data in capsule update boot mode.\n"));
87 DEBUG ((EFI_D_ERROR
, "Please check the followings are correct if unexpected capsule update error happens.\n"));
88 DEBUG ((EFI_D_ERROR
, "1. CapsuleX64 is built as X64 module when PEI is IA32 and DXE is X64\n"));
89 DEBUG ((EFI_D_ERROR
, "2. Capsule data should persist in memory across a system reset.\n"));
90 PlatformBdsLockNonUpdatableFlash ();
95 // Init temp Capsule Data table.
97 CapsulePtr
= (VOID
**) AllocateZeroPool (sizeof (VOID
*) * CapsuleTotalNumber
);
98 ASSERT (CapsulePtr
!= NULL
);
99 CapsulePtrCache
= (VOID
**) AllocateZeroPool (sizeof (VOID
*) * CapsuleTotalNumber
);
100 ASSERT (CapsulePtrCache
!= NULL
);
101 CapsuleGuidCache
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
) * CapsuleTotalNumber
);
102 ASSERT (CapsuleGuidCache
!= NULL
);
105 // Find all capsule images from hob
107 HobPointer
.Raw
= GetHobList ();
108 while ((HobPointer
.Raw
= GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE
, HobPointer
.Raw
)) != NULL
) {
109 CapsulePtr
[CapsuleNumber
++] = (VOID
*) (UINTN
) HobPointer
.Capsule
->BaseAddress
;
110 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
114 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
115 //capsuleTable to configure table with EFI_CAPSULE_GUID
119 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
120 // System to have information persist across a system reset. EFI System Table must
121 // point to an array of capsules that contains the same CapsuleGuid value. And agents
122 // searching for this type capsule will look in EFI System Table and search for the
123 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
124 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
125 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
126 // array for later sorting capsules by CapsuleGuid.
128 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
129 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
130 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
132 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
133 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
134 // an additional one.
137 while (CacheIndex
< CacheNumber
) {
138 if (CompareGuid(&CapsuleGuidCache
[CacheIndex
],&CapsuleHeader
->CapsuleGuid
)) {
143 if (CacheIndex
== CacheNumber
) {
144 CopyMem(&CapsuleGuidCache
[CacheNumber
++],&CapsuleHeader
->CapsuleGuid
,sizeof(EFI_GUID
));
150 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
151 // whose guid is the same as it, and malloc memory for an array which preceding
152 // with UINT32. The array fills with entry point of capsules that have the same
153 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
154 // this array into EFI System Table, so that agents searching for this type capsule
155 // will look in EFI System Table and search for the capsule's Guid and associated
156 // pointer to retrieve the data.
159 while (CacheIndex
< CacheNumber
) {
161 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
162 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
163 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
164 if (CompareGuid (&CapsuleGuidCache
[CacheIndex
], &CapsuleHeader
->CapsuleGuid
)) {
166 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
168 CapsulePtrCache
[CapsuleNumber
++] = (VOID
*)CapsuleHeader
;
172 if (CapsuleNumber
!= 0) {
173 Size
= sizeof(EFI_CAPSULE_TABLE
) + (CapsuleNumber
- 1) * sizeof(VOID
*);
174 CapsuleTable
= AllocateRuntimePool (Size
);
175 ASSERT (CapsuleTable
!= NULL
);
176 CapsuleTable
->CapsuleArrayNumber
= CapsuleNumber
;
177 CopyMem(&CapsuleTable
->CapsulePtr
[0], CapsulePtrCache
, CapsuleNumber
* sizeof(VOID
*));
178 Status
= gBS
->InstallConfigurationTable (&CapsuleGuidCache
[CacheIndex
], (VOID
*)CapsuleTable
);
179 ASSERT_EFI_ERROR (Status
);
185 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
186 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
187 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
188 // type capsule transaction could be extended. It depends on platform policy.
190 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
191 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
192 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
194 // Call capsule library to process capsule image.
196 ProcessCapsuleImage (CapsuleHeader
);
200 PlatformBdsLockNonUpdatableFlash ();
203 // Free the allocated temp memory space.
205 FreePool (CapsuleGuidCache
);
206 FreePool (CapsulePtrCache
);
207 FreePool (CapsulePtr
);