]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/Capsules.c
Correct a typo: Change the type of the 4th parameter of EFI_DRIVER_HEALTH_PROTOCOL...
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / Capsules.c
1 /** @file
2 BDS routines to handle capsules.
3
4 Copyright (c) 2004 - 2011, 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
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
56 CapsuleNumber = 0;
57 CapsuleTotalNumber = 0;
58 CacheIndex = 0;
59 CacheNumber = 0;
60 CapsulePtr = NULL;
61 CapsulePtrCache = NULL;
62 CapsuleGuidCache = NULL;
63
64 //
65 // We don't do anything else if the boot mode is not flash-update
66 //
67 if (BootMode != BOOT_ON_FLASH_UPDATE) {
68 DEBUG ((EFI_D_ERROR, "Boot mode is not correct for capsule update.\n"));
69 return EFI_INVALID_PARAMETER;
70 }
71
72 Status = EFI_SUCCESS;
73 //
74 // Find all capsule images from hob
75 //
76 HobPointer.Raw = GetHobList ();
77 while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {
78 CapsuleTotalNumber ++;
79 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
80 }
81
82 if (CapsuleTotalNumber == 0) {
83 //
84 // We didn't find a hob, so had no errors.
85 //
86 DEBUG ((EFI_D_ERROR, "We can not find capsule data in capsule update boot mode.\n"));
87 DEBUG ((EFI_D_ERROR, "Please check the followings are correct if unexpected capsule update error happens.\n"));
88 DEBUG ((EFI_D_ERROR, "1. CapsuleX64 is built as X64 module when PEI is IA32 and DXE is X64\n"));
89 DEBUG ((EFI_D_ERROR, "2. Capsule data should persist in memory across a system reset.\n"));
90 PlatformBdsLockNonUpdatableFlash ();
91 return EFI_SUCCESS;
92 }
93
94 //
95 // Init temp Capsule Data table.
96 //
97 CapsulePtr = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);
98 ASSERT (CapsulePtr != NULL);
99 CapsulePtrCache = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);
100 ASSERT (CapsulePtrCache != NULL);
101 CapsuleGuidCache = (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) * CapsuleTotalNumber);
102 ASSERT (CapsuleGuidCache != NULL);
103
104 //
105 // Find all capsule images from hob
106 //
107 HobPointer.Raw = GetHobList ();
108 while ((HobPointer.Raw = GetNextHob (EFI_HOB_TYPE_UEFI_CAPSULE, HobPointer.Raw)) != NULL) {
109 CapsulePtr [CapsuleNumber++] = (VOID *) (UINTN) HobPointer.Capsule->BaseAddress;
110 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
111 }
112
113 //
114 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
115 //capsuleTable to configure table with EFI_CAPSULE_GUID
116 //
117
118 //
119 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
120 // System to have information persist across a system reset. EFI System Table must
121 // point to an array of capsules that contains the same CapsuleGuid value. And agents
122 // searching for this type capsule will look in EFI System Table and search for the
123 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
124 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
125 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
126 // array for later sorting capsules by CapsuleGuid.
127 //
128 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
129 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
130 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
131 //
132 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
133 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
134 // an additional one.
135 //
136 CacheIndex = 0;
137 while (CacheIndex < CacheNumber) {
138 if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->CapsuleGuid)) {
139 break;
140 }
141 CacheIndex++;
142 }
143 if (CacheIndex == CacheNumber) {
144 CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader->CapsuleGuid,sizeof(EFI_GUID));
145 }
146 }
147 }
148
149 //
150 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
151 // whose guid is the same as it, and malloc memory for an array which preceding
152 // with UINT32. The array fills with entry point of capsules that have the same
153 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
154 // this array into EFI System Table, so that agents searching for this type capsule
155 // will look in EFI System Table and search for the capsule's Guid and associated
156 // pointer to retrieve the data.
157 //
158 CacheIndex = 0;
159 while (CacheIndex < CacheNumber) {
160 CapsuleNumber = 0;
161 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
162 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
163 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
164 if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {
165 //
166 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
167 //
168 CapsulePtrCache[CapsuleNumber++] = (VOID*)CapsuleHeader;
169 }
170 }
171 }
172 if (CapsuleNumber != 0) {
173 Size = sizeof(EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof(VOID*);
174 CapsuleTable = AllocateRuntimePool (Size);
175 ASSERT (CapsuleTable != NULL);
176 CapsuleTable->CapsuleArrayNumber = CapsuleNumber;
177 CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber * sizeof(VOID*));
178 Status = gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIndex], (VOID*)CapsuleTable);
179 ASSERT_EFI_ERROR (Status);
180 }
181 CacheIndex++;
182 }
183
184 //
185 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
186 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
187 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
188 // type capsule transaction could be extended. It depends on platform policy.
189 //
190 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
191 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
192 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
193 //
194 // Call capsule library to process capsule image.
195 //
196 ProcessCapsuleImage (CapsuleHeader);
197 }
198 }
199
200 PlatformBdsLockNonUpdatableFlash ();
201
202 //
203 // Free the allocated temp memory space.
204 //
205 FreePool (CapsuleGuidCache);
206 FreePool (CapsulePtrCache);
207 FreePool (CapsulePtr);
208
209 return Status;
210 }
211