]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
remove unnecessary EFI64_SHADOW_ALL_LEGACY_ROM in GenericBdsLib.
[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
180a5a35
HT
4Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
5c08e117 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "InternalBdsLib.h"\r
9aa7ba01 16#include "String.h"\r
5c08e117 17\r
18BOOLEAN mEnumBootDevice = FALSE;\r
9aa7ba01 19EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;\r
5c08e117 20\r
21///\r
22/// This GUID is used for an EFI Variable that stores the front device pathes\r
23/// for a partial device path that starts with the HD node.\r
24///\r
25EFI_GUID mHdBootVariablePrivateGuid = { 0xfab7e9e1, 0x39dd, 0x4f2b, { 0x84, 0x8, 0xe2, 0xe, 0x90, 0x6c, 0xb6, 0xde } };\r
26\r
9aa7ba01 27///\r
28/// This GUID is used for register UNI string.\r
29///\r
30EFI_GUID mBdsLibStringPackGuid = { 0x3b4d9b23, 0x95ac, 0x44f6, { 0x9f, 0xcd, 0xe, 0x95, 0x94, 0x58, 0x6c, 0x72 } };\r
31\r
32///\r
33/// This GUID is used for Set/Get platform language into/from variable at last time enumeration to ensure the enumeration will\r
34/// only execute once.\r
35///\r
36EFI_GUID mBdsLibLastLangGuid = { 0xe8c545b, 0xa2ee, 0x470d, { 0x8e, 0x26, 0xbd, 0xa1, 0xa1, 0x3c, 0xa, 0xa3 } };\r
37\r
38/**\r
39 The constructor function register UNI strings into imageHandle.\r
40 \r
41 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
42\r
43 @param ImageHandle The firmware allocated handle for the EFI image.\r
44 @param SystemTable A pointer to the EFI System Table.\r
45 \r
46 @retval EFI_SUCCESS The constructor successfully added string package.\r
47 @retval Other value The constructor can't add string package.\r
48\r
49**/\r
50EFI_STATUS\r
51EFIAPI\r
52GenericBdsLibConstructor (\r
53 IN EFI_HANDLE ImageHandle,\r
54 IN EFI_SYSTEM_TABLE *SystemTable\r
55 )\r
56{\r
57\r
58 gBdsLibStringPackHandle = HiiAddPackages (\r
59 &mBdsLibStringPackGuid,\r
60 &ImageHandle,\r
61 GenericBdsLibStrings,\r
62 NULL\r
63 );\r
64\r
65 ASSERT (gBdsLibStringPackHandle != NULL);\r
66\r
67 return EFI_SUCCESS;\r
68}\r
69\r
5c08e117 70\r
71\r
72/**\r
73 Boot the legacy system with the boot option\r
74\r
75 @param Option The legacy boot option which have BBS device path\r
76\r
77 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support\r
78 legacy boot.\r
79 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().\r
80\r
81**/\r
82EFI_STATUS\r
83BdsLibDoLegacyBoot (\r
84 IN BDS_COMMON_OPTION *Option\r
85 )\r
86{\r
87 EFI_STATUS Status;\r
88 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;\r
89\r
90 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);\r
91 if (EFI_ERROR (Status)) {\r
92 //\r
93 // If no LegacyBios protocol we do not support legacy boot\r
94 //\r
95 return EFI_UNSUPPORTED;\r
96 }\r
97 //\r
98 // Notes: if we separate the int 19, then we don't need to refresh BBS\r
99 //\r
100 BdsRefreshBbsTableForBoot (Option);\r
101\r
102 //\r
cd6a3b15 103 // Write boot to OS performance data for legacy boot.\r
5c08e117 104 //\r
105 PERF_CODE (\r
106 WriteBootToOsPerformanceData ();\r
107 );\r
108\r
109 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));\r
110 return LegacyBios->LegacyBoot (\r
111 LegacyBios,\r
112 (BBS_BBS_DEVICE_PATH *) Option->DevicePath,\r
113 Option->LoadOptionsSize,\r
114 Option->LoadOptions\r
115 );\r
116}\r
117\r
3d7decbc 118/**\r
119 Internal function to check if the input boot option is a valid EFI NV Boot####.\r
120\r
121 @param OptionToCheck Boot option to be checked.\r
122\r
123 @retval TRUE This boot option matches a valid EFI NV Boot####.\r
124 @retval FALSE If not.\r
128efbbc 125\r
3d7decbc 126**/\r
3d7decbc 127BOOLEAN\r
128IsBootOptionValidNVVarialbe (\r
129 IN BDS_COMMON_OPTION *OptionToCheck\r
130 )\r
131{\r
132 LIST_ENTRY TempList;\r
133 BDS_COMMON_OPTION *BootOption;\r
134 BOOLEAN Valid;\r
135 CHAR16 OptionName[20];\r
136\r
137 Valid = FALSE;\r
138\r
139 InitializeListHead (&TempList);\r
140 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);\r
5c08e117 141\r
3d7decbc 142 BootOption = BdsLibVariableToOption (&TempList, OptionName);\r
143 if (BootOption == NULL) {\r
144 return FALSE;\r
145 }\r
146\r
147 //\r
128efbbc 148 // If the Boot Option Number and Device Path matches, OptionToCheck matches a\r
3d7decbc 149 // valid EFI NV Boot####.\r
150 //\r
151 if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&\r
152 (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))\r
153 {\r
154 Valid = TRUE;\r
155 }\r
156\r
157 FreePool (BootOption);\r
128efbbc 158\r
3d7decbc 159 return Valid;\r
160}\r
5c08e117 161/**\r
162 Process the boot option follow the UEFI specification and\r
163 special treat the legacy boot option with BBS_DEVICE_PATH.\r
164\r
165 @param Option The boot option need to be processed\r
166 @param DevicePath The device path which describe where to load the\r
167 boot image or the legacy BBS device path to boot\r
168 the legacy OS\r
169 @param ExitDataSize The size of exit data.\r
170 @param ExitData Data returned when Boot image failed.\r
171\r
172 @retval EFI_SUCCESS Boot from the input boot option successfully.\r
173 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
174\r
175**/\r
176EFI_STATUS\r
177EFIAPI\r
178BdsLibBootViaBootOption (\r
179 IN BDS_COMMON_OPTION *Option,\r
180 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
181 OUT UINTN *ExitDataSize,\r
182 OUT CHAR16 **ExitData OPTIONAL\r
183 )\r
184{\r
185 EFI_STATUS Status;\r
186 EFI_HANDLE Handle;\r
187 EFI_HANDLE ImageHandle;\r
188 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
189 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;\r
190 EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;\r
191 EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;\r
192 LIST_ENTRY TempBootLists;\r
193\r
194 //\r
195 // Record the performance data for End of BDS\r
196 //\r
128efbbc 197 PERF_END(NULL, "BDS", NULL, 0);\r
5c08e117 198\r
199 *ExitDataSize = 0;\r
200 *ExitData = NULL;\r
201\r
5c08e117 202 //\r
203 // Notes: this code can be remove after the s3 script table\r
204 // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r
205 // EVT_SIGNAL_LEGACY_BOOT\r
206 //\r
207 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
208 if (!EFI_ERROR (Status)) {\r
209 AcpiS3Save->S3Save (AcpiS3Save, NULL);\r
210 }\r
211 //\r
212 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a\r
213 // full device path\r
214 //\r
215 WorkingDevicePath = NULL;\r
216 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
217 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r
218 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
219 (HARDDRIVE_DEVICE_PATH *)DevicePath\r
220 );\r
221 if (WorkingDevicePath != NULL) {\r
222 DevicePath = WorkingDevicePath;\r
223 }\r
224 }\r
225 //\r
226 // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
227 //\r
228 EfiSignalEventReadyToBoot();\r
128efbbc 229\r
230\r
5c08e117 231 //\r
232 // Set Boot Current\r
233 //\r
3d7decbc 234 if (IsBootOptionValidNVVarialbe (Option)) {\r
235 //\r
236 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.\r
237 // In this case, "BootCurrent" is not created.\r
238 // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r
239 //\r
240 gRT->SetVariable (\r
241 L"BootCurrent",\r
242 &gEfiGlobalVariableGuid,\r
243 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
244 sizeof (UINT16),\r
245 &Option->BootCurrent\r
246 );\r
247 }\r
5c08e117 248\r
249 ASSERT (Option->DevicePath != NULL);\r
250 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r
251 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r
252 ) {\r
253 //\r
254 // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
255 //\r
256 return BdsLibDoLegacyBoot (Option);\r
257 }\r
258\r
259 //\r
260 // If the boot option point to Internal FV shell, make sure it is valid\r
261 //\r
d46f3632 262 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));\r
5c08e117 263 if (!EFI_ERROR(Status)) {\r
264 if (Option->DevicePath != NULL) {\r
265 FreePool(Option->DevicePath);\r
266 }\r
267 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
268 ASSERT(Option->DevicePath != NULL);\r
269 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
270 //\r
271 // Update the shell boot option\r
272 //\r
273 InitializeListHead (&TempBootLists);\r
274 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
128efbbc 275\r
5c08e117 276 //\r
277 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
278 //\r
128efbbc 279 FreePool (DevicePath);\r
5c08e117 280 DevicePath = Option->DevicePath;\r
281 }\r
282\r
ab8cc80b 283 DEBUG_CODE_BEGIN();\r
5c08e117 284\r
9aa7ba01 285 if (Option->Description == NULL) {\r
286 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));\r
287 } else {\r
288 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
ab8cc80b 289 }\r
9aa7ba01 290 \r
ab8cc80b 291 DEBUG_CODE_END();\r
292 \r
5c08e117 293 Status = gBS->LoadImage (\r
294 TRUE,\r
295 mBdsImageHandle,\r
296 DevicePath,\r
297 NULL,\r
298 0,\r
299 &ImageHandle\r
300 );\r
301\r
302 //\r
261835cc 303 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
5c08e117 304 // and load the image according to the default boot behavior for removable device.\r
305 //\r
306 if (EFI_ERROR (Status)) {\r
307 //\r
308 // check if there is a bootable removable media could be found in this device path ,\r
309 // and get the bootable media handle\r
310 //\r
311 Handle = BdsLibGetBootableHandle(DevicePath);\r
312 if (Handle == NULL) {\r
313 goto Done;\r
314 }\r
315 //\r
316 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
317 // machinename is ia32, ia64, x64, ...\r
318 //\r
c62dbf31 319 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
5c08e117 320 if (FilePath != NULL) {\r
321 Status = gBS->LoadImage (\r
322 TRUE,\r
323 mBdsImageHandle,\r
324 FilePath,\r
325 NULL,\r
326 0,\r
327 &ImageHandle\r
328 );\r
329 if (EFI_ERROR (Status)) {\r
330 //\r
331 // The DevicePath failed, and it's not a valid\r
332 // removable media device.\r
333 //\r
334 goto Done;\r
335 }\r
336 }\r
337 }\r
338\r
339 if (EFI_ERROR (Status)) {\r
340 //\r
341 // It there is any error from the Boot attempt exit now.\r
342 //\r
343 goto Done;\r
344 }\r
345 //\r
346 // Provide the image with it's load options\r
347 //\r
348 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
349 ASSERT_EFI_ERROR (Status);\r
350\r
351 if (Option->LoadOptionsSize != 0) {\r
352 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
353 ImageInfo->LoadOptions = Option->LoadOptions;\r
354 }\r
355 //\r
356 // Before calling the image, enable the Watchdog Timer for\r
357 // the 5 Minute period\r
358 //\r
359 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
360\r
cd6a3b15 361 //\r
362 // Write boot to OS performance data for UEFI boot\r
363 //\r
364 PERF_CODE (\r
365 WriteBootToOsPerformanceData ();\r
366 );\r
367\r
5c08e117 368 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
369 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
370\r
371 //\r
372 // Clear the Watchdog Timer after the image returns\r
373 //\r
374 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
375\r
376Done:\r
377 //\r
378 // Clear Boot Current\r
379 //\r
380 gRT->SetVariable (\r
381 L"BootCurrent",\r
382 &gEfiGlobalVariableGuid,\r
383 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
384 0,\r
385 &Option->BootCurrent\r
386 );\r
387\r
388 return Status;\r
389}\r
390\r
391\r
392/**\r
393 Expand a device path that starts with a hard drive media device path node to be a\r
394 full device path that includes the full hardware path to the device. We need\r
395 to do this so it can be booted. As an optimization the front match (the part point\r
396 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
397 so a connect all is not required on every boot. All successful history device path\r
398 which point to partition node (the front part) will be saved.\r
399\r
400 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard\r
401 drive media device path.\r
402 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path\r
403 cannot be found.\r
404\r
405**/\r
406EFI_DEVICE_PATH_PROTOCOL *\r
407EFIAPI\r
408BdsExpandPartitionPartialDevicePathToFull (\r
409 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
410 )\r
411{\r
412 EFI_STATUS Status;\r
413 UINTN BlockIoHandleCount;\r
414 EFI_HANDLE *BlockIoBuffer;\r
415 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
416 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r
417 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
418 UINTN Index;\r
419 UINTN InstanceNum;\r
420 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r
421 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
422 UINTN CachedDevicePathSize;\r
423 BOOLEAN DeviceExist;\r
424 BOOLEAN NeedAdjust;\r
425 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
426 UINTN Size;\r
427\r
428 FullDevicePath = NULL;\r
429 //\r
430 // Check if there is prestore 'HDDP' variable.\r
431 // If exist, search the front path which point to partition node in the variable instants.\r
432 // If fail to find or 'HDDP' not exist, reconnect all and search in all system\r
433 //\r
434 CachedDevicePath = BdsLibGetVariableAndSize (\r
435 L"HDDP",\r
436 &mHdBootVariablePrivateGuid,\r
437 &CachedDevicePathSize\r
438 );\r
128efbbc 439\r
5c08e117 440 if (CachedDevicePath != NULL) {\r
441 TempNewDevicePath = CachedDevicePath;\r
442 DeviceExist = FALSE;\r
443 NeedAdjust = FALSE;\r
444 do {\r
445 //\r
446 // Check every instance of the variable\r
447 // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r
448 // partial partition boot option. Second, check whether the instance could be connected.\r
449 //\r
450 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
451 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
452 //\r
453 // Connect the device path instance, the device path point to hard drive media device path node\r
454 // e.g. ACPI() /PCI()/ATA()/Partition()\r
455 //\r
456 Status = BdsLibConnectDevicePath (Instance);\r
457 if (!EFI_ERROR (Status)) {\r
458 DeviceExist = TRUE;\r
459 break;\r
460 }\r
461 }\r
462 //\r
463 // Come here means the first instance is not matched\r
464 //\r
465 NeedAdjust = TRUE;\r
466 FreePool(Instance);\r
467 } while (TempNewDevicePath != NULL);\r
468\r
469 if (DeviceExist) {\r
470 //\r
471 // Find the matched device path.\r
472 // Append the file path information from the boot option and return the fully expanded device path.\r
473 //\r
474 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
475 FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
476\r
477 //\r
478 // Adjust the 'HDDP' instances sequence if the matched one is not first one.\r
479 //\r
480 if (NeedAdjust) {\r
481 //\r
482 // First delete the matched instance.\r
483 //\r
484 TempNewDevicePath = CachedDevicePath;\r
485 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
486 FreePool (TempNewDevicePath);\r
128efbbc 487\r
5c08e117 488 //\r
489 // Second, append the remaining path after the matched instance\r
490 //\r
491 TempNewDevicePath = CachedDevicePath;\r
492 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
493 FreePool (TempNewDevicePath);\r
494 //\r
495 // Save the matching Device Path so we don't need to do a connect all next time\r
496 //\r
497 Status = gRT->SetVariable (\r
498 L"HDDP",\r
499 &mHdBootVariablePrivateGuid,\r
500 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
501 GetDevicePathSize (CachedDevicePath),\r
502 CachedDevicePath\r
503 );\r
504 }\r
128efbbc 505\r
5c08e117 506 FreePool (Instance);\r
507 FreePool (CachedDevicePath);\r
508 return FullDevicePath;\r
509 }\r
510 }\r
511\r
512 //\r
513 // If we get here we fail to find or 'HDDP' not exist, and now we need\r
514 // to search all devices in the system for a matched partition\r
515 //\r
516 BdsLibConnectAllDriversToAllControllers ();\r
517 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
518 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {\r
519 //\r
520 // If there was an error or there are no device handles that support\r
521 // the BLOCK_IO Protocol, then return.\r
522 //\r
523 return NULL;\r
524 }\r
525 //\r
526 // Loop through all the device handles that support the BLOCK_IO Protocol\r
527 //\r
528 for (Index = 0; Index < BlockIoHandleCount; Index++) {\r
529\r
530 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r
531 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r
532 continue;\r
533 }\r
534\r
535 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {\r
536 //\r
537 // Find the matched partition device path\r
538 //\r
539 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
540 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
541\r
542 //\r
543 // Save the matched partition device path in 'HDDP' variable\r
544 //\r
545 if (CachedDevicePath != NULL) {\r
546 //\r
547 // Save the matched partition device path as first instance of 'HDDP' variable\r
548 //\r
549 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
550 TempNewDevicePath = CachedDevicePath;\r
551 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
552 FreePool(TempNewDevicePath);\r
553\r
554 TempNewDevicePath = CachedDevicePath;\r
555 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
cd730ec0 556 if (TempNewDevicePath != NULL) {\r
557 FreePool(TempNewDevicePath);\r
558 }\r
5c08e117 559 } else {\r
560 TempNewDevicePath = CachedDevicePath;\r
561 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
562 FreePool(TempNewDevicePath);\r
563 }\r
564 //\r
565 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
566 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.\r
567 //\r
568 InstanceNum = 0;\r
569 ASSERT (CachedDevicePath != NULL);\r
570 TempNewDevicePath = CachedDevicePath;\r
571 while (!IsDevicePathEnd (TempNewDevicePath)) {\r
572 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
573 //\r
574 // Parse one instance\r
575 //\r
576 while (!IsDevicePathEndType (TempNewDevicePath)) {\r
577 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
578 }\r
579 InstanceNum++;\r
580 //\r
581 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r
582 //\r
583 if (InstanceNum >= 12) {\r
584 SetDevicePathEndNode (TempNewDevicePath);\r
585 break;\r
586 }\r
587 }\r
588 } else {\r
589 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
590 }\r
591\r
592 //\r
593 // Save the matching Device Path so we don't need to do a connect all next time\r
594 //\r
595 Status = gRT->SetVariable (\r
596 L"HDDP",\r
597 &mHdBootVariablePrivateGuid,\r
598 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
599 GetDevicePathSize (CachedDevicePath),\r
600 CachedDevicePath\r
601 );\r
602\r
603 break;\r
604 }\r
605 }\r
128efbbc 606\r
cd730ec0 607 if (CachedDevicePath != NULL) {\r
608 FreePool (CachedDevicePath);\r
609 }\r
5c08e117 610 if (BlockIoBuffer != NULL) {\r
611 FreePool (BlockIoBuffer);\r
612 }\r
613 return FullDevicePath;\r
614}\r
615\r
616/**\r
617 Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
618 instances, has the same partition node with HardDriveDevicePath device path\r
619\r
620 @param BlockIoDevicePath Multi device path instances which need to check\r
621 @param HardDriveDevicePath A device path which starts with a hard drive media\r
622 device path.\r
623\r
624 @retval TRUE There is a matched device path instance.\r
625 @retval FALSE There is no matched device path instance.\r
626\r
627**/\r
628BOOLEAN\r
629EFIAPI\r
630MatchPartitionDevicePathNode (\r
631 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r
632 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
633 )\r
634{\r
635 HARDDRIVE_DEVICE_PATH *TmpHdPath;\r
636 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
637 BOOLEAN Match;\r
638 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;\r
639\r
640 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
641 return FALSE;\r
642 }\r
128efbbc 643\r
5c08e117 644 //\r
645 // Make PreviousDevicePath == the device path node before the end node\r
646 //\r
647 DevicePath = BlockIoDevicePath;\r
648 BlockIoHdDevicePathNode = NULL;\r
649\r
650 //\r
651 // find the partition device path node\r
652 //\r
653 while (!IsDevicePathEnd (DevicePath)) {\r
654 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
655 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
656 ) {\r
657 BlockIoHdDevicePathNode = DevicePath;\r
658 break;\r
659 }\r
660\r
661 DevicePath = NextDevicePathNode (DevicePath);\r
662 }\r
663\r
664 if (BlockIoHdDevicePathNode == NULL) {\r
665 return FALSE;\r
666 }\r
667 //\r
668 // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
669 //\r
670 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
671 Match = FALSE;\r
128efbbc 672\r
5c08e117 673 //\r
674 // Check for the match\r
675 //\r
676 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r
677 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r
678 switch (TmpHdPath->SignatureType) {\r
679 case SIGNATURE_TYPE_GUID:\r
680 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r
681 break;\r
682 case SIGNATURE_TYPE_MBR:\r
683 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r
684 break;\r
685 default:\r
686 Match = FALSE;\r
687 break;\r
688 }\r
689 }\r
690\r
691 return Match;\r
692}\r
693\r
694/**\r
695 Delete the boot option associated with the handle passed in.\r
696\r
697 @param Handle The handle which present the device path to create\r
698 boot option\r
699\r
700 @retval EFI_SUCCESS Delete the boot option success\r
701 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
702 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
703 @retval Other Error return value from SetVariable()\r
704\r
705**/\r
706EFI_STATUS\r
707BdsLibDeleteOptionFromHandle (\r
708 IN EFI_HANDLE Handle\r
709 )\r
710{\r
711 UINT16 *BootOrder;\r
712 UINT8 *BootOptionVar;\r
713 UINTN BootOrderSize;\r
714 UINTN BootOptionSize;\r
715 EFI_STATUS Status;\r
716 UINTN Index;\r
717 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
718 UINTN DevicePathSize;\r
719 UINTN OptionDevicePathSize;\r
720 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
721 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
722 UINT8 *TempPtr;\r
723\r
724 Status = EFI_SUCCESS;\r
725 BootOrder = NULL;\r
726 BootOrderSize = 0;\r
727\r
728 //\r
729 // Check "BootOrder" variable, if no, means there is no any boot order.\r
730 //\r
731 BootOrder = BdsLibGetVariableAndSize (\r
732 L"BootOrder",\r
733 &gEfiGlobalVariableGuid,\r
734 &BootOrderSize\r
735 );\r
736 if (BootOrder == NULL) {\r
737 return EFI_NOT_FOUND;\r
738 }\r
739\r
740 //\r
741 // Convert device handle to device path protocol instance\r
742 //\r
743 DevicePath = DevicePathFromHandle (Handle);\r
744 if (DevicePath == NULL) {\r
745 return EFI_NOT_FOUND;\r
746 }\r
747 DevicePathSize = GetDevicePathSize (DevicePath);\r
748\r
749 //\r
750 // Loop all boot order variable and find the matching device path\r
751 //\r
752 Index = 0;\r
753 while (Index < BootOrderSize / sizeof (UINT16)) {\r
754 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
755 BootOptionVar = BdsLibGetVariableAndSize (\r
756 BootOption,\r
757 &gEfiGlobalVariableGuid,\r
758 &BootOptionSize\r
759 );\r
128efbbc 760\r
5c08e117 761 if (BootOptionVar == NULL) {\r
762 FreePool (BootOrder);\r
763 return EFI_OUT_OF_RESOURCES;\r
764 }\r
765\r
766 TempPtr = BootOptionVar;\r
767 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
768 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
769 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
770 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
771\r
772 //\r
773 // Check whether the device path match\r
774 //\r
775 if ((OptionDevicePathSize == DevicePathSize) &&\r
776 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
777 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
778 FreePool (BootOptionVar);\r
779 break;\r
780 }\r
781\r
782 FreePool (BootOptionVar);\r
783 Index++;\r
784 }\r
785\r
786 //\r
787 // Adjust number of boot option for "BootOrder" variable.\r
788 //\r
789 Status = gRT->SetVariable (\r
790 L"BootOrder",\r
791 &gEfiGlobalVariableGuid,\r
792 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
793 BootOrderSize,\r
794 BootOrder\r
795 );\r
796\r
797 FreePool (BootOrder);\r
798\r
799 return Status;\r
800}\r
801\r
802\r
803/**\r
3384a9bc 804 Delete all invalid EFI boot options.\r
5c08e117 805\r
806 @retval EFI_SUCCESS Delete all invalid boot option success\r
807 @retval EFI_NOT_FOUND Variable "BootOrder" is not found\r
808 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
809 @retval Other Error return value from SetVariable()\r
810\r
811**/\r
812EFI_STATUS\r
813BdsDeleteAllInvalidEfiBootOption (\r
814 VOID\r
815 )\r
816{\r
817 UINT16 *BootOrder;\r
818 UINT8 *BootOptionVar;\r
819 UINTN BootOrderSize;\r
820 UINTN BootOptionSize;\r
821 EFI_STATUS Status;\r
822 UINTN Index;\r
823 UINTN Index2;\r
824 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
825 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
826 UINT8 *TempPtr;\r
3384a9bc 827 CHAR16 *Description;\r
5c08e117 828\r
829 Status = EFI_SUCCESS;\r
830 BootOrder = NULL;\r
831 BootOrderSize = 0;\r
832\r
833 //\r
834 // Check "BootOrder" variable firstly, this variable hold the number of boot options\r
835 //\r
836 BootOrder = BdsLibGetVariableAndSize (\r
837 L"BootOrder",\r
838 &gEfiGlobalVariableGuid,\r
839 &BootOrderSize\r
840 );\r
841 if (NULL == BootOrder) {\r
842 return EFI_NOT_FOUND;\r
843 }\r
844\r
845 Index = 0;\r
846 while (Index < BootOrderSize / sizeof (UINT16)) {\r
847 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
848 BootOptionVar = BdsLibGetVariableAndSize (\r
849 BootOption,\r
850 &gEfiGlobalVariableGuid,\r
851 &BootOptionSize\r
852 );\r
853 if (NULL == BootOptionVar) {\r
854 FreePool (BootOrder);\r
855 return EFI_OUT_OF_RESOURCES;\r
856 }\r
857\r
858 TempPtr = BootOptionVar;\r
859 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
3384a9bc 860 Description = (CHAR16 *) TempPtr;\r
5c08e117 861 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
862 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
863\r
864 //\r
865 // Skip legacy boot option (BBS boot device)\r
866 //\r
867 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
868 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
869 FreePool (BootOptionVar);\r
870 Index++;\r
871 continue;\r
872 }\r
873\r
3384a9bc 874 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {\r
5c08e117 875 //\r
876 // Delete this invalid boot option "Boot####"\r
877 //\r
878 Status = gRT->SetVariable (\r
879 BootOption,\r
880 &gEfiGlobalVariableGuid,\r
881 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
882 0,\r
883 NULL\r
884 );\r
885 //\r
886 // Mark this boot option in boot order as deleted\r
887 //\r
888 BootOrder[Index] = 0xffff;\r
889 }\r
890\r
891 FreePool (BootOptionVar);\r
892 Index++;\r
893 }\r
894\r
895 //\r
896 // Adjust boot order array\r
897 //\r
898 Index2 = 0;\r
899 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
900 if (BootOrder[Index] != 0xffff) {\r
901 BootOrder[Index2] = BootOrder[Index];\r
902 Index2 ++;\r
903 }\r
904 }\r
905 Status = gRT->SetVariable (\r
906 L"BootOrder",\r
907 &gEfiGlobalVariableGuid,\r
908 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
909 Index2 * sizeof (UINT16),\r
910 BootOrder\r
911 );\r
912\r
913 FreePool (BootOrder);\r
914\r
915 return Status;\r
916}\r
917\r
918\r
919/**\r
3384a9bc 920 For EFI boot option, BDS separate them as six types:\r
128efbbc 921 1. Network - The boot option points to the SimpleNetworkProtocol device.\r
3384a9bc 922 Bds will try to automatically create this type boot option when enumerate.\r
128efbbc 923 2. Shell - The boot option points to internal flash shell.\r
3384a9bc 924 Bds will try to automatically create this type boot option when enumerate.\r
925 3. Removable BlockIo - The boot option only points to the removable media\r
926 device, like USB flash disk, DVD, Floppy etc.\r
927 These device should contain a *removable* blockIo\r
928 protocol in their device handle.\r
128efbbc 929 Bds will try to automatically create this type boot option\r
3384a9bc 930 when enumerate.\r
128efbbc 931 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r
3384a9bc 932 like HardDisk.\r
933 These device should contain a *fixed* blockIo\r
934 protocol in their device handle.\r
935 BDS will skip fixed blockIo devices, and NOT\r
128efbbc 936 automatically create boot option for them. But BDS\r
937 will help to delete those fixed blockIo boot option,\r
3384a9bc 938 whose description rule conflict with other auto-created\r
939 boot options.\r
128efbbc 940 5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r
3384a9bc 941 has SimpleFileSystem Protocol, but has no blockio\r
942 protocol. These devices do not offer blockIo\r
128efbbc 943 protocol, but BDS still can get the\r
3384a9bc 944 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r
945 Protocol.\r
128efbbc 946 6. File - The boot option points to a file. These boot options are usually\r
3384a9bc 947 created by user manually or OS loader. BDS will not delete or modify\r
128efbbc 948 these boot options.\r
949\r
3384a9bc 950 This function will enumerate all possible boot device in the system, and\r
128efbbc 951 automatically create boot options for Network, Shell, Removable BlockIo,\r
3384a9bc 952 and Non-BlockIo Simplefile devices.\r
8d3b5aff 953 It will only execute once of every boot.\r
128efbbc 954\r
5c08e117 955 @param BdsBootOptionList The header of the link list which indexed all\r
956 current boot options\r
957\r
958 @retval EFI_SUCCESS Finished all the boot device enumerate and create\r
959 the boot option base on that boot device\r
960\r
e83c9064 961 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list\r
5c08e117 962**/\r
963EFI_STATUS\r
964EFIAPI\r
965BdsLibEnumerateAllBootOption (\r
966 IN OUT LIST_ENTRY *BdsBootOptionList\r
967 )\r
968{\r
969 EFI_STATUS Status;\r
970 UINT16 FloppyNumber;\r
971 UINT16 CdromNumber;\r
972 UINT16 UsbNumber;\r
973 UINT16 MiscNumber;\r
8d3b5aff 974 UINT16 ScsiNumber;\r
5c08e117 975 UINT16 NonBlockNumber;\r
976 UINTN NumberBlockIoHandles;\r
977 EFI_HANDLE *BlockIoHandles;\r
978 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
979 UINTN Index;\r
a7a523e0 980 UINTN NumOfLoadFileHandles;\r
981 EFI_HANDLE *LoadFileHandles;\r
5c08e117 982 UINTN FvHandleCount;\r
983 EFI_HANDLE *FvHandleBuffer;\r
984 EFI_FV_FILETYPE Type;\r
985 UINTN Size;\r
986 EFI_FV_FILE_ATTRIBUTES Attributes;\r
987 UINT32 AuthenticationStatus;\r
8d3b5aff 988 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
989 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
5c08e117 990 UINTN DevicePathType;\r
991 CHAR16 Buffer[40];\r
992 EFI_HANDLE *FileSystemHandles;\r
993 UINTN NumberFileSystemHandles;\r
994 BOOLEAN NeedDelete;\r
995 EFI_IMAGE_DOS_HEADER DosHeader;\r
9aa7ba01 996 CHAR8 *PlatLang;\r
997 CHAR8 *LastLang;\r
5c08e117 998 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
999 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1000\r
1001 FloppyNumber = 0;\r
1002 CdromNumber = 0;\r
1003 UsbNumber = 0;\r
1004 MiscNumber = 0;\r
8d3b5aff 1005 ScsiNumber = 0;\r
9aa7ba01 1006 PlatLang = NULL;\r
1007 LastLang = NULL;\r
5c08e117 1008 ZeroMem (Buffer, sizeof (Buffer));\r
128efbbc 1009\r
5c08e117 1010 //\r
1011 // If the boot device enumerate happened, just get the boot\r
1012 // device from the boot order variable\r
1013 //\r
1014 if (mEnumBootDevice) {\r
9aa7ba01 1015 LastLang = GetVariable (L"LastEnumLang", &mBdsLibLastLangGuid);\r
1016 PlatLang = GetEfiGlobalVariable (L"PlatformLang");\r
1017 if (LastLang == PlatLang) {\r
1018 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
1019 return Status;\r
1020 } else {\r
1021 Status = gRT->SetVariable (\r
1022 L"LastEnumLang",\r
1023 &mBdsLibLastLangGuid,\r
1024 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1025 sizeof (PlatLang),\r
1026 PlatLang\r
1027 );\r
1028 ASSERT_EFI_ERROR (Status);\r
1029 }\r
5c08e117 1030 }\r
128efbbc 1031\r
5c08e117 1032 //\r
1033 // Notes: this dirty code is to get the legacy boot option from the\r
1034 // BBS table and create to variable as the EFI boot option, it should\r
1035 // be removed after the CSM can provide legacy boot option directly\r
1036 //\r
1037 REFRESH_LEGACY_BOOT_OPTIONS;\r
1038\r
1039 //\r
1040 // Delete invalid boot option\r
1041 //\r
1042 BdsDeleteAllInvalidEfiBootOption ();\r
128efbbc 1043\r
5c08e117 1044 //\r
1045 // Parse removable media\r
1046 //\r
1047 gBS->LocateHandleBuffer (\r
1048 ByProtocol,\r
1049 &gEfiBlockIoProtocolGuid,\r
1050 NULL,\r
1051 &NumberBlockIoHandles,\r
1052 &BlockIoHandles\r
1053 );\r
128efbbc 1054\r
5c08e117 1055 for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
1056 Status = gBS->HandleProtocol (\r
1057 BlockIoHandles[Index],\r
1058 &gEfiBlockIoProtocolGuid,\r
1059 (VOID **) &BlkIo\r
1060 );\r
1061 if (!EFI_ERROR (Status)) {\r
1062 if (!BlkIo->Media->RemovableMedia) {\r
1063 //\r
1064 // skip the non-removable block devices\r
1065 //\r
1066 continue;\r
1067 }\r
1068 }\r
1069 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r
1070 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
1071\r
1072 switch (DevicePathType) {\r
1073 case BDS_EFI_ACPI_FLOPPY_BOOT:\r
9aa7ba01 1074 if (FloppyNumber != 0) {\r
1075 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
1076 } else {\r
1077 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
1078 }\r
5c08e117 1079 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1080 FloppyNumber++;\r
1081 break;\r
128efbbc 1082\r
3384a9bc 1083 //\r
1084 // Assume a removable SATA device should be the DVD/CD device\r
1085 //\r
5c08e117 1086 case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
3384a9bc 1087 case BDS_EFI_MESSAGE_SATA_BOOT:\r
9aa7ba01 1088 if (CdromNumber != 0) {\r
1089 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
1090 } else {\r
1091 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
1092 }\r
1093 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
5c08e117 1094 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1095 CdromNumber++;\r
1096 break;\r
1097\r
1098 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
9aa7ba01 1099 if (UsbNumber != 0) {\r
1100 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
1101 } else {\r
1102 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
1103 }\r
5c08e117 1104 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1105 UsbNumber++;\r
1106 break;\r
1107\r
1108 case BDS_EFI_MESSAGE_SCSI_BOOT:\r
9aa7ba01 1109 if (ScsiNumber != 0) {\r
1110 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
1111 } else {\r
1112 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
1113 }\r
128efbbc 1114 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
8d3b5aff 1115 ScsiNumber++;\r
5c08e117 1116 break;\r
1117\r
1118 case BDS_EFI_MESSAGE_MISC_BOOT:\r
9aa7ba01 1119 if (MiscNumber != 0) {\r
1120 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
1121 } else {\r
1122 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
1123 }\r
128efbbc 1124 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 1125 MiscNumber++;\r
1126 break;\r
1127\r
1128 default:\r
1129 break;\r
1130 }\r
1131 }\r
1132\r
1133 if (NumberBlockIoHandles != 0) {\r
1134 FreePool (BlockIoHandles);\r
1135 }\r
1136\r
1137 //\r
1138 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
1139 //\r
1140 NonBlockNumber = 0;\r
1141 gBS->LocateHandleBuffer (\r
1142 ByProtocol,\r
1143 &gEfiSimpleFileSystemProtocolGuid,\r
1144 NULL,\r
1145 &NumberFileSystemHandles,\r
1146 &FileSystemHandles\r
1147 );\r
1148 for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
1149 Status = gBS->HandleProtocol (\r
1150 FileSystemHandles[Index],\r
1151 &gEfiBlockIoProtocolGuid,\r
1152 (VOID **) &BlkIo\r
1153 );\r
1154 if (!EFI_ERROR (Status)) {\r
1155 //\r
1156 // Skip if the file system handle supports a BlkIo protocol,\r
1157 //\r
1158 continue;\r
1159 }\r
1160\r
1161 //\r
1162 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
1163 // machinename is ia32, ia64, x64, ...\r
1164 //\r
1165 Hdr.Union = &HdrData;\r
1166 NeedDelete = TRUE;\r
1167 Status = BdsLibGetImageHeader (\r
1168 FileSystemHandles[Index],\r
c62dbf31 1169 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1170 &DosHeader,\r
1171 Hdr\r
1172 );\r
1173 if (!EFI_ERROR (Status) &&\r
1174 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1175 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1176 NeedDelete = FALSE;\r
1177 }\r
1178\r
1179 if (NeedDelete) {\r
1180 //\r
1181 // No such file or the file is not a EFI application, delete this boot option\r
1182 //\r
1183 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
1184 } else {\r
9aa7ba01 1185 if (NonBlockNumber != 0) {\r
1186 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
1187 } else {\r
1188 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
1189 }\r
5c08e117 1190 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
1191 NonBlockNumber++;\r
1192 }\r
1193 }\r
1194\r
1195 if (NumberFileSystemHandles != 0) {\r
1196 FreePool (FileSystemHandles);\r
1197 }\r
1198\r
1199 //\r
1200 // Parse Network Boot Device\r
1201 //\r
a7a523e0 1202 NumOfLoadFileHandles = 0;\r
ff482c56 1203 //\r
a7a523e0 1204 // Search Load File protocol for PXE boot option.\r
ff482c56 1205 //\r
5c08e117 1206 gBS->LocateHandleBuffer (\r
1207 ByProtocol,\r
a7a523e0 1208 &gEfiLoadFileProtocolGuid,\r
5c08e117 1209 NULL,\r
a7a523e0 1210 &NumOfLoadFileHandles,\r
1211 &LoadFileHandles\r
5c08e117 1212 );\r
8d3b5aff 1213\r
a7a523e0 1214 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
9aa7ba01 1215 if (Index != 0) {\r
1216 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r
1217 } else {\r
1218 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r
1219 }\r
a7a523e0 1220 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 1221 }\r
1222\r
a7a523e0 1223 if (NumOfLoadFileHandles != 0) {\r
1224 FreePool (LoadFileHandles);\r
5c08e117 1225 }\r
1226\r
1227 //\r
1228 // Check if we have on flash shell\r
1229 //\r
1230 gBS->LocateHandleBuffer (\r
1231 ByProtocol,\r
1232 &gEfiFirmwareVolume2ProtocolGuid,\r
1233 NULL,\r
1234 &FvHandleCount,\r
1235 &FvHandleBuffer\r
1236 );\r
1237 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 1238 gBS->HandleProtocol (\r
1239 FvHandleBuffer[Index],\r
1240 &gEfiFirmwareVolume2ProtocolGuid,\r
1241 (VOID **) &Fv\r
1242 );\r
1243\r
1244 Status = Fv->ReadFile (\r
1245 Fv,\r
d46f3632 1246 PcdGetPtr(PcdShellFile),\r
5c08e117 1247 NULL,\r
1248 &Size,\r
1249 &Type,\r
1250 &Attributes,\r
1251 &AuthenticationStatus\r
1252 );\r
1253 if (EFI_ERROR (Status)) {\r
1254 //\r
1255 // Skip if no shell file in the FV\r
1256 //\r
1257 continue;\r
1258 }\r
1259 //\r
1260 // Build the shell boot option\r
1261 //\r
1262 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
1263 }\r
1264\r
1265 if (FvHandleCount != 0) {\r
1266 FreePool (FvHandleBuffer);\r
1267 }\r
1268 //\r
1269 // Make sure every boot only have one time\r
1270 // boot device enumerate\r
1271 //\r
e83c9064 1272 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 1273 mEnumBootDevice = TRUE;\r
1274\r
e83c9064 1275 return Status;\r
5c08e117 1276}\r
1277\r
1278/**\r
1279 Build the boot option with the handle parsed in\r
1280\r
1281 @param Handle The handle which present the device path to create\r
1282 boot option\r
1283 @param BdsBootOptionList The header of the link list which indexed all\r
1284 current boot options\r
1285 @param String The description of the boot option.\r
1286\r
1287**/\r
1288VOID\r
1289EFIAPI\r
1290BdsLibBuildOptionFromHandle (\r
1291 IN EFI_HANDLE Handle,\r
1292 IN LIST_ENTRY *BdsBootOptionList,\r
1293 IN CHAR16 *String\r
1294 )\r
1295{\r
1296 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 1297\r
8d3b5aff 1298 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 1299\r
1300 //\r
1301 // Create and register new boot option\r
1302 //\r
1303 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
1304}\r
1305\r
1306\r
1307/**\r
1308 Build the on flash shell boot option with the handle parsed in.\r
1309\r
1310 @param Handle The handle which present the device path to create\r
1311 on flash shell boot option\r
1312 @param BdsBootOptionList The header of the link list which indexed all\r
1313 current boot options\r
1314\r
1315**/\r
1316VOID\r
1317EFIAPI\r
1318BdsLibBuildOptionFromShell (\r
1319 IN EFI_HANDLE Handle,\r
1320 IN OUT LIST_ENTRY *BdsBootOptionList\r
1321 )\r
1322{\r
1323 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1324 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
1325\r
1326 DevicePath = DevicePathFromHandle (Handle);\r
1327\r
1328 //\r
1329 // Build the shell device path\r
1330 //\r
d46f3632 1331 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 1332\r
1333 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
1334\r
1335 //\r
1336 // Create and register the shell boot option\r
1337 //\r
1338 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
1339\r
1340}\r
1341\r
1342/**\r
1343 Boot from the UEFI spec defined "BootNext" variable.\r
1344\r
1345**/\r
1346VOID\r
1347EFIAPI\r
1348BdsLibBootNext (\r
1349 VOID\r
1350 )\r
1351{\r
1352 UINT16 *BootNext;\r
1353 UINTN BootNextSize;\r
1354 CHAR16 Buffer[20];\r
1355 BDS_COMMON_OPTION *BootOption;\r
1356 LIST_ENTRY TempList;\r
1357 UINTN ExitDataSize;\r
1358 CHAR16 *ExitData;\r
1359\r
1360 //\r
1361 // Init the boot option name buffer and temp link list\r
1362 //\r
1363 InitializeListHead (&TempList);\r
1364 ZeroMem (Buffer, sizeof (Buffer));\r
1365\r
1366 BootNext = BdsLibGetVariableAndSize (\r
1367 L"BootNext",\r
1368 &gEfiGlobalVariableGuid,\r
1369 &BootNextSize\r
1370 );\r
1371\r
1372 //\r
1373 // Clear the boot next variable first\r
1374 //\r
1375 if (BootNext != NULL) {\r
1376 gRT->SetVariable (\r
1377 L"BootNext",\r
1378 &gEfiGlobalVariableGuid,\r
1379 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1380 0,\r
1381 BootNext\r
1382 );\r
1383\r
1384 //\r
1385 // Start to build the boot option and try to boot\r
1386 //\r
1387 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
1388 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
1389 ASSERT (BootOption != NULL);\r
1390 BdsLibConnectDevicePath (BootOption->DevicePath);\r
1391 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
1392 }\r
1393\r
1394}\r
1395\r
1396/**\r
1397 Return the bootable media handle.\r
1398 First, check the device is connected\r
1399 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
1400 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
1401\r
e83c9064 1402 @param DevicePath Device Path to a bootable device\r
5c08e117 1403\r
e83c9064 1404 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 1405\r
1406**/\r
1407EFI_HANDLE\r
1408EFIAPI\r
1409BdsLibGetBootableHandle (\r
1410 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1411 )\r
1412{\r
1413 EFI_STATUS Status;\r
1414 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1415 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
1416 EFI_HANDLE Handle;\r
1417 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1418 VOID *Buffer;\r
1419 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1420 UINTN Size;\r
1421 UINTN TempSize;\r
1422 EFI_HANDLE ReturnHandle;\r
1423 EFI_HANDLE *SimpleFileSystemHandles;\r
1424\r
1425 UINTN NumberSimpleFileSystemHandles;\r
1426 UINTN Index;\r
1427 EFI_IMAGE_DOS_HEADER DosHeader;\r
1428 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
1429 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1430\r
1431 UpdatedDevicePath = DevicePath;\r
128efbbc 1432\r
5c08e117 1433 //\r
1434 // Check whether the device is connected\r
1435 //\r
1436 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
1437 if (EFI_ERROR (Status)) {\r
1438 //\r
1439 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
1440 //\r
1441 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
1442 if (EFI_ERROR (Status)) {\r
1443 //\r
1444 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
1445 //\r
1446 UpdatedDevicePath = DevicePath;\r
1447 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1448 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
1449 }\r
1450 } else {\r
e74f510b
RN
1451 //\r
1452 // For removable device boot option, its contained device path only point to the removable device handle, \r
1453 // should make sure all its children handles (its child partion or media handles) are created and connected. \r
1454 //\r
1455 gBS->ConnectController (Handle, NULL, NULL, TRUE); \r
5c08e117 1456 //\r
1457 // Get BlockIo protocol and check removable attribute\r
1458 //\r
1459 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1460 //\r
1461 // Issue a dummy read to the device to check for media change.\r
1462 // When the removable media is changed, any Block IO read/write will\r
1463 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
1464 // returned. After the Block IO protocol is reinstalled, subsequent\r
1465 // Block IO read/write will success.\r
1466 //\r
1467 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
1468 if (Buffer != NULL) {\r
1469 BlockIo->ReadBlocks (\r
1470 BlockIo,\r
1471 BlockIo->Media->MediaId,\r
1472 0,\r
1473 BlockIo->Media->BlockSize,\r
1474 Buffer\r
1475 );\r
1476 FreePool(Buffer);\r
1477 }\r
1478 }\r
1479\r
1480 //\r
1481 // Detect the the default boot file from removable Media\r
1482 //\r
1483\r
1484 //\r
1485 // 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
1486 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
1487 //\r
1488 DupDevicePath = DuplicateDevicePath (DevicePath);\r
1489 ASSERT (DupDevicePath != NULL);\r
128efbbc 1490\r
5c08e117 1491 UpdatedDevicePath = DupDevicePath;\r
1492 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1493 //\r
1494 // 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
1495 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
1496 //\r
1497 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1498 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
1499 //\r
1500 // Remove the usb node, let the device path only point to PCI node\r
1501 //\r
1502 SetDevicePathEndNode (UpdatedDevicePath);\r
1503 UpdatedDevicePath = DupDevicePath;\r
1504 } else {\r
1505 UpdatedDevicePath = DevicePath;\r
1506 }\r
1507\r
1508 //\r
1509 // Get the device path size of boot option\r
1510 //\r
1511 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1512 ReturnHandle = NULL;\r
1513 gBS->LocateHandleBuffer (\r
1514 ByProtocol,\r
1515 &gEfiSimpleFileSystemProtocolGuid,\r
1516 NULL,\r
1517 &NumberSimpleFileSystemHandles,\r
1518 &SimpleFileSystemHandles\r
1519 );\r
1520 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
1521 //\r
1522 // Get the device path size of SimpleFileSystem handle\r
1523 //\r
1524 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
1525 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1526 //\r
1527 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
1528 //\r
1529 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
1530 //\r
1531 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
1532 // machinename is ia32, ia64, x64, ...\r
1533 //\r
1534 Hdr.Union = &HdrData;\r
1535 Status = BdsLibGetImageHeader (\r
1536 SimpleFileSystemHandles[Index],\r
c62dbf31 1537 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1538 &DosHeader,\r
1539 Hdr\r
1540 );\r
1541 if (!EFI_ERROR (Status) &&\r
1542 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1543 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1544 ReturnHandle = SimpleFileSystemHandles[Index];\r
1545 break;\r
1546 }\r
1547 }\r
1548 }\r
1549\r
1550 FreePool(DupDevicePath);\r
1551\r
1552 if (SimpleFileSystemHandles != NULL) {\r
1553 FreePool(SimpleFileSystemHandles);\r
1554 }\r
1555\r
1556 return ReturnHandle;\r
1557}\r
1558\r
1559/**\r
1560 Check to see if the network cable is plugged in. If the DevicePath is not\r
1561 connected it will be connected.\r
1562\r
1563 @param DevicePath Device Path to check\r
1564\r
1565 @retval TRUE DevicePath points to an Network that is connected\r
1566 @retval FALSE DevicePath does not point to a bootable network\r
1567\r
1568**/\r
1569BOOLEAN\r
1570BdsLibNetworkBootWithMediaPresent (\r
1571 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1572 )\r
1573{\r
1574 EFI_STATUS Status;\r
1575 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1576 EFI_HANDLE Handle;\r
1577 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
1578 BOOLEAN MediaPresent;\r
e51e619e 1579 UINT32 InterruptStatus;\r
5c08e117 1580\r
1581 MediaPresent = FALSE;\r
1582\r
1583 UpdatedDevicePath = DevicePath;\r
ff482c56 1584 //\r
a7a523e0 1585 // Locate Load File Protocol for PXE boot option first\r
ff482c56 1586 //\r
a7a523e0 1587 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 1588 if (EFI_ERROR (Status)) {\r
1589 //\r
1590 // Device not present so see if we need to connect it\r
1591 //\r
1592 Status = BdsLibConnectDevicePath (DevicePath);\r
1593 if (!EFI_ERROR (Status)) {\r
1594 //\r
1595 // This one should work after we did the connect\r
1596 //\r
a7a523e0 1597 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 1598 }\r
1599 }\r
1600\r
1601 if (!EFI_ERROR (Status)) {\r
1602 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
ff482c56 1603 if (EFI_ERROR (Status)) {\r
1604 //\r
1605 // Failed to open SNP from this handle, try to get SNP from parent handle\r
1606 //\r
1607 UpdatedDevicePath = DevicePathFromHandle (Handle);\r
1608 if (UpdatedDevicePath != NULL) {\r
1609 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
1610 if (!EFI_ERROR (Status)) {\r
1611 //\r
1612 // SNP handle found, get SNP from it\r
1613 //\r
1614 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r
1615 }\r
1616 }\r
1617 }\r
1618\r
5c08e117 1619 if (!EFI_ERROR (Status)) {\r
1620 if (Snp->Mode->MediaPresentSupported) {\r
1621 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
e51e619e 1622 //\r
1623 // Invoke Snp->GetStatus() to refresh the media status\r
1624 //\r
1625 Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
1626\r
5c08e117 1627 //\r
1628 // In case some one else is using the SNP check to see if it's connected\r
1629 //\r
1630 MediaPresent = Snp->Mode->MediaPresent;\r
1631 } else {\r
1632 //\r
1633 // No one is using SNP so we need to Start and Initialize so\r
1634 // MediaPresent will be valid.\r
1635 //\r
1636 Status = Snp->Start (Snp);\r
1637 if (!EFI_ERROR (Status)) {\r
1638 Status = Snp->Initialize (Snp, 0, 0);\r
1639 if (!EFI_ERROR (Status)) {\r
1640 MediaPresent = Snp->Mode->MediaPresent;\r
1641 Snp->Shutdown (Snp);\r
1642 }\r
1643 Snp->Stop (Snp);\r
1644 }\r
1645 }\r
1646 } else {\r
1647 MediaPresent = TRUE;\r
1648 }\r
1649 }\r
1650 }\r
1651\r
1652 return MediaPresent;\r
1653}\r
1654\r
1655/**\r
1656 For a bootable Device path, return its boot type.\r
1657\r
1658 @param DevicePath The bootable device Path to check\r
1659\r
128efbbc 1660 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 1661 which subtype is MEDIA_HARDDRIVE_DP\r
1662 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
1663 which subtype is MEDIA_CDROM_DP\r
1664 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
1665 which HID is floppy device.\r
1666 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1667 and its last device path node's subtype is MSG_ATAPI_DP.\r
1668 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1669 and its last device path node's subtype is MSG_SCSI_DP.\r
1670 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1671 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 1672 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 1673 its last device path node point to a message device path node.\r
1674 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 1675 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 1676\r
1677**/\r
1678UINT32\r
1679EFIAPI\r
1680BdsGetBootTypeFromDevicePath (\r
1681 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1682 )\r
1683{\r
1684 ACPI_HID_DEVICE_PATH *Acpi;\r
1685 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1686 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
ff482c56 1687 UINT32 BootType;\r
5c08e117 1688\r
1689 if (NULL == DevicePath) {\r
1690 return BDS_EFI_UNSUPPORT;\r
1691 }\r
1692\r
1693 TempDevicePath = DevicePath;\r
1694\r
1695 while (!IsDevicePathEndType (TempDevicePath)) {\r
1696 switch (DevicePathType (TempDevicePath)) {\r
1697 case BBS_DEVICE_PATH:\r
1698 return BDS_LEGACY_BBS_BOOT;\r
1699 case MEDIA_DEVICE_PATH:\r
1700 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
1701 return BDS_EFI_MEDIA_HD_BOOT;\r
1702 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
1703 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 1704 }\r
5c08e117 1705 break;\r
1706 case ACPI_DEVICE_PATH:\r
1707 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
1708 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
1709 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
1710 }\r
1711 break;\r
1712 case MESSAGING_DEVICE_PATH:\r
1713 //\r
1714 // Get the last device path node\r
1715 //\r
1716 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
1717 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
1718 //\r
1719 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
ff482c56 1720 // skip it\r
5c08e117 1721 //\r
1722 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
1723 }\r
1724 //\r
1725 // if the device path not only point to driver device, it is not a messaging device path,\r
1726 //\r
1727 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 1728 break;\r
5c08e117 1729 }\r
1730\r
ff482c56 1731 switch (DevicePathSubType (TempDevicePath)) {\r
1732 case MSG_ATAPI_DP:\r
1733 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r
1734 break;\r
1735\r
1736 case MSG_USB_DP:\r
1737 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
1738 break;\r
1739\r
1740 case MSG_SCSI_DP:\r
1741 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r
1742 break;\r
1743\r
1744 case MSG_SATA_DP:\r
1745 BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r
1746 break;\r
1747\r
1748 case MSG_MAC_ADDR_DP:\r
1749 case MSG_VLAN_DP:\r
a7a523e0 1750 case MSG_IPv4_DP:\r
1751 case MSG_IPv6_DP:\r
ff482c56 1752 BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r
1753 break;\r
1754\r
1755 default:\r
1756 BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r
1757 break;\r
5c08e117 1758 }\r
ff482c56 1759 return BootType;\r
1760\r
5c08e117 1761 default:\r
1762 break;\r
1763 }\r
1764 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1765 }\r
1766\r
1767 return BDS_EFI_UNSUPPORT;\r
1768}\r
1769\r
1770/**\r
1771 Check whether the Device path in a boot option point to a valid bootable device,\r
1772 And if CheckMedia is true, check the device is ready to boot now.\r
1773\r
1774 @param DevPath the Device path in a boot option\r
1775 @param CheckMedia if true, check the device is ready to boot now.\r
1776\r
1777 @retval TRUE the Device path is valid\r
1778 @retval FALSE the Device path is invalid .\r
1779\r
1780**/\r
1781BOOLEAN\r
1782EFIAPI\r
1783BdsLibIsValidEFIBootOptDevicePath (\r
1784 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1785 IN BOOLEAN CheckMedia\r
1786 )\r
3384a9bc 1787{\r
1788 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
1789}\r
1790\r
1791/**\r
1792 Check whether the Device path in a boot option point to a valid bootable device,\r
1793 And if CheckMedia is true, check the device is ready to boot now.\r
1794 If Description is not NULL and the device path point to a fixed BlockIo\r
1795 device, check the description whether conflict with other auto-created\r
1796 boot options.\r
1797\r
1798 @param DevPath the Device path in a boot option\r
1799 @param CheckMedia if true, check the device is ready to boot now.\r
1800 @param Description the description in a boot option\r
1801\r
1802 @retval TRUE the Device path is valid\r
1803 @retval FALSE the Device path is invalid .\r
1804\r
1805**/\r
1806BOOLEAN\r
1807EFIAPI\r
1808BdsLibIsValidEFIBootOptDevicePathExt (\r
1809 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1810 IN BOOLEAN CheckMedia,\r
1811 IN CHAR16 *Description\r
1812 )\r
5c08e117 1813{\r
1814 EFI_STATUS Status;\r
1815 EFI_HANDLE Handle;\r
1816 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1817 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1818 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1819\r
1820 TempDevicePath = DevPath;\r
1821 LastDeviceNode = DevPath;\r
128efbbc 1822\r
5c08e117 1823 //\r
a7a523e0 1824 // Check if it's a valid boot option for network boot device.\r
1825 // Check if there is EfiLoadFileProtocol installed. \r
1826 // If yes, that means there is a boot option for network.\r
5c08e117 1827 //\r
1828 Status = gBS->LocateDevicePath (\r
a7a523e0 1829 &gEfiLoadFileProtocolGuid,\r
5c08e117 1830 &TempDevicePath,\r
1831 &Handle\r
1832 );\r
1833 if (EFI_ERROR (Status)) {\r
1834 //\r
1835 // Device not present so see if we need to connect it\r
1836 //\r
1837 TempDevicePath = DevPath;\r
1838 BdsLibConnectDevicePath (TempDevicePath);\r
1839 Status = gBS->LocateDevicePath (\r
a7a523e0 1840 &gEfiLoadFileProtocolGuid,\r
5c08e117 1841 &TempDevicePath,\r
1842 &Handle\r
1843 );\r
1844 }\r
128efbbc 1845\r
5c08e117 1846 if (!EFI_ERROR (Status)) {\r
a7a523e0 1847 if (!IsDevicePathEnd (TempDevicePath)) {\r
1848 //\r
1849 // LoadFile protocol is not installed on handle with exactly the same DevPath\r
1850 //\r
1851 return FALSE;\r
1852 }\r
ff482c56 1853\r
a7a523e0 1854 if (CheckMedia) {\r
1855 //\r
1856 // Test if it is ready to boot now\r
1857 //\r
1858 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
5c08e117 1859 return TRUE;\r
1860 }\r
a7a523e0 1861 } else {\r
1862 return TRUE;\r
1863 } \r
5c08e117 1864 }\r
1865\r
1866 //\r
1867 // If the boot option point to a file, it is a valid EFI boot option,\r
1868 // and assume it is ready to boot now\r
1869 //\r
1870 while (!IsDevicePathEnd (TempDevicePath)) {\r
1871 LastDeviceNode = TempDevicePath;\r
1872 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1873 }\r
1874 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
1875 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
1876 return TRUE;\r
1877 }\r
1878\r
1879 //\r
1880 // Check if it's a valid boot option for internal Shell\r
1881 //\r
1882 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
1883 //\r
1884 // If the boot option point to Internal FV shell, make sure it is valid\r
1885 //\r
128efbbc 1886 TempDevicePath = DevPath;\r
d46f3632 1887 Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, PcdGetPtr(PcdShellFile));\r
5c08e117 1888 if (Status == EFI_ALREADY_STARTED) {\r
1889 return TRUE;\r
1890 } else {\r
1891 if (Status == EFI_SUCCESS) {\r
128efbbc 1892 FreePool (TempDevicePath);\r
5c08e117 1893 }\r
1894 return FALSE;\r
1895 }\r
1896 }\r
128efbbc 1897\r
5c08e117 1898 //\r
3384a9bc 1899 // If the boot option point to a blockIO device:\r
8d3b5aff 1900 // if it is a removable blockIo device, it is valid.\r
128efbbc 1901 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 1902 //\r
1903 TempDevicePath = DevPath;\r
1904 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1905 if (EFI_ERROR (Status)) {\r
1906 //\r
1907 // Device not present so see if we need to connect it\r
1908 //\r
1909 Status = BdsLibConnectDevicePath (DevPath);\r
1910 if (!EFI_ERROR (Status)) {\r
1911 //\r
1912 // Try again to get the Block Io protocol after we did the connect\r
1913 //\r
1914 TempDevicePath = DevPath;\r
1915 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1916 }\r
1917 }\r
128efbbc 1918\r
5c08e117 1919 if (!EFI_ERROR (Status)) {\r
1920 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1921 if (!EFI_ERROR (Status)) {\r
1922 if (CheckMedia) {\r
1923 //\r
1924 // Test if it is ready to boot now\r
1925 //\r
1926 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1927 return TRUE;\r
1928 }\r
1929 } else {\r
1930 return TRUE;\r
1931 }\r
1932 }\r
1933 } else {\r
1934 //\r
1935 // 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
1936 //\r
1937 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
1938 if (!EFI_ERROR (Status)) {\r
1939 if (CheckMedia) {\r
1940 //\r
1941 // Test if it is ready to boot now\r
1942 //\r
1943 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1944 return TRUE;\r
1945 }\r
1946 } else {\r
1947 return TRUE;\r
1948 }\r
1949 }\r
1950 }\r
1951\r
1952 return FALSE;\r
1953}\r
1954\r
1955\r
1956/**\r
1957 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
1958 try to return the valid device path.\r
1959 FV address maybe changes for memory layout adjust from time to time, use this function\r
1960 could promise the Fv file device path is right.\r
1961\r
1962 @param DevicePath on input, the Fv file device path need to check on\r
1963 output, the updated valid Fv file device path\r
1964 @param FileGuid the Fv file guild\r
1965\r
1966 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
1967 parameter\r
1968 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
1969 guild at all\r
1970 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
1971 valid\r
1972 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
1973 and return the updated device path in DevicePath\r
1974\r
1975**/\r
1976EFI_STATUS\r
1977EFIAPI\r
1978BdsLibUpdateFvFileDevicePath (\r
1979 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
1980 IN EFI_GUID *FileGuid\r
1981 )\r
1982{\r
1983 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1984 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1985 EFI_STATUS Status;\r
1986 EFI_GUID *GuidPoint;\r
1987 UINTN Index;\r
1988 UINTN FvHandleCount;\r
1989 EFI_HANDLE *FvHandleBuffer;\r
1990 EFI_FV_FILETYPE Type;\r
1991 UINTN Size;\r
1992 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1993 UINT32 AuthenticationStatus;\r
1994 BOOLEAN FindFvFile;\r
1995 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
1996 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1997 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
1998 EFI_HANDLE FoundFvHandle;\r
1999 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
2000\r
2001 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
2002 return EFI_INVALID_PARAMETER;\r
2003 }\r
2004 if (FileGuid == NULL) {\r
2005 return EFI_INVALID_PARAMETER;\r
2006 }\r
128efbbc 2007\r
5c08e117 2008 //\r
2009 // Check whether the device path point to the default the input Fv file\r
2010 //\r
2011 TempDevicePath = *DevicePath;\r
2012 LastDeviceNode = TempDevicePath;\r
2013 while (!IsDevicePathEnd (TempDevicePath)) {\r
2014 LastDeviceNode = TempDevicePath;\r
2015 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
2016 }\r
2017 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
2018 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
2019 );\r
2020 if (GuidPoint == NULL) {\r
2021 //\r
2022 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
2023 //\r
2024 return EFI_UNSUPPORTED;\r
2025 }\r
2026 if (!CompareGuid (GuidPoint, FileGuid)) {\r
2027 //\r
2028 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
2029 //\r
2030 return EFI_UNSUPPORTED;\r
2031 }\r
2032\r
2033 //\r
2034 // Check whether the input Fv file device path is valid\r
2035 //\r
2036 TempDevicePath = *DevicePath;\r
2037 FoundFvHandle = NULL;\r
2038 Status = gBS->LocateDevicePath (\r
2039 &gEfiFirmwareVolume2ProtocolGuid,\r
2040 &TempDevicePath,\r
2041 &FoundFvHandle\r
2042 );\r
2043 if (!EFI_ERROR (Status)) {\r
2044 Status = gBS->HandleProtocol (\r
2045 FoundFvHandle,\r
2046 &gEfiFirmwareVolume2ProtocolGuid,\r
2047 (VOID **) &Fv\r
2048 );\r
2049 if (!EFI_ERROR (Status)) {\r
2050 //\r
2051 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
2052 //\r
2053 Status = Fv->ReadFile (\r
2054 Fv,\r
2055 FileGuid,\r
2056 NULL,\r
2057 &Size,\r
2058 &Type,\r
2059 &Attributes,\r
2060 &AuthenticationStatus\r
2061 );\r
2062 if (!EFI_ERROR (Status)) {\r
2063 return EFI_ALREADY_STARTED;\r
2064 }\r
2065 }\r
2066 }\r
2067\r
2068 //\r
2069 // Look for the input wanted FV file in current FV\r
2070 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
2071 //\r
2072 FindFvFile = FALSE;\r
2073 FoundFvHandle = NULL;\r
2074 Status = gBS->HandleProtocol (\r
2075 mBdsImageHandle,\r
2076 &gEfiLoadedImageProtocolGuid,\r
2077 (VOID **) &LoadedImage\r
2078 );\r
2079 if (!EFI_ERROR (Status)) {\r
2080 Status = gBS->HandleProtocol (\r
2081 LoadedImage->DeviceHandle,\r
2082 &gEfiFirmwareVolume2ProtocolGuid,\r
2083 (VOID **) &Fv\r
2084 );\r
2085 if (!EFI_ERROR (Status)) {\r
2086 Status = Fv->ReadFile (\r
2087 Fv,\r
2088 FileGuid,\r
2089 NULL,\r
2090 &Size,\r
2091 &Type,\r
2092 &Attributes,\r
2093 &AuthenticationStatus\r
2094 );\r
2095 if (!EFI_ERROR (Status)) {\r
2096 FindFvFile = TRUE;\r
2097 FoundFvHandle = LoadedImage->DeviceHandle;\r
2098 }\r
2099 }\r
2100 }\r
2101 //\r
2102 // Second, if fail to find, try to enumerate all FV\r
2103 //\r
2104 if (!FindFvFile) {\r
2105 FvHandleBuffer = NULL;\r
2106 gBS->LocateHandleBuffer (\r
2107 ByProtocol,\r
2108 &gEfiFirmwareVolume2ProtocolGuid,\r
2109 NULL,\r
2110 &FvHandleCount,\r
2111 &FvHandleBuffer\r
2112 );\r
2113 for (Index = 0; Index < FvHandleCount; Index++) {\r
2114 gBS->HandleProtocol (\r
2115 FvHandleBuffer[Index],\r
2116 &gEfiFirmwareVolume2ProtocolGuid,\r
2117 (VOID **) &Fv\r
2118 );\r
2119\r
2120 Status = Fv->ReadFile (\r
2121 Fv,\r
2122 FileGuid,\r
2123 NULL,\r
2124 &Size,\r
2125 &Type,\r
2126 &Attributes,\r
2127 &AuthenticationStatus\r
2128 );\r
2129 if (EFI_ERROR (Status)) {\r
2130 //\r
2131 // Skip if input Fv file not in the FV\r
2132 //\r
2133 continue;\r
2134 }\r
2135 FindFvFile = TRUE;\r
2136 FoundFvHandle = FvHandleBuffer[Index];\r
2137 break;\r
2138 }\r
2139\r
2140 if (FvHandleBuffer != NULL) {\r
128efbbc 2141 FreePool (FvHandleBuffer);\r
5c08e117 2142 }\r
2143 }\r
2144\r
2145 if (FindFvFile) {\r
2146 //\r
2147 // Build the shell device path\r
2148 //\r
2149 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
2150 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
2151 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
2152 *DevicePath = NewDevicePath;\r
2153 return EFI_SUCCESS;\r
2154 }\r
2155 return EFI_NOT_FOUND;\r
2156}\r