]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
MdeModulePkg/Bds: BDS hotkey shouldn't work on inactive consoles
[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
665b26ba
RN
125 save them into the variables for next boot. It resets the system when\r
126 memory information is updated and the current boot option belongs to\r
127 boot category instead of application category.\r
128\r
129 @param Boot TRUE if current boot option belongs to boot category instead of\r
130 application category.\r
067ed98a
RN
131**/\r
132VOID\r
133BmSetMemoryTypeInformationVariable (\r
665b26ba 134 IN BOOLEAN Boot\r
067ed98a
RN
135 )\r
136{\r
137 EFI_STATUS Status;\r
138 EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r
139 EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r
140 UINTN VariableSize;\r
141 UINTN Index;\r
142 UINTN Index1;\r
143 UINT32 Previous;\r
144 UINT32 Current;\r
145 UINT32 Next;\r
146 EFI_HOB_GUID_TYPE *GuidHob;\r
147 BOOLEAN MemoryTypeInformationModified;\r
148 BOOLEAN MemoryTypeInformationVariableExists;\r
149 EFI_BOOT_MODE BootMode;\r
150\r
151 MemoryTypeInformationModified = FALSE;\r
152 MemoryTypeInformationVariableExists = FALSE;\r
153\r
154\r
155 BootMode = GetBootModeHob ();\r
156 //\r
157 // In BOOT_IN_RECOVERY_MODE, Variable region is not reliable.\r
158 //\r
159 if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
160 return;\r
161 }\r
162\r
163 //\r
164 // Only check the the Memory Type Information variable in the boot mode \r
165 // other than BOOT_WITH_DEFAULT_SETTINGS because the Memory Type\r
166 // Information is not valid in this boot mode.\r
167 //\r
168 if (BootMode != BOOT_WITH_DEFAULT_SETTINGS) {\r
169 VariableSize = 0;\r
170 Status = gRT->GetVariable (\r
171 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
172 &gEfiMemoryTypeInformationGuid,\r
173 NULL, \r
174 &VariableSize, \r
175 NULL\r
176 );\r
177 if (Status == EFI_BUFFER_TOO_SMALL) {\r
178 MemoryTypeInformationVariableExists = TRUE;\r
179 }\r
180 }\r
181\r
182 //\r
183 // Retrieve the current memory usage statistics. If they are not found, then\r
184 // no adjustments can be made to the Memory Type Information variable.\r
185 //\r
186 Status = EfiGetSystemConfigurationTable (\r
187 &gEfiMemoryTypeInformationGuid,\r
188 (VOID **) &CurrentMemoryTypeInformation\r
189 );\r
190 if (EFI_ERROR (Status) || CurrentMemoryTypeInformation == NULL) {\r
191 return;\r
192 }\r
193\r
194 //\r
195 // Get the Memory Type Information settings from Hob if they exist,\r
196 // PEI is responsible for getting them from variable and build a Hob to save them.\r
197 // If the previous Memory Type Information is not available, then set defaults\r
198 //\r
199 GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
200 if (GuidHob == NULL) {\r
201 //\r
202 // If Platform has not built Memory Type Info into the Hob, just return.\r
203 //\r
204 return;\r
205 }\r
206 PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
207 VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
208\r
209 //\r
210 // Use a heuristic to adjust the Memory Type Information for the next boot\r
211 //\r
212 DEBUG ((EFI_D_INFO, "Memory Previous Current Next \n"));\r
213 DEBUG ((EFI_D_INFO, " Type Pages Pages Pages \n"));\r
214 DEBUG ((EFI_D_INFO, "====== ======== ======== ========\n"));\r
215\r
216 for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
217\r
218 for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r
219 if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r
220 break;\r
221 }\r
222 }\r
223 if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r
224 continue;\r
225 }\r
226\r
227 //\r
228 // Previous is the number of pages pre-allocated\r
229 // Current is the number of pages actually needed\r
230 //\r
231 Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r
232 Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r
233 Next = Previous;\r
234\r
235 //\r
236 // Inconsistent Memory Reserved across bootings may lead to S4 fail\r
237 // Write next varible to 125% * current when the pre-allocated memory is:\r
238 // 1. More than 150% of needed memory and boot mode is BOOT_WITH_DEFAULT_SETTING\r
239 // 2. Less than the needed memory\r
240 //\r
241 if ((Current + (Current >> 1)) < Previous) {\r
242 if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {\r
243 Next = Current + (Current >> 2);\r
244 }\r
245 } else if (Current > Previous) {\r
246 Next = Current + (Current >> 2);\r
247 }\r
248 if (Next > 0 && Next < 4) {\r
249 Next = 4;\r
250 }\r
251\r
252 if (Next != Previous) {\r
253 PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r
254 MemoryTypeInformationModified = TRUE;\r
255 }\r
256\r
257 DEBUG ((EFI_D_INFO, " %02x %08x %08x %08x\n", PreviousMemoryTypeInformation[Index].Type, Previous, Current, Next));\r
258 }\r
259\r
260 //\r
261 // If any changes were made to the Memory Type Information settings, then set the new variable value;\r
262 // Or create the variable in first boot.\r
263 //\r
264 if (MemoryTypeInformationModified || !MemoryTypeInformationVariableExists) {\r
265 Status = BmSetVariableAndReportStatusCodeOnError (\r
266 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
267 &gEfiMemoryTypeInformationGuid,\r
268 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
269 VariableSize,\r
270 PreviousMemoryTypeInformation\r
271 );\r
272\r
273 if (!EFI_ERROR (Status)) {\r
274 //\r
665b26ba
RN
275 // If the Memory Type Information settings have been modified and the boot option belongs to boot category,\r
276 // then reset the platform so the new Memory Type Information setting will be used to guarantee that an S4\r
067ed98a
RN
277 // entry/resume cycle will not fail.\r
278 //\r
665b26ba 279 if (MemoryTypeInformationModified && Boot && PcdGetBool (PcdResetOnMemoryTypeInformationChange)) {\r
067ed98a
RN
280 DEBUG ((EFI_D_INFO, "Memory Type Information settings change. Warm Reset!!!\n"));\r
281 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
282 }\r
283 } else {\r
284 DEBUG ((EFI_D_ERROR, "Memory Type Information settings cannot be saved. OS S4 may fail!\n"));\r
285 }\r
286 }\r
287}\r
288\r
289/**\r
290 Set the variable and report the error through status code upon failure.\r
291\r
292 @param VariableName A Null-terminated string that is the name of the vendor's variable.\r
293 Each VariableName is unique for each VendorGuid. VariableName must\r
294 contain 1 or more characters. If VariableName is an empty string,\r
295 then EFI_INVALID_PARAMETER is returned.\r
296 @param VendorGuid A unique identifier for the vendor.\r
297 @param Attributes Attributes bitmask to set for the variable.\r
298 @param DataSize The size in bytes of the Data buffer. Unless the EFI_VARIABLE_APPEND_WRITE, \r
299 EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, or \r
300 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute is set, a size of zero \r
301 causes the variable to be deleted. When the EFI_VARIABLE_APPEND_WRITE attribute is \r
302 set, then a SetVariable() call with a DataSize of zero will not cause any change to \r
303 the variable value (the timestamp associated with the variable may be updated however \r
304 even if no new data value is provided,see the description of the \r
305 EFI_VARIABLE_AUTHENTICATION_2 descriptor below. In this case the DataSize will not \r
306 be zero since the EFI_VARIABLE_AUTHENTICATION_2 descriptor will be populated). \r
307 @param Data The contents for the variable.\r
308\r
309 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
310 defined by the Attributes.\r
311 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied, or the\r
312 DataSize exceeds the maximum allowed.\r
313 @retval EFI_INVALID_PARAMETER VariableName is an empty string.\r
314 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
315 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.\r
316 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
317 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
318 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS \r
319 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, but the AuthInfo \r
320 does NOT pass the validation check carried out by the firmware.\r
321\r
322 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
323**/\r
324EFI_STATUS\r
325BmSetVariableAndReportStatusCodeOnError (\r
326 IN CHAR16 *VariableName,\r
327 IN EFI_GUID *VendorGuid,\r
328 IN UINT32 Attributes,\r
329 IN UINTN DataSize,\r
330 IN VOID *Data\r
331 )\r
332{\r
333 EFI_STATUS Status;\r
334 EDKII_SET_VARIABLE_STATUS *SetVariableStatus;\r
335 UINTN NameSize;\r
336\r
337 Status = gRT->SetVariable (\r
338 VariableName,\r
339 VendorGuid,\r
340 Attributes,\r
341 DataSize,\r
342 Data\r
343 );\r
344 if (EFI_ERROR (Status)) {\r
345 NameSize = StrSize (VariableName);\r
346 SetVariableStatus = AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize);\r
347 if (SetVariableStatus != NULL) {\r
348 CopyGuid (&SetVariableStatus->Guid, VendorGuid);\r
349 SetVariableStatus->NameSize = NameSize;\r
350 SetVariableStatus->DataSize = DataSize;\r
351 SetVariableStatus->SetStatus = Status;\r
352 SetVariableStatus->Attributes = Attributes;\r
353 CopyMem (SetVariableStatus + 1, VariableName, NameSize);\r
354 CopyMem (((UINT8 *) (SetVariableStatus + 1)) + NameSize, Data, DataSize);\r
355\r
356 REPORT_STATUS_CODE_EX (\r
357 EFI_ERROR_CODE,\r
358 PcdGet32 (PcdErrorCodeSetVariable),\r
359 0,\r
360 NULL,\r
361 &gEdkiiStatusCodeDataTypeVariableGuid,\r
362 SetVariableStatus,\r
363 sizeof (EDKII_SET_VARIABLE_STATUS) + NameSize + DataSize\r
364 );\r
365\r
366 FreePool (SetVariableStatus);\r
367 }\r
368 }\r
369\r
370 return Status;\r
371}\r
372\r
373\r
374/**\r
375 Print the device path info.\r
376\r
377 @param DevicePath The device path need to print.\r
378**/\r
379VOID\r
380BmPrintDp (\r
381 EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
382 )\r
383{\r
384 CHAR16 *Str;\r
385\r
386 Str = ConvertDevicePathToText (DevicePath, FALSE, FALSE);\r
387 DEBUG ((EFI_D_INFO, "%s", Str));\r
388 if (Str != NULL) {\r
389 FreePool (Str);\r
390 }\r
391}\r
418e8cd9
RN
392\r
393/**\r
394 Convert a single character to number.\r
395 It assumes the input Char is in the scope of L'0' ~ L'9' and L'A' ~ L'F'\r
396\r
397 @param Char The input char which need to convert to int.\r
398\r
399 @return The converted 8-bit number or (UINTN) -1 if conversion failed.\r
400**/\r
401UINTN\r
402BmCharToUint (\r
403 IN CHAR16 Char\r
404 )\r
405{\r
406 if ((Char >= L'0') && (Char <= L'9')) {\r
407 return (UINTN) (Char - L'0');\r
408 }\r
409\r
410 if ((Char >= L'A') && (Char <= L'F')) {\r
411 return (UINTN) (Char - L'A' + 0xA);\r
412 }\r
413\r
414 ASSERT (FALSE);\r
415 return (UINTN) -1;\r
416}\r
417\r