]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/Capsules.c
Move BdsDxe and GenericBdsLib to IntelFrameworkModulePkg, these modules need dependen...
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / Capsules.c
1 /** @file
2 BDS routines to handle capsules.
3
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
9
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.
12
13 **/
14 #include "Bds.h"
15
16 /**
17 This function locks the block
18
19 @param CpuIo A instance of EFI_CPU_IO_PROTOCOL.
20 @param Base The base address flash region to be locked.
21
22 **/
23 VOID
24 BdsLockFv (
25 IN EFI_CPU_IO_PROTOCOL *CpuIo,
26 IN EFI_PHYSICAL_ADDRESS Base
27 )
28 {
29 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
30 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
31 EFI_PHYSICAL_ADDRESS BaseAddress;
32 UINT8 Data;
33 UINT32 BlockLength;
34 UINTN Index;
35
36 BaseAddress = Base - 0x400000 + 2;
37 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) (Base));
38 BlockMap = &(FvHeader->BlockMap[0]);
39
40 while ((BlockMap->NumBlocks != 0) && (BlockMap->Length != 0)) {
41 BlockLength = BlockMap->Length;
42 for (Index = 0; Index < BlockMap->NumBlocks; Index++) {
43 CpuIo->Mem.Read (
44 CpuIo,
45 EfiCpuIoWidthUint8,
46 BaseAddress,
47 1,
48 &Data
49 );
50 Data = (UINT8) (Data | 0x3);
51 CpuIo->Mem.Write (
52 CpuIo,
53 EfiCpuIoWidthUint8,
54 BaseAddress,
55 1,
56 &Data
57 );
58 BaseAddress += BlockLength;
59 }
60
61 BlockMap++;
62 }
63 }
64
65 /**
66
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.
72
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.
78
79 @param BootMode the current boot mode
80
81 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update
82 @retval EFI_SUCCESS There is no error when processing capsule
83
84 **/
85 EFI_STATUS
86 ProcessCapsules (
87 EFI_BOOT_MODE BootMode
88 )
89 {
90 EFI_STATUS Status;
91 EFI_PEI_HOB_POINTERS HobPointer;
92 EFI_CAPSULE_HEADER *CapsuleHeader;
93 UINT32 Size;
94 UINT32 CapsuleNumber;
95 UINT32 CapsuleTotalNumber;
96 EFI_CAPSULE_TABLE *CapsuleTable;
97 UINT32 Index;
98 UINT32 CacheIndex;
99 UINT32 CacheNumber;
100 VOID **CapsulePtr;
101 VOID **CapsulePtrCache;
102 EFI_GUID *CapsuleGuidCache;
103 CAPSULE_HOB_INFO *CapsuleHobInfo;
104
105 CapsuleNumber = 0;
106 CapsuleTotalNumber = 0;
107 CacheIndex = 0;
108 CacheNumber = 0;
109 CapsulePtr = NULL;
110 CapsulePtrCache = NULL;
111 CapsuleGuidCache = NULL;
112
113 //
114 // We don't do anything else if the boot mode is not flash-update
115 //
116 if (BootMode != BOOT_ON_FLASH_UPDATE) {
117 return EFI_INVALID_PARAMETER;
118 }
119
120 Status = EFI_SUCCESS;
121 //
122 // Find all capsule images from hob
123 //
124 HobPointer.Raw = GetHobList ();
125 while ((HobPointer.Raw = GetNextGuidHob (&gEfiCapsuleVendorGuid, HobPointer.Raw)) != NULL) {
126 CapsuleTotalNumber ++;
127
128 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
129 }
130
131 if (CapsuleTotalNumber == 0) {
132 //
133 // We didn't find a hob, so had no errors.
134 //
135 PlatformBdsLockNonUpdatableFlash ();
136 return EFI_SUCCESS;
137 }
138
139 //
140 // Init temp Capsule Data table.
141 //
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);
148
149 //
150 // Find all capsule images from hob
151 //
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);
156
157 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
158 }
159
160 //
161 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
162 //capsuleTable to configure table with EFI_CAPSULE_GUID
163 //
164
165 //
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.
174 //
175 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
176 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
177 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
178 //
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.
182 //
183 CacheIndex = 0;
184 while (CacheIndex < CacheNumber) {
185 if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->CapsuleGuid)) {
186 break;
187 }
188 CacheIndex++;
189 }
190 if (CacheIndex == CacheNumber) {
191 CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader->CapsuleGuid,sizeof(EFI_GUID));
192 }
193 }
194 }
195
196 //
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.
204 //
205 CacheIndex = 0;
206 while (CacheIndex < CacheNumber) {
207 CapsuleNumber = 0;
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)) {
212 //
213 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
214 //
215 CapsulePtrCache[CapsuleNumber++] = (VOID*)CapsuleHeader;
216 }
217 }
218 }
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);
227 }
228 CacheIndex++;
229 }
230
231 //
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.
236 //
237 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
238 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
239 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
240 //
241 // Call capsule library to process capsule image.
242 //
243 ProcessCapsuleImage (CapsuleHeader);
244 }
245 }
246
247 PlatformBdsLockNonUpdatableFlash ();
248
249 //
250 // Free the allocated temp memory space.
251 //
252 FreePool (CapsuleGuidCache);
253 FreePool (CapsulePtrCache);
254 FreePool (CapsulePtr);
255
256 return Status;
257 }