]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
MdeModulePkg/UefiBootManagerLib: Skip the DNS device path node check.
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmBoot.c
1 /** @file
2 Library functions which relates with booting.
3
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "InternalBm.h"
17
18 EFI_RAM_DISK_PROTOCOL *mRamDisk = NULL;
19
20 EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION mBmRefreshLegacyBootOption = NULL;
21 EFI_BOOT_MANAGER_LEGACY_BOOT mBmLegacyBoot = NULL;
22
23 ///
24 /// This GUID is used for an EFI Variable that stores the front device pathes
25 /// for a partial device path that starts with the HD node.
26 ///
27 EFI_GUID mBmHardDriveBootVariableGuid = { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x08, 0xe2, 0x0e, 0x90, 0x6c, 0xb6, 0xde } };
28 EFI_GUID mBmAutoCreateBootOptionGuid = { 0x8108ac4e, 0x9f11, 0x4d59, { 0x85, 0x0e, 0xe2, 0x1a, 0x52, 0x2c, 0x59, 0xb2 } };
29
30 /**
31 The function registers the legacy boot support capabilities.
32
33 @param RefreshLegacyBootOption The function pointer to create all the legacy boot options.
34 @param LegacyBoot The function pointer to boot the legacy boot option.
35 **/
36 VOID
37 EFIAPI
38 EfiBootManagerRegisterLegacyBootSupport (
39 EFI_BOOT_MANAGER_REFRESH_LEGACY_BOOT_OPTION RefreshLegacyBootOption,
40 EFI_BOOT_MANAGER_LEGACY_BOOT LegacyBoot
41 )
42 {
43 mBmRefreshLegacyBootOption = RefreshLegacyBootOption;
44 mBmLegacyBoot = LegacyBoot;
45 }
46
47 /**
48 Return TRUE when the boot option is auto-created instead of manually added.
49
50 @param BootOption Pointer to the boot option to check.
51
52 @retval TRUE The boot option is auto-created.
53 @retval FALSE The boot option is manually added.
54 **/
55 BOOLEAN
56 BmIsAutoCreateBootOption (
57 EFI_BOOT_MANAGER_LOAD_OPTION *BootOption
58 )
59 {
60 if ((BootOption->OptionalDataSize == sizeof (EFI_GUID)) &&
61 CompareGuid ((EFI_GUID *) BootOption->OptionalData, &mBmAutoCreateBootOptionGuid)
62 ) {
63 return TRUE;
64 } else {
65 return FALSE;
66 }
67 }
68
69 /**
70 Find the boot option in the NV storage and return the option number.
71
72 @param OptionToFind Boot option to be checked.
73
74 @return The option number of the found boot option.
75
76 **/
77 UINTN
78 BmFindBootOptionInVariable (
79 IN EFI_BOOT_MANAGER_LOAD_OPTION *OptionToFind
80 )
81 {
82 EFI_STATUS Status;
83 EFI_BOOT_MANAGER_LOAD_OPTION BootOption;
84 UINTN OptionNumber;
85 CHAR16 OptionName[BM_OPTION_NAME_LEN];
86 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
87 UINTN BootOptionCount;
88 UINTN Index;
89
90 OptionNumber = LoadOptionNumberUnassigned;
91
92 //
93 // Try to match the variable exactly if the option number is assigned
94 //
95 if (OptionToFind->OptionNumber != LoadOptionNumberUnassigned) {
96 UnicodeSPrint (
97 OptionName, sizeof (OptionName), L"%s%04x",
98 mBmLoadOptionName[OptionToFind->OptionType], OptionToFind->OptionNumber
99 );
100 Status = EfiBootManagerVariableToLoadOption (OptionName, &BootOption);
101
102 if (!EFI_ERROR (Status)) {
103 ASSERT (OptionToFind->OptionNumber == BootOption.OptionNumber);
104 if ((OptionToFind->Attributes == BootOption.Attributes) &&
105 (StrCmp (OptionToFind->Description, BootOption.Description) == 0) &&
106 (CompareMem (OptionToFind->FilePath, BootOption.FilePath, GetDevicePathSize (OptionToFind->FilePath)) == 0) &&
107 (OptionToFind->OptionalDataSize == BootOption.OptionalDataSize) &&
108 (CompareMem (OptionToFind->OptionalData, BootOption.OptionalData, OptionToFind->OptionalDataSize) == 0)
109 ) {
110 OptionNumber = OptionToFind->OptionNumber;
111 }
112 EfiBootManagerFreeLoadOption (&BootOption);
113 }
114 }
115
116 //
117 // The option number assigned is either incorrect or unassigned.
118 //
119 if (OptionNumber == LoadOptionNumberUnassigned) {
120 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
121
122 Index = EfiBootManagerFindLoadOption (OptionToFind, BootOptions, BootOptionCount);
123 if (Index != -1) {
124 OptionNumber = BootOptions[Index].OptionNumber;
125 }
126
127 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
128 }
129
130 return OptionNumber;
131 }
132
133 /**
134 Return the correct FV file path.
135 FV address may change across reboot. This routine promises the FV file device path is right.
136
137 @param FilePath The Memory Mapped Device Path to get the file buffer.
138
139 @return The updated FV Device Path pointint to the file.
140 **/
141 EFI_DEVICE_PATH_PROTOCOL *
142 BmAdjustFvFilePath (
143 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
144 )
145 {
146 EFI_STATUS Status;
147 UINTN Index;
148 EFI_DEVICE_PATH_PROTOCOL *FvFileNode;
149 EFI_HANDLE FvHandle;
150 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
151 UINTN FvHandleCount;
152 EFI_HANDLE *FvHandles;
153 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
154 EFI_DEVICE_PATH_PROTOCOL *FullPath;
155
156 //
157 // Get the file buffer by using the exactly FilePath.
158 //
159 FvFileNode = FilePath;
160 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &FvFileNode, &FvHandle);
161 if (!EFI_ERROR (Status)) {
162 return DuplicateDevicePath (FilePath);
163 }
164
165 //
166 // Only wide match other FVs if it's a memory mapped FV file path.
167 //
168 if ((DevicePathType (FilePath) != HARDWARE_DEVICE_PATH) || (DevicePathSubType (FilePath) != HW_MEMMAP_DP)) {
169 return NULL;
170 }
171
172 FvFileNode = NextDevicePathNode (FilePath);
173
174 //
175 // Firstly find the FV file in current FV
176 //
177 gBS->HandleProtocol (
178 gImageHandle,
179 &gEfiLoadedImageProtocolGuid,
180 (VOID **) &LoadedImage
181 );
182 NewDevicePath = AppendDevicePathNode (DevicePathFromHandle (LoadedImage->DeviceHandle), FvFileNode);
183 FullPath = BmAdjustFvFilePath (NewDevicePath);
184 FreePool (NewDevicePath);
185 if (FullPath != NULL) {
186 return FullPath;
187 }
188
189 //
190 // Secondly find the FV file in all other FVs
191 //
192 gBS->LocateHandleBuffer (
193 ByProtocol,
194 &gEfiFirmwareVolume2ProtocolGuid,
195 NULL,
196 &FvHandleCount,
197 &FvHandles
198 );
199 for (Index = 0; Index < FvHandleCount; Index++) {
200 if (FvHandles[Index] == LoadedImage->DeviceHandle) {
201 //
202 // Skip current FV, it was handed in first step.
203 //
204 continue;
205 }
206 NewDevicePath = AppendDevicePathNode (DevicePathFromHandle (FvHandles[Index]), FvFileNode);
207 FullPath = BmAdjustFvFilePath (NewDevicePath);
208 FreePool (NewDevicePath);
209 if (FullPath != NULL) {
210 break;
211 }
212 }
213
214 if (FvHandles != NULL) {
215 FreePool (FvHandles);
216 }
217 return FullPath;
218 }
219
220 /**
221 Check if it's a Device Path pointing to FV file.
222
223 The function doesn't garentee the device path points to existing FV file.
224
225 @param DevicePath Input device path.
226
227 @retval TRUE The device path is a FV File Device Path.
228 @retval FALSE The device path is NOT a FV File Device Path.
229 **/
230 BOOLEAN
231 BmIsFvFilePath (
232 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
233 )
234 {
235 EFI_STATUS Status;
236 EFI_HANDLE Handle;
237 EFI_DEVICE_PATH_PROTOCOL *Node;
238
239 Node = DevicePath;
240 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &Handle);
241 if (!EFI_ERROR (Status)) {
242 return TRUE;
243 }
244
245 if ((DevicePathType (DevicePath) == HARDWARE_DEVICE_PATH) && (DevicePathSubType (DevicePath) == HW_MEMMAP_DP)) {
246 DevicePath = NextDevicePathNode (DevicePath);
247 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) && (DevicePathSubType (DevicePath) == MEDIA_PIWG_FW_FILE_DP)) {
248 return IsDevicePathEnd (NextDevicePathNode (DevicePath));
249 }
250 }
251 return FALSE;
252 }
253
254 /**
255 Check whether a USB device match the specified USB Class device path. This
256 function follows "Load Option Processing" behavior in UEFI specification.
257
258 @param UsbIo USB I/O protocol associated with the USB device.
259 @param UsbClass The USB Class device path to match.
260
261 @retval TRUE The USB device match the USB Class device path.
262 @retval FALSE The USB device does not match the USB Class device path.
263
264 **/
265 BOOLEAN
266 BmMatchUsbClass (
267 IN EFI_USB_IO_PROTOCOL *UsbIo,
268 IN USB_CLASS_DEVICE_PATH *UsbClass
269 )
270 {
271 EFI_STATUS Status;
272 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
273 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
274 UINT8 DeviceClass;
275 UINT8 DeviceSubClass;
276 UINT8 DeviceProtocol;
277
278 if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||
279 (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){
280 return FALSE;
281 }
282
283 //
284 // Check Vendor Id and Product Id.
285 //
286 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
287 if (EFI_ERROR (Status)) {
288 return FALSE;
289 }
290
291 if ((UsbClass->VendorId != 0xffff) &&
292 (UsbClass->VendorId != DevDesc.IdVendor)) {
293 return FALSE;
294 }
295
296 if ((UsbClass->ProductId != 0xffff) &&
297 (UsbClass->ProductId != DevDesc.IdProduct)) {
298 return FALSE;
299 }
300
301 DeviceClass = DevDesc.DeviceClass;
302 DeviceSubClass = DevDesc.DeviceSubClass;
303 DeviceProtocol = DevDesc.DeviceProtocol;
304 if (DeviceClass == 0) {
305 //
306 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
307 // Protocol in Interface Descriptor instead.
308 //
309 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
310 if (EFI_ERROR (Status)) {
311 return FALSE;
312 }
313
314 DeviceClass = IfDesc.InterfaceClass;
315 DeviceSubClass = IfDesc.InterfaceSubClass;
316 DeviceProtocol = IfDesc.InterfaceProtocol;
317 }
318
319 //
320 // Check Class, SubClass and Protocol.
321 //
322 if ((UsbClass->DeviceClass != 0xff) &&
323 (UsbClass->DeviceClass != DeviceClass)) {
324 return FALSE;
325 }
326
327 if ((UsbClass->DeviceSubClass != 0xff) &&
328 (UsbClass->DeviceSubClass != DeviceSubClass)) {
329 return FALSE;
330 }
331
332 if ((UsbClass->DeviceProtocol != 0xff) &&
333 (UsbClass->DeviceProtocol != DeviceProtocol)) {
334 return FALSE;
335 }
336
337 return TRUE;
338 }
339
340 /**
341 Check whether a USB device match the specified USB WWID device path. This
342 function follows "Load Option Processing" behavior in UEFI specification.
343
344 @param UsbIo USB I/O protocol associated with the USB device.
345 @param UsbWwid The USB WWID device path to match.
346
347 @retval TRUE The USB device match the USB WWID device path.
348 @retval FALSE The USB device does not match the USB WWID device path.
349
350 **/
351 BOOLEAN
352 BmMatchUsbWwid (
353 IN EFI_USB_IO_PROTOCOL *UsbIo,
354 IN USB_WWID_DEVICE_PATH *UsbWwid
355 )
356 {
357 EFI_STATUS Status;
358 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
359 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
360 UINT16 *LangIdTable;
361 UINT16 TableSize;
362 UINT16 Index;
363 CHAR16 *CompareStr;
364 UINTN CompareLen;
365 CHAR16 *SerialNumberStr;
366 UINTN Length;
367
368 if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||
369 (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP)) {
370 return FALSE;
371 }
372
373 //
374 // Check Vendor Id and Product Id.
375 //
376 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
377 if (EFI_ERROR (Status)) {
378 return FALSE;
379 }
380 if ((DevDesc.IdVendor != UsbWwid->VendorId) ||
381 (DevDesc.IdProduct != UsbWwid->ProductId)) {
382 return FALSE;
383 }
384
385 //
386 // Check Interface Number.
387 //
388 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
389 if (EFI_ERROR (Status)) {
390 return FALSE;
391 }
392 if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {
393 return FALSE;
394 }
395
396 //
397 // Check Serial Number.
398 //
399 if (DevDesc.StrSerialNumber == 0) {
400 return FALSE;
401 }
402
403 //
404 // Get all supported languages.
405 //
406 TableSize = 0;
407 LangIdTable = NULL;
408 Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);
409 if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {
410 return FALSE;
411 }
412
413 //
414 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
415 //
416 CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);
417 CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);
418 if (CompareStr[CompareLen - 1] == L'\0') {
419 CompareLen--;
420 }
421
422 //
423 // Compare serial number in each supported language.
424 //
425 for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {
426 SerialNumberStr = NULL;
427 Status = UsbIo->UsbGetStringDescriptor (
428 UsbIo,
429 LangIdTable[Index],
430 DevDesc.StrSerialNumber,
431 &SerialNumberStr
432 );
433 if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {
434 continue;
435 }
436
437 Length = StrLen (SerialNumberStr);
438 if ((Length >= CompareLen) &&
439 (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {
440 FreePool (SerialNumberStr);
441 return TRUE;
442 }
443
444 FreePool (SerialNumberStr);
445 }
446
447 return FALSE;
448 }
449
450 /**
451 Find a USB device which match the specified short-form device path start with
452 USB Class or USB WWID device path. If ParentDevicePath is NULL, this function
453 will search in all USB devices of the platform. If ParentDevicePath is not NULL,
454 this function will only search in its child devices.
455
456 @param DevicePath The device path that contains USB Class or USB WWID device path.
457 @param ParentDevicePathSize The length of the device path before the USB Class or
458 USB WWID device path.
459 @param UsbIoHandleCount A pointer to the count of the returned USB IO handles.
460
461 @retval NULL The matched USB IO handles cannot be found.
462 @retval other The matched USB IO handles.
463
464 **/
465 EFI_HANDLE *
466 BmFindUsbDevice (
467 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
468 IN UINTN ParentDevicePathSize,
469 OUT UINTN *UsbIoHandleCount
470 )
471 {
472 EFI_STATUS Status;
473 EFI_HANDLE *UsbIoHandles;
474 EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;
475 EFI_USB_IO_PROTOCOL *UsbIo;
476 UINTN Index;
477 BOOLEAN Matched;
478
479 ASSERT (UsbIoHandleCount != NULL);
480
481 //
482 // Get all UsbIo Handles.
483 //
484 Status = gBS->LocateHandleBuffer (
485 ByProtocol,
486 &gEfiUsbIoProtocolGuid,
487 NULL,
488 UsbIoHandleCount,
489 &UsbIoHandles
490 );
491 if (EFI_ERROR (Status)) {
492 *UsbIoHandleCount = 0;
493 UsbIoHandles = NULL;
494 }
495
496 for (Index = 0; Index < *UsbIoHandleCount; ) {
497 //
498 // Get the Usb IO interface.
499 //
500 Status = gBS->HandleProtocol(
501 UsbIoHandles[Index],
502 &gEfiUsbIoProtocolGuid,
503 (VOID **) &UsbIo
504 );
505 UsbIoDevicePath = DevicePathFromHandle (UsbIoHandles[Index]);
506 Matched = FALSE;
507 if (!EFI_ERROR (Status) && (UsbIoDevicePath != NULL)) {
508
509 //
510 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
511 //
512 if (CompareMem (UsbIoDevicePath, DevicePath, ParentDevicePathSize) == 0) {
513 if (BmMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ((UINTN) DevicePath + ParentDevicePathSize)) ||
514 BmMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ((UINTN) DevicePath + ParentDevicePathSize))) {
515 Matched = TRUE;
516 }
517 }
518 }
519
520 if (!Matched) {
521 (*UsbIoHandleCount) --;
522 CopyMem (&UsbIoHandles[Index], &UsbIoHandles[Index + 1], (*UsbIoHandleCount - Index) * sizeof (EFI_HANDLE));
523 } else {
524 Index++;
525 }
526 }
527
528 return UsbIoHandles;
529 }
530
531 /**
532 Expand USB Class or USB WWID device path node to be full device path of a USB
533 device in platform.
534
535 This function support following 4 cases:
536 1) Boot Option device path starts with a USB Class or USB WWID device path,
537 and there is no Media FilePath device path in the end.
538 In this case, it will follow Removable Media Boot Behavior.
539 2) Boot Option device path starts with a USB Class or USB WWID device path,
540 and ended with Media FilePath device path.
541 3) Boot Option device path starts with a full device path to a USB Host Controller,
542 contains a USB Class or USB WWID device path node, while not ended with Media
543 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
544 4) Boot Option device path starts with a full device path to a USB Host Controller,
545 contains a USB Class or USB WWID device path node, and ended with Media
546 FilePath device path.
547
548 @param FilePath The device path pointing to a load option.
549 It could be a short-form device path.
550 @param FullPath The full path returned by the routine in last call.
551 Set to NULL in first call.
552 @param ShortformNode Pointer to the USB short-form device path node in the FilePath buffer.
553
554 @return The next possible full path pointing to the load option.
555 Caller is responsible to free the memory.
556 **/
557 EFI_DEVICE_PATH_PROTOCOL *
558 BmExpandUsbDevicePath (
559 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
560 IN EFI_DEVICE_PATH_PROTOCOL *FullPath,
561 IN EFI_DEVICE_PATH_PROTOCOL *ShortformNode
562 )
563 {
564 UINTN ParentDevicePathSize;
565 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
566 EFI_DEVICE_PATH_PROTOCOL *NextFullPath;
567 EFI_HANDLE *Handles;
568 UINTN HandleCount;
569 UINTN Index;
570 BOOLEAN GetNext;
571
572 NextFullPath = NULL;
573 GetNext = (BOOLEAN)(FullPath == NULL);
574 ParentDevicePathSize = (UINTN) ShortformNode - (UINTN) FilePath;
575 RemainingDevicePath = NextDevicePathNode (ShortformNode);
576 Handles = BmFindUsbDevice (FilePath, ParentDevicePathSize, &HandleCount);
577
578 for (Index = 0; Index < HandleCount; Index++) {
579 FilePath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), RemainingDevicePath);
580 if (FilePath == NULL) {
581 //
582 // Out of memory.
583 //
584 continue;
585 }
586 NextFullPath = BmGetNextLoadOptionDevicePath (FilePath, NULL);
587 FreePool (FilePath);
588 if (NextFullPath == NULL) {
589 //
590 // No BlockIo or SimpleFileSystem under FilePath.
591 //
592 continue;
593 }
594 if (GetNext) {
595 break;
596 } else {
597 GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);
598 FreePool (NextFullPath);
599 NextFullPath = NULL;
600 }
601 }
602
603 if (Handles != NULL) {
604 FreePool (Handles);
605 }
606
607 return NextFullPath;
608 }
609
610 /**
611 Expand File-path device path node to be full device path in platform.
612
613 @param FilePath The device path pointing to a load option.
614 It could be a short-form device path.
615 @param FullPath The full path returned by the routine in last call.
616 Set to NULL in first call.
617
618 @return The next possible full path pointing to the load option.
619 Caller is responsible to free the memory.
620 **/
621 EFI_DEVICE_PATH_PROTOCOL *
622 BmExpandFileDevicePath (
623 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
624 IN EFI_DEVICE_PATH_PROTOCOL *FullPath
625 )
626 {
627 EFI_STATUS Status;
628 UINTN Index;
629 UINTN HandleCount;
630 EFI_HANDLE *Handles;
631 EFI_BLOCK_IO_PROTOCOL *BlockIo;
632 UINTN MediaType;
633 EFI_DEVICE_PATH_PROTOCOL *NextFullPath;
634 BOOLEAN GetNext;
635
636 EfiBootManagerConnectAll ();
637 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiSimpleFileSystemProtocolGuid, NULL, &HandleCount, &Handles);
638 if (EFI_ERROR (Status)) {
639 HandleCount = 0;
640 Handles = NULL;
641 }
642
643 GetNext = (BOOLEAN)(FullPath == NULL);
644 NextFullPath = NULL;
645 //
646 // Enumerate all removable media devices followed by all fixed media devices,
647 // followed by media devices which don't layer on block io.
648 //
649 for (MediaType = 0; MediaType < 3; MediaType++) {
650 for (Index = 0; Index < HandleCount; Index++) {
651 Status = gBS->HandleProtocol (Handles[Index], &gEfiBlockIoProtocolGuid, (VOID *) &BlockIo);
652 if (EFI_ERROR (Status)) {
653 BlockIo = NULL;
654 }
655 if ((MediaType == 0 && BlockIo != NULL && BlockIo->Media->RemovableMedia) ||
656 (MediaType == 1 && BlockIo != NULL && !BlockIo->Media->RemovableMedia) ||
657 (MediaType == 2 && BlockIo == NULL)
658 ) {
659 NextFullPath = AppendDevicePath (DevicePathFromHandle (Handles[Index]), FilePath);
660 if (GetNext) {
661 break;
662 } else {
663 GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);
664 FreePool (NextFullPath);
665 NextFullPath = NULL;
666 }
667 }
668 }
669 if (NextFullPath != NULL) {
670 break;
671 }
672 }
673
674 if (Handles != NULL) {
675 FreePool (Handles);
676 }
677
678 return NextFullPath;
679 }
680
681 /**
682 Expand URI device path node to be full device path in platform.
683
684 @param FilePath The device path pointing to a load option.
685 It could be a short-form device path.
686 @param FullPath The full path returned by the routine in last call.
687 Set to NULL in first call.
688
689 @return The next possible full path pointing to the load option.
690 Caller is responsible to free the memory.
691 **/
692 EFI_DEVICE_PATH_PROTOCOL *
693 BmExpandUriDevicePath (
694 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
695 IN EFI_DEVICE_PATH_PROTOCOL *FullPath
696 )
697 {
698 EFI_STATUS Status;
699 UINTN Index;
700 UINTN HandleCount;
701 EFI_HANDLE *Handles;
702 EFI_DEVICE_PATH_PROTOCOL *NextFullPath;
703 EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath;
704 BOOLEAN GetNext;
705
706 EfiBootManagerConnectAll ();
707 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiLoadFileProtocolGuid, NULL, &HandleCount, &Handles);
708 if (EFI_ERROR (Status)) {
709 HandleCount = 0;
710 Handles = NULL;
711 }
712
713 NextFullPath = NULL;
714 GetNext = (BOOLEAN)(FullPath == NULL);
715 for (Index = 0; Index < HandleCount; Index++) {
716 NextFullPath = BmExpandLoadFile (Handles[Index], FilePath);
717
718 if (NextFullPath == NULL) {
719 continue;
720 }
721
722 if (GetNext) {
723 break;
724 } else {
725 GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);
726 //
727 // Free the resource occupied by the RAM disk.
728 //
729 RamDiskDevicePath = BmGetRamDiskDevicePath (NextFullPath);
730 if (RamDiskDevicePath != NULL) {
731 BmDestroyRamDisk (RamDiskDevicePath);
732 FreePool (RamDiskDevicePath);
733 }
734 FreePool (NextFullPath);
735 NextFullPath = NULL;
736 }
737 }
738
739 if (Handles != NULL) {
740 FreePool (Handles);
741 }
742
743 return NextFullPath;
744 }
745
746 /**
747 Save the partition DevicePath to the CachedDevicePath as the first instance.
748
749 @param CachedDevicePath The device path cache.
750 @param DevicePath The partition device path to be cached.
751 **/
752 VOID
753 BmCachePartitionDevicePath (
754 IN OUT EFI_DEVICE_PATH_PROTOCOL **CachedDevicePath,
755 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
756 )
757 {
758 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
759 UINTN Count;
760
761 if (BmMatchDevicePaths (*CachedDevicePath, DevicePath)) {
762 TempDevicePath = *CachedDevicePath;
763 *CachedDevicePath = BmDelPartMatchInstance (*CachedDevicePath, DevicePath);
764 FreePool (TempDevicePath);
765 }
766
767 if (*CachedDevicePath == NULL) {
768 *CachedDevicePath = DuplicateDevicePath (DevicePath);
769 return;
770 }
771
772 TempDevicePath = *CachedDevicePath;
773 *CachedDevicePath = AppendDevicePathInstance (DevicePath, *CachedDevicePath);
774 if (TempDevicePath != NULL) {
775 FreePool (TempDevicePath);
776 }
777
778 //
779 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
780 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.
781 //
782 Count = 0;
783 TempDevicePath = *CachedDevicePath;
784 while (!IsDevicePathEnd (TempDevicePath)) {
785 TempDevicePath = NextDevicePathNode (TempDevicePath);
786 //
787 // Parse one instance
788 //
789 while (!IsDevicePathEndType (TempDevicePath)) {
790 TempDevicePath = NextDevicePathNode (TempDevicePath);
791 }
792 Count++;
793 //
794 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
795 //
796 if (Count == 12) {
797 SetDevicePathEndNode (TempDevicePath);
798 break;
799 }
800 }
801 }
802
803 /**
804 Expand a device path that starts with a hard drive media device path node to be a
805 full device path that includes the full hardware path to the device. We need
806 to do this so it can be booted. As an optimization the front match (the part point
807 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
808 so a connect all is not required on every boot. All successful history device path
809 which point to partition node (the front part) will be saved.
810
811 @param FilePath The device path pointing to a load option.
812 It could be a short-form device path.
813
814 @return The full device path pointing to the load option.
815 **/
816 EFI_DEVICE_PATH_PROTOCOL *
817 BmExpandPartitionDevicePath (
818 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
819 )
820 {
821 EFI_STATUS Status;
822 UINTN BlockIoHandleCount;
823 EFI_HANDLE *BlockIoBuffer;
824 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;
825 UINTN Index;
826 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;
827 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
828 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
829 EFI_DEVICE_PATH_PROTOCOL *FullPath;
830 UINTN CachedDevicePathSize;
831 BOOLEAN NeedAdjust;
832 EFI_DEVICE_PATH_PROTOCOL *Instance;
833 UINTN Size;
834
835 //
836 // Check if there is prestore 'HDDP' variable.
837 // If exist, search the front path which point to partition node in the variable instants.
838 // If fail to find or 'HDDP' not exist, reconnect all and search in all system
839 //
840 GetVariable2 (L"HDDP", &mBmHardDriveBootVariableGuid, (VOID **) &CachedDevicePath, &CachedDevicePathSize);
841
842 //
843 // Delete the invalid 'HDDP' variable.
844 //
845 if ((CachedDevicePath != NULL) && !IsDevicePathValid (CachedDevicePath, CachedDevicePathSize)) {
846 FreePool (CachedDevicePath);
847 CachedDevicePath = NULL;
848 Status = gRT->SetVariable (
849 L"HDDP",
850 &mBmHardDriveBootVariableGuid,
851 0,
852 0,
853 NULL
854 );
855 ASSERT_EFI_ERROR (Status);
856 }
857
858 FullPath = NULL;
859 if (CachedDevicePath != NULL) {
860 TempNewDevicePath = CachedDevicePath;
861 NeedAdjust = FALSE;
862 do {
863 //
864 // Check every instance of the variable
865 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
866 // partial partition boot option. Second, check whether the instance could be connected.
867 //
868 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);
869 if (BmMatchPartitionDevicePathNode (Instance, (HARDDRIVE_DEVICE_PATH *) FilePath)) {
870 //
871 // Connect the device path instance, the device path point to hard drive media device path node
872 // e.g. ACPI() /PCI()/ATA()/Partition()
873 //
874 Status = EfiBootManagerConnectDevicePath (Instance, NULL);
875 if (!EFI_ERROR (Status)) {
876 TempDevicePath = AppendDevicePath (Instance, NextDevicePathNode (FilePath));
877 //
878 // TempDevicePath = ACPI()/PCI()/ATA()/Partition()
879 // or = ACPI()/PCI()/ATA()/Partition()/.../A.EFI
880 //
881 // When TempDevicePath = ACPI()/PCI()/ATA()/Partition(),
882 // it may expand to two potienal full paths (nested partition, rarely happen):
883 // 1. ACPI()/PCI()/ATA()/Partition()/Partition(A1)/EFI/BootX64.EFI
884 // 2. ACPI()/PCI()/ATA()/Partition()/Partition(A2)/EFI/BootX64.EFI
885 // For simplicity, only #1 is returned.
886 //
887 FullPath = BmGetNextLoadOptionDevicePath (TempDevicePath, NULL);
888 FreePool (TempDevicePath);
889
890 if (FullPath != NULL) {
891 //
892 // Adjust the 'HDDP' instances sequence if the matched one is not first one.
893 //
894 if (NeedAdjust) {
895 BmCachePartitionDevicePath (&CachedDevicePath, Instance);
896 //
897 // Save the matching Device Path so we don't need to do a connect all next time
898 // Failing to save only impacts performance next time expanding the short-form device path
899 //
900 Status = gRT->SetVariable (
901 L"HDDP",
902 &mBmHardDriveBootVariableGuid,
903 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
904 GetDevicePathSize (CachedDevicePath),
905 CachedDevicePath
906 );
907 }
908
909 FreePool (Instance);
910 FreePool (CachedDevicePath);
911 return FullPath;
912 }
913 }
914 }
915 //
916 // Come here means the first instance is not matched
917 //
918 NeedAdjust = TRUE;
919 FreePool(Instance);
920 } while (TempNewDevicePath != NULL);
921 }
922
923 //
924 // If we get here we fail to find or 'HDDP' not exist, and now we need
925 // to search all devices in the system for a matched partition
926 //
927 EfiBootManagerConnectAll ();
928 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);
929 if (EFI_ERROR (Status)) {
930 BlockIoHandleCount = 0;
931 BlockIoBuffer = NULL;
932 }
933 //
934 // Loop through all the device handles that support the BLOCK_IO Protocol
935 //
936 for (Index = 0; Index < BlockIoHandleCount; Index++) {
937 BlockIoDevicePath = DevicePathFromHandle (BlockIoBuffer[Index]);
938 if (BlockIoDevicePath == NULL) {
939 continue;
940 }
941
942 if (BmMatchPartitionDevicePathNode (BlockIoDevicePath, (HARDDRIVE_DEVICE_PATH *) FilePath)) {
943 //
944 // Find the matched partition device path
945 //
946 TempDevicePath = AppendDevicePath (BlockIoDevicePath, NextDevicePathNode (FilePath));
947 FullPath = BmGetNextLoadOptionDevicePath (TempDevicePath, NULL);
948 FreePool (TempDevicePath);
949
950 if (FullPath != NULL) {
951 BmCachePartitionDevicePath (&CachedDevicePath, BlockIoDevicePath);
952
953 //
954 // Save the matching Device Path so we don't need to do a connect all next time
955 // Failing to save only impacts performance next time expanding the short-form device path
956 //
957 Status = gRT->SetVariable (
958 L"HDDP",
959 &mBmHardDriveBootVariableGuid,
960 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
961 GetDevicePathSize (CachedDevicePath),
962 CachedDevicePath
963 );
964
965 break;
966 }
967 }
968 }
969
970 if (CachedDevicePath != NULL) {
971 FreePool (CachedDevicePath);
972 }
973 if (BlockIoBuffer != NULL) {
974 FreePool (BlockIoBuffer);
975 }
976 return FullPath;
977 }
978
979 /**
980 Expand the media device path which points to a BlockIo or SimpleFileSystem instance
981 by appending EFI_REMOVABLE_MEDIA_FILE_NAME.
982
983 @param DevicePath The media device path pointing to a BlockIo or SimpleFileSystem instance.
984 @param FullPath The full path returned by the routine in last call.
985 Set to NULL in first call.
986
987 @return The next possible full path pointing to the load option.
988 Caller is responsible to free the memory.
989 **/
990 EFI_DEVICE_PATH_PROTOCOL *
991 BmExpandMediaDevicePath (
992 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
993 IN EFI_DEVICE_PATH_PROTOCOL *FullPath
994 )
995 {
996 EFI_STATUS Status;
997 EFI_HANDLE Handle;
998 EFI_BLOCK_IO_PROTOCOL *BlockIo;
999 VOID *Buffer;
1000 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1001 EFI_DEVICE_PATH_PROTOCOL *NextFullPath;
1002 UINTN Size;
1003 UINTN TempSize;
1004 EFI_HANDLE *SimpleFileSystemHandles;
1005 UINTN NumberSimpleFileSystemHandles;
1006 UINTN Index;
1007 BOOLEAN GetNext;
1008
1009 GetNext = (BOOLEAN)(FullPath == NULL);
1010 //
1011 // Check whether the device is connected
1012 //
1013 TempDevicePath = DevicePath;
1014 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);
1015 if (!EFI_ERROR (Status)) {
1016 ASSERT (IsDevicePathEnd (TempDevicePath));
1017
1018 NextFullPath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
1019 //
1020 // For device path pointing to simple file system, it only expands to one full path.
1021 //
1022 if (GetNext) {
1023 return NextFullPath;
1024 } else {
1025 FreePool (NextFullPath);
1026 return NULL;
1027 }
1028 }
1029
1030 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
1031 ASSERT_EFI_ERROR (Status);
1032
1033 //
1034 // For device boot option only pointing to the removable device handle,
1035 // should make sure all its children handles (its child partion or media handles)
1036 // are created and connected.
1037 //
1038 gBS->ConnectController (Handle, NULL, NULL, TRUE);
1039
1040 //
1041 // Issue a dummy read to the device to check for media change.
1042 // When the removable media is changed, any Block IO read/write will
1043 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
1044 // returned. After the Block IO protocol is reinstalled, subsequent
1045 // Block IO read/write will success.
1046 //
1047 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **) &BlockIo);
1048 ASSERT_EFI_ERROR (Status);
1049 Buffer = AllocatePool (BlockIo->Media->BlockSize);
1050 if (Buffer != NULL) {
1051 BlockIo->ReadBlocks (
1052 BlockIo,
1053 BlockIo->Media->MediaId,
1054 0,
1055 BlockIo->Media->BlockSize,
1056 Buffer
1057 );
1058 FreePool (Buffer);
1059 }
1060
1061 //
1062 // Detect the the default boot file from removable Media
1063 //
1064 NextFullPath = NULL;
1065 Size = GetDevicePathSize (DevicePath) - END_DEVICE_PATH_LENGTH;
1066 gBS->LocateHandleBuffer (
1067 ByProtocol,
1068 &gEfiSimpleFileSystemProtocolGuid,
1069 NULL,
1070 &NumberSimpleFileSystemHandles,
1071 &SimpleFileSystemHandles
1072 );
1073 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
1074 //
1075 // Get the device path size of SimpleFileSystem handle
1076 //
1077 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
1078 TempSize = GetDevicePathSize (TempDevicePath) - END_DEVICE_PATH_LENGTH;
1079 //
1080 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
1081 //
1082 if ((Size <= TempSize) && (CompareMem (TempDevicePath, DevicePath, Size) == 0)) {
1083 NextFullPath = FileDevicePath (SimpleFileSystemHandles[Index], EFI_REMOVABLE_MEDIA_FILE_NAME);
1084 if (GetNext) {
1085 break;
1086 } else {
1087 GetNext = (BOOLEAN)(CompareMem (NextFullPath, FullPath, GetDevicePathSize (NextFullPath)) == 0);
1088 FreePool (NextFullPath);
1089 NextFullPath = NULL;
1090 }
1091 }
1092 }
1093
1094 if (SimpleFileSystemHandles != NULL) {
1095 FreePool (SimpleFileSystemHandles);
1096 }
1097
1098 return NextFullPath;
1099 }
1100
1101 /**
1102 Check whether Left and Right are the same without matching the specific
1103 device path data in IP device path and URI device path node.
1104
1105 @retval TRUE Left and Right are the same.
1106 @retval FALSE Left and Right are the different.
1107 **/
1108 BOOLEAN
1109 BmMatchHttpBootDevicePath (
1110 IN EFI_DEVICE_PATH_PROTOCOL *Left,
1111 IN EFI_DEVICE_PATH_PROTOCOL *Right
1112 )
1113 {
1114 for (; !IsDevicePathEnd (Left) && !IsDevicePathEnd (Right)
1115 ; Left = NextDevicePathNode (Left), Right = NextDevicePathNode (Right)
1116 ) {
1117 if (CompareMem (Left, Right, DevicePathNodeLength (Left)) != 0) {
1118 if ((DevicePathType (Left) != MESSAGING_DEVICE_PATH) || (DevicePathType (Right) != MESSAGING_DEVICE_PATH)) {
1119 return FALSE;
1120 }
1121
1122 if (DevicePathSubType (Left) == MSG_DNS_DP) {
1123 Left = NextDevicePathNode (Left);
1124 }
1125
1126 if (DevicePathSubType (Right) == MSG_DNS_DP) {
1127 Right = NextDevicePathNode (Right);
1128 }
1129
1130 if (((DevicePathSubType (Left) != MSG_IPv4_DP) || (DevicePathSubType (Right) != MSG_IPv4_DP)) &&
1131 ((DevicePathSubType (Left) != MSG_IPv6_DP) || (DevicePathSubType (Right) != MSG_IPv6_DP)) &&
1132 ((DevicePathSubType (Left) != MSG_URI_DP) || (DevicePathSubType (Right) != MSG_URI_DP))
1133 ) {
1134 return FALSE;
1135 }
1136 }
1137 }
1138 return (BOOLEAN) (IsDevicePathEnd (Left) && IsDevicePathEnd (Right));
1139 }
1140
1141 /**
1142 Get the file buffer from the file system produced by Load File instance.
1143
1144 @param LoadFileHandle The handle of LoadFile instance.
1145 @param RamDiskHandle Return the RAM Disk handle.
1146
1147 @return The next possible full path pointing to the load option.
1148 Caller is responsible to free the memory.
1149 **/
1150 EFI_DEVICE_PATH_PROTOCOL *
1151 BmExpandNetworkFileSystem (
1152 IN EFI_HANDLE LoadFileHandle,
1153 OUT EFI_HANDLE *RamDiskHandle
1154 )
1155 {
1156 EFI_STATUS Status;
1157 EFI_HANDLE Handle;
1158 EFI_HANDLE *Handles;
1159 UINTN HandleCount;
1160 UINTN Index;
1161 EFI_DEVICE_PATH_PROTOCOL *Node;
1162
1163 Status = gBS->LocateHandleBuffer (
1164 ByProtocol,
1165 &gEfiBlockIoProtocolGuid,
1166 NULL,
1167 &HandleCount,
1168 &Handles
1169 );
1170 if (EFI_ERROR (Status)) {
1171 Handles = NULL;
1172 HandleCount = 0;
1173 }
1174
1175 Handle = NULL;
1176 for (Index = 0; Index < HandleCount; Index++) {
1177 Node = DevicePathFromHandle (Handles[Index]);
1178 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);
1179 if (!EFI_ERROR (Status) &&
1180 (Handle == LoadFileHandle) &&
1181 (DevicePathType (Node) == MEDIA_DEVICE_PATH) && (DevicePathSubType (Node) == MEDIA_RAM_DISK_DP)) {
1182 //
1183 // Find the BlockIo instance populated from the LoadFile.
1184 //
1185 Handle = Handles[Index];
1186 break;
1187 }
1188 }
1189
1190 if (Handles != NULL) {
1191 FreePool (Handles);
1192 }
1193
1194 if (Index == HandleCount) {
1195 Handle = NULL;
1196 }
1197
1198 *RamDiskHandle = Handle;
1199
1200 if (Handle != NULL) {
1201 //
1202 // Re-use BmExpandMediaDevicePath() to get the full device path of load option.
1203 // But assume only one SimpleFileSystem can be found under the BlockIo.
1204 //
1205 return BmExpandMediaDevicePath (DevicePathFromHandle (Handle), NULL);
1206 } else {
1207 return NULL;
1208 }
1209 }
1210
1211 /**
1212 Return the RAM Disk device path created by LoadFile.
1213
1214 @param FilePath The source file path.
1215
1216 @return Callee-to-free RAM Disk device path
1217 **/
1218 EFI_DEVICE_PATH_PROTOCOL *
1219 BmGetRamDiskDevicePath (
1220 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
1221 )
1222 {
1223 EFI_STATUS Status;
1224 EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath;
1225 EFI_DEVICE_PATH_PROTOCOL *Node;
1226 EFI_HANDLE Handle;
1227
1228 Node = FilePath;
1229 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);
1230 if (!EFI_ERROR (Status) &&
1231 (DevicePathType (Node) == MEDIA_DEVICE_PATH) &&
1232 (DevicePathSubType (Node) == MEDIA_RAM_DISK_DP)
1233 ) {
1234
1235 //
1236 // Construct the device path pointing to RAM Disk
1237 //
1238 Node = NextDevicePathNode (Node);
1239 RamDiskDevicePath = DuplicateDevicePath (FilePath);
1240 ASSERT (RamDiskDevicePath != NULL);
1241 SetDevicePathEndNode ((VOID *) ((UINTN) RamDiskDevicePath + ((UINTN) Node - (UINTN) FilePath)));
1242 return RamDiskDevicePath;
1243 }
1244
1245 return NULL;
1246 }
1247
1248 /**
1249 Return the buffer and buffer size occupied by the RAM Disk.
1250
1251 @param RamDiskDevicePath RAM Disk device path.
1252 @param RamDiskSizeInPages Return RAM Disk size in pages.
1253
1254 @retval RAM Disk buffer.
1255 **/
1256 VOID *
1257 BmGetRamDiskMemoryInfo (
1258 IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath,
1259 OUT UINTN *RamDiskSizeInPages
1260 )
1261 {
1262
1263 EFI_STATUS Status;
1264 EFI_HANDLE Handle;
1265 UINT64 StartingAddr;
1266 UINT64 EndingAddr;
1267
1268 ASSERT (RamDiskDevicePath != NULL);
1269
1270 *RamDiskSizeInPages = 0;
1271
1272 //
1273 // Get the buffer occupied by RAM Disk.
1274 //
1275 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &RamDiskDevicePath, &Handle);
1276 ASSERT_EFI_ERROR (Status);
1277 ASSERT ((DevicePathType (RamDiskDevicePath) == MEDIA_DEVICE_PATH) &&
1278 (DevicePathSubType (RamDiskDevicePath) == MEDIA_RAM_DISK_DP));
1279 StartingAddr = ReadUnaligned64 ((UINT64 *) ((MEDIA_RAM_DISK_DEVICE_PATH *) RamDiskDevicePath)->StartingAddr);
1280 EndingAddr = ReadUnaligned64 ((UINT64 *) ((MEDIA_RAM_DISK_DEVICE_PATH *) RamDiskDevicePath)->EndingAddr);
1281 *RamDiskSizeInPages = EFI_SIZE_TO_PAGES ((UINTN) (EndingAddr - StartingAddr + 1));
1282 return (VOID *) (UINTN) StartingAddr;
1283 }
1284
1285 /**
1286 Destroy the RAM Disk.
1287
1288 The destroy operation includes to call RamDisk.Unregister to
1289 unregister the RAM DISK from RAM DISK driver, free the memory
1290 allocated for the RAM Disk.
1291
1292 @param RamDiskDevicePath RAM Disk device path.
1293 **/
1294 VOID
1295 BmDestroyRamDisk (
1296 IN EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath
1297 )
1298 {
1299 EFI_STATUS Status;
1300 VOID *RamDiskBuffer;
1301 UINTN RamDiskSizeInPages;
1302
1303 ASSERT (RamDiskDevicePath != NULL);
1304
1305 RamDiskBuffer = BmGetRamDiskMemoryInfo (RamDiskDevicePath, &RamDiskSizeInPages);
1306
1307 //
1308 // Destroy RAM Disk.
1309 //
1310 if (mRamDisk == NULL) {
1311 Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID *) &mRamDisk);
1312 ASSERT_EFI_ERROR (Status);
1313 }
1314 Status = mRamDisk->Unregister (RamDiskDevicePath);
1315 ASSERT_EFI_ERROR (Status);
1316 FreePages (RamDiskBuffer, RamDiskSizeInPages);
1317 }
1318
1319 /**
1320 Get the file buffer from the specified Load File instance.
1321
1322 @param LoadFileHandle The specified Load File instance.
1323 @param FilePath The file path which will pass to LoadFile().
1324
1325 @return The full device path pointing to the load option buffer.
1326 **/
1327 EFI_DEVICE_PATH_PROTOCOL *
1328 BmExpandLoadFile (
1329 IN EFI_HANDLE LoadFileHandle,
1330 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
1331 )
1332 {
1333 EFI_STATUS Status;
1334 EFI_LOAD_FILE_PROTOCOL *LoadFile;
1335 VOID *FileBuffer;
1336 EFI_HANDLE RamDiskHandle;
1337 UINTN BufferSize;
1338 EFI_DEVICE_PATH_PROTOCOL *FullPath;
1339
1340 Status = gBS->OpenProtocol (
1341 LoadFileHandle,
1342 &gEfiLoadFileProtocolGuid,
1343 (VOID **) &LoadFile,
1344 gImageHandle,
1345 NULL,
1346 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1347 );
1348 ASSERT_EFI_ERROR (Status);
1349
1350 FileBuffer = NULL;
1351 BufferSize = 0;
1352 Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer);
1353 if ((Status != EFI_WARN_FILE_SYSTEM) && (Status != EFI_BUFFER_TOO_SMALL)) {
1354 return NULL;
1355 }
1356
1357 if (Status == EFI_BUFFER_TOO_SMALL) {
1358 //
1359 // The load option buffer is directly returned by LoadFile.
1360 //
1361 return DuplicateDevicePath (DevicePathFromHandle (LoadFileHandle));
1362 }
1363
1364 //
1365 // The load option resides in a RAM disk.
1366 //
1367 FileBuffer = AllocateReservedPages (EFI_SIZE_TO_PAGES (BufferSize));
1368 if (FileBuffer == NULL) {
1369 return NULL;
1370 }
1371
1372 Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer);
1373 if (EFI_ERROR (Status)) {
1374 FreePages (FileBuffer, EFI_SIZE_TO_PAGES (BufferSize));
1375 return NULL;
1376 }
1377
1378 FullPath = BmExpandNetworkFileSystem (LoadFileHandle, &RamDiskHandle);
1379 if (FullPath == NULL) {
1380 //
1381 // Free the memory occupied by the RAM disk if there is no BlockIo or SimpleFileSystem instance.
1382 //
1383 BmDestroyRamDisk (DevicePathFromHandle (RamDiskHandle));
1384 }
1385
1386 return FullPath;
1387 }
1388
1389 /**
1390 Return the full device path pointing to the load option.
1391
1392 FilePath may:
1393 1. Exactly matches to a LoadFile instance.
1394 2. Cannot match to any LoadFile instance. Wide match is required.
1395 In either case, the routine may return:
1396 1. A copy of FilePath when FilePath matches to a LoadFile instance and
1397 the LoadFile returns a load option buffer.
1398 2. A new device path with IP and URI information updated when wide match
1399 happens.
1400 3. A new device path pointing to a load option in RAM disk.
1401 In either case, only one full device path is returned for a specified
1402 FilePath.
1403
1404 @param FilePath The media device path pointing to a LoadFile instance.
1405
1406 @return The load option buffer.
1407 **/
1408 EFI_DEVICE_PATH_PROTOCOL *
1409 BmExpandLoadFiles (
1410 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
1411 )
1412 {
1413 EFI_STATUS Status;
1414 EFI_HANDLE Handle;
1415 EFI_HANDLE *Handles;
1416 UINTN HandleCount;
1417 UINTN Index;
1418 EFI_DEVICE_PATH_PROTOCOL *Node;
1419
1420 //
1421 // Get file buffer from load file instance.
1422 //
1423 Node = FilePath;
1424 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);
1425 if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) {
1426 //
1427 // When wide match happens, pass full device path to LoadFile (),
1428 // otherwise, pass remaining device path to LoadFile ().
1429 //
1430 FilePath = Node;
1431 } else {
1432 Handle = NULL;
1433 //
1434 // Use wide match algorithm to find one when
1435 // cannot find a LoadFile instance to exactly match the FilePath
1436 //
1437 Status = gBS->LocateHandleBuffer (
1438 ByProtocol,
1439 &gEfiLoadFileProtocolGuid,
1440 NULL,
1441 &HandleCount,
1442 &Handles
1443 );
1444 if (EFI_ERROR (Status)) {
1445 Handles = NULL;
1446 HandleCount = 0;
1447 }
1448 for (Index = 0; Index < HandleCount; Index++) {
1449 if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles[Index]), FilePath)) {
1450 Handle = Handles[Index];
1451 break;
1452 }
1453 }
1454 if (Handles != NULL) {
1455 FreePool (Handles);
1456 }
1457 }
1458
1459 if (Handle == NULL) {
1460 return NULL;
1461 }
1462
1463 return BmExpandLoadFile (Handle, FilePath);
1464 }
1465
1466 /**
1467 Get the load option by its device path.
1468
1469 @param FilePath The device path pointing to a load option.
1470 It could be a short-form device path.
1471 @param FullPath Return the full device path of the load option after
1472 short-form device path expanding.
1473 Caller is responsible to free it.
1474 @param FileSize Return the load option size.
1475
1476 @return The load option buffer. Caller is responsible to free the memory.
1477 **/
1478 VOID *
1479 EFIAPI
1480 EfiBootManagerGetLoadOptionBuffer (
1481 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1482 OUT EFI_DEVICE_PATH_PROTOCOL **FullPath,
1483 OUT UINTN *FileSize
1484 )
1485 {
1486 *FullPath = NULL;
1487
1488 EfiBootManagerConnectDevicePath (FilePath, NULL);
1489 return BmGetNextLoadOptionBuffer (LoadOptionTypeMax, FilePath, FullPath, FileSize);
1490 }
1491
1492 /**
1493 Get the next possible full path pointing to the load option.
1494 The routine doesn't guarantee the returned full path points to an existing
1495 file, and it also doesn't guarantee the existing file is a valid load option.
1496 BmGetNextLoadOptionBuffer() guarantees.
1497
1498 @param FilePath The device path pointing to a load option.
1499 It could be a short-form device path.
1500 @param FullPath The full path returned by the routine in last call.
1501 Set to NULL in first call.
1502
1503 @return The next possible full path pointing to the load option.
1504 Caller is responsible to free the memory.
1505 **/
1506 EFI_DEVICE_PATH_PROTOCOL *
1507 BmGetNextLoadOptionDevicePath (
1508 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1509 IN EFI_DEVICE_PATH_PROTOCOL *FullPath
1510 )
1511 {
1512 EFI_HANDLE Handle;
1513 EFI_DEVICE_PATH_PROTOCOL *Node;
1514 EFI_STATUS Status;
1515
1516 ASSERT (FilePath != NULL);
1517
1518 //
1519 // Boot from media device by adding a default file name \EFI\BOOT\BOOT{machine type short-name}.EFI
1520 //
1521 Node = FilePath;
1522 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle);
1523 if (EFI_ERROR (Status)) {
1524 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &Node, &Handle);
1525 }
1526
1527 if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) {
1528 return BmExpandMediaDevicePath (FilePath, FullPath);
1529 }
1530
1531 //
1532 // Expand the short-form device path to full device path
1533 //
1534 if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) &&
1535 (DevicePathSubType (FilePath) == MEDIA_HARDDRIVE_DP)) {
1536 //
1537 // Expand the Harddrive device path
1538 //
1539 if (FullPath == NULL) {
1540 return BmExpandPartitionDevicePath (FilePath);
1541 } else {
1542 return NULL;
1543 }
1544 } else if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) &&
1545 (DevicePathSubType (FilePath) == MEDIA_FILEPATH_DP)) {
1546 //
1547 // Expand the File-path device path
1548 //
1549 return BmExpandFileDevicePath (FilePath, FullPath);
1550 } else if ((DevicePathType (FilePath) == MESSAGING_DEVICE_PATH) &&
1551 (DevicePathSubType (FilePath) == MSG_URI_DP)) {
1552 //
1553 // Expand the URI device path
1554 //
1555 return BmExpandUriDevicePath (FilePath, FullPath);
1556 } else {
1557 Node = FilePath;
1558 Status = gBS->LocateDevicePath (&gEfiUsbIoProtocolGuid, &Node, &Handle);
1559 if (EFI_ERROR (Status)) {
1560 //
1561 // Only expand the USB WWID/Class device path
1562 // when FilePath doesn't point to a physical UsbIo controller.
1563 // Otherwise, infinite recursion will happen.
1564 //
1565 for (Node = FilePath; !IsDevicePathEnd (Node); Node = NextDevicePathNode (Node)) {
1566 if ((DevicePathType (Node) == MESSAGING_DEVICE_PATH) &&
1567 ((DevicePathSubType (Node) == MSG_USB_CLASS_DP) || (DevicePathSubType (Node) == MSG_USB_WWID_DP))) {
1568 break;
1569 }
1570 }
1571
1572 //
1573 // Expand the USB WWID/Class device path
1574 //
1575 if (!IsDevicePathEnd (Node)) {
1576 if (FilePath == Node) {
1577 //
1578 // Boot Option device path starts with USB Class or USB WWID device path.
1579 // For Boot Option device path which doesn't begin with the USB Class or
1580 // USB WWID device path, it's not needed to connect again here.
1581 //
1582 BmConnectUsbShortFormDevicePath (FilePath);
1583 }
1584 return BmExpandUsbDevicePath (FilePath, FullPath, Node);
1585 }
1586 }
1587 }
1588
1589 //
1590 // For the below cases, FilePath only expands to one Full path.
1591 // So just handle the case when FullPath == NULL.
1592 //
1593 if (FullPath != NULL) {
1594 return NULL;
1595 }
1596
1597 //
1598 // Load option resides in FV.
1599 //
1600 if (BmIsFvFilePath (FilePath)) {
1601 return BmAdjustFvFilePath (FilePath);
1602 }
1603
1604 //
1605 // Load option resides in Simple File System.
1606 //
1607 Node = FilePath;
1608 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle);
1609 if (!EFI_ERROR (Status)) {
1610 return DuplicateDevicePath (FilePath);
1611 }
1612
1613 //
1614 // Last chance to try: Load option may be loaded through LoadFile.
1615 //
1616 return BmExpandLoadFiles (FilePath);
1617 }
1618
1619 /**
1620 Check if it's a Device Path pointing to BootManagerMenu.
1621
1622 @param DevicePath Input device path.
1623
1624 @retval TRUE The device path is BootManagerMenu File Device Path.
1625 @retval FALSE The device path is NOT BootManagerMenu File Device Path.
1626 **/
1627 BOOLEAN
1628 BmIsBootManagerMenuFilePath (
1629 EFI_DEVICE_PATH_PROTOCOL *DevicePath
1630 )
1631 {
1632 EFI_HANDLE FvHandle;
1633 VOID *NameGuid;
1634 EFI_STATUS Status;
1635
1636 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePath, &FvHandle);
1637 if (!EFI_ERROR (Status)) {
1638 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePath);
1639 if (NameGuid != NULL) {
1640 return CompareGuid (NameGuid, PcdGetPtr (PcdBootManagerMenuFile));
1641 }
1642 }
1643
1644 return FALSE;
1645 }
1646
1647 /**
1648 Attempt to boot the EFI boot option. This routine sets L"BootCurent" and
1649 also signals the EFI ready to boot event. If the device path for the option
1650 starts with a BBS device path a legacy boot is attempted via the registered
1651 gLegacyBoot function. Short form device paths are also supported via this
1652 rountine. A device path starting with MEDIA_HARDDRIVE_DP, MSG_USB_WWID_DP,
1653 MSG_USB_CLASS_DP gets expaned out to find the first device that matches.
1654 If the BootOption Device Path fails the removable media boot algorithm
1655 is attempted (\EFI\BOOTIA32.EFI, \EFI\BOOTX64.EFI,... only one file type
1656 is tried per processor type)
1657
1658 @param BootOption Boot Option to try and boot.
1659 On return, BootOption->Status contains the boot status.
1660 EFI_SUCCESS BootOption was booted
1661 EFI_UNSUPPORTED A BBS device path was found with no valid callback
1662 registered via EfiBootManagerInitialize().
1663 EFI_NOT_FOUND The BootOption was not found on the system
1664 !EFI_SUCCESS BootOption failed with this error status
1665
1666 **/
1667 VOID
1668 EFIAPI
1669 EfiBootManagerBoot (
1670 IN EFI_BOOT_MANAGER_LOAD_OPTION *BootOption
1671 )
1672 {
1673 EFI_STATUS Status;
1674 EFI_HANDLE ImageHandle;
1675 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
1676 UINT16 Uint16;
1677 UINTN OptionNumber;
1678 UINTN OriginalOptionNumber;
1679 EFI_DEVICE_PATH_PROTOCOL *FilePath;
1680 EFI_DEVICE_PATH_PROTOCOL *RamDiskDevicePath;
1681 VOID *FileBuffer;
1682 UINTN FileSize;
1683 EFI_BOOT_LOGO_PROTOCOL *BootLogo;
1684 EFI_EVENT LegacyBootEvent;
1685
1686 if (BootOption == NULL) {
1687 return;
1688 }
1689
1690 if (BootOption->FilePath == NULL || BootOption->OptionType != LoadOptionTypeBoot) {
1691 BootOption->Status = EFI_INVALID_PARAMETER;
1692 return;
1693 }
1694
1695 //
1696 // 1. Create Boot#### for a temporary boot if there is no match Boot#### (i.e. a boot by selected a EFI Shell using "Boot From File")
1697 //
1698 OptionNumber = BmFindBootOptionInVariable (BootOption);
1699 if (OptionNumber == LoadOptionNumberUnassigned) {
1700 Status = BmGetFreeOptionNumber (LoadOptionTypeBoot, &Uint16);
1701 if (!EFI_ERROR (Status)) {
1702 //
1703 // Save the BootOption->OptionNumber to restore later
1704 //
1705 OptionNumber = Uint16;
1706 OriginalOptionNumber = BootOption->OptionNumber;
1707 BootOption->OptionNumber = OptionNumber;
1708 Status = EfiBootManagerLoadOptionToVariable (BootOption);
1709 BootOption->OptionNumber = OriginalOptionNumber;
1710 }
1711
1712 if (EFI_ERROR (Status)) {
1713 DEBUG ((EFI_D_ERROR, "[Bds] Failed to create Boot#### for a temporary boot - %r!\n", Status));
1714 BootOption->Status = Status;
1715 return ;
1716 }
1717 }
1718
1719 //
1720 // 2. Set BootCurrent
1721 //
1722 Uint16 = (UINT16) OptionNumber;
1723 BmSetVariableAndReportStatusCodeOnError (
1724 L"BootCurrent",
1725 &gEfiGlobalVariableGuid,
1726 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
1727 sizeof (UINT16),
1728 &Uint16
1729 );
1730
1731 //
1732 // 3. Signal the EVT_SIGNAL_READY_TO_BOOT event when we are about to load and execute
1733 // the boot option.
1734 //
1735 if (BmIsBootManagerMenuFilePath (BootOption->FilePath)) {
1736 DEBUG ((EFI_D_INFO, "[Bds] Booting Boot Manager Menu.\n"));
1737 BmStopHotkeyService (NULL, NULL);
1738 } else {
1739 EfiSignalEventReadyToBoot();
1740 //
1741 // Report Status Code to indicate ReadyToBoot was signalled
1742 //
1743 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));
1744 //
1745 // 4. Repair system through DriverHealth protocol
1746 //
1747 BmRepairAllControllers ();
1748 }
1749
1750 PERF_START_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);
1751
1752 //
1753 // 5. Adjust the different type memory page number just before booting
1754 // and save the updated info into the variable for next boot to use
1755 //
1756 BmSetMemoryTypeInformationVariable (
1757 (BOOLEAN) ((BootOption->Attributes & LOAD_OPTION_CATEGORY) == LOAD_OPTION_CATEGORY_BOOT)
1758 );
1759
1760 //
1761 // 6. Load EFI boot option to ImageHandle
1762 //
1763 DEBUG_CODE_BEGIN ();
1764 if (BootOption->Description == NULL) {
1765 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting from unknown device path\n"));
1766 } else {
1767 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting %s\n", BootOption->Description));
1768 }
1769 DEBUG_CODE_END ();
1770
1771 ImageHandle = NULL;
1772 RamDiskDevicePath = NULL;
1773 if (DevicePathType (BootOption->FilePath) != BBS_DEVICE_PATH) {
1774 Status = EFI_NOT_FOUND;
1775 FilePath = NULL;
1776 EfiBootManagerConnectDevicePath (BootOption->FilePath, NULL);
1777 FileBuffer = BmGetNextLoadOptionBuffer (LoadOptionTypeBoot, BootOption->FilePath, &FilePath, &FileSize);
1778 if (FileBuffer != NULL) {
1779 RamDiskDevicePath = BmGetRamDiskDevicePath (FilePath);
1780
1781 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
1782 Status = gBS->LoadImage (
1783 TRUE,
1784 gImageHandle,
1785 FilePath,
1786 FileBuffer,
1787 FileSize,
1788 &ImageHandle
1789 );
1790 }
1791 if (FileBuffer != NULL) {
1792 FreePool (FileBuffer);
1793 }
1794 if (FilePath != NULL) {
1795 FreePool (FilePath);
1796 }
1797
1798 if (EFI_ERROR (Status)) {
1799 //
1800 // Report Status Code to indicate that the failure to load boot option
1801 //
1802 REPORT_STATUS_CODE (
1803 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1804 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)
1805 );
1806 BootOption->Status = Status;
1807 //
1808 // Destroy the RAM disk
1809 //
1810 if (RamDiskDevicePath != NULL) {
1811 BmDestroyRamDisk (RamDiskDevicePath);
1812 FreePool (RamDiskDevicePath);
1813 }
1814 return;
1815 }
1816 }
1817
1818 //
1819 // Check to see if we should legacy BOOT. If yes then do the legacy boot
1820 // Write boot to OS performance data for Legacy boot
1821 //
1822 if ((DevicePathType (BootOption->FilePath) == BBS_DEVICE_PATH) && (DevicePathSubType (BootOption->FilePath) == BBS_BBS_DP)) {
1823 if (mBmLegacyBoot != NULL) {
1824 //
1825 // Write boot to OS performance data for legacy boot.
1826 //
1827 PERF_CODE (
1828 //
1829 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1830 //
1831 Status = EfiCreateEventLegacyBootEx(
1832 TPL_NOTIFY,
1833 BmWriteBootToOsPerformanceData,
1834 NULL,
1835 &LegacyBootEvent
1836 );
1837 ASSERT_EFI_ERROR (Status);
1838 );
1839
1840 mBmLegacyBoot (BootOption);
1841 } else {
1842 BootOption->Status = EFI_UNSUPPORTED;
1843 }
1844
1845 PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);
1846 return;
1847 }
1848
1849 //
1850 // Provide the image with its load options
1851 //
1852 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
1853 ASSERT_EFI_ERROR (Status);
1854
1855 if (!BmIsAutoCreateBootOption (BootOption)) {
1856 ImageInfo->LoadOptionsSize = BootOption->OptionalDataSize;
1857 ImageInfo->LoadOptions = BootOption->OptionalData;
1858 }
1859
1860 //
1861 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
1862 //
1863 ImageInfo->ParentHandle = NULL;
1864
1865 //
1866 // Before calling the image, enable the Watchdog Timer for 5 minutes period
1867 //
1868 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
1869
1870 //
1871 // Write boot to OS performance data for UEFI boot
1872 //
1873 PERF_CODE (
1874 BmWriteBootToOsPerformanceData (NULL, NULL);
1875 );
1876
1877 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));
1878
1879 Status = gBS->StartImage (ImageHandle, &BootOption->ExitDataSize, &BootOption->ExitData);
1880 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));
1881 BootOption->Status = Status;
1882 if (EFI_ERROR (Status)) {
1883 //
1884 // Report Status Code to indicate that boot failure
1885 //
1886 REPORT_STATUS_CODE (
1887 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1888 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)
1889 );
1890 }
1891 PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);
1892
1893 //
1894 // Destroy the RAM disk
1895 //
1896 if (RamDiskDevicePath != NULL) {
1897 BmDestroyRamDisk (RamDiskDevicePath);
1898 FreePool (RamDiskDevicePath);
1899 }
1900
1901 //
1902 // Clear the Watchdog Timer after the image returns
1903 //
1904 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
1905
1906 //
1907 // Set Logo status invalid after trying one boot option
1908 //
1909 BootLogo = NULL;
1910 Status = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);
1911 if (!EFI_ERROR (Status) && (BootLogo != NULL)) {
1912 Status = BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);
1913 ASSERT_EFI_ERROR (Status);
1914 }
1915
1916 //
1917 // Clear Boot Current
1918 //
1919 Status = gRT->SetVariable (
1920 L"BootCurrent",
1921 &gEfiGlobalVariableGuid,
1922 0,
1923 0,
1924 NULL
1925 );
1926 //
1927 // Deleting variable with current variable implementation shouldn't fail.
1928 // When BootXXXX (e.g.: BootManagerMenu) boots BootYYYY, exiting BootYYYY causes BootCurrent deleted,
1929 // exiting BootXXXX causes deleting BootCurrent returns EFI_NOT_FOUND.
1930 //
1931 ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_FOUND);
1932 }
1933
1934 /**
1935 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
1936 instances, has the same partition node with HardDriveDevicePath device path
1937
1938 @param BlockIoDevicePath Multi device path instances which need to check
1939 @param HardDriveDevicePath A device path which starts with a hard drive media
1940 device path.
1941
1942 @retval TRUE There is a matched device path instance.
1943 @retval FALSE There is no matched device path instance.
1944
1945 **/
1946 BOOLEAN
1947 BmMatchPartitionDevicePathNode (
1948 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,
1949 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
1950 )
1951 {
1952 HARDDRIVE_DEVICE_PATH *Node;
1953
1954 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {
1955 return FALSE;
1956 }
1957
1958 //
1959 // find the partition device path node
1960 //
1961 while (!IsDevicePathEnd (BlockIoDevicePath)) {
1962 if ((DevicePathType (BlockIoDevicePath) == MEDIA_DEVICE_PATH) &&
1963 (DevicePathSubType (BlockIoDevicePath) == MEDIA_HARDDRIVE_DP)
1964 ) {
1965 break;
1966 }
1967
1968 BlockIoDevicePath = NextDevicePathNode (BlockIoDevicePath);
1969 }
1970
1971 if (IsDevicePathEnd (BlockIoDevicePath)) {
1972 return FALSE;
1973 }
1974
1975 //
1976 // See if the harddrive device path in blockio matches the orig Hard Drive Node
1977 //
1978 Node = (HARDDRIVE_DEVICE_PATH *) BlockIoDevicePath;
1979
1980 //
1981 // Match Signature and PartitionNumber.
1982 // Unused bytes in Signature are initiaized with zeros.
1983 //
1984 return (BOOLEAN) (
1985 (Node->PartitionNumber == HardDriveDevicePath->PartitionNumber) &&
1986 (Node->MBRType == HardDriveDevicePath->MBRType) &&
1987 (Node->SignatureType == HardDriveDevicePath->SignatureType) &&
1988 (CompareMem (Node->Signature, HardDriveDevicePath->Signature, sizeof (Node->Signature)) == 0)
1989 );
1990 }
1991
1992 /**
1993 Emuerate all possible bootable medias in the following order:
1994 1. Removable BlockIo - The boot option only points to the removable media
1995 device, like USB key, DVD, Floppy etc.
1996 2. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
1997 like HardDisk.
1998 3. Non-BlockIo SimpleFileSystem - The boot option points to a device supporting
1999 SimpleFileSystem Protocol, but not supporting BlockIo
2000 protocol.
2001 4. LoadFile - The boot option points to the media supporting
2002 LoadFile protocol.
2003 Reference: UEFI Spec chapter 3.3 Boot Option Variables Default Boot Behavior
2004
2005 @param BootOptionCount Return the boot option count which has been found.
2006
2007 @retval Pointer to the boot option array.
2008 **/
2009 EFI_BOOT_MANAGER_LOAD_OPTION *
2010 BmEnumerateBootOptions (
2011 UINTN *BootOptionCount
2012 )
2013 {
2014 EFI_STATUS Status;
2015 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
2016 UINTN HandleCount;
2017 EFI_HANDLE *Handles;
2018 EFI_BLOCK_IO_PROTOCOL *BlkIo;
2019 UINTN Removable;
2020 UINTN Index;
2021 CHAR16 *Description;
2022
2023 ASSERT (BootOptionCount != NULL);
2024
2025 *BootOptionCount = 0;
2026 BootOptions = NULL;
2027
2028 //
2029 // Parse removable block io followed by fixed block io
2030 //
2031 gBS->LocateHandleBuffer (
2032 ByProtocol,
2033 &gEfiBlockIoProtocolGuid,
2034 NULL,
2035 &HandleCount,
2036 &Handles
2037 );
2038
2039 for (Removable = 0; Removable < 2; Removable++) {
2040 for (Index = 0; Index < HandleCount; Index++) {
2041 Status = gBS->HandleProtocol (
2042 Handles[Index],
2043 &gEfiBlockIoProtocolGuid,
2044 (VOID **) &BlkIo
2045 );
2046 if (EFI_ERROR (Status)) {
2047 continue;
2048 }
2049
2050 //
2051 // Skip the logical partitions
2052 //
2053 if (BlkIo->Media->LogicalPartition) {
2054 continue;
2055 }
2056
2057 //
2058 // Skip the fixed block io then the removable block io
2059 //
2060 if (BlkIo->Media->RemovableMedia == ((Removable == 0) ? FALSE : TRUE)) {
2061 continue;
2062 }
2063
2064 Description = BmGetBootDescription (Handles[Index]);
2065 BootOptions = ReallocatePool (
2066 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),
2067 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),
2068 BootOptions
2069 );
2070 ASSERT (BootOptions != NULL);
2071
2072 Status = EfiBootManagerInitializeLoadOption (
2073 &BootOptions[(*BootOptionCount)++],
2074 LoadOptionNumberUnassigned,
2075 LoadOptionTypeBoot,
2076 LOAD_OPTION_ACTIVE,
2077 Description,
2078 DevicePathFromHandle (Handles[Index]),
2079 NULL,
2080 0
2081 );
2082 ASSERT_EFI_ERROR (Status);
2083
2084 FreePool (Description);
2085 }
2086 }
2087
2088 if (HandleCount != 0) {
2089 FreePool (Handles);
2090 }
2091
2092 //
2093 // Parse simple file system not based on block io
2094 //
2095 gBS->LocateHandleBuffer (
2096 ByProtocol,
2097 &gEfiSimpleFileSystemProtocolGuid,
2098 NULL,
2099 &HandleCount,
2100 &Handles
2101 );
2102 for (Index = 0; Index < HandleCount; Index++) {
2103 Status = gBS->HandleProtocol (
2104 Handles[Index],
2105 &gEfiBlockIoProtocolGuid,
2106 (VOID **) &BlkIo
2107 );
2108 if (!EFI_ERROR (Status)) {
2109 //
2110 // Skip if the file system handle supports a BlkIo protocol, which we've handled in above
2111 //
2112 continue;
2113 }
2114 Description = BmGetBootDescription (Handles[Index]);
2115 BootOptions = ReallocatePool (
2116 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),
2117 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),
2118 BootOptions
2119 );
2120 ASSERT (BootOptions != NULL);
2121
2122 Status = EfiBootManagerInitializeLoadOption (
2123 &BootOptions[(*BootOptionCount)++],
2124 LoadOptionNumberUnassigned,
2125 LoadOptionTypeBoot,
2126 LOAD_OPTION_ACTIVE,
2127 Description,
2128 DevicePathFromHandle (Handles[Index]),
2129 NULL,
2130 0
2131 );
2132 ASSERT_EFI_ERROR (Status);
2133 FreePool (Description);
2134 }
2135
2136 if (HandleCount != 0) {
2137 FreePool (Handles);
2138 }
2139
2140 //
2141 // Parse load file protocol
2142 //
2143 gBS->LocateHandleBuffer (
2144 ByProtocol,
2145 &gEfiLoadFileProtocolGuid,
2146 NULL,
2147 &HandleCount,
2148 &Handles
2149 );
2150 for (Index = 0; Index < HandleCount; Index++) {
2151 //
2152 // Ignore BootManagerMenu. its boot option will be created by EfiBootManagerGetBootManagerMenu().
2153 //
2154 if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles[Index]))) {
2155 continue;
2156 }
2157
2158 Description = BmGetBootDescription (Handles[Index]);
2159 BootOptions = ReallocatePool (
2160 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),
2161 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),
2162 BootOptions
2163 );
2164 ASSERT (BootOptions != NULL);
2165
2166 Status = EfiBootManagerInitializeLoadOption (
2167 &BootOptions[(*BootOptionCount)++],
2168 LoadOptionNumberUnassigned,
2169 LoadOptionTypeBoot,
2170 LOAD_OPTION_ACTIVE,
2171 Description,
2172 DevicePathFromHandle (Handles[Index]),
2173 NULL,
2174 0
2175 );
2176 ASSERT_EFI_ERROR (Status);
2177 FreePool (Description);
2178 }
2179
2180 if (HandleCount != 0) {
2181 FreePool (Handles);
2182 }
2183
2184 BmMakeBootOptionDescriptionUnique (BootOptions, *BootOptionCount);
2185 return BootOptions;
2186 }
2187
2188 /**
2189 The function enumerates all boot options, creates them and registers them in the BootOrder variable.
2190 **/
2191 VOID
2192 EFIAPI
2193 EfiBootManagerRefreshAllBootOption (
2194 VOID
2195 )
2196 {
2197 EFI_STATUS Status;
2198 EFI_BOOT_MANAGER_LOAD_OPTION *NvBootOptions;
2199 UINTN NvBootOptionCount;
2200 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
2201 UINTN BootOptionCount;
2202 UINTN Index;
2203
2204 //
2205 // Optionally refresh the legacy boot option
2206 //
2207 if (mBmRefreshLegacyBootOption != NULL) {
2208 mBmRefreshLegacyBootOption ();
2209 }
2210
2211 BootOptions = BmEnumerateBootOptions (&BootOptionCount);
2212 NvBootOptions = EfiBootManagerGetLoadOptions (&NvBootOptionCount, LoadOptionTypeBoot);
2213
2214 //
2215 // Mark the boot option as added by BDS by setting OptionalData to a special GUID
2216 //
2217 for (Index = 0; Index < BootOptionCount; Index++) {
2218 BootOptions[Index].OptionalData = AllocateCopyPool (sizeof (EFI_GUID), &mBmAutoCreateBootOptionGuid);
2219 BootOptions[Index].OptionalDataSize = sizeof (EFI_GUID);
2220 }
2221
2222 //
2223 // Remove invalid EFI boot options from NV
2224 //
2225 for (Index = 0; Index < NvBootOptionCount; Index++) {
2226 if (((DevicePathType (NvBootOptions[Index].FilePath) != BBS_DEVICE_PATH) ||
2227 (DevicePathSubType (NvBootOptions[Index].FilePath) != BBS_BBS_DP)
2228 ) && BmIsAutoCreateBootOption (&NvBootOptions[Index])
2229 ) {
2230 //
2231 // Only check those added by BDS
2232 // so that the boot options added by end-user or OS installer won't be deleted
2233 //
2234 if (EfiBootManagerFindLoadOption (&NvBootOptions[Index], BootOptions, BootOptionCount) == -1) {
2235 Status = EfiBootManagerDeleteLoadOptionVariable (NvBootOptions[Index].OptionNumber, LoadOptionTypeBoot);
2236 //
2237 // Deleting variable with current variable implementation shouldn't fail.
2238 //
2239 ASSERT_EFI_ERROR (Status);
2240 }
2241 }
2242 }
2243
2244 //
2245 // Add new EFI boot options to NV
2246 //
2247 for (Index = 0; Index < BootOptionCount; Index++) {
2248 if (EfiBootManagerFindLoadOption (&BootOptions[Index], NvBootOptions, NvBootOptionCount) == -1) {
2249 EfiBootManagerAddLoadOptionVariable (&BootOptions[Index], (UINTN) -1);
2250 //
2251 // Try best to add the boot options so continue upon failure.
2252 //
2253 }
2254 }
2255
2256 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
2257 EfiBootManagerFreeLoadOptions (NvBootOptions, NvBootOptionCount);
2258 }
2259
2260 /**
2261 This function is called to get or create the boot option for the Boot Manager Menu.
2262
2263 The Boot Manager Menu is shown after successfully booting a boot option.
2264 Assume the BootManagerMenuFile is in the same FV as the module links to this library.
2265
2266 @param BootOption Return the boot option of the Boot Manager Menu
2267
2268 @retval EFI_SUCCESS Successfully register the Boot Manager Menu.
2269 @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found.
2270 @retval others Return status of gRT->SetVariable (). BootOption still points
2271 to the Boot Manager Menu even the Status is not EFI_SUCCESS
2272 and EFI_NOT_FOUND.
2273 **/
2274 EFI_STATUS
2275 BmRegisterBootManagerMenu (
2276 OUT EFI_BOOT_MANAGER_LOAD_OPTION *BootOption
2277 )
2278 {
2279 EFI_STATUS Status;
2280 CHAR16 *Description;
2281 UINTN DescriptionLength;
2282 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2283 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
2284 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;
2285 UINTN HandleCount;
2286 EFI_HANDLE *Handles;
2287 UINTN Index;
2288 VOID *Data;
2289 UINTN DataSize;
2290
2291 DevicePath = NULL;
2292 Description = NULL;
2293 //
2294 // Try to find BootManagerMenu from LoadFile protocol
2295 //
2296 gBS->LocateHandleBuffer (
2297 ByProtocol,
2298 &gEfiLoadFileProtocolGuid,
2299 NULL,
2300 &HandleCount,
2301 &Handles
2302 );
2303 for (Index = 0; Index < HandleCount; Index++) {
2304 if (BmIsBootManagerMenuFilePath (DevicePathFromHandle (Handles[Index]))) {
2305 DevicePath = DuplicateDevicePath (DevicePathFromHandle (Handles[Index]));
2306 Description = BmGetBootDescription (Handles[Index]);
2307 break;
2308 }
2309 }
2310 if (HandleCount != 0) {
2311 FreePool (Handles);
2312 }
2313
2314 if (DevicePath == NULL) {
2315 Data = NULL;
2316 Status = GetSectionFromFv (
2317 PcdGetPtr (PcdBootManagerMenuFile),
2318 EFI_SECTION_PE32,
2319 0,
2320 (VOID **) &Data,
2321 &DataSize
2322 );
2323 if (Data != NULL) {
2324 FreePool (Data);
2325 }
2326 if (EFI_ERROR (Status)) {
2327 DEBUG ((EFI_D_WARN, "[Bds]BootManagerMenu FFS section can not be found, skip its boot option registration\n"));
2328 return EFI_NOT_FOUND;
2329 }
2330
2331 //
2332 // Get BootManagerMenu application's description from EFI User Interface Section.
2333 //
2334 Status = GetSectionFromFv (
2335 PcdGetPtr (PcdBootManagerMenuFile),
2336 EFI_SECTION_USER_INTERFACE,
2337 0,
2338 (VOID **) &Description,
2339 &DescriptionLength
2340 );
2341 if (EFI_ERROR (Status)) {
2342 Description = NULL;
2343 }
2344
2345 EfiInitializeFwVolDevicepathNode (&FileNode, PcdGetPtr (PcdBootManagerMenuFile));
2346 Status = gBS->HandleProtocol (
2347 gImageHandle,
2348 &gEfiLoadedImageProtocolGuid,
2349 (VOID **) &LoadedImage
2350 );
2351 ASSERT_EFI_ERROR (Status);
2352 DevicePath = AppendDevicePathNode (
2353 DevicePathFromHandle (LoadedImage->DeviceHandle),
2354 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode
2355 );
2356 ASSERT (DevicePath != NULL);
2357 }
2358
2359 Status = EfiBootManagerInitializeLoadOption (
2360 BootOption,
2361 LoadOptionNumberUnassigned,
2362 LoadOptionTypeBoot,
2363 LOAD_OPTION_CATEGORY_APP | LOAD_OPTION_ACTIVE | LOAD_OPTION_HIDDEN,
2364 (Description != NULL) ? Description : L"Boot Manager Menu",
2365 DevicePath,
2366 NULL,
2367 0
2368 );
2369 ASSERT_EFI_ERROR (Status);
2370 FreePool (DevicePath);
2371 if (Description != NULL) {
2372 FreePool (Description);
2373 }
2374
2375 DEBUG_CODE (
2376 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
2377 UINTN BootOptionCount;
2378
2379 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
2380 ASSERT (EfiBootManagerFindLoadOption (BootOption, BootOptions, BootOptionCount) == -1);
2381 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
2382 );
2383
2384 return EfiBootManagerAddLoadOptionVariable (BootOption, 0);
2385 }
2386
2387 /**
2388 Return the boot option corresponding to the Boot Manager Menu.
2389 It may automatically create one if the boot option hasn't been created yet.
2390
2391 @param BootOption Return the Boot Manager Menu.
2392
2393 @retval EFI_SUCCESS The Boot Manager Menu is successfully returned.
2394 @retval EFI_NOT_FOUND The Boot Manager Menu cannot be found.
2395 @retval others Return status of gRT->SetVariable (). BootOption still points
2396 to the Boot Manager Menu even the Status is not EFI_SUCCESS
2397 and EFI_NOT_FOUND.
2398 **/
2399 EFI_STATUS
2400 EFIAPI
2401 EfiBootManagerGetBootManagerMenu (
2402 EFI_BOOT_MANAGER_LOAD_OPTION *BootOption
2403 )
2404 {
2405 EFI_STATUS Status;
2406 UINTN BootOptionCount;
2407 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;
2408 UINTN Index;
2409
2410 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
2411
2412 for (Index = 0; Index < BootOptionCount; Index++) {
2413 if (BmIsBootManagerMenuFilePath (BootOptions[Index].FilePath)) {
2414 Status = EfiBootManagerInitializeLoadOption (
2415 BootOption,
2416 BootOptions[Index].OptionNumber,
2417 BootOptions[Index].OptionType,
2418 BootOptions[Index].Attributes,
2419 BootOptions[Index].Description,
2420 BootOptions[Index].FilePath,
2421 BootOptions[Index].OptionalData,
2422 BootOptions[Index].OptionalDataSize
2423 );
2424 ASSERT_EFI_ERROR (Status);
2425 break;
2426 }
2427 }
2428
2429 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);
2430
2431 //
2432 // Automatically create the Boot#### for Boot Manager Menu when not found.
2433 //
2434 if (Index == BootOptionCount) {
2435 return BmRegisterBootManagerMenu (BootOption);
2436 } else {
2437 return EFI_SUCCESS;
2438 }
2439 }
2440