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