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