]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Update the copyright notice format
[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
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
e74f510b
RN
1456 //\r
1457 // For removable device boot option, its contained device path only point to the removable device handle, \r
1458 // should make sure all its children handles (its child partion or media handles) are created and connected. \r
1459 //\r
1460 gBS->ConnectController (Handle, NULL, NULL, TRUE); \r
5c08e117 1461 //\r
1462 // Get BlockIo protocol and check removable attribute\r
1463 //\r
1464 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1465 //\r
1466 // Issue a dummy read to the device to check for media change.\r
1467 // When the removable media is changed, any Block IO read/write will\r
1468 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
1469 // returned. After the Block IO protocol is reinstalled, subsequent\r
1470 // Block IO read/write will success.\r
1471 //\r
1472 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
1473 if (Buffer != NULL) {\r
1474 BlockIo->ReadBlocks (\r
1475 BlockIo,\r
1476 BlockIo->Media->MediaId,\r
1477 0,\r
1478 BlockIo->Media->BlockSize,\r
1479 Buffer\r
1480 );\r
1481 FreePool(Buffer);\r
1482 }\r
1483 }\r
1484\r
1485 //\r
1486 // Detect the the default boot file from removable Media\r
1487 //\r
1488\r
1489 //\r
1490 // 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
1491 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
1492 //\r
1493 DupDevicePath = DuplicateDevicePath (DevicePath);\r
1494 ASSERT (DupDevicePath != NULL);\r
128efbbc 1495\r
5c08e117 1496 UpdatedDevicePath = DupDevicePath;\r
1497 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1498 //\r
1499 // 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
1500 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
1501 //\r
1502 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1503 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
1504 //\r
1505 // Remove the usb node, let the device path only point to PCI node\r
1506 //\r
1507 SetDevicePathEndNode (UpdatedDevicePath);\r
1508 UpdatedDevicePath = DupDevicePath;\r
1509 } else {\r
1510 UpdatedDevicePath = DevicePath;\r
1511 }\r
1512\r
1513 //\r
1514 // Get the device path size of boot option\r
1515 //\r
1516 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1517 ReturnHandle = NULL;\r
1518 gBS->LocateHandleBuffer (\r
1519 ByProtocol,\r
1520 &gEfiSimpleFileSystemProtocolGuid,\r
1521 NULL,\r
1522 &NumberSimpleFileSystemHandles,\r
1523 &SimpleFileSystemHandles\r
1524 );\r
1525 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
1526 //\r
1527 // Get the device path size of SimpleFileSystem handle\r
1528 //\r
1529 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
1530 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1531 //\r
1532 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
1533 //\r
1534 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
1535 //\r
1536 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
1537 // machinename is ia32, ia64, x64, ...\r
1538 //\r
1539 Hdr.Union = &HdrData;\r
1540 Status = BdsLibGetImageHeader (\r
1541 SimpleFileSystemHandles[Index],\r
c62dbf31 1542 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1543 &DosHeader,\r
1544 Hdr\r
1545 );\r
1546 if (!EFI_ERROR (Status) &&\r
1547 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1548 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1549 ReturnHandle = SimpleFileSystemHandles[Index];\r
1550 break;\r
1551 }\r
1552 }\r
1553 }\r
1554\r
1555 FreePool(DupDevicePath);\r
1556\r
1557 if (SimpleFileSystemHandles != NULL) {\r
1558 FreePool(SimpleFileSystemHandles);\r
1559 }\r
1560\r
1561 return ReturnHandle;\r
1562}\r
1563\r
1564/**\r
1565 Check to see if the network cable is plugged in. If the DevicePath is not\r
1566 connected it will be connected.\r
1567\r
1568 @param DevicePath Device Path to check\r
1569\r
1570 @retval TRUE DevicePath points to an Network that is connected\r
1571 @retval FALSE DevicePath does not point to a bootable network\r
1572\r
1573**/\r
1574BOOLEAN\r
1575BdsLibNetworkBootWithMediaPresent (\r
1576 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1577 )\r
1578{\r
1579 EFI_STATUS Status;\r
1580 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1581 EFI_HANDLE Handle;\r
1582 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
1583 BOOLEAN MediaPresent;\r
e51e619e 1584 UINT32 InterruptStatus;\r
5c08e117 1585\r
1586 MediaPresent = FALSE;\r
1587\r
1588 UpdatedDevicePath = DevicePath;\r
ff482c56 1589 //\r
a7a523e0 1590 // Locate Load File Protocol for PXE boot option first\r
ff482c56 1591 //\r
a7a523e0 1592 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 1593 if (EFI_ERROR (Status)) {\r
1594 //\r
1595 // Device not present so see if we need to connect it\r
1596 //\r
1597 Status = BdsLibConnectDevicePath (DevicePath);\r
1598 if (!EFI_ERROR (Status)) {\r
1599 //\r
1600 // This one should work after we did the connect\r
1601 //\r
a7a523e0 1602 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);\r
5c08e117 1603 }\r
1604 }\r
1605\r
1606 if (!EFI_ERROR (Status)) {\r
1607 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
ff482c56 1608 if (EFI_ERROR (Status)) {\r
1609 //\r
1610 // Failed to open SNP from this handle, try to get SNP from parent handle\r
1611 //\r
1612 UpdatedDevicePath = DevicePathFromHandle (Handle);\r
1613 if (UpdatedDevicePath != NULL) {\r
1614 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
1615 if (!EFI_ERROR (Status)) {\r
1616 //\r
1617 // SNP handle found, get SNP from it\r
1618 //\r
1619 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);\r
1620 }\r
1621 }\r
1622 }\r
1623\r
5c08e117 1624 if (!EFI_ERROR (Status)) {\r
1625 if (Snp->Mode->MediaPresentSupported) {\r
1626 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
e51e619e 1627 //\r
1628 // Invoke Snp->GetStatus() to refresh the media status\r
1629 //\r
1630 Snp->GetStatus (Snp, &InterruptStatus, NULL);\r
1631\r
5c08e117 1632 //\r
1633 // In case some one else is using the SNP check to see if it's connected\r
1634 //\r
1635 MediaPresent = Snp->Mode->MediaPresent;\r
1636 } else {\r
1637 //\r
1638 // No one is using SNP so we need to Start and Initialize so\r
1639 // MediaPresent will be valid.\r
1640 //\r
1641 Status = Snp->Start (Snp);\r
1642 if (!EFI_ERROR (Status)) {\r
1643 Status = Snp->Initialize (Snp, 0, 0);\r
1644 if (!EFI_ERROR (Status)) {\r
1645 MediaPresent = Snp->Mode->MediaPresent;\r
1646 Snp->Shutdown (Snp);\r
1647 }\r
1648 Snp->Stop (Snp);\r
1649 }\r
1650 }\r
1651 } else {\r
1652 MediaPresent = TRUE;\r
1653 }\r
1654 }\r
1655 }\r
1656\r
1657 return MediaPresent;\r
1658}\r
1659\r
1660/**\r
1661 For a bootable Device path, return its boot type.\r
1662\r
1663 @param DevicePath The bootable device Path to check\r
1664\r
128efbbc 1665 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 1666 which subtype is MEDIA_HARDDRIVE_DP\r
1667 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
1668 which subtype is MEDIA_CDROM_DP\r
1669 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
1670 which HID is floppy device.\r
1671 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1672 and its last device path node's subtype is MSG_ATAPI_DP.\r
1673 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1674 and its last device path node's subtype is MSG_SCSI_DP.\r
1675 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1676 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 1677 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 1678 its last device path node point to a message device path node.\r
1679 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 1680 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 1681\r
1682**/\r
1683UINT32\r
1684EFIAPI\r
1685BdsGetBootTypeFromDevicePath (\r
1686 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1687 )\r
1688{\r
1689 ACPI_HID_DEVICE_PATH *Acpi;\r
1690 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1691 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
ff482c56 1692 UINT32 BootType;\r
5c08e117 1693\r
1694 if (NULL == DevicePath) {\r
1695 return BDS_EFI_UNSUPPORT;\r
1696 }\r
1697\r
1698 TempDevicePath = DevicePath;\r
1699\r
1700 while (!IsDevicePathEndType (TempDevicePath)) {\r
1701 switch (DevicePathType (TempDevicePath)) {\r
1702 case BBS_DEVICE_PATH:\r
1703 return BDS_LEGACY_BBS_BOOT;\r
1704 case MEDIA_DEVICE_PATH:\r
1705 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
1706 return BDS_EFI_MEDIA_HD_BOOT;\r
1707 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
1708 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 1709 }\r
5c08e117 1710 break;\r
1711 case ACPI_DEVICE_PATH:\r
1712 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
1713 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
1714 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
1715 }\r
1716 break;\r
1717 case MESSAGING_DEVICE_PATH:\r
1718 //\r
1719 // Get the last device path node\r
1720 //\r
1721 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
1722 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
1723 //\r
1724 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
ff482c56 1725 // skip it\r
5c08e117 1726 //\r
1727 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
1728 }\r
1729 //\r
1730 // if the device path not only point to driver device, it is not a messaging device path,\r
1731 //\r
1732 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 1733 break;\r
5c08e117 1734 }\r
1735\r
ff482c56 1736 switch (DevicePathSubType (TempDevicePath)) {\r
1737 case MSG_ATAPI_DP:\r
1738 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;\r
1739 break;\r
1740\r
1741 case MSG_USB_DP:\r
1742 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
1743 break;\r
1744\r
1745 case MSG_SCSI_DP:\r
1746 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;\r
1747 break;\r
1748\r
1749 case MSG_SATA_DP:\r
1750 BootType = BDS_EFI_MESSAGE_SATA_BOOT;\r
1751 break;\r
1752\r
1753 case MSG_MAC_ADDR_DP:\r
1754 case MSG_VLAN_DP:\r
a7a523e0 1755 case MSG_IPv4_DP:\r
1756 case MSG_IPv6_DP:\r
ff482c56 1757 BootType = BDS_EFI_MESSAGE_MAC_BOOT;\r
1758 break;\r
1759\r
1760 default:\r
1761 BootType = BDS_EFI_MESSAGE_MISC_BOOT;\r
1762 break;\r
5c08e117 1763 }\r
ff482c56 1764 return BootType;\r
1765\r
5c08e117 1766 default:\r
1767 break;\r
1768 }\r
1769 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1770 }\r
1771\r
1772 return BDS_EFI_UNSUPPORT;\r
1773}\r
1774\r
1775/**\r
1776 Check whether the Device path in a boot option point to a valid bootable device,\r
1777 And if CheckMedia is true, check the device is ready to boot now.\r
1778\r
1779 @param DevPath the Device path in a boot option\r
1780 @param CheckMedia if true, check the device is ready to boot now.\r
1781\r
1782 @retval TRUE the Device path is valid\r
1783 @retval FALSE the Device path is invalid .\r
1784\r
1785**/\r
1786BOOLEAN\r
1787EFIAPI\r
1788BdsLibIsValidEFIBootOptDevicePath (\r
1789 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1790 IN BOOLEAN CheckMedia\r
1791 )\r
3384a9bc 1792{\r
1793 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
1794}\r
1795\r
1796/**\r
1797 Check whether the Device path in a boot option point to a valid bootable device,\r
1798 And if CheckMedia is true, check the device is ready to boot now.\r
1799 If Description is not NULL and the device path point to a fixed BlockIo\r
1800 device, check the description whether conflict with other auto-created\r
1801 boot options.\r
1802\r
1803 @param DevPath the Device path in a boot option\r
1804 @param CheckMedia if true, check the device is ready to boot now.\r
1805 @param Description the description in a boot option\r
1806\r
1807 @retval TRUE the Device path is valid\r
1808 @retval FALSE the Device path is invalid .\r
1809\r
1810**/\r
1811BOOLEAN\r
1812EFIAPI\r
1813BdsLibIsValidEFIBootOptDevicePathExt (\r
1814 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1815 IN BOOLEAN CheckMedia,\r
1816 IN CHAR16 *Description\r
1817 )\r
5c08e117 1818{\r
1819 EFI_STATUS Status;\r
1820 EFI_HANDLE Handle;\r
1821 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1822 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1823 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1824\r
1825 TempDevicePath = DevPath;\r
1826 LastDeviceNode = DevPath;\r
128efbbc 1827\r
5c08e117 1828 //\r
a7a523e0 1829 // Check if it's a valid boot option for network boot device.\r
1830 // Check if there is EfiLoadFileProtocol installed. \r
1831 // If yes, that means there is a boot option for network.\r
5c08e117 1832 //\r
1833 Status = gBS->LocateDevicePath (\r
a7a523e0 1834 &gEfiLoadFileProtocolGuid,\r
5c08e117 1835 &TempDevicePath,\r
1836 &Handle\r
1837 );\r
1838 if (EFI_ERROR (Status)) {\r
1839 //\r
1840 // Device not present so see if we need to connect it\r
1841 //\r
1842 TempDevicePath = DevPath;\r
1843 BdsLibConnectDevicePath (TempDevicePath);\r
1844 Status = gBS->LocateDevicePath (\r
a7a523e0 1845 &gEfiLoadFileProtocolGuid,\r
5c08e117 1846 &TempDevicePath,\r
1847 &Handle\r
1848 );\r
1849 }\r
128efbbc 1850\r
5c08e117 1851 if (!EFI_ERROR (Status)) {\r
a7a523e0 1852 if (!IsDevicePathEnd (TempDevicePath)) {\r
1853 //\r
1854 // LoadFile protocol is not installed on handle with exactly the same DevPath\r
1855 //\r
1856 return FALSE;\r
1857 }\r
ff482c56 1858\r
a7a523e0 1859 if (CheckMedia) {\r
1860 //\r
1861 // Test if it is ready to boot now\r
1862 //\r
1863 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
5c08e117 1864 return TRUE;\r
1865 }\r
a7a523e0 1866 } else {\r
1867 return TRUE;\r
1868 } \r
5c08e117 1869 }\r
1870\r
1871 //\r
1872 // If the boot option point to a file, it is a valid EFI boot option,\r
1873 // and assume it is ready to boot now\r
1874 //\r
1875 while (!IsDevicePathEnd (TempDevicePath)) {\r
1876 LastDeviceNode = TempDevicePath;\r
1877 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1878 }\r
1879 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
1880 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
1881 return TRUE;\r
1882 }\r
1883\r
1884 //\r
1885 // Check if it's a valid boot option for internal Shell\r
1886 //\r
1887 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
1888 //\r
1889 // If the boot option point to Internal FV shell, make sure it is valid\r
1890 //\r
128efbbc 1891 TempDevicePath = DevPath;\r
d46f3632 1892 Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, PcdGetPtr(PcdShellFile));\r
5c08e117 1893 if (Status == EFI_ALREADY_STARTED) {\r
1894 return TRUE;\r
1895 } else {\r
1896 if (Status == EFI_SUCCESS) {\r
128efbbc 1897 FreePool (TempDevicePath);\r
5c08e117 1898 }\r
1899 return FALSE;\r
1900 }\r
1901 }\r
128efbbc 1902\r
5c08e117 1903 //\r
3384a9bc 1904 // If the boot option point to a blockIO device:\r
8d3b5aff 1905 // if it is a removable blockIo device, it is valid.\r
128efbbc 1906 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 1907 //\r
1908 TempDevicePath = DevPath;\r
1909 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1910 if (EFI_ERROR (Status)) {\r
1911 //\r
1912 // Device not present so see if we need to connect it\r
1913 //\r
1914 Status = BdsLibConnectDevicePath (DevPath);\r
1915 if (!EFI_ERROR (Status)) {\r
1916 //\r
1917 // Try again to get the Block Io protocol after we did the connect\r
1918 //\r
1919 TempDevicePath = DevPath;\r
1920 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1921 }\r
1922 }\r
128efbbc 1923\r
5c08e117 1924 if (!EFI_ERROR (Status)) {\r
1925 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1926 if (!EFI_ERROR (Status)) {\r
1927 if (CheckMedia) {\r
1928 //\r
1929 // Test if it is ready to boot now\r
1930 //\r
1931 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1932 return TRUE;\r
1933 }\r
1934 } else {\r
1935 return TRUE;\r
1936 }\r
1937 }\r
1938 } else {\r
1939 //\r
1940 // 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
1941 //\r
1942 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
1943 if (!EFI_ERROR (Status)) {\r
1944 if (CheckMedia) {\r
1945 //\r
1946 // Test if it is ready to boot now\r
1947 //\r
1948 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1949 return TRUE;\r
1950 }\r
1951 } else {\r
1952 return TRUE;\r
1953 }\r
1954 }\r
1955 }\r
1956\r
1957 return FALSE;\r
1958}\r
1959\r
1960\r
1961/**\r
1962 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
1963 try to return the valid device path.\r
1964 FV address maybe changes for memory layout adjust from time to time, use this function\r
1965 could promise the Fv file device path is right.\r
1966\r
1967 @param DevicePath on input, the Fv file device path need to check on\r
1968 output, the updated valid Fv file device path\r
1969 @param FileGuid the Fv file guild\r
1970\r
1971 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
1972 parameter\r
1973 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
1974 guild at all\r
1975 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
1976 valid\r
1977 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
1978 and return the updated device path in DevicePath\r
1979\r
1980**/\r
1981EFI_STATUS\r
1982EFIAPI\r
1983BdsLibUpdateFvFileDevicePath (\r
1984 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
1985 IN EFI_GUID *FileGuid\r
1986 )\r
1987{\r
1988 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1989 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1990 EFI_STATUS Status;\r
1991 EFI_GUID *GuidPoint;\r
1992 UINTN Index;\r
1993 UINTN FvHandleCount;\r
1994 EFI_HANDLE *FvHandleBuffer;\r
1995 EFI_FV_FILETYPE Type;\r
1996 UINTN Size;\r
1997 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1998 UINT32 AuthenticationStatus;\r
1999 BOOLEAN FindFvFile;\r
2000 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
2001 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
2002 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
2003 EFI_HANDLE FoundFvHandle;\r
2004 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
2005\r
2006 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
2007 return EFI_INVALID_PARAMETER;\r
2008 }\r
2009 if (FileGuid == NULL) {\r
2010 return EFI_INVALID_PARAMETER;\r
2011 }\r
128efbbc 2012\r
5c08e117 2013 //\r
2014 // Check whether the device path point to the default the input Fv file\r
2015 //\r
2016 TempDevicePath = *DevicePath;\r
2017 LastDeviceNode = TempDevicePath;\r
2018 while (!IsDevicePathEnd (TempDevicePath)) {\r
2019 LastDeviceNode = TempDevicePath;\r
2020 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
2021 }\r
2022 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
2023 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
2024 );\r
2025 if (GuidPoint == NULL) {\r
2026 //\r
2027 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
2028 //\r
2029 return EFI_UNSUPPORTED;\r
2030 }\r
2031 if (!CompareGuid (GuidPoint, FileGuid)) {\r
2032 //\r
2033 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
2034 //\r
2035 return EFI_UNSUPPORTED;\r
2036 }\r
2037\r
2038 //\r
2039 // Check whether the input Fv file device path is valid\r
2040 //\r
2041 TempDevicePath = *DevicePath;\r
2042 FoundFvHandle = NULL;\r
2043 Status = gBS->LocateDevicePath (\r
2044 &gEfiFirmwareVolume2ProtocolGuid,\r
2045 &TempDevicePath,\r
2046 &FoundFvHandle\r
2047 );\r
2048 if (!EFI_ERROR (Status)) {\r
2049 Status = gBS->HandleProtocol (\r
2050 FoundFvHandle,\r
2051 &gEfiFirmwareVolume2ProtocolGuid,\r
2052 (VOID **) &Fv\r
2053 );\r
2054 if (!EFI_ERROR (Status)) {\r
2055 //\r
2056 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
2057 //\r
2058 Status = Fv->ReadFile (\r
2059 Fv,\r
2060 FileGuid,\r
2061 NULL,\r
2062 &Size,\r
2063 &Type,\r
2064 &Attributes,\r
2065 &AuthenticationStatus\r
2066 );\r
2067 if (!EFI_ERROR (Status)) {\r
2068 return EFI_ALREADY_STARTED;\r
2069 }\r
2070 }\r
2071 }\r
2072\r
2073 //\r
2074 // Look for the input wanted FV file in current FV\r
2075 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
2076 //\r
2077 FindFvFile = FALSE;\r
2078 FoundFvHandle = NULL;\r
2079 Status = gBS->HandleProtocol (\r
2080 mBdsImageHandle,\r
2081 &gEfiLoadedImageProtocolGuid,\r
2082 (VOID **) &LoadedImage\r
2083 );\r
2084 if (!EFI_ERROR (Status)) {\r
2085 Status = gBS->HandleProtocol (\r
2086 LoadedImage->DeviceHandle,\r
2087 &gEfiFirmwareVolume2ProtocolGuid,\r
2088 (VOID **) &Fv\r
2089 );\r
2090 if (!EFI_ERROR (Status)) {\r
2091 Status = Fv->ReadFile (\r
2092 Fv,\r
2093 FileGuid,\r
2094 NULL,\r
2095 &Size,\r
2096 &Type,\r
2097 &Attributes,\r
2098 &AuthenticationStatus\r
2099 );\r
2100 if (!EFI_ERROR (Status)) {\r
2101 FindFvFile = TRUE;\r
2102 FoundFvHandle = LoadedImage->DeviceHandle;\r
2103 }\r
2104 }\r
2105 }\r
2106 //\r
2107 // Second, if fail to find, try to enumerate all FV\r
2108 //\r
2109 if (!FindFvFile) {\r
2110 FvHandleBuffer = NULL;\r
2111 gBS->LocateHandleBuffer (\r
2112 ByProtocol,\r
2113 &gEfiFirmwareVolume2ProtocolGuid,\r
2114 NULL,\r
2115 &FvHandleCount,\r
2116 &FvHandleBuffer\r
2117 );\r
2118 for (Index = 0; Index < FvHandleCount; Index++) {\r
2119 gBS->HandleProtocol (\r
2120 FvHandleBuffer[Index],\r
2121 &gEfiFirmwareVolume2ProtocolGuid,\r
2122 (VOID **) &Fv\r
2123 );\r
2124\r
2125 Status = Fv->ReadFile (\r
2126 Fv,\r
2127 FileGuid,\r
2128 NULL,\r
2129 &Size,\r
2130 &Type,\r
2131 &Attributes,\r
2132 &AuthenticationStatus\r
2133 );\r
2134 if (EFI_ERROR (Status)) {\r
2135 //\r
2136 // Skip if input Fv file not in the FV\r
2137 //\r
2138 continue;\r
2139 }\r
2140 FindFvFile = TRUE;\r
2141 FoundFvHandle = FvHandleBuffer[Index];\r
2142 break;\r
2143 }\r
2144\r
2145 if (FvHandleBuffer != NULL) {\r
128efbbc 2146 FreePool (FvHandleBuffer);\r
5c08e117 2147 }\r
2148 }\r
2149\r
2150 if (FindFvFile) {\r
2151 //\r
2152 // Build the shell device path\r
2153 //\r
2154 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
2155 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
2156 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
2157 *DevicePath = NewDevicePath;\r
2158 return EFI_SUCCESS;\r
2159 }\r
2160 return EFI_NOT_FOUND;\r
2161}\r