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.
17 This function locks the block
19 @param CpuIo A instance of EFI_CPU_IO_PROTOCOL.
20 @param Base The base address flash region to be locked.
25 IN EFI_CPU_IO_PROTOCOL
*CpuIo
,
26 IN EFI_PHYSICAL_ADDRESS Base
29 EFI_FV_BLOCK_MAP_ENTRY
*BlockMap
;
30 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
31 EFI_PHYSICAL_ADDRESS BaseAddress
;
36 BaseAddress
= Base
- 0x400000 + 2;
37 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) ((UINTN
) (Base
));
38 BlockMap
= &(FvHeader
->BlockMap
[0]);
40 while ((BlockMap
->NumBlocks
!= 0) && (BlockMap
->Length
!= 0)) {
41 BlockLength
= BlockMap
->Length
;
42 for (Index
= 0; Index
< BlockMap
->NumBlocks
; Index
++) {
50 Data
= (UINT8
) (Data
| 0x3);
58 BaseAddress
+= BlockLength
;
67 This routine is called to see if there are any capsules we need to process.
68 If the boot mode is not UPDATE, then we do nothing. Otherwise find the
69 capsule HOBS and produce firmware volumes for them via the DXE service.
70 Then call the dispatcher to dispatch drivers from them. Finally, check
71 the status of the updates.
73 This function should be called by BDS in case we need to do some
74 sort of processing even if there is no capsule to process. We
75 need to do this if an earlier update went away and we need to
76 clear the capsule variable so on the next reset PEI does not see it and
77 think there is a capsule available.
79 @param BootMode the current boot mode
81 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update
82 @retval EFI_SUCCESS There is no error when processing capsule
87 EFI_BOOT_MODE BootMode
91 EFI_PEI_HOB_POINTERS HobPointer
;
92 EFI_CAPSULE_HEADER
*CapsuleHeader
;
95 UINT32 CapsuleTotalNumber
;
96 EFI_CAPSULE_TABLE
*CapsuleTable
;
101 VOID
**CapsulePtrCache
;
102 EFI_GUID
*CapsuleGuidCache
;
103 CAPSULE_HOB_INFO
*CapsuleHobInfo
;
106 CapsuleTotalNumber
= 0;
110 CapsulePtrCache
= NULL
;
111 CapsuleGuidCache
= NULL
;
114 // We don't do anything else if the boot mode is not flash-update
116 if (BootMode
!= BOOT_ON_FLASH_UPDATE
) {
117 return EFI_INVALID_PARAMETER
;
120 Status
= EFI_SUCCESS
;
122 // Find all capsule images from hob
124 HobPointer
.Raw
= GetHobList ();
125 while ((HobPointer
.Raw
= GetNextGuidHob (&gEfiCapsuleVendorGuid
, HobPointer
.Raw
)) != NULL
) {
126 CapsuleTotalNumber
++;
128 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
131 if (CapsuleTotalNumber
== 0) {
133 // We didn't find a hob, so had no errors.
135 PlatformBdsLockNonUpdatableFlash ();
140 // Init temp Capsule Data table.
142 CapsulePtr
= (VOID
**) AllocateZeroPool (sizeof (VOID
*) * CapsuleTotalNumber
);
143 ASSERT (CapsulePtr
!= NULL
);
144 CapsulePtrCache
= (VOID
**) AllocateZeroPool (sizeof (VOID
*) * CapsuleTotalNumber
);
145 ASSERT (CapsulePtrCache
!= NULL
);
146 CapsuleGuidCache
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
) * CapsuleTotalNumber
);
147 ASSERT (CapsuleGuidCache
!= NULL
);
150 // Find all capsule images from hob
152 HobPointer
.Raw
= GetHobList ();
153 while ((HobPointer
.Raw
= GetNextGuidHob (&gEfiCapsuleVendorGuid
, HobPointer
.Raw
)) != NULL
) {
154 CapsuleHobInfo
= GET_GUID_HOB_DATA (HobPointer
.Guid
);
155 CapsulePtr
[CapsuleNumber
++] = (VOID
*)(UINTN
)(CapsuleHobInfo
->BaseAddress
);
157 HobPointer
.Raw
= GET_NEXT_HOB (HobPointer
);
161 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
162 //capsuleTable to configure table with EFI_CAPSULE_GUID
166 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
167 // System to have information persist across a system reset. EFI System Table must
168 // point to an array of capsules that contains the same CapsuleGuid value. And agents
169 // searching for this type capsule will look in EFI System Table and search for the
170 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
171 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
172 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
173 // array for later sorting capsules by CapsuleGuid.
175 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
176 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
177 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
179 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
180 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
181 // an additional one.
184 while (CacheIndex
< CacheNumber
) {
185 if (CompareGuid(&CapsuleGuidCache
[CacheIndex
],&CapsuleHeader
->CapsuleGuid
)) {
190 if (CacheIndex
== CacheNumber
) {
191 CopyMem(&CapsuleGuidCache
[CacheNumber
++],&CapsuleHeader
->CapsuleGuid
,sizeof(EFI_GUID
));
197 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
198 // whose guid is the same as it, and malloc memory for an array which preceding
199 // with UINT32. The array fills with entry point of capsules that have the same
200 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
201 // this array into EFI System Table, so that agents searching for this type capsule
202 // will look in EFI System Table and search for the capsule's Guid and associated
203 // pointer to retrieve the data.
206 while (CacheIndex
< CacheNumber
) {
208 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
209 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
210 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
211 if (CompareGuid (&CapsuleGuidCache
[CacheIndex
], &CapsuleHeader
->CapsuleGuid
)) {
213 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
215 CapsulePtrCache
[CapsuleNumber
++] = (VOID
*)CapsuleHeader
;
219 if (CapsuleNumber
!= 0) {
220 Size
= sizeof(EFI_CAPSULE_TABLE
) + (CapsuleNumber
- 1) * sizeof(VOID
*);
221 CapsuleTable
= AllocateRuntimePool (Size
);
222 ASSERT (CapsuleTable
!= NULL
);
223 CapsuleTable
->CapsuleArrayNumber
= CapsuleNumber
;
224 CopyMem(&CapsuleTable
->CapsulePtr
[0], CapsulePtrCache
, CapsuleNumber
* sizeof(VOID
*));
225 Status
= gBS
->InstallConfigurationTable (&CapsuleGuidCache
[CacheIndex
], (VOID
*)CapsuleTable
);
226 ASSERT_EFI_ERROR (Status
);
232 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
233 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
234 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
235 // type capsule transaction could be extended. It depends on platform policy.
237 for (Index
= 0; Index
< CapsuleTotalNumber
; Index
++) {
238 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) CapsulePtr
[Index
];
239 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
241 // Call capsule library to process capsule image.
243 ProcessCapsuleImage (CapsuleHeader
);
247 PlatformBdsLockNonUpdatableFlash ();
250 // Free the allocated temp memory space.
252 FreePool (CapsuleGuidCache
);
253 FreePool (CapsulePtrCache
);
254 FreePool (CapsulePtr
);