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