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