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