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