2 BDS routines to handle capsules.
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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
38 EFI_BOOT_MODE BootMode
42 EFI_PEI_HOB_POINTERS HobPointer
;
43 EFI_CAPSULE_HEADER
*CapsuleHeader
;
46 UINT32 CapsuleTotalNumber
;
47 EFI_CAPSULE_TABLE
*CapsuleTable
;
52 VOID
**CapsulePtrCache
;
53 EFI_GUID
*CapsuleGuidCache
;
54 CAPSULE_HOB_INFO
*CapsuleHobInfo
;
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 return EFI_INVALID_PARAMETER
;
73 // Find all capsule images from hob
75 HobPointer
.Raw
= GetHobList ();
76 while ((HobPointer
.Raw
= GetNextGuidHob (&gEfiCapsuleVendorGuid
, HobPointer
.Raw
)) != NULL
) {
77 CapsuleTotalNumber
++;
79 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
82 if (CapsuleTotalNumber
== 0) {
84 // We didn't find a hob, so had no errors.
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
= GetNextGuidHob (&gEfiCapsuleVendorGuid
, HobPointer
.Raw
)) != NULL
) {
105 CapsuleHobInfo
= GET_GUID_HOB_DATA (HobPointer
.Guid
);
106 CapsulePtr
[CapsuleNumber
++] = (VOID
*)(UINTN
)(CapsuleHobInfo
->BaseAddress
);
108 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
112 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
113 //capsuleTable to configure table with EFI_CAPSULE_GUID
117 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
118 // System to have information persist across a system reset. EFI System Table must
119 // point to an array of capsules that contains the same CapsuleGuid value. And agents
120 // searching for this type capsule will look in EFI System Table and search for the
121 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
122 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
123 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
124 // array for later sorting capsules by CapsuleGuid.
126 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
127 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
128 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
130 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
131 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
132 // an additional one.
135 while (CacheIndex
< CacheNumber
) {
136 if (CompareGuid(&CapsuleGuidCache
[CacheIndex
],&CapsuleHeader
->CapsuleGuid
)) {
141 if (CacheIndex
== CacheNumber
) {
142 CopyMem(&CapsuleGuidCache
[CacheNumber
++],&CapsuleHeader
->CapsuleGuid
,sizeof(EFI_GUID
));
148 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
149 // whose guid is the same as it, and malloc memory for an array which preceding
150 // with UINT32. The array fills with entry point of capsules that have the same
151 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
152 // this array into EFI System Table, so that agents searching for this type capsule
153 // will look in EFI System Table and search for the capsule's Guid and associated
154 // pointer to retrieve the data.
157 while (CacheIndex
< CacheNumber
) {
159 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
160 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
161 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
162 if (CompareGuid (&CapsuleGuidCache
[CacheIndex
], &CapsuleHeader
->CapsuleGuid
)) {
164 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
166 CapsulePtrCache
[CapsuleNumber
++] = (VOID
*)CapsuleHeader
;
170 if (CapsuleNumber
!= 0) {
171 Size
= sizeof(EFI_CAPSULE_TABLE
) + (CapsuleNumber
- 1) * sizeof(VOID
*);
172 CapsuleTable
= AllocateRuntimePool (Size
);
173 ASSERT (CapsuleTable
!= NULL
);
174 CapsuleTable
->CapsuleArrayNumber
= CapsuleNumber
;
175 CopyMem(&CapsuleTable
->CapsulePtr
[0], CapsulePtrCache
, CapsuleNumber
* sizeof(VOID
*));
176 Status
= gBS
->InstallConfigurationTable (&CapsuleGuidCache
[CacheIndex
], (VOID
*)CapsuleTable
);
177 ASSERT_EFI_ERROR (Status
);
183 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
184 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
185 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
186 // type capsule transaction could be extended. It depends on platform policy.
188 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
189 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
190 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
192 // Call capsule library to process capsule image.
194 ProcessCapsuleImage (CapsuleHeader
);
198 PlatformBdsLockNonUpdatableFlash ();
201 // Free the allocated temp memory space.
203 FreePool (CapsuleGuidCache
);
204 FreePool (CapsulePtrCache
);
205 FreePool (CapsulePtr
);