]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
MdeModulePkg/Bds: Wide match HTTP boot option.
[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
bf5328f0
RN
1530/**\r
1531 Check whether Left and Right are the same without matching the specific\r
1532 device path data in IP device path and URI device path node.\r
1533\r
1534 @retval TRUE Left and Right are the same.\r
1535 @retval FALSE Left and Right are the different.\r
1536**/\r
1537BOOLEAN\r
1538BmMatchHttpBootDevicePath (\r
1539 IN EFI_DEVICE_PATH_PROTOCOL *Left,\r
1540 IN EFI_DEVICE_PATH_PROTOCOL *Right\r
1541 )\r
1542{\r
1543 for (; !IsDevicePathEnd (Left) && !IsDevicePathEnd (Right)\r
1544 ; Left = NextDevicePathNode (Left), Right = NextDevicePathNode (Right)\r
1545 ) {\r
1546 if (CompareMem (Left, Right, DevicePathNodeLength (Left)) != 0) {\r
1547 if ((DevicePathType (Left) != MESSAGING_DEVICE_PATH) || (DevicePathType (Right) != MESSAGING_DEVICE_PATH)) {\r
1548 return FALSE;\r
1549 }\r
1550\r
1551 if (((DevicePathSubType (Left) != MSG_IPv4_DP) || (DevicePathSubType (Right) != MSG_IPv4_DP)) &&\r
1552 ((DevicePathSubType (Left) != MSG_IPv6_DP) || (DevicePathSubType (Right) != MSG_IPv6_DP)) &&\r
1553 ((DevicePathSubType (Left) != MSG_URI_DP) || (DevicePathSubType (Right) != MSG_URI_DP))\r
1554 ) {\r
1555 return FALSE;\r
1556 }\r
1557 }\r
1558 }\r
1559 return (BOOLEAN) (IsDevicePathEnd (Left) && IsDevicePathEnd (Right));\r
1560}\r
1561\r
1562/**\r
1563 Get the file buffer from Load File instance.\r
1564\r
1565 @param FilePath The media device path pointing to a LoadFile instance.\r
1566 @param FullPath Return the full device path pointing to the load option.\r
1567 @param FileSize Return the size of the load option.\r
1568\r
1569 @return The load option buffer.\r
1570**/\r
1571VOID *\r
1572BmGetFileBufferFromLoadFile (\r
1573 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
1574 OUT EFI_DEVICE_PATH_PROTOCOL **FullPath,\r
1575 OUT UINTN *FileSize\r
1576 )\r
1577{\r
1578 EFI_STATUS Status;\r
1579 EFI_HANDLE Handle;\r
1580 VOID *FileBuffer;\r
1581 EFI_HANDLE *Handles;\r
1582 UINTN HandleCount;\r
1583 UINTN Index;\r
1584 EFI_DEVICE_PATH_PROTOCOL *Node;\r
1585 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
1586 UINTN BufferSize;\r
1587\r
1588 //\r
1589 // Get file buffer from load file instance.\r
1590 //\r
1591 Node = FilePath;\r
1592 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);\r
1593 if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) {\r
1594 //\r
1595 // When wide match happens, pass full device path to LoadFile (),\r
1596 // otherwise, pass remaining device path to LoadFile ().\r
1597 //\r
1598 FilePath = Node;\r
1599 } else {\r
1600 Handle = NULL;\r
1601 //\r
1602 // Use wide match algorithm to find one when\r
1603 // cannot find a LoadFile instance to exactly match the FilePath\r
1604 //\r
1605 Status = gBS->LocateHandleBuffer (\r
1606 ByProtocol,\r
1607 &gEfiLoadFileProtocolGuid,\r
1608 NULL,\r
1609 &HandleCount,\r
1610 &Handles\r
1611 );\r
1612 if (EFI_ERROR (Status)) {\r
1613 Handles = NULL;\r
1614 HandleCount = 0;\r
1615 }\r
1616 for (Index = 0; Index < HandleCount; Index++) {\r
1617 if (BmMatchHttpBootDevicePath (DevicePathFromHandle (Handles[Index]), FilePath)) {\r
1618 Handle = Handles[Index];\r
1619 break;\r
1620 }\r
1621 }\r
1622 if (Handles != NULL) {\r
1623 FreePool (Handles);\r
1624 }\r
1625 }\r
1626\r
1627 if (Handle == NULL) {\r
1628 return NULL;\r
1629 }\r
1630\r
1631 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID **) &LoadFile);\r
1632 ASSERT_EFI_ERROR (Status);\r
1633\r
1634 BufferSize = 0;\r
1635 FileBuffer = NULL;\r
1636 Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer);\r
1637 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1638 FileBuffer = AllocatePool (BufferSize);\r
1639 if (FileBuffer != NULL) {\r
1640 Status = EFI_SUCCESS;\r
1641 }\r
1642 }\r
1643\r
1644 if (!EFI_ERROR (Status)) {\r
1645 Status = LoadFile->LoadFile (LoadFile, FilePath, TRUE, &BufferSize, FileBuffer);\r
1646 }\r
1647\r
1648 if (!EFI_ERROR (Status)) {\r
1649 //\r
1650 // LoadFile () may cause the device path of the Handle be updated.\r
1651 //\r
1652 *FullPath = DuplicateDevicePath (DevicePathFromHandle (Handle));\r
1653 *FileSize = BufferSize;\r
1654 return FileBuffer;\r
1655 } else {\r
1656 return NULL;\r
1657 }\r
1658}\r
1659\r
067ed98a
RN
1660/**\r
1661 Get the load option by its device path.\r
1662\r
1663 @param FilePath The device path pointing to a load option.\r
1664 It could be a short-form device path.\r
1665 @param FullPath Return the full device path of the load option after\r
1666 short-form device path expanding.\r
1667 Caller is responsible to free it.\r
1668 @param FileSize Return the load option size.\r
1669\r
1670 @return The load option buffer. Caller is responsible to free the memory.\r
1671**/\r
1672VOID *\r
1673BmGetLoadOptionBuffer (\r
1674 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
1675 OUT EFI_DEVICE_PATH_PROTOCOL **FullPath,\r
1676 OUT UINTN *FileSize\r
1677 )\r
1678{\r
1679 EFI_HANDLE Handle;\r
1680 VOID *FileBuffer;\r
1681 UINT32 AuthenticationStatus;\r
1682 EFI_DEVICE_PATH_PROTOCOL *Node;\r
1683 EFI_STATUS Status;\r
1684\r
1685 ASSERT ((FilePath != NULL) && (FullPath != NULL) && (FileSize != NULL));\r
1686\r
1687 EfiBootManagerConnectDevicePath (FilePath, NULL);\r
1688\r
1689 *FullPath = NULL;\r
1690 *FileSize = 0;\r
1691 FileBuffer = NULL;\r
1692\r
1693 //\r
1694 // Boot from media device by adding a default file name \EFI\BOOT\BOOT{machine type short-name}.EFI\r
1695 //\r
1696 Node = FilePath;\r
1697 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle);\r
1698 if (EFI_ERROR (Status)) {\r
1699 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &Node, &Handle);\r
1700 }\r
1701\r
1702 if (!EFI_ERROR (Status) && IsDevicePathEnd (Node)) {\r
1703 return BmExpandMediaDevicePath (FilePath, FullPath, FileSize);\r
1704 }\r
1705\r
1706 //\r
1707 // Expand the short-form device path to full device path\r
1708 //\r
1709 if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) &&\r
1710 (DevicePathSubType (FilePath) == MEDIA_HARDDRIVE_DP)) {\r
1711 //\r
1712 // Expand the Harddrive device path\r
1713 //\r
1714 return BmExpandPartitionDevicePath (FilePath, FullPath, FileSize);\r
ccb66799
RN
1715 } else if ((DevicePathType (FilePath) == MEDIA_DEVICE_PATH) &&\r
1716 (DevicePathSubType (FilePath) == MEDIA_FILEPATH_DP)) {\r
1717 //\r
1718 // Expand the File-path device path\r
1719 //\r
1720 return BmExpandFileDevicePath (FilePath, FullPath, FileSize);\r
067ed98a
RN
1721 } else {\r
1722 for (Node = FilePath; !IsDevicePathEnd (Node); Node = NextDevicePathNode (Node)) {\r
1723 if ((DevicePathType (Node) == MESSAGING_DEVICE_PATH) &&\r
1724 ((DevicePathSubType (Node) == MSG_USB_CLASS_DP) || (DevicePathSubType (Node) == MSG_USB_WWID_DP))) {\r
1725 break;\r
1726 }\r
1727 }\r
1728\r
1729 if (!IsDevicePathEnd (Node)) {\r
1730 //\r
1731 // Expand the USB WWID/Class device path\r
1732 //\r
1733 FileBuffer = BmExpandUsbDevicePath (FilePath, FullPath, FileSize, Node);\r
1734 if ((FileBuffer == NULL) && (FilePath == Node)) {\r
1735 //\r
1736 // Boot Option device path starts with USB Class or USB WWID device path.\r
1737 // For Boot Option device path which doesn't begin with the USB Class or\r
1738 // USB WWID device path, it's not needed to connect again here.\r
1739 //\r
1740 BmConnectUsbShortFormDevicePath (FilePath);\r
1741 FileBuffer = BmExpandUsbDevicePath (FilePath, FullPath, FileSize, Node);\r
1742 }\r
1743 return FileBuffer;\r
1744 }\r
1745 }\r
1746\r
1747 //\r
525839ed 1748 // Get file buffer from FV file path.\r
067ed98a 1749 //\r
525839ed
RN
1750 if (BmIsFvFilePath (FilePath)) {\r
1751 return BmGetFileBufferByFvFilePath (FilePath, FullPath, FileSize);\r
067ed98a
RN
1752 }\r
1753\r
1754 //\r
bf5328f0 1755 // Get file buffer from simple file system.\r
067ed98a 1756 //\r
de6c0eff 1757 Node = FilePath;\r
bf5328f0
RN
1758 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &Node, &Handle);\r
1759 if (!EFI_ERROR (Status)) {\r
1760 FileBuffer = GetFileBufferByFilePath (TRUE, FilePath, FileSize, &AuthenticationStatus);\r
1761 if (FileBuffer != NULL) {\r
de6c0eff 1762 *FullPath = DuplicateDevicePath (FilePath);\r
de6c0eff 1763 }\r
bf5328f0 1764 return FileBuffer;\r
067ed98a
RN
1765 }\r
1766\r
bf5328f0 1767 return BmGetFileBufferFromLoadFile (FilePath, FullPath, FileSize);\r
067ed98a
RN
1768}\r
1769\r
1770/**\r
1771 Attempt to boot the EFI boot option. This routine sets L"BootCurent" and\r
1772 also signals the EFI ready to boot event. If the device path for the option\r
1773 starts with a BBS device path a legacy boot is attempted via the registered \r
1774 gLegacyBoot function. Short form device paths are also supported via this \r
1775 rountine. A device path starting with MEDIA_HARDDRIVE_DP, MSG_USB_WWID_DP,\r
1776 MSG_USB_CLASS_DP gets expaned out to find the first device that matches.\r
1777 If the BootOption Device Path fails the removable media boot algorithm \r
1778 is attempted (\EFI\BOOTIA32.EFI, \EFI\BOOTX64.EFI,... only one file type \r
1779 is tried per processor type)\r
1780\r
1781 @param BootOption Boot Option to try and boot.\r
1782 On return, BootOption->Status contains the boot status.\r
1783 EFI_SUCCESS BootOption was booted\r
1784 EFI_UNSUPPORTED A BBS device path was found with no valid callback\r
1785 registered via EfiBootManagerInitialize().\r
1786 EFI_NOT_FOUND The BootOption was not found on the system\r
1787 !EFI_SUCCESS BootOption failed with this error status\r
1788\r
1789**/\r
1790VOID\r
1791EFIAPI\r
1792EfiBootManagerBoot (\r
1793 IN EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r
1794 )\r
1795{\r
1796 EFI_STATUS Status;\r
1797 EFI_HANDLE ImageHandle;\r
1798 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
1799 UINT16 Uint16;\r
1800 UINTN OptionNumber;\r
1801 UINTN OriginalOptionNumber;\r
1802 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
1803 EFI_DEVICE_PATH_PROTOCOL *Node;\r
1804 EFI_HANDLE FvHandle;\r
1805 VOID *FileBuffer;\r
1806 UINTN FileSize;\r
1807 EFI_BOOT_LOGO_PROTOCOL *BootLogo;\r
1808 EFI_EVENT LegacyBootEvent;\r
1809\r
1810 if (BootOption == NULL) {\r
1811 return;\r
1812 }\r
1813\r
1814 if (BootOption->FilePath == NULL || BootOption->OptionType != LoadOptionTypeBoot) {\r
1815 BootOption->Status = EFI_INVALID_PARAMETER;\r
1816 return;\r
1817 }\r
1818\r
1819 //\r
1820 // 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
1821 //\r
1822 OptionNumber = BmFindBootOptionInVariable (BootOption);\r
1823 if (OptionNumber == LoadOptionNumberUnassigned) {\r
1824 Status = BmGetFreeOptionNumber (LoadOptionTypeBoot, &Uint16);\r
1825 if (!EFI_ERROR (Status)) {\r
1826 //\r
1827 // Save the BootOption->OptionNumber to restore later\r
1828 //\r
1829 OptionNumber = Uint16;\r
1830 OriginalOptionNumber = BootOption->OptionNumber;\r
1831 BootOption->OptionNumber = OptionNumber;\r
1832 Status = EfiBootManagerLoadOptionToVariable (BootOption);\r
1833 BootOption->OptionNumber = OriginalOptionNumber;\r
1834 }\r
1835\r
1836 if (EFI_ERROR (Status)) {\r
1837 DEBUG ((EFI_D_ERROR, "[Bds] Failed to create Boot#### for a temporary boot - %r!\n", Status));\r
1838 BootOption->Status = Status;\r
1839 return ;\r
1840 }\r
1841 }\r
1842\r
1843 //\r
1844 // 2. Set BootCurrent\r
1845 //\r
1846 Uint16 = (UINT16) OptionNumber;\r
1847 BmSetVariableAndReportStatusCodeOnError (\r
1848 L"BootCurrent",\r
1849 &gEfiGlobalVariableGuid,\r
1850 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
1851 sizeof (UINT16),\r
1852 &Uint16\r
1853 );\r
1854\r
1855 //\r
1856 // 3. Signal the EVT_SIGNAL_READY_TO_BOOT event when we are about to load and execute\r
1857 // the boot option.\r
1858 //\r
1859 Node = BootOption->FilePath;\r
1860 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &FvHandle);\r
1861 if (!EFI_ERROR (Status) && CompareGuid (\r
1862 EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) Node),\r
1863 PcdGetPtr (PcdBootManagerMenuFile)\r
1864 )) {\r
1865 DEBUG ((EFI_D_INFO, "[Bds] Booting Boot Manager Menu.\n"));\r
1866 BmStopHotkeyService (NULL, NULL);\r
1867 } else {\r
1868 EfiSignalEventReadyToBoot();\r
1869 //\r
1870 // Report Status Code to indicate ReadyToBoot was signalled\r
1871 //\r
1872 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));\r
1873 //\r
1874 // 4. Repair system through DriverHealth protocol\r
1875 //\r
1876 BmRepairAllControllers ();\r
1877 }\r
1878\r
1879 PERF_START_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);\r
1880\r
1881 //\r
1882 // 5. Load EFI boot option to ImageHandle\r
1883 //\r
1884 ImageHandle = NULL;\r
1885 if (DevicePathType (BootOption->FilePath) != BBS_DEVICE_PATH) {\r
1886 Status = EFI_NOT_FOUND;\r
1887 FileBuffer = BmGetLoadOptionBuffer (BootOption->FilePath, &FilePath, &FileSize);\r
1888 DEBUG_CODE (\r
1889 if (FileBuffer != NULL && CompareMem (BootOption->FilePath, FilePath, GetDevicePathSize (FilePath)) != 0) {\r
1890 DEBUG ((EFI_D_INFO, "[Bds] DevicePath expand: "));\r
1891 BmPrintDp (BootOption->FilePath);\r
1892 DEBUG ((EFI_D_INFO, " -> "));\r
1893 BmPrintDp (FilePath);\r
1894 DEBUG ((EFI_D_INFO, "\n"));\r
1895 }\r
1896 );\r
1897 if (BmIsLoadOptionPeHeaderValid (BootOption->OptionType, FileBuffer, FileSize)) {\r
1898 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));\r
1899 Status = gBS->LoadImage (\r
1900 TRUE,\r
1901 gImageHandle,\r
1902 FilePath,\r
1903 FileBuffer,\r
1904 FileSize,\r
1905 &ImageHandle\r
1906 );\r
1907 }\r
1908 if (FileBuffer != NULL) {\r
1909 FreePool (FileBuffer);\r
1910 }\r
1911 if (FilePath != NULL) {\r
1912 FreePool (FilePath);\r
1913 }\r
1914\r
1915 if (EFI_ERROR (Status)) {\r
1916 //\r
1917 // Report Status Code to indicate that the failure to load boot option\r
1918 //\r
1919 REPORT_STATUS_CODE (\r
1920 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1921 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)\r
1922 );\r
1923 BootOption->Status = Status;\r
1924 return;\r
1925 }\r
1926 }\r
1927\r
1928 //\r
1929 // 6. Adjust the different type memory page number just before booting\r
1930 // and save the updated info into the variable for next boot to use\r
1931 //\r
665b26ba
RN
1932 BmSetMemoryTypeInformationVariable (\r
1933 (BOOLEAN) ((BootOption->Attributes & LOAD_OPTION_CATEGORY) == LOAD_OPTION_CATEGORY_BOOT)\r
1934 );\r
067ed98a
RN
1935\r
1936 DEBUG_CODE_BEGIN();\r
1937 if (BootOption->Description == NULL) {\r
1938 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting from unknown device path\n"));\r
1939 } else {\r
1940 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "[Bds]Booting %s\n", BootOption->Description));\r
1941 }\r
1942 DEBUG_CODE_END();\r
1943\r
1944 //\r
1945 // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
1946 // Write boot to OS performance data for Legacy boot\r
1947 //\r
1948 if ((DevicePathType (BootOption->FilePath) == BBS_DEVICE_PATH) && (DevicePathSubType (BootOption->FilePath) == BBS_BBS_DP)) {\r
1949 if (mBmLegacyBoot != NULL) {\r
1950 //\r
1951 // Write boot to OS performance data for legacy boot.\r
1952 //\r
1953 PERF_CODE (\r
1954 //\r
1955 // Create an event to be signalled when Legacy Boot occurs to write performance data.\r
1956 //\r
1957 Status = EfiCreateEventLegacyBootEx(\r
1958 TPL_NOTIFY,\r
1959 BmWriteBootToOsPerformanceData,\r
1960 NULL, \r
1961 &LegacyBootEvent\r
1962 );\r
1963 ASSERT_EFI_ERROR (Status);\r
1964 );\r
1965\r
1966 mBmLegacyBoot (BootOption);\r
1967 } else {\r
1968 BootOption->Status = EFI_UNSUPPORTED;\r
1969 }\r
1970\r
1971 PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);\r
1972 return;\r
1973 }\r
1974 \r
1975 //\r
1976 // Provide the image with its load options\r
1977 //\r
1978 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
1979 ASSERT_EFI_ERROR (Status);\r
1980\r
404bd442
RN
1981 if (!BmIsAutoCreateBootOption (BootOption)) {\r
1982 ImageInfo->LoadOptionsSize = BootOption->OptionalDataSize;\r
1983 ImageInfo->LoadOptions = BootOption->OptionalData;\r
1984 }\r
067ed98a
RN
1985\r
1986 //\r
1987 // Clean to NULL because the image is loaded directly from the firmwares boot manager.\r
1988 //\r
1989 ImageInfo->ParentHandle = NULL;\r
1990\r
1991 //\r
1992 // Before calling the image, enable the Watchdog Timer for 5 minutes period\r
1993 //\r
1994 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
1995\r
1996 //\r
1997 // Write boot to OS performance data for UEFI boot\r
1998 //\r
1999 PERF_CODE (\r
2000 BmWriteBootToOsPerformanceData (NULL, NULL);\r
2001 );\r
2002\r
2003 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));\r
2004\r
2005 Status = gBS->StartImage (ImageHandle, &BootOption->ExitDataSize, &BootOption->ExitData);\r
2006 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
2007 BootOption->Status = Status;\r
2008 if (EFI_ERROR (Status)) {\r
2009 //\r
2010 // Report Status Code to indicate that boot failure\r
2011 //\r
2012 REPORT_STATUS_CODE (\r
2013 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
2014 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)\r
2015 );\r
2016 }\r
2017 PERF_END_EX (gImageHandle, "BdsAttempt", NULL, 0, (UINT32) OptionNumber);\r
2018\r
2019 //\r
2020 // Clear the Watchdog Timer after the image returns\r
2021 //\r
2022 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
2023\r
2024 //\r
2025 // Set Logo status invalid after trying one boot option\r
2026 //\r
2027 BootLogo = NULL;\r
2028 Status = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);\r
2029 if (!EFI_ERROR (Status) && (BootLogo != NULL)) {\r
2030 Status = BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);\r
2031 ASSERT_EFI_ERROR (Status);\r
2032 }\r
2033\r
2034 //\r
2035 // Clear Boot Current\r
2036 //\r
2037 Status = gRT->SetVariable (\r
2038 L"BootCurrent",\r
2039 &gEfiGlobalVariableGuid,\r
2040 0,\r
2041 0,\r
2042 NULL\r
2043 );\r
2044 //\r
2045 // Deleting variable with current variable implementation shouldn't fail.\r
2046 // When BootXXXX (e.g.: BootManagerMenu) boots BootYYYY, exiting BootYYYY causes BootCurrent deleted,\r
2047 // exiting BootXXXX causes deleting BootCurrent returns EFI_NOT_FOUND.\r
2048 //\r
2049 ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_FOUND);\r
2050}\r
2051\r
2052/**\r
2053 Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
2054 instances, has the same partition node with HardDriveDevicePath device path\r
2055\r
2056 @param BlockIoDevicePath Multi device path instances which need to check\r
2057 @param HardDriveDevicePath A device path which starts with a hard drive media\r
2058 device path.\r
2059\r
2060 @retval TRUE There is a matched device path instance.\r
2061 @retval FALSE There is no matched device path instance.\r
2062\r
2063**/\r
2064BOOLEAN\r
2065BmMatchPartitionDevicePathNode (\r
2066 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r
2067 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
2068 )\r
2069{\r
2070 HARDDRIVE_DEVICE_PATH *Node;\r
2071\r
2072 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
2073 return FALSE;\r
2074 }\r
2075\r
2076 //\r
2077 // find the partition device path node\r
2078 //\r
2079 while (!IsDevicePathEnd (BlockIoDevicePath)) {\r
2080 if ((DevicePathType (BlockIoDevicePath) == MEDIA_DEVICE_PATH) &&\r
2081 (DevicePathSubType (BlockIoDevicePath) == MEDIA_HARDDRIVE_DP)\r
2082 ) {\r
2083 break;\r
2084 }\r
2085\r
2086 BlockIoDevicePath = NextDevicePathNode (BlockIoDevicePath);\r
2087 }\r
2088\r
2089 if (IsDevicePathEnd (BlockIoDevicePath)) {\r
2090 return FALSE;\r
2091 }\r
2092\r
2093 //\r
2094 // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
2095 //\r
2096 Node = (HARDDRIVE_DEVICE_PATH *) BlockIoDevicePath;\r
2097\r
2098 //\r
2099 // Match Signature and PartitionNumber.\r
2100 // Unused bytes in Signature are initiaized with zeros.\r
2101 //\r
2102 return (BOOLEAN) (\r
2103 (Node->PartitionNumber == HardDriveDevicePath->PartitionNumber) &&\r
2104 (Node->MBRType == HardDriveDevicePath->MBRType) &&\r
2105 (Node->SignatureType == HardDriveDevicePath->SignatureType) &&\r
2106 (CompareMem (Node->Signature, HardDriveDevicePath->Signature, sizeof (Node->Signature)) == 0)\r
2107 );\r
2108}\r
2109\r
d948fe96
RN
2110/**\r
2111 Enumerate all boot option descriptions and append " 2"/" 3"/... to make\r
2112 unique description.\r
2113\r
2114 @param BootOptions Array of boot options.\r
2115 @param BootOptionCount Count of boot options.\r
2116**/\r
2117VOID\r
2118BmMakeBootOptionDescriptionUnique (\r
2119 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions,\r
2120 UINTN BootOptionCount\r
2121 )\r
2122{\r
2123 UINTN Base;\r
2124 UINTN Index;\r
2125 UINTN DescriptionSize;\r
2126 UINTN MaxSuffixSize;\r
2127 BOOLEAN *Visited;\r
2128 UINTN MatchCount;\r
2129\r
2130 if (BootOptionCount == 0) {\r
2131 return;\r
2132 }\r
2133\r
2134 //\r
2135 // Calculate the maximum buffer size for the number suffix.\r
2136 // The initial sizeof (CHAR16) is for the blank space before the number.\r
2137 //\r
2138 MaxSuffixSize = sizeof (CHAR16);\r
2139 for (Index = BootOptionCount; Index != 0; Index = Index / 10) {\r
2140 MaxSuffixSize += sizeof (CHAR16);\r
2141 }\r
2142\r
2143 Visited = AllocateZeroPool (sizeof (BOOLEAN) * BootOptionCount);\r
2144 ASSERT (Visited != NULL);\r
2145\r
2146 for (Base = 0; Base < BootOptionCount; Base++) {\r
2147 if (!Visited[Base]) {\r
2148 MatchCount = 1;\r
2149 Visited[Base] = TRUE;\r
2150 DescriptionSize = StrSize (BootOptions[Base].Description);\r
2151 for (Index = Base + 1; Index < BootOptionCount; Index++) {\r
2152 if (!Visited[Index] && StrCmp (BootOptions[Base].Description, BootOptions[Index].Description) == 0) {\r
2153 Visited[Index] = TRUE;\r
2154 MatchCount++;\r
2155 FreePool (BootOptions[Index].Description);\r
2156 BootOptions[Index].Description = AllocatePool (DescriptionSize + MaxSuffixSize);\r
2157 UnicodeSPrint (\r
2158 BootOptions[Index].Description, DescriptionSize + MaxSuffixSize,\r
2159 L"%s %d",\r
2160 BootOptions[Base].Description, MatchCount\r
2161 );\r
2162 }\r
2163 }\r
2164 }\r
2165 }\r
2166\r
2167 FreePool (Visited);\r
2168}\r
2169\r
067ed98a
RN
2170/**\r
2171 Emuerate all possible bootable medias in the following order:\r
2172 1. Removable BlockIo - The boot option only points to the removable media\r
2173 device, like USB key, DVD, Floppy etc.\r
2174 2. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r
2175 like HardDisk.\r
2176 3. Non-BlockIo SimpleFileSystem - The boot option points to a device supporting\r
2177 SimpleFileSystem Protocol, but not supporting BlockIo\r
2178 protocol.\r
2179 4. LoadFile - The boot option points to the media supporting \r
2180 LoadFile protocol.\r
2181 Reference: UEFI Spec chapter 3.3 Boot Option Variables Default Boot Behavior\r
2182\r
2183 @param BootOptionCount Return the boot option count which has been found.\r
2184\r
2185 @retval Pointer to the boot option array.\r
2186**/\r
2187EFI_BOOT_MANAGER_LOAD_OPTION *\r
2188BmEnumerateBootOptions (\r
2189 UINTN *BootOptionCount\r
2190 )\r
2191{\r
2192 EFI_STATUS Status;\r
2193 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r
067ed98a
RN
2194 UINTN HandleCount;\r
2195 EFI_HANDLE *Handles;\r
2196 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
2197 UINTN Removable;\r
2198 UINTN Index;\r
f41c71d2 2199 CHAR16 *Description;\r
067ed98a
RN
2200\r
2201 ASSERT (BootOptionCount != NULL);\r
2202\r
2203 *BootOptionCount = 0;\r
2204 BootOptions = NULL;\r
2205\r
2206 //\r
2207 // Parse removable block io followed by fixed block io\r
2208 //\r
2209 gBS->LocateHandleBuffer (\r
2210 ByProtocol,\r
2211 &gEfiBlockIoProtocolGuid,\r
2212 NULL,\r
2213 &HandleCount,\r
2214 &Handles\r
2215 );\r
2216\r
2217 for (Removable = 0; Removable < 2; Removable++) {\r
2218 for (Index = 0; Index < HandleCount; Index++) {\r
2219 Status = gBS->HandleProtocol (\r
2220 Handles[Index],\r
2221 &gEfiBlockIoProtocolGuid,\r
2222 (VOID **) &BlkIo\r
2223 );\r
2224 if (EFI_ERROR (Status)) {\r
2225 continue;\r
2226 }\r
2227\r
2228 //\r
2229 // Skip the logical partitions\r
2230 //\r
2231 if (BlkIo->Media->LogicalPartition) {\r
2232 continue;\r
2233 }\r
2234\r
2235 //\r
2236 // Skip the fixed block io then the removable block io\r
2237 //\r
2238 if (BlkIo->Media->RemovableMedia == ((Removable == 0) ? FALSE : TRUE)) {\r
2239 continue;\r
2240 }\r
2241\r
f41c71d2 2242 Description = BmGetBootDescription (Handles[Index]);\r
067ed98a
RN
2243 BootOptions = ReallocatePool (\r
2244 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),\r
2245 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),\r
2246 BootOptions\r
2247 );\r
2248 ASSERT (BootOptions != NULL);\r
2249\r
2250 Status = EfiBootManagerInitializeLoadOption (\r
2251 &BootOptions[(*BootOptionCount)++],\r
2252 LoadOptionNumberUnassigned,\r
2253 LoadOptionTypeBoot,\r
2254 LOAD_OPTION_ACTIVE,\r
f41c71d2 2255 Description,\r
067ed98a
RN
2256 DevicePathFromHandle (Handles[Index]),\r
2257 NULL,\r
2258 0\r
2259 );\r
2260 ASSERT_EFI_ERROR (Status);\r
2261\r
f41c71d2 2262 FreePool (Description);\r
067ed98a
RN
2263 }\r
2264 }\r
2265\r
2266 if (HandleCount != 0) {\r
2267 FreePool (Handles);\r
2268 }\r
2269\r
2270 //\r
2271 // Parse simple file system not based on block io\r
2272 //\r
067ed98a
RN
2273 gBS->LocateHandleBuffer (\r
2274 ByProtocol,\r
2275 &gEfiSimpleFileSystemProtocolGuid,\r
2276 NULL,\r
2277 &HandleCount,\r
2278 &Handles\r
2279 );\r
2280 for (Index = 0; Index < HandleCount; Index++) {\r
2281 Status = gBS->HandleProtocol (\r
2282 Handles[Index],\r
2283 &gEfiBlockIoProtocolGuid,\r
2284 (VOID **) &BlkIo\r
2285 );\r
2286 if (!EFI_ERROR (Status)) {\r
2287 //\r
2288 // Skip if the file system handle supports a BlkIo protocol, which we've handled in above\r
2289 //\r
2290 continue;\r
2291 }\r
f41c71d2 2292 Description = BmGetBootDescription (Handles[Index]);\r
067ed98a
RN
2293 BootOptions = ReallocatePool (\r
2294 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),\r
2295 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),\r
2296 BootOptions\r
2297 );\r
2298 ASSERT (BootOptions != NULL);\r
2299\r
2300 Status = EfiBootManagerInitializeLoadOption (\r
2301 &BootOptions[(*BootOptionCount)++],\r
2302 LoadOptionNumberUnassigned,\r
2303 LoadOptionTypeBoot,\r
2304 LOAD_OPTION_ACTIVE,\r
2305 Description,\r
2306 DevicePathFromHandle (Handles[Index]),\r
2307 NULL,\r
2308 0\r
2309 );\r
2310 ASSERT_EFI_ERROR (Status);\r
f41c71d2 2311 FreePool (Description);\r
067ed98a
RN
2312 }\r
2313\r
2314 if (HandleCount != 0) {\r
2315 FreePool (Handles);\r
2316 }\r
2317\r
2318 //\r
2319 // Parse load file, assuming UEFI Network boot option\r
2320 //\r
2321 gBS->LocateHandleBuffer (\r
2322 ByProtocol,\r
2323 &gEfiLoadFileProtocolGuid,\r
2324 NULL,\r
2325 &HandleCount,\r
2326 &Handles\r
2327 );\r
2328 for (Index = 0; Index < HandleCount; Index++) {\r
2329\r
f41c71d2 2330 Description = BmGetBootDescription (Handles[Index]);\r
067ed98a
RN
2331 BootOptions = ReallocatePool (\r
2332 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),\r
2333 sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),\r
2334 BootOptions\r
2335 );\r
2336 ASSERT (BootOptions != NULL);\r
2337\r
2338 Status = EfiBootManagerInitializeLoadOption (\r
2339 &BootOptions[(*BootOptionCount)++],\r
2340 LoadOptionNumberUnassigned,\r
2341 LoadOptionTypeBoot,\r
2342 LOAD_OPTION_ACTIVE,\r
2343 Description,\r
2344 DevicePathFromHandle (Handles[Index]),\r
2345 NULL,\r
2346 0\r
2347 );\r
2348 ASSERT_EFI_ERROR (Status);\r
f41c71d2 2349 FreePool (Description);\r
067ed98a
RN
2350 }\r
2351\r
2352 if (HandleCount != 0) {\r
2353 FreePool (Handles);\r
2354 }\r
2355\r
d948fe96 2356 BmMakeBootOptionDescriptionUnique (BootOptions, *BootOptionCount);\r
067ed98a
RN
2357 return BootOptions;\r
2358}\r
2359\r
2360/**\r
2361 The function enumerates all boot options, creates them and registers them in the BootOrder variable.\r
2362**/\r
2363VOID\r
2364EFIAPI\r
2365EfiBootManagerRefreshAllBootOption (\r
2366 VOID\r
2367 )\r
2368{\r
2369 EFI_STATUS Status;\r
2370 EFI_BOOT_MANAGER_LOAD_OPTION *NvBootOptions;\r
2371 UINTN NvBootOptionCount;\r
2372 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r
2373 UINTN BootOptionCount;\r
2374 UINTN Index;\r
2375\r
2376 //\r
2377 // Optionally refresh the legacy boot option\r
2378 //\r
2379 if (mBmRefreshLegacyBootOption != NULL) {\r
2380 mBmRefreshLegacyBootOption ();\r
2381 }\r
2382\r
2383 BootOptions = BmEnumerateBootOptions (&BootOptionCount);\r
2384 NvBootOptions = EfiBootManagerGetLoadOptions (&NvBootOptionCount, LoadOptionTypeBoot);\r
2385\r
2386 //\r
2387 // Mark the boot option as added by BDS by setting OptionalData to a special GUID\r
2388 //\r
2389 for (Index = 0; Index < BootOptionCount; Index++) {\r
2390 BootOptions[Index].OptionalData = AllocateCopyPool (sizeof (EFI_GUID), &mBmAutoCreateBootOptionGuid);\r
2391 BootOptions[Index].OptionalDataSize = sizeof (EFI_GUID);\r
2392 }\r
2393\r
2394 //\r
2395 // Remove invalid EFI boot options from NV\r
2396 //\r
2397 for (Index = 0; Index < NvBootOptionCount; Index++) {\r
2398 if (((DevicePathType (NvBootOptions[Index].FilePath) != BBS_DEVICE_PATH) || \r
2399 (DevicePathSubType (NvBootOptions[Index].FilePath) != BBS_BBS_DP)\r
404bd442 2400 ) && BmIsAutoCreateBootOption (&NvBootOptions[Index])\r
067ed98a
RN
2401 ) {\r
2402 //\r
2403 // Only check those added by BDS\r
2404 // so that the boot options added by end-user or OS installer won't be deleted\r
2405 //\r
5d3a9896 2406 if (EfiBootManagerFindLoadOption (&NvBootOptions[Index], BootOptions, BootOptionCount) == (UINTN) -1) {\r
067ed98a
RN
2407 Status = EfiBootManagerDeleteLoadOptionVariable (NvBootOptions[Index].OptionNumber, LoadOptionTypeBoot);\r
2408 //\r
2409 // Deleting variable with current variable implementation shouldn't fail.\r
2410 //\r
2411 ASSERT_EFI_ERROR (Status);\r
2412 }\r
2413 }\r
2414 }\r
2415\r
2416 //\r
2417 // Add new EFI boot options to NV\r
2418 //\r
2419 for (Index = 0; Index < BootOptionCount; Index++) {\r
5d3a9896 2420 if (EfiBootManagerFindLoadOption (&BootOptions[Index], NvBootOptions, NvBootOptionCount) == (UINTN) -1) {\r
067ed98a
RN
2421 EfiBootManagerAddLoadOptionVariable (&BootOptions[Index], (UINTN) -1);\r
2422 //\r
2423 // Try best to add the boot options so continue upon failure.\r
2424 //\r
2425 }\r
2426 }\r
2427\r
2428 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
2429 EfiBootManagerFreeLoadOptions (NvBootOptions, NvBootOptionCount);\r
2430}\r
2431\r
2432/**\r
2433 This function is called to create the boot option for the Boot Manager Menu.\r
2434\r
2435 The Boot Manager Menu is shown after successfully booting a boot option.\r
2436 Assume the BootManagerMenuFile is in the same FV as the module links to this library.\r
2437\r
2438 @param BootOption Return the boot option of the Boot Manager Menu\r
2439\r
2440 @retval EFI_SUCCESS Successfully register the Boot Manager Menu.\r
2441 @retval Status Return status of gRT->SetVariable (). BootOption still points\r
2442 to the Boot Manager Menu even the Status is not EFI_SUCCESS.\r
2443**/\r
2444EFI_STATUS\r
2445BmRegisterBootManagerMenu (\r
2446 OUT EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r
2447 )\r
2448{\r
2449 EFI_STATUS Status;\r
2450 CHAR16 *Description;\r
2451 UINTN DescriptionLength;\r
2452 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2453 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
2454 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FileNode;\r
2455\r
2456 Status = GetSectionFromFv (\r
2457 PcdGetPtr (PcdBootManagerMenuFile),\r
2458 EFI_SECTION_USER_INTERFACE,\r
2459 0,\r
2460 (VOID **) &Description,\r
2461 &DescriptionLength\r
2462 );\r
2463 if (EFI_ERROR (Status)) {\r
2464 Description = NULL;\r
2465 }\r
2466\r
2467 EfiInitializeFwVolDevicepathNode (&FileNode, PcdGetPtr (PcdBootManagerMenuFile));\r
2468 Status = gBS->HandleProtocol (\r
2469 gImageHandle,\r
2470 &gEfiLoadedImageProtocolGuid,\r
2471 (VOID **) &LoadedImage\r
2472 );\r
2473 ASSERT_EFI_ERROR (Status);\r
2474 DevicePath = AppendDevicePathNode (\r
2475 DevicePathFromHandle (LoadedImage->DeviceHandle),\r
2476 (EFI_DEVICE_PATH_PROTOCOL *) &FileNode\r
2477 );\r
2478 ASSERT (DevicePath != NULL);\r
2479\r
2480 Status = EfiBootManagerInitializeLoadOption (\r
2481 BootOption,\r
2482 LoadOptionNumberUnassigned,\r
2483 LoadOptionTypeBoot,\r
2484 LOAD_OPTION_CATEGORY_APP | LOAD_OPTION_ACTIVE | LOAD_OPTION_HIDDEN,\r
2485 (Description != NULL) ? Description : L"Boot Manager Menu",\r
2486 DevicePath,\r
2487 NULL,\r
2488 0\r
2489 );\r
2490 ASSERT_EFI_ERROR (Status);\r
2491 FreePool (DevicePath);\r
2492 if (Description != NULL) {\r
2493 FreePool (Description);\r
2494 }\r
2495\r
2496 DEBUG_CODE (\r
2497 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r
2498 UINTN BootOptionCount;\r
2499\r
2500 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r
5d3a9896 2501 ASSERT (EfiBootManagerFindLoadOption (BootOption, BootOptions, BootOptionCount) == -1);\r
067ed98a
RN
2502 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
2503 );\r
2504\r
2505 return EfiBootManagerAddLoadOptionVariable (BootOption, 0);\r
2506}\r
2507\r
2508/**\r
2509 Return the boot option corresponding to the Boot Manager Menu.\r
2510 It may automatically create one if the boot option hasn't been created yet.\r
2511 \r
2512 @param BootOption Return the Boot Manager Menu.\r
2513\r
2514 @retval EFI_SUCCESS The Boot Manager Menu is successfully returned.\r
2515 @retval Status Return status of gRT->SetVariable (). BootOption still points\r
2516 to the Boot Manager Menu even the Status is not EFI_SUCCESS.\r
2517**/\r
2518EFI_STATUS\r
2519EFIAPI\r
2520EfiBootManagerGetBootManagerMenu (\r
2521 EFI_BOOT_MANAGER_LOAD_OPTION *BootOption\r
2522 )\r
2523{\r
2524 EFI_STATUS Status;\r
2525 UINTN BootOptionCount;\r
2526 EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions;\r
2527 UINTN Index;\r
2528 EFI_DEVICE_PATH_PROTOCOL *Node;\r
2529 EFI_HANDLE FvHandle;\r
2530 \r
2531 BootOptions = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);\r
2532\r
2533 for (Index = 0; Index < BootOptionCount; Index++) {\r
2534 Node = BootOptions[Index].FilePath;\r
2535 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Node, &FvHandle);\r
2536 if (!EFI_ERROR (Status)) {\r
2537 if (CompareGuid (\r
2538 EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) Node),\r
2539 PcdGetPtr (PcdBootManagerMenuFile)\r
2540 )\r
2541 ) { \r
2542 Status = EfiBootManagerInitializeLoadOption (\r
2543 BootOption,\r
2544 BootOptions[Index].OptionNumber,\r
2545 BootOptions[Index].OptionType,\r
2546 BootOptions[Index].Attributes,\r
2547 BootOptions[Index].Description,\r
2548 BootOptions[Index].FilePath,\r
2549 BootOptions[Index].OptionalData,\r
2550 BootOptions[Index].OptionalDataSize\r
2551 );\r
2552 ASSERT_EFI_ERROR (Status);\r
2553 break;\r
2554 }\r
2555 }\r
2556 }\r
2557\r
2558 EfiBootManagerFreeLoadOptions (BootOptions, BootOptionCount);\r
2559\r
2560 //\r
2561 // Automatically create the Boot#### for Boot Manager Menu when not found.\r
2562 //\r
2563 if (Index == BootOptionCount) {\r
2564 return BmRegisterBootManagerMenu (BootOption);\r
2565 } else {\r
2566 return EFI_SUCCESS;\r
2567 }\r
2568}\r
2569\r