]>
Commit | Line | Data |
---|---|---|
897f0eee | 1 | /** @file\r |
2 | Misc BDS library function\r | |
3 | \r | |
4 | Copyright (c) 2004 - 2008, Intel Corporation. <BR>\r | |
5 | All rights reserved. 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 "InternalBdsLib.h"\r | |
16 | \r | |
17 | \r | |
18 | #define MAX_STRING_LEN 200\r | |
19 | \r | |
11ef23f9 | 20 | BOOLEAN mFeaturerSwitch = TRUE;\r |
ec8cd35c | 21 | BOOLEAN mResetRequired = FALSE;\r |
897f0eee | 22 | \r |
23 | extern UINT16 gPlatformBootTimeOutDefault;\r | |
24 | \r | |
25 | \r | |
26 | /**\r | |
27 | Return the default value for system Timeout variable.\r | |
28 | \r | |
29 | @return Timeout value.\r | |
30 | \r | |
31 | **/\r | |
32 | UINT16\r | |
33 | EFIAPI\r | |
34 | BdsLibGetTimeout (\r | |
35 | VOID\r | |
36 | )\r | |
37 | {\r | |
38 | UINT16 Timeout;\r | |
39 | UINTN Size;\r | |
40 | EFI_STATUS Status;\r | |
41 | \r | |
42 | //\r | |
43 | // Return Timeout variable or 0xffff if no valid\r | |
44 | // Timeout variable exists.\r | |
45 | //\r | |
46 | Size = sizeof (UINT16);\r | |
47 | Status = gRT->GetVariable (L"Timeout", &gEfiGlobalVariableGuid, NULL, &Size, &Timeout);\r | |
0ef93eb7 | 48 | if (EFI_ERROR (Status)) {\r |
49 | //\r | |
50 | // According to UEFI 2.0 spec, it should treat the Timeout value as 0xffff\r | |
51 | // (default value PcdPlatformBootTimeOutDefault) when L"Timeout" variable is not present.\r | |
52 | // To make the current EFI Automatic-Test activity possible, platform can choose other value\r | |
53 | // for automatic boot when the variable is not present.\r | |
54 | //\r | |
55 | Timeout = PcdGet16 (PcdPlatformBootTimeOutDefault);\r | |
897f0eee | 56 | }\r |
897f0eee | 57 | \r |
897f0eee | 58 | return Timeout;\r |
59 | }\r | |
60 | \r | |
897f0eee | 61 | /**\r |
62 | The function will go through the driver optoin link list, load and start\r | |
63 | every driver the driver optoin device path point to.\r | |
64 | \r | |
65 | @param BdsDriverLists The header of the current driver option link list\r | |
66 | \r | |
67 | **/\r | |
68 | VOID\r | |
69 | EFIAPI\r | |
70 | BdsLibLoadDrivers (\r | |
71 | IN LIST_ENTRY *BdsDriverLists\r | |
72 | )\r | |
73 | {\r | |
74 | EFI_STATUS Status;\r | |
75 | LIST_ENTRY *Link;\r | |
76 | BDS_COMMON_OPTION *Option;\r | |
77 | EFI_HANDLE ImageHandle;\r | |
78 | EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r | |
79 | UINTN ExitDataSize;\r | |
80 | CHAR16 *ExitData;\r | |
81 | BOOLEAN ReconnectAll;\r | |
82 | \r | |
83 | ReconnectAll = FALSE;\r | |
84 | \r | |
85 | //\r | |
86 | // Process the driver option\r | |
87 | //\r | |
88 | for (Link = BdsDriverLists->ForwardLink; Link != BdsDriverLists; Link = Link->ForwardLink) {\r | |
89 | Option = CR (Link, BDS_COMMON_OPTION, Link, BDS_LOAD_OPTION_SIGNATURE);\r | |
ec8cd35c | 90 | \r |
897f0eee | 91 | //\r |
92 | // If a load option is not marked as LOAD_OPTION_ACTIVE,\r | |
93 | // the boot manager will not automatically load the option.\r | |
94 | //\r | |
95 | if (!IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_ACTIVE)) {\r | |
96 | continue;\r | |
97 | }\r | |
ec8cd35c | 98 | \r |
897f0eee | 99 | //\r |
100 | // If a driver load option is marked as LOAD_OPTION_FORCE_RECONNECT,\r | |
101 | // then all of the EFI drivers in the system will be disconnected and\r | |
102 | // reconnected after the last driver load option is processed.\r | |
103 | //\r | |
104 | if (IS_LOAD_OPTION_TYPE (Option->Attribute, LOAD_OPTION_FORCE_RECONNECT)) {\r | |
105 | ReconnectAll = TRUE;\r | |
106 | }\r | |
ec8cd35c | 107 | \r |
897f0eee | 108 | //\r |
109 | // Make sure the driver path is connected.\r | |
110 | //\r | |
111 | BdsLibConnectDevicePath (Option->DevicePath);\r | |
112 | \r | |
113 | //\r | |
114 | // Load and start the image that Driver#### describes\r | |
115 | //\r | |
116 | Status = gBS->LoadImage (\r | |
117 | FALSE,\r | |
118 | mBdsImageHandle,\r | |
119 | Option->DevicePath,\r | |
120 | NULL,\r | |
121 | 0,\r | |
122 | &ImageHandle\r | |
123 | );\r | |
124 | \r | |
125 | if (!EFI_ERROR (Status)) {\r | |
126 | gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r | |
127 | \r | |
128 | //\r | |
129 | // Verify whether this image is a driver, if not,\r | |
130 | // exit it and continue to parse next load option\r | |
131 | //\r | |
132 | if (ImageInfo->ImageCodeType != EfiBootServicesCode && ImageInfo->ImageCodeType != EfiRuntimeServicesCode) {\r | |
133 | gBS->Exit (ImageHandle, EFI_INVALID_PARAMETER, 0, NULL);\r | |
134 | continue;\r | |
135 | }\r | |
136 | \r | |
137 | if (Option->LoadOptionsSize != 0) {\r | |
138 | ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r | |
139 | ImageInfo->LoadOptions = Option->LoadOptions;\r | |
140 | }\r | |
141 | //\r | |
142 | // Before calling the image, enable the Watchdog Timer for\r | |
143 | // the 5 Minute period\r | |
144 | //\r | |
145 | gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r | |
146 | \r | |
147 | Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);\r | |
148 | DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Driver Return Status = %r\n", Status));\r | |
149 | \r | |
150 | //\r | |
151 | // Clear the Watchdog Timer after the image returns\r | |
152 | //\r | |
153 | gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r | |
154 | }\r | |
155 | }\r | |
ec8cd35c | 156 | \r |
897f0eee | 157 | //\r |
158 | // Process the LOAD_OPTION_FORCE_RECONNECT driver option\r | |
159 | //\r | |
160 | if (ReconnectAll) {\r | |
161 | BdsLibDisconnectAllEfi ();\r | |
162 | BdsLibConnectAll ();\r | |
163 | }\r | |
164 | \r | |
165 | }\r | |
166 | \r | |
897f0eee | 167 | /**\r |
11ef23f9 | 168 | Get the Option Number that does not used.\r |
169 | Try to locate the specific option variable one by one untile find a free number.\r | |
897f0eee | 170 | \r |
171 | @param VariableName Indicate if the boot#### or driver#### option\r | |
172 | \r | |
173 | @return The Minimal Free Option Number\r | |
174 | \r | |
175 | **/\r | |
176 | UINT16\r | |
177 | BdsLibGetFreeOptionNumber (\r | |
178 | IN CHAR16 *VariableName\r | |
179 | )\r | |
180 | {\r | |
897f0eee | 181 | UINTN Index;\r |
182 | CHAR16 StrTemp[10];\r | |
183 | UINT16 *OptionBuffer;\r | |
184 | UINTN OptionSize;\r | |
185 | \r | |
186 | //\r | |
187 | // Try to find the minimum free number from 0, 1, 2, 3....\r | |
188 | //\r | |
189 | Index = 0;\r | |
190 | do {\r | |
191 | if (*VariableName == 'B') {\r | |
192 | UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);\r | |
193 | } else {\r | |
194 | UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Driver%04x", Index);\r | |
195 | }\r | |
196 | //\r | |
197 | // try if the option number is used\r | |
198 | //\r | |
199 | OptionBuffer = BdsLibGetVariableAndSize (\r | |
ec8cd35c | 200 | StrTemp,\r |
201 | &gEfiGlobalVariableGuid,\r | |
202 | &OptionSize\r | |
203 | );\r | |
897f0eee | 204 | if (OptionBuffer == NULL) {\r |
205 | break;\r | |
206 | }\r | |
ec8cd35c | 207 | Index ++;\r |
208 | } while (TRUE);\r | |
897f0eee | 209 | \r |
ec8cd35c | 210 | return ((UINT16) Index);\r |
897f0eee | 211 | }\r |
212 | \r | |
213 | \r | |
214 | /**\r | |
215 | This function will register the new boot#### or driver#### option base on\r | |
216 | the VariableName. The new registered boot#### or driver#### will be linked\r | |
217 | to BdsOptionList and also update to the VariableName. After the boot#### or\r | |
218 | driver#### updated, the BootOrder or DriverOrder will also be updated.\r | |
219 | \r | |
220 | @param BdsOptionList The header of the boot#### or driver#### link list\r | |
221 | @param DevicePath The device path which the boot#### or driver####\r | |
222 | option present\r | |
223 | @param String The description of the boot#### or driver####\r | |
224 | @param VariableName Indicate if the boot#### or driver#### option\r | |
225 | \r | |
226 | @retval EFI_SUCCESS The boot#### or driver#### have been success\r | |
227 | registered\r | |
228 | @retval EFI_STATUS Return the status of gRT->SetVariable ().\r | |
229 | \r | |
230 | **/\r | |
231 | EFI_STATUS\r | |
232 | EFIAPI\r | |
233 | BdsLibRegisterNewOption (\r | |
234 | IN LIST_ENTRY *BdsOptionList,\r | |
235 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r | |
236 | IN CHAR16 *String,\r | |
237 | IN CHAR16 *VariableName\r | |
238 | )\r | |
239 | {\r | |
240 | EFI_STATUS Status;\r | |
241 | UINTN Index;\r | |
242 | UINT16 RegisterOptionNumber;\r | |
243 | UINT16 *TempOptionPtr;\r | |
244 | UINTN TempOptionSize;\r | |
245 | UINT16 *OptionOrderPtr;\r | |
246 | VOID *OptionPtr;\r | |
247 | UINTN OptionSize;\r | |
248 | UINT8 *TempPtr;\r | |
249 | EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r | |
250 | CHAR16 *Description;\r | |
251 | CHAR16 OptionName[10];\r | |
252 | BOOLEAN UpdateDescription;\r | |
253 | UINT16 BootOrderEntry;\r | |
254 | UINTN OrderItemNum;\r | |
255 | \r | |
256 | \r | |
257 | OptionPtr = NULL;\r | |
258 | OptionSize = 0;\r | |
259 | TempPtr = NULL;\r | |
260 | OptionDevicePath = NULL;\r | |
261 | Description = NULL;\r | |
262 | OptionOrderPtr = NULL;\r | |
263 | UpdateDescription = FALSE;\r | |
ec8cd35c | 264 | Status = EFI_SUCCESS;\r |
897f0eee | 265 | ZeroMem (OptionName, sizeof (OptionName));\r |
266 | \r | |
267 | TempOptionSize = 0;\r | |
268 | TempOptionPtr = BdsLibGetVariableAndSize (\r | |
269 | VariableName,\r | |
270 | &gEfiGlobalVariableGuid,\r | |
271 | &TempOptionSize\r | |
272 | );\r | |
273 | \r | |
274 | //\r | |
275 | // Compare with current option variable\r | |
276 | //\r | |
277 | for (Index = 0; Index < TempOptionSize / sizeof (UINT16); Index++) {\r | |
278 | \r | |
279 | if (*VariableName == 'B') {\r | |
280 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", TempOptionPtr[Index]);\r | |
281 | } else {\r | |
282 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", TempOptionPtr[Index]);\r | |
283 | }\r | |
284 | \r | |
285 | OptionPtr = BdsLibGetVariableAndSize (\r | |
286 | OptionName,\r | |
287 | &gEfiGlobalVariableGuid,\r | |
288 | &OptionSize\r | |
289 | );\r | |
290 | if (OptionPtr == NULL) {\r | |
291 | continue;\r | |
292 | }\r | |
ec8cd35c | 293 | TempPtr = OptionPtr;\r |
294 | TempPtr += sizeof (UINT32) + sizeof (UINT16);\r | |
295 | Description = (CHAR16 *) TempPtr;\r | |
296 | TempPtr += StrSize ((CHAR16 *) TempPtr);\r | |
297 | OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r | |
897f0eee | 298 | \r |
299 | //\r | |
300 | // Notes: the description may will change base on the GetStringToken\r | |
301 | //\r | |
302 | if (CompareMem (OptionDevicePath, DevicePath, GetDevicePathSize (OptionDevicePath)) == 0) {\r | |
303 | if (CompareMem (Description, String, StrSize (Description)) == 0) { \r | |
304 | //\r | |
305 | // Got the option, so just return\r | |
306 | //\r | |
676df92c | 307 | FreePool (OptionPtr);\r |
383ad679 | 308 | FreePool (TempOptionPtr);\r |
897f0eee | 309 | return EFI_SUCCESS;\r |
310 | } else {\r | |
311 | //\r | |
312 | // Option description changed, need update.\r | |
313 | //\r | |
314 | UpdateDescription = TRUE;\r | |
676df92c | 315 | FreePool (OptionPtr);\r |
897f0eee | 316 | break;\r |
317 | }\r | |
318 | }\r | |
319 | \r | |
676df92c | 320 | FreePool (OptionPtr);\r |
897f0eee | 321 | }\r |
322 | \r | |
323 | OptionSize = sizeof (UINT32) + sizeof (UINT16) + StrSize (String);\r | |
ec8cd35c | 324 | OptionSize += GetDevicePathSize (DevicePath);\r |
897f0eee | 325 | OptionPtr = AllocateZeroPool (OptionSize);\r |
676df92c | 326 | ASSERT (OptionPtr != NULL);\r |
327 | \r | |
897f0eee | 328 | TempPtr = OptionPtr;\r |
329 | *(UINT32 *) TempPtr = LOAD_OPTION_ACTIVE;\r | |
ec8cd35c | 330 | TempPtr += sizeof (UINT32);\r |
897f0eee | 331 | *(UINT16 *) TempPtr = (UINT16) GetDevicePathSize (DevicePath);\r |
ec8cd35c | 332 | TempPtr += sizeof (UINT16);\r |
897f0eee | 333 | CopyMem (TempPtr, String, StrSize (String));\r |
ec8cd35c | 334 | TempPtr += StrSize (String);\r |
897f0eee | 335 | CopyMem (TempPtr, DevicePath, GetDevicePathSize (DevicePath));\r |
336 | \r | |
337 | if (UpdateDescription) {\r | |
338 | //\r | |
339 | // The number in option#### to be updated\r | |
340 | //\r | |
341 | RegisterOptionNumber = TempOptionPtr[Index];\r | |
342 | } else {\r | |
343 | //\r | |
344 | // The new option#### number\r | |
345 | //\r | |
346 | RegisterOptionNumber = BdsLibGetFreeOptionNumber(VariableName);\r | |
347 | }\r | |
348 | \r | |
349 | if (*VariableName == 'B') {\r | |
350 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", RegisterOptionNumber);\r | |
351 | } else {\r | |
352 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", RegisterOptionNumber);\r | |
353 | }\r | |
354 | \r | |
355 | Status = gRT->SetVariable (\r | |
356 | OptionName,\r | |
357 | &gEfiGlobalVariableGuid,\r | |
358 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
359 | OptionSize,\r | |
360 | OptionPtr\r | |
361 | );\r | |
362 | //\r | |
363 | // Return if only need to update a changed description or fail to set option.\r | |
364 | //\r | |
365 | if (EFI_ERROR (Status) || UpdateDescription) {\r | |
676df92c | 366 | FreePool (OptionPtr);\r |
c473cc17 | 367 | if (TempOptionPtr != NULL) {\r |
368 | FreePool (TempOptionPtr);\r | |
369 | }\r | |
897f0eee | 370 | return Status;\r |
371 | }\r | |
372 | \r | |
676df92c | 373 | FreePool (OptionPtr);\r |
897f0eee | 374 | \r |
375 | //\r | |
376 | // Update the option order variable\r | |
377 | //\r | |
378 | \r | |
379 | //\r | |
380 | // If no option order\r | |
381 | //\r | |
382 | if (TempOptionSize == 0) {\r | |
383 | BootOrderEntry = 0;\r | |
384 | Status = gRT->SetVariable (\r | |
385 | VariableName,\r | |
386 | &gEfiGlobalVariableGuid,\r | |
387 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
388 | sizeof (UINT16),\r | |
389 | &BootOrderEntry\r | |
390 | );\r | |
c473cc17 | 391 | if (TempOptionPtr != NULL) {\r |
392 | FreePool (TempOptionPtr);\r | |
393 | }\r | |
ec8cd35c | 394 | return Status;\r |
897f0eee | 395 | }\r |
396 | \r | |
397 | //\r | |
398 | // Append the new option number to the original option order\r | |
399 | //\r | |
400 | OrderItemNum = (TempOptionSize / sizeof (UINT16)) + 1 ;\r | |
401 | OptionOrderPtr = AllocateZeroPool ( OrderItemNum * sizeof (UINT16));\r | |
676df92c | 402 | ASSERT (OptionOrderPtr!= NULL);\r |
403 | \r | |
897f0eee | 404 | CopyMem (OptionOrderPtr, TempOptionPtr, (OrderItemNum - 1) * sizeof (UINT16));\r |
405 | \r | |
406 | OptionOrderPtr[Index] = RegisterOptionNumber;\r | |
407 | \r | |
408 | Status = gRT->SetVariable (\r | |
409 | VariableName,\r | |
410 | &gEfiGlobalVariableGuid,\r | |
411 | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r | |
412 | OrderItemNum * sizeof (UINT16),\r | |
413 | OptionOrderPtr\r | |
414 | );\r | |
676df92c | 415 | FreePool (TempOptionPtr);\r |
416 | FreePool (OptionOrderPtr);\r | |
897f0eee | 417 | \r |
ec8cd35c | 418 | return Status;\r |
897f0eee | 419 | }\r |
420 | \r | |
421 | \r | |
422 | /**\r | |
423 | Build the boot#### or driver#### option from the VariableName, the\r | |
11ef23f9 | 424 | build boot#### or driver#### will also be linked to BdsCommonOptionList.\r |
897f0eee | 425 | \r |
426 | @param BdsCommonOptionList The header of the boot#### or driver#### option\r | |
427 | link list\r | |
428 | @param VariableName EFI Variable name indicate if it is boot#### or\r | |
429 | driver####\r | |
430 | \r | |
431 | @retval BDS_COMMON_OPTION Get the option just been created\r | |
432 | @retval NULL Failed to get the new option\r | |
433 | \r | |
434 | **/\r | |
435 | BDS_COMMON_OPTION *\r | |
436 | EFIAPI\r | |
437 | BdsLibVariableToOption (\r | |
438 | IN OUT LIST_ENTRY *BdsCommonOptionList,\r | |
439 | IN CHAR16 *VariableName\r | |
440 | )\r | |
441 | {\r | |
442 | UINT32 Attribute;\r | |
443 | UINT16 FilePathSize;\r | |
444 | UINT8 *Variable;\r | |
445 | UINT8 *TempPtr;\r | |
446 | UINTN VariableSize;\r | |
447 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
448 | BDS_COMMON_OPTION *Option;\r | |
449 | VOID *LoadOptions;\r | |
450 | UINT32 LoadOptionsSize;\r | |
451 | CHAR16 *Description;\r | |
452 | UINT8 NumOff;\r | |
453 | //\r | |
454 | // Read the variable. We will never free this data.\r | |
455 | //\r | |
456 | Variable = BdsLibGetVariableAndSize (\r | |
457 | VariableName,\r | |
458 | &gEfiGlobalVariableGuid,\r | |
459 | &VariableSize\r | |
460 | );\r | |
461 | if (Variable == NULL) {\r | |
462 | return NULL;\r | |
463 | }\r | |
464 | //\r | |
465 | // Notes: careful defined the variable of Boot#### or\r | |
466 | // Driver####, consider use some macro to abstract the code\r | |
467 | //\r | |
468 | //\r | |
469 | // Get the option attribute\r | |
470 | //\r | |
ec8cd35c | 471 | TempPtr = Variable;\r |
472 | Attribute = *(UINT32 *) Variable;\r | |
473 | TempPtr += sizeof (UINT32);\r | |
897f0eee | 474 | \r |
475 | //\r | |
476 | // Get the option's device path size\r | |
477 | //\r | |
ec8cd35c | 478 | FilePathSize = *(UINT16 *) TempPtr;\r |
479 | TempPtr += sizeof (UINT16);\r | |
897f0eee | 480 | \r |
481 | //\r | |
482 | // Get the option's description string\r | |
483 | //\r | |
484 | Description = (CHAR16 *) TempPtr;\r | |
485 | \r | |
486 | //\r | |
487 | // Get the option's description string size\r | |
488 | //\r | |
ec8cd35c | 489 | TempPtr += StrSize ((CHAR16 *) TempPtr);\r |
897f0eee | 490 | \r |
491 | //\r | |
492 | // Get the option's device path\r | |
493 | //\r | |
ec8cd35c | 494 | DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r |
495 | TempPtr += FilePathSize;\r | |
897f0eee | 496 | \r |
497 | LoadOptions = TempPtr;\r | |
498 | LoadOptionsSize = (UINT32) (VariableSize - (UINTN) (TempPtr - Variable));\r | |
499 | \r | |
500 | //\r | |
501 | // The Console variables may have multiple device paths, so make\r | |
502 | // an Entry for each one.\r | |
503 | //\r | |
504 | Option = AllocateZeroPool (sizeof (BDS_COMMON_OPTION));\r | |
505 | if (Option == NULL) {\r | |
506 | return NULL;\r | |
507 | }\r | |
508 | \r | |
509 | Option->Signature = BDS_LOAD_OPTION_SIGNATURE;\r | |
510 | Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r | |
511 | CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r | |
512 | Option->Attribute = Attribute;\r | |
513 | Option->Description = AllocateZeroPool (StrSize (Description));\r | |
514 | CopyMem (Option->Description, Description, StrSize (Description));\r | |
515 | Option->LoadOptions = AllocateZeroPool (LoadOptionsSize);\r | |
516 | CopyMem (Option->LoadOptions, LoadOptions, LoadOptionsSize);\r | |
517 | Option->LoadOptionsSize = LoadOptionsSize;\r | |
518 | \r | |
519 | //\r | |
520 | // Get the value from VariableName Unicode string\r | |
521 | // since the ISO standard assumes ASCII equivalent abbreviations, we can be safe in converting this\r | |
522 | // Unicode stream to ASCII without any loss in meaning.\r | |
523 | //\r | |
524 | if (*VariableName == 'B') {\r | |
525 | NumOff = sizeof (L"Boot")/sizeof(CHAR16) -1 ;\r | |
526 | Option->BootCurrent = (UINT16) ((VariableName[NumOff] -'0') * 0x1000);\r | |
527 | Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+1]-'0') * 0x100));\r | |
528 | Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+2]-'0') * 0x10));\r | |
529 | Option->BootCurrent = (UINT16) (Option->BootCurrent + ((VariableName[NumOff+3]-'0')));\r | |
530 | }\r | |
531 | //\r | |
532 | // Insert active entry to BdsDeviceList\r | |
533 | //\r | |
534 | if ((Option->Attribute & LOAD_OPTION_ACTIVE) == LOAD_OPTION_ACTIVE) {\r | |
535 | InsertTailList (BdsCommonOptionList, &Option->Link);\r | |
676df92c | 536 | FreePool (Variable);\r |
897f0eee | 537 | return Option;\r |
538 | }\r | |
539 | \r | |
676df92c | 540 | FreePool (Variable);\r |
541 | FreePool (Option);\r | |
897f0eee | 542 | return NULL;\r |
543 | \r | |
544 | }\r | |
545 | \r | |
897f0eee | 546 | /**\r |
547 | Process BootOrder, or DriverOrder variables, by calling\r | |
548 | BdsLibVariableToOption () for each UINT16 in the variables.\r | |
549 | \r | |
550 | @param BdsCommonOptionList The header of the option list base on variable\r | |
551 | VariableName\r | |
552 | @param VariableName EFI Variable name indicate the BootOrder or\r | |
553 | DriverOrder\r | |
554 | \r | |
555 | @retval EFI_SUCCESS Success create the boot option or driver option\r | |
556 | list\r | |
557 | @retval EFI_OUT_OF_RESOURCES Failed to get the boot option or driver option list\r | |
558 | \r | |
559 | **/\r | |
560 | EFI_STATUS\r | |
561 | EFIAPI\r | |
562 | BdsLibBuildOptionFromVar (\r | |
563 | IN LIST_ENTRY *BdsCommonOptionList,\r | |
564 | IN CHAR16 *VariableName\r | |
565 | )\r | |
566 | {\r | |
567 | UINT16 *OptionOrder;\r | |
568 | UINTN OptionOrderSize;\r | |
569 | UINTN Index;\r | |
570 | BDS_COMMON_OPTION *Option;\r | |
571 | CHAR16 OptionName[20];\r | |
572 | \r | |
573 | //\r | |
574 | // Zero Buffer in order to get all BOOT#### variables\r | |
575 | //\r | |
576 | ZeroMem (OptionName, sizeof (OptionName));\r | |
577 | \r | |
578 | //\r | |
579 | // Read the BootOrder, or DriverOrder variable.\r | |
580 | //\r | |
581 | OptionOrder = BdsLibGetVariableAndSize (\r | |
582 | VariableName,\r | |
583 | &gEfiGlobalVariableGuid,\r | |
584 | &OptionOrderSize\r | |
585 | );\r | |
586 | if (OptionOrder == NULL) {\r | |
587 | return EFI_OUT_OF_RESOURCES;\r | |
588 | }\r | |
589 | \r | |
590 | for (Index = 0; Index < OptionOrderSize / sizeof (UINT16); Index++) {\r | |
591 | if (*VariableName == 'B') {\r | |
592 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionOrder[Index]);\r | |
593 | } else {\r | |
594 | UnicodeSPrint (OptionName, sizeof (OptionName), L"Driver%04x", OptionOrder[Index]);\r | |
595 | }\r | |
596 | \r | |
597 | Option = BdsLibVariableToOption (BdsCommonOptionList, OptionName);\r | |
598 | Option->BootCurrent = OptionOrder[Index];\r | |
599 | \r | |
600 | }\r | |
601 | \r | |
676df92c | 602 | FreePool (OptionOrder);\r |
897f0eee | 603 | \r |
604 | return EFI_SUCCESS;\r | |
605 | }\r | |
606 | \r | |
897f0eee | 607 | /**\r |
608 | Get boot mode by looking up configuration table and parsing HOB list\r | |
609 | \r | |
610 | @param BootMode Boot mode from PEI handoff HOB.\r | |
611 | \r | |
612 | @retval EFI_SUCCESS Successfully get boot mode\r | |
613 | \r | |
614 | **/\r | |
615 | EFI_STATUS\r | |
616 | EFIAPI\r | |
617 | BdsLibGetBootMode (\r | |
618 | OUT EFI_BOOT_MODE *BootMode\r | |
619 | )\r | |
620 | {\r | |
621 | *BootMode = GetBootModeHob ();\r | |
622 | \r | |
623 | return EFI_SUCCESS;\r | |
624 | }\r | |
625 | \r | |
897f0eee | 626 | /**\r |
627 | Read the EFI variable (VendorGuid/Name) and return a dynamically allocated\r | |
628 | buffer, and the size of the buffer. If failure return NULL.\r | |
629 | \r | |
630 | @param Name String part of EFI variable name\r | |
631 | @param VendorGuid GUID part of EFI variable name\r | |
632 | @param VariableSize Returns the size of the EFI variable that was read\r | |
633 | \r | |
262b0668 | 634 | @return Dynamically allocated memory that contains a copy of the EFI variable\r |
635 | Caller is responsible freeing the buffer.\r | |
897f0eee | 636 | @retval NULL Variable was not read\r |
637 | \r | |
638 | **/\r | |
639 | VOID *\r | |
640 | EFIAPI\r | |
641 | BdsLibGetVariableAndSize (\r | |
642 | IN CHAR16 *Name,\r | |
643 | IN EFI_GUID *VendorGuid,\r | |
644 | OUT UINTN *VariableSize\r | |
645 | )\r | |
646 | {\r | |
647 | EFI_STATUS Status;\r | |
648 | UINTN BufferSize;\r | |
649 | VOID *Buffer;\r | |
650 | \r | |
651 | Buffer = NULL;\r | |
652 | \r | |
653 | //\r | |
654 | // Pass in a zero size buffer to find the required buffer size.\r | |
655 | //\r | |
656 | BufferSize = 0;\r | |
657 | Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r | |
658 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
659 | //\r | |
660 | // Allocate the buffer to return\r | |
661 | //\r | |
662 | Buffer = AllocateZeroPool (BufferSize);\r | |
663 | if (Buffer == NULL) {\r | |
664 | return NULL;\r | |
665 | }\r | |
666 | //\r | |
667 | // Read variable into the allocated buffer.\r | |
668 | //\r | |
669 | Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);\r | |
670 | if (EFI_ERROR (Status)) {\r | |
671 | BufferSize = 0;\r | |
672 | }\r | |
673 | }\r | |
674 | \r | |
675 | *VariableSize = BufferSize;\r | |
676 | return Buffer;\r | |
677 | }\r | |
678 | \r | |
897f0eee | 679 | /**\r |
680 | Delete the instance in Multi which matches partly with Single instance\r | |
681 | \r | |
682 | @param Multi A pointer to a multi-instance device path data\r | |
683 | structure.\r | |
684 | @param Single A pointer to a single-instance device path data\r | |
685 | structure.\r | |
686 | \r | |
687 | @return This function will remove the device path instances in Multi which partly\r | |
688 | match with the Single, and return the result device path. If there is no\r | |
689 | remaining device path as a result, this function will return NULL.\r | |
690 | \r | |
691 | **/\r | |
692 | EFI_DEVICE_PATH_PROTOCOL *\r | |
693 | EFIAPI\r | |
694 | BdsLibDelPartMatchInstance (\r | |
695 | IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r | |
696 | IN EFI_DEVICE_PATH_PROTOCOL *Single\r | |
697 | )\r | |
698 | {\r | |
699 | EFI_DEVICE_PATH_PROTOCOL *Instance;\r | |
700 | EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r | |
701 | EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r | |
702 | UINTN InstanceSize;\r | |
703 | UINTN SingleDpSize;\r | |
704 | UINTN Size;\r | |
705 | \r | |
706 | NewDevicePath = NULL;\r | |
707 | TempNewDevicePath = NULL;\r | |
708 | \r | |
709 | if (Multi == NULL || Single == NULL) {\r | |
710 | return Multi;\r | |
711 | }\r | |
712 | \r | |
713 | Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r | |
714 | SingleDpSize = GetDevicePathSize (Single) - END_DEVICE_PATH_LENGTH;\r | |
715 | InstanceSize -= END_DEVICE_PATH_LENGTH;\r | |
716 | \r | |
717 | while (Instance != NULL) {\r | |
718 | \r | |
719 | Size = (SingleDpSize < InstanceSize) ? SingleDpSize : InstanceSize;\r | |
720 | \r | |
721 | if ((CompareMem (Instance, Single, Size) != 0)) {\r | |
722 | //\r | |
723 | // Append the device path instance which does not match with Single\r | |
724 | //\r | |
725 | TempNewDevicePath = NewDevicePath;\r | |
726 | NewDevicePath = AppendDevicePathInstance (NewDevicePath, Instance);\r | |
676df92c | 727 | if (TempNewDevicePath != NULL) {\r |
728 | FreePool(TempNewDevicePath);\r | |
729 | }\r | |
897f0eee | 730 | }\r |
676df92c | 731 | FreePool(Instance);\r |
897f0eee | 732 | Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);\r |
733 | InstanceSize -= END_DEVICE_PATH_LENGTH;\r | |
734 | }\r | |
735 | \r | |
736 | return NewDevicePath;\r | |
737 | }\r | |
738 | \r | |
897f0eee | 739 | /**\r |
740 | Function compares a device path data structure to that of all the nodes of a\r | |
741 | second device path instance.\r | |
742 | \r | |
743 | @param Multi A pointer to a multi-instance device path data\r | |
744 | structure.\r | |
745 | @param Single A pointer to a single-instance device path data\r | |
746 | structure.\r | |
747 | \r | |
262b0668 | 748 | @retval TRUE If the Single device path is contained within Multi device path.\r |
749 | @retval FALSE The Single device path is not match within Multi device path.\r | |
897f0eee | 750 | \r |
751 | **/\r | |
752 | BOOLEAN\r | |
753 | EFIAPI\r | |
754 | BdsLibMatchDevicePaths (\r | |
755 | IN EFI_DEVICE_PATH_PROTOCOL *Multi,\r | |
756 | IN EFI_DEVICE_PATH_PROTOCOL *Single\r | |
757 | )\r | |
758 | {\r | |
759 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r | |
760 | EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;\r | |
761 | UINTN Size;\r | |
762 | \r | |
ff6bf04b | 763 | if (Multi == NULL || Single == NULL) {\r |
897f0eee | 764 | return FALSE;\r |
765 | }\r | |
766 | \r | |
767 | DevicePath = Multi;\r | |
768 | DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r | |
769 | \r | |
770 | //\r | |
771 | // Search for the match of 'Single' in 'Multi'\r | |
772 | //\r | |
773 | while (DevicePathInst != NULL) {\r | |
774 | //\r | |
775 | // If the single device path is found in multiple device paths,\r | |
776 | // return success\r | |
777 | //\r | |
778 | if (CompareMem (Single, DevicePathInst, Size) == 0) {\r | |
676df92c | 779 | FreePool (DevicePathInst);\r |
897f0eee | 780 | return TRUE;\r |
781 | }\r | |
782 | \r | |
676df92c | 783 | FreePool (DevicePathInst);\r |
897f0eee | 784 | DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);\r |
785 | }\r | |
786 | \r | |
787 | return FALSE;\r | |
788 | }\r | |
789 | \r | |
897f0eee | 790 | /**\r |
791 | This function prints a series of strings.\r | |
792 | \r | |
793 | @param ConOut Pointer to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL\r | |
794 | @param ... A variable argument list containing series of\r | |
795 | strings, the last string must be NULL.\r | |
796 | \r | |
797 | @retval EFI_SUCCESS Success print out the string using ConOut.\r | |
798 | @retval EFI_STATUS Return the status of the ConOut->OutputString ().\r | |
799 | \r | |
800 | **/\r | |
801 | EFI_STATUS\r | |
802 | EFIAPI\r | |
803 | BdsLibOutputStrings (\r | |
804 | IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut,\r | |
805 | ...\r | |
806 | )\r | |
807 | {\r | |
11ef23f9 | 808 | VA_LIST Args;\r |
897f0eee | 809 | EFI_STATUS Status;\r |
810 | CHAR16 *String;\r | |
811 | \r | |
812 | Status = EFI_SUCCESS;\r | |
11ef23f9 | 813 | VA_START (Args, ConOut);\r |
897f0eee | 814 | \r |
815 | while (!EFI_ERROR (Status)) {\r | |
816 | //\r | |
817 | // If String is NULL, then it's the end of the list\r | |
818 | //\r | |
11ef23f9 | 819 | String = VA_ARG (Args, CHAR16 *);\r |
820 | if (String != NULL) {\r | |
897f0eee | 821 | break;\r |
822 | }\r | |
823 | \r | |
824 | Status = ConOut->OutputString (ConOut, String);\r | |
825 | \r | |
826 | if (EFI_ERROR (Status)) {\r | |
827 | break;\r | |
828 | }\r | |
829 | }\r | |
830 | \r | |
831 | return Status;\r | |
832 | }\r | |
833 | \r | |
834 | //\r | |
835 | // Following are BDS Lib functions which contain all the code about setup browser reset reminder feature.\r | |
836 | // Setup Browser reset reminder feature is that an reset reminder will be given before user leaves the setup browser if\r | |
837 | // user change any option setting which needs a reset to be effective, and the reset will be applied according to the user selection.\r | |
838 | //\r | |
839 | \r | |
840 | \r | |
841 | /**\r | |
842 | Enable the setup browser reset reminder feature.\r | |
843 | This routine is used in platform tip. If the platform policy need the feature, use the routine to enable it.\r | |
844 | \r | |
845 | **/\r | |
846 | VOID\r | |
847 | EFIAPI\r | |
848 | EnableResetReminderFeature (\r | |
849 | VOID\r | |
850 | )\r | |
851 | {\r | |
852 | mFeaturerSwitch = TRUE;\r | |
853 | }\r | |
854 | \r | |
855 | \r | |
856 | /**\r | |
857 | Disable the setup browser reset reminder feature.\r | |
858 | This routine is used in platform tip. If the platform policy do not want the feature, use the routine to disable it.\r | |
859 | \r | |
860 | **/\r | |
861 | VOID\r | |
862 | EFIAPI\r | |
863 | DisableResetReminderFeature (\r | |
864 | VOID\r | |
865 | )\r | |
866 | {\r | |
867 | mFeaturerSwitch = FALSE;\r | |
868 | }\r | |
869 | \r | |
870 | \r | |
871 | /**\r | |
872 | Record the info that a reset is required.\r | |
873 | A module boolean variable is used to record whether a reset is required.\r | |
874 | \r | |
875 | **/\r | |
876 | VOID\r | |
877 | EFIAPI\r | |
878 | EnableResetRequired (\r | |
879 | VOID\r | |
880 | )\r | |
881 | {\r | |
882 | mResetRequired = TRUE;\r | |
883 | }\r | |
884 | \r | |
885 | \r | |
886 | /**\r | |
887 | Record the info that no reset is required.\r | |
888 | A module boolean variable is used to record whether a reset is required.\r | |
889 | \r | |
890 | **/\r | |
891 | VOID\r | |
892 | EFIAPI\r | |
893 | DisableResetRequired (\r | |
894 | VOID\r | |
895 | )\r | |
896 | {\r | |
897 | mResetRequired = FALSE;\r | |
898 | }\r | |
899 | \r | |
900 | \r | |
901 | /**\r | |
902 | Check whether platform policy enable the reset reminder feature. The default is enabled.\r | |
903 | \r | |
904 | **/\r | |
905 | BOOLEAN\r | |
906 | EFIAPI\r | |
907 | IsResetReminderFeatureEnable (\r | |
908 | VOID\r | |
909 | )\r | |
910 | {\r | |
911 | return mFeaturerSwitch;\r | |
912 | }\r | |
913 | \r | |
914 | \r | |
915 | /**\r | |
916 | Check if user changed any option setting which needs a system reset to be effective.\r | |
917 | \r | |
918 | **/\r | |
919 | BOOLEAN\r | |
920 | EFIAPI\r | |
921 | IsResetRequired (\r | |
922 | VOID\r | |
923 | )\r | |
924 | {\r | |
925 | return mResetRequired;\r | |
926 | }\r | |
927 | \r | |
928 | \r | |
929 | /**\r | |
930 | Check whether a reset is needed, and finish the reset reminder feature.\r | |
931 | If a reset is needed, Popup a menu to notice user, and finish the feature\r | |
932 | according to the user selection.\r | |
933 | \r | |
934 | **/\r | |
935 | VOID\r | |
936 | EFIAPI\r | |
937 | SetupResetReminder (\r | |
938 | VOID\r | |
939 | )\r | |
940 | {\r | |
941 | EFI_INPUT_KEY Key;\r | |
942 | CHAR16 *StringBuffer1;\r | |
943 | CHAR16 *StringBuffer2;\r | |
944 | \r | |
945 | \r | |
946 | //\r | |
947 | //check any reset required change is applied? if yes, reset system\r | |
948 | //\r | |
949 | if (IsResetReminderFeatureEnable ()) {\r | |
950 | if (IsResetRequired ()) {\r | |
951 | \r | |
952 | StringBuffer1 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r | |
953 | ASSERT (StringBuffer1 != NULL);\r | |
954 | StringBuffer2 = AllocateZeroPool (MAX_STRING_LEN * sizeof (CHAR16));\r | |
955 | ASSERT (StringBuffer2 != NULL);\r | |
956 | StrCpy (StringBuffer1, L"Configuration changed. Reset to apply it Now ? ");\r | |
957 | StrCpy (StringBuffer2, L"Enter (YES) / Esc (NO)");\r | |
958 | //\r | |
959 | // Popup a menu to notice user\r | |
960 | //\r | |
961 | do {\r | |
962 | IfrLibCreatePopUp (2, &Key, StringBuffer1, StringBuffer2);\r | |
963 | } while ((Key.ScanCode != SCAN_ESC) && (Key.UnicodeChar != CHAR_CARRIAGE_RETURN));\r | |
964 | \r | |
676df92c | 965 | FreePool (StringBuffer1);\r |
966 | FreePool (StringBuffer2);\r | |
897f0eee | 967 | //\r |
968 | // If the user hits the YES Response key, reset\r | |
969 | //\r | |
970 | if ((Key.UnicodeChar == CHAR_CARRIAGE_RETURN)) {\r | |
971 | gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r | |
972 | }\r | |
973 | gST->ConOut->ClearScreen (gST->ConOut);\r | |
974 | }\r | |
975 | }\r | |
976 | }\r | |
977 | \r | |
897f0eee | 978 | /**\r |
262b0668 | 979 | Get the headers (dos, image, optional header) from an image\r |
897f0eee | 980 | \r |
981 | @param Device SimpleFileSystem device handle\r | |
982 | @param FileName File name for the image\r | |
983 | @param DosHeader Pointer to dos header\r | |
262b0668 | 984 | @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r |
897f0eee | 985 | \r |
986 | @retval EFI_SUCCESS Successfully get the machine type.\r | |
987 | @retval EFI_NOT_FOUND The file is not found.\r | |
988 | @retval EFI_LOAD_ERROR File is not a valid image file.\r | |
989 | \r | |
990 | **/\r | |
991 | EFI_STATUS\r | |
992 | EFIAPI\r | |
993 | BdsLibGetImageHeader (\r | |
994 | IN EFI_HANDLE Device,\r | |
995 | IN CHAR16 *FileName,\r | |
996 | OUT EFI_IMAGE_DOS_HEADER *DosHeader,\r | |
997 | OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r | |
998 | )\r | |
999 | {\r | |
1000 | EFI_STATUS Status;\r | |
1001 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r | |
1002 | EFI_FILE_HANDLE Root;\r | |
1003 | EFI_FILE_HANDLE ThisFile;\r | |
1004 | UINTN BufferSize;\r | |
1005 | UINT64 FileSize;\r | |
1006 | EFI_FILE_INFO *Info;\r | |
1007 | \r | |
1008 | Root = NULL;\r | |
1009 | ThisFile = NULL;\r | |
1010 | //\r | |
1011 | // Handle the file system interface to the device\r | |
1012 | //\r | |
1013 | Status = gBS->HandleProtocol (\r | |
1014 | Device,\r | |
1015 | &gEfiSimpleFileSystemProtocolGuid,\r | |
1016 | (VOID *) &Volume\r | |
1017 | );\r | |
1018 | if (EFI_ERROR (Status)) {\r | |
1019 | goto Done;\r | |
1020 | }\r | |
1021 | \r | |
1022 | Status = Volume->OpenVolume (\r | |
1023 | Volume,\r | |
1024 | &Root\r | |
1025 | );\r | |
1026 | if (EFI_ERROR (Status)) {\r | |
676df92c | 1027 | Root = NULL;\r |
897f0eee | 1028 | goto Done;\r |
1029 | }\r | |
1030 | \r | |
1031 | Status = Root->Open (Root, &ThisFile, FileName, EFI_FILE_MODE_READ, 0);\r | |
1032 | if (EFI_ERROR (Status)) {\r | |
1033 | goto Done;\r | |
1034 | }\r | |
1035 | \r | |
1036 | //\r | |
1037 | // Get file size\r | |
1038 | //\r | |
1039 | BufferSize = SIZE_OF_EFI_FILE_INFO + 200;\r | |
1040 | do {\r | |
1041 | Info = NULL;\r | |
1042 | Status = gBS->AllocatePool (EfiBootServicesData, BufferSize, (VOID **) &Info);\r | |
1043 | if (EFI_ERROR (Status)) {\r | |
1044 | goto Done;\r | |
1045 | }\r | |
1046 | Status = ThisFile->GetInfo (\r | |
1047 | ThisFile,\r | |
1048 | &gEfiFileInfoGuid,\r | |
1049 | &BufferSize,\r | |
1050 | Info\r | |
1051 | );\r | |
1052 | if (!EFI_ERROR (Status)) {\r | |
1053 | break;\r | |
1054 | }\r | |
1055 | if (Status != EFI_BUFFER_TOO_SMALL) {\r | |
676df92c | 1056 | FreePool (Info);\r |
897f0eee | 1057 | goto Done;\r |
1058 | }\r | |
676df92c | 1059 | FreePool (Info);\r |
897f0eee | 1060 | } while (TRUE);\r |
1061 | \r | |
1062 | FileSize = Info->FileSize;\r | |
676df92c | 1063 | FreePool (Info);\r |
897f0eee | 1064 | \r |
1065 | //\r | |
1066 | // Read dos header\r | |
1067 | //\r | |
1068 | BufferSize = sizeof (EFI_IMAGE_DOS_HEADER);\r | |
1069 | Status = ThisFile->Read (ThisFile, &BufferSize, DosHeader);\r | |
1070 | if (EFI_ERROR (Status) ||\r | |
1071 | BufferSize < sizeof (EFI_IMAGE_DOS_HEADER) ||\r | |
1072 | FileSize <= DosHeader->e_lfanew ||\r | |
1073 | DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r | |
1074 | Status = EFI_LOAD_ERROR;\r | |
1075 | goto Done;\r | |
1076 | }\r | |
1077 | \r | |
1078 | //\r | |
1079 | // Move to PE signature\r | |
1080 | //\r | |
1081 | Status = ThisFile->SetPosition (ThisFile, DosHeader->e_lfanew);\r | |
1082 | if (EFI_ERROR (Status)) {\r | |
1083 | Status = EFI_LOAD_ERROR;\r | |
1084 | goto Done;\r | |
1085 | }\r | |
1086 | \r | |
1087 | //\r | |
1088 | // Read and check PE signature\r | |
1089 | //\r | |
1090 | BufferSize = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r | |
1091 | Status = ThisFile->Read (ThisFile, &BufferSize, Hdr.Pe32);\r | |
1092 | if (EFI_ERROR (Status) ||\r | |
1093 | BufferSize < sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION) ||\r | |
1094 | Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r | |
1095 | Status = EFI_LOAD_ERROR;\r | |
1096 | goto Done;\r | |
1097 | }\r | |
1098 | \r | |
1099 | //\r | |
1100 | // Check PE32 or PE32+ magic\r | |
1101 | //\r | |
1102 | if (Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC &&\r | |
1103 | Hdr.Pe32->OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r | |
1104 | Status = EFI_LOAD_ERROR;\r | |
1105 | goto Done;\r | |
1106 | }\r | |
1107 | \r | |
1108 | Done:\r | |
1109 | if (ThisFile != NULL) {\r | |
1110 | ThisFile->Close (ThisFile);\r | |
1111 | }\r | |
1112 | if (Root != NULL) {\r | |
1113 | Root->Close (Root);\r | |
1114 | }\r | |
1115 | return Status;\r | |
1116 | }\r | |
1117 | \r | |
11ef23f9 | 1118 | /**\r |
897f0eee | 1119 | \r |
1120 | This routine is a notification function for legayc boot or exit boot\r | |
1121 | service event. It will adjust the memory information for different\r | |
11ef23f9 | 1122 | memory type and save them into the variables for next boot.\r |
897f0eee | 1123 | \r |
897f0eee | 1124 | \r |
11ef23f9 | 1125 | @param Event The event that triggered this notification function.\r |
1126 | @param Context Pointer to the notification functions context.\r | |
897f0eee | 1127 | \r |
11ef23f9 | 1128 | **/\r |
1129 | VOID\r | |
1130 | EFIAPI\r | |
1131 | BdsSetMemoryTypeInformationVariable (\r | |
1132 | EFI_EVENT Event,\r | |
1133 | VOID *Context\r | |
1134 | )\r | |
897f0eee | 1135 | {\r |
1136 | EFI_STATUS Status;\r | |
1137 | EFI_MEMORY_TYPE_INFORMATION *PreviousMemoryTypeInformation;\r | |
1138 | EFI_MEMORY_TYPE_INFORMATION *CurrentMemoryTypeInformation;\r | |
1139 | UINTN VariableSize;\r | |
1140 | BOOLEAN UpdateRequired;\r | |
1141 | UINTN Index;\r | |
1142 | UINTN Index1;\r | |
1143 | UINT32 Previous;\r | |
1144 | UINT32 Current;\r | |
1145 | UINT32 Next;\r | |
1146 | EFI_HOB_GUID_TYPE *GuidHob;\r | |
1147 | \r | |
1148 | UpdateRequired = FALSE;\r | |
1149 | \r | |
1150 | //\r | |
1151 | // Retrieve the current memory usage statistics. If they are not found, then\r | |
1152 | // no adjustments can be made to the Memory Type Information variable.\r | |
1153 | //\r | |
1154 | Status = EfiGetSystemConfigurationTable (\r | |
1155 | &gEfiMemoryTypeInformationGuid,\r | |
1156 | (VOID **) &CurrentMemoryTypeInformation\r | |
1157 | );\r | |
1158 | if (EFI_ERROR (Status)) {\r | |
1159 | return;\r | |
1160 | }\r | |
1161 | \r | |
1162 | //\r | |
1163 | // Get the Memory Type Information settings from Hob if they exist,\r | |
1164 | // PEI is responsible for getting them from variable and build a Hob to save them.\r | |
1165 | // If the previous Memory Type Information is not available, then set defaults\r | |
1166 | //\r | |
1167 | GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r | |
1168 | if (GuidHob == NULL) {\r | |
1169 | //\r | |
1170 | // If Platform has not built Memory Type Info into the Hob, just return.\r | |
1171 | //\r | |
1172 | return;\r | |
1173 | }\r | |
1174 | PreviousMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r | |
1175 | VariableSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r | |
1176 | \r | |
1177 | //\r | |
1178 | // Use a heuristic to adjust the Memory Type Information for the next boot\r | |
1179 | //\r | |
1180 | for (Index = 0; PreviousMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
1181 | \r | |
1182 | Current = 0;\r | |
1183 | for (Index1 = 0; CurrentMemoryTypeInformation[Index1].Type != EfiMaxMemoryType; Index1++) {\r | |
1184 | if (PreviousMemoryTypeInformation[Index].Type == CurrentMemoryTypeInformation[Index1].Type) {\r | |
1185 | Current = CurrentMemoryTypeInformation[Index1].NumberOfPages;\r | |
1186 | break;\r | |
1187 | }\r | |
1188 | }\r | |
1189 | \r | |
1190 | if (CurrentMemoryTypeInformation[Index1].Type == EfiMaxMemoryType) {\r | |
1191 | continue;\r | |
1192 | }\r | |
1193 | \r | |
1194 | Previous = PreviousMemoryTypeInformation[Index].NumberOfPages;\r | |
1195 | \r | |
1196 | //\r | |
1197 | // Write next varible to 125% * current and Inconsistent Memory Reserved across bootings may lead to S4 fail\r | |
1198 | //\r | |
1199 | if (Current > Previous) {\r | |
1200 | Next = Current + (Current >> 2);\r | |
1201 | } else {\r | |
1202 | Next = Previous;\r | |
1203 | }\r | |
1204 | if (Next > 0 && Next < 4) {\r | |
1205 | Next = 4;\r | |
1206 | }\r | |
1207 | \r | |
1208 | if (Next != Previous) {\r | |
1209 | PreviousMemoryTypeInformation[Index].NumberOfPages = Next;\r | |
1210 | UpdateRequired = TRUE;\r | |
1211 | }\r | |
1212 | \r | |
1213 | }\r | |
1214 | \r | |
1215 | //\r | |
1216 | // If any changes were made to the Memory Type Information settings, then set the new variable value\r | |
1217 | //\r | |
1218 | if (UpdateRequired) {\r | |
1219 | Status = gRT->SetVariable (\r | |
1220 | EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r | |
1221 | &gEfiMemoryTypeInformationGuid,\r | |
1222 | EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r | |
1223 | VariableSize,\r | |
1224 | PreviousMemoryTypeInformation\r | |
1225 | );\r | |
1226 | }\r | |
1227 | \r | |
1228 | return;\r | |
1229 | }\r | |
1230 | \r | |
897f0eee | 1231 | /**\r |
262b0668 | 1232 | This routine register a function to adjust the different type memory page number\r |
1233 | just before booting and save the updated info into the variable for next boot to use.\r | |
897f0eee | 1234 | \r |
1235 | **/\r | |
1236 | VOID\r | |
1237 | EFIAPI\r | |
1238 | BdsLibSaveMemoryTypeInformation (\r | |
1239 | VOID\r | |
1240 | )\r | |
1241 | {\r | |
1242 | EFI_STATUS Status;\r | |
1243 | EFI_EVENT ReadyToBootEvent;\r | |
1244 | \r | |
1245 | Status = EfiCreateEventReadyToBootEx (\r | |
1246 | TPL_CALLBACK,\r | |
1247 | BdsSetMemoryTypeInformationVariable,\r | |
1248 | NULL,\r | |
1249 | &ReadyToBootEvent\r | |
1250 | );\r | |
1251 | if (EFI_ERROR (Status)) {\r | |
1252 | DEBUG ((DEBUG_ERROR,"Bds Set Memory Type Informationa Variable Fails\n"));\r | |
1253 | }\r | |
1254 | \r | |
1255 | }\r | |
1256 | \r | |
1257 | \r |