]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/BdsDxe/Capsules.c
IntelFrameworkModulePkg: Clean up source files
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / Capsules.c
CommitLineData
5c08e117 1/** @file\r
2 BDS routines to handle capsules.\r
3\r
0a6f4824 4Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
180a5a35 5This program and the accompanying materials\r
5c08e117 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
5c08e117 16/**\r
17\r
18 This routine is called to see if there are any capsules we need to process.\r
19 If the boot mode is not UPDATE, then we do nothing. Otherwise find the\r
20 capsule HOBS and produce firmware volumes for them via the DXE service.\r
21 Then call the dispatcher to dispatch drivers from them. Finally, check\r
22 the status of the updates.\r
23\r
24 This function should be called by BDS in case we need to do some\r
25 sort of processing even if there is no capsule to process. We\r
26 need to do this if an earlier update went away and we need to\r
27 clear the capsule variable so on the next reset PEI does not see it and\r
28 think there is a capsule available.\r
29\r
30 @param BootMode the current boot mode\r
31\r
32 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update\r
33 @retval EFI_SUCCESS There is no error when processing capsule\r
34\r
35**/\r
36EFI_STATUS\r
8861fc79 37EFIAPI\r
19bf20e1 38BdsProcessCapsules (\r
5c08e117 39 EFI_BOOT_MODE BootMode\r
40 )\r
41{\r
42 EFI_STATUS Status;\r
43 EFI_PEI_HOB_POINTERS HobPointer;\r
44 EFI_CAPSULE_HEADER *CapsuleHeader;\r
45 UINT32 Size;\r
46 UINT32 CapsuleNumber;\r
47 UINT32 CapsuleTotalNumber;\r
48 EFI_CAPSULE_TABLE *CapsuleTable;\r
49 UINT32 Index;\r
50 UINT32 CacheIndex;\r
51 UINT32 CacheNumber;\r
52 VOID **CapsulePtr;\r
53 VOID **CapsulePtrCache;\r
0a6f4824 54 EFI_GUID *CapsuleGuidCache;\r
566771b0 55 BOOLEAN NeedReset;\r
5c08e117 56\r
566771b0 57 CapsuleNumber = 0;\r
5c08e117 58 CapsuleTotalNumber = 0;\r
566771b0 59 CacheIndex = 0;\r
60 CacheNumber = 0;\r
61 CapsulePtr = NULL;\r
62 CapsulePtrCache = NULL;\r
63 CapsuleGuidCache = NULL;\r
64 NeedReset = FALSE;\r
5c08e117 65\r
66 //\r
67 // We don't do anything else if the boot mode is not flash-update\r
68 //\r
69 if (BootMode != BOOT_ON_FLASH_UPDATE) {\r
5025be3f 70 DEBUG ((EFI_D_ERROR, "Boot mode is not correct for capsule update.\n"));\r
5c08e117 71 return EFI_INVALID_PARAMETER;\r
72 }\r
0a6f4824 73\r
5c08e117 74 Status = EFI_SUCCESS;\r
75 //\r
76 // Find all capsule images from hob\r
77 //\r
78 HobPointer.Raw = GetHobList ();\r
019dfb9c 79 while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {\r
5c08e117 80 CapsuleTotalNumber ++;\r
5c08e117 81 HobPointer.Raw = GET_NEXT_HOB (HobPointer);\r
82 }\r
0a6f4824 83\r
5c08e117 84 if (CapsuleTotalNumber == 0) {\r
85 //\r
86 // We didn't find a hob, so had no errors.\r
87 //\r
5025be3f 88 DEBUG ((EFI_D_ERROR, "We can not find capsule data in capsule update boot mode.\n"));\r
89 DEBUG ((EFI_D_ERROR, "Please check the followings are correct if unexpected capsule update error happens.\n"));\r
90 DEBUG ((EFI_D_ERROR, "1. CapsuleX64 is built as X64 module when PEI is IA32 and DXE is X64\n"));\r
91 DEBUG ((EFI_D_ERROR, "2. Capsule data should persist in memory across a system reset.\n"));\r
5c08e117 92 PlatformBdsLockNonUpdatableFlash ();\r
93 return EFI_SUCCESS;\r
94 }\r
0a6f4824 95\r
5c08e117 96 //\r
97 // Init temp Capsule Data table.\r
98 //\r
99 CapsulePtr = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);\r
100 ASSERT (CapsulePtr != NULL);\r
101 CapsulePtrCache = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);\r
102 ASSERT (CapsulePtrCache != NULL);\r
103 CapsuleGuidCache = (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) * CapsuleTotalNumber);\r
104 ASSERT (CapsuleGuidCache != NULL);\r
0a6f4824 105\r
5c08e117 106 //\r
107 // Find all capsule images from hob\r
108 //\r
109 HobPointer.Raw = GetHobList ();\r
019dfb9c 110 while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {\r
111 CapsulePtr [CapsuleNumber++] = (VOID *) (UINTN) HobPointer.Capsule->BaseAddress;\r
5c08e117 112 HobPointer.Raw = GET_NEXT_HOB (HobPointer);\r
113 }\r
114\r
115 //\r
116 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install\r
117 //capsuleTable to configure table with EFI_CAPSULE_GUID\r
118 //\r
119\r
120 //\r
121 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating\r
0a6f4824 122 // System to have information persist across a system reset. EFI System Table must\r
5c08e117 123 // point to an array of capsules that contains the same CapsuleGuid value. And agents\r
0a6f4824 124 // searching for this type capsule will look in EFI System Table and search for the\r
5c08e117 125 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes\r
0a6f4824
LG
126 // how to sorting the capsules by the unique guid and install the array to EFI System Table.\r
127 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an\r
5c08e117 128 // array for later sorting capsules by CapsuleGuid.\r
129 //\r
130 for (Index = 0; Index < CapsuleTotalNumber; Index++) {\r
131 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];\r
132 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {\r
133 //\r
134 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.\r
0a6f4824 135 // If already has the Guid, skip it. Whereas, record it in the CacheArray as\r
5c08e117 136 // an additional one.\r
137 //\r
138 CacheIndex = 0;\r
139 while (CacheIndex < CacheNumber) {\r
140 if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->CapsuleGuid)) {\r
141 break;\r
142 }\r
143 CacheIndex++;\r
144 }\r
145 if (CacheIndex == CacheNumber) {\r
146 CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader->CapsuleGuid,sizeof(EFI_GUID));\r
147 }\r
148 }\r
149 }\r
150\r
151 //\r
152 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules\r
153 // whose guid is the same as it, and malloc memory for an array which preceding\r
154 // with UINT32. The array fills with entry point of capsules that have the same\r
155 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install\r
156 // this array into EFI System Table, so that agents searching for this type capsule\r
157 // will look in EFI System Table and search for the capsule's Guid and associated\r
158 // pointer to retrieve the data.\r
159 //\r
160 CacheIndex = 0;\r
161 while (CacheIndex < CacheNumber) {\r
0a6f4824 162 CapsuleNumber = 0;\r
5c08e117 163 for (Index = 0; Index < CapsuleTotalNumber; Index++) {\r
164 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];\r
165 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {\r
166 if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {\r
167 //\r
168 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.\r
169 //\r
170 CapsulePtrCache[CapsuleNumber++] = (VOID*)CapsuleHeader;\r
171 }\r
172 }\r
173 }\r
174 if (CapsuleNumber != 0) {\r
0a6f4824 175 Size = sizeof(EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof(VOID*);\r
5c08e117 176 CapsuleTable = AllocateRuntimePool (Size);\r
177 ASSERT (CapsuleTable != NULL);\r
178 CapsuleTable->CapsuleArrayNumber = CapsuleNumber;\r
179 CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber * sizeof(VOID*));\r
180 Status = gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIndex], (VOID*)CapsuleTable);\r
181 ASSERT_EFI_ERROR (Status);\r
182 }\r
183 CacheIndex++;\r
184 }\r
185\r
186 //\r
187 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are\r
0a6f4824 188 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash\r
5c08e117 189 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional\r
190 // type capsule transaction could be extended. It depends on platform policy.\r
191 //\r
192 for (Index = 0; Index < CapsuleTotalNumber; Index++) {\r
193 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];\r
194 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
566771b0 195 //\r
196 // Always reset system after all capsule processed if FMP capsule exist\r
197 //\r
198 if (CompareGuid (&gEfiFmpCapsuleGuid, &CapsuleHeader->CapsuleGuid)){\r
199 NeedReset = TRUE;\r
200 }\r
201\r
5c08e117 202 //\r
203 // Call capsule library to process capsule image.\r
204 //\r
205 ProcessCapsuleImage (CapsuleHeader);\r
206 }\r
207 }\r
208\r
566771b0 209 if (NeedReset) {\r
210 Print(L"Capsule Request Cold Reboot.\n");\r
211\r
212 for (Index = 5; Index > 0; Index--) {\r
213 Print(L"\rResetting system in %d seconds ...", Index);\r
214 gBS->Stall (1000000);\r
215 }\r
216\r
217 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
218\r
219 CpuDeadLoop ();\r
220 }\r
221\r
5c08e117 222 PlatformBdsLockNonUpdatableFlash ();\r
0a6f4824 223\r
5c08e117 224 //\r
225 // Free the allocated temp memory space.\r
226 //\r
227 FreePool (CapsuleGuidCache);\r
228 FreePool (CapsulePtrCache);\r
229 FreePool (CapsulePtr);\r
230\r
231 return Status;\r
232}\r
019dfb9c 233\r