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