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