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