]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/BdsDxe/Capsules.c
sync comments, fix function header, rename variable name to follow coding style.
[mirror_edk2.git] / MdeModulePkg / Universal / BdsDxe / Capsules.c
1 /** @file
2 BDS routines to handle capsules.
3
4 Copyright (c) 2004 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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 This function locks the block
18
19 @param CpuIo A instance of EFI_CPU_IO_PROTOCOL.
20 @param Base The base address flash region to be locked.
21
22 **/
23 VOID
24 BdsLockFv (
25 IN EFI_CPU_IO_PROTOCOL *CpuIo,
26 IN EFI_PHYSICAL_ADDRESS Base
27 )
28 {
29 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
30 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
31 EFI_PHYSICAL_ADDRESS BaseAddress;
32 UINT8 Data;
33 UINT32 BlockLength;
34 UINTN Index;
35
36 BaseAddress = Base - 0x400000 + 2;
37 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) (Base));
38 BlockMap = &(FvHeader->BlockMap[0]);
39
40 while ((BlockMap->NumBlocks != 0) && (BlockMap->Length != 0)) {
41 BlockLength = BlockMap->Length;
42 for (Index = 0; Index < BlockMap->NumBlocks; Index++) {
43 CpuIo->Mem.Read (
44 CpuIo,
45 EfiCpuIoWidthUint8,
46 BaseAddress,
47 1,
48 &Data
49 );
50 Data = (UINT8) (Data | 0x3);
51 CpuIo->Mem.Write (
52 CpuIo,
53 EfiCpuIoWidthUint8,
54 BaseAddress,
55 1,
56 &Data
57 );
58 BaseAddress += BlockLength;
59 }
60
61 BlockMap++;
62 }
63 }
64
65 /**
66
67 This routine is called to see if there are any capsules we need to process.
68 If the boot mode is not UPDATE, then we do nothing. Otherwise find the
69 capsule HOBS and produce firmware volumes for them via the DXE service.
70 Then call the dispatcher to dispatch drivers from them. Finally, check
71 the status of the updates.
72
73
74 @param BootMode - the current boot mode
75
76 @retval EFI_INVALID_PARAMETER boot mode is not correct for an update
77 Note:
78 This function should be called by BDS in case we need to do some
79 sort of processing even if there is no capsule to process. We
80 need to do this if an earlier update went awry and we need to
81 clear the capsule variable so on the next reset PEI does not see it and
82 think there is a capsule available.
83 @retval EFI_SUCCESS There is no error when processing capsule
84
85 **/
86 EFI_STATUS
87 ProcessCapsules (
88 EFI_BOOT_MODE BootMode
89 )
90 {
91 EFI_STATUS Status;
92 EFI_PEI_HOB_POINTERS HobPointer;
93 EFI_CAPSULE_HEADER *CapsuleHeader;
94 UINT32 Size;
95 UINT32 CapsuleNumber;
96 UINT32 CapsuleTotalNumber;
97 EFI_CAPSULE_TABLE *CapsuleTable;
98 UINT32 Index;
99 UINT32 CacheIndex;
100 UINT32 CacheNumber;
101 VOID **CapsulePtr;
102 VOID **CapsulePtrCache;
103 EFI_GUID *CapsuleGuidCache;
104 CAPSULE_HOB_INFO *CapsuleHobInfo;
105
106 CapsuleNumber = 0;
107 CapsuleTotalNumber = 0;
108 CacheIndex = 0;
109 CacheNumber = 0;
110 CapsulePtr = NULL;
111 CapsulePtrCache = NULL;
112 CapsuleGuidCache = NULL;
113
114 //
115 // We don't do anything else if the boot mode is not flash-update
116 //
117 if (BootMode != BOOT_ON_FLASH_UPDATE) {
118 return EFI_INVALID_PARAMETER;
119 }
120
121 Status = EFI_SUCCESS;
122 //
123 // Find all capsule images from hob
124 //
125 HobPointer.Raw = GetHobList ();
126 while ((HobPointer.Raw = GetNextGuidHob (&gEfiCapsuleVendorGuid, HobPointer.Raw)) != NULL) {
127 CapsuleTotalNumber ++;
128
129 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
130 }
131
132 if (CapsuleTotalNumber == 0) {
133 //
134 // We didn't find a hob, so had no errors.
135 //
136 PlatformBdsLockNonUpdatableFlash ();
137 return EFI_SUCCESS;
138 }
139
140 //
141 // Init temp Capsule Data table.
142 //
143 CapsulePtr = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);
144 ASSERT (CapsulePtr != NULL);
145 CapsulePtrCache = (VOID **) AllocateZeroPool (sizeof (VOID *) * CapsuleTotalNumber);
146 ASSERT (CapsulePtrCache != NULL);
147 CapsuleGuidCache = (EFI_GUID *) AllocateZeroPool (sizeof (EFI_GUID) * CapsuleTotalNumber);
148 ASSERT (CapsuleGuidCache != NULL);
149
150 //
151 // Find all capsule images from hob
152 //
153 HobPointer.Raw = GetHobList ();
154 while ((HobPointer.Raw = GetNextGuidHob (&gEfiCapsuleVendorGuid, HobPointer.Raw)) != NULL) {
155 CapsuleHobInfo = GET_GUID_HOB_DATA (HobPointer.Guid);
156 CapsulePtr [CapsuleNumber++] = (VOID *)(UINTN)(CapsuleHobInfo->BaseAddress);
157
158 HobPointer.Raw = GET_NEXT_HOB (HobPointer);
159 }
160
161 //
162 //Check the capsule flags,if contains CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE, install
163 //capsuleTable to configure table with EFI_CAPSULE_GUID
164 //
165
166 //
167 // Capsules who have CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE always are used for operating
168 // System to have information persist across a system reset. EFI System Table must
169 // point to an array of capsules that contains the same CapsuleGuid value. And agents
170 // searching for this type capsule will look in EFI System Table and search for the
171 // capsule's Guid and associated pointer to retrieve the data. Two steps below describes
172 // how to sorting the capsules by the unique guid and install the array to EFI System Table.
173 // Firstly, Loop for all coalesced capsules, record unique CapsuleGuids and cache them in an
174 // array for later sorting capsules by CapsuleGuid.
175 //
176 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
177 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
178 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
179 //
180 // For each capsule, we compare it with known CapsuleGuid in the CacheArray.
181 // If already has the Guid, skip it. Whereas, record it in the CacheArray as
182 // an additional one.
183 //
184 CacheIndex = 0;
185 while (CacheIndex < CacheNumber) {
186 if (CompareGuid(&CapsuleGuidCache[CacheIndex],&CapsuleHeader->CapsuleGuid)) {
187 break;
188 }
189 CacheIndex++;
190 }
191 if (CacheIndex == CacheNumber) {
192 CopyMem(&CapsuleGuidCache[CacheNumber++],&CapsuleHeader->CapsuleGuid,sizeof(EFI_GUID));
193 }
194 }
195 }
196
197 //
198 // Secondly, for each unique CapsuleGuid in CacheArray, gather all coalesced capsules
199 // whose guid is the same as it, and malloc memory for an array which preceding
200 // with UINT32. The array fills with entry point of capsules that have the same
201 // CapsuleGuid, and UINT32 represents the size of the array of capsules. Then install
202 // this array into EFI System Table, so that agents searching for this type capsule
203 // will look in EFI System Table and search for the capsule's Guid and associated
204 // pointer to retrieve the data.
205 //
206 CacheIndex = 0;
207 while (CacheIndex < CacheNumber) {
208 CapsuleNumber = 0;
209 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
210 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
211 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
212 if (CompareGuid (&CapsuleGuidCache[CacheIndex], &CapsuleHeader->CapsuleGuid)) {
213 //
214 // Cache Caspuleheader to the array, this array is uniqued with certain CapsuleGuid.
215 //
216 CapsulePtrCache[CapsuleNumber++] = (VOID*)CapsuleHeader;
217 }
218 }
219 }
220 if (CapsuleNumber != 0) {
221 Size = sizeof(EFI_CAPSULE_TABLE) + (CapsuleNumber - 1) * sizeof(VOID*);
222 CapsuleTable = AllocateRuntimePool (Size);
223 ASSERT (CapsuleTable != NULL);
224 CapsuleTable->CapsuleArrayNumber = CapsuleNumber;
225 CopyMem(&CapsuleTable->CapsulePtr[0], CapsulePtrCache, CapsuleNumber * sizeof(VOID*));
226 Status = gBS->InstallConfigurationTable (&CapsuleGuidCache[CacheIndex], (VOID*)CapsuleTable);
227 ASSERT_EFI_ERROR (Status);
228 }
229 CacheIndex++;
230 }
231
232 //
233 // Besides ones with CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag, all capsules left are
234 // recognized by platform with CapsuleGuid. For general platform driver, UpdateFlash
235 // type is commonly supported, so here only deal with encapsuled FVs capsule. Additional
236 // type capsule transaction could be extended. It depends on platform policy.
237 //
238 for (Index = 0; Index < CapsuleTotalNumber; Index++) {
239 CapsuleHeader = (EFI_CAPSULE_HEADER*) CapsulePtr [Index];
240 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
241 //
242 // Call capsule library to process capsule image.
243 //
244 ProcessCapsuleImage (CapsuleHeader);
245 }
246 }
247
248 PlatformBdsLockNonUpdatableFlash ();
249
250 //
251 // Free the allocated temp memory space.
252 //
253 FreePool (CapsuleGuidCache);
254 FreePool (CapsulePtrCache);
255 FreePool (CapsulePtr);
256
257 return Status;
258 }