]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Cleanup meta data for GenericBdsLib library instance's INF
[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
153 PERF_END (0, BDS_TOK, NULL, 0);\r
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
1117 //\r
1118 // Only care the dispatched FV. If no dispatch protocol on the FV, it is not dispatched, then skip it.\r
1119 //\r
1120 Status = gBS->HandleProtocol (\r
1121 FvHandleBuffer[Index],\r
1122 &gEfiFirmwareVolumeDispatchProtocolGuid,\r
1123 (VOID **) &Fv\r
1124 );\r
1125 if (EFI_ERROR (Status)) {\r
1126 continue;\r
1127 }\r
1128 \r
1129 gBS->HandleProtocol (\r
1130 FvHandleBuffer[Index],\r
1131 &gEfiFirmwareVolume2ProtocolGuid,\r
1132 (VOID **) &Fv\r
1133 );\r
1134\r
1135 Status = Fv->ReadFile (\r
1136 Fv,\r
1137 &gEfiShellFileGuid,\r
1138 NULL,\r
1139 &Size,\r
1140 &Type,\r
1141 &Attributes,\r
1142 &AuthenticationStatus\r
1143 );\r
1144 if (EFI_ERROR (Status)) {\r
1145 //\r
1146 // Skip if no shell file in the FV\r
1147 //\r
1148 continue;\r
1149 }\r
1150 //\r
1151 // Build the shell boot option\r
1152 //\r
1153 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
1154 }\r
1155\r
1156 if (FvHandleCount != 0) {\r
1157 FreePool (FvHandleBuffer);\r
1158 }\r
1159 //\r
1160 // Make sure every boot only have one time\r
1161 // boot device enumerate\r
1162 //\r
1163 BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
1164 mEnumBootDevice = TRUE;\r
1165\r
1166 return EFI_SUCCESS;\r
1167}\r
1168\r
1169/**\r
1170 Build the boot option with the handle parsed in\r
1171\r
1172 @param Handle The handle which present the device path to create\r
1173 boot option\r
1174 @param BdsBootOptionList The header of the link list which indexed all\r
1175 current boot options\r
1176 @param String The description of the boot option.\r
1177\r
1178**/\r
1179VOID\r
1180EFIAPI\r
1181BdsLibBuildOptionFromHandle (\r
1182 IN EFI_HANDLE Handle,\r
1183 IN LIST_ENTRY *BdsBootOptionList,\r
1184 IN CHAR16 *String\r
1185 )\r
1186{\r
1187 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1188\r
1189 DevicePath = DevicePathFromHandle (Handle);\r
1190\r
1191 //\r
1192 // Create and register new boot option\r
1193 //\r
1194 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
1195}\r
1196\r
1197\r
1198/**\r
1199 Build the on flash shell boot option with the handle parsed in.\r
1200\r
1201 @param Handle The handle which present the device path to create\r
1202 on flash shell boot option\r
1203 @param BdsBootOptionList The header of the link list which indexed all\r
1204 current boot options\r
1205\r
1206**/\r
1207VOID\r
1208EFIAPI\r
1209BdsLibBuildOptionFromShell (\r
1210 IN EFI_HANDLE Handle,\r
1211 IN OUT LIST_ENTRY *BdsBootOptionList\r
1212 )\r
1213{\r
1214 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1215 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
1216\r
1217 DevicePath = DevicePathFromHandle (Handle);\r
1218\r
1219 //\r
1220 // Build the shell device path\r
1221 //\r
1222 EfiInitializeFwVolDevicepathNode (&ShellNode, &gEfiShellFileGuid);\r
1223\r
1224 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
1225\r
1226 //\r
1227 // Create and register the shell boot option\r
1228 //\r
1229 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
1230\r
1231}\r
1232\r
1233/**\r
1234 Boot from the UEFI spec defined "BootNext" variable.\r
1235\r
1236**/\r
1237VOID\r
1238EFIAPI\r
1239BdsLibBootNext (\r
1240 VOID\r
1241 )\r
1242{\r
1243 UINT16 *BootNext;\r
1244 UINTN BootNextSize;\r
1245 CHAR16 Buffer[20];\r
1246 BDS_COMMON_OPTION *BootOption;\r
1247 LIST_ENTRY TempList;\r
1248 UINTN ExitDataSize;\r
1249 CHAR16 *ExitData;\r
1250\r
1251 //\r
1252 // Init the boot option name buffer and temp link list\r
1253 //\r
1254 InitializeListHead (&TempList);\r
1255 ZeroMem (Buffer, sizeof (Buffer));\r
1256\r
1257 BootNext = BdsLibGetVariableAndSize (\r
1258 L"BootNext",\r
1259 &gEfiGlobalVariableGuid,\r
1260 &BootNextSize\r
1261 );\r
1262\r
1263 //\r
1264 // Clear the boot next variable first\r
1265 //\r
1266 if (BootNext != NULL) {\r
1267 gRT->SetVariable (\r
1268 L"BootNext",\r
1269 &gEfiGlobalVariableGuid,\r
1270 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1271 0,\r
1272 BootNext\r
1273 );\r
1274\r
1275 //\r
1276 // Start to build the boot option and try to boot\r
1277 //\r
1278 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
1279 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
1280 ASSERT (BootOption != NULL);\r
1281 BdsLibConnectDevicePath (BootOption->DevicePath);\r
1282 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
1283 }\r
1284\r
1285}\r
1286\r
1287/**\r
1288 Return the bootable media handle.\r
1289 First, check the device is connected\r
1290 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
1291 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
1292\r
1293 @param DevicePath Device Path to a bootable device\r
1294\r
1295 @retval NULL The media on the DevicePath is not bootable\r
1296\r
1297**/\r
1298EFI_HANDLE\r
1299EFIAPI\r
1300BdsLibGetBootableHandle (\r
1301 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1302 )\r
1303{\r
1304 EFI_STATUS Status;\r
1305 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1306 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
1307 EFI_HANDLE Handle;\r
1308 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1309 VOID *Buffer;\r
1310 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1311 UINTN Size;\r
1312 UINTN TempSize;\r
1313 EFI_HANDLE ReturnHandle;\r
1314 EFI_HANDLE *SimpleFileSystemHandles;\r
1315\r
1316 UINTN NumberSimpleFileSystemHandles;\r
1317 UINTN Index;\r
1318 EFI_IMAGE_DOS_HEADER DosHeader;\r
1319 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
1320 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1321\r
1322 UpdatedDevicePath = DevicePath;\r
1323 \r
1324 //\r
1325 // Check whether the device is connected\r
1326 //\r
1327 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
1328 if (EFI_ERROR (Status)) {\r
1329 //\r
1330 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
1331 //\r
1332 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
1333 if (EFI_ERROR (Status)) {\r
1334 //\r
1335 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
1336 //\r
1337 UpdatedDevicePath = DevicePath;\r
1338 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1339 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
1340 }\r
1341 } else {\r
1342 //\r
1343 // Get BlockIo protocol and check removable attribute\r
1344 //\r
1345 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1346 //\r
1347 // Issue a dummy read to the device to check for media change.\r
1348 // When the removable media is changed, any Block IO read/write will\r
1349 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
1350 // returned. After the Block IO protocol is reinstalled, subsequent\r
1351 // Block IO read/write will success.\r
1352 //\r
1353 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
1354 if (Buffer != NULL) {\r
1355 BlockIo->ReadBlocks (\r
1356 BlockIo,\r
1357 BlockIo->Media->MediaId,\r
1358 0,\r
1359 BlockIo->Media->BlockSize,\r
1360 Buffer\r
1361 );\r
1362 FreePool(Buffer);\r
1363 }\r
1364 }\r
1365\r
1366 //\r
1367 // Detect the the default boot file from removable Media\r
1368 //\r
1369\r
1370 //\r
1371 // 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
1372 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
1373 //\r
1374 DupDevicePath = DuplicateDevicePath (DevicePath);\r
1375 ASSERT (DupDevicePath != NULL);\r
1376 \r
1377 UpdatedDevicePath = DupDevicePath;\r
1378 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1379 //\r
1380 // 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
1381 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
1382 //\r
1383 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1384 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
1385 //\r
1386 // Remove the usb node, let the device path only point to PCI node\r
1387 //\r
1388 SetDevicePathEndNode (UpdatedDevicePath);\r
1389 UpdatedDevicePath = DupDevicePath;\r
1390 } else {\r
1391 UpdatedDevicePath = DevicePath;\r
1392 }\r
1393\r
1394 //\r
1395 // Get the device path size of boot option\r
1396 //\r
1397 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1398 ReturnHandle = NULL;\r
1399 gBS->LocateHandleBuffer (\r
1400 ByProtocol,\r
1401 &gEfiSimpleFileSystemProtocolGuid,\r
1402 NULL,\r
1403 &NumberSimpleFileSystemHandles,\r
1404 &SimpleFileSystemHandles\r
1405 );\r
1406 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
1407 //\r
1408 // Get the device path size of SimpleFileSystem handle\r
1409 //\r
1410 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
1411 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1412 //\r
1413 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
1414 //\r
1415 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
1416 //\r
1417 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
1418 // machinename is ia32, ia64, x64, ...\r
1419 //\r
1420 Hdr.Union = &HdrData;\r
1421 Status = BdsLibGetImageHeader (\r
1422 SimpleFileSystemHandles[Index],\r
1423 (CHAR16*)PcdGetPtr(PcdDefaultBootFileName),\r
1424 &DosHeader,\r
1425 Hdr\r
1426 );\r
1427 if (!EFI_ERROR (Status) &&\r
1428 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1429 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1430 ReturnHandle = SimpleFileSystemHandles[Index];\r
1431 break;\r
1432 }\r
1433 }\r
1434 }\r
1435\r
1436 FreePool(DupDevicePath);\r
1437\r
1438 if (SimpleFileSystemHandles != NULL) {\r
1439 FreePool(SimpleFileSystemHandles);\r
1440 }\r
1441\r
1442 return ReturnHandle;\r
1443}\r
1444\r
1445/**\r
1446 Check to see if the network cable is plugged in. If the DevicePath is not\r
1447 connected it will be connected.\r
1448\r
1449 @param DevicePath Device Path to check\r
1450\r
1451 @retval TRUE DevicePath points to an Network that is connected\r
1452 @retval FALSE DevicePath does not point to a bootable network\r
1453\r
1454**/\r
1455BOOLEAN\r
1456BdsLibNetworkBootWithMediaPresent (\r
1457 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1458 )\r
1459{\r
1460 EFI_STATUS Status;\r
1461 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1462 EFI_HANDLE Handle;\r
1463 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
1464 BOOLEAN MediaPresent;\r
1465\r
1466 MediaPresent = FALSE;\r
1467\r
1468 UpdatedDevicePath = DevicePath;\r
1469 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
1470 if (EFI_ERROR (Status)) {\r
1471 //\r
1472 // Device not present so see if we need to connect it\r
1473 //\r
1474 Status = BdsLibConnectDevicePath (DevicePath);\r
1475 if (!EFI_ERROR (Status)) {\r
1476 //\r
1477 // This one should work after we did the connect\r
1478 //\r
1479 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
1480 }\r
1481 }\r
1482\r
1483 if (!EFI_ERROR (Status)) {\r
1484 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
1485 if (!EFI_ERROR (Status)) {\r
1486 if (Snp->Mode->MediaPresentSupported) {\r
1487 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
1488 //\r
1489 // In case some one else is using the SNP check to see if it's connected\r
1490 //\r
1491 MediaPresent = Snp->Mode->MediaPresent;\r
1492 } else {\r
1493 //\r
1494 // No one is using SNP so we need to Start and Initialize so\r
1495 // MediaPresent will be valid.\r
1496 //\r
1497 Status = Snp->Start (Snp);\r
1498 if (!EFI_ERROR (Status)) {\r
1499 Status = Snp->Initialize (Snp, 0, 0);\r
1500 if (!EFI_ERROR (Status)) {\r
1501 MediaPresent = Snp->Mode->MediaPresent;\r
1502 Snp->Shutdown (Snp);\r
1503 }\r
1504 Snp->Stop (Snp);\r
1505 }\r
1506 }\r
1507 } else {\r
1508 MediaPresent = TRUE;\r
1509 }\r
1510 }\r
1511 }\r
1512\r
1513 return MediaPresent;\r
1514}\r
1515\r
1516/**\r
1517 For a bootable Device path, return its boot type.\r
1518\r
1519 @param DevicePath The bootable device Path to check\r
1520\r
1521 @retval BDS_EFI_MEDIA_HD_BOOT If the device path contains any media device path node, it is media boot type\r
1522 For the floppy node, handle it as media node\r
1523 @retval BDS_EFI_MEDIA_CDROM_BOOT If the device path contains any media device path node, it is media boot type\r
1524 For the floppy node, handle it as media node\r
1525 @retval BDS_EFI_ACPI_FLOPPY_BOOT If the device path contains any media device path node, it is media boot type\r
1526 For the floppy node, handle it as media node\r
1527 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If the device path not contains any media device path node, and\r
1528 its last device path node point to a message device path node, it is\r
1529 \r
1530 @retval BDS_EFI_MESSAGE_SCSI_BOOT If the device path not contains any media device path node, and\r
1531 its last device path node point to a message device path node, it is\r
1532 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If the device path not contains any media device path node, and\r
1533 its last device path node point to a message device path node, it is\r
1534 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
1535 its last device path node point to a message device path node, it is\r
1536 @retval BDS_LEGACY_BBS_BOOT Legacy boot type\r
1537 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device, \r
1538\r
1539**/\r
1540UINT32\r
1541EFIAPI\r
1542BdsGetBootTypeFromDevicePath (\r
1543 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1544 )\r
1545{\r
1546 ACPI_HID_DEVICE_PATH *Acpi;\r
1547 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1548 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1549\r
1550\r
1551 if (NULL == DevicePath) {\r
1552 return BDS_EFI_UNSUPPORT;\r
1553 }\r
1554\r
1555 TempDevicePath = DevicePath;\r
1556\r
1557 while (!IsDevicePathEndType (TempDevicePath)) {\r
1558 switch (DevicePathType (TempDevicePath)) {\r
1559 case BBS_DEVICE_PATH:\r
1560 return BDS_LEGACY_BBS_BOOT;\r
1561 case MEDIA_DEVICE_PATH:\r
1562 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
1563 return BDS_EFI_MEDIA_HD_BOOT;\r
1564 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
1565 return BDS_EFI_MEDIA_CDROM_BOOT;\r
1566 }\r
1567 break;\r
1568 case ACPI_DEVICE_PATH:\r
1569 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
1570 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
1571 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
1572 }\r
1573 break;\r
1574 case MESSAGING_DEVICE_PATH:\r
1575 //\r
1576 // Get the last device path node\r
1577 //\r
1578 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
1579 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
1580 //\r
1581 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
1582 // skit it\r
1583 //\r
1584 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
1585 }\r
1586 //\r
1587 // if the device path not only point to driver device, it is not a messaging device path,\r
1588 //\r
1589 if (!IsDevicePathEndType (LastDeviceNode)) {\r
1590 break; \r
1591 }\r
1592\r
1593 if (DevicePathSubType(TempDevicePath) == MSG_ATAPI_DP) {\r
1594 return BDS_EFI_MESSAGE_ATAPI_BOOT;\r
1595 } else if (DevicePathSubType(TempDevicePath) == MSG_USB_DP) {\r
1596 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
1597 } else if (DevicePathSubType(TempDevicePath) == MSG_SCSI_DP) {\r
1598 return BDS_EFI_MESSAGE_SCSI_BOOT;\r
1599 }\r
1600 return BDS_EFI_MESSAGE_MISC_BOOT;\r
1601 default:\r
1602 break;\r
1603 }\r
1604 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1605 }\r
1606\r
1607 return BDS_EFI_UNSUPPORT;\r
1608}\r
1609\r
1610/**\r
1611 Check whether the Device path in a boot option point to a valid bootable device,\r
1612 And if CheckMedia is true, check the device is ready to boot now.\r
1613\r
1614 @param DevPath the Device path in a boot option\r
1615 @param CheckMedia if true, check the device is ready to boot now.\r
1616\r
1617 @retval TRUE the Device path is valid\r
1618 @retval FALSE the Device path is invalid .\r
1619\r
1620**/\r
1621BOOLEAN\r
1622EFIAPI\r
1623BdsLibIsValidEFIBootOptDevicePath (\r
1624 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1625 IN BOOLEAN CheckMedia\r
1626 )\r
1627{\r
1628 EFI_STATUS Status;\r
1629 EFI_HANDLE Handle;\r
1630 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1631 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1632 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1633\r
1634 TempDevicePath = DevPath;\r
1635 LastDeviceNode = DevPath;\r
1636 \r
1637 //\r
1638 // Check if it's a valid boot option for network boot device\r
1639 // Only check if there is SimpleNetworkProtocol installed. If yes, that means\r
1640 // there is the network card there.\r
1641 //\r
1642 Status = gBS->LocateDevicePath (\r
1643 &gEfiSimpleNetworkProtocolGuid,\r
1644 &TempDevicePath,\r
1645 &Handle\r
1646 );\r
1647 if (EFI_ERROR (Status)) {\r
1648 //\r
1649 // Device not present so see if we need to connect it\r
1650 //\r
1651 TempDevicePath = DevPath;\r
1652 BdsLibConnectDevicePath (TempDevicePath);\r
1653 Status = gBS->LocateDevicePath (\r
1654 &gEfiSimpleNetworkProtocolGuid,\r
1655 &TempDevicePath,\r
1656 &Handle\r
1657 );\r
1658 }\r
1659 \r
1660 if (!EFI_ERROR (Status)) {\r
1661 if (CheckMedia) {\r
1662 //\r
1663 // Test if it is ready to boot now\r
1664 //\r
1665 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
1666 return TRUE;\r
1667 }\r
1668 } else {\r
1669 return TRUE;\r
1670 }\r
1671 }\r
1672\r
1673 //\r
1674 // If the boot option point to a file, it is a valid EFI boot option,\r
1675 // and assume it is ready to boot now\r
1676 //\r
1677 while (!IsDevicePathEnd (TempDevicePath)) {\r
1678 LastDeviceNode = TempDevicePath;\r
1679 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1680 }\r
1681 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
1682 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
1683 return TRUE;\r
1684 }\r
1685\r
1686 //\r
1687 // Check if it's a valid boot option for internal Shell\r
1688 //\r
1689 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
1690 //\r
1691 // If the boot option point to Internal FV shell, make sure it is valid\r
1692 //\r
1693 TempDevicePath = DevPath; \r
1694 Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, &gEfiShellFileGuid);\r
1695 if (Status == EFI_ALREADY_STARTED) {\r
1696 return TRUE;\r
1697 } else {\r
1698 if (Status == EFI_SUCCESS) {\r
1699 FreePool (TempDevicePath); \r
1700 }\r
1701 return FALSE;\r
1702 }\r
1703 }\r
1704 \r
1705 //\r
1706 // 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
1707 //\r
1708 TempDevicePath = DevPath;\r
1709 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1710 if (EFI_ERROR (Status)) {\r
1711 //\r
1712 // Device not present so see if we need to connect it\r
1713 //\r
1714 Status = BdsLibConnectDevicePath (DevPath);\r
1715 if (!EFI_ERROR (Status)) {\r
1716 //\r
1717 // Try again to get the Block Io protocol after we did the connect\r
1718 //\r
1719 TempDevicePath = DevPath;\r
1720 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1721 }\r
1722 }\r
1723 \r
1724 if (!EFI_ERROR (Status)) {\r
1725 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1726 if (!EFI_ERROR (Status)) {\r
1727 if (CheckMedia) {\r
1728 //\r
1729 // Test if it is ready to boot now\r
1730 //\r
1731 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1732 return TRUE;\r
1733 }\r
1734 } else {\r
1735 return TRUE;\r
1736 }\r
1737 }\r
1738 } else {\r
1739 //\r
1740 // 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
1741 //\r
1742 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
1743 if (!EFI_ERROR (Status)) {\r
1744 if (CheckMedia) {\r
1745 //\r
1746 // Test if it is ready to boot now\r
1747 //\r
1748 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1749 return TRUE;\r
1750 }\r
1751 } else {\r
1752 return TRUE;\r
1753 }\r
1754 }\r
1755 }\r
1756\r
1757 return FALSE;\r
1758}\r
1759\r
1760\r
1761/**\r
1762 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
1763 try to return the valid device path.\r
1764 FV address maybe changes for memory layout adjust from time to time, use this function\r
1765 could promise the Fv file device path is right.\r
1766\r
1767 @param DevicePath on input, the Fv file device path need to check on\r
1768 output, the updated valid Fv file device path\r
1769 @param FileGuid the Fv file guild\r
1770\r
1771 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
1772 parameter\r
1773 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
1774 guild at all\r
1775 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
1776 valid\r
1777 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
1778 and return the updated device path in DevicePath\r
1779\r
1780**/\r
1781EFI_STATUS\r
1782EFIAPI\r
1783BdsLibUpdateFvFileDevicePath (\r
1784 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
1785 IN EFI_GUID *FileGuid\r
1786 )\r
1787{\r
1788 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1789 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1790 EFI_STATUS Status;\r
1791 EFI_GUID *GuidPoint;\r
1792 UINTN Index;\r
1793 UINTN FvHandleCount;\r
1794 EFI_HANDLE *FvHandleBuffer;\r
1795 EFI_FV_FILETYPE Type;\r
1796 UINTN Size;\r
1797 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1798 UINT32 AuthenticationStatus;\r
1799 BOOLEAN FindFvFile;\r
1800 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
1801 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1802 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
1803 EFI_HANDLE FoundFvHandle;\r
1804 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
1805\r
1806 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
1807 return EFI_INVALID_PARAMETER;\r
1808 }\r
1809 if (FileGuid == NULL) {\r
1810 return EFI_INVALID_PARAMETER;\r
1811 }\r
1812 \r
1813 //\r
1814 // Check whether the device path point to the default the input Fv file\r
1815 //\r
1816 TempDevicePath = *DevicePath;\r
1817 LastDeviceNode = TempDevicePath;\r
1818 while (!IsDevicePathEnd (TempDevicePath)) {\r
1819 LastDeviceNode = TempDevicePath;\r
1820 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1821 }\r
1822 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
1823 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
1824 );\r
1825 if (GuidPoint == NULL) {\r
1826 //\r
1827 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
1828 //\r
1829 return EFI_UNSUPPORTED;\r
1830 }\r
1831 if (!CompareGuid (GuidPoint, FileGuid)) {\r
1832 //\r
1833 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
1834 //\r
1835 return EFI_UNSUPPORTED;\r
1836 }\r
1837\r
1838 //\r
1839 // Check whether the input Fv file device path is valid\r
1840 //\r
1841 TempDevicePath = *DevicePath;\r
1842 FoundFvHandle = NULL;\r
1843 Status = gBS->LocateDevicePath (\r
1844 &gEfiFirmwareVolume2ProtocolGuid,\r
1845 &TempDevicePath,\r
1846 &FoundFvHandle\r
1847 );\r
1848 if (!EFI_ERROR (Status)) {\r
1849 Status = gBS->HandleProtocol (\r
1850 FoundFvHandle,\r
1851 &gEfiFirmwareVolume2ProtocolGuid,\r
1852 (VOID **) &Fv\r
1853 );\r
1854 if (!EFI_ERROR (Status)) {\r
1855 //\r
1856 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
1857 //\r
1858 Status = Fv->ReadFile (\r
1859 Fv,\r
1860 FileGuid,\r
1861 NULL,\r
1862 &Size,\r
1863 &Type,\r
1864 &Attributes,\r
1865 &AuthenticationStatus\r
1866 );\r
1867 if (!EFI_ERROR (Status)) {\r
1868 return EFI_ALREADY_STARTED;\r
1869 }\r
1870 }\r
1871 }\r
1872\r
1873 //\r
1874 // Look for the input wanted FV file in current FV\r
1875 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
1876 //\r
1877 FindFvFile = FALSE;\r
1878 FoundFvHandle = NULL;\r
1879 Status = gBS->HandleProtocol (\r
1880 mBdsImageHandle,\r
1881 &gEfiLoadedImageProtocolGuid,\r
1882 (VOID **) &LoadedImage\r
1883 );\r
1884 if (!EFI_ERROR (Status)) {\r
1885 Status = gBS->HandleProtocol (\r
1886 LoadedImage->DeviceHandle,\r
1887 &gEfiFirmwareVolume2ProtocolGuid,\r
1888 (VOID **) &Fv\r
1889 );\r
1890 if (!EFI_ERROR (Status)) {\r
1891 Status = Fv->ReadFile (\r
1892 Fv,\r
1893 FileGuid,\r
1894 NULL,\r
1895 &Size,\r
1896 &Type,\r
1897 &Attributes,\r
1898 &AuthenticationStatus\r
1899 );\r
1900 if (!EFI_ERROR (Status)) {\r
1901 FindFvFile = TRUE;\r
1902 FoundFvHandle = LoadedImage->DeviceHandle;\r
1903 }\r
1904 }\r
1905 }\r
1906 //\r
1907 // Second, if fail to find, try to enumerate all FV\r
1908 //\r
1909 if (!FindFvFile) {\r
1910 FvHandleBuffer = NULL;\r
1911 gBS->LocateHandleBuffer (\r
1912 ByProtocol,\r
1913 &gEfiFirmwareVolume2ProtocolGuid,\r
1914 NULL,\r
1915 &FvHandleCount,\r
1916 &FvHandleBuffer\r
1917 );\r
1918 for (Index = 0; Index < FvHandleCount; Index++) {\r
1919 gBS->HandleProtocol (\r
1920 FvHandleBuffer[Index],\r
1921 &gEfiFirmwareVolume2ProtocolGuid,\r
1922 (VOID **) &Fv\r
1923 );\r
1924\r
1925 Status = Fv->ReadFile (\r
1926 Fv,\r
1927 FileGuid,\r
1928 NULL,\r
1929 &Size,\r
1930 &Type,\r
1931 &Attributes,\r
1932 &AuthenticationStatus\r
1933 );\r
1934 if (EFI_ERROR (Status)) {\r
1935 //\r
1936 // Skip if input Fv file not in the FV\r
1937 //\r
1938 continue;\r
1939 }\r
1940 FindFvFile = TRUE;\r
1941 FoundFvHandle = FvHandleBuffer[Index];\r
1942 break;\r
1943 }\r
1944\r
1945 if (FvHandleBuffer != NULL) {\r
1946 FreePool (FvHandleBuffer); \r
1947 }\r
1948 }\r
1949\r
1950 if (FindFvFile) {\r
1951 //\r
1952 // Build the shell device path\r
1953 //\r
1954 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
1955 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
1956 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
1957 *DevicePath = NewDevicePath;\r
1958 return EFI_SUCCESS;\r
1959 }\r
1960 return EFI_NOT_FOUND;\r
1961}\r