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