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