]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Add BootlogoOnly feature in BDS for BGRT
[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 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalBdsLib.h"
16 #include "String.h"
17
18 BOOLEAN mEnumBootDevice = FALSE;
19 EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;
20
21 /**
22 The constructor function register UNI strings into imageHandle.
23
24 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
25
26 @param ImageHandle The firmware allocated handle for the EFI image.
27 @param SystemTable A pointer to the EFI System Table.
28
29 @retval EFI_SUCCESS The constructor successfully added string package.
30 @retval Other value The constructor can't add string package.
31
32 **/
33 EFI_STATUS
34 EFIAPI
35 GenericBdsLibConstructor (
36 IN EFI_HANDLE ImageHandle,
37 IN EFI_SYSTEM_TABLE *SystemTable
38 )
39 {
40
41 gBdsLibStringPackHandle = HiiAddPackages (
42 &gBdsLibStringPackageGuid,
43 &ImageHandle,
44 GenericBdsLibStrings,
45 NULL
46 );
47
48 ASSERT (gBdsLibStringPackHandle != NULL);
49
50 return EFI_SUCCESS;
51 }
52
53
54
55 /**
56 Boot the legacy system with the boot option
57
58 @param Option The legacy boot option which have BBS device path
59
60 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
61 legacy boot.
62 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
63
64 **/
65 EFI_STATUS
66 BdsLibDoLegacyBoot (
67 IN BDS_COMMON_OPTION *Option
68 )
69 {
70 EFI_STATUS Status;
71 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
72
73 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
74 if (EFI_ERROR (Status)) {
75 //
76 // If no LegacyBios protocol we do not support legacy boot
77 //
78 return EFI_UNSUPPORTED;
79 }
80 //
81 // Notes: if we separate the int 19, then we don't need to refresh BBS
82 //
83 BdsRefreshBbsTableForBoot (Option);
84
85 //
86 // Write boot to OS performance data for legacy boot.
87 //
88 PERF_CODE (
89 WriteBootToOsPerformanceData ();
90 );
91
92 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));
93 return LegacyBios->LegacyBoot (
94 LegacyBios,
95 (BBS_BBS_DEVICE_PATH *) Option->DevicePath,
96 Option->LoadOptionsSize,
97 Option->LoadOptions
98 );
99 }
100
101 /**
102 Internal function to check if the input boot option is a valid EFI NV Boot####.
103
104 @param OptionToCheck Boot option to be checked.
105
106 @retval TRUE This boot option matches a valid EFI NV Boot####.
107 @retval FALSE If not.
108
109 **/
110 BOOLEAN
111 IsBootOptionValidNVVarialbe (
112 IN BDS_COMMON_OPTION *OptionToCheck
113 )
114 {
115 LIST_ENTRY TempList;
116 BDS_COMMON_OPTION *BootOption;
117 BOOLEAN Valid;
118 CHAR16 OptionName[20];
119
120 Valid = FALSE;
121
122 InitializeListHead (&TempList);
123 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);
124
125 BootOption = BdsLibVariableToOption (&TempList, OptionName);
126 if (BootOption == NULL) {
127 return FALSE;
128 }
129
130 //
131 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
132 // valid EFI NV Boot####.
133 //
134 if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&
135 (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))
136 {
137 Valid = TRUE;
138 }
139
140 FreePool (BootOption);
141
142 return Valid;
143 }
144
145 /**
146 Check whether a USB device match the specified USB Class device path. This
147 function follows "Load Option Processing" behavior in UEFI specification.
148
149 @param UsbIo USB I/O protocol associated with the USB device.
150 @param UsbClass The USB Class device path to match.
151
152 @retval TRUE The USB device match the USB Class device path.
153 @retval FALSE The USB device does not match the USB Class device path.
154
155 **/
156 BOOLEAN
157 BdsMatchUsbClass (
158 IN EFI_USB_IO_PROTOCOL *UsbIo,
159 IN USB_CLASS_DEVICE_PATH *UsbClass
160 )
161 {
162 EFI_STATUS Status;
163 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
164 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
165 UINT8 DeviceClass;
166 UINT8 DeviceSubClass;
167 UINT8 DeviceProtocol;
168
169 if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||
170 (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){
171 return FALSE;
172 }
173
174 //
175 // Check Vendor Id and Product Id.
176 //
177 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
178 if (EFI_ERROR (Status)) {
179 return FALSE;
180 }
181
182 if ((UsbClass->VendorId != 0xffff) &&
183 (UsbClass->VendorId != DevDesc.IdVendor)) {
184 return FALSE;
185 }
186
187 if ((UsbClass->ProductId != 0xffff) &&
188 (UsbClass->ProductId != DevDesc.IdProduct)) {
189 return FALSE;
190 }
191
192 DeviceClass = DevDesc.DeviceClass;
193 DeviceSubClass = DevDesc.DeviceSubClass;
194 DeviceProtocol = DevDesc.DeviceProtocol;
195 if (DeviceClass == 0) {
196 //
197 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
198 // Protocol in Interface Descriptor instead.
199 //
200 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
201 if (EFI_ERROR (Status)) {
202 return FALSE;
203 }
204
205 DeviceClass = IfDesc.InterfaceClass;
206 DeviceSubClass = IfDesc.InterfaceSubClass;
207 DeviceProtocol = IfDesc.InterfaceProtocol;
208 }
209
210 //
211 // Check Class, SubClass and Protocol.
212 //
213 if ((UsbClass->DeviceClass != 0xff) &&
214 (UsbClass->DeviceClass != DeviceClass)) {
215 return FALSE;
216 }
217
218 if ((UsbClass->DeviceSubClass != 0xff) &&
219 (UsbClass->DeviceSubClass != DeviceSubClass)) {
220 return FALSE;
221 }
222
223 if ((UsbClass->DeviceProtocol != 0xff) &&
224 (UsbClass->DeviceProtocol != DeviceProtocol)) {
225 return FALSE;
226 }
227
228 return TRUE;
229 }
230
231 /**
232 Check whether a USB device match the specified USB WWID device path. This
233 function follows "Load Option Processing" behavior in UEFI specification.
234
235 @param UsbIo USB I/O protocol associated with the USB device.
236 @param UsbWwid The USB WWID device path to match.
237
238 @retval TRUE The USB device match the USB WWID device path.
239 @retval FALSE The USB device does not match the USB WWID device path.
240
241 **/
242 BOOLEAN
243 BdsMatchUsbWwid (
244 IN EFI_USB_IO_PROTOCOL *UsbIo,
245 IN USB_WWID_DEVICE_PATH *UsbWwid
246 )
247 {
248 EFI_STATUS Status;
249 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
250 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
251 UINT16 *LangIdTable;
252 UINT16 TableSize;
253 UINT16 Index;
254 CHAR16 *CompareStr;
255 UINTN CompareLen;
256 CHAR16 *SerialNumberStr;
257 UINTN Length;
258
259 if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||
260 (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP )){
261 return FALSE;
262 }
263
264 //
265 // Check Vendor Id and Product Id.
266 //
267 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
268 if (EFI_ERROR (Status)) {
269 return FALSE;
270 }
271 if ((DevDesc.IdVendor != UsbWwid->VendorId) ||
272 (DevDesc.IdProduct != UsbWwid->ProductId)) {
273 return FALSE;
274 }
275
276 //
277 // Check Interface Number.
278 //
279 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
280 if (EFI_ERROR (Status)) {
281 return FALSE;
282 }
283 if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {
284 return FALSE;
285 }
286
287 //
288 // Check Serial Number.
289 //
290 if (DevDesc.StrSerialNumber == 0) {
291 return FALSE;
292 }
293
294 //
295 // Get all supported languages.
296 //
297 TableSize = 0;
298 LangIdTable = NULL;
299 Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);
300 if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {
301 return FALSE;
302 }
303
304 //
305 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
306 //
307 CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);
308 CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);
309 if (CompareStr[CompareLen - 1] == L'\0') {
310 CompareLen--;
311 }
312
313 //
314 // Compare serial number in each supported language.
315 //
316 for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {
317 SerialNumberStr = NULL;
318 Status = UsbIo->UsbGetStringDescriptor (
319 UsbIo,
320 LangIdTable[Index],
321 DevDesc.StrSerialNumber,
322 &SerialNumberStr
323 );
324 if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {
325 continue;
326 }
327
328 Length = StrLen (SerialNumberStr);
329 if ((Length >= CompareLen) &&
330 (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {
331 FreePool (SerialNumberStr);
332 return TRUE;
333 }
334
335 FreePool (SerialNumberStr);
336 }
337
338 return FALSE;
339 }
340
341 /**
342 Find a USB device path which match the specified short-form device path start
343 with USB Class or USB WWID device path and load the boot file then return the
344 image handle. If ParentDevicePath is NULL, this function will search in all USB
345 devices of the platform. If ParentDevicePath is not NULL,this function will only
346 search in its child devices.
347
348 @param ParentDevicePath The device path of the parent.
349 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
350
351 @return The image Handle if find load file from specified short-form device path
352 or NULL if not found.
353
354 **/
355 EFI_HANDLE *
356 BdsFindUsbDevice (
357 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
358 IN EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath
359 )
360 {
361 EFI_STATUS Status;
362 UINTN UsbIoHandleCount;
363 EFI_HANDLE *UsbIoHandleBuffer;
364 EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;
365 EFI_USB_IO_PROTOCOL *UsbIo;
366 UINTN Index;
367 UINTN ParentSize;
368 UINTN Size;
369 EFI_HANDLE ImageHandle;
370 EFI_HANDLE Handle;
371 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
372 EFI_DEVICE_PATH_PROTOCOL *NextDevicePath;
373
374 FullDevicePath = NULL;
375 ImageHandle = NULL;
376
377 //
378 // Get all UsbIo Handles.
379 //
380 UsbIoHandleCount = 0;
381 UsbIoHandleBuffer = NULL;
382 Status = gBS->LocateHandleBuffer (
383 ByProtocol,
384 &gEfiUsbIoProtocolGuid,
385 NULL,
386 &UsbIoHandleCount,
387 &UsbIoHandleBuffer
388 );
389 if (EFI_ERROR (Status) || (UsbIoHandleCount == 0) || (UsbIoHandleBuffer == NULL)) {
390 return NULL;
391 }
392
393 ParentSize = (ParentDevicePath == NULL) ? 0 : GetDevicePathSize (ParentDevicePath);
394 for (Index = 0; Index < UsbIoHandleCount; Index++) {
395 //
396 // Get the Usb IO interface.
397 //
398 Status = gBS->HandleProtocol(
399 UsbIoHandleBuffer[Index],
400 &gEfiUsbIoProtocolGuid,
401 (VOID **) &UsbIo
402 );
403 if (EFI_ERROR (Status)) {
404 continue;
405 }
406
407 UsbIoDevicePath = DevicePathFromHandle (UsbIoHandleBuffer[Index]);
408 if (UsbIoDevicePath == NULL) {
409 continue;
410 }
411
412 if (ParentDevicePath != NULL) {
413 //
414 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
415 //
416 Size = GetDevicePathSize (UsbIoDevicePath);
417 if ((Size < ParentSize) ||
418 (CompareMem (UsbIoDevicePath, ParentDevicePath, ParentSize - END_DEVICE_PATH_LENGTH) != 0)) {
419 continue;
420 }
421 }
422
423 if (BdsMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ShortFormDevicePath) ||
424 BdsMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ShortFormDevicePath)) {
425 //
426 // Try to find if there is the boot file in this DevicePath
427 //
428 NextDevicePath = NextDevicePathNode (ShortFormDevicePath);
429 if (!IsDevicePathEnd (NextDevicePath)) {
430 FullDevicePath = AppendDevicePath (UsbIoDevicePath, NextDevicePath);
431 //
432 // Connect the full device path, so that Simple File System protocol
433 // could be installed for this USB device.
434 //
435 BdsLibConnectDevicePath (FullDevicePath);
436 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
437 Status = gBS->LoadImage (
438 TRUE,
439 gImageHandle,
440 FullDevicePath,
441 NULL,
442 0,
443 &ImageHandle
444 );
445 FreePool (FullDevicePath);
446 } else {
447 FullDevicePath = UsbIoDevicePath;
448 Status = EFI_NOT_FOUND;
449 }
450
451 //
452 // If we didn't find an image directly, we need to try as if it is a removable device boot option
453 // and load the image according to the default boot behavior for removable device.
454 //
455 if (EFI_ERROR (Status)) {
456 //
457 // check if there is a bootable removable media could be found in this device path ,
458 // and get the bootable media handle
459 //
460 Handle = BdsLibGetBootableHandle(UsbIoDevicePath);
461 if (Handle == NULL) {
462 continue;
463 }
464 //
465 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
466 // machinename is ia32, ia64, x64, ...
467 //
468 FullDevicePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
469 if (FullDevicePath != NULL) {
470 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
471 Status = gBS->LoadImage (
472 TRUE,
473 gImageHandle,
474 FullDevicePath,
475 NULL,
476 0,
477 &ImageHandle
478 );
479 if (EFI_ERROR (Status)) {
480 //
481 // The DevicePath failed, and it's not a valid
482 // removable media device.
483 //
484 continue;
485 }
486 } else {
487 continue;
488 }
489 }
490 break;
491 }
492 }
493
494 FreePool (UsbIoHandleBuffer);
495 return ImageHandle;
496 }
497
498 /**
499 Expand USB Class or USB WWID device path node to be full device path of a USB
500 device in platform then load the boot file on this full device path and return the
501 image handle.
502
503 This function support following 4 cases:
504 1) Boot Option device path starts with a USB Class or USB WWID device path,
505 and there is no Media FilePath device path in the end.
506 In this case, it will follow Removable Media Boot Behavior.
507 2) Boot Option device path starts with a USB Class or USB WWID device path,
508 and ended with Media FilePath device path.
509 3) Boot Option device path starts with a full device path to a USB Host Controller,
510 contains a USB Class or USB WWID device path node, while not ended with Media
511 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
512 4) Boot Option device path starts with a full device path to a USB Host Controller,
513 contains a USB Class or USB WWID device path node, and ended with Media
514 FilePath device path.
515
516 @param DevicePath The Boot Option device path.
517
518 @return The image handle of boot file, or NULL if there is no boot file found in
519 the specified USB Class or USB WWID device path.
520
521 **/
522 EFI_HANDLE *
523 BdsExpandUsbShortFormDevicePath (
524 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
525 )
526 {
527 EFI_HANDLE *ImageHandle;
528 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
529 EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath;
530
531 //
532 // Search for USB Class or USB WWID device path node.
533 //
534 ShortFormDevicePath = NULL;
535 ImageHandle = NULL;
536 TempDevicePath = DevicePath;
537 while (!IsDevicePathEnd (TempDevicePath)) {
538 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&
539 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||
540 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {
541 ShortFormDevicePath = TempDevicePath;
542 break;
543 }
544 TempDevicePath = NextDevicePathNode (TempDevicePath);
545 }
546
547 if (ShortFormDevicePath == NULL) {
548 //
549 // No USB Class or USB WWID device path node found, do nothing.
550 //
551 return NULL;
552 }
553
554 if (ShortFormDevicePath == DevicePath) {
555 //
556 // Boot Option device path starts with USB Class or USB WWID device path.
557 //
558 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);
559 if (ImageHandle == NULL) {
560 //
561 // Failed to find a match in existing devices, connect the short form USB
562 // device path and try again.
563 //
564 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath);
565 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);
566 }
567 } else {
568 //
569 // Boot Option device path contains USB Class or USB WWID device path node.
570 //
571
572 //
573 // Prepare the parent device path for search.
574 //
575 TempDevicePath = DuplicateDevicePath (DevicePath);
576 ASSERT (TempDevicePath != NULL);
577 SetDevicePathEndNode (((UINT8 *) TempDevicePath) + ((UINTN) ShortFormDevicePath - (UINTN) DevicePath));
578
579 //
580 // The USB Host Controller device path is already in Boot Option device path
581 // and USB Bus driver already support RemainingDevicePath starts with USB
582 // Class or USB WWID device path, so just search in existing USB devices and
583 // doesn't perform ConnectController here.
584 //
585 ImageHandle = BdsFindUsbDevice (TempDevicePath, ShortFormDevicePath);
586 FreePool (TempDevicePath);
587 }
588
589 return ImageHandle;
590 }
591
592 /**
593 Process the boot option follow the UEFI specification and
594 special treat the legacy boot option with BBS_DEVICE_PATH.
595
596 @param Option The boot option need to be processed
597 @param DevicePath The device path which describe where to load the
598 boot image or the legacy BBS device path to boot
599 the legacy OS
600 @param ExitDataSize The size of exit data.
601 @param ExitData Data returned when Boot image failed.
602
603 @retval EFI_SUCCESS Boot from the input boot option successfully.
604 @retval EFI_NOT_FOUND If the Device Path is not found in the system
605
606 **/
607 EFI_STATUS
608 EFIAPI
609 BdsLibBootViaBootOption (
610 IN BDS_COMMON_OPTION *Option,
611 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
612 OUT UINTN *ExitDataSize,
613 OUT CHAR16 **ExitData OPTIONAL
614 )
615 {
616 EFI_STATUS Status;
617 EFI_STATUS StatusLogo;
618 EFI_HANDLE Handle;
619 EFI_HANDLE ImageHandle;
620 EFI_DEVICE_PATH_PROTOCOL *FilePath;
621 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
622 EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;
623 EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;
624 LIST_ENTRY TempBootLists;
625 EFI_BOOT_LOGO_PROTOCOL *BootLogo;
626
627 //
628 // Record the performance data for End of BDS
629 //
630 PERF_END(NULL, "BDS", NULL, 0);
631
632 *ExitDataSize = 0;
633 *ExitData = NULL;
634
635 //
636 // Notes: this code can be remove after the s3 script table
637 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
638 // EVT_SIGNAL_LEGACY_BOOT
639 //
640 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);
641 if (!EFI_ERROR (Status)) {
642 AcpiS3Save->S3Save (AcpiS3Save, NULL);
643 }
644 //
645 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
646 // full device path
647 //
648 WorkingDevicePath = NULL;
649 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
650 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {
651 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (
652 (HARDDRIVE_DEVICE_PATH *)DevicePath
653 );
654 if (WorkingDevicePath != NULL) {
655 DevicePath = WorkingDevicePath;
656 }
657 }
658
659 //
660 // Set Boot Current
661 //
662 if (IsBootOptionValidNVVarialbe (Option)) {
663 //
664 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
665 // In this case, "BootCurrent" is not created.
666 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
667 //
668 gRT->SetVariable (
669 L"BootCurrent",
670 &gEfiGlobalVariableGuid,
671 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
672 sizeof (UINT16),
673 &Option->BootCurrent
674 );
675 }
676
677 //
678 // Signal the EVT_SIGNAL_READY_TO_BOOT event
679 //
680 EfiSignalEventReadyToBoot();
681
682 //
683 // Expand USB Class or USB WWID device path node to be full device path of a USB
684 // device in platform then load the boot file on this full device path and get the
685 // image handle.
686 //
687 ImageHandle = BdsExpandUsbShortFormDevicePath (DevicePath);
688
689 //
690 // Adjust the different type memory page number just before booting
691 // and save the updated info into the variable for next boot to use
692 //
693 BdsSetMemoryTypeInformationVariable ();
694
695 //
696 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
697 // Here get the ImageHandle for the non USB class or WWID device path.
698 //
699 if (ImageHandle == NULL) {
700 ASSERT (Option->DevicePath != NULL);
701 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&
702 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)
703 ) {
704 //
705 // Check to see if we should legacy BOOT. If yes then do the legacy boot
706 //
707 return BdsLibDoLegacyBoot (Option);
708 }
709
710 //
711 // If the boot option point to Internal FV shell, make sure it is valid
712 //
713 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));
714 if (!EFI_ERROR(Status)) {
715 if (Option->DevicePath != NULL) {
716 FreePool(Option->DevicePath);
717 }
718 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
719 ASSERT(Option->DevicePath != NULL);
720 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
721 //
722 // Update the shell boot option
723 //
724 InitializeListHead (&TempBootLists);
725 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");
726
727 //
728 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
729 //
730 FreePool (DevicePath);
731 DevicePath = Option->DevicePath;
732 }
733
734 DEBUG_CODE_BEGIN();
735
736 if (Option->Description == NULL) {
737 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));
738 } else {
739 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));
740 }
741
742 DEBUG_CODE_END();
743
744 //
745 // Report status code for OS Loader LoadImage.
746 //
747 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
748 Status = gBS->LoadImage (
749 TRUE,
750 gImageHandle,
751 DevicePath,
752 NULL,
753 0,
754 &ImageHandle
755 );
756
757 //
758 // If we didn't find an image directly, we need to try as if it is a removable device boot option
759 // and load the image according to the default boot behavior for removable device.
760 //
761 if (EFI_ERROR (Status)) {
762 //
763 // check if there is a bootable removable media could be found in this device path ,
764 // and get the bootable media handle
765 //
766 Handle = BdsLibGetBootableHandle(DevicePath);
767 if (Handle == NULL) {
768 goto Done;
769 }
770 //
771 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
772 // machinename is ia32, ia64, x64, ...
773 //
774 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
775 if (FilePath != NULL) {
776 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
777 Status = gBS->LoadImage (
778 TRUE,
779 gImageHandle,
780 FilePath,
781 NULL,
782 0,
783 &ImageHandle
784 );
785 if (EFI_ERROR (Status)) {
786 //
787 // The DevicePath failed, and it's not a valid
788 // removable media device.
789 //
790 goto Done;
791 }
792 }
793 }
794
795 if (EFI_ERROR (Status)) {
796 //
797 // It there is any error from the Boot attempt exit now.
798 //
799 goto Done;
800 }
801 }
802 //
803 // Provide the image with it's load options
804 //
805 if (ImageHandle == NULL) {
806 goto Done;
807 }
808 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
809 ASSERT_EFI_ERROR (Status);
810
811 if (Option->LoadOptionsSize != 0) {
812 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
813 ImageInfo->LoadOptions = Option->LoadOptions;
814 }
815 //
816 // Before calling the image, enable the Watchdog Timer for
817 // the 5 Minute period
818 //
819 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
820
821 //
822 // Write boot to OS performance data for UEFI boot
823 //
824 PERF_CODE (
825 WriteBootToOsPerformanceData ();
826 );
827
828 //
829 // Report status code for OS Loader StartImage.
830 //
831 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));
832
833 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);
834 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));
835
836 //
837 // Clear the Watchdog Timer after the image returns
838 //
839 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
840
841 Done:
842 //
843 // Set Logo status invalid after trying one boot option
844 //
845 BootLogo = NULL;
846 StatusLogo = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);
847 if (!EFI_ERROR (StatusLogo) && (BootLogo != NULL)) {
848 BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);
849 }
850
851 //
852 // Clear Boot Current
853 //
854 gRT->SetVariable (
855 L"BootCurrent",
856 &gEfiGlobalVariableGuid,
857 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
858 0,
859 &Option->BootCurrent
860 );
861
862 return Status;
863 }
864
865
866 /**
867 Expand a device path that starts with a hard drive media device path node to be a
868 full device path that includes the full hardware path to the device. We need
869 to do this so it can be booted. As an optimization the front match (the part point
870 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
871 so a connect all is not required on every boot. All successful history device path
872 which point to partition node (the front part) will be saved.
873
874 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
875 drive media device path.
876 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
877 cannot be found.
878
879 **/
880 EFI_DEVICE_PATH_PROTOCOL *
881 EFIAPI
882 BdsExpandPartitionPartialDevicePathToFull (
883 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
884 )
885 {
886 EFI_STATUS Status;
887 UINTN BlockIoHandleCount;
888 EFI_HANDLE *BlockIoBuffer;
889 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
890 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;
891 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
892 UINTN Index;
893 UINTN InstanceNum;
894 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;
895 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
896 UINTN CachedDevicePathSize;
897 BOOLEAN DeviceExist;
898 BOOLEAN NeedAdjust;
899 EFI_DEVICE_PATH_PROTOCOL *Instance;
900 UINTN Size;
901
902 FullDevicePath = NULL;
903 //
904 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
905 // If exist, search the front path which point to partition node in the variable instants.
906 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
907 //
908 CachedDevicePath = BdsLibGetVariableAndSize (
909 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
910 &gHdBootDevicePathVariablGuid,
911 &CachedDevicePathSize
912 );
913
914 if (CachedDevicePath != NULL) {
915 TempNewDevicePath = CachedDevicePath;
916 DeviceExist = FALSE;
917 NeedAdjust = FALSE;
918 do {
919 //
920 // Check every instance of the variable
921 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
922 // partial partition boot option. Second, check whether the instance could be connected.
923 //
924 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);
925 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {
926 //
927 // Connect the device path instance, the device path point to hard drive media device path node
928 // e.g. ACPI() /PCI()/ATA()/Partition()
929 //
930 Status = BdsLibConnectDevicePath (Instance);
931 if (!EFI_ERROR (Status)) {
932 DeviceExist = TRUE;
933 break;
934 }
935 }
936 //
937 // Come here means the first instance is not matched
938 //
939 NeedAdjust = TRUE;
940 FreePool(Instance);
941 } while (TempNewDevicePath != NULL);
942
943 if (DeviceExist) {
944 //
945 // Find the matched device path.
946 // Append the file path information from the boot option and return the fully expanded device path.
947 //
948 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
949 FullDevicePath = AppendDevicePath (Instance, DevicePath);
950
951 //
952 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
953 //
954 if (NeedAdjust) {
955 //
956 // First delete the matched instance.
957 //
958 TempNewDevicePath = CachedDevicePath;
959 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );
960 FreePool (TempNewDevicePath);
961
962 //
963 // Second, append the remaining path after the matched instance
964 //
965 TempNewDevicePath = CachedDevicePath;
966 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );
967 FreePool (TempNewDevicePath);
968 //
969 // Save the matching Device Path so we don't need to do a connect all next time
970 //
971 Status = gRT->SetVariable (
972 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
973 &gHdBootDevicePathVariablGuid,
974 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
975 GetDevicePathSize (CachedDevicePath),
976 CachedDevicePath
977 );
978 }
979
980 FreePool (Instance);
981 FreePool (CachedDevicePath);
982 return FullDevicePath;
983 }
984 }
985
986 //
987 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
988 // to search all devices in the system for a matched partition
989 //
990 BdsLibConnectAllDriversToAllControllers ();
991 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);
992 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {
993 //
994 // If there was an error or there are no device handles that support
995 // the BLOCK_IO Protocol, then return.
996 //
997 return NULL;
998 }
999 //
1000 // Loop through all the device handles that support the BLOCK_IO Protocol
1001 //
1002 for (Index = 0; Index < BlockIoHandleCount; Index++) {
1003
1004 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);
1005 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {
1006 continue;
1007 }
1008
1009 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {
1010 //
1011 // Find the matched partition device path
1012 //
1013 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
1014 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);
1015
1016 //
1017 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
1018 //
1019 if (CachedDevicePath != NULL) {
1020 //
1021 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
1022 //
1023 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {
1024 TempNewDevicePath = CachedDevicePath;
1025 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);
1026 FreePool(TempNewDevicePath);
1027
1028 TempNewDevicePath = CachedDevicePath;
1029 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
1030 if (TempNewDevicePath != NULL) {
1031 FreePool(TempNewDevicePath);
1032 }
1033 } else {
1034 TempNewDevicePath = CachedDevicePath;
1035 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
1036 FreePool(TempNewDevicePath);
1037 }
1038 //
1039 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
1040 // If the user try to boot many OS in different HDs or partitions, in theory,
1041 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
1042 //
1043 InstanceNum = 0;
1044 ASSERT (CachedDevicePath != NULL);
1045 TempNewDevicePath = CachedDevicePath;
1046 while (!IsDevicePathEnd (TempNewDevicePath)) {
1047 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
1048 //
1049 // Parse one instance
1050 //
1051 while (!IsDevicePathEndType (TempNewDevicePath)) {
1052 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
1053 }
1054 InstanceNum++;
1055 //
1056 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
1057 //
1058 if (InstanceNum >= 12) {
1059 SetDevicePathEndNode (TempNewDevicePath);
1060 break;
1061 }
1062 }
1063 } else {
1064 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);
1065 }
1066
1067 //
1068 // Save the matching Device Path so we don't need to do a connect all next time
1069 //
1070 Status = gRT->SetVariable (
1071 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
1072 &gHdBootDevicePathVariablGuid,
1073 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1074 GetDevicePathSize (CachedDevicePath),
1075 CachedDevicePath
1076 );
1077
1078 break;
1079 }
1080 }
1081
1082 if (CachedDevicePath != NULL) {
1083 FreePool (CachedDevicePath);
1084 }
1085 if (BlockIoBuffer != NULL) {
1086 FreePool (BlockIoBuffer);
1087 }
1088 return FullDevicePath;
1089 }
1090
1091 /**
1092 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
1093 instances, has the same partition node with HardDriveDevicePath device path
1094
1095 @param BlockIoDevicePath Multi device path instances which need to check
1096 @param HardDriveDevicePath A device path which starts with a hard drive media
1097 device path.
1098
1099 @retval TRUE There is a matched device path instance.
1100 @retval FALSE There is no matched device path instance.
1101
1102 **/
1103 BOOLEAN
1104 EFIAPI
1105 MatchPartitionDevicePathNode (
1106 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,
1107 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
1108 )
1109 {
1110 HARDDRIVE_DEVICE_PATH *TmpHdPath;
1111 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1112 BOOLEAN Match;
1113 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;
1114
1115 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {
1116 return FALSE;
1117 }
1118
1119 //
1120 // Make PreviousDevicePath == the device path node before the end node
1121 //
1122 DevicePath = BlockIoDevicePath;
1123 BlockIoHdDevicePathNode = NULL;
1124
1125 //
1126 // find the partition device path node
1127 //
1128 while (!IsDevicePathEnd (DevicePath)) {
1129 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
1130 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)
1131 ) {
1132 BlockIoHdDevicePathNode = DevicePath;
1133 break;
1134 }
1135
1136 DevicePath = NextDevicePathNode (DevicePath);
1137 }
1138
1139 if (BlockIoHdDevicePathNode == NULL) {
1140 return FALSE;
1141 }
1142 //
1143 // See if the harddrive device path in blockio matches the orig Hard Drive Node
1144 //
1145 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;
1146 Match = FALSE;
1147
1148 //
1149 // Check for the match
1150 //
1151 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&
1152 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {
1153 switch (TmpHdPath->SignatureType) {
1154 case SIGNATURE_TYPE_GUID:
1155 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);
1156 break;
1157 case SIGNATURE_TYPE_MBR:
1158 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));
1159 break;
1160 default:
1161 Match = FALSE;
1162 break;
1163 }
1164 }
1165
1166 return Match;
1167 }
1168
1169 /**
1170 Delete the boot option associated with the handle passed in.
1171
1172 @param Handle The handle which present the device path to create
1173 boot option
1174
1175 @retval EFI_SUCCESS Delete the boot option success
1176 @retval EFI_NOT_FOUND If the Device Path is not found in the system
1177 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
1178 @retval Other Error return value from SetVariable()
1179
1180 **/
1181 EFI_STATUS
1182 BdsLibDeleteOptionFromHandle (
1183 IN EFI_HANDLE Handle
1184 )
1185 {
1186 UINT16 *BootOrder;
1187 UINT8 *BootOptionVar;
1188 UINTN BootOrderSize;
1189 UINTN BootOptionSize;
1190 EFI_STATUS Status;
1191 UINTN Index;
1192 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
1193 UINTN DevicePathSize;
1194 UINTN OptionDevicePathSize;
1195 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1196 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
1197 UINT8 *TempPtr;
1198
1199 Status = EFI_SUCCESS;
1200 BootOrder = NULL;
1201 BootOrderSize = 0;
1202
1203 //
1204 // Check "BootOrder" variable, if no, means there is no any boot order.
1205 //
1206 BootOrder = BdsLibGetVariableAndSize (
1207 L"BootOrder",
1208 &gEfiGlobalVariableGuid,
1209 &BootOrderSize
1210 );
1211 if (BootOrder == NULL) {
1212 return EFI_NOT_FOUND;
1213 }
1214
1215 //
1216 // Convert device handle to device path protocol instance
1217 //
1218 DevicePath = DevicePathFromHandle (Handle);
1219 if (DevicePath == NULL) {
1220 return EFI_NOT_FOUND;
1221 }
1222 DevicePathSize = GetDevicePathSize (DevicePath);
1223
1224 //
1225 // Loop all boot order variable and find the matching device path
1226 //
1227 Index = 0;
1228 while (Index < BootOrderSize / sizeof (UINT16)) {
1229 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
1230 BootOptionVar = BdsLibGetVariableAndSize (
1231 BootOption,
1232 &gEfiGlobalVariableGuid,
1233 &BootOptionSize
1234 );
1235
1236 if (BootOptionVar == NULL) {
1237 FreePool (BootOrder);
1238 return EFI_OUT_OF_RESOURCES;
1239 }
1240
1241 TempPtr = BootOptionVar;
1242 TempPtr += sizeof (UINT32) + sizeof (UINT16);
1243 TempPtr += StrSize ((CHAR16 *) TempPtr);
1244 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
1245 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);
1246
1247 //
1248 // Check whether the device path match
1249 //
1250 if ((OptionDevicePathSize == DevicePathSize) &&
1251 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {
1252 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);
1253 FreePool (BootOptionVar);
1254 break;
1255 }
1256
1257 FreePool (BootOptionVar);
1258 Index++;
1259 }
1260
1261 //
1262 // Adjust number of boot option for "BootOrder" variable.
1263 //
1264 Status = gRT->SetVariable (
1265 L"BootOrder",
1266 &gEfiGlobalVariableGuid,
1267 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1268 BootOrderSize,
1269 BootOrder
1270 );
1271
1272 FreePool (BootOrder);
1273
1274 return Status;
1275 }
1276
1277
1278 /**
1279 Delete all invalid EFI boot options.
1280
1281 @retval EFI_SUCCESS Delete all invalid boot option success
1282 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
1283 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
1284 @retval Other Error return value from SetVariable()
1285
1286 **/
1287 EFI_STATUS
1288 BdsDeleteAllInvalidEfiBootOption (
1289 VOID
1290 )
1291 {
1292 UINT16 *BootOrder;
1293 UINT8 *BootOptionVar;
1294 UINTN BootOrderSize;
1295 UINTN BootOptionSize;
1296 EFI_STATUS Status;
1297 UINTN Index;
1298 UINTN Index2;
1299 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
1300 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
1301 UINT8 *TempPtr;
1302 CHAR16 *Description;
1303
1304 Status = EFI_SUCCESS;
1305 BootOrder = NULL;
1306 BootOrderSize = 0;
1307
1308 //
1309 // Check "BootOrder" variable firstly, this variable hold the number of boot options
1310 //
1311 BootOrder = BdsLibGetVariableAndSize (
1312 L"BootOrder",
1313 &gEfiGlobalVariableGuid,
1314 &BootOrderSize
1315 );
1316 if (NULL == BootOrder) {
1317 return EFI_NOT_FOUND;
1318 }
1319
1320 Index = 0;
1321 while (Index < BootOrderSize / sizeof (UINT16)) {
1322 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
1323 BootOptionVar = BdsLibGetVariableAndSize (
1324 BootOption,
1325 &gEfiGlobalVariableGuid,
1326 &BootOptionSize
1327 );
1328 if (NULL == BootOptionVar) {
1329 FreePool (BootOrder);
1330 return EFI_OUT_OF_RESOURCES;
1331 }
1332
1333 TempPtr = BootOptionVar;
1334 TempPtr += sizeof (UINT32) + sizeof (UINT16);
1335 Description = (CHAR16 *) TempPtr;
1336 TempPtr += StrSize ((CHAR16 *) TempPtr);
1337 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
1338
1339 //
1340 // Skip legacy boot option (BBS boot device)
1341 //
1342 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&
1343 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {
1344 FreePool (BootOptionVar);
1345 Index++;
1346 continue;
1347 }
1348
1349 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {
1350 //
1351 // Delete this invalid boot option "Boot####"
1352 //
1353 Status = gRT->SetVariable (
1354 BootOption,
1355 &gEfiGlobalVariableGuid,
1356 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1357 0,
1358 NULL
1359 );
1360 //
1361 // Mark this boot option in boot order as deleted
1362 //
1363 BootOrder[Index] = 0xffff;
1364 }
1365
1366 FreePool (BootOptionVar);
1367 Index++;
1368 }
1369
1370 //
1371 // Adjust boot order array
1372 //
1373 Index2 = 0;
1374 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
1375 if (BootOrder[Index] != 0xffff) {
1376 BootOrder[Index2] = BootOrder[Index];
1377 Index2 ++;
1378 }
1379 }
1380 Status = gRT->SetVariable (
1381 L"BootOrder",
1382 &gEfiGlobalVariableGuid,
1383 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1384 Index2 * sizeof (UINT16),
1385 BootOrder
1386 );
1387
1388 FreePool (BootOrder);
1389
1390 return Status;
1391 }
1392
1393
1394 /**
1395 For EFI boot option, BDS separate them as six types:
1396 1. Network - The boot option points to the SimpleNetworkProtocol device.
1397 Bds will try to automatically create this type boot option when enumerate.
1398 2. Shell - The boot option points to internal flash shell.
1399 Bds will try to automatically create this type boot option when enumerate.
1400 3. Removable BlockIo - The boot option only points to the removable media
1401 device, like USB flash disk, DVD, Floppy etc.
1402 These device should contain a *removable* blockIo
1403 protocol in their device handle.
1404 Bds will try to automatically create this type boot option
1405 when enumerate.
1406 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
1407 like HardDisk.
1408 These device should contain a *fixed* blockIo
1409 protocol in their device handle.
1410 BDS will skip fixed blockIo devices, and NOT
1411 automatically create boot option for them. But BDS
1412 will help to delete those fixed blockIo boot option,
1413 whose description rule conflict with other auto-created
1414 boot options.
1415 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
1416 has SimpleFileSystem Protocol, but has no blockio
1417 protocol. These devices do not offer blockIo
1418 protocol, but BDS still can get the
1419 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
1420 Protocol.
1421 6. File - The boot option points to a file. These boot options are usually
1422 created by user manually or OS loader. BDS will not delete or modify
1423 these boot options.
1424
1425 This function will enumerate all possible boot device in the system, and
1426 automatically create boot options for Network, Shell, Removable BlockIo,
1427 and Non-BlockIo Simplefile devices.
1428 It will only execute once of every boot.
1429
1430 @param BdsBootOptionList The header of the link list which indexed all
1431 current boot options
1432
1433 @retval EFI_SUCCESS Finished all the boot device enumerate and create
1434 the boot option base on that boot device
1435
1436 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
1437 **/
1438 EFI_STATUS
1439 EFIAPI
1440 BdsLibEnumerateAllBootOption (
1441 IN OUT LIST_ENTRY *BdsBootOptionList
1442 )
1443 {
1444 EFI_STATUS Status;
1445 UINT16 FloppyNumber;
1446 UINT16 HarddriveNumber;
1447 UINT16 CdromNumber;
1448 UINT16 UsbNumber;
1449 UINT16 MiscNumber;
1450 UINT16 ScsiNumber;
1451 UINT16 NonBlockNumber;
1452 UINTN NumberBlockIoHandles;
1453 EFI_HANDLE *BlockIoHandles;
1454 EFI_BLOCK_IO_PROTOCOL *BlkIo;
1455 BOOLEAN Removable[2];
1456 UINTN RemovableIndex;
1457 UINTN Index;
1458 UINTN NumOfLoadFileHandles;
1459 EFI_HANDLE *LoadFileHandles;
1460 UINTN FvHandleCount;
1461 EFI_HANDLE *FvHandleBuffer;
1462 EFI_FV_FILETYPE Type;
1463 UINTN Size;
1464 EFI_FV_FILE_ATTRIBUTES Attributes;
1465 UINT32 AuthenticationStatus;
1466 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1467 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1468 UINTN DevicePathType;
1469 CHAR16 Buffer[40];
1470 EFI_HANDLE *FileSystemHandles;
1471 UINTN NumberFileSystemHandles;
1472 BOOLEAN NeedDelete;
1473 EFI_IMAGE_DOS_HEADER DosHeader;
1474 CHAR8 *PlatLang;
1475 CHAR8 *LastLang;
1476 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
1477 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1478
1479 FloppyNumber = 0;
1480 HarddriveNumber = 0;
1481 CdromNumber = 0;
1482 UsbNumber = 0;
1483 MiscNumber = 0;
1484 ScsiNumber = 0;
1485 PlatLang = NULL;
1486 LastLang = NULL;
1487 ZeroMem (Buffer, sizeof (Buffer));
1488
1489 //
1490 // If the boot device enumerate happened, just get the boot
1491 // device from the boot order variable
1492 //
1493 if (mEnumBootDevice) {
1494 LastLang = GetVariable (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid);
1495 PlatLang = GetEfiGlobalVariable (L"PlatformLang");
1496 ASSERT (PlatLang != NULL);
1497 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {
1498 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
1499 FreePool (LastLang);
1500 FreePool (PlatLang);
1501 return Status;
1502 } else {
1503 Status = gRT->SetVariable (
1504 LAST_ENUM_LANGUAGE_VARIABLE_NAME,
1505 &gLastEnumLangGuid,
1506 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1507 AsciiStrSize (PlatLang),
1508 PlatLang
1509 );
1510 ASSERT_EFI_ERROR (Status);
1511
1512 if (LastLang != NULL) {
1513 FreePool (LastLang);
1514 }
1515 FreePool (PlatLang);
1516 }
1517 }
1518
1519 //
1520 // Notes: this dirty code is to get the legacy boot option from the
1521 // BBS table and create to variable as the EFI boot option, it should
1522 // be removed after the CSM can provide legacy boot option directly
1523 //
1524 REFRESH_LEGACY_BOOT_OPTIONS;
1525
1526 //
1527 // Delete invalid boot option
1528 //
1529 BdsDeleteAllInvalidEfiBootOption ();
1530
1531 //
1532 // Parse removable media followed by fixed media.
1533 // The Removable[] array is used by the for-loop below to create removable media boot options
1534 // at first, and then to create fixed media boot options.
1535 //
1536 Removable[0] = FALSE;
1537 Removable[1] = TRUE;
1538
1539 gBS->LocateHandleBuffer (
1540 ByProtocol,
1541 &gEfiBlockIoProtocolGuid,
1542 NULL,
1543 &NumberBlockIoHandles,
1544 &BlockIoHandles
1545 );
1546
1547 for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {
1548 for (Index = 0; Index < NumberBlockIoHandles; Index++) {
1549 Status = gBS->HandleProtocol (
1550 BlockIoHandles[Index],
1551 &gEfiBlockIoProtocolGuid,
1552 (VOID **) &BlkIo
1553 );
1554 //
1555 // skip the fixed block io then the removable block io
1556 //
1557 if (EFI_ERROR (Status) || (BlkIo->Media->RemovableMedia == Removable[RemovableIndex])) {
1558 continue;
1559 }
1560 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);
1561 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);
1562
1563 switch (DevicePathType) {
1564 case BDS_EFI_ACPI_FLOPPY_BOOT:
1565 if (FloppyNumber != 0) {
1566 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);
1567 } else {
1568 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));
1569 }
1570 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1571 FloppyNumber++;
1572 break;
1573
1574 //
1575 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
1576 //
1577 case BDS_EFI_MESSAGE_ATAPI_BOOT:
1578 case BDS_EFI_MESSAGE_SATA_BOOT:
1579 if (BlkIo->Media->RemovableMedia) {
1580 if (CdromNumber != 0) {
1581 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);
1582 } else {
1583 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));
1584 }
1585 CdromNumber++;
1586 } else {
1587 if (HarddriveNumber != 0) {
1588 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);
1589 } else {
1590 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));
1591 }
1592 HarddriveNumber++;
1593 }
1594 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));
1595 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1596 break;
1597
1598 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:
1599 if (UsbNumber != 0) {
1600 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);
1601 } else {
1602 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));
1603 }
1604 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1605 UsbNumber++;
1606 break;
1607
1608 case BDS_EFI_MESSAGE_SCSI_BOOT:
1609 if (ScsiNumber != 0) {
1610 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);
1611 } else {
1612 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));
1613 }
1614 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1615 ScsiNumber++;
1616 break;
1617
1618 case BDS_EFI_MESSAGE_MISC_BOOT:
1619 if (MiscNumber != 0) {
1620 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);
1621 } else {
1622 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));
1623 }
1624 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
1625 MiscNumber++;
1626 break;
1627
1628 default:
1629 break;
1630 }
1631 }
1632 }
1633
1634 if (NumberBlockIoHandles != 0) {
1635 FreePool (BlockIoHandles);
1636 }
1637
1638 //
1639 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
1640 //
1641 NonBlockNumber = 0;
1642 gBS->LocateHandleBuffer (
1643 ByProtocol,
1644 &gEfiSimpleFileSystemProtocolGuid,
1645 NULL,
1646 &NumberFileSystemHandles,
1647 &FileSystemHandles
1648 );
1649 for (Index = 0; Index < NumberFileSystemHandles; Index++) {
1650 Status = gBS->HandleProtocol (
1651 FileSystemHandles[Index],
1652 &gEfiBlockIoProtocolGuid,
1653 (VOID **) &BlkIo
1654 );
1655 if (!EFI_ERROR (Status)) {
1656 //
1657 // Skip if the file system handle supports a BlkIo protocol,
1658 //
1659 continue;
1660 }
1661
1662 //
1663 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
1664 // machinename is ia32, ia64, x64, ...
1665 //
1666 Hdr.Union = &HdrData;
1667 NeedDelete = TRUE;
1668 Status = BdsLibGetImageHeader (
1669 FileSystemHandles[Index],
1670 EFI_REMOVABLE_MEDIA_FILE_NAME,
1671 &DosHeader,
1672 Hdr
1673 );
1674 if (!EFI_ERROR (Status) &&
1675 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
1676 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
1677 NeedDelete = FALSE;
1678 }
1679
1680 if (NeedDelete) {
1681 //
1682 // No such file or the file is not a EFI application, delete this boot option
1683 //
1684 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);
1685 } else {
1686 if (NonBlockNumber != 0) {
1687 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);
1688 } else {
1689 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));
1690 }
1691 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);
1692 NonBlockNumber++;
1693 }
1694 }
1695
1696 if (NumberFileSystemHandles != 0) {
1697 FreePool (FileSystemHandles);
1698 }
1699
1700 //
1701 // Parse Network Boot Device
1702 //
1703 NumOfLoadFileHandles = 0;
1704 //
1705 // Search Load File protocol for PXE boot option.
1706 //
1707 gBS->LocateHandleBuffer (
1708 ByProtocol,
1709 &gEfiLoadFileProtocolGuid,
1710 NULL,
1711 &NumOfLoadFileHandles,
1712 &LoadFileHandles
1713 );
1714
1715 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {
1716 if (Index != 0) {
1717 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);
1718 } else {
1719 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));
1720 }
1721 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);
1722 }
1723
1724 if (NumOfLoadFileHandles != 0) {
1725 FreePool (LoadFileHandles);
1726 }
1727
1728 //
1729 // Check if we have on flash shell
1730 //
1731 gBS->LocateHandleBuffer (
1732 ByProtocol,
1733 &gEfiFirmwareVolume2ProtocolGuid,
1734 NULL,
1735 &FvHandleCount,
1736 &FvHandleBuffer
1737 );
1738 for (Index = 0; Index < FvHandleCount; Index++) {
1739 gBS->HandleProtocol (
1740 FvHandleBuffer[Index],
1741 &gEfiFirmwareVolume2ProtocolGuid,
1742 (VOID **) &Fv
1743 );
1744
1745 Status = Fv->ReadFile (
1746 Fv,
1747 PcdGetPtr(PcdShellFile),
1748 NULL,
1749 &Size,
1750 &Type,
1751 &Attributes,
1752 &AuthenticationStatus
1753 );
1754 if (EFI_ERROR (Status)) {
1755 //
1756 // Skip if no shell file in the FV
1757 //
1758 continue;
1759 }
1760 //
1761 // Build the shell boot option
1762 //
1763 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);
1764 }
1765
1766 if (FvHandleCount != 0) {
1767 FreePool (FvHandleBuffer);
1768 }
1769 //
1770 // Make sure every boot only have one time
1771 // boot device enumerate
1772 //
1773 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
1774 mEnumBootDevice = TRUE;
1775
1776 return Status;
1777 }
1778
1779 /**
1780 Build the boot option with the handle parsed in
1781
1782 @param Handle The handle which present the device path to create
1783 boot option
1784 @param BdsBootOptionList The header of the link list which indexed all
1785 current boot options
1786 @param String The description of the boot option.
1787
1788 **/
1789 VOID
1790 EFIAPI
1791 BdsLibBuildOptionFromHandle (
1792 IN EFI_HANDLE Handle,
1793 IN LIST_ENTRY *BdsBootOptionList,
1794 IN CHAR16 *String
1795 )
1796 {
1797 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1798
1799 DevicePath = DevicePathFromHandle (Handle);
1800
1801 //
1802 // Create and register new boot option
1803 //
1804 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");
1805 }
1806
1807
1808 /**
1809 Build the on flash shell boot option with the handle parsed in.
1810
1811 @param Handle The handle which present the device path to create
1812 on flash shell boot option
1813 @param BdsBootOptionList The header of the link list which indexed all
1814 current boot options
1815
1816 **/
1817 VOID
1818 EFIAPI
1819 BdsLibBuildOptionFromShell (
1820 IN EFI_HANDLE Handle,
1821 IN OUT LIST_ENTRY *BdsBootOptionList
1822 )
1823 {
1824 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1825 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;
1826
1827 DevicePath = DevicePathFromHandle (Handle);
1828
1829 //
1830 // Build the shell device path
1831 //
1832 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));
1833
1834 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);
1835
1836 //
1837 // Create and register the shell boot option
1838 //
1839 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");
1840
1841 }
1842
1843 /**
1844 Boot from the UEFI spec defined "BootNext" variable.
1845
1846 **/
1847 VOID
1848 EFIAPI
1849 BdsLibBootNext (
1850 VOID
1851 )
1852 {
1853 UINT16 *BootNext;
1854 UINTN BootNextSize;
1855 CHAR16 Buffer[20];
1856 BDS_COMMON_OPTION *BootOption;
1857 LIST_ENTRY TempList;
1858 UINTN ExitDataSize;
1859 CHAR16 *ExitData;
1860
1861 //
1862 // Init the boot option name buffer and temp link list
1863 //
1864 InitializeListHead (&TempList);
1865 ZeroMem (Buffer, sizeof (Buffer));
1866
1867 BootNext = BdsLibGetVariableAndSize (
1868 L"BootNext",
1869 &gEfiGlobalVariableGuid,
1870 &BootNextSize
1871 );
1872
1873 //
1874 // Clear the boot next variable first
1875 //
1876 if (BootNext != NULL) {
1877 gRT->SetVariable (
1878 L"BootNext",
1879 &gEfiGlobalVariableGuid,
1880 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1881 0,
1882 BootNext
1883 );
1884
1885 //
1886 // Start to build the boot option and try to boot
1887 //
1888 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);
1889 BootOption = BdsLibVariableToOption (&TempList, Buffer);
1890 ASSERT (BootOption != NULL);
1891 BdsLibConnectDevicePath (BootOption->DevicePath);
1892 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
1893 }
1894
1895 }
1896
1897 /**
1898 Return the bootable media handle.
1899 First, check the device is connected
1900 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
1901 Third, detect the the default boot file in the Media, and return the removable Media handle.
1902
1903 @param DevicePath Device Path to a bootable device
1904
1905 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
1906
1907 **/
1908 EFI_HANDLE
1909 EFIAPI
1910 BdsLibGetBootableHandle (
1911 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1912 )
1913 {
1914 EFI_STATUS Status;
1915 EFI_TPL OldTpl;
1916 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
1917 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
1918 EFI_HANDLE Handle;
1919 EFI_BLOCK_IO_PROTOCOL *BlockIo;
1920 VOID *Buffer;
1921 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1922 UINTN Size;
1923 UINTN TempSize;
1924 EFI_HANDLE ReturnHandle;
1925 EFI_HANDLE *SimpleFileSystemHandles;
1926
1927 UINTN NumberSimpleFileSystemHandles;
1928 UINTN Index;
1929 EFI_IMAGE_DOS_HEADER DosHeader;
1930 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
1931 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1932
1933 UpdatedDevicePath = DevicePath;
1934
1935 //
1936 // Enter to critical section to protect the acquired BlockIo instance
1937 // from getting released due to the USB mass storage hotplug event
1938 //
1939 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1940
1941 //
1942 // Check whether the device is connected
1943 //
1944 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);
1945 if (EFI_ERROR (Status)) {
1946 //
1947 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
1948 //
1949 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);
1950 if (EFI_ERROR (Status)) {
1951 //
1952 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
1953 //
1954 UpdatedDevicePath = DevicePath;
1955 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
1956 gBS->ConnectController (Handle, NULL, NULL, TRUE);
1957 }
1958 } else {
1959 //
1960 // For removable device boot option, its contained device path only point to the removable device handle,
1961 // should make sure all its children handles (its child partion or media handles) are created and connected.
1962 //
1963 gBS->ConnectController (Handle, NULL, NULL, TRUE);
1964 //
1965 // Get BlockIo protocol and check removable attribute
1966 //
1967 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
1968 ASSERT_EFI_ERROR (Status);
1969
1970 //
1971 // Issue a dummy read to the device to check for media change.
1972 // When the removable media is changed, any Block IO read/write will
1973 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1974 // returned. After the Block IO protocol is reinstalled, subsequent
1975 // Block IO read/write will success.
1976 //
1977 Buffer = AllocatePool (BlockIo->Media->BlockSize);
1978 if (Buffer != NULL) {
1979 BlockIo->ReadBlocks (
1980 BlockIo,
1981 BlockIo->Media->MediaId,
1982 0,
1983 BlockIo->Media->BlockSize,
1984 Buffer
1985 );
1986 FreePool(Buffer);
1987 }
1988 }
1989
1990 //
1991 // Detect the the default boot file from removable Media
1992 //
1993
1994 //
1995 // 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
1996 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
1997 //
1998 DupDevicePath = DuplicateDevicePath (DevicePath);
1999 ASSERT (DupDevicePath != NULL);
2000
2001 UpdatedDevicePath = DupDevicePath;
2002 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
2003 //
2004 // 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
2005 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
2006 //
2007 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&
2008 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {
2009 //
2010 // Remove the usb node, let the device path only point to PCI node
2011 //
2012 SetDevicePathEndNode (UpdatedDevicePath);
2013 UpdatedDevicePath = DupDevicePath;
2014 } else {
2015 UpdatedDevicePath = DevicePath;
2016 }
2017
2018 //
2019 // Get the device path size of boot option
2020 //
2021 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
2022 ReturnHandle = NULL;
2023 gBS->LocateHandleBuffer (
2024 ByProtocol,
2025 &gEfiSimpleFileSystemProtocolGuid,
2026 NULL,
2027 &NumberSimpleFileSystemHandles,
2028 &SimpleFileSystemHandles
2029 );
2030 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
2031 //
2032 // Get the device path size of SimpleFileSystem handle
2033 //
2034 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
2035 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
2036 //
2037 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
2038 //
2039 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {
2040 //
2041 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2042 // machinename is ia32, ia64, x64, ...
2043 //
2044 Hdr.Union = &HdrData;
2045 Status = BdsLibGetImageHeader (
2046 SimpleFileSystemHandles[Index],
2047 EFI_REMOVABLE_MEDIA_FILE_NAME,
2048 &DosHeader,
2049 Hdr
2050 );
2051 if (!EFI_ERROR (Status) &&
2052 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
2053 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
2054 ReturnHandle = SimpleFileSystemHandles[Index];
2055 break;
2056 }
2057 }
2058 }
2059
2060 FreePool(DupDevicePath);
2061
2062 if (SimpleFileSystemHandles != NULL) {
2063 FreePool(SimpleFileSystemHandles);
2064 }
2065
2066 gBS->RestoreTPL (OldTpl);
2067
2068 return ReturnHandle;
2069 }
2070
2071 /**
2072 Check to see if the network cable is plugged in. If the DevicePath is not
2073 connected it will be connected.
2074
2075 @param DevicePath Device Path to check
2076
2077 @retval TRUE DevicePath points to an Network that is connected
2078 @retval FALSE DevicePath does not point to a bootable network
2079
2080 **/
2081 BOOLEAN
2082 BdsLibNetworkBootWithMediaPresent (
2083 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
2084 )
2085 {
2086 EFI_STATUS Status;
2087 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
2088 EFI_HANDLE Handle;
2089 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
2090 BOOLEAN MediaPresent;
2091 UINT32 InterruptStatus;
2092
2093 MediaPresent = FALSE;
2094
2095 UpdatedDevicePath = DevicePath;
2096 //
2097 // Locate Load File Protocol for PXE boot option first
2098 //
2099 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
2100 if (EFI_ERROR (Status)) {
2101 //
2102 // Device not present so see if we need to connect it
2103 //
2104 Status = BdsLibConnectDevicePath (DevicePath);
2105 if (!EFI_ERROR (Status)) {
2106 //
2107 // This one should work after we did the connect
2108 //
2109 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
2110 }
2111 }
2112
2113 if (!EFI_ERROR (Status)) {
2114 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
2115 if (EFI_ERROR (Status)) {
2116 //
2117 // Failed to open SNP from this handle, try to get SNP from parent handle
2118 //
2119 UpdatedDevicePath = DevicePathFromHandle (Handle);
2120 if (UpdatedDevicePath != NULL) {
2121 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);
2122 if (!EFI_ERROR (Status)) {
2123 //
2124 // SNP handle found, get SNP from it
2125 //
2126 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);
2127 }
2128 }
2129 }
2130
2131 if (!EFI_ERROR (Status)) {
2132 if (Snp->Mode->MediaPresentSupported) {
2133 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {
2134 //
2135 // Invoke Snp->GetStatus() to refresh the media status
2136 //
2137 Snp->GetStatus (Snp, &InterruptStatus, NULL);
2138
2139 //
2140 // In case some one else is using the SNP check to see if it's connected
2141 //
2142 MediaPresent = Snp->Mode->MediaPresent;
2143 } else {
2144 //
2145 // No one is using SNP so we need to Start and Initialize so
2146 // MediaPresent will be valid.
2147 //
2148 Status = Snp->Start (Snp);
2149 if (!EFI_ERROR (Status)) {
2150 Status = Snp->Initialize (Snp, 0, 0);
2151 if (!EFI_ERROR (Status)) {
2152 MediaPresent = Snp->Mode->MediaPresent;
2153 Snp->Shutdown (Snp);
2154 }
2155 Snp->Stop (Snp);
2156 }
2157 }
2158 } else {
2159 MediaPresent = TRUE;
2160 }
2161 }
2162 }
2163
2164 return MediaPresent;
2165 }
2166
2167 /**
2168 For a bootable Device path, return its boot type.
2169
2170 @param DevicePath The bootable device Path to check
2171
2172 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
2173 which subtype is MEDIA_HARDDRIVE_DP
2174 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
2175 which subtype is MEDIA_CDROM_DP
2176 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
2177 which HID is floppy device.
2178 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2179 and its last device path node's subtype is MSG_ATAPI_DP.
2180 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2181 and its last device path node's subtype is MSG_SCSI_DP.
2182 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
2183 and its last device path node's subtype is MSG_USB_DP.
2184 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
2185 its last device path node point to a message device path node.
2186 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
2187 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
2188
2189 **/
2190 UINT32
2191 EFIAPI
2192 BdsGetBootTypeFromDevicePath (
2193 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
2194 )
2195 {
2196 ACPI_HID_DEVICE_PATH *Acpi;
2197 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
2198 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
2199 UINT32 BootType;
2200
2201 if (NULL == DevicePath) {
2202 return BDS_EFI_UNSUPPORT;
2203 }
2204
2205 TempDevicePath = DevicePath;
2206
2207 while (!IsDevicePathEndType (TempDevicePath)) {
2208 switch (DevicePathType (TempDevicePath)) {
2209 case BBS_DEVICE_PATH:
2210 return BDS_LEGACY_BBS_BOOT;
2211 case MEDIA_DEVICE_PATH:
2212 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {
2213 return BDS_EFI_MEDIA_HD_BOOT;
2214 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {
2215 return BDS_EFI_MEDIA_CDROM_BOOT;
2216 }
2217 break;
2218 case ACPI_DEVICE_PATH:
2219 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;
2220 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {
2221 return BDS_EFI_ACPI_FLOPPY_BOOT;
2222 }
2223 break;
2224 case MESSAGING_DEVICE_PATH:
2225 //
2226 // Get the last device path node
2227 //
2228 LastDeviceNode = NextDevicePathNode (TempDevicePath);
2229 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {
2230 //
2231 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
2232 // skip it
2233 //
2234 LastDeviceNode = NextDevicePathNode (LastDeviceNode);
2235 }
2236 //
2237 // if the device path not only point to driver device, it is not a messaging device path,
2238 //
2239 if (!IsDevicePathEndType (LastDeviceNode)) {
2240 break;
2241 }
2242
2243 switch (DevicePathSubType (TempDevicePath)) {
2244 case MSG_ATAPI_DP:
2245 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;
2246 break;
2247
2248 case MSG_USB_DP:
2249 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;
2250 break;
2251
2252 case MSG_SCSI_DP:
2253 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;
2254 break;
2255
2256 case MSG_SATA_DP:
2257 BootType = BDS_EFI_MESSAGE_SATA_BOOT;
2258 break;
2259
2260 case MSG_MAC_ADDR_DP:
2261 case MSG_VLAN_DP:
2262 case MSG_IPv4_DP:
2263 case MSG_IPv6_DP:
2264 BootType = BDS_EFI_MESSAGE_MAC_BOOT;
2265 break;
2266
2267 default:
2268 BootType = BDS_EFI_MESSAGE_MISC_BOOT;
2269 break;
2270 }
2271 return BootType;
2272
2273 default:
2274 break;
2275 }
2276 TempDevicePath = NextDevicePathNode (TempDevicePath);
2277 }
2278
2279 return BDS_EFI_UNSUPPORT;
2280 }
2281
2282 /**
2283 Check whether the Device path in a boot option point to a valid bootable device,
2284 And if CheckMedia is true, check the device is ready to boot now.
2285
2286 @param DevPath the Device path in a boot option
2287 @param CheckMedia if true, check the device is ready to boot now.
2288
2289 @retval TRUE the Device path is valid
2290 @retval FALSE the Device path is invalid .
2291
2292 **/
2293 BOOLEAN
2294 EFIAPI
2295 BdsLibIsValidEFIBootOptDevicePath (
2296 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
2297 IN BOOLEAN CheckMedia
2298 )
2299 {
2300 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);
2301 }
2302
2303 /**
2304 Check whether the Device path in a boot option point to a valid bootable device,
2305 And if CheckMedia is true, check the device is ready to boot now.
2306 If Description is not NULL and the device path point to a fixed BlockIo
2307 device, check the description whether conflict with other auto-created
2308 boot options.
2309
2310 @param DevPath the Device path in a boot option
2311 @param CheckMedia if true, check the device is ready to boot now.
2312 @param Description the description in a boot option
2313
2314 @retval TRUE the Device path is valid
2315 @retval FALSE the Device path is invalid .
2316
2317 **/
2318 BOOLEAN
2319 EFIAPI
2320 BdsLibIsValidEFIBootOptDevicePathExt (
2321 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
2322 IN BOOLEAN CheckMedia,
2323 IN CHAR16 *Description
2324 )
2325 {
2326 EFI_STATUS Status;
2327 EFI_HANDLE Handle;
2328 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
2329 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
2330 EFI_BLOCK_IO_PROTOCOL *BlockIo;
2331
2332 TempDevicePath = DevPath;
2333 LastDeviceNode = DevPath;
2334
2335 //
2336 // Check if it's a valid boot option for network boot device.
2337 // Check if there is EfiLoadFileProtocol installed.
2338 // If yes, that means there is a boot option for network.
2339 //
2340 Status = gBS->LocateDevicePath (
2341 &gEfiLoadFileProtocolGuid,
2342 &TempDevicePath,
2343 &Handle
2344 );
2345 if (EFI_ERROR (Status)) {
2346 //
2347 // Device not present so see if we need to connect it
2348 //
2349 TempDevicePath = DevPath;
2350 BdsLibConnectDevicePath (TempDevicePath);
2351 Status = gBS->LocateDevicePath (
2352 &gEfiLoadFileProtocolGuid,
2353 &TempDevicePath,
2354 &Handle
2355 );
2356 }
2357
2358 if (!EFI_ERROR (Status)) {
2359 if (!IsDevicePathEnd (TempDevicePath)) {
2360 //
2361 // LoadFile protocol is not installed on handle with exactly the same DevPath
2362 //
2363 return FALSE;
2364 }
2365
2366 if (CheckMedia) {
2367 //
2368 // Test if it is ready to boot now
2369 //
2370 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {
2371 return TRUE;
2372 }
2373 } else {
2374 return TRUE;
2375 }
2376 }
2377
2378 //
2379 // If the boot option point to a file, it is a valid EFI boot option,
2380 // and assume it is ready to boot now
2381 //
2382 while (!IsDevicePathEnd (TempDevicePath)) {
2383 //
2384 // If there is USB Class or USB WWID device path node, treat it as valid EFI
2385 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
2386 // to full device path.
2387 //
2388 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&
2389 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||
2390 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {
2391 return TRUE;
2392 }
2393
2394 LastDeviceNode = TempDevicePath;
2395 TempDevicePath = NextDevicePathNode (TempDevicePath);
2396 }
2397 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&
2398 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {
2399 return TRUE;
2400 }
2401
2402 //
2403 // Check if it's a valid boot option for internal FV application
2404 //
2405 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {
2406 //
2407 // If the boot option point to internal FV application, make sure it is valid
2408 //
2409 TempDevicePath = DevPath;
2410 Status = BdsLibUpdateFvFileDevicePath (
2411 &TempDevicePath,
2412 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)
2413 );
2414 if (Status == EFI_ALREADY_STARTED) {
2415 return TRUE;
2416 } else {
2417 if (Status == EFI_SUCCESS) {
2418 FreePool (TempDevicePath);
2419 }
2420 return FALSE;
2421 }
2422 }
2423
2424 //
2425 // If the boot option point to a blockIO device:
2426 // if it is a removable blockIo device, it is valid.
2427 // if it is a fixed blockIo device, check its description confliction.
2428 //
2429 TempDevicePath = DevPath;
2430 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
2431 if (EFI_ERROR (Status)) {
2432 //
2433 // Device not present so see if we need to connect it
2434 //
2435 Status = BdsLibConnectDevicePath (DevPath);
2436 if (!EFI_ERROR (Status)) {
2437 //
2438 // Try again to get the Block Io protocol after we did the connect
2439 //
2440 TempDevicePath = DevPath;
2441 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
2442 }
2443 }
2444
2445 if (!EFI_ERROR (Status)) {
2446 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
2447 if (!EFI_ERROR (Status)) {
2448 if (CheckMedia) {
2449 //
2450 // Test if it is ready to boot now
2451 //
2452 if (BdsLibGetBootableHandle (DevPath) != NULL) {
2453 return TRUE;
2454 }
2455 } else {
2456 return TRUE;
2457 }
2458 }
2459 } else {
2460 //
2461 // 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,
2462 //
2463 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);
2464 if (!EFI_ERROR (Status)) {
2465 if (CheckMedia) {
2466 //
2467 // Test if it is ready to boot now
2468 //
2469 if (BdsLibGetBootableHandle (DevPath) != NULL) {
2470 return TRUE;
2471 }
2472 } else {
2473 return TRUE;
2474 }
2475 }
2476 }
2477
2478 return FALSE;
2479 }
2480
2481
2482 /**
2483 According to a file guild, check a Fv file device path is valid. If it is invalid,
2484 try to return the valid device path.
2485 FV address maybe changes for memory layout adjust from time to time, use this function
2486 could promise the Fv file device path is right.
2487
2488 @param DevicePath on input, the Fv file device path need to check on
2489 output, the updated valid Fv file device path
2490 @param FileGuid the Fv file guild
2491
2492 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
2493 parameter
2494 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
2495 guild at all
2496 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
2497 valid
2498 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
2499 and return the updated device path in DevicePath
2500
2501 **/
2502 EFI_STATUS
2503 EFIAPI
2504 BdsLibUpdateFvFileDevicePath (
2505 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
2506 IN EFI_GUID *FileGuid
2507 )
2508 {
2509 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
2510 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
2511 EFI_STATUS Status;
2512 EFI_GUID *GuidPoint;
2513 UINTN Index;
2514 UINTN FvHandleCount;
2515 EFI_HANDLE *FvHandleBuffer;
2516 EFI_FV_FILETYPE Type;
2517 UINTN Size;
2518 EFI_FV_FILE_ATTRIBUTES Attributes;
2519 UINT32 AuthenticationStatus;
2520 BOOLEAN FindFvFile;
2521 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
2522 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
2523 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;
2524 EFI_HANDLE FoundFvHandle;
2525 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
2526
2527 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
2528 return EFI_INVALID_PARAMETER;
2529 }
2530 if (FileGuid == NULL) {
2531 return EFI_INVALID_PARAMETER;
2532 }
2533
2534 //
2535 // Check whether the device path point to the default the input Fv file
2536 //
2537 TempDevicePath = *DevicePath;
2538 LastDeviceNode = TempDevicePath;
2539 while (!IsDevicePathEnd (TempDevicePath)) {
2540 LastDeviceNode = TempDevicePath;
2541 TempDevicePath = NextDevicePathNode (TempDevicePath);
2542 }
2543 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
2544 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
2545 );
2546 if (GuidPoint == NULL) {
2547 //
2548 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
2549 //
2550 return EFI_UNSUPPORTED;
2551 }
2552 if (!CompareGuid (GuidPoint, FileGuid)) {
2553 //
2554 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
2555 //
2556 return EFI_UNSUPPORTED;
2557 }
2558
2559 //
2560 // Check whether the input Fv file device path is valid
2561 //
2562 TempDevicePath = *DevicePath;
2563 FoundFvHandle = NULL;
2564 Status = gBS->LocateDevicePath (
2565 &gEfiFirmwareVolume2ProtocolGuid,
2566 &TempDevicePath,
2567 &FoundFvHandle
2568 );
2569 if (!EFI_ERROR (Status)) {
2570 Status = gBS->HandleProtocol (
2571 FoundFvHandle,
2572 &gEfiFirmwareVolume2ProtocolGuid,
2573 (VOID **) &Fv
2574 );
2575 if (!EFI_ERROR (Status)) {
2576 //
2577 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
2578 //
2579 Status = Fv->ReadFile (
2580 Fv,
2581 FileGuid,
2582 NULL,
2583 &Size,
2584 &Type,
2585 &Attributes,
2586 &AuthenticationStatus
2587 );
2588 if (!EFI_ERROR (Status)) {
2589 return EFI_ALREADY_STARTED;
2590 }
2591 }
2592 }
2593
2594 //
2595 // Look for the input wanted FV file in current FV
2596 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
2597 //
2598 FindFvFile = FALSE;
2599 FoundFvHandle = NULL;
2600 Status = gBS->HandleProtocol (
2601 gImageHandle,
2602 &gEfiLoadedImageProtocolGuid,
2603 (VOID **) &LoadedImage
2604 );
2605 if (!EFI_ERROR (Status)) {
2606 Status = gBS->HandleProtocol (
2607 LoadedImage->DeviceHandle,
2608 &gEfiFirmwareVolume2ProtocolGuid,
2609 (VOID **) &Fv
2610 );
2611 if (!EFI_ERROR (Status)) {
2612 Status = Fv->ReadFile (
2613 Fv,
2614 FileGuid,
2615 NULL,
2616 &Size,
2617 &Type,
2618 &Attributes,
2619 &AuthenticationStatus
2620 );
2621 if (!EFI_ERROR (Status)) {
2622 FindFvFile = TRUE;
2623 FoundFvHandle = LoadedImage->DeviceHandle;
2624 }
2625 }
2626 }
2627 //
2628 // Second, if fail to find, try to enumerate all FV
2629 //
2630 if (!FindFvFile) {
2631 FvHandleBuffer = NULL;
2632 gBS->LocateHandleBuffer (
2633 ByProtocol,
2634 &gEfiFirmwareVolume2ProtocolGuid,
2635 NULL,
2636 &FvHandleCount,
2637 &FvHandleBuffer
2638 );
2639 for (Index = 0; Index < FvHandleCount; Index++) {
2640 gBS->HandleProtocol (
2641 FvHandleBuffer[Index],
2642 &gEfiFirmwareVolume2ProtocolGuid,
2643 (VOID **) &Fv
2644 );
2645
2646 Status = Fv->ReadFile (
2647 Fv,
2648 FileGuid,
2649 NULL,
2650 &Size,
2651 &Type,
2652 &Attributes,
2653 &AuthenticationStatus
2654 );
2655 if (EFI_ERROR (Status)) {
2656 //
2657 // Skip if input Fv file not in the FV
2658 //
2659 continue;
2660 }
2661 FindFvFile = TRUE;
2662 FoundFvHandle = FvHandleBuffer[Index];
2663 break;
2664 }
2665
2666 if (FvHandleBuffer != NULL) {
2667 FreePool (FvHandleBuffer);
2668 }
2669 }
2670
2671 if (FindFvFile) {
2672 //
2673 // Build the shell device path
2674 //
2675 NewDevicePath = DevicePathFromHandle (FoundFvHandle);
2676 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
2677 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
2678 *DevicePath = NewDevicePath;
2679 return EFI_SUCCESS;
2680 }
2681 return EFI_NOT_FOUND;
2682 }