]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
1.Change the behavior of BdsBoot, to enumerate Boot Options by EfiLoadFileProtocol...
[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 NumOfLoadFileHandles;
995 EFI_HANDLE *LoadFileHandles;
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 NumOfLoadFileHandles = 0;
1175 //
1176 // Search Load File protocol for PXE boot option.
1177 //
1178 gBS->LocateHandleBuffer (
1179 ByProtocol,
1180 &gEfiLoadFileProtocolGuid,
1181 NULL,
1182 &NumOfLoadFileHandles,
1183 &LoadFileHandles
1184 );
1185
1186 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {
1187 UnicodeSPrint (Buffer, sizeof (Buffer), L"%d", Index);
1188 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);
1189 }
1190
1191 if (NumOfLoadFileHandles != 0) {
1192 FreePool (LoadFileHandles);
1193 }
1194
1195 //
1196 // Check if we have on flash shell
1197 //
1198 gBS->LocateHandleBuffer (
1199 ByProtocol,
1200 &gEfiFirmwareVolume2ProtocolGuid,
1201 NULL,
1202 &FvHandleCount,
1203 &FvHandleBuffer
1204 );
1205 for (Index = 0; Index < FvHandleCount; Index++) {
1206 gBS->HandleProtocol (
1207 FvHandleBuffer[Index],
1208 &gEfiFirmwareVolume2ProtocolGuid,
1209 (VOID **) &Fv
1210 );
1211
1212 Status = Fv->ReadFile (
1213 Fv,
1214 PcdGetPtr(PcdShellFile),
1215 NULL,
1216 &Size,
1217 &Type,
1218 &Attributes,
1219 &AuthenticationStatus
1220 );
1221 if (EFI_ERROR (Status)) {
1222 //
1223 // Skip if no shell file in the FV
1224 //
1225 continue;
1226 }
1227 //
1228 // Build the shell boot option
1229 //
1230 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);
1231 }
1232
1233 if (FvHandleCount != 0) {
1234 FreePool (FvHandleBuffer);
1235 }
1236 //
1237 // Make sure every boot only have one time
1238 // boot device enumerate
1239 //
1240 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
1241 mEnumBootDevice = TRUE;
1242
1243 return Status;
1244 }
1245
1246 /**
1247 Build the boot option with the handle parsed in
1248
1249 @param Handle The handle which present the device path to create
1250 boot option
1251 @param BdsBootOptionList The header of the link list which indexed all
1252 current boot options
1253 @param String The description of the boot option.
1254
1255 **/
1256 VOID
1257 EFIAPI
1258 BdsLibBuildOptionFromHandle (
1259 IN EFI_HANDLE Handle,
1260 IN LIST_ENTRY *BdsBootOptionList,
1261 IN CHAR16 *String
1262 )
1263 {
1264 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1265
1266 DevicePath = DevicePathFromHandle (Handle);
1267
1268 //
1269 // Create and register new boot option
1270 //
1271 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");
1272 }
1273
1274
1275 /**
1276 Build the on flash shell boot option with the handle parsed in.
1277
1278 @param Handle The handle which present the device path to create
1279 on flash shell boot option
1280 @param BdsBootOptionList The header of the link list which indexed all
1281 current boot options
1282
1283 **/
1284 VOID
1285 EFIAPI
1286 BdsLibBuildOptionFromShell (
1287 IN EFI_HANDLE Handle,
1288 IN OUT LIST_ENTRY *BdsBootOptionList
1289 )
1290 {
1291 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1292 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;
1293
1294 DevicePath = DevicePathFromHandle (Handle);
1295
1296 //
1297 // Build the shell device path
1298 //
1299 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));
1300
1301 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);
1302
1303 //
1304 // Create and register the shell boot option
1305 //
1306 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");
1307
1308 }
1309
1310 /**
1311 Boot from the UEFI spec defined "BootNext" variable.
1312
1313 **/
1314 VOID
1315 EFIAPI
1316 BdsLibBootNext (
1317 VOID
1318 )
1319 {
1320 UINT16 *BootNext;
1321 UINTN BootNextSize;
1322 CHAR16 Buffer[20];
1323 BDS_COMMON_OPTION *BootOption;
1324 LIST_ENTRY TempList;
1325 UINTN ExitDataSize;
1326 CHAR16 *ExitData;
1327
1328 //
1329 // Init the boot option name buffer and temp link list
1330 //
1331 InitializeListHead (&TempList);
1332 ZeroMem (Buffer, sizeof (Buffer));
1333
1334 BootNext = BdsLibGetVariableAndSize (
1335 L"BootNext",
1336 &gEfiGlobalVariableGuid,
1337 &BootNextSize
1338 );
1339
1340 //
1341 // Clear the boot next variable first
1342 //
1343 if (BootNext != NULL) {
1344 gRT->SetVariable (
1345 L"BootNext",
1346 &gEfiGlobalVariableGuid,
1347 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1348 0,
1349 BootNext
1350 );
1351
1352 //
1353 // Start to build the boot option and try to boot
1354 //
1355 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);
1356 BootOption = BdsLibVariableToOption (&TempList, Buffer);
1357 ASSERT (BootOption != NULL);
1358 BdsLibConnectDevicePath (BootOption->DevicePath);
1359 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
1360 }
1361
1362 }
1363
1364 /**
1365 Return the bootable media handle.
1366 First, check the device is connected
1367 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1368 Third, detect the the default boot file in the Media, and return the removable Media handle.
1369
1370 @param DevicePath Device Path to a bootable device
1371
1372 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1373
1374 **/
1375 EFI_HANDLE
1376 EFIAPI
1377 BdsLibGetBootableHandle (
1378 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1379 )
1380 {
1381 EFI_STATUS Status;
1382 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
1383 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
1384 EFI_HANDLE Handle;
1385 EFI_BLOCK_IO_PROTOCOL *BlockIo;
1386 VOID *Buffer;
1387 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1388 UINTN Size;
1389 UINTN TempSize;
1390 EFI_HANDLE ReturnHandle;
1391 EFI_HANDLE *SimpleFileSystemHandles;
1392
1393 UINTN NumberSimpleFileSystemHandles;
1394 UINTN Index;
1395 EFI_IMAGE_DOS_HEADER DosHeader;
1396 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
1397 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1398
1399 UpdatedDevicePath = DevicePath;
1400
1401 //
1402 // Check whether the device is connected
1403 //
1404 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);
1405 if (EFI_ERROR (Status)) {
1406 //
1407 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1408 //
1409 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);
1410 if (EFI_ERROR (Status)) {
1411 //
1412 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1413 //
1414 UpdatedDevicePath = DevicePath;
1415 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
1416 gBS->ConnectController (Handle, NULL, NULL, TRUE);
1417 }
1418 } else {
1419 //
1420 // Get BlockIo protocol and check removable attribute
1421 //
1422 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
1423 //
1424 // Issue a dummy read to the device to check for media change.
1425 // When the removable media is changed, any Block IO read/write will
1426 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1427 // returned. After the Block IO protocol is reinstalled, subsequent
1428 // Block IO read/write will success.
1429 //
1430 Buffer = AllocatePool (BlockIo->Media->BlockSize);
1431 if (Buffer != NULL) {
1432 BlockIo->ReadBlocks (
1433 BlockIo,
1434 BlockIo->Media->MediaId,
1435 0,
1436 BlockIo->Media->BlockSize,
1437 Buffer
1438 );
1439 FreePool(Buffer);
1440 }
1441 }
1442
1443 //
1444 // Detect the the default boot file from removable Media
1445 //
1446
1447 //
1448 // 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
1449 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1450 //
1451 DupDevicePath = DuplicateDevicePath (DevicePath);
1452 ASSERT (DupDevicePath != NULL);
1453
1454 UpdatedDevicePath = DupDevicePath;
1455 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
1456 //
1457 // 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
1458 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
1459 //
1460 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&
1461 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {
1462 //
1463 // Remove the usb node, let the device path only point to PCI node
1464 //
1465 SetDevicePathEndNode (UpdatedDevicePath);
1466 UpdatedDevicePath = DupDevicePath;
1467 } else {
1468 UpdatedDevicePath = DevicePath;
1469 }
1470
1471 //
1472 // Get the device path size of boot option
1473 //
1474 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
1475 ReturnHandle = NULL;
1476 gBS->LocateHandleBuffer (
1477 ByProtocol,
1478 &gEfiSimpleFileSystemProtocolGuid,
1479 NULL,
1480 &NumberSimpleFileSystemHandles,
1481 &SimpleFileSystemHandles
1482 );
1483 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
1484 //
1485 // Get the device path size of SimpleFileSystem handle
1486 //
1487 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
1488 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
1489 //
1490 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1491 //
1492 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {
1493 //
1494 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
1495 // machinename is ia32, ia64, x64, ...
1496 //
1497 Hdr.Union = &HdrData;
1498 Status = BdsLibGetImageHeader (
1499 SimpleFileSystemHandles[Index],
1500 EFI_REMOVABLE_MEDIA_FILE_NAME,
1501 &DosHeader,
1502 Hdr
1503 );
1504 if (!EFI_ERROR (Status) &&
1505 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
1506 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1507 ReturnHandle = SimpleFileSystemHandles[Index];
1508 break;
1509 }
1510 }
1511 }
1512
1513 FreePool(DupDevicePath);
1514
1515 if (SimpleFileSystemHandles != NULL) {
1516 FreePool(SimpleFileSystemHandles);
1517 }
1518
1519 return ReturnHandle;
1520 }
1521
1522 /**
1523 Check to see if the network cable is plugged in. If the DevicePath is not
1524 connected it will be connected.
1525
1526 @param DevicePath Device Path to check
1527
1528 @retval TRUE DevicePath points to an Network that is connected
1529 @retval FALSE DevicePath does not point to a bootable network
1530
1531 **/
1532 BOOLEAN
1533 BdsLibNetworkBootWithMediaPresent (
1534 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1535 )
1536 {
1537 EFI_STATUS Status;
1538 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
1539 EFI_HANDLE Handle;
1540 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
1541 BOOLEAN MediaPresent;
1542
1543 MediaPresent = FALSE;
1544
1545 UpdatedDevicePath = DevicePath;
1546 //
1547 // Locate Load File Protocol for PXE boot option first
1548 //
1549 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
1550 if (EFI_ERROR (Status)) {
1551 //
1552 // Device not present so see if we need to connect it
1553 //
1554 Status = BdsLibConnectDevicePath (DevicePath);
1555 if (!EFI_ERROR (Status)) {
1556 //
1557 // This one should work after we did the connect
1558 //
1559 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
1560 }
1561 }
1562
1563 if (!EFI_ERROR (Status)) {
1564 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
1565 if (EFI_ERROR (Status)) {
1566 //
1567 // Failed to open SNP from this handle, try to get SNP from parent handle
1568 //
1569 UpdatedDevicePath = DevicePathFromHandle (Handle);
1570 if (UpdatedDevicePath != NULL) {
1571 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);
1572 if (!EFI_ERROR (Status)) {
1573 //
1574 // SNP handle found, get SNP from it
1575 //
1576 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);
1577 }
1578 }
1579 }
1580
1581 if (!EFI_ERROR (Status)) {
1582 if (Snp->Mode->MediaPresentSupported) {
1583 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {
1584 //
1585 // In case some one else is using the SNP check to see if it's connected
1586 //
1587 MediaPresent = Snp->Mode->MediaPresent;
1588 } else {
1589 //
1590 // No one is using SNP so we need to Start and Initialize so
1591 // MediaPresent will be valid.
1592 //
1593 Status = Snp->Start (Snp);
1594 if (!EFI_ERROR (Status)) {
1595 Status = Snp->Initialize (Snp, 0, 0);
1596 if (!EFI_ERROR (Status)) {
1597 MediaPresent = Snp->Mode->MediaPresent;
1598 Snp->Shutdown (Snp);
1599 }
1600 Snp->Stop (Snp);
1601 }
1602 }
1603 } else {
1604 MediaPresent = TRUE;
1605 }
1606 }
1607 }
1608
1609 return MediaPresent;
1610 }
1611
1612 /**
1613 For a bootable Device path, return its boot type.
1614
1615 @param DevicePath The bootable device Path to check
1616
1617 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1618 which subtype is MEDIA_HARDDRIVE_DP
1619 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
1620 which subtype is MEDIA_CDROM_DP
1621 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
1622 which HID is floppy device.
1623 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1624 and its last device path node's subtype is MSG_ATAPI_DP.
1625 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1626 and its last device path node's subtype is MSG_SCSI_DP.
1627 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
1628 and its last device path node's subtype is MSG_USB_DP.
1629 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
1630 its last device path node point to a message device path node.
1631 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
1632 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
1633
1634 **/
1635 UINT32
1636 EFIAPI
1637 BdsGetBootTypeFromDevicePath (
1638 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1639 )
1640 {
1641 ACPI_HID_DEVICE_PATH *Acpi;
1642 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1643 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1644 UINT32 BootType;
1645
1646 if (NULL == DevicePath) {
1647 return BDS_EFI_UNSUPPORT;
1648 }
1649
1650 TempDevicePath = DevicePath;
1651
1652 while (!IsDevicePathEndType (TempDevicePath)) {
1653 switch (DevicePathType (TempDevicePath)) {
1654 case BBS_DEVICE_PATH:
1655 return BDS_LEGACY_BBS_BOOT;
1656 case MEDIA_DEVICE_PATH:
1657 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {
1658 return BDS_EFI_MEDIA_HD_BOOT;
1659 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {
1660 return BDS_EFI_MEDIA_CDROM_BOOT;
1661 }
1662 break;
1663 case ACPI_DEVICE_PATH:
1664 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;
1665 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {
1666 return BDS_EFI_ACPI_FLOPPY_BOOT;
1667 }
1668 break;
1669 case MESSAGING_DEVICE_PATH:
1670 //
1671 // Get the last device path node
1672 //
1673 LastDeviceNode = NextDevicePathNode (TempDevicePath);
1674 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {
1675 //
1676 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
1677 // skip it
1678 //
1679 LastDeviceNode = NextDevicePathNode (LastDeviceNode);
1680 }
1681 //
1682 // if the device path not only point to driver device, it is not a messaging device path,
1683 //
1684 if (!IsDevicePathEndType (LastDeviceNode)) {
1685 break;
1686 }
1687
1688 switch (DevicePathSubType (TempDevicePath)) {
1689 case MSG_ATAPI_DP:
1690 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;
1691 break;
1692
1693 case MSG_USB_DP:
1694 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;
1695 break;
1696
1697 case MSG_SCSI_DP:
1698 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;
1699 break;
1700
1701 case MSG_SATA_DP:
1702 BootType = BDS_EFI_MESSAGE_SATA_BOOT;
1703 break;
1704
1705 case MSG_MAC_ADDR_DP:
1706 case MSG_VLAN_DP:
1707 case MSG_IPv4_DP:
1708 case MSG_IPv6_DP:
1709 BootType = BDS_EFI_MESSAGE_MAC_BOOT;
1710 break;
1711
1712 default:
1713 BootType = BDS_EFI_MESSAGE_MISC_BOOT;
1714 break;
1715 }
1716 return BootType;
1717
1718 default:
1719 break;
1720 }
1721 TempDevicePath = NextDevicePathNode (TempDevicePath);
1722 }
1723
1724 return BDS_EFI_UNSUPPORT;
1725 }
1726
1727 /**
1728 Check whether the Device path in a boot option point to a valid bootable device,
1729 And if CheckMedia is true, check the device is ready to boot now.
1730
1731 @param DevPath the Device path in a boot option
1732 @param CheckMedia if true, check the device is ready to boot now.
1733
1734 @retval TRUE the Device path is valid
1735 @retval FALSE the Device path is invalid .
1736
1737 **/
1738 BOOLEAN
1739 EFIAPI
1740 BdsLibIsValidEFIBootOptDevicePath (
1741 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
1742 IN BOOLEAN CheckMedia
1743 )
1744 {
1745 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);
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 If Description is not NULL and the device path point to a fixed BlockIo
1752 device, check the description whether conflict with other auto-created
1753 boot options.
1754
1755 @param DevPath the Device path in a boot option
1756 @param CheckMedia if true, check the device is ready to boot now.
1757 @param Description the description in a boot option
1758
1759 @retval TRUE the Device path is valid
1760 @retval FALSE the Device path is invalid .
1761
1762 **/
1763 BOOLEAN
1764 EFIAPI
1765 BdsLibIsValidEFIBootOptDevicePathExt (
1766 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
1767 IN BOOLEAN CheckMedia,
1768 IN CHAR16 *Description
1769 )
1770 {
1771 EFI_STATUS Status;
1772 EFI_HANDLE Handle;
1773 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1774 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1775 EFI_BLOCK_IO_PROTOCOL *BlockIo;
1776
1777 TempDevicePath = DevPath;
1778 LastDeviceNode = DevPath;
1779
1780 //
1781 // Check if it's a valid boot option for network boot device.
1782 // Check if there is EfiLoadFileProtocol installed.
1783 // If yes, that means there is a boot option for network.
1784 //
1785 Status = gBS->LocateDevicePath (
1786 &gEfiLoadFileProtocolGuid,
1787 &TempDevicePath,
1788 &Handle
1789 );
1790 if (EFI_ERROR (Status)) {
1791 //
1792 // Device not present so see if we need to connect it
1793 //
1794 TempDevicePath = DevPath;
1795 BdsLibConnectDevicePath (TempDevicePath);
1796 Status = gBS->LocateDevicePath (
1797 &gEfiLoadFileProtocolGuid,
1798 &TempDevicePath,
1799 &Handle
1800 );
1801 }
1802
1803 if (!EFI_ERROR (Status)) {
1804 if (!IsDevicePathEnd (TempDevicePath)) {
1805 //
1806 // LoadFile protocol is not installed on handle with exactly the same DevPath
1807 //
1808 return FALSE;
1809 }
1810
1811 if (CheckMedia) {
1812 //
1813 // Test if it is ready to boot now
1814 //
1815 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {
1816 return TRUE;
1817 }
1818 } else {
1819 return TRUE;
1820 }
1821 }
1822
1823 //
1824 // If the boot option point to a file, it is a valid EFI boot option,
1825 // and assume it is ready to boot now
1826 //
1827 while (!IsDevicePathEnd (TempDevicePath)) {
1828 LastDeviceNode = TempDevicePath;
1829 TempDevicePath = NextDevicePathNode (TempDevicePath);
1830 }
1831 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&
1832 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {
1833 return TRUE;
1834 }
1835
1836 //
1837 // Check if it's a valid boot option for internal Shell
1838 //
1839 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {
1840 //
1841 // If the boot option point to Internal FV shell, make sure it is valid
1842 //
1843 TempDevicePath = DevPath;
1844 Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, PcdGetPtr(PcdShellFile));
1845 if (Status == EFI_ALREADY_STARTED) {
1846 return TRUE;
1847 } else {
1848 if (Status == EFI_SUCCESS) {
1849 FreePool (TempDevicePath);
1850 }
1851 return FALSE;
1852 }
1853 }
1854
1855 //
1856 // If the boot option point to a blockIO device:
1857 // if it is a removable blockIo device, it is valid.
1858 // if it is a fixed blockIo device, check its description confliction.
1859 //
1860 TempDevicePath = DevPath;
1861 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
1862 if (EFI_ERROR (Status)) {
1863 //
1864 // Device not present so see if we need to connect it
1865 //
1866 Status = BdsLibConnectDevicePath (DevPath);
1867 if (!EFI_ERROR (Status)) {
1868 //
1869 // Try again to get the Block Io protocol after we did the connect
1870 //
1871 TempDevicePath = DevPath;
1872 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
1873 }
1874 }
1875
1876 if (!EFI_ERROR (Status)) {
1877 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
1878 if (!EFI_ERROR (Status)) {
1879 if (CheckMedia) {
1880 //
1881 // Test if it is ready to boot now
1882 //
1883 if (BdsLibGetBootableHandle (DevPath) != NULL) {
1884 return TRUE;
1885 }
1886 } else {
1887 return TRUE;
1888 }
1889 }
1890 } else {
1891 //
1892 // 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,
1893 //
1894 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);
1895 if (!EFI_ERROR (Status)) {
1896 if (CheckMedia) {
1897 //
1898 // Test if it is ready to boot now
1899 //
1900 if (BdsLibGetBootableHandle (DevPath) != NULL) {
1901 return TRUE;
1902 }
1903 } else {
1904 return TRUE;
1905 }
1906 }
1907 }
1908
1909 return FALSE;
1910 }
1911
1912
1913 /**
1914 According to a file guild, check a Fv file device path is valid. If it is invalid,
1915 try to return the valid device path.
1916 FV address maybe changes for memory layout adjust from time to time, use this function
1917 could promise the Fv file device path is right.
1918
1919 @param DevicePath on input, the Fv file device path need to check on
1920 output, the updated valid Fv file device path
1921 @param FileGuid the Fv file guild
1922
1923 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1924 parameter
1925 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1926 guild at all
1927 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
1928 valid
1929 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1930 and return the updated device path in DevicePath
1931
1932 **/
1933 EFI_STATUS
1934 EFIAPI
1935 BdsLibUpdateFvFileDevicePath (
1936 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
1937 IN EFI_GUID *FileGuid
1938 )
1939 {
1940 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1941 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1942 EFI_STATUS Status;
1943 EFI_GUID *GuidPoint;
1944 UINTN Index;
1945 UINTN FvHandleCount;
1946 EFI_HANDLE *FvHandleBuffer;
1947 EFI_FV_FILETYPE Type;
1948 UINTN Size;
1949 EFI_FV_FILE_ATTRIBUTES Attributes;
1950 UINT32 AuthenticationStatus;
1951 BOOLEAN FindFvFile;
1952 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
1953 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1954 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;
1955 EFI_HANDLE FoundFvHandle;
1956 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
1957
1958 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
1959 return EFI_INVALID_PARAMETER;
1960 }
1961 if (FileGuid == NULL) {
1962 return EFI_INVALID_PARAMETER;
1963 }
1964
1965 //
1966 // Check whether the device path point to the default the input Fv file
1967 //
1968 TempDevicePath = *DevicePath;
1969 LastDeviceNode = TempDevicePath;
1970 while (!IsDevicePathEnd (TempDevicePath)) {
1971 LastDeviceNode = TempDevicePath;
1972 TempDevicePath = NextDevicePathNode (TempDevicePath);
1973 }
1974 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
1975 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
1976 );
1977 if (GuidPoint == NULL) {
1978 //
1979 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1980 //
1981 return EFI_UNSUPPORTED;
1982 }
1983 if (!CompareGuid (GuidPoint, FileGuid)) {
1984 //
1985 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1986 //
1987 return EFI_UNSUPPORTED;
1988 }
1989
1990 //
1991 // Check whether the input Fv file device path is valid
1992 //
1993 TempDevicePath = *DevicePath;
1994 FoundFvHandle = NULL;
1995 Status = gBS->LocateDevicePath (
1996 &gEfiFirmwareVolume2ProtocolGuid,
1997 &TempDevicePath,
1998 &FoundFvHandle
1999 );
2000 if (!EFI_ERROR (Status)) {
2001 Status = gBS->HandleProtocol (
2002 FoundFvHandle,
2003 &gEfiFirmwareVolume2ProtocolGuid,
2004 (VOID **) &Fv
2005 );
2006 if (!EFI_ERROR (Status)) {
2007 //
2008 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2009 //
2010 Status = Fv->ReadFile (
2011 Fv,
2012 FileGuid,
2013 NULL,
2014 &Size,
2015 &Type,
2016 &Attributes,
2017 &AuthenticationStatus
2018 );
2019 if (!EFI_ERROR (Status)) {
2020 return EFI_ALREADY_STARTED;
2021 }
2022 }
2023 }
2024
2025 //
2026 // Look for the input wanted FV file in current FV
2027 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2028 //
2029 FindFvFile = FALSE;
2030 FoundFvHandle = NULL;
2031 Status = gBS->HandleProtocol (
2032 mBdsImageHandle,
2033 &gEfiLoadedImageProtocolGuid,
2034 (VOID **) &LoadedImage
2035 );
2036 if (!EFI_ERROR (Status)) {
2037 Status = gBS->HandleProtocol (
2038 LoadedImage->DeviceHandle,
2039 &gEfiFirmwareVolume2ProtocolGuid,
2040 (VOID **) &Fv
2041 );
2042 if (!EFI_ERROR (Status)) {
2043 Status = Fv->ReadFile (
2044 Fv,
2045 FileGuid,
2046 NULL,
2047 &Size,
2048 &Type,
2049 &Attributes,
2050 &AuthenticationStatus
2051 );
2052 if (!EFI_ERROR (Status)) {
2053 FindFvFile = TRUE;
2054 FoundFvHandle = LoadedImage->DeviceHandle;
2055 }
2056 }
2057 }
2058 //
2059 // Second, if fail to find, try to enumerate all FV
2060 //
2061 if (!FindFvFile) {
2062 FvHandleBuffer = NULL;
2063 gBS->LocateHandleBuffer (
2064 ByProtocol,
2065 &gEfiFirmwareVolume2ProtocolGuid,
2066 NULL,
2067 &FvHandleCount,
2068 &FvHandleBuffer
2069 );
2070 for (Index = 0; Index < FvHandleCount; Index++) {
2071 gBS->HandleProtocol (
2072 FvHandleBuffer[Index],
2073 &gEfiFirmwareVolume2ProtocolGuid,
2074 (VOID **) &Fv
2075 );
2076
2077 Status = Fv->ReadFile (
2078 Fv,
2079 FileGuid,
2080 NULL,
2081 &Size,
2082 &Type,
2083 &Attributes,
2084 &AuthenticationStatus
2085 );
2086 if (EFI_ERROR (Status)) {
2087 //
2088 // Skip if input Fv file not in the FV
2089 //
2090 continue;
2091 }
2092 FindFvFile = TRUE;
2093 FoundFvHandle = FvHandleBuffer[Index];
2094 break;
2095 }
2096
2097 if (FvHandleBuffer != NULL) {
2098 FreePool (FvHandleBuffer);
2099 }
2100 }
2101
2102 if (FindFvFile) {
2103 //
2104 // Build the shell device path
2105 //
2106 NewDevicePath = DevicePathFromHandle (FoundFvHandle);
2107 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
2108 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
2109 *DevicePath = NewDevicePath;
2110 return EFI_SUCCESS;
2111 }
2112 return EFI_NOT_FOUND;
2113 }