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