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