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