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