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