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