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