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