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