]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
61699edeb892c51df30df8ac2d0f4dd094d864b0
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsBoot.c
1 /** @file
2 BDS Lib functions which relate with create or process the boot option.
3
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 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 #include "String.h"
17
18 BOOLEAN mEnumBootDevice = FALSE;
19 EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;
20
21 ///
22 /// This GUID is used for an EFI Variable that stores the front device pathes
23 /// for a partial device path that starts with the HD node.
24 ///
25 EFI_GUID mHdBootVariablePrivateGuid = { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };
26
27 ///
28 /// This GUID is used for register UNI string.
29 ///
30 EFI_GUID mBdsLibStringPackGuid = { 0x3b4d9b23, 0x95ac, 0x44f6, { 0x9f, 0xcd, 0xe, 0x95, 0x94, 0x58, 0x6c, 0x72 } };
31
32 ///
33 /// This GUID is used for Set/Get platform language into/from variable at last time enumeration to ensure the enumeration will
34 /// only execute once.
35 ///
36 EFI_GUID mBdsLibLastLangGuid = { 0xe8c545b, 0xa2ee, 0x470d, { 0x8e, 0x26, 0xbd, 0xa1, 0xa1, 0x3c, 0xa, 0xa3 } };
37
38 /**
39 The constructor function register UNI strings into imageHandle.
40
41 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
42
43 @param ImageHandle The firmware allocated handle for the EFI image.
44 @param SystemTable A pointer to the EFI System Table.
45
46 @retval EFI_SUCCESS The constructor successfully added string package.
47 @retval Other value The constructor can't add string package.
48
49 **/
50 EFI_STATUS
51 EFIAPI
52 GenericBdsLibConstructor (
53 IN EFI_HANDLE ImageHandle,
54 IN EFI_SYSTEM_TABLE *SystemTable
55 )
56 {
57
58 gBdsLibStringPackHandle = HiiAddPackages (
59 &mBdsLibStringPackGuid,
60 &ImageHandle,
61 GenericBdsLibStrings,
62 NULL
63 );
64
65 ASSERT (gBdsLibStringPackHandle != NULL);
66
67 return EFI_SUCCESS;
68 }
69
70
71
72 /**
73 Boot the legacy system with the boot option
74
75 @param Option The legacy boot option which have BBS device path
76
77 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
78 legacy boot.
79 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
80
81 **/
82 EFI_STATUS
83 BdsLibDoLegacyBoot (
84 IN BDS_COMMON_OPTION *Option
85 )
86 {
87 EFI_STATUS Status;
88 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
89
90 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
91 if (EFI_ERROR (Status)) {
92 //
93 // If no LegacyBios protocol we do not support legacy boot
94 //
95 return EFI_UNSUPPORTED;
96 }
97 //
98 // Notes: if we separate the int 19, then we don't need to refresh BBS
99 //
100 BdsRefreshBbsTableForBoot (Option);
101
102 //
103 // Write boot to OS performance data for legacy boot.
104 //
105 PERF_CODE (
106 WriteBootToOsPerformanceData ();
107 );
108
109 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));
110 return LegacyBios->LegacyBoot (
111 LegacyBios,
112 (BBS_BBS_DEVICE_PATH *) Option->DevicePath,
113 Option->LoadOptionsSize,
114 Option->LoadOptions
115 );
116 }
117
118 /**
119 Internal function to check if the input boot option is a valid EFI NV Boot####.
120
121 @param OptionToCheck Boot option to be checked.
122
123 @retval TRUE This boot option matches a valid EFI NV Boot####.
124 @retval FALSE If not.
125
126 **/
127 BOOLEAN
128 IsBootOptionValidNVVarialbe (
129 IN BDS_COMMON_OPTION *OptionToCheck
130 )
131 {
132 LIST_ENTRY TempList;
133 BDS_COMMON_OPTION *BootOption;
134 BOOLEAN Valid;
135 CHAR16 OptionName[20];
136
137 Valid = FALSE;
138
139 InitializeListHead (&TempList);
140 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);
141
142 BootOption = BdsLibVariableToOption (&TempList, OptionName);
143 if (BootOption == NULL) {
144 return FALSE;
145 }
146
147 //
148 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
149 // valid EFI NV Boot####.
150 //
151 if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&
152 (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))
153 {
154 Valid = TRUE;
155 }
156
157 FreePool (BootOption);
158
159 return Valid;
160 }
161 /**
162 Process the boot option follow the UEFI specification and
163 special treat the legacy boot option with BBS_DEVICE_PATH.
164
165 @param Option The boot option need to be processed
166 @param DevicePath The device path which describe where to load the
167 boot image or the legacy BBS device path to boot
168 the legacy OS
169 @param ExitDataSize The size of exit data.
170 @param ExitData Data returned when Boot image failed.
171
172 @retval EFI_SUCCESS Boot from the input boot option successfully.
173 @retval EFI_NOT_FOUND If the Device Path is not found in the system
174
175 **/
176 EFI_STATUS
177 EFIAPI
178 BdsLibBootViaBootOption (
179 IN BDS_COMMON_OPTION *Option,
180 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
181 OUT UINTN *ExitDataSize,
182 OUT CHAR16 **ExitData OPTIONAL
183 )
184 {
185 EFI_STATUS Status;
186 EFI_HANDLE Handle;
187 EFI_HANDLE ImageHandle;
188 EFI_DEVICE_PATH_PROTOCOL *FilePath;
189 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
190 EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;
191 EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;
192 LIST_ENTRY TempBootLists;
193
194 //
195 // Record the performance data for End of BDS
196 //
197 PERF_END(NULL, "BDS", NULL, 0);
198
199 *ExitDataSize = 0;
200 *ExitData = NULL;
201
202 //
203 // Notes: this code can be remove after the s3 script table
204 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
205 // EVT_SIGNAL_LEGACY_BOOT
206 //
207 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);
208 if (!EFI_ERROR (Status)) {
209 AcpiS3Save->S3Save (AcpiS3Save, NULL);
210 }
211 //
212 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
213 // full device path
214 //
215 WorkingDevicePath = NULL;
216 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
217 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {
218 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (
219 (HARDDRIVE_DEVICE_PATH *)DevicePath
220 );
221 if (WorkingDevicePath != NULL) {
222 DevicePath = WorkingDevicePath;
223 }
224 }
225 //
226 // Signal the EVT_SIGNAL_READY_TO_BOOT event
227 //
228 EfiSignalEventReadyToBoot();
229
230 //
231 // Adjust the different type memory page number just before booting
232 // and save the updated info into the variable for next boot to use
233 //
234 BdsSetMemoryTypeInformationVariable ();
235
236
237 //
238 // Set Boot Current
239 //
240 if (IsBootOptionValidNVVarialbe (Option)) {
241 //
242 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
243 // In this case, "BootCurrent" is not created.
244 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
245 //
246 gRT->SetVariable (
247 L"BootCurrent",
248 &gEfiGlobalVariableGuid,
249 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
250 sizeof (UINT16),
251 &Option->BootCurrent
252 );
253 }
254
255 ASSERT (Option->DevicePath != NULL);
256 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&
257 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)
258 ) {
259 //
260 // Check to see if we should legacy BOOT. If yes then do the legacy boot
261 //
262 return BdsLibDoLegacyBoot (Option);
263 }
264
265 //
266 // If the boot option point to Internal FV shell, make sure it is valid
267 //
268 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));
269 if (!EFI_ERROR(Status)) {
270 if (Option->DevicePath != NULL) {
271 FreePool(Option->DevicePath);
272 }
273 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
274 ASSERT(Option->DevicePath != NULL);
275 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
276 //
277 // Update the shell boot option
278 //
279 InitializeListHead (&TempBootLists);
280 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");
281
282 //
283 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
284 //
285 FreePool (DevicePath);
286 DevicePath = Option->DevicePath;
287 }
288
289 DEBUG_CODE_BEGIN();
290
291 if (Option->Description == NULL) {
292 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));
293 } else {
294 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));
295 }
296
297 DEBUG_CODE_END();
298
299 Status = gBS->LoadImage (
300 TRUE,
301 gImageHandle,
302 DevicePath,
303 NULL,
304 0,
305 &ImageHandle
306 );
307
308 //
309 // If we didn't find an image directly, we need to try as if it is a removable device boot option
310 // and load the image according to the default boot behavior for removable device.
311 //
312 if (EFI_ERROR (Status)) {
313 //
314 // check if there is a bootable removable media could be found in this device path ,
315 // and get the bootable media handle
316 //
317 Handle = BdsLibGetBootableHandle(DevicePath);
318 if (Handle == NULL) {
319 goto Done;
320 }
321 //
322 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
323 // machinename is ia32, ia64, x64, ...
324 //
325 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
326 if (FilePath != NULL) {
327 Status = gBS->LoadImage (
328 TRUE,
329 gImageHandle,
330 FilePath,
331 NULL,
332 0,
333 &ImageHandle
334 );
335 if (EFI_ERROR (Status)) {
336 //
337 // The DevicePath failed, and it's not a valid
338 // removable media device.
339 //
340 goto Done;
341 }
342 }
343 }
344
345 if (EFI_ERROR (Status)) {
346 //
347 // It there is any error from the Boot attempt exit now.
348 //
349 goto Done;
350 }
351 //
352 // Provide the image with it's load options
353 //
354 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
355 ASSERT_EFI_ERROR (Status);
356
357 if (Option->LoadOptionsSize != 0) {
358 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
359 ImageInfo->LoadOptions = Option->LoadOptions;
360 }
361 //
362 // Before calling the image, enable the Watchdog Timer for
363 // the 5 Minute period
364 //
365 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
366
367 //
368 // Write boot to OS performance data for UEFI boot
369 //
370 PERF_CODE (
371 WriteBootToOsPerformanceData ();
372 );
373
374 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);
375 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));
376
377 //
378 // Clear the Watchdog Timer after the image returns
379 //
380 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
381
382 Done:
383 //
384 // Clear Boot Current
385 //
386 gRT->SetVariable (
387 L"BootCurrent",
388 &gEfiGlobalVariableGuid,
389 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
390 0,
391 &Option->BootCurrent
392 );
393
394 return Status;
395 }
396
397
398 /**
399 Expand a device path that starts with a hard drive media device path node to be a
400 full device path that includes the full hardware path to the device. We need
401 to do this so it can be booted. As an optimization the front match (the part point
402 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
403 so a connect all is not required on every boot. All successful history device path
404 which point to partition node (the front part) will be saved.
405
406 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
407 drive media device path.
408 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
409 cannot be found.
410
411 **/
412 EFI_DEVICE_PATH_PROTOCOL *
413 EFIAPI
414 BdsExpandPartitionPartialDevicePathToFull (
415 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
416 )
417 {
418 EFI_STATUS Status;
419 UINTN BlockIoHandleCount;
420 EFI_HANDLE *BlockIoBuffer;
421 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
422 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;
423 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
424 UINTN Index;
425 UINTN InstanceNum;
426 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;
427 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
428 UINTN CachedDevicePathSize;
429 BOOLEAN DeviceExist;
430 BOOLEAN NeedAdjust;
431 EFI_DEVICE_PATH_PROTOCOL *Instance;
432 UINTN Size;
433
434 FullDevicePath = NULL;
435 //
436 // Check if there is prestore 'HDDP' variable.
437 // If exist, search the front path which point to partition node in the variable instants.
438 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
439 //
440 CachedDevicePath = BdsLibGetVariableAndSize (
441 L"HDDP",
442 &mHdBootVariablePrivateGuid,
443 &CachedDevicePathSize
444 );
445
446 if (CachedDevicePath != NULL) {
447 TempNewDevicePath = CachedDevicePath;
448 DeviceExist = FALSE;
449 NeedAdjust = FALSE;
450 do {
451 //
452 // Check every instance of the variable
453 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
454 // partial partition boot option. Second, check whether the instance could be connected.
455 //
456 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);
457 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {
458 //
459 // Connect the device path instance, the device path point to hard drive media device path node
460 // e.g. ACPI() /PCI()/ATA()/Partition()
461 //
462 Status = BdsLibConnectDevicePath (Instance);
463 if (!EFI_ERROR (Status)) {
464 DeviceExist = TRUE;
465 break;
466 }
467 }
468 //
469 // Come here means the first instance is not matched
470 //
471 NeedAdjust = TRUE;
472 FreePool(Instance);
473 } while (TempNewDevicePath != NULL);
474
475 if (DeviceExist) {
476 //
477 // Find the matched device path.
478 // Append the file path information from the boot option and return the fully expanded device path.
479 //
480 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
481 FullDevicePath = AppendDevicePath (Instance, DevicePath);
482
483 //
484 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
485 //
486 if (NeedAdjust) {
487 //
488 // First delete the matched instance.
489 //
490 TempNewDevicePath = CachedDevicePath;
491 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );
492 FreePool (TempNewDevicePath);
493
494 //
495 // Second, append the remaining path after the matched instance
496 //
497 TempNewDevicePath = CachedDevicePath;
498 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );
499 FreePool (TempNewDevicePath);
500 //
501 // Save the matching Device Path so we don't need to do a connect all next time
502 //
503 Status = gRT->SetVariable (
504 L"HDDP",
505 &mHdBootVariablePrivateGuid,
506 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
507 GetDevicePathSize (CachedDevicePath),
508 CachedDevicePath
509 );
510 }
511
512 FreePool (Instance);
513 FreePool (CachedDevicePath);
514 return FullDevicePath;
515 }
516 }
517
518 //
519 // If we get here we fail to find or 'HDDP' not exist, and now we need
520 // to search all devices in the system for a matched partition
521 //
522 BdsLibConnectAllDriversToAllControllers ();
523 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);
524 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {
525 //
526 // If there was an error or there are no device handles that support
527 // the BLOCK_IO Protocol, then return.
528 //
529 return NULL;
530 }
531 //
532 // Loop through all the device handles that support the BLOCK_IO Protocol
533 //
534 for (Index = 0; Index < BlockIoHandleCount; Index++) {
535
536 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);
537 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {
538 continue;
539 }
540
541 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {
542 //
543 // Find the matched partition device path
544 //
545 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
546 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);
547
548 //
549 // Save the matched partition device path in 'HDDP' variable
550 //
551 if (CachedDevicePath != NULL) {
552 //
553 // Save the matched partition device path as first instance of 'HDDP' variable
554 //
555 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {
556 TempNewDevicePath = CachedDevicePath;
557 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);
558 FreePool(TempNewDevicePath);
559
560 TempNewDevicePath = CachedDevicePath;
561 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
562 if (TempNewDevicePath != NULL) {
563 FreePool(TempNewDevicePath);
564 }
565 } else {
566 TempNewDevicePath = CachedDevicePath;
567 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
568 FreePool(TempNewDevicePath);
569 }
570 //
571 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
572 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
573 //
574 InstanceNum = 0;
575 ASSERT (CachedDevicePath != NULL);
576 TempNewDevicePath = CachedDevicePath;
577 while (!IsDevicePathEnd (TempNewDevicePath)) {
578 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
579 //
580 // Parse one instance
581 //
582 while (!IsDevicePathEndType (TempNewDevicePath)) {
583 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
584 }
585 InstanceNum++;
586 //
587 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
588 //
589 if (InstanceNum >= 12) {
590 SetDevicePathEndNode (TempNewDevicePath);
591 break;
592 }
593 }
594 } else {
595 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);
596 }
597
598 //
599 // Save the matching Device Path so we don't need to do a connect all next time
600 //
601 Status = gRT->SetVariable (
602 L"HDDP",
603 &mHdBootVariablePrivateGuid,
604 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
605 GetDevicePathSize (CachedDevicePath),
606 CachedDevicePath
607 );
608
609 break;
610 }
611 }
612
613 if (CachedDevicePath != NULL) {
614 FreePool (CachedDevicePath);
615 }
616 if (BlockIoBuffer != NULL) {
617 FreePool (BlockIoBuffer);
618 }
619 return FullDevicePath;
620 }
621
622 /**
623 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
624 instances, has the same partition node with HardDriveDevicePath device path
625
626 @param BlockIoDevicePath Multi device path instances which need to check
627 @param HardDriveDevicePath A device path which starts with a hard drive media
628 device path.
629
630 @retval TRUE There is a matched device path instance.
631 @retval FALSE There is no matched device path instance.
632
633 **/
634 BOOLEAN
635 EFIAPI
636 MatchPartitionDevicePathNode (
637 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,
638 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
639 )
640 {
641 HARDDRIVE_DEVICE_PATH *TmpHdPath;
642 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
643 BOOLEAN Match;
644 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;
645
646 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {
647 return FALSE;
648 }
649
650 //
651 // Make PreviousDevicePath == the device path node before the end node
652 //
653 DevicePath = BlockIoDevicePath;
654 BlockIoHdDevicePathNode = NULL;
655
656 //
657 // find the partition device path node
658 //
659 while (!IsDevicePathEnd (DevicePath)) {
660 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
661 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)
662 ) {
663 BlockIoHdDevicePathNode = DevicePath;
664 break;
665 }
666
667 DevicePath = NextDevicePathNode (DevicePath);
668 }
669
670 if (BlockIoHdDevicePathNode == NULL) {
671 return FALSE;
672 }
673 //
674 // See if the harddrive device path in blockio matches the orig Hard Drive Node
675 //
676 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;
677 Match = FALSE;
678
679 //
680 // Check for the match
681 //
682 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&
683 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {
684 switch (TmpHdPath->SignatureType) {
685 case SIGNATURE_TYPE_GUID:
686 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);
687 break;
688 case SIGNATURE_TYPE_MBR:
689 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));
690 break;
691 default:
692 Match = FALSE;
693 break;
694 }
695 }
696
697 return Match;
698 }
699
700 /**
701 Delete the boot option associated with the handle passed in.
702
703 @param Handle The handle which present the device path to create
704 boot option
705
706 @retval EFI_SUCCESS Delete the boot option success
707 @retval EFI_NOT_FOUND If the Device Path is not found in the system
708 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
709 @retval Other Error return value from SetVariable()
710
711 **/
712 EFI_STATUS
713 BdsLibDeleteOptionFromHandle (
714 IN EFI_HANDLE Handle
715 )
716 {
717 UINT16 *BootOrder;
718 UINT8 *BootOptionVar;
719 UINTN BootOrderSize;
720 UINTN BootOptionSize;
721 EFI_STATUS Status;
722 UINTN Index;
723 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
724 UINTN DevicePathSize;
725 UINTN OptionDevicePathSize;
726 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
727 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
728 UINT8 *TempPtr;
729
730 Status = EFI_SUCCESS;
731 BootOrder = NULL;
732 BootOrderSize = 0;
733
734 //
735 // Check "BootOrder" variable, if no, means there is no any boot order.
736 //
737 BootOrder = BdsLibGetVariableAndSize (
738 L"BootOrder",
739 &gEfiGlobalVariableGuid,
740 &BootOrderSize
741 );
742 if (BootOrder == NULL) {
743 return EFI_NOT_FOUND;
744 }
745
746 //
747 // Convert device handle to device path protocol instance
748 //
749 DevicePath = DevicePathFromHandle (Handle);
750 if (DevicePath == NULL) {
751 return EFI_NOT_FOUND;
752 }
753 DevicePathSize = GetDevicePathSize (DevicePath);
754
755 //
756 // Loop all boot order variable and find the matching device path
757 //
758 Index = 0;
759 while (Index < BootOrderSize / sizeof (UINT16)) {
760 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
761 BootOptionVar = BdsLibGetVariableAndSize (
762 BootOption,
763 &gEfiGlobalVariableGuid,
764 &BootOptionSize
765 );
766
767 if (BootOptionVar == NULL) {
768 FreePool (BootOrder);
769 return EFI_OUT_OF_RESOURCES;
770 }
771
772 TempPtr = BootOptionVar;
773 TempPtr += sizeof (UINT32) + sizeof (UINT16);
774 TempPtr += StrSize ((CHAR16 *) TempPtr);
775 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
776 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);
777
778 //
779 // Check whether the device path match
780 //
781 if ((OptionDevicePathSize == DevicePathSize) &&
782 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {
783 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);
784 FreePool (BootOptionVar);
785 break;
786 }
787
788 FreePool (BootOptionVar);
789 Index++;
790 }
791
792 //
793 // Adjust number of boot option for "BootOrder" variable.
794 //
795 Status = gRT->SetVariable (
796 L"BootOrder",
797 &gEfiGlobalVariableGuid,
798 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
799 BootOrderSize,
800 BootOrder
801 );
802
803 FreePool (BootOrder);
804
805 return Status;
806 }
807
808
809 /**
810 Delete all invalid EFI boot options.
811
812 @retval EFI_SUCCESS Delete all invalid boot option success
813 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
814 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
815 @retval Other Error return value from SetVariable()
816
817 **/
818 EFI_STATUS
819 BdsDeleteAllInvalidEfiBootOption (
820 VOID
821 )
822 {
823 UINT16 *BootOrder;
824 UINT8 *BootOptionVar;
825 UINTN BootOrderSize;
826 UINTN BootOptionSize;
827 EFI_STATUS Status;
828 UINTN Index;
829 UINTN Index2;
830 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
831 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
832 UINT8 *TempPtr;
833 CHAR16 *Description;
834
835 Status = EFI_SUCCESS;
836 BootOrder = NULL;
837 BootOrderSize = 0;
838
839 //
840 // Check "BootOrder" variable firstly, this variable hold the number of boot options
841 //
842 BootOrder = BdsLibGetVariableAndSize (
843 L"BootOrder",
844 &gEfiGlobalVariableGuid,
845 &BootOrderSize
846 );
847 if (NULL == BootOrder) {
848 return EFI_NOT_FOUND;
849 }
850
851 Index = 0;
852 while (Index < BootOrderSize / sizeof (UINT16)) {
853 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
854 BootOptionVar = BdsLibGetVariableAndSize (
855 BootOption,
856 &gEfiGlobalVariableGuid,
857 &BootOptionSize
858 );
859 if (NULL == BootOptionVar) {
860 FreePool (BootOrder);
861 return EFI_OUT_OF_RESOURCES;
862 }
863
864 TempPtr = BootOptionVar;
865 TempPtr += sizeof (UINT32) + sizeof (UINT16);
866 Description = (CHAR16 *) TempPtr;
867 TempPtr += StrSize ((CHAR16 *) TempPtr);
868 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
869
870 //
871 // Skip legacy boot option (BBS boot device)
872 //
873 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&
874 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {
875 FreePool (BootOptionVar);
876 Index++;
877 continue;
878 }
879
880 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {
881 //
882 // Delete this invalid boot option "Boot####"
883 //
884 Status = gRT->SetVariable (
885 BootOption,
886 &gEfiGlobalVariableGuid,
887 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
888 0,
889 NULL
890 );
891 //
892 // Mark this boot option in boot order as deleted
893 //
894 BootOrder[Index] = 0xffff;
895 }
896
897 FreePool (BootOptionVar);
898 Index++;
899 }
900
901 //
902 // Adjust boot order array
903 //
904 Index2 = 0;
905 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
906 if (BootOrder[Index] != 0xffff) {
907 BootOrder[Index2] = BootOrder[Index];
908 Index2 ++;
909 }
910 }
911 Status = gRT->SetVariable (
912 L"BootOrder",
913 &gEfiGlobalVariableGuid,
914 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
915 Index2 * sizeof (UINT16),
916 BootOrder
917 );
918
919 FreePool (BootOrder);
920
921 return Status;
922 }
923
924
925 /**
926 For EFI boot option, BDS separate them as six types:
927 1. Network - The boot option points to the SimpleNetworkProtocol device.
928 Bds will try to automatically create this type boot option when enumerate.
929 2. Shell - The boot option points to internal flash shell.
930 Bds will try to automatically create this type boot option when enumerate.
931 3. Removable BlockIo - The boot option only points to the removable media
932 device, like USB flash disk, DVD, Floppy etc.
933 These device should contain a *removable* blockIo
934 protocol in their device handle.
935 Bds will try to automatically create this type boot option
936 when enumerate.
937 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
938 like HardDisk.
939 These device should contain a *fixed* blockIo
940 protocol in their device handle.
941 BDS will skip fixed blockIo devices, and NOT
942 automatically create boot option for them. But BDS
943 will help to delete those fixed blockIo boot option,
944 whose description rule conflict with other auto-created
945 boot options.
946 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
947 has SimpleFileSystem Protocol, but has no blockio
948 protocol. These devices do not offer blockIo
949 protocol, but BDS still can get the
950 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
951 Protocol.
952 6. File - The boot option points to a file. These boot options are usually
953 created by user manually or OS loader. BDS will not delete or modify
954 these boot options.
955
956 This function will enumerate all possible boot device in the system, and
957 automatically create boot options for Network, Shell, Removable BlockIo,
958 and Non-BlockIo Simplefile devices.
959 It will only execute once of every boot.
960
961 @param BdsBootOptionList The header of the link list which indexed all
962 current boot options
963
964 @retval EFI_SUCCESS Finished all the boot device enumerate and create
965 the boot option base on that boot device
966
967 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
968 **/
969 EFI_STATUS
970 EFIAPI
971 BdsLibEnumerateAllBootOption (
972 IN OUT LIST_ENTRY *BdsBootOptionList
973 )
974 {
975 EFI_STATUS Status;
976 UINT16 FloppyNumber;
977 UINT16 CdromNumber;
978 UINT16 UsbNumber;
979 UINT16 MiscNumber;
980 UINT16 ScsiNumber;
981 UINT16 NonBlockNumber;
982 UINTN NumberBlockIoHandles;
983 EFI_HANDLE *BlockIoHandles;
984 EFI_BLOCK_IO_PROTOCOL *BlkIo;
985 UINTN Index;
986 UINTN NumOfLoadFileHandles;
987 EFI_HANDLE *LoadFileHandles;
988 UINTN FvHandleCount;
989 EFI_HANDLE *FvHandleBuffer;
990 EFI_FV_FILETYPE Type;
991 UINTN Size;
992 EFI_FV_FILE_ATTRIBUTES Attributes;
993 UINT32 AuthenticationStatus;
994 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
995 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
996 UINTN DevicePathType;
997 CHAR16 Buffer[40];
998 EFI_HANDLE *FileSystemHandles;
999 UINTN NumberFileSystemHandles;
1000 BOOLEAN NeedDelete;
1001 EFI_IMAGE_DOS_HEADER DosHeader;
1002 CHAR8 *PlatLang;
1003 CHAR8 *LastLang;
1004 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
1005 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1006
1007 FloppyNumber = 0;
1008 CdromNumber = 0;
1009 UsbNumber = 0;
1010 MiscNumber = 0;
1011 ScsiNumber = 0;
1012 PlatLang = NULL;
1013 LastLang = NULL;
1014 ZeroMem (Buffer, sizeof (Buffer));
1015
1016 //
1017 // If the boot device enumerate happened, just get the boot
1018 // device from the boot order variable
1019 //
1020 if (mEnumBootDevice) {
1021 LastLang = GetVariable (L"LastEnumLang", &mBdsLibLastLangGuid);
1022 PlatLang = GetEfiGlobalVariable (L"PlatformLang");
1023 ASSERT (PlatLang != NULL);
1024 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {
1025 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
1026 FreePool (LastLang);
1027 FreePool (PlatLang);
1028 return Status;
1029 } else {
1030 Status = gRT->SetVariable (
1031 L"LastEnumLang",
1032 &mBdsLibLastLangGuid,
1033 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1034 AsciiStrSize (PlatLang),
1035 PlatLang
1036 );
1037 ASSERT_EFI_ERROR (Status);
1038
1039 if (LastLang != NULL) {
1040 FreePool (LastLang);
1041 }
1042 FreePool (PlatLang);
1043 }
1044 }
1045
1046 //
1047 // Notes: this dirty code is to get the legacy boot option from the
1048 // BBS table and create to variable as the EFI boot option, it should
1049 // be removed after the CSM can provide legacy boot option directly
1050 //
1051 REFRESH_LEGACY_BOOT_OPTIONS;
1052
1053 //
1054 // Delete invalid boot option
1055 //
1056 BdsDeleteAllInvalidEfiBootOption ();
1057
1058 //
1059 // Parse removable media
1060 //
1061 gBS->LocateHandleBuffer (
1062 ByProtocol,
1063 &gEfiBlockIoProtocolGuid,
1064 NULL,
1065 &NumberBlockIoHandles,
1066 &BlockIoHandles
1067 );
1068
1069 for (Index = 0; Index < NumberBlockIoHandles; Index++) {
1070 Status = gBS->HandleProtocol (
1071 BlockIoHandles[Index],
1072 &gEfiBlockIoProtocolGuid,
1073 (VOID **) &BlkIo
1074 );
1075 if (!EFI_ERROR (Status)) {
1076 if (!BlkIo->Media->RemovableMedia) {
1077 //
1078 // skip the non-removable block devices
1079 //
1080 continue;
1081 }
1082 }
1083 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);
1084 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);
1085
1086 switch (DevicePathType) {
1087 case BDS_EFI_ACPI_FLOPPY_BOOT:
1088 if (FloppyNumber != 0) {
1089 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);
1090 } else {
1091 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));
1092 }
1093 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1094 FloppyNumber++;
1095 break;
1096
1097 //
1098 // Assume a removable SATA device should be the DVD/CD device
1099 //
1100 case BDS_EFI_MESSAGE_ATAPI_BOOT:
1101 case BDS_EFI_MESSAGE_SATA_BOOT:
1102 if (CdromNumber != 0) {
1103 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);
1104 } else {
1105 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));
1106 }
1107 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));
1108 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1109 CdromNumber++;
1110 break;
1111
1112 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:
1113 if (UsbNumber != 0) {
1114 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);
1115 } else {
1116 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));
1117 }
1118 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1119 UsbNumber++;
1120 break;
1121
1122 case BDS_EFI_MESSAGE_SCSI_BOOT:
1123 if (ScsiNumber != 0) {
1124 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);
1125 } else {
1126 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));
1127 }
1128 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1129 ScsiNumber++;
1130 break;
1131
1132 case BDS_EFI_MESSAGE_MISC_BOOT:
1133 if (MiscNumber != 0) {
1134 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);
1135 } else {
1136 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));
1137 }
1138 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1139 MiscNumber++;
1140 break;
1141
1142 default:
1143 break;
1144 }
1145 }
1146
1147 if (NumberBlockIoHandles != 0) {
1148 FreePool (BlockIoHandles);
1149 }
1150
1151 //
1152 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1153 //
1154 NonBlockNumber = 0;
1155 gBS->LocateHandleBuffer (
1156 ByProtocol,
1157 &gEfiSimpleFileSystemProtocolGuid,
1158 NULL,
1159 &NumberFileSystemHandles,
1160 &FileSystemHandles
1161 );
1162 for (Index = 0; Index < NumberFileSystemHandles; Index++) {
1163 Status = gBS->HandleProtocol (
1164 FileSystemHandles[Index],
1165 &gEfiBlockIoProtocolGuid,
1166 (VOID **) &BlkIo
1167 );
1168 if (!EFI_ERROR (Status)) {
1169 //
1170 // Skip if the file system handle supports a BlkIo protocol,
1171 //
1172 continue;
1173 }
1174
1175 //
1176 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1177 // machinename is ia32, ia64, x64, ...
1178 //
1179 Hdr.Union = &HdrData;
1180 NeedDelete = TRUE;
1181 Status = BdsLibGetImageHeader (
1182 FileSystemHandles[Index],
1183 EFI_REMOVABLE_MEDIA_FILE_NAME,
1184 &DosHeader,
1185 Hdr
1186 );
1187 if (!EFI_ERROR (Status) &&
1188 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
1189 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1190 NeedDelete = FALSE;
1191 }
1192
1193 if (NeedDelete) {
1194 //
1195 // No such file or the file is not a EFI application, delete this boot option
1196 //
1197 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);
1198 } else {
1199 if (NonBlockNumber != 0) {
1200 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);
1201 } else {
1202 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));
1203 }
1204 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);
1205 NonBlockNumber++;
1206 }
1207 }
1208
1209 if (NumberFileSystemHandles != 0) {
1210 FreePool (FileSystemHandles);
1211 }
1212
1213 //
1214 // Parse Network Boot Device
1215 //
1216 NumOfLoadFileHandles = 0;
1217 //
1218 // Search Load File protocol for PXE boot option.
1219 //
1220 gBS->LocateHandleBuffer (
1221 ByProtocol,
1222 &gEfiLoadFileProtocolGuid,
1223 NULL,
1224 &NumOfLoadFileHandles,
1225 &LoadFileHandles
1226 );
1227
1228 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {
1229 if (Index != 0) {
1230 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);
1231 } else {
1232 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));
1233 }
1234 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);
1235 }
1236
1237 if (NumOfLoadFileHandles != 0) {
1238 FreePool (LoadFileHandles);
1239 }
1240
1241 //
1242 // Check if we have on flash shell
1243 //
1244 gBS->LocateHandleBuffer (
1245 ByProtocol,
1246 &gEfiFirmwareVolume2ProtocolGuid,
1247 NULL,
1248 &FvHandleCount,
1249 &FvHandleBuffer
1250 );
1251 for (Index = 0; Index < FvHandleCount; Index++) {
1252 gBS->HandleProtocol (
1253 FvHandleBuffer[Index],
1254 &gEfiFirmwareVolume2ProtocolGuid,
1255 (VOID **) &Fv
1256 );
1257
1258 Status = Fv->ReadFile (
1259 Fv,
1260 PcdGetPtr(PcdShellFile),
1261 NULL,
1262 &Size,
1263 &Type,
1264 &Attributes,
1265 &AuthenticationStatus
1266 );
1267 if (EFI_ERROR (Status)) {
1268 //
1269 // Skip if no shell file in the FV
1270 //
1271 continue;
1272 }
1273 //
1274 // Build the shell boot option
1275 //
1276 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);
1277 }
1278
1279 if (FvHandleCount != 0) {
1280 FreePool (FvHandleBuffer);
1281 }
1282 //
1283 // Make sure every boot only have one time
1284 // boot device enumerate
1285 //
1286 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
1287 mEnumBootDevice = TRUE;
1288
1289 return Status;
1290 }
1291
1292 /**
1293 Build the boot option with the handle parsed in
1294
1295 @param Handle The handle which present the device path to create
1296 boot option
1297 @param BdsBootOptionList The header of the link list which indexed all
1298 current boot options
1299 @param String The description of the boot option.
1300
1301 **/
1302 VOID
1303 EFIAPI
1304 BdsLibBuildOptionFromHandle (
1305 IN EFI_HANDLE Handle,
1306 IN LIST_ENTRY *BdsBootOptionList,
1307 IN CHAR16 *String
1308 )
1309 {
1310 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1311
1312 DevicePath = DevicePathFromHandle (Handle);
1313
1314 //
1315 // Create and register new boot option
1316 //
1317 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");
1318 }
1319
1320
1321 /**
1322 Build the on flash shell boot option with the handle parsed in.
1323
1324 @param Handle The handle which present the device path to create
1325 on flash shell boot option
1326 @param BdsBootOptionList The header of the link list which indexed all
1327 current boot options
1328
1329 **/
1330 VOID
1331 EFIAPI
1332 BdsLibBuildOptionFromShell (
1333 IN EFI_HANDLE Handle,
1334 IN OUT LIST_ENTRY *BdsBootOptionList
1335 )
1336 {
1337 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1338 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;
1339
1340 DevicePath = DevicePathFromHandle (Handle);
1341
1342 //
1343 // Build the shell device path
1344 //
1345 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));
1346
1347 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);
1348
1349 //
1350 // Create and register the shell boot option
1351 //
1352 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");
1353
1354 }
1355
1356 /**
1357 Boot from the UEFI spec defined "BootNext" variable.
1358
1359 **/
1360 VOID
1361 EFIAPI
1362 BdsLibBootNext (
1363 VOID
1364 )
1365 {
1366 UINT16 *BootNext;
1367 UINTN BootNextSize;
1368 CHAR16 Buffer[20];
1369 BDS_COMMON_OPTION *BootOption;
1370 LIST_ENTRY TempList;
1371 UINTN ExitDataSize;
1372 CHAR16 *ExitData;
1373
1374 //
1375 // Init the boot option name buffer and temp link list
1376 //
1377 InitializeListHead (&TempList);
1378 ZeroMem (Buffer, sizeof (Buffer));
1379
1380 BootNext = BdsLibGetVariableAndSize (
1381 L"BootNext",
1382 &gEfiGlobalVariableGuid,
1383 &BootNextSize
1384 );
1385
1386 //
1387 // Clear the boot next variable first
1388 //
1389 if (BootNext != NULL) {
1390 gRT->SetVariable (
1391 L"BootNext",
1392 &gEfiGlobalVariableGuid,
1393 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1394 0,
1395 BootNext
1396 );
1397
1398 //
1399 // Start to build the boot option and try to boot
1400 //
1401 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);
1402 BootOption = BdsLibVariableToOption (&TempList, Buffer);
1403 ASSERT (BootOption != NULL);
1404 BdsLibConnectDevicePath (BootOption->DevicePath);
1405 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
1406 }
1407
1408 }
1409
1410 /**
1411 Return the bootable media handle.
1412 First, check the device is connected
1413 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1414 Third, detect the the default boot file in the Media, and return the removable Media handle.
1415
1416 @param DevicePath Device Path to a bootable device
1417
1418 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1419
1420 **/
1421 EFI_HANDLE
1422 EFIAPI
1423 BdsLibGetBootableHandle (
1424 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1425 )
1426 {
1427 EFI_STATUS Status;
1428 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
1429 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
1430 EFI_HANDLE Handle;
1431 EFI_BLOCK_IO_PROTOCOL *BlockIo;
1432 VOID *Buffer;
1433 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1434 UINTN Size;
1435 UINTN TempSize;
1436 EFI_HANDLE ReturnHandle;
1437 EFI_HANDLE *SimpleFileSystemHandles;
1438
1439 UINTN NumberSimpleFileSystemHandles;
1440 UINTN Index;
1441 EFI_IMAGE_DOS_HEADER DosHeader;
1442 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
1443 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1444
1445 UpdatedDevicePath = DevicePath;
1446
1447 //
1448 // Check whether the device is connected
1449 //
1450 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);
1451 if (EFI_ERROR (Status)) {
1452 //
1453 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1454 //
1455 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);
1456 if (EFI_ERROR (Status)) {
1457 //
1458 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1459 //
1460 UpdatedDevicePath = DevicePath;
1461 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
1462 gBS->ConnectController (Handle, NULL, NULL, TRUE);
1463 }
1464 } else {
1465 //
1466 // For removable device boot option, its contained device path only point to the removable device handle,
1467 // should make sure all its children handles (its child partion or media handles) are created and connected.
1468 //
1469 gBS->ConnectController (Handle, NULL, NULL, TRUE);
1470 //
1471 // Get BlockIo protocol and check removable attribute
1472 //
1473 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
1474 //
1475 // Issue a dummy read to the device to check for media change.
1476 // When the removable media is changed, any Block IO read/write will
1477 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1478 // returned. After the Block IO protocol is reinstalled, subsequent
1479 // Block IO read/write will success.
1480 //
1481 Buffer = AllocatePool (BlockIo->Media->BlockSize);
1482 if (Buffer != NULL) {
1483 BlockIo->ReadBlocks (
1484 BlockIo,
1485 BlockIo->Media->MediaId,
1486 0,
1487 BlockIo->Media->BlockSize,
1488 Buffer
1489 );
1490 FreePool(Buffer);
1491 }
1492 }
1493
1494 //
1495 // Detect the the default boot file from removable Media
1496 //
1497
1498 //
1499 // If fail to get bootable handle specified by a USB boot option, the BDS should try to find other bootable device in the same USB bus
1500 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1501 //
1502 DupDevicePath = DuplicateDevicePath (DevicePath);
1503 ASSERT (DupDevicePath != NULL);
1504
1505 UpdatedDevicePath = DupDevicePath;
1506 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
1507 //
1508 // if the resulting device path point to a usb node, and the usb node is a dummy node, should only let device path only point to the previous Pci node
1509 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1510 //
1511 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&
1512 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {
1513 //
1514 // Remove the usb node, let the device path only point to PCI node
1515 //
1516 SetDevicePathEndNode (UpdatedDevicePath);
1517 UpdatedDevicePath = DupDevicePath;
1518 } else {
1519 UpdatedDevicePath = DevicePath;
1520 }
1521
1522 //
1523 // Get the device path size of boot option
1524 //
1525 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
1526 ReturnHandle = NULL;
1527 gBS->LocateHandleBuffer (
1528 ByProtocol,
1529 &gEfiSimpleFileSystemProtocolGuid,
1530 NULL,
1531 &NumberSimpleFileSystemHandles,
1532 &SimpleFileSystemHandles
1533 );
1534 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
1535 //
1536 // Get the device path size of SimpleFileSystem handle
1537 //
1538 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
1539 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
1540 //
1541 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1542 //
1543 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {
1544 //
1545 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1546 // machinename is ia32, ia64, x64, ...
1547 //
1548 Hdr.Union = &HdrData;
1549 Status = BdsLibGetImageHeader (
1550 SimpleFileSystemHandles[Index],
1551 EFI_REMOVABLE_MEDIA_FILE_NAME,
1552 &DosHeader,
1553 Hdr
1554 );
1555 if (!EFI_ERROR (Status) &&
1556 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
1557 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1558 ReturnHandle = SimpleFileSystemHandles[Index];
1559 break;
1560 }
1561 }
1562 }
1563
1564 FreePool(DupDevicePath);
1565
1566 if (SimpleFileSystemHandles != NULL) {
1567 FreePool(SimpleFileSystemHandles);
1568 }
1569
1570 return ReturnHandle;
1571 }
1572
1573 /**
1574 Check to see if the network cable is plugged in. If the DevicePath is not
1575 connected it will be connected.
1576
1577 @param DevicePath Device Path to check
1578
1579 @retval TRUE DevicePath points to an Network that is connected
1580 @retval FALSE DevicePath does not point to a bootable network
1581
1582 **/
1583 BOOLEAN
1584 BdsLibNetworkBootWithMediaPresent (
1585 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1586 )
1587 {
1588 EFI_STATUS Status;
1589 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
1590 EFI_HANDLE Handle;
1591 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
1592 BOOLEAN MediaPresent;
1593 UINT32 InterruptStatus;
1594
1595 MediaPresent = FALSE;
1596
1597 UpdatedDevicePath = DevicePath;
1598 //
1599 // Locate Load File Protocol for PXE boot option first
1600 //
1601 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
1602 if (EFI_ERROR (Status)) {
1603 //
1604 // Device not present so see if we need to connect it
1605 //
1606 Status = BdsLibConnectDevicePath (DevicePath);
1607 if (!EFI_ERROR (Status)) {
1608 //
1609 // This one should work after we did the connect
1610 //
1611 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
1612 }
1613 }
1614
1615 if (!EFI_ERROR (Status)) {
1616 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
1617 if (EFI_ERROR (Status)) {
1618 //
1619 // Failed to open SNP from this handle, try to get SNP from parent handle
1620 //
1621 UpdatedDevicePath = DevicePathFromHandle (Handle);
1622 if (UpdatedDevicePath != NULL) {
1623 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);
1624 if (!EFI_ERROR (Status)) {
1625 //
1626 // SNP handle found, get SNP from it
1627 //
1628 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);
1629 }
1630 }
1631 }
1632
1633 if (!EFI_ERROR (Status)) {
1634 if (Snp->Mode->MediaPresentSupported) {
1635 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {
1636 //
1637 // Invoke Snp->GetStatus() to refresh the media status
1638 //
1639 Snp->GetStatus (Snp, &InterruptStatus, NULL);
1640
1641 //
1642 // In case some one else is using the SNP check to see if it's connected
1643 //
1644 MediaPresent = Snp->Mode->MediaPresent;
1645 } else {
1646 //
1647 // No one is using SNP so we need to Start and Initialize so
1648 // MediaPresent will be valid.
1649 //
1650 Status = Snp->Start (Snp);
1651 if (!EFI_ERROR (Status)) {
1652 Status = Snp->Initialize (Snp, 0, 0);
1653 if (!EFI_ERROR (Status)) {
1654 MediaPresent = Snp->Mode->MediaPresent;
1655 Snp->Shutdown (Snp);
1656 }
1657 Snp->Stop (Snp);
1658 }
1659 }
1660 } else {
1661 MediaPresent = TRUE;
1662 }
1663 }
1664 }
1665
1666 return MediaPresent;
1667 }
1668
1669 /**
1670 For a bootable Device path, return its boot type.
1671
1672 @param DevicePath The bootable device Path to check
1673
1674 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1675 which subtype is MEDIA_HARDDRIVE_DP
1676 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1677 which subtype is MEDIA_CDROM_DP
1678 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1679 which HID is floppy device.
1680 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1681 and its last device path node's subtype is MSG_ATAPI_DP.
1682 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1683 and its last device path node's subtype is MSG_SCSI_DP.
1684 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1685 and its last device path node's subtype is MSG_USB_DP.
1686 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1687 its last device path node point to a message device path node.
1688 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1689 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1690
1691 **/
1692 UINT32
1693 EFIAPI
1694 BdsGetBootTypeFromDevicePath (
1695 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1696 )
1697 {
1698 ACPI_HID_DEVICE_PATH *Acpi;
1699 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1700 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1701 UINT32 BootType;
1702
1703 if (NULL == DevicePath) {
1704 return BDS_EFI_UNSUPPORT;
1705 }
1706
1707 TempDevicePath = DevicePath;
1708
1709 while (!IsDevicePathEndType (TempDevicePath)) {
1710 switch (DevicePathType (TempDevicePath)) {
1711 case BBS_DEVICE_PATH:
1712 return BDS_LEGACY_BBS_BOOT;
1713 case MEDIA_DEVICE_PATH:
1714 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {
1715 return BDS_EFI_MEDIA_HD_BOOT;
1716 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {
1717 return BDS_EFI_MEDIA_CDROM_BOOT;
1718 }
1719 break;
1720 case ACPI_DEVICE_PATH:
1721 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;
1722 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {
1723 return BDS_EFI_ACPI_FLOPPY_BOOT;
1724 }
1725 break;
1726 case MESSAGING_DEVICE_PATH:
1727 //
1728 // Get the last device path node
1729 //
1730 LastDeviceNode = NextDevicePathNode (TempDevicePath);
1731 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {
1732 //
1733 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1734 // skip it
1735 //
1736 LastDeviceNode = NextDevicePathNode (LastDeviceNode);
1737 }
1738 //
1739 // if the device path not only point to driver device, it is not a messaging device path,
1740 //
1741 if (!IsDevicePathEndType (LastDeviceNode)) {
1742 break;
1743 }
1744
1745 switch (DevicePathSubType (TempDevicePath)) {
1746 case MSG_ATAPI_DP:
1747 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;
1748 break;
1749
1750 case MSG_USB_DP:
1751 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;
1752 break;
1753
1754 case MSG_SCSI_DP:
1755 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;
1756 break;
1757
1758 case MSG_SATA_DP:
1759 BootType = BDS_EFI_MESSAGE_SATA_BOOT;
1760 break;
1761
1762 case MSG_MAC_ADDR_DP:
1763 case MSG_VLAN_DP:
1764 case MSG_IPv4_DP:
1765 case MSG_IPv6_DP:
1766 BootType = BDS_EFI_MESSAGE_MAC_BOOT;
1767 break;
1768
1769 default:
1770 BootType = BDS_EFI_MESSAGE_MISC_BOOT;
1771 break;
1772 }
1773 return BootType;
1774
1775 default:
1776 break;
1777 }
1778 TempDevicePath = NextDevicePathNode (TempDevicePath);
1779 }
1780
1781 return BDS_EFI_UNSUPPORT;
1782 }
1783
1784 /**
1785 Check whether the Device path in a boot option point to a valid bootable device,
1786 And if CheckMedia is true, check the device is ready to boot now.
1787
1788 @param DevPath the Device path in a boot option
1789 @param CheckMedia if true, check the device is ready to boot now.
1790
1791 @retval TRUE the Device path is valid
1792 @retval FALSE the Device path is invalid .
1793
1794 **/
1795 BOOLEAN
1796 EFIAPI
1797 BdsLibIsValidEFIBootOptDevicePath (
1798 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
1799 IN BOOLEAN CheckMedia
1800 )
1801 {
1802 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);
1803 }
1804
1805 /**
1806 Check whether the Device path in a boot option point to a valid bootable device,
1807 And if CheckMedia is true, check the device is ready to boot now.
1808 If Description is not NULL and the device path point to a fixed BlockIo
1809 device, check the description whether conflict with other auto-created
1810 boot options.
1811
1812 @param DevPath the Device path in a boot option
1813 @param CheckMedia if true, check the device is ready to boot now.
1814 @param Description the description in a boot option
1815
1816 @retval TRUE the Device path is valid
1817 @retval FALSE the Device path is invalid .
1818
1819 **/
1820 BOOLEAN
1821 EFIAPI
1822 BdsLibIsValidEFIBootOptDevicePathExt (
1823 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
1824 IN BOOLEAN CheckMedia,
1825 IN CHAR16 *Description
1826 )
1827 {
1828 EFI_STATUS Status;
1829 EFI_HANDLE Handle;
1830 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1831 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1832 EFI_BLOCK_IO_PROTOCOL *BlockIo;
1833
1834 TempDevicePath = DevPath;
1835 LastDeviceNode = DevPath;
1836
1837 //
1838 // Check if it's a valid boot option for network boot device.
1839 // Check if there is EfiLoadFileProtocol installed.
1840 // If yes, that means there is a boot option for network.
1841 //
1842 Status = gBS->LocateDevicePath (
1843 &gEfiLoadFileProtocolGuid,
1844 &TempDevicePath,
1845 &Handle
1846 );
1847 if (EFI_ERROR (Status)) {
1848 //
1849 // Device not present so see if we need to connect it
1850 //
1851 TempDevicePath = DevPath;
1852 BdsLibConnectDevicePath (TempDevicePath);
1853 Status = gBS->LocateDevicePath (
1854 &gEfiLoadFileProtocolGuid,
1855 &TempDevicePath,
1856 &Handle
1857 );
1858 }
1859
1860 if (!EFI_ERROR (Status)) {
1861 if (!IsDevicePathEnd (TempDevicePath)) {
1862 //
1863 // LoadFile protocol is not installed on handle with exactly the same DevPath
1864 //
1865 return FALSE;
1866 }
1867
1868 if (CheckMedia) {
1869 //
1870 // Test if it is ready to boot now
1871 //
1872 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {
1873 return TRUE;
1874 }
1875 } else {
1876 return TRUE;
1877 }
1878 }
1879
1880 //
1881 // If the boot option point to a file, it is a valid EFI boot option,
1882 // and assume it is ready to boot now
1883 //
1884 while (!IsDevicePathEnd (TempDevicePath)) {
1885 LastDeviceNode = TempDevicePath;
1886 TempDevicePath = NextDevicePathNode (TempDevicePath);
1887 }
1888 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&
1889 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {
1890 return TRUE;
1891 }
1892
1893 //
1894 // Check if it's a valid boot option for internal Shell
1895 //
1896 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {
1897 //
1898 // If the boot option point to Internal FV shell, make sure it is valid
1899 //
1900 TempDevicePath = DevPath;
1901 Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, PcdGetPtr(PcdShellFile));
1902 if (Status == EFI_ALREADY_STARTED) {
1903 return TRUE;
1904 } else {
1905 if (Status == EFI_SUCCESS) {
1906 FreePool (TempDevicePath);
1907 }
1908 return FALSE;
1909 }
1910 }
1911
1912 //
1913 // If the boot option point to a blockIO device:
1914 // if it is a removable blockIo device, it is valid.
1915 // if it is a fixed blockIo device, check its description confliction.
1916 //
1917 TempDevicePath = DevPath;
1918 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
1919 if (EFI_ERROR (Status)) {
1920 //
1921 // Device not present so see if we need to connect it
1922 //
1923 Status = BdsLibConnectDevicePath (DevPath);
1924 if (!EFI_ERROR (Status)) {
1925 //
1926 // Try again to get the Block Io protocol after we did the connect
1927 //
1928 TempDevicePath = DevPath;
1929 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
1930 }
1931 }
1932
1933 if (!EFI_ERROR (Status)) {
1934 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
1935 if (!EFI_ERROR (Status)) {
1936 if (CheckMedia) {
1937 //
1938 // Test if it is ready to boot now
1939 //
1940 if (BdsLibGetBootableHandle (DevPath) != NULL) {
1941 return TRUE;
1942 }
1943 } else {
1944 return TRUE;
1945 }
1946 }
1947 } else {
1948 //
1949 // if the boot option point to a simple file protocol which does not consume block Io protocol, it is also a valid EFI boot option,
1950 //
1951 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);
1952 if (!EFI_ERROR (Status)) {
1953 if (CheckMedia) {
1954 //
1955 // Test if it is ready to boot now
1956 //
1957 if (BdsLibGetBootableHandle (DevPath) != NULL) {
1958 return TRUE;
1959 }
1960 } else {
1961 return TRUE;
1962 }
1963 }
1964 }
1965
1966 return FALSE;
1967 }
1968
1969
1970 /**
1971 According to a file guild, check a Fv file device path is valid. If it is invalid,
1972 try to return the valid device path.
1973 FV address maybe changes for memory layout adjust from time to time, use this function
1974 could promise the Fv file device path is right.
1975
1976 @param DevicePath on input, the Fv file device path need to check on
1977 output, the updated valid Fv file device path
1978 @param FileGuid the Fv file guild
1979
1980 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1981 parameter
1982 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1983 guild at all
1984 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1985 valid
1986 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1987 and return the updated device path in DevicePath
1988
1989 **/
1990 EFI_STATUS
1991 EFIAPI
1992 BdsLibUpdateFvFileDevicePath (
1993 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
1994 IN EFI_GUID *FileGuid
1995 )
1996 {
1997 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1998 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1999 EFI_STATUS Status;
2000 EFI_GUID *GuidPoint;
2001 UINTN Index;
2002 UINTN FvHandleCount;
2003 EFI_HANDLE *FvHandleBuffer;
2004 EFI_FV_FILETYPE Type;
2005 UINTN Size;
2006 EFI_FV_FILE_ATTRIBUTES Attributes;
2007 UINT32 AuthenticationStatus;
2008 BOOLEAN FindFvFile;
2009 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
2010 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
2011 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;
2012 EFI_HANDLE FoundFvHandle;
2013 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
2014
2015 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
2016 return EFI_INVALID_PARAMETER;
2017 }
2018 if (FileGuid == NULL) {
2019 return EFI_INVALID_PARAMETER;
2020 }
2021
2022 //
2023 // Check whether the device path point to the default the input Fv file
2024 //
2025 TempDevicePath = *DevicePath;
2026 LastDeviceNode = TempDevicePath;
2027 while (!IsDevicePathEnd (TempDevicePath)) {
2028 LastDeviceNode = TempDevicePath;
2029 TempDevicePath = NextDevicePathNode (TempDevicePath);
2030 }
2031 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
2032 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
2033 );
2034 if (GuidPoint == NULL) {
2035 //
2036 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2037 //
2038 return EFI_UNSUPPORTED;
2039 }
2040 if (!CompareGuid (GuidPoint, FileGuid)) {
2041 //
2042 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2043 //
2044 return EFI_UNSUPPORTED;
2045 }
2046
2047 //
2048 // Check whether the input Fv file device path is valid
2049 //
2050 TempDevicePath = *DevicePath;
2051 FoundFvHandle = NULL;
2052 Status = gBS->LocateDevicePath (
2053 &gEfiFirmwareVolume2ProtocolGuid,
2054 &TempDevicePath,
2055 &FoundFvHandle
2056 );
2057 if (!EFI_ERROR (Status)) {
2058 Status = gBS->HandleProtocol (
2059 FoundFvHandle,
2060 &gEfiFirmwareVolume2ProtocolGuid,
2061 (VOID **) &Fv
2062 );
2063 if (!EFI_ERROR (Status)) {
2064 //
2065 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2066 //
2067 Status = Fv->ReadFile (
2068 Fv,
2069 FileGuid,
2070 NULL,
2071 &Size,
2072 &Type,
2073 &Attributes,
2074 &AuthenticationStatus
2075 );
2076 if (!EFI_ERROR (Status)) {
2077 return EFI_ALREADY_STARTED;
2078 }
2079 }
2080 }
2081
2082 //
2083 // Look for the input wanted FV file in current FV
2084 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2085 //
2086 FindFvFile = FALSE;
2087 FoundFvHandle = NULL;
2088 Status = gBS->HandleProtocol (
2089 gImageHandle,
2090 &gEfiLoadedImageProtocolGuid,
2091 (VOID **) &LoadedImage
2092 );
2093 if (!EFI_ERROR (Status)) {
2094 Status = gBS->HandleProtocol (
2095 LoadedImage->DeviceHandle,
2096 &gEfiFirmwareVolume2ProtocolGuid,
2097 (VOID **) &Fv
2098 );
2099 if (!EFI_ERROR (Status)) {
2100 Status = Fv->ReadFile (
2101 Fv,
2102 FileGuid,
2103 NULL,
2104 &Size,
2105 &Type,
2106 &Attributes,
2107 &AuthenticationStatus
2108 );
2109 if (!EFI_ERROR (Status)) {
2110 FindFvFile = TRUE;
2111 FoundFvHandle = LoadedImage->DeviceHandle;
2112 }
2113 }
2114 }
2115 //
2116 // Second, if fail to find, try to enumerate all FV
2117 //
2118 if (!FindFvFile) {
2119 FvHandleBuffer = NULL;
2120 gBS->LocateHandleBuffer (
2121 ByProtocol,
2122 &gEfiFirmwareVolume2ProtocolGuid,
2123 NULL,
2124 &FvHandleCount,
2125 &FvHandleBuffer
2126 );
2127 for (Index = 0; Index < FvHandleCount; Index++) {
2128 gBS->HandleProtocol (
2129 FvHandleBuffer[Index],
2130 &gEfiFirmwareVolume2ProtocolGuid,
2131 (VOID **) &Fv
2132 );
2133
2134 Status = Fv->ReadFile (
2135 Fv,
2136 FileGuid,
2137 NULL,
2138 &Size,
2139 &Type,
2140 &Attributes,
2141 &AuthenticationStatus
2142 );
2143 if (EFI_ERROR (Status)) {
2144 //
2145 // Skip if input Fv file not in the FV
2146 //
2147 continue;
2148 }
2149 FindFvFile = TRUE;
2150 FoundFvHandle = FvHandleBuffer[Index];
2151 break;
2152 }
2153
2154 if (FvHandleBuffer != NULL) {
2155 FreePool (FvHandleBuffer);
2156 }
2157 }
2158
2159 if (FindFvFile) {
2160 //
2161 // Build the shell device path
2162 //
2163 NewDevicePath = DevicePathFromHandle (FoundFvHandle);
2164 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
2165 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
2166 *DevicePath = NewDevicePath;
2167 return EFI_SUCCESS;
2168 }
2169 return EFI_NOT_FOUND;
2170 }