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