]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
For network dynamic media detect support: invoke Snp->GetStatus() before use Snp...
[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
1ace0001 4Copyright (c) 2004 - 2010, Intel Corporation. <BR>\r
5c08e117 5All rights reserved. This program and the accompanying materials\r
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
202 //\r
203 // Notes: put EFI64 ROM Shadow Solution\r
204 //\r
205 EFI64_SHADOW_ALL_LEGACY_ROM ();\r
206\r
207 //\r
208 // Notes: this code can be remove after the s3 script table\r
209 // hook on the event EVT_SIGNAL_READY_TO_BOOT or\r
210 // EVT_SIGNAL_LEGACY_BOOT\r
211 //\r
212 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);\r
213 if (!EFI_ERROR (Status)) {\r
214 AcpiS3Save->S3Save (AcpiS3Save, NULL);\r
215 }\r
216 //\r
217 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a\r
218 // full device path\r
219 //\r
220 WorkingDevicePath = NULL;\r
221 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
222 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {\r
223 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (\r
224 (HARDDRIVE_DEVICE_PATH *)DevicePath\r
225 );\r
226 if (WorkingDevicePath != NULL) {\r
227 DevicePath = WorkingDevicePath;\r
228 }\r
229 }\r
230 //\r
231 // Signal the EVT_SIGNAL_READY_TO_BOOT event\r
232 //\r
233 EfiSignalEventReadyToBoot();\r
128efbbc 234\r
235\r
5c08e117 236 //\r
237 // Set Boot Current\r
238 //\r
3d7decbc 239 if (IsBootOptionValidNVVarialbe (Option)) {\r
240 //\r
241 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.\r
242 // In this case, "BootCurrent" is not created.\r
243 // Only create the BootCurrent variable when it points to a valid Boot#### variable.\r
244 //\r
245 gRT->SetVariable (\r
246 L"BootCurrent",\r
247 &gEfiGlobalVariableGuid,\r
248 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
249 sizeof (UINT16),\r
250 &Option->BootCurrent\r
251 );\r
252 }\r
5c08e117 253\r
254 ASSERT (Option->DevicePath != NULL);\r
255 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&\r
256 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)\r
257 ) {\r
258 //\r
259 // Check to see if we should legacy BOOT. If yes then do the legacy boot\r
260 //\r
261 return BdsLibDoLegacyBoot (Option);\r
262 }\r
263\r
264 //\r
265 // If the boot option point to Internal FV shell, make sure it is valid\r
266 //\r
d46f3632 267 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));\r
5c08e117 268 if (!EFI_ERROR(Status)) {\r
269 if (Option->DevicePath != NULL) {\r
270 FreePool(Option->DevicePath);\r
271 }\r
272 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));\r
273 ASSERT(Option->DevicePath != NULL);\r
274 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));\r
275 //\r
276 // Update the shell boot option\r
277 //\r
278 InitializeListHead (&TempBootLists);\r
279 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
128efbbc 280\r
5c08e117 281 //\r
282 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()\r
283 //\r
128efbbc 284 FreePool (DevicePath);\r
5c08e117 285 DevicePath = Option->DevicePath;\r
286 }\r
287\r
ab8cc80b 288 DEBUG_CODE_BEGIN();\r
5c08e117 289\r
9aa7ba01 290 if (Option->Description == NULL) {\r
291 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));\r
292 } else {\r
293 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));\r
ab8cc80b 294 }\r
9aa7ba01 295 \r
ab8cc80b 296 DEBUG_CODE_END();\r
297 \r
5c08e117 298 Status = gBS->LoadImage (\r
299 TRUE,\r
300 mBdsImageHandle,\r
301 DevicePath,\r
302 NULL,\r
303 0,\r
304 &ImageHandle\r
305 );\r
306\r
307 //\r
261835cc 308 // If we didn't find an image directly, we need to try as if it is a removable device boot option\r
5c08e117 309 // and load the image according to the default boot behavior for removable device.\r
310 //\r
311 if (EFI_ERROR (Status)) {\r
312 //\r
313 // check if there is a bootable removable media could be found in this device path ,\r
314 // and get the bootable media handle\r
315 //\r
316 Handle = BdsLibGetBootableHandle(DevicePath);\r
317 if (Handle == NULL) {\r
318 goto Done;\r
319 }\r
320 //\r
321 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
322 // machinename is ia32, ia64, x64, ...\r
323 //\r
c62dbf31 324 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);\r
5c08e117 325 if (FilePath != NULL) {\r
326 Status = gBS->LoadImage (\r
327 TRUE,\r
328 mBdsImageHandle,\r
329 FilePath,\r
330 NULL,\r
331 0,\r
332 &ImageHandle\r
333 );\r
334 if (EFI_ERROR (Status)) {\r
335 //\r
336 // The DevicePath failed, and it's not a valid\r
337 // removable media device.\r
338 //\r
339 goto Done;\r
340 }\r
341 }\r
342 }\r
343\r
344 if (EFI_ERROR (Status)) {\r
345 //\r
346 // It there is any error from the Boot attempt exit now.\r
347 //\r
348 goto Done;\r
349 }\r
350 //\r
351 // Provide the image with it's load options\r
352 //\r
353 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);\r
354 ASSERT_EFI_ERROR (Status);\r
355\r
356 if (Option->LoadOptionsSize != 0) {\r
357 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;\r
358 ImageInfo->LoadOptions = Option->LoadOptions;\r
359 }\r
360 //\r
361 // Before calling the image, enable the Watchdog Timer for\r
362 // the 5 Minute period\r
363 //\r
364 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);\r
365\r
cd6a3b15 366 //\r
367 // Write boot to OS performance data for UEFI boot\r
368 //\r
369 PERF_CODE (\r
370 WriteBootToOsPerformanceData ();\r
371 );\r
372\r
5c08e117 373 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);\r
374 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));\r
375\r
376 //\r
377 // Clear the Watchdog Timer after the image returns\r
378 //\r
379 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);\r
380\r
381Done:\r
382 //\r
383 // Clear Boot Current\r
384 //\r
385 gRT->SetVariable (\r
386 L"BootCurrent",\r
387 &gEfiGlobalVariableGuid,\r
388 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
389 0,\r
390 &Option->BootCurrent\r
391 );\r
392\r
393 return Status;\r
394}\r
395\r
396\r
397/**\r
398 Expand a device path that starts with a hard drive media device path node to be a\r
399 full device path that includes the full hardware path to the device. We need\r
400 to do this so it can be booted. As an optimization the front match (the part point\r
401 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable\r
402 so a connect all is not required on every boot. All successful history device path\r
403 which point to partition node (the front part) will be saved.\r
404\r
405 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard\r
406 drive media device path.\r
407 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path\r
408 cannot be found.\r
409\r
410**/\r
411EFI_DEVICE_PATH_PROTOCOL *\r
412EFIAPI\r
413BdsExpandPartitionPartialDevicePathToFull (\r
414 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
415 )\r
416{\r
417 EFI_STATUS Status;\r
418 UINTN BlockIoHandleCount;\r
419 EFI_HANDLE *BlockIoBuffer;\r
420 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;\r
421 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;\r
422 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
423 UINTN Index;\r
424 UINTN InstanceNum;\r
425 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;\r
426 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;\r
427 UINTN CachedDevicePathSize;\r
428 BOOLEAN DeviceExist;\r
429 BOOLEAN NeedAdjust;\r
430 EFI_DEVICE_PATH_PROTOCOL *Instance;\r
431 UINTN Size;\r
432\r
433 FullDevicePath = NULL;\r
434 //\r
435 // Check if there is prestore 'HDDP' variable.\r
436 // If exist, search the front path which point to partition node in the variable instants.\r
437 // If fail to find or 'HDDP' not exist, reconnect all and search in all system\r
438 //\r
439 CachedDevicePath = BdsLibGetVariableAndSize (\r
440 L"HDDP",\r
441 &mHdBootVariablePrivateGuid,\r
442 &CachedDevicePathSize\r
443 );\r
128efbbc 444\r
5c08e117 445 if (CachedDevicePath != NULL) {\r
446 TempNewDevicePath = CachedDevicePath;\r
447 DeviceExist = FALSE;\r
448 NeedAdjust = FALSE;\r
449 do {\r
450 //\r
451 // Check every instance of the variable\r
452 // First, check whether the instance contain the partition node, which is needed for distinguishing multi\r
453 // partial partition boot option. Second, check whether the instance could be connected.\r
454 //\r
455 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);\r
456 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {\r
457 //\r
458 // Connect the device path instance, the device path point to hard drive media device path node\r
459 // e.g. ACPI() /PCI()/ATA()/Partition()\r
460 //\r
461 Status = BdsLibConnectDevicePath (Instance);\r
462 if (!EFI_ERROR (Status)) {\r
463 DeviceExist = TRUE;\r
464 break;\r
465 }\r
466 }\r
467 //\r
468 // Come here means the first instance is not matched\r
469 //\r
470 NeedAdjust = TRUE;\r
471 FreePool(Instance);\r
472 } while (TempNewDevicePath != NULL);\r
473\r
474 if (DeviceExist) {\r
475 //\r
476 // Find the matched device path.\r
477 // Append the file path information from the boot option and return the fully expanded device path.\r
478 //\r
479 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
480 FullDevicePath = AppendDevicePath (Instance, DevicePath);\r
481\r
482 //\r
483 // Adjust the 'HDDP' instances sequence if the matched one is not first one.\r
484 //\r
485 if (NeedAdjust) {\r
486 //\r
487 // First delete the matched instance.\r
488 //\r
489 TempNewDevicePath = CachedDevicePath;\r
490 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );\r
491 FreePool (TempNewDevicePath);\r
128efbbc 492\r
5c08e117 493 //\r
494 // Second, append the remaining path after the matched instance\r
495 //\r
496 TempNewDevicePath = CachedDevicePath;\r
497 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );\r
498 FreePool (TempNewDevicePath);\r
499 //\r
500 // Save the matching Device Path so we don't need to do a connect all next time\r
501 //\r
502 Status = gRT->SetVariable (\r
503 L"HDDP",\r
504 &mHdBootVariablePrivateGuid,\r
505 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
506 GetDevicePathSize (CachedDevicePath),\r
507 CachedDevicePath\r
508 );\r
509 }\r
128efbbc 510\r
5c08e117 511 FreePool (Instance);\r
512 FreePool (CachedDevicePath);\r
513 return FullDevicePath;\r
514 }\r
515 }\r
516\r
517 //\r
518 // If we get here we fail to find or 'HDDP' not exist, and now we need\r
519 // to search all devices in the system for a matched partition\r
520 //\r
521 BdsLibConnectAllDriversToAllControllers ();\r
522 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);\r
523 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {\r
524 //\r
525 // If there was an error or there are no device handles that support\r
526 // the BLOCK_IO Protocol, then return.\r
527 //\r
528 return NULL;\r
529 }\r
530 //\r
531 // Loop through all the device handles that support the BLOCK_IO Protocol\r
532 //\r
533 for (Index = 0; Index < BlockIoHandleCount; Index++) {\r
534\r
535 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);\r
536 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {\r
537 continue;\r
538 }\r
539\r
540 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {\r
541 //\r
542 // Find the matched partition device path\r
543 //\r
544 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);\r
545 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);\r
546\r
547 //\r
548 // Save the matched partition device path in 'HDDP' variable\r
549 //\r
550 if (CachedDevicePath != NULL) {\r
551 //\r
552 // Save the matched partition device path as first instance of 'HDDP' variable\r
553 //\r
554 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {\r
555 TempNewDevicePath = CachedDevicePath;\r
556 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);\r
557 FreePool(TempNewDevicePath);\r
558\r
559 TempNewDevicePath = CachedDevicePath;\r
560 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
cd730ec0 561 if (TempNewDevicePath != NULL) {\r
562 FreePool(TempNewDevicePath);\r
563 }\r
5c08e117 564 } else {\r
565 TempNewDevicePath = CachedDevicePath;\r
566 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);\r
567 FreePool(TempNewDevicePath);\r
568 }\r
569 //\r
570 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller\r
571 // If the user try to boot many OS in different HDs or partitions, in theory, the 'HDDP' variable maybe become larger and larger.\r
572 //\r
573 InstanceNum = 0;\r
574 ASSERT (CachedDevicePath != NULL);\r
575 TempNewDevicePath = CachedDevicePath;\r
576 while (!IsDevicePathEnd (TempNewDevicePath)) {\r
577 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
578 //\r
579 // Parse one instance\r
580 //\r
581 while (!IsDevicePathEndType (TempNewDevicePath)) {\r
582 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);\r
583 }\r
584 InstanceNum++;\r
585 //\r
586 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.\r
587 //\r
588 if (InstanceNum >= 12) {\r
589 SetDevicePathEndNode (TempNewDevicePath);\r
590 break;\r
591 }\r
592 }\r
593 } else {\r
594 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);\r
595 }\r
596\r
597 //\r
598 // Save the matching Device Path so we don't need to do a connect all next time\r
599 //\r
600 Status = gRT->SetVariable (\r
601 L"HDDP",\r
602 &mHdBootVariablePrivateGuid,\r
603 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
604 GetDevicePathSize (CachedDevicePath),\r
605 CachedDevicePath\r
606 );\r
607\r
608 break;\r
609 }\r
610 }\r
128efbbc 611\r
cd730ec0 612 if (CachedDevicePath != NULL) {\r
613 FreePool (CachedDevicePath);\r
614 }\r
5c08e117 615 if (BlockIoBuffer != NULL) {\r
616 FreePool (BlockIoBuffer);\r
617 }\r
618 return FullDevicePath;\r
619}\r
620\r
621/**\r
622 Check whether there is a instance in BlockIoDevicePath, which contain multi device path\r
623 instances, has the same partition node with HardDriveDevicePath device path\r
624\r
625 @param BlockIoDevicePath Multi device path instances which need to check\r
626 @param HardDriveDevicePath A device path which starts with a hard drive media\r
627 device path.\r
628\r
629 @retval TRUE There is a matched device path instance.\r
630 @retval FALSE There is no matched device path instance.\r
631\r
632**/\r
633BOOLEAN\r
634EFIAPI\r
635MatchPartitionDevicePathNode (\r
636 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,\r
637 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath\r
638 )\r
639{\r
640 HARDDRIVE_DEVICE_PATH *TmpHdPath;\r
641 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
642 BOOLEAN Match;\r
643 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;\r
644\r
645 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {\r
646 return FALSE;\r
647 }\r
128efbbc 648\r
5c08e117 649 //\r
650 // Make PreviousDevicePath == the device path node before the end node\r
651 //\r
652 DevicePath = BlockIoDevicePath;\r
653 BlockIoHdDevicePathNode = NULL;\r
654\r
655 //\r
656 // find the partition device path node\r
657 //\r
658 while (!IsDevicePathEnd (DevicePath)) {\r
659 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&\r
660 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)\r
661 ) {\r
662 BlockIoHdDevicePathNode = DevicePath;\r
663 break;\r
664 }\r
665\r
666 DevicePath = NextDevicePathNode (DevicePath);\r
667 }\r
668\r
669 if (BlockIoHdDevicePathNode == NULL) {\r
670 return FALSE;\r
671 }\r
672 //\r
673 // See if the harddrive device path in blockio matches the orig Hard Drive Node\r
674 //\r
675 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;\r
676 Match = FALSE;\r
128efbbc 677\r
5c08e117 678 //\r
679 // Check for the match\r
680 //\r
681 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&\r
682 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {\r
683 switch (TmpHdPath->SignatureType) {\r
684 case SIGNATURE_TYPE_GUID:\r
685 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);\r
686 break;\r
687 case SIGNATURE_TYPE_MBR:\r
688 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));\r
689 break;\r
690 default:\r
691 Match = FALSE;\r
692 break;\r
693 }\r
694 }\r
695\r
696 return Match;\r
697}\r
698\r
699/**\r
700 Delete the boot option associated with the handle passed in.\r
701\r
702 @param Handle The handle which present the device path to create\r
703 boot option\r
704\r
705 @retval EFI_SUCCESS Delete the boot option success\r
706 @retval EFI_NOT_FOUND If the Device Path is not found in the system\r
707 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
708 @retval Other Error return value from SetVariable()\r
709\r
710**/\r
711EFI_STATUS\r
712BdsLibDeleteOptionFromHandle (\r
713 IN EFI_HANDLE Handle\r
714 )\r
715{\r
716 UINT16 *BootOrder;\r
717 UINT8 *BootOptionVar;\r
718 UINTN BootOrderSize;\r
719 UINTN BootOptionSize;\r
720 EFI_STATUS Status;\r
721 UINTN Index;\r
722 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
723 UINTN DevicePathSize;\r
724 UINTN OptionDevicePathSize;\r
725 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
726 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
727 UINT8 *TempPtr;\r
728\r
729 Status = EFI_SUCCESS;\r
730 BootOrder = NULL;\r
731 BootOrderSize = 0;\r
732\r
733 //\r
734 // Check "BootOrder" variable, if no, means there is no any boot order.\r
735 //\r
736 BootOrder = BdsLibGetVariableAndSize (\r
737 L"BootOrder",\r
738 &gEfiGlobalVariableGuid,\r
739 &BootOrderSize\r
740 );\r
741 if (BootOrder == NULL) {\r
742 return EFI_NOT_FOUND;\r
743 }\r
744\r
745 //\r
746 // Convert device handle to device path protocol instance\r
747 //\r
748 DevicePath = DevicePathFromHandle (Handle);\r
749 if (DevicePath == NULL) {\r
750 return EFI_NOT_FOUND;\r
751 }\r
752 DevicePathSize = GetDevicePathSize (DevicePath);\r
753\r
754 //\r
755 // Loop all boot order variable and find the matching device path\r
756 //\r
757 Index = 0;\r
758 while (Index < BootOrderSize / sizeof (UINT16)) {\r
759 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
760 BootOptionVar = BdsLibGetVariableAndSize (\r
761 BootOption,\r
762 &gEfiGlobalVariableGuid,\r
763 &BootOptionSize\r
764 );\r
128efbbc 765\r
5c08e117 766 if (BootOptionVar == NULL) {\r
767 FreePool (BootOrder);\r
768 return EFI_OUT_OF_RESOURCES;\r
769 }\r
770\r
771 TempPtr = BootOptionVar;\r
772 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
773 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
774 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
775 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);\r
776\r
777 //\r
778 // Check whether the device path match\r
779 //\r
780 if ((OptionDevicePathSize == DevicePathSize) &&\r
781 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {\r
782 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);\r
783 FreePool (BootOptionVar);\r
784 break;\r
785 }\r
786\r
787 FreePool (BootOptionVar);\r
788 Index++;\r
789 }\r
790\r
791 //\r
792 // Adjust number of boot option for "BootOrder" variable.\r
793 //\r
794 Status = gRT->SetVariable (\r
795 L"BootOrder",\r
796 &gEfiGlobalVariableGuid,\r
797 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
798 BootOrderSize,\r
799 BootOrder\r
800 );\r
801\r
802 FreePool (BootOrder);\r
803\r
804 return Status;\r
805}\r
806\r
807\r
808/**\r
3384a9bc 809 Delete all invalid EFI boot options.\r
5c08e117 810\r
811 @retval EFI_SUCCESS Delete all invalid boot option success\r
812 @retval EFI_NOT_FOUND Variable "BootOrder" is not found\r
813 @retval EFI_OUT_OF_RESOURCES Lack of memory resource\r
814 @retval Other Error return value from SetVariable()\r
815\r
816**/\r
817EFI_STATUS\r
818BdsDeleteAllInvalidEfiBootOption (\r
819 VOID\r
820 )\r
821{\r
822 UINT16 *BootOrder;\r
823 UINT8 *BootOptionVar;\r
824 UINTN BootOrderSize;\r
825 UINTN BootOptionSize;\r
826 EFI_STATUS Status;\r
827 UINTN Index;\r
828 UINTN Index2;\r
829 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];\r
830 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;\r
831 UINT8 *TempPtr;\r
3384a9bc 832 CHAR16 *Description;\r
5c08e117 833\r
834 Status = EFI_SUCCESS;\r
835 BootOrder = NULL;\r
836 BootOrderSize = 0;\r
837\r
838 //\r
839 // Check "BootOrder" variable firstly, this variable hold the number of boot options\r
840 //\r
841 BootOrder = BdsLibGetVariableAndSize (\r
842 L"BootOrder",\r
843 &gEfiGlobalVariableGuid,\r
844 &BootOrderSize\r
845 );\r
846 if (NULL == BootOrder) {\r
847 return EFI_NOT_FOUND;\r
848 }\r
849\r
850 Index = 0;\r
851 while (Index < BootOrderSize / sizeof (UINT16)) {\r
852 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);\r
853 BootOptionVar = BdsLibGetVariableAndSize (\r
854 BootOption,\r
855 &gEfiGlobalVariableGuid,\r
856 &BootOptionSize\r
857 );\r
858 if (NULL == BootOptionVar) {\r
859 FreePool (BootOrder);\r
860 return EFI_OUT_OF_RESOURCES;\r
861 }\r
862\r
863 TempPtr = BootOptionVar;\r
864 TempPtr += sizeof (UINT32) + sizeof (UINT16);\r
3384a9bc 865 Description = (CHAR16 *) TempPtr;\r
5c08e117 866 TempPtr += StrSize ((CHAR16 *) TempPtr);\r
867 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;\r
868\r
869 //\r
870 // Skip legacy boot option (BBS boot device)\r
871 //\r
872 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&\r
873 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {\r
874 FreePool (BootOptionVar);\r
875 Index++;\r
876 continue;\r
877 }\r
878\r
3384a9bc 879 if (!BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {\r
5c08e117 880 //\r
881 // Delete this invalid boot option "Boot####"\r
882 //\r
883 Status = gRT->SetVariable (\r
884 BootOption,\r
885 &gEfiGlobalVariableGuid,\r
886 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
887 0,\r
888 NULL\r
889 );\r
890 //\r
891 // Mark this boot option in boot order as deleted\r
892 //\r
893 BootOrder[Index] = 0xffff;\r
894 }\r
895\r
896 FreePool (BootOptionVar);\r
897 Index++;\r
898 }\r
899\r
900 //\r
901 // Adjust boot order array\r
902 //\r
903 Index2 = 0;\r
904 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {\r
905 if (BootOrder[Index] != 0xffff) {\r
906 BootOrder[Index2] = BootOrder[Index];\r
907 Index2 ++;\r
908 }\r
909 }\r
910 Status = gRT->SetVariable (\r
911 L"BootOrder",\r
912 &gEfiGlobalVariableGuid,\r
913 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
914 Index2 * sizeof (UINT16),\r
915 BootOrder\r
916 );\r
917\r
918 FreePool (BootOrder);\r
919\r
920 return Status;\r
921}\r
922\r
923\r
924/**\r
3384a9bc 925 For EFI boot option, BDS separate them as six types:\r
128efbbc 926 1. Network - The boot option points to the SimpleNetworkProtocol device.\r
3384a9bc 927 Bds will try to automatically create this type boot option when enumerate.\r
128efbbc 928 2. Shell - The boot option points to internal flash shell.\r
3384a9bc 929 Bds will try to automatically create this type boot option when enumerate.\r
930 3. Removable BlockIo - The boot option only points to the removable media\r
931 device, like USB flash disk, DVD, Floppy etc.\r
932 These device should contain a *removable* blockIo\r
933 protocol in their device handle.\r
128efbbc 934 Bds will try to automatically create this type boot option\r
3384a9bc 935 when enumerate.\r
128efbbc 936 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,\r
3384a9bc 937 like HardDisk.\r
938 These device should contain a *fixed* blockIo\r
939 protocol in their device handle.\r
940 BDS will skip fixed blockIo devices, and NOT\r
128efbbc 941 automatically create boot option for them. But BDS\r
942 will help to delete those fixed blockIo boot option,\r
3384a9bc 943 whose description rule conflict with other auto-created\r
944 boot options.\r
128efbbc 945 5. Non-BlockIo Simplefile - The boot option points to a device whose handle\r
3384a9bc 946 has SimpleFileSystem Protocol, but has no blockio\r
947 protocol. These devices do not offer blockIo\r
128efbbc 948 protocol, but BDS still can get the\r
3384a9bc 949 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem\r
950 Protocol.\r
128efbbc 951 6. File - The boot option points to a file. These boot options are usually\r
3384a9bc 952 created by user manually or OS loader. BDS will not delete or modify\r
128efbbc 953 these boot options.\r
954\r
3384a9bc 955 This function will enumerate all possible boot device in the system, and\r
128efbbc 956 automatically create boot options for Network, Shell, Removable BlockIo,\r
3384a9bc 957 and Non-BlockIo Simplefile devices.\r
8d3b5aff 958 It will only execute once of every boot.\r
128efbbc 959\r
5c08e117 960 @param BdsBootOptionList The header of the link list which indexed all\r
961 current boot options\r
962\r
963 @retval EFI_SUCCESS Finished all the boot device enumerate and create\r
964 the boot option base on that boot device\r
965\r
e83c9064 966 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list\r
5c08e117 967**/\r
968EFI_STATUS\r
969EFIAPI\r
970BdsLibEnumerateAllBootOption (\r
971 IN OUT LIST_ENTRY *BdsBootOptionList\r
972 )\r
973{\r
974 EFI_STATUS Status;\r
975 UINT16 FloppyNumber;\r
976 UINT16 CdromNumber;\r
977 UINT16 UsbNumber;\r
978 UINT16 MiscNumber;\r
8d3b5aff 979 UINT16 ScsiNumber;\r
5c08e117 980 UINT16 NonBlockNumber;\r
981 UINTN NumberBlockIoHandles;\r
982 EFI_HANDLE *BlockIoHandles;\r
983 EFI_BLOCK_IO_PROTOCOL *BlkIo;\r
984 UINTN Index;\r
a7a523e0 985 UINTN NumOfLoadFileHandles;\r
986 EFI_HANDLE *LoadFileHandles;\r
5c08e117 987 UINTN FvHandleCount;\r
988 EFI_HANDLE *FvHandleBuffer;\r
989 EFI_FV_FILETYPE Type;\r
990 UINTN Size;\r
991 EFI_FV_FILE_ATTRIBUTES Attributes;\r
992 UINT32 AuthenticationStatus;\r
8d3b5aff 993 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
994 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
5c08e117 995 UINTN DevicePathType;\r
996 CHAR16 Buffer[40];\r
997 EFI_HANDLE *FileSystemHandles;\r
998 UINTN NumberFileSystemHandles;\r
999 BOOLEAN NeedDelete;\r
1000 EFI_IMAGE_DOS_HEADER DosHeader;\r
9aa7ba01 1001 CHAR8 *PlatLang;\r
1002 CHAR8 *LastLang;\r
5c08e117 1003 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
1004 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1005\r
1006 FloppyNumber = 0;\r
1007 CdromNumber = 0;\r
1008 UsbNumber = 0;\r
1009 MiscNumber = 0;\r
8d3b5aff 1010 ScsiNumber = 0;\r
9aa7ba01 1011 PlatLang = NULL;\r
1012 LastLang = NULL;\r
5c08e117 1013 ZeroMem (Buffer, sizeof (Buffer));\r
128efbbc 1014\r
5c08e117 1015 //\r
1016 // If the boot device enumerate happened, just get the boot\r
1017 // device from the boot order variable\r
1018 //\r
1019 if (mEnumBootDevice) {\r
9aa7ba01 1020 LastLang = GetVariable (L"LastEnumLang", &mBdsLibLastLangGuid);\r
1021 PlatLang = GetEfiGlobalVariable (L"PlatformLang");\r
1022 if (LastLang == PlatLang) {\r
1023 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
1024 return Status;\r
1025 } else {\r
1026 Status = gRT->SetVariable (\r
1027 L"LastEnumLang",\r
1028 &mBdsLibLastLangGuid,\r
1029 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1030 sizeof (PlatLang),\r
1031 PlatLang\r
1032 );\r
1033 ASSERT_EFI_ERROR (Status);\r
1034 }\r
5c08e117 1035 }\r
128efbbc 1036\r
5c08e117 1037 //\r
1038 // Notes: this dirty code is to get the legacy boot option from the\r
1039 // BBS table and create to variable as the EFI boot option, it should\r
1040 // be removed after the CSM can provide legacy boot option directly\r
1041 //\r
1042 REFRESH_LEGACY_BOOT_OPTIONS;\r
1043\r
1044 //\r
1045 // Delete invalid boot option\r
1046 //\r
1047 BdsDeleteAllInvalidEfiBootOption ();\r
128efbbc 1048\r
5c08e117 1049 //\r
1050 // Parse removable media\r
1051 //\r
1052 gBS->LocateHandleBuffer (\r
1053 ByProtocol,\r
1054 &gEfiBlockIoProtocolGuid,\r
1055 NULL,\r
1056 &NumberBlockIoHandles,\r
1057 &BlockIoHandles\r
1058 );\r
128efbbc 1059\r
5c08e117 1060 for (Index = 0; Index < NumberBlockIoHandles; Index++) {\r
1061 Status = gBS->HandleProtocol (\r
1062 BlockIoHandles[Index],\r
1063 &gEfiBlockIoProtocolGuid,\r
1064 (VOID **) &BlkIo\r
1065 );\r
1066 if (!EFI_ERROR (Status)) {\r
1067 if (!BlkIo->Media->RemovableMedia) {\r
1068 //\r
1069 // skip the non-removable block devices\r
1070 //\r
1071 continue;\r
1072 }\r
1073 }\r
1074 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);\r
1075 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);\r
1076\r
1077 switch (DevicePathType) {\r
1078 case BDS_EFI_ACPI_FLOPPY_BOOT:\r
9aa7ba01 1079 if (FloppyNumber != 0) {\r
1080 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);\r
1081 } else {\r
1082 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));\r
1083 }\r
5c08e117 1084 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1085 FloppyNumber++;\r
1086 break;\r
128efbbc 1087\r
3384a9bc 1088 //\r
1089 // Assume a removable SATA device should be the DVD/CD device\r
1090 //\r
5c08e117 1091 case BDS_EFI_MESSAGE_ATAPI_BOOT:\r
3384a9bc 1092 case BDS_EFI_MESSAGE_SATA_BOOT:\r
9aa7ba01 1093 if (CdromNumber != 0) {\r
1094 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);\r
1095 } else {\r
1096 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));\r
1097 }\r
1098 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));\r
5c08e117 1099 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1100 CdromNumber++;\r
1101 break;\r
1102\r
1103 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:\r
9aa7ba01 1104 if (UsbNumber != 0) {\r
1105 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);\r
1106 } else {\r
1107 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));\r
1108 }\r
5c08e117 1109 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
1110 UsbNumber++;\r
1111 break;\r
1112\r
1113 case BDS_EFI_MESSAGE_SCSI_BOOT:\r
9aa7ba01 1114 if (ScsiNumber != 0) {\r
1115 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);\r
1116 } else {\r
1117 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));\r
1118 }\r
128efbbc 1119 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
8d3b5aff 1120 ScsiNumber++;\r
5c08e117 1121 break;\r
1122\r
1123 case BDS_EFI_MESSAGE_MISC_BOOT:\r
9aa7ba01 1124 if (MiscNumber != 0) {\r
1125 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);\r
1126 } else {\r
1127 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));\r
1128 }\r
128efbbc 1129 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 1130 MiscNumber++;\r
1131 break;\r
1132\r
1133 default:\r
1134 break;\r
1135 }\r
1136 }\r
1137\r
1138 if (NumberBlockIoHandles != 0) {\r
1139 FreePool (BlockIoHandles);\r
1140 }\r
1141\r
1142 //\r
1143 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.\r
1144 //\r
1145 NonBlockNumber = 0;\r
1146 gBS->LocateHandleBuffer (\r
1147 ByProtocol,\r
1148 &gEfiSimpleFileSystemProtocolGuid,\r
1149 NULL,\r
1150 &NumberFileSystemHandles,\r
1151 &FileSystemHandles\r
1152 );\r
1153 for (Index = 0; Index < NumberFileSystemHandles; Index++) {\r
1154 Status = gBS->HandleProtocol (\r
1155 FileSystemHandles[Index],\r
1156 &gEfiBlockIoProtocolGuid,\r
1157 (VOID **) &BlkIo\r
1158 );\r
1159 if (!EFI_ERROR (Status)) {\r
1160 //\r
1161 // Skip if the file system handle supports a BlkIo protocol,\r
1162 //\r
1163 continue;\r
1164 }\r
1165\r
1166 //\r
1167 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI\r
1168 // machinename is ia32, ia64, x64, ...\r
1169 //\r
1170 Hdr.Union = &HdrData;\r
1171 NeedDelete = TRUE;\r
1172 Status = BdsLibGetImageHeader (\r
1173 FileSystemHandles[Index],\r
c62dbf31 1174 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1175 &DosHeader,\r
1176 Hdr\r
1177 );\r
1178 if (!EFI_ERROR (Status) &&\r
1179 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1180 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1181 NeedDelete = FALSE;\r
1182 }\r
1183\r
1184 if (NeedDelete) {\r
1185 //\r
1186 // No such file or the file is not a EFI application, delete this boot option\r
1187 //\r
1188 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);\r
1189 } else {\r
9aa7ba01 1190 if (NonBlockNumber != 0) {\r
1191 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);\r
1192 } else {\r
1193 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));\r
1194 }\r
5c08e117 1195 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);\r
1196 NonBlockNumber++;\r
1197 }\r
1198 }\r
1199\r
1200 if (NumberFileSystemHandles != 0) {\r
1201 FreePool (FileSystemHandles);\r
1202 }\r
1203\r
1204 //\r
1205 // Parse Network Boot Device\r
1206 //\r
a7a523e0 1207 NumOfLoadFileHandles = 0;\r
ff482c56 1208 //\r
a7a523e0 1209 // Search Load File protocol for PXE boot option.\r
ff482c56 1210 //\r
5c08e117 1211 gBS->LocateHandleBuffer (\r
1212 ByProtocol,\r
a7a523e0 1213 &gEfiLoadFileProtocolGuid,\r
5c08e117 1214 NULL,\r
a7a523e0 1215 &NumOfLoadFileHandles,\r
1216 &LoadFileHandles\r
5c08e117 1217 );\r
8d3b5aff 1218\r
a7a523e0 1219 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {\r
9aa7ba01 1220 if (Index != 0) {\r
1221 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)), Index);\r
1222 } else {\r
1223 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)));\r
1224 }\r
a7a523e0 1225 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);\r
5c08e117 1226 }\r
1227\r
a7a523e0 1228 if (NumOfLoadFileHandles != 0) {\r
1229 FreePool (LoadFileHandles);\r
5c08e117 1230 }\r
1231\r
1232 //\r
1233 // Check if we have on flash shell\r
1234 //\r
1235 gBS->LocateHandleBuffer (\r
1236 ByProtocol,\r
1237 &gEfiFirmwareVolume2ProtocolGuid,\r
1238 NULL,\r
1239 &FvHandleCount,\r
1240 &FvHandleBuffer\r
1241 );\r
1242 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 1243 gBS->HandleProtocol (\r
1244 FvHandleBuffer[Index],\r
1245 &gEfiFirmwareVolume2ProtocolGuid,\r
1246 (VOID **) &Fv\r
1247 );\r
1248\r
1249 Status = Fv->ReadFile (\r
1250 Fv,\r
d46f3632 1251 PcdGetPtr(PcdShellFile),\r
5c08e117 1252 NULL,\r
1253 &Size,\r
1254 &Type,\r
1255 &Attributes,\r
1256 &AuthenticationStatus\r
1257 );\r
1258 if (EFI_ERROR (Status)) {\r
1259 //\r
1260 // Skip if no shell file in the FV\r
1261 //\r
1262 continue;\r
1263 }\r
1264 //\r
1265 // Build the shell boot option\r
1266 //\r
1267 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
1268 }\r
1269\r
1270 if (FvHandleCount != 0) {\r
1271 FreePool (FvHandleBuffer);\r
1272 }\r
1273 //\r
1274 // Make sure every boot only have one time\r
1275 // boot device enumerate\r
1276 //\r
e83c9064 1277 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 1278 mEnumBootDevice = TRUE;\r
1279\r
e83c9064 1280 return Status;\r
5c08e117 1281}\r
1282\r
1283/**\r
1284 Build the boot option with the handle parsed in\r
1285\r
1286 @param Handle The handle which present the device path to create\r
1287 boot option\r
1288 @param BdsBootOptionList The header of the link list which indexed all\r
1289 current boot options\r
1290 @param String The description of the boot option.\r
1291\r
1292**/\r
1293VOID\r
1294EFIAPI\r
1295BdsLibBuildOptionFromHandle (\r
1296 IN EFI_HANDLE Handle,\r
1297 IN LIST_ENTRY *BdsBootOptionList,\r
1298 IN CHAR16 *String\r
1299 )\r
1300{\r
1301 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 1302\r
8d3b5aff 1303 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 1304\r
1305 //\r
1306 // Create and register new boot option\r
1307 //\r
1308 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
1309}\r
1310\r
1311\r
1312/**\r
1313 Build the on flash shell boot option with the handle parsed in.\r
1314\r
1315 @param Handle The handle which present the device path to create\r
1316 on flash shell boot option\r
1317 @param BdsBootOptionList The header of the link list which indexed all\r
1318 current boot options\r
1319\r
1320**/\r
1321VOID\r
1322EFIAPI\r
1323BdsLibBuildOptionFromShell (\r
1324 IN EFI_HANDLE Handle,\r
1325 IN OUT LIST_ENTRY *BdsBootOptionList\r
1326 )\r
1327{\r
1328 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1329 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
1330\r
1331 DevicePath = DevicePathFromHandle (Handle);\r
1332\r
1333 //\r
1334 // Build the shell device path\r
1335 //\r
d46f3632 1336 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 1337\r
1338 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
1339\r
1340 //\r
1341 // Create and register the shell boot option\r
1342 //\r
1343 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
1344\r
1345}\r
1346\r
1347/**\r
1348 Boot from the UEFI spec defined "BootNext" variable.\r
1349\r
1350**/\r
1351VOID\r
1352EFIAPI\r
1353BdsLibBootNext (\r
1354 VOID\r
1355 )\r
1356{\r
1357 UINT16 *BootNext;\r
1358 UINTN BootNextSize;\r
1359 CHAR16 Buffer[20];\r
1360 BDS_COMMON_OPTION *BootOption;\r
1361 LIST_ENTRY TempList;\r
1362 UINTN ExitDataSize;\r
1363 CHAR16 *ExitData;\r
1364\r
1365 //\r
1366 // Init the boot option name buffer and temp link list\r
1367 //\r
1368 InitializeListHead (&TempList);\r
1369 ZeroMem (Buffer, sizeof (Buffer));\r
1370\r
1371 BootNext = BdsLibGetVariableAndSize (\r
1372 L"BootNext",\r
1373 &gEfiGlobalVariableGuid,\r
1374 &BootNextSize\r
1375 );\r
1376\r
1377 //\r
1378 // Clear the boot next variable first\r
1379 //\r
1380 if (BootNext != NULL) {\r
1381 gRT->SetVariable (\r
1382 L"BootNext",\r
1383 &gEfiGlobalVariableGuid,\r
1384 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1385 0,\r
1386 BootNext\r
1387 );\r
1388\r
1389 //\r
1390 // Start to build the boot option and try to boot\r
1391 //\r
1392 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
1393 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
1394 ASSERT (BootOption != NULL);\r
1395 BdsLibConnectDevicePath (BootOption->DevicePath);\r
1396 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
1397 }\r
1398\r
1399}\r
1400\r
1401/**\r
1402 Return the bootable media handle.\r
1403 First, check the device is connected\r
1404 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
1405 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
1406\r
e83c9064 1407 @param DevicePath Device Path to a bootable device\r
5c08e117 1408\r
e83c9064 1409 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 1410\r
1411**/\r
1412EFI_HANDLE\r
1413EFIAPI\r
1414BdsLibGetBootableHandle (\r
1415 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1416 )\r
1417{\r
1418 EFI_STATUS Status;\r
1419 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1420 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
1421 EFI_HANDLE Handle;\r
1422 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1423 VOID *Buffer;\r
1424 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1425 UINTN Size;\r
1426 UINTN TempSize;\r
1427 EFI_HANDLE ReturnHandle;\r
1428 EFI_HANDLE *SimpleFileSystemHandles;\r
1429\r
1430 UINTN NumberSimpleFileSystemHandles;\r
1431 UINTN Index;\r
1432 EFI_IMAGE_DOS_HEADER DosHeader;\r
1433 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
1434 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1435\r
1436 UpdatedDevicePath = DevicePath;\r
128efbbc 1437\r
5c08e117 1438 //\r
1439 // Check whether the device is connected\r
1440 //\r
1441 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
1442 if (EFI_ERROR (Status)) {\r
1443 //\r
1444 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
1445 //\r
1446 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
1447 if (EFI_ERROR (Status)) {\r
1448 //\r
1449 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
1450 //\r
1451 UpdatedDevicePath = DevicePath;\r
1452 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1453 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
1454 }\r
1455 } else {\r
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