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