]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
BeagleBoardPkg: Fix build
[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
1433 FloppyNumber = 0;\r
1434 CdromNumber = 0;\r
1435 UsbNumber = 0;\r
1436 MiscNumber = 0;\r
8d3b5aff 1437 ScsiNumber = 0;\r
9aa7ba01 1438 PlatLang = NULL;\r
1439 LastLang = NULL;\r
5c08e117 1440 ZeroMem (Buffer, sizeof (Buffer));\r
128efbbc 1441\r
5c08e117 1442 //\r
1443 // If the boot device enumerate happened, just get the boot\r
1444 // device from the boot order variable\r
1445 //\r
1446 if (mEnumBootDevice) {\r
9aa7ba01 1447 LastLang = GetVariable (L"LastEnumLang", &mBdsLibLastLangGuid);\r
1448 PlatLang = GetEfiGlobalVariable (L"PlatformLang");\r
0fa3ac1b
RN
1449 ASSERT (PlatLang != NULL);\r
1450 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {\r
9aa7ba01 1451 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
0fa3ac1b
RN
1452 FreePool (LastLang);\r
1453 FreePool (PlatLang);\r
9aa7ba01 1454 return Status;\r
1455 } else {\r
1456 Status = gRT->SetVariable (\r
1457 L"LastEnumLang",\r
1458 &mBdsLibLastLangGuid,\r
1459 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
0fa3ac1b 1460 AsciiStrSize (PlatLang),\r
9aa7ba01 1461 PlatLang\r
1462 );\r
1463 ASSERT_EFI_ERROR (Status);\r
0fa3ac1b
RN
1464\r
1465 if (LastLang != NULL) {\r
1466 FreePool (LastLang);\r
1467 }\r
1468 FreePool (PlatLang);\r
9aa7ba01 1469 }\r
5c08e117 1470 }\r
128efbbc 1471\r
5c08e117 1472 //\r
1473 // Notes: this dirty code is to get the legacy boot option from the\r
1474 // BBS table and create to variable as the EFI boot option, it should\r
1475 // be removed after the CSM can provide legacy boot option directly\r
1476 //\r
1477 REFRESH_LEGACY_BOOT_OPTIONS;\r
1478\r
1479 //\r
1480 // Delete invalid boot option\r
1481 //\r
1482 BdsDeleteAllInvalidEfiBootOption ();\r
128efbbc 1483\r
5c08e117 1484 //\r
701e17e5
RN
1485 // Parse removable media followed by fixed media.\r
1486 // The Removable[] array is used by the for-loop below to create removable media boot options \r
1487 // at first, and then to create fixed media boot options.\r
5c08e117 1488 //\r
701e17e5
RN
1489 Removable[0] = FALSE;\r
1490 Removable[1] = TRUE;\r
1491\r
5c08e117 1492 gBS->LocateHandleBuffer (\r
1493 ByProtocol,\r
1494 &gEfiBlockIoProtocolGuid,\r
1495 NULL,\r
1496 &NumberBlockIoHandles,\r
1497 &BlockIoHandles\r
1498 );\r
128efbbc 1499\r
35bc0e9f
RN
1500 for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {\r
1501 for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
1502 Status = gBS->HandleProtocol (\r
1503 BlockIoHandles[Index],\r
1504 &gEfiBlockIoProtocolGuid,\r
1505 (VOID **) &BlkIo\r
1506 );\r
1507 //\r
1508 // skip the fixed block io then the removable block io\r
1509 //\r
1510 if (EFI_ERROR (Status) || (BlkIo->Media->RemovableMedia == Removable[RemovableIndex])) {\r
5c08e117 1511 continue;\r
1512 }\r
35bc0e9f
RN
1513 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r
1514 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
5c08e117 1515\r
35bc0e9f
RN
1516 switch (DevicePathType) {\r
1517 case BDS_EFI_ACPI_FLOPPY_BOOT:\r
1518 if (FloppyNumber != 0) {\r
1519 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
1520 } else {\r
1521 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
1522 }\r
1523 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1524 FloppyNumber++;\r
1525 break;\r
128efbbc 1526\r
35bc0e9f
RN
1527 //\r
1528 // Assume a removable SATA device should be the DVD/CD device\r
1529 //\r
1530 case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
1531 case BDS_EFI_MESSAGE_SATA_BOOT:\r
1532 if (CdromNumber != 0) {\r
1533 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
1534 } else {\r
1535 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
1536 }\r
1537 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
1538 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1539 CdromNumber++;\r
1540 break;\r
1541\r
1542 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
1543 if (UsbNumber != 0) {\r
1544 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
1545 } else {\r
1546 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
1547 }\r
1548 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1549 UsbNumber++;\r
1550 break;\r
5c08e117 1551\r
35bc0e9f
RN
1552 case BDS_EFI_MESSAGE_SCSI_BOOT:\r
1553 if (ScsiNumber != 0) {\r
1554 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
1555 } else {\r
1556 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
1557 }\r
1558 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1559 ScsiNumber++;\r
1560 break;\r
5c08e117 1561\r
35bc0e9f
RN
1562 case BDS_EFI_MESSAGE_MISC_BOOT:\r
1563 if (MiscNumber != 0) {\r
1564 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
1565 } else {\r
1566 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
1567 }\r
1568 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1569 MiscNumber++;\r
1570 break;\r
5c08e117 1571\r
35bc0e9f
RN
1572 default:\r
1573 break;\r
9aa7ba01 1574 }\r
5c08e117 1575 }\r
1576 }\r
1577\r
1578 if (NumberBlockIoHandles != 0) {\r
1579 FreePool (BlockIoHandles);\r
1580 }\r
1581\r
1582 //\r
1583 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
1584 //\r
1585 NonBlockNumber = 0;\r
1586 gBS->LocateHandleBuffer (\r
1587 ByProtocol,\r
1588 &gEfiSimpleFileSystemProtocolGuid,\r
1589 NULL,\r
1590 &NumberFileSystemHandles,\r
1591 &FileSystemHandles\r
1592 );\r
1593 for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
1594 Status = gBS->HandleProtocol (\r
1595 FileSystemHandles[Index],\r
1596 &gEfiBlockIoProtocolGuid,\r
1597 (VOID **) &BlkIo\r
1598 );\r
1599 if (!EFI_ERROR (Status)) {\r
1600 //\r
1601 // Skip if the file system handle supports a BlkIo protocol,\r
1602 //\r
1603 continue;\r
1604 }\r
1605\r
1606 //\r
1607 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
1608 // machinename is ia32, ia64, x64, ...\r
1609 //\r
35bc0e9f 1610 Hdr.Union = &HdrData;\r
5c08e117 1611 NeedDelete = TRUE;\r
1612 Status = BdsLibGetImageHeader (\r
1613 FileSystemHandles[Index],\r
c62dbf31 1614 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1615 &DosHeader,\r
1616 Hdr\r
1617 );\r
1618 if (!EFI_ERROR (Status) &&\r
1619 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1620 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1621 NeedDelete = FALSE;\r
1622 }\r
1623\r
1624 if (NeedDelete) {\r
1625 //\r
1626 // No such file or the file is not a EFI application, delete this boot option\r
1627 //\r
1628 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
1629 } else {\r
9aa7ba01 1630 if (NonBlockNumber != 0) {\r
1631 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
1632 } else {\r
1633 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
1634 }\r
5c08e117 1635 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
1636 NonBlockNumber++;\r
1637 }\r
1638 }\r
1639\r
1640 if (NumberFileSystemHandles != 0) {\r
1641 FreePool (FileSystemHandles);\r
1642 }\r
1643\r
1644 //\r
1645 // Parse Network Boot Device\r
1646 //\r
a7a523e0 1647 NumOfLoadFileHandles = 0;\r
ff482c56 1648 //\r
a7a523e0 1649 // Search Load File protocol for PXE boot option.\r
ff482c56 1650 //\r
5c08e117 1651 gBS->LocateHandleBuffer (\r
1652 ByProtocol,\r
a7a523e0 1653 &gEfiLoadFileProtocolGuid,\r
5c08e117 1654 NULL,\r
a7a523e0 1655 &NumOfLoadFileHandles,\r
1656 &LoadFileHandles\r
5c08e117 1657 );\r
8d3b5aff 1658\r
a7a523e0 1659 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
9aa7ba01 1660 if (Index != 0) {\r
1661 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r
1662 } else {\r
1663 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r
1664 }\r
a7a523e0 1665 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 1666 }\r
1667\r
a7a523e0 1668 if (NumOfLoadFileHandles != 0) {\r
1669 FreePool (LoadFileHandles);\r
5c08e117 1670 }\r
1671\r
1672 //\r
1673 // Check if we have on flash shell\r
1674 //\r
1675 gBS->LocateHandleBuffer (\r
1676 ByProtocol,\r
1677 &gEfiFirmwareVolume2ProtocolGuid,\r
1678 NULL,\r
1679 &FvHandleCount,\r
1680 &FvHandleBuffer\r
1681 );\r
1682 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 1683 gBS->HandleProtocol (\r
1684 FvHandleBuffer[Index],\r
1685 &gEfiFirmwareVolume2ProtocolGuid,\r
1686 (VOID **) &Fv\r
1687 );\r
1688\r
1689 Status = Fv->ReadFile (\r
1690 Fv,\r
d46f3632 1691 PcdGetPtr(PcdShellFile),\r
5c08e117 1692 NULL,\r
1693 &Size,\r
1694 &Type,\r
1695 &Attributes,\r
1696 &AuthenticationStatus\r
1697 );\r
1698 if (EFI_ERROR (Status)) {\r
1699 //\r
1700 // Skip if no shell file in the FV\r
1701 //\r
1702 continue;\r
1703 }\r
1704 //\r
1705 // Build the shell boot option\r
1706 //\r
1707 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
1708 }\r
1709\r
1710 if (FvHandleCount != 0) {\r
1711 FreePool (FvHandleBuffer);\r
1712 }\r
1713 //\r
1714 // Make sure every boot only have one time\r
1715 // boot device enumerate\r
1716 //\r
e83c9064 1717 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 1718 mEnumBootDevice = TRUE;\r
1719\r
e83c9064 1720 return Status;\r
5c08e117 1721}\r
1722\r
1723/**\r
1724 Build the boot option with the handle parsed in\r
1725\r
1726 @param Handle The handle which present the device path to create\r
1727 boot option\r
1728 @param BdsBootOptionList The header of the link list which indexed all\r
1729 current boot options\r
1730 @param String The description of the boot option.\r
1731\r
1732**/\r
1733VOID\r
1734EFIAPI\r
1735BdsLibBuildOptionFromHandle (\r
1736 IN EFI_HANDLE Handle,\r
1737 IN LIST_ENTRY *BdsBootOptionList,\r
1738 IN CHAR16 *String\r
1739 )\r
1740{\r
1741 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 1742\r
8d3b5aff 1743 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 1744\r
1745 //\r
1746 // Create and register new boot option\r
1747 //\r
1748 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
1749}\r
1750\r
1751\r
1752/**\r
1753 Build the on flash shell boot option with the handle parsed in.\r
1754\r
1755 @param Handle The handle which present the device path to create\r
1756 on flash shell boot option\r
1757 @param BdsBootOptionList The header of the link list which indexed all\r
1758 current boot options\r
1759\r
1760**/\r
1761VOID\r
1762EFIAPI\r
1763BdsLibBuildOptionFromShell (\r
1764 IN EFI_HANDLE Handle,\r
1765 IN OUT LIST_ENTRY *BdsBootOptionList\r
1766 )\r
1767{\r
1768 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1769 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
1770\r
1771 DevicePath = DevicePathFromHandle (Handle);\r
1772\r
1773 //\r
1774 // Build the shell device path\r
1775 //\r
d46f3632 1776 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 1777\r
1778 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
1779\r
1780 //\r
1781 // Create and register the shell boot option\r
1782 //\r
1783 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
1784\r
1785}\r
1786\r
1787/**\r
1788 Boot from the UEFI spec defined "BootNext" variable.\r
1789\r
1790**/\r
1791VOID\r
1792EFIAPI\r
1793BdsLibBootNext (\r
1794 VOID\r
1795 )\r
1796{\r
1797 UINT16 *BootNext;\r
1798 UINTN BootNextSize;\r
1799 CHAR16 Buffer[20];\r
1800 BDS_COMMON_OPTION *BootOption;\r
1801 LIST_ENTRY TempList;\r
1802 UINTN ExitDataSize;\r
1803 CHAR16 *ExitData;\r
1804\r
1805 //\r
1806 // Init the boot option name buffer and temp link list\r
1807 //\r
1808 InitializeListHead (&TempList);\r
1809 ZeroMem (Buffer, sizeof (Buffer));\r
1810\r
1811 BootNext = BdsLibGetVariableAndSize (\r
1812 L"BootNext",\r
1813 &gEfiGlobalVariableGuid,\r
1814 &BootNextSize\r
1815 );\r
1816\r
1817 //\r
1818 // Clear the boot next variable first\r
1819 //\r
1820 if (BootNext != NULL) {\r
1821 gRT->SetVariable (\r
1822 L"BootNext",\r
1823 &gEfiGlobalVariableGuid,\r
1824 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1825 0,\r
1826 BootNext\r
1827 );\r
1828\r
1829 //\r
1830 // Start to build the boot option and try to boot\r
1831 //\r
1832 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
1833 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
1834 ASSERT (BootOption != NULL);\r
1835 BdsLibConnectDevicePath (BootOption->DevicePath);\r
1836 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
1837 }\r
1838\r
1839}\r
1840\r
1841/**\r
1842 Return the bootable media handle.\r
1843 First, check the device is connected\r
1844 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
1845 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
1846\r
e83c9064 1847 @param DevicePath Device Path to a bootable device\r
5c08e117 1848\r
e83c9064 1849 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 1850\r
1851**/\r
1852EFI_HANDLE\r
1853EFIAPI\r
1854BdsLibGetBootableHandle (\r
1855 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1856 )\r
1857{\r
1858 EFI_STATUS Status;\r
1859 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1860 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
1861 EFI_HANDLE Handle;\r
1862 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1863 VOID *Buffer;\r
1864 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1865 UINTN Size;\r
1866 UINTN TempSize;\r
1867 EFI_HANDLE ReturnHandle;\r
1868 EFI_HANDLE *SimpleFileSystemHandles;\r
1869\r
1870 UINTN NumberSimpleFileSystemHandles;\r
1871 UINTN Index;\r
1872 EFI_IMAGE_DOS_HEADER DosHeader;\r
1873 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
1874 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1875\r
1876 UpdatedDevicePath = DevicePath;\r
128efbbc 1877\r
5c08e117 1878 //\r
1879 // Check whether the device is connected\r
1880 //\r
1881 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
1882 if (EFI_ERROR (Status)) {\r
1883 //\r
1884 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
1885 //\r
1886 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
1887 if (EFI_ERROR (Status)) {\r
1888 //\r
1889 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
1890 //\r
1891 UpdatedDevicePath = DevicePath;\r
1892 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1893 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
1894 }\r
1895 } else {\r
e74f510b
RN
1896 //\r
1897 // For removable device boot option, its contained device path only point to the removable device handle, \r
1898 // should make sure all its children handles (its child partion or media handles) are created and connected. \r
1899 //\r
1900 gBS->ConnectController (Handle, NULL, NULL, TRUE); \r
5c08e117 1901 //\r
1902 // Get BlockIo protocol and check removable attribute\r
1903 //\r
1904 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1905 //\r
1906 // Issue a dummy read to the device to check for media change.\r
1907 // When the removable media is changed, any Block IO read/write will\r
1908 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
1909 // returned. After the Block IO protocol is reinstalled, subsequent\r
1910 // Block IO read/write will success.\r
1911 //\r
1912 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
1913 if (Buffer != NULL) {\r
1914 BlockIo->ReadBlocks (\r
1915 BlockIo,\r
1916 BlockIo->Media->MediaId,\r
1917 0,\r
1918 BlockIo->Media->BlockSize,\r
1919 Buffer\r
1920 );\r
1921 FreePool(Buffer);\r
1922 }\r
1923 }\r
1924\r
1925 //\r
1926 // Detect the the default boot file from removable Media\r
1927 //\r
1928\r
1929 //\r
1930 // 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
1931 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
1932 //\r
1933 DupDevicePath = DuplicateDevicePath (DevicePath);\r
1934 ASSERT (DupDevicePath != NULL);\r
128efbbc 1935\r
5c08e117 1936 UpdatedDevicePath = DupDevicePath;\r
1937 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1938 //\r
1939 // 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
1940 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
1941 //\r
1942 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1943 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
1944 //\r
1945 // Remove the usb node, let the device path only point to PCI node\r
1946 //\r
1947 SetDevicePathEndNode (UpdatedDevicePath);\r
1948 UpdatedDevicePath = DupDevicePath;\r
1949 } else {\r
1950 UpdatedDevicePath = DevicePath;\r
1951 }\r
1952\r
1953 //\r
1954 // Get the device path size of boot option\r
1955 //\r
1956 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1957 ReturnHandle = NULL;\r
1958 gBS->LocateHandleBuffer (\r
1959 ByProtocol,\r
1960 &gEfiSimpleFileSystemProtocolGuid,\r
1961 NULL,\r
1962 &NumberSimpleFileSystemHandles,\r
1963 &SimpleFileSystemHandles\r
1964 );\r
1965 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
1966 //\r
1967 // Get the device path size of SimpleFileSystem handle\r
1968 //\r
1969 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
1970 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1971 //\r
1972 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
1973 //\r
1974 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
1975 //\r
1976 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
1977 // machinename is ia32, ia64, x64, ...\r
1978 //\r
1979 Hdr.Union = &HdrData;\r
1980 Status = BdsLibGetImageHeader (\r
1981 SimpleFileSystemHandles[Index],\r
c62dbf31 1982 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1983 &DosHeader,\r
1984 Hdr\r
1985 );\r
1986 if (!EFI_ERROR (Status) &&\r
1987 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1988 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1989 ReturnHandle = SimpleFileSystemHandles[Index];\r
1990 break;\r
1991 }\r
1992 }\r
1993 }\r
1994\r
1995 FreePool(DupDevicePath);\r
1996\r
1997 if (SimpleFileSystemHandles != NULL) {\r
1998 FreePool(SimpleFileSystemHandles);\r
1999 }\r
2000\r
2001 return ReturnHandle;\r
2002}\r
2003\r
2004/**\r
2005 Check to see if the network cable is plugged in. If the DevicePath is not\r
2006 connected it will be connected.\r
2007\r
2008 @param DevicePath Device Path to check\r
2009\r
2010 @retval TRUE DevicePath points to an Network that is connected\r
2011 @retval FALSE DevicePath does not point to a bootable network\r
2012\r
2013**/\r
2014BOOLEAN\r
2015BdsLibNetworkBootWithMediaPresent (\r
2016 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
2017 )\r
2018{\r
2019 EFI_STATUS Status;\r
2020 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
2021 EFI_HANDLE Handle;\r
2022 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
2023 BOOLEAN MediaPresent;\r
e51e619e 2024 UINT32 InterruptStatus;\r
5c08e117 2025\r
2026 MediaPresent = FALSE;\r
2027\r
2028 UpdatedDevicePath = DevicePath;\r
ff482c56 2029 //\r
a7a523e0 2030 // Locate Load File Protocol for PXE boot option first\r
ff482c56 2031 //\r
a7a523e0 2032 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 2033 if (EFI_ERROR (Status)) {\r
2034 //\r
2035 // Device not present so see if we need to connect it\r
2036 //\r
2037 Status = BdsLibConnectDevicePath (DevicePath);\r
2038 if (!EFI_ERROR (Status)) {\r
2039 //\r
2040 // This one should work after we did the connect\r
2041 //\r
a7a523e0 2042 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 2043 }\r
2044 }\r
2045\r
2046 if (!EFI_ERROR (Status)) {\r
2047 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
ff482c56 2048 if (EFI_ERROR (Status)) {\r
2049 //\r
2050 // Failed to open SNP from this handle, try to get SNP from parent handle\r
2051 //\r
2052 UpdatedDevicePath = DevicePathFromHandle (Handle);\r
2053 if (UpdatedDevicePath != NULL) {\r
2054 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
2055 if (!EFI_ERROR (Status)) {\r
2056 //\r
2057 // SNP handle found, get SNP from it\r
2058 //\r
2059 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r
2060 }\r
2061 }\r
2062 }\r
2063\r
5c08e117 2064 if (!EFI_ERROR (Status)) {\r
2065 if (Snp->Mode->MediaPresentSupported) {\r
2066 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
e51e619e 2067 //\r
2068 // Invoke Snp->GetStatus() to refresh the media status\r
2069 //\r
2070 Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
2071\r
5c08e117 2072 //\r
2073 // In case some one else is using the SNP check to see if it's connected\r
2074 //\r
2075 MediaPresent = Snp->Mode->MediaPresent;\r
2076 } else {\r
2077 //\r
2078 // No one is using SNP so we need to Start and Initialize so\r
2079 // MediaPresent will be valid.\r
2080 //\r
2081 Status = Snp->Start (Snp);\r
2082 if (!EFI_ERROR (Status)) {\r
2083 Status = Snp->Initialize (Snp, 0, 0);\r
2084 if (!EFI_ERROR (Status)) {\r
2085 MediaPresent = Snp->Mode->MediaPresent;\r
2086 Snp->Shutdown (Snp);\r
2087 }\r
2088 Snp->Stop (Snp);\r
2089 }\r
2090 }\r
2091 } else {\r
2092 MediaPresent = TRUE;\r
2093 }\r
2094 }\r
2095 }\r
2096\r
2097 return MediaPresent;\r
2098}\r
2099\r
2100/**\r
2101 For a bootable Device path, return its boot type.\r
2102\r
2103 @param DevicePath The bootable device Path to check\r
2104\r
128efbbc 2105 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 2106 which subtype is MEDIA_HARDDRIVE_DP\r
2107 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
2108 which subtype is MEDIA_CDROM_DP\r
2109 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
2110 which HID is floppy device.\r
2111 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
2112 and its last device path node's subtype is MSG_ATAPI_DP.\r
2113 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
2114 and its last device path node's subtype is MSG_SCSI_DP.\r
2115 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
2116 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 2117 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 2118 its last device path node point to a message device path node.\r
2119 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 2120 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 2121\r
2122**/\r
2123UINT32\r
2124EFIAPI\r
2125BdsGetBootTypeFromDevicePath (\r
2126 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
2127 )\r
2128{\r
2129 ACPI_HID_DEVICE_PATH *Acpi;\r
2130 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
2131 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
ff482c56 2132 UINT32 BootType;\r
5c08e117 2133\r
2134 if (NULL == DevicePath) {\r
2135 return BDS_EFI_UNSUPPORT;\r
2136 }\r
2137\r
2138 TempDevicePath = DevicePath;\r
2139\r
2140 while (!IsDevicePathEndType (TempDevicePath)) {\r
2141 switch (DevicePathType (TempDevicePath)) {\r
2142 case BBS_DEVICE_PATH:\r
2143 return BDS_LEGACY_BBS_BOOT;\r
2144 case MEDIA_DEVICE_PATH:\r
2145 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
2146 return BDS_EFI_MEDIA_HD_BOOT;\r
2147 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
2148 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 2149 }\r
5c08e117 2150 break;\r
2151 case ACPI_DEVICE_PATH:\r
2152 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
2153 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
2154 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
2155 }\r
2156 break;\r
2157 case MESSAGING_DEVICE_PATH:\r
2158 //\r
2159 // Get the last device path node\r
2160 //\r
2161 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
2162 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
2163 //\r
2164 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
ff482c56 2165 // skip it\r
5c08e117 2166 //\r
2167 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
2168 }\r
2169 //\r
2170 // if the device path not only point to driver device, it is not a messaging device path,\r
2171 //\r
2172 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 2173 break;\r
5c08e117 2174 }\r
2175\r
ff482c56 2176 switch (DevicePathSubType (TempDevicePath)) {\r
2177 case MSG_ATAPI_DP:\r
2178 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r
2179 break;\r
2180\r
2181 case MSG_USB_DP:\r
2182 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
2183 break;\r
2184\r
2185 case MSG_SCSI_DP:\r
2186 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r
2187 break;\r
2188\r
2189 case MSG_SATA_DP:\r
2190 BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r
2191 break;\r
2192\r
2193 case MSG_MAC_ADDR_DP:\r
2194 case MSG_VLAN_DP:\r
a7a523e0 2195 case MSG_IPv4_DP:\r
2196 case MSG_IPv6_DP:\r
ff482c56 2197 BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r
2198 break;\r
2199\r
2200 default:\r
2201 BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r
2202 break;\r
5c08e117 2203 }\r
ff482c56 2204 return BootType;\r
2205\r
5c08e117 2206 default:\r
2207 break;\r
2208 }\r
2209 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
2210 }\r
2211\r
2212 return BDS_EFI_UNSUPPORT;\r
2213}\r
2214\r
2215/**\r
2216 Check whether the Device path in a boot option point to a valid bootable device,\r
2217 And if CheckMedia is true, check the device is ready to boot now.\r
2218\r
2219 @param DevPath the Device path in a boot option\r
2220 @param CheckMedia if true, check the device is ready to boot now.\r
2221\r
2222 @retval TRUE the Device path is valid\r
2223 @retval FALSE the Device path is invalid .\r
2224\r
2225**/\r
2226BOOLEAN\r
2227EFIAPI\r
2228BdsLibIsValidEFIBootOptDevicePath (\r
2229 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
2230 IN BOOLEAN CheckMedia\r
2231 )\r
3384a9bc 2232{\r
2233 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
2234}\r
2235\r
2236/**\r
2237 Check whether the Device path in a boot option point to a valid bootable device,\r
2238 And if CheckMedia is true, check the device is ready to boot now.\r
2239 If Description is not NULL and the device path point to a fixed BlockIo\r
2240 device, check the description whether conflict with other auto-created\r
2241 boot options.\r
2242\r
2243 @param DevPath the Device path in a boot option\r
2244 @param CheckMedia if true, check the device is ready to boot now.\r
2245 @param Description the description in a boot option\r
2246\r
2247 @retval TRUE the Device path is valid\r
2248 @retval FALSE the Device path is invalid .\r
2249\r
2250**/\r
2251BOOLEAN\r
2252EFIAPI\r
2253BdsLibIsValidEFIBootOptDevicePathExt (\r
2254 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
2255 IN BOOLEAN CheckMedia,\r
2256 IN CHAR16 *Description\r
2257 )\r
5c08e117 2258{\r
2259 EFI_STATUS Status;\r
2260 EFI_HANDLE Handle;\r
2261 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
2262 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
2263 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
2264\r
2265 TempDevicePath = DevPath;\r
2266 LastDeviceNode = DevPath;\r
128efbbc 2267\r
5c08e117 2268 //\r
a7a523e0 2269 // Check if it's a valid boot option for network boot device.\r
2270 // Check if there is EfiLoadFileProtocol installed. \r
2271 // If yes, that means there is a boot option for network.\r
5c08e117 2272 //\r
2273 Status = gBS->LocateDevicePath (\r
a7a523e0 2274 &gEfiLoadFileProtocolGuid,\r
5c08e117 2275 &TempDevicePath,\r
2276 &Handle\r
2277 );\r
2278 if (EFI_ERROR (Status)) {\r
2279 //\r
2280 // Device not present so see if we need to connect it\r
2281 //\r
2282 TempDevicePath = DevPath;\r
2283 BdsLibConnectDevicePath (TempDevicePath);\r
2284 Status = gBS->LocateDevicePath (\r
a7a523e0 2285 &gEfiLoadFileProtocolGuid,\r
5c08e117 2286 &TempDevicePath,\r
2287 &Handle\r
2288 );\r
2289 }\r
128efbbc 2290\r
5c08e117 2291 if (!EFI_ERROR (Status)) {\r
a7a523e0 2292 if (!IsDevicePathEnd (TempDevicePath)) {\r
2293 //\r
2294 // LoadFile protocol is not installed on handle with exactly the same DevPath\r
2295 //\r
2296 return FALSE;\r
2297 }\r
ff482c56 2298\r
a7a523e0 2299 if (CheckMedia) {\r
2300 //\r
2301 // Test if it is ready to boot now\r
2302 //\r
2303 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
5c08e117 2304 return TRUE;\r
2305 }\r
a7a523e0 2306 } else {\r
2307 return TRUE;\r
2308 } \r
5c08e117 2309 }\r
2310\r
2311 //\r
2312 // If the boot option point to a file, it is a valid EFI boot option,\r
2313 // and assume it is ready to boot now\r
2314 //\r
2315 while (!IsDevicePathEnd (TempDevicePath)) {\r
7389fdd0 2316 //\r
2317 // If there is USB Class or USB WWID device path node, treat it as valid EFI\r
2318 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it\r
2319 // to full device path.\r
2320 //\r
2321 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
2322 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||\r
2323 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {\r
2324 return TRUE;\r
2325 }\r
2326\r
2327 LastDeviceNode = TempDevicePath;\r
2328 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
5c08e117 2329 }\r
2330 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
2331 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
2332 return TRUE;\r
2333 }\r
2334\r
2335 //\r
6617d838 2336 // Check if it's a valid boot option for internal FV application\r
5c08e117 2337 //\r
2338 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
2339 //\r
6617d838 2340 // If the boot option point to internal FV application, make sure it is valid\r
5c08e117 2341 //\r
128efbbc 2342 TempDevicePath = DevPath;\r
6617d838
RN
2343 Status = BdsLibUpdateFvFileDevicePath (\r
2344 &TempDevicePath,\r
2345 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)\r
2346 );\r
5c08e117 2347 if (Status == EFI_ALREADY_STARTED) {\r
2348 return TRUE;\r
2349 } else {\r
2350 if (Status == EFI_SUCCESS) {\r
128efbbc 2351 FreePool (TempDevicePath);\r
5c08e117 2352 }\r
2353 return FALSE;\r
2354 }\r
2355 }\r
128efbbc 2356\r
5c08e117 2357 //\r
3384a9bc 2358 // If the boot option point to a blockIO device:\r
8d3b5aff 2359 // if it is a removable blockIo device, it is valid.\r
128efbbc 2360 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 2361 //\r
2362 TempDevicePath = DevPath;\r
2363 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
2364 if (EFI_ERROR (Status)) {\r
2365 //\r
2366 // Device not present so see if we need to connect it\r
2367 //\r
2368 Status = BdsLibConnectDevicePath (DevPath);\r
2369 if (!EFI_ERROR (Status)) {\r
2370 //\r
2371 // Try again to get the Block Io protocol after we did the connect\r
2372 //\r
2373 TempDevicePath = DevPath;\r
2374 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
2375 }\r
2376 }\r
128efbbc 2377\r
5c08e117 2378 if (!EFI_ERROR (Status)) {\r
2379 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
2380 if (!EFI_ERROR (Status)) {\r
2381 if (CheckMedia) {\r
2382 //\r
2383 // Test if it is ready to boot now\r
2384 //\r
2385 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
2386 return TRUE;\r
2387 }\r
2388 } else {\r
2389 return TRUE;\r
2390 }\r
2391 }\r
2392 } else {\r
2393 //\r
2394 // 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
2395 //\r
2396 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
2397 if (!EFI_ERROR (Status)) {\r
2398 if (CheckMedia) {\r
2399 //\r
2400 // Test if it is ready to boot now\r
2401 //\r
2402 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
2403 return TRUE;\r
2404 }\r
2405 } else {\r
2406 return TRUE;\r
2407 }\r
2408 }\r
2409 }\r
2410\r
2411 return FALSE;\r
2412}\r
2413\r
2414\r
2415/**\r
2416 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
2417 try to return the valid device path.\r
2418 FV address maybe changes for memory layout adjust from time to time, use this function\r
2419 could promise the Fv file device path is right.\r
2420\r
2421 @param DevicePath on input, the Fv file device path need to check on\r
2422 output, the updated valid Fv file device path\r
2423 @param FileGuid the Fv file guild\r
2424\r
2425 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
2426 parameter\r
2427 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
2428 guild at all\r
2429 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
2430 valid\r
2431 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
2432 and return the updated device path in DevicePath\r
2433\r
2434**/\r
2435EFI_STATUS\r
2436EFIAPI\r
2437BdsLibUpdateFvFileDevicePath (\r
2438 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
2439 IN EFI_GUID *FileGuid\r
2440 )\r
2441{\r
2442 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
2443 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
2444 EFI_STATUS Status;\r
2445 EFI_GUID *GuidPoint;\r
2446 UINTN Index;\r
2447 UINTN FvHandleCount;\r
2448 EFI_HANDLE *FvHandleBuffer;\r
2449 EFI_FV_FILETYPE Type;\r
2450 UINTN Size;\r
2451 EFI_FV_FILE_ATTRIBUTES Attributes;\r
2452 UINT32 AuthenticationStatus;\r
2453 BOOLEAN FindFvFile;\r
2454 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
2455 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
2456 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
2457 EFI_HANDLE FoundFvHandle;\r
2458 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
2459\r
2460 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
2461 return EFI_INVALID_PARAMETER;\r
2462 }\r
2463 if (FileGuid == NULL) {\r
2464 return EFI_INVALID_PARAMETER;\r
2465 }\r
128efbbc 2466\r
5c08e117 2467 //\r
2468 // Check whether the device path point to the default the input Fv file\r
2469 //\r
2470 TempDevicePath = *DevicePath;\r
2471 LastDeviceNode = TempDevicePath;\r
2472 while (!IsDevicePathEnd (TempDevicePath)) {\r
2473 LastDeviceNode = TempDevicePath;\r
2474 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
2475 }\r
2476 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
2477 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
2478 );\r
2479 if (GuidPoint == NULL) {\r
2480 //\r
2481 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
2482 //\r
2483 return EFI_UNSUPPORTED;\r
2484 }\r
2485 if (!CompareGuid (GuidPoint, FileGuid)) {\r
2486 //\r
2487 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
2488 //\r
2489 return EFI_UNSUPPORTED;\r
2490 }\r
2491\r
2492 //\r
2493 // Check whether the input Fv file device path is valid\r
2494 //\r
2495 TempDevicePath = *DevicePath;\r
2496 FoundFvHandle = NULL;\r
2497 Status = gBS->LocateDevicePath (\r
2498 &gEfiFirmwareVolume2ProtocolGuid,\r
2499 &TempDevicePath,\r
2500 &FoundFvHandle\r
2501 );\r
2502 if (!EFI_ERROR (Status)) {\r
2503 Status = gBS->HandleProtocol (\r
2504 FoundFvHandle,\r
2505 &gEfiFirmwareVolume2ProtocolGuid,\r
2506 (VOID **) &Fv\r
2507 );\r
2508 if (!EFI_ERROR (Status)) {\r
2509 //\r
2510 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
2511 //\r
2512 Status = Fv->ReadFile (\r
2513 Fv,\r
2514 FileGuid,\r
2515 NULL,\r
2516 &Size,\r
2517 &Type,\r
2518 &Attributes,\r
2519 &AuthenticationStatus\r
2520 );\r
2521 if (!EFI_ERROR (Status)) {\r
2522 return EFI_ALREADY_STARTED;\r
2523 }\r
2524 }\r
2525 }\r
2526\r
2527 //\r
2528 // Look for the input wanted FV file in current FV\r
2529 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
2530 //\r
2531 FindFvFile = FALSE;\r
2532 FoundFvHandle = NULL;\r
2533 Status = gBS->HandleProtocol (\r
fefefa4c 2534 gImageHandle,\r
5c08e117 2535 &gEfiLoadedImageProtocolGuid,\r
2536 (VOID **) &LoadedImage\r
2537 );\r
2538 if (!EFI_ERROR (Status)) {\r
2539 Status = gBS->HandleProtocol (\r
2540 LoadedImage->DeviceHandle,\r
2541 &gEfiFirmwareVolume2ProtocolGuid,\r
2542 (VOID **) &Fv\r
2543 );\r
2544 if (!EFI_ERROR (Status)) {\r
2545 Status = Fv->ReadFile (\r
2546 Fv,\r
2547 FileGuid,\r
2548 NULL,\r
2549 &Size,\r
2550 &Type,\r
2551 &Attributes,\r
2552 &AuthenticationStatus\r
2553 );\r
2554 if (!EFI_ERROR (Status)) {\r
2555 FindFvFile = TRUE;\r
2556 FoundFvHandle = LoadedImage->DeviceHandle;\r
2557 }\r
2558 }\r
2559 }\r
2560 //\r
2561 // Second, if fail to find, try to enumerate all FV\r
2562 //\r
2563 if (!FindFvFile) {\r
2564 FvHandleBuffer = NULL;\r
2565 gBS->LocateHandleBuffer (\r
2566 ByProtocol,\r
2567 &gEfiFirmwareVolume2ProtocolGuid,\r
2568 NULL,\r
2569 &FvHandleCount,\r
2570 &FvHandleBuffer\r
2571 );\r
2572 for (Index = 0; Index < FvHandleCount; Index++) {\r
2573 gBS->HandleProtocol (\r
2574 FvHandleBuffer[Index],\r
2575 &gEfiFirmwareVolume2ProtocolGuid,\r
2576 (VOID **) &Fv\r
2577 );\r
2578\r
2579 Status = Fv->ReadFile (\r
2580 Fv,\r
2581 FileGuid,\r
2582 NULL,\r
2583 &Size,\r
2584 &Type,\r
2585 &Attributes,\r
2586 &AuthenticationStatus\r
2587 );\r
2588 if (EFI_ERROR (Status)) {\r
2589 //\r
2590 // Skip if input Fv file not in the FV\r
2591 //\r
2592 continue;\r
2593 }\r
2594 FindFvFile = TRUE;\r
2595 FoundFvHandle = FvHandleBuffer[Index];\r
2596 break;\r
2597 }\r
2598\r
2599 if (FvHandleBuffer != NULL) {\r
128efbbc 2600 FreePool (FvHandleBuffer);\r
5c08e117 2601 }\r
2602 }\r
2603\r
2604 if (FindFvFile) {\r
2605 //\r
2606 // Build the shell device path\r
2607 //\r
2608 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
2609 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
2610 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
2611 *DevicePath = NewDevicePath;\r
2612 return EFI_SUCCESS;\r
2613 }\r
2614 return EFI_NOT_FOUND;\r
2615}\r