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