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