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