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