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