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