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