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