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