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