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