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