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