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