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