]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/BdsDxe/Capsules.c
Clean up BdsDxe for Doxygen comments requirement.
[mirror_edk2.git] / MdeModulePkg / 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 @return VOID No return.
23
24 **/
25 VOID
26 BdsLockFv (
27 IN EFI_CPU_IO_PROTOCOL *CpuIo,
28 IN EFI_PHYSICAL_ADDRESS Base
29 )
30 {
31 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
32 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
33 EFI_PHYSICAL_ADDRESS BaseAddress;
34 UINT8 Data;
35 UINT32 BlockLength;
36 UINTN Index;
37
38 BaseAddress = Base - 0x400000 + 2;
39 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) (Base));
40 BlockMap = &(FvHeader->BlockMap[0]);
41
42 while ((BlockMap->NumBlocks != 0) && (BlockMap->Length != 0)) {
43 BlockLength = BlockMap->Length;
44 for (Index = 0; Index < BlockMap->NumBlocks; Index++) {
45 CpuIo->Mem.Read (
46 CpuIo,
47 EfiCpuIoWidthUint8,
48 BaseAddress,
49 1,
50 &Data
51 );
52 Data = (UINT8) (Data | 0x3);
53 CpuIo->Mem.Write (
54 CpuIo,
55 EfiCpuIoWidthUint8,
56 BaseAddress,
57 1,
58 &Data
59 );
60 BaseAddress += BlockLength;
61 }
62
63 BlockMap++;
64 }
65 }
66
67 /**
68
69 This routine is called to see if there are any capsules we need to process.
70 If the boot mode is not UPDATE, then we do nothing. Otherwise find the
71 capsule HOBS and produce firmware volumes for them via the DXE service.
72 Then call the dispatcher to dispatch drivers from them. Finally, check
73 the status of the updates.
74
75
76 @param BootMode - the current boot mode
77
78 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update
79 Note:
80 This function should be called by BDS in case we need to do some
81 sort of processing even if there is no capsule to process. We
82 need to do this if an earlier update went awry and we need to
83 clear the capsule variable so on the next reset PEI does not see it and
84 think there is a capsule available.
85
86 **/
87 EFI_STATUS
88 ProcessCapsules (
89 EFI_BOOT_MODE BootMode
90 )
91 {
92 EFI_STATUS Status;
93 EFI_PEI_HOB_POINTERS HobPointer;
94 EFI_CAPSULE_HEADER *CapsuleHeader;
95 UINT32 Size;
96 UINT32 CapsuleNumber;
97 UINT32 CapsuleTotalNumber;
98 EFI_CAPSULE_TABLE *CapsuleTable;
99 UINT32 Index;
100 UINT32 CacheIndex;
101 UINT32 CacheNumber;
102 VOID **CapsulePtr;
103 VOID **CapsulePtrCache;
104 EFI_GUID *CapsuleGuidCache;
105 CAPSULE_HOB_INFO *CapsuleHobInfo;
106
107 CapsuleNumber = 0;
108 CapsuleTotalNumber = 0;
109 CacheIndex = 0;
110 CacheNumber = 0;
111 CapsulePtr = NULL;
112 CapsulePtrCache = NULL;
113 CapsuleGuidCache = NULL;
114
115 //
116 // We don't do anything else if the boot mode is not flash-update
117 //
118 if (BootMode != BOOT_ON_FLASH_UPDATE) {
119 return EFI_INVALID_PARAMETER;
120 }
121
122 Status = EFI_SUCCESS;
123 //
124 // Find all capsule images from hob
125 //
126 HobPointer.Raw = GetHobList ();
127 while ((HobPointer.Raw = GetNextGuidHob (&gEfiCapsuleVendorGuid, HobPointer.Raw)) != NULL) {
128 CapsuleTotalNumber ++;
129
130 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
131 }
132
133 if (CapsuleTotalNumber == 0) {
134 //
135 // We didn't find a hob, so had no errors.
136 //
137 PlatformBdsLockNonUpdatableFlash ();
138 return EFI_SUCCESS;
139 }
140
141 //
142 // Init temp Capsule Data table.
143 //
144 CapsulePtr = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);
145 ASSERT (CapsulePtr != NULL);
146 CapsulePtrCache = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);
147 ASSERT (CapsulePtrCache != NULL);
148 CapsuleGuidCache = (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) * CapsuleTotalNumber);
149 ASSERT (CapsuleGuidCache != NULL);
150
151 //
152 // Find all capsule images from hob
153 //
154 HobPointer.Raw = GetHobList ();
155 while ((HobPointer.Raw = GetNextGuidHob (&gEfiCapsuleVendorGuid, HobPointer.Raw)) != NULL) {
156 CapsuleHobInfo = GET_GUID_HOB_DATA (HobPointer.Guid);
157 CapsulePtr [CapsuleNumber++] = (VOID *)(UINTN)(CapsuleHobInfo->BaseAddress);
158
159 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
160 }
161
162 //
163 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
164 //capsuleTable to configure table with EFI_CAPSULE_GUID
165 //
166
167 //
168 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
169 // System to have information persist across a system reset. EFI System Table must
170 // point to an array of capsules that contains the same CapsuleGuid value. And agents
171 // searching for this type capsule will look in EFI System Table and search for the
172 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
173 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
174 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
175 // array for later sorting capsules by CapsuleGuid.
176 //
177 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
178 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
179 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
180 //
181 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
182 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
183 // an additional one.
184 //
185 CacheIndex = 0;
186 while (CacheIndex < CacheNumber) {
187 if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->CapsuleGuid)) {
188 break;
189 }
190 CacheIndex++;
191 }
192 if (CacheIndex == CacheNumber) {
193 CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader->CapsuleGuid,sizeof(EFI_GUID));
194 }
195 }
196 }
197
198 //
199 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
200 // whose guid is the same as it, and malloc memory for an array which preceding
201 // with UINT32. The array fills with entry point of capsules that have the same
202 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
203 // this array into EFI System Table, so that agents searching for this type capsule
204 // will look in EFI System Table and search for the capsule's Guid and associated
205 // pointer to retrieve the data.
206 //
207 CacheIndex = 0;
208 while (CacheIndex < CacheNumber) {
209 CapsuleNumber = 0;
210 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
211 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
212 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
213 if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {
214 //
215 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
216 //
217 CapsulePtrCache[CapsuleNumber++] = (VOID*)CapsuleHeader;
218 }
219 }
220 }
221 if (CapsuleNumber != 0) {
222 Size = sizeof(EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof(VOID*);
223 CapsuleTable = AllocateRuntimePool (Size);
224 ASSERT (CapsuleTable != NULL);
225 CapsuleTable->CapsuleArrayNumber = CapsuleNumber;
226 CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber * sizeof(VOID*));
227 Status = gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIndex], (VOID*)CapsuleTable);
228 ASSERT_EFI_ERROR (Status);
229 }
230 CacheIndex++;
231 }
232
233 //
234 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
235 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
236 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
237 // type capsule transaction could be extended. It depends on platform policy.
238 //
239 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
240 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
241 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
242 //
243 // Call capsule library to process capsule image.
244 //
245 ProcessCapsuleImage (CapsuleHeader);
246 }
247 }
248
249 PlatformBdsLockNonUpdatableFlash ();
250
251 //
252 // Free the allocated temp memory space.
253 //
254 FreePool (CapsuleGuidCache);
255 FreePool (CapsulePtrCache);
256 FreePool (CapsulePtr);
257
258 return Status;
259 }