]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg: Add assertion to make code easier for read.
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmMisc.c
CommitLineData
067ed98a
RN
1/** @file\r
2 Misc library functions.\r
3\r
4Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>\r
5This 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\r
15#include "InternalBm.h"\r
16\r
17/**\r
18 Delete the instance in Multi which matches partly with Single instance\r
19\r
20 @param Multi A pointer to a multi-instance device path data\r
21 structure.\r
22 @param Single A pointer to a single-instance device path data\r
23 structure.\r
24\r
25 @return This function will remove the device path instances in Multi which partly\r
26 match with the Single, and return the result device path. If there is no\r
27 remaining device path as a result, this function will return NULL.\r
28\r
29**/\r
30EFI_DEVICE_PATH_PROTOCOL *\r
31BmDelPartMatchInstance (\r
32 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
33 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
34 )\r
35{\r
36 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
37 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
38 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
39 UINTN InstanceSize;\r
40 UINTN SingleDpSize;\r
41\r
42 NewDevicePath = NULL;\r
43 TempNewDevicePath = NULL;\r
44\r
45 if (Multi == NULL || Single == NULL) {\r
46 return Multi;\r
47 }\r
48\r
49 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
50 SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r
51 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
52\r
53 while (Instance != NULL) {\r
54\r
55 if (CompareMem (Instance, Single, MIN (SingleDpSize, InstanceSize)) != 0) {\r
56 //\r
57 // Append the device path instance which does not match with Single\r
58 //\r
59 TempNewDevicePath = NewDevicePath;\r
60 NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r
61 if (TempNewDevicePath != NULL) {\r
62 FreePool(TempNewDevicePath);\r
63 }\r
64 }\r
65 FreePool(Instance);\r
66 Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r
67 InstanceSize -= END_DEVICE_PATH_LENGTH;\r
68 }\r
69\r
70 return NewDevicePath;\r
71}\r
72\r
73/**\r
74 Function compares a device path data structure to that of all the nodes of a\r
75 second device path instance.\r
76\r
77 @param Multi A pointer to a multi-instance device path data\r
78 structure.\r
79 @param Single A pointer to a single-instance device path data\r
80 structure.\r
81\r
82 @retval TRUE If the Single device path is contained within Multi device path.\r
83 @retval FALSE The Single device path is not match within Multi device path.\r
84\r
85**/\r
86BOOLEAN\r
87BmMatchDevicePaths (\r
88 IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r
89 IN EFI_DEVICE_PATH_PROTOCOL *Single\r
90 )\r
91{\r
92 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
93 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r
94 UINTN Size;\r
95\r
96 if (Multi == NULL || Single == NULL) {\r
97 return FALSE;\r
98 }\r
99\r
100 DevicePath = Multi;\r
101 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
102\r
103 //\r
104 // Search for the match of 'Single' in 'Multi'\r
105 //\r
106 while (DevicePathInst != NULL) {\r
107 //\r
108 // If the single device path is found in multiple device paths,\r
109 // return success\r
110 //\r
111 if (CompareMem (Single, DevicePathInst, Size) == 0) {\r
112 FreePool (DevicePathInst);\r
113 return TRUE;\r
114 }\r
115\r
116 FreePool (DevicePathInst);\r
117 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r
118 }\r
119\r
120 return FALSE;\r
121}\r
122\r
123/**\r
124 This routine adjust the memory information for different memory type and \r
125 save them into the variables for next boot.\r
126**/\r
127VOID\r
128BmSetMemoryTypeInformationVariable (\r
129 VOID\r
130 )\r
131{\r
132 EFI_STATUS Status;\r
133 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
134 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
135 UINTN VariableSize;\r
136 UINTN Index;\r
137 UINTN Index1;\r
138 UINT32 Previous;\r
139 UINT32 Current;\r
140 UINT32 Next;\r
141 EFI_HOB_GUID_TYPE *GuidHob;\r
142 BOOLEAN MemoryTypeInformationModified;\r
143 BOOLEAN MemoryTypeInformationVariableExists;\r
144 EFI_BOOT_MODE BootMode;\r
145\r
146 MemoryTypeInformationModified = FALSE;\r
147 MemoryTypeInformationVariableExists = FALSE;\r
148\r
149\r
150 BootMode = GetBootModeHob ();\r
151 //\r
152 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
153 //\r
154 if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
155 return;\r
156 }\r
157\r
158 //\r
159 // Only check the the Memory Type Information variable in the boot mode \r
160 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
161 // Information is not valid in this boot mode.\r
162 //\r
163 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
164 VariableSize = 0;\r
165 Status = gRT->GetVariable (\r
166 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
167 &gEfiMemoryTypeInformationGuid,\r
168 NULL, \r
169 &VariableSize, \r
170 NULL\r
171 );\r
172 if (Status == EFI_BUFFER_TOO_SMALL) {\r
173 MemoryTypeInformationVariableExists = TRUE;\r
174 }\r
175 }\r
176\r
177 //\r
178 // Retrieve the current memory usage statistics. If they are not found, then\r
179 // no adjustments can be made to the Memory Type Information variable.\r
180 //\r
181 Status = EfiGetSystemConfigurationTable (\r
182 &gEfiMemoryTypeInformationGuid,\r
183 (VOID **) &CurrentMemoryTypeInformation\r
184 );\r
185 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {\r
186 return;\r
187 }\r
188\r
189 //\r
190 // Get the Memory Type Information settings from Hob if they exist,\r
191 // PEI is responsible for getting them from variable and build a Hob to save them.\r
192 // If the previous Memory Type Information is not available, then set defaults\r
193 //\r
194 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
195 if (GuidHob == NULL) {\r
196 //\r
197 // If Platform has not built Memory Type Info into the Hob, just return.\r
198 //\r
199 return;\r
200 }\r
201 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
202 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
203\r
204 //\r
205 // Use a heuristic to adjust the Memory Type Information for the next boot\r
206 //\r
207 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
208 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
209 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
210\r
211 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
212\r
213 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
214 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
215 break;\r
216 }\r
217 }\r
218 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
219 continue;\r
220 }\r
221\r
222 //\r
223 // Previous is the number of pages pre-allocated\r
224 // Current is the number of pages actually needed\r
225 //\r
226 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
227 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
228 Next = Previous;\r
229\r
230 //\r
231 // Inconsistent Memory Reserved across bootings may lead to S4 fail\r
232 // Write next varible to 125% * current when the pre-allocated memory is:\r
233 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING\r
234 // 2. Less than the needed memory\r
235 //\r
236 if ((Current + (Current >> 1)) < Previous) {\r
237 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
238 Next = Current + (Current >> 2);\r
239 }\r
240 } else if (Current > Previous) {\r
241 Next = Current + (Current >> 2);\r
242 }\r
243 if (Next > 0 && Next < 4) {\r
244 Next = 4;\r
245 }\r
246\r
247 if (Next != Previous) {\r
248 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
249 MemoryTypeInformationModified = TRUE;\r
250 }\r
251\r
252 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
253 }\r
254\r
255 //\r
256 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
257 // Or create the variable in first boot.\r
258 //\r
259 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
260 Status = BmSetVariableAndReportStatusCodeOnError (\r
261 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
262 &gEfiMemoryTypeInformationGuid,\r
263 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
264 VariableSize,\r
265 PreviousMemoryTypeInformation\r
266 );\r
267\r
268 if (!EFI_ERROR (Status)) {\r
269 //\r
270 // If the Memory Type Information settings have been modified, then reset the platform\r
271 // so the new Memory Type Information setting will be used to guarantee that an S4\r
272 // entry/resume cycle will not fail.\r
273 //\r
274 if (MemoryTypeInformationModified) {\r
275 DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
276 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
277 }\r
278 } else {\r
279 DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));\r
280 }\r
281 }\r
282}\r
283\r
284/**\r
285 Set the variable and report the error through status code upon failure.\r
286\r
287 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
288 Each VariableName is unique for each VendorGuid. VariableName must\r
289 contain 1 or more characters. If VariableName is an empty string,\r
290 then EFI_INVALID_PARAMETER is returned.\r
291 @param VendorGuid A unique identifier for the vendor.\r
292 @param Attributes Attributes bitmask to set for the variable.\r
293 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
294 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
295 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
296 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
297 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
298 the variable value (the timestamp associated with the variable may be updated however \r
299 even if no new data value is provided,see the description of the \r
300 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
301 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
302 @param Data The contents for the variable.\r
303\r
304 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
305 defined by the Attributes.\r
306 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
307 DataSize exceeds the maximum allowed.\r
308 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
309 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
310 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
311 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
312 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
313 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
314 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
315 does NOT pass the validation check carried out by the firmware.\r
316\r
317 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
318**/\r
319EFI_STATUS\r
320BmSetVariableAndReportStatusCodeOnError (\r
321 IN CHAR16 *VariableName,\r
322 IN EFI_GUID *VendorGuid,\r
323 IN UINT32 Attributes,\r
324 IN UINTN DataSize,\r
325 IN VOID *Data\r
326 )\r
327{\r
328 EFI_STATUS Status;\r
329 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
330 UINTN NameSize;\r
331\r
332 Status = gRT->SetVariable (\r
333 VariableName,\r
334 VendorGuid,\r
335 Attributes,\r
336 DataSize,\r
337 Data\r
338 );\r
339 if (EFI_ERROR (Status)) {\r
340 NameSize = StrSize (VariableName);\r
341 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
342 if (SetVariableStatus != NULL) {\r
343 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
344 SetVariableStatus->NameSize = NameSize;\r
345 SetVariableStatus->DataSize = DataSize;\r
346 SetVariableStatus->SetStatus = Status;\r
347 SetVariableStatus->Attributes = Attributes;\r
348 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
349 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
350\r
351 REPORT_STATUS_CODE_EX (\r
352 EFI_ERROR_CODE,\r
353 PcdGet32 (PcdErrorCodeSetVariable),\r
354 0,\r
355 NULL,\r
356 &gEdkiiStatusCodeDataTypeVariableGuid,\r
357 SetVariableStatus,\r
358 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
359 );\r
360\r
361 FreePool (SetVariableStatus);\r
362 }\r
363 }\r
364\r
365 return Status;\r
366}\r
367\r
368\r
369/**\r
370 Print the device path info.\r
371\r
372 @param DevicePath The device path need to print.\r
373**/\r
374VOID\r
375BmPrintDp (\r
376 EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
377 )\r
378{\r
379 CHAR16 *Str;\r
380\r
381 Str = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
382 DEBUG ((EFI_D_INFO, "%s", Str));\r
383 if (Str != NULL) {\r
384 FreePool (Str);\r
385 }\r
386}\r