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