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