]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
Don't need call SAP measure GPT table, which can be covered in SAP implementation.
[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
989 UINTN NumberSimpleNetworkHandles;\r
990 EFI_HANDLE *SimpleNetworkHandles;\r
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
1169 gBS->LocateHandleBuffer (\r
1170 ByProtocol,\r
1171 &gEfiSimpleNetworkProtocolGuid,\r
1172 NULL,\r
1173 &NumberSimpleNetworkHandles,\r
1174 &SimpleNetworkHandles\r
1175 );\r
8d3b5aff 1176\r
5c08e117 1177 for (Index = 0; Index < NumberSimpleNetworkHandles; Index++) {\r
429cac9c 1178 UnicodeSPrint (Buffer, sizeof (Buffer), L"%d", Index);\r
5c08e117 1179 BdsLibBuildOptionFromHandle (SimpleNetworkHandles[Index], BdsBootOptionList, Buffer);\r
1180 }\r
1181\r
1182 if (NumberSimpleNetworkHandles != 0) {\r
1183 FreePool (SimpleNetworkHandles);\r
1184 }\r
1185\r
1186 //\r
1187 // Check if we have on flash shell\r
1188 //\r
1189 gBS->LocateHandleBuffer (\r
1190 ByProtocol,\r
1191 &gEfiFirmwareVolume2ProtocolGuid,\r
1192 NULL,\r
1193 &FvHandleCount,\r
1194 &FvHandleBuffer\r
1195 );\r
1196 for (Index = 0; Index < FvHandleCount; Index++) {\r
5c08e117 1197 gBS->HandleProtocol (\r
1198 FvHandleBuffer[Index],\r
1199 &gEfiFirmwareVolume2ProtocolGuid,\r
1200 (VOID **) &Fv\r
1201 );\r
1202\r
1203 Status = Fv->ReadFile (\r
1204 Fv,\r
d46f3632 1205 PcdGetPtr(PcdShellFile),\r
5c08e117 1206 NULL,\r
1207 &Size,\r
1208 &Type,\r
1209 &Attributes,\r
1210 &AuthenticationStatus\r
1211 );\r
1212 if (EFI_ERROR (Status)) {\r
1213 //\r
1214 // Skip if no shell file in the FV\r
1215 //\r
1216 continue;\r
1217 }\r
1218 //\r
1219 // Build the shell boot option\r
1220 //\r
1221 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);\r
1222 }\r
1223\r
1224 if (FvHandleCount != 0) {\r
1225 FreePool (FvHandleBuffer);\r
1226 }\r
1227 //\r
1228 // Make sure every boot only have one time\r
1229 // boot device enumerate\r
1230 //\r
e83c9064 1231 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");\r
5c08e117 1232 mEnumBootDevice = TRUE;\r
1233\r
e83c9064 1234 return Status;\r
5c08e117 1235}\r
1236\r
1237/**\r
1238 Build the boot option with the handle parsed in\r
1239\r
1240 @param Handle The handle which present the device path to create\r
1241 boot option\r
1242 @param BdsBootOptionList The header of the link list which indexed all\r
1243 current boot options\r
1244 @param String The description of the boot option.\r
1245\r
1246**/\r
1247VOID\r
1248EFIAPI\r
1249BdsLibBuildOptionFromHandle (\r
1250 IN EFI_HANDLE Handle,\r
1251 IN LIST_ENTRY *BdsBootOptionList,\r
1252 IN CHAR16 *String\r
1253 )\r
1254{\r
1255 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
128efbbc 1256\r
8d3b5aff 1257 DevicePath = DevicePathFromHandle (Handle);\r
5c08e117 1258\r
1259 //\r
1260 // Create and register new boot option\r
1261 //\r
1262 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");\r
1263}\r
1264\r
1265\r
1266/**\r
1267 Build the on flash shell boot option with the handle parsed in.\r
1268\r
1269 @param Handle The handle which present the device path to create\r
1270 on flash shell boot option\r
1271 @param BdsBootOptionList The header of the link list which indexed all\r
1272 current boot options\r
1273\r
1274**/\r
1275VOID\r
1276EFIAPI\r
1277BdsLibBuildOptionFromShell (\r
1278 IN EFI_HANDLE Handle,\r
1279 IN OUT LIST_ENTRY *BdsBootOptionList\r
1280 )\r
1281{\r
1282 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
1283 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;\r
1284\r
1285 DevicePath = DevicePathFromHandle (Handle);\r
1286\r
1287 //\r
1288 // Build the shell device path\r
1289 //\r
d46f3632 1290 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));\r
5c08e117 1291\r
1292 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);\r
1293\r
1294 //\r
1295 // Create and register the shell boot option\r
1296 //\r
1297 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");\r
1298\r
1299}\r
1300\r
1301/**\r
1302 Boot from the UEFI spec defined "BootNext" variable.\r
1303\r
1304**/\r
1305VOID\r
1306EFIAPI\r
1307BdsLibBootNext (\r
1308 VOID\r
1309 )\r
1310{\r
1311 UINT16 *BootNext;\r
1312 UINTN BootNextSize;\r
1313 CHAR16 Buffer[20];\r
1314 BDS_COMMON_OPTION *BootOption;\r
1315 LIST_ENTRY TempList;\r
1316 UINTN ExitDataSize;\r
1317 CHAR16 *ExitData;\r
1318\r
1319 //\r
1320 // Init the boot option name buffer and temp link list\r
1321 //\r
1322 InitializeListHead (&TempList);\r
1323 ZeroMem (Buffer, sizeof (Buffer));\r
1324\r
1325 BootNext = BdsLibGetVariableAndSize (\r
1326 L"BootNext",\r
1327 &gEfiGlobalVariableGuid,\r
1328 &BootNextSize\r
1329 );\r
1330\r
1331 //\r
1332 // Clear the boot next variable first\r
1333 //\r
1334 if (BootNext != NULL) {\r
1335 gRT->SetVariable (\r
1336 L"BootNext",\r
1337 &gEfiGlobalVariableGuid,\r
1338 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
1339 0,\r
1340 BootNext\r
1341 );\r
1342\r
1343 //\r
1344 // Start to build the boot option and try to boot\r
1345 //\r
1346 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);\r
1347 BootOption = BdsLibVariableToOption (&TempList, Buffer);\r
1348 ASSERT (BootOption != NULL);\r
1349 BdsLibConnectDevicePath (BootOption->DevicePath);\r
1350 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);\r
1351 }\r
1352\r
1353}\r
1354\r
1355/**\r
1356 Return the bootable media handle.\r
1357 First, check the device is connected\r
1358 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,\r
1359 Third, detect the the default boot file in the Media, and return the removable Media handle.\r
1360\r
e83c9064 1361 @param DevicePath Device Path to a bootable device\r
5c08e117 1362\r
e83c9064 1363 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.\r
5c08e117 1364\r
1365**/\r
1366EFI_HANDLE\r
1367EFIAPI\r
1368BdsLibGetBootableHandle (\r
1369 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1370 )\r
1371{\r
1372 EFI_STATUS Status;\r
1373 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1374 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;\r
1375 EFI_HANDLE Handle;\r
1376 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1377 VOID *Buffer;\r
1378 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1379 UINTN Size;\r
1380 UINTN TempSize;\r
1381 EFI_HANDLE ReturnHandle;\r
1382 EFI_HANDLE *SimpleFileSystemHandles;\r
1383\r
1384 UINTN NumberSimpleFileSystemHandles;\r
1385 UINTN Index;\r
1386 EFI_IMAGE_DOS_HEADER DosHeader;\r
1387 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
1388 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
1389\r
1390 UpdatedDevicePath = DevicePath;\r
128efbbc 1391\r
5c08e117 1392 //\r
1393 // Check whether the device is connected\r
1394 //\r
1395 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);\r
1396 if (EFI_ERROR (Status)) {\r
1397 //\r
1398 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,\r
1399 //\r
1400 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);\r
1401 if (EFI_ERROR (Status)) {\r
1402 //\r
1403 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly\r
1404 //\r
1405 UpdatedDevicePath = DevicePath;\r
1406 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1407 gBS->ConnectController (Handle, NULL, NULL, TRUE);\r
1408 }\r
1409 } else {\r
1410 //\r
1411 // Get BlockIo protocol and check removable attribute\r
1412 //\r
1413 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1414 //\r
1415 // Issue a dummy read to the device to check for media change.\r
1416 // When the removable media is changed, any Block IO read/write will\r
1417 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is\r
1418 // returned. After the Block IO protocol is reinstalled, subsequent\r
1419 // Block IO read/write will success.\r
1420 //\r
1421 Buffer = AllocatePool (BlockIo->Media->BlockSize);\r
1422 if (Buffer != NULL) {\r
1423 BlockIo->ReadBlocks (\r
1424 BlockIo,\r
1425 BlockIo->Media->MediaId,\r
1426 0,\r
1427 BlockIo->Media->BlockSize,\r
1428 Buffer\r
1429 );\r
1430 FreePool(Buffer);\r
1431 }\r
1432 }\r
1433\r
1434 //\r
1435 // Detect the the default boot file from removable Media\r
1436 //\r
1437\r
1438 //\r
1439 // 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
1440 // Try to locate the USB node device path first, if fail then use its previous PCI node to search\r
1441 //\r
1442 DupDevicePath = DuplicateDevicePath (DevicePath);\r
1443 ASSERT (DupDevicePath != NULL);\r
128efbbc 1444\r
5c08e117 1445 UpdatedDevicePath = DupDevicePath;\r
1446 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);\r
1447 //\r
1448 // 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
1449 // Acpi()/Pci()/Usb() --> Acpi()/Pci()\r
1450 //\r
1451 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&\r
1452 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {\r
1453 //\r
1454 // Remove the usb node, let the device path only point to PCI node\r
1455 //\r
1456 SetDevicePathEndNode (UpdatedDevicePath);\r
1457 UpdatedDevicePath = DupDevicePath;\r
1458 } else {\r
1459 UpdatedDevicePath = DevicePath;\r
1460 }\r
1461\r
1462 //\r
1463 // Get the device path size of boot option\r
1464 //\r
1465 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1466 ReturnHandle = NULL;\r
1467 gBS->LocateHandleBuffer (\r
1468 ByProtocol,\r
1469 &gEfiSimpleFileSystemProtocolGuid,\r
1470 NULL,\r
1471 &NumberSimpleFileSystemHandles,\r
1472 &SimpleFileSystemHandles\r
1473 );\r
1474 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {\r
1475 //\r
1476 // Get the device path size of SimpleFileSystem handle\r
1477 //\r
1478 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);\r
1479 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node\r
1480 //\r
1481 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path\r
1482 //\r
1483 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {\r
1484 //\r
1485 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media\r
1486 // machinename is ia32, ia64, x64, ...\r
1487 //\r
1488 Hdr.Union = &HdrData;\r
1489 Status = BdsLibGetImageHeader (\r
1490 SimpleFileSystemHandles[Index],\r
c62dbf31 1491 EFI_REMOVABLE_MEDIA_FILE_NAME,\r
5c08e117 1492 &DosHeader,\r
1493 Hdr\r
1494 );\r
1495 if (!EFI_ERROR (Status) &&\r
1496 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&\r
1497 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {\r
1498 ReturnHandle = SimpleFileSystemHandles[Index];\r
1499 break;\r
1500 }\r
1501 }\r
1502 }\r
1503\r
1504 FreePool(DupDevicePath);\r
1505\r
1506 if (SimpleFileSystemHandles != NULL) {\r
1507 FreePool(SimpleFileSystemHandles);\r
1508 }\r
1509\r
1510 return ReturnHandle;\r
1511}\r
1512\r
1513/**\r
1514 Check to see if the network cable is plugged in. If the DevicePath is not\r
1515 connected it will be connected.\r
1516\r
1517 @param DevicePath Device Path to check\r
1518\r
1519 @retval TRUE DevicePath points to an Network that is connected\r
1520 @retval FALSE DevicePath does not point to a bootable network\r
1521\r
1522**/\r
1523BOOLEAN\r
1524BdsLibNetworkBootWithMediaPresent (\r
1525 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1526 )\r
1527{\r
1528 EFI_STATUS Status;\r
1529 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;\r
1530 EFI_HANDLE Handle;\r
1531 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;\r
1532 BOOLEAN MediaPresent;\r
1533\r
1534 MediaPresent = FALSE;\r
1535\r
1536 UpdatedDevicePath = DevicePath;\r
1537 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
1538 if (EFI_ERROR (Status)) {\r
1539 //\r
1540 // Device not present so see if we need to connect it\r
1541 //\r
1542 Status = BdsLibConnectDevicePath (DevicePath);\r
1543 if (!EFI_ERROR (Status)) {\r
1544 //\r
1545 // This one should work after we did the connect\r
1546 //\r
1547 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);\r
1548 }\r
1549 }\r
1550\r
1551 if (!EFI_ERROR (Status)) {\r
1552 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);\r
1553 if (!EFI_ERROR (Status)) {\r
1554 if (Snp->Mode->MediaPresentSupported) {\r
1555 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {\r
1556 //\r
1557 // In case some one else is using the SNP check to see if it's connected\r
1558 //\r
1559 MediaPresent = Snp->Mode->MediaPresent;\r
1560 } else {\r
1561 //\r
1562 // No one is using SNP so we need to Start and Initialize so\r
1563 // MediaPresent will be valid.\r
1564 //\r
1565 Status = Snp->Start (Snp);\r
1566 if (!EFI_ERROR (Status)) {\r
1567 Status = Snp->Initialize (Snp, 0, 0);\r
1568 if (!EFI_ERROR (Status)) {\r
1569 MediaPresent = Snp->Mode->MediaPresent;\r
1570 Snp->Shutdown (Snp);\r
1571 }\r
1572 Snp->Stop (Snp);\r
1573 }\r
1574 }\r
1575 } else {\r
1576 MediaPresent = TRUE;\r
1577 }\r
1578 }\r
1579 }\r
1580\r
1581 return MediaPresent;\r
1582}\r
1583\r
1584/**\r
1585 For a bootable Device path, return its boot type.\r
1586\r
1587 @param DevicePath The bootable device Path to check\r
1588\r
128efbbc 1589 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
11c5022d 1590 which subtype is MEDIA_HARDDRIVE_DP\r
1591 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node\r
1592 which subtype is MEDIA_CDROM_DP\r
1593 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node\r
1594 which HID is floppy device.\r
1595 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1596 and its last device path node's subtype is MSG_ATAPI_DP.\r
1597 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1598 and its last device path node's subtype is MSG_SCSI_DP.\r
1599 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node\r
1600 and its last device path node's subtype is MSG_USB_DP.\r
5c08e117 1601 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and\r
11c5022d 1602 its last device path node point to a message device path node.\r
1603 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.\r
128efbbc 1604 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,\r
5c08e117 1605\r
1606**/\r
1607UINT32\r
1608EFIAPI\r
1609BdsGetBootTypeFromDevicePath (\r
1610 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
1611 )\r
1612{\r
1613 ACPI_HID_DEVICE_PATH *Acpi;\r
1614 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1615 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1616\r
1617\r
1618 if (NULL == DevicePath) {\r
1619 return BDS_EFI_UNSUPPORT;\r
1620 }\r
1621\r
1622 TempDevicePath = DevicePath;\r
1623\r
1624 while (!IsDevicePathEndType (TempDevicePath)) {\r
1625 switch (DevicePathType (TempDevicePath)) {\r
1626 case BBS_DEVICE_PATH:\r
1627 return BDS_LEGACY_BBS_BOOT;\r
1628 case MEDIA_DEVICE_PATH:\r
1629 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {\r
1630 return BDS_EFI_MEDIA_HD_BOOT;\r
1631 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {\r
1632 return BDS_EFI_MEDIA_CDROM_BOOT;\r
128efbbc 1633 }\r
5c08e117 1634 break;\r
1635 case ACPI_DEVICE_PATH:\r
1636 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;\r
1637 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {\r
1638 return BDS_EFI_ACPI_FLOPPY_BOOT;\r
1639 }\r
1640 break;\r
1641 case MESSAGING_DEVICE_PATH:\r
1642 //\r
1643 // Get the last device path node\r
1644 //\r
1645 LastDeviceNode = NextDevicePathNode (TempDevicePath);\r
1646 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {\r
1647 //\r
1648 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),\r
1649 // skit it\r
1650 //\r
1651 LastDeviceNode = NextDevicePathNode (LastDeviceNode);\r
1652 }\r
1653 //\r
1654 // if the device path not only point to driver device, it is not a messaging device path,\r
1655 //\r
1656 if (!IsDevicePathEndType (LastDeviceNode)) {\r
128efbbc 1657 break;\r
5c08e117 1658 }\r
1659\r
1660 if (DevicePathSubType(TempDevicePath) == MSG_ATAPI_DP) {\r
1661 return BDS_EFI_MESSAGE_ATAPI_BOOT;\r
1662 } else if (DevicePathSubType(TempDevicePath) == MSG_USB_DP) {\r
1663 return BDS_EFI_MESSAGE_USB_DEVICE_BOOT;\r
1664 } else if (DevicePathSubType(TempDevicePath) == MSG_SCSI_DP) {\r
1665 return BDS_EFI_MESSAGE_SCSI_BOOT;\r
3384a9bc 1666 } else if (DevicePathSubType(TempDevicePath) == MSG_SATA_DP) {\r
1667 return BDS_EFI_MESSAGE_SATA_BOOT;\r
429cac9c 1668 } else if (DevicePathSubType(TempDevicePath) == MSG_MAC_ADDR_DP) {\r
1669 return BDS_EFI_MESSAGE_MAC_BOOT;\r
5c08e117 1670 }\r
1671 return BDS_EFI_MESSAGE_MISC_BOOT;\r
1672 default:\r
1673 break;\r
1674 }\r
1675 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1676 }\r
1677\r
1678 return BDS_EFI_UNSUPPORT;\r
1679}\r
1680\r
1681/**\r
1682 Check whether the Device path in a boot option point to a valid bootable device,\r
1683 And if CheckMedia is true, check the device is ready to boot now.\r
1684\r
1685 @param DevPath the Device path in a boot option\r
1686 @param CheckMedia if true, check the device is ready to boot now.\r
1687\r
1688 @retval TRUE the Device path is valid\r
1689 @retval FALSE the Device path is invalid .\r
1690\r
1691**/\r
1692BOOLEAN\r
1693EFIAPI\r
1694BdsLibIsValidEFIBootOptDevicePath (\r
1695 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1696 IN BOOLEAN CheckMedia\r
1697 )\r
3384a9bc 1698{\r
1699 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);\r
1700}\r
1701\r
1702/**\r
1703 Check whether the Device path in a boot option point to a valid bootable device,\r
1704 And if CheckMedia is true, check the device is ready to boot now.\r
1705 If Description is not NULL and the device path point to a fixed BlockIo\r
1706 device, check the description whether conflict with other auto-created\r
1707 boot options.\r
1708\r
1709 @param DevPath the Device path in a boot option\r
1710 @param CheckMedia if true, check the device is ready to boot now.\r
1711 @param Description the description in a boot option\r
1712\r
1713 @retval TRUE the Device path is valid\r
1714 @retval FALSE the Device path is invalid .\r
1715\r
1716**/\r
1717BOOLEAN\r
1718EFIAPI\r
1719BdsLibIsValidEFIBootOptDevicePathExt (\r
1720 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,\r
1721 IN BOOLEAN CheckMedia,\r
1722 IN CHAR16 *Description\r
1723 )\r
5c08e117 1724{\r
1725 EFI_STATUS Status;\r
1726 EFI_HANDLE Handle;\r
1727 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1728 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1729 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1730\r
1731 TempDevicePath = DevPath;\r
1732 LastDeviceNode = DevPath;\r
128efbbc 1733\r
5c08e117 1734 //\r
1735 // Check if it's a valid boot option for network boot device\r
1736 // Only check if there is SimpleNetworkProtocol installed. If yes, that means\r
1737 // there is the network card there.\r
1738 //\r
1739 Status = gBS->LocateDevicePath (\r
1740 &gEfiSimpleNetworkProtocolGuid,\r
1741 &TempDevicePath,\r
1742 &Handle\r
1743 );\r
1744 if (EFI_ERROR (Status)) {\r
1745 //\r
1746 // Device not present so see if we need to connect it\r
1747 //\r
1748 TempDevicePath = DevPath;\r
1749 BdsLibConnectDevicePath (TempDevicePath);\r
1750 Status = gBS->LocateDevicePath (\r
1751 &gEfiSimpleNetworkProtocolGuid,\r
1752 &TempDevicePath,\r
1753 &Handle\r
1754 );\r
1755 }\r
128efbbc 1756\r
5c08e117 1757 if (!EFI_ERROR (Status)) {\r
1758 if (CheckMedia) {\r
1759 //\r
1760 // Test if it is ready to boot now\r
1761 //\r
1762 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {\r
1763 return TRUE;\r
1764 }\r
1765 } else {\r
1766 return TRUE;\r
1767 }\r
1768 }\r
1769\r
1770 //\r
1771 // If the boot option point to a file, it is a valid EFI boot option,\r
1772 // and assume it is ready to boot now\r
1773 //\r
1774 while (!IsDevicePathEnd (TempDevicePath)) {\r
1775 LastDeviceNode = TempDevicePath;\r
1776 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1777 }\r
1778 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&\r
1779 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {\r
1780 return TRUE;\r
1781 }\r
1782\r
1783 //\r
1784 // Check if it's a valid boot option for internal Shell\r
1785 //\r
1786 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {\r
1787 //\r
1788 // If the boot option point to Internal FV shell, make sure it is valid\r
1789 //\r
128efbbc 1790 TempDevicePath = DevPath;\r
d46f3632 1791 Status = BdsLibUpdateFvFileDevicePath (&TempDevicePath, PcdGetPtr(PcdShellFile));\r
5c08e117 1792 if (Status == EFI_ALREADY_STARTED) {\r
1793 return TRUE;\r
1794 } else {\r
1795 if (Status == EFI_SUCCESS) {\r
128efbbc 1796 FreePool (TempDevicePath);\r
5c08e117 1797 }\r
1798 return FALSE;\r
1799 }\r
1800 }\r
128efbbc 1801\r
5c08e117 1802 //\r
3384a9bc 1803 // If the boot option point to a blockIO device:\r
8d3b5aff 1804 // if it is a removable blockIo device, it is valid.\r
128efbbc 1805 // if it is a fixed blockIo device, check its description confliction.\r
5c08e117 1806 //\r
1807 TempDevicePath = DevPath;\r
1808 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1809 if (EFI_ERROR (Status)) {\r
1810 //\r
1811 // Device not present so see if we need to connect it\r
1812 //\r
1813 Status = BdsLibConnectDevicePath (DevPath);\r
1814 if (!EFI_ERROR (Status)) {\r
1815 //\r
1816 // Try again to get the Block Io protocol after we did the connect\r
1817 //\r
1818 TempDevicePath = DevPath;\r
1819 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);\r
1820 }\r
1821 }\r
128efbbc 1822\r
5c08e117 1823 if (!EFI_ERROR (Status)) {\r
1824 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);\r
1825 if (!EFI_ERROR (Status)) {\r
1826 if (CheckMedia) {\r
1827 //\r
1828 // Test if it is ready to boot now\r
1829 //\r
1830 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1831 return TRUE;\r
1832 }\r
1833 } else {\r
1834 return TRUE;\r
1835 }\r
1836 }\r
1837 } else {\r
1838 //\r
1839 // 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
1840 //\r
1841 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);\r
1842 if (!EFI_ERROR (Status)) {\r
1843 if (CheckMedia) {\r
1844 //\r
1845 // Test if it is ready to boot now\r
1846 //\r
1847 if (BdsLibGetBootableHandle (DevPath) != NULL) {\r
1848 return TRUE;\r
1849 }\r
1850 } else {\r
1851 return TRUE;\r
1852 }\r
1853 }\r
1854 }\r
1855\r
1856 return FALSE;\r
1857}\r
1858\r
1859\r
1860/**\r
1861 According to a file guild, check a Fv file device path is valid. If it is invalid,\r
1862 try to return the valid device path.\r
1863 FV address maybe changes for memory layout adjust from time to time, use this function\r
1864 could promise the Fv file device path is right.\r
1865\r
1866 @param DevicePath on input, the Fv file device path need to check on\r
1867 output, the updated valid Fv file device path\r
1868 @param FileGuid the Fv file guild\r
1869\r
1870 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid\r
1871 parameter\r
1872 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file\r
1873 guild at all\r
1874 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is\r
1875 valid\r
1876 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,\r
1877 and return the updated device path in DevicePath\r
1878\r
1879**/\r
1880EFI_STATUS\r
1881EFIAPI\r
1882BdsLibUpdateFvFileDevicePath (\r
1883 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,\r
1884 IN EFI_GUID *FileGuid\r
1885 )\r
1886{\r
1887 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
1888 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;\r
1889 EFI_STATUS Status;\r
1890 EFI_GUID *GuidPoint;\r
1891 UINTN Index;\r
1892 UINTN FvHandleCount;\r
1893 EFI_HANDLE *FvHandleBuffer;\r
1894 EFI_FV_FILETYPE Type;\r
1895 UINTN Size;\r
1896 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1897 UINT32 AuthenticationStatus;\r
1898 BOOLEAN FindFvFile;\r
1899 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
1900 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1901 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;\r
1902 EFI_HANDLE FoundFvHandle;\r
1903 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
1904\r
1905 if ((DevicePath == NULL) || (*DevicePath == NULL)) {\r
1906 return EFI_INVALID_PARAMETER;\r
1907 }\r
1908 if (FileGuid == NULL) {\r
1909 return EFI_INVALID_PARAMETER;\r
1910 }\r
128efbbc 1911\r
5c08e117 1912 //\r
1913 // Check whether the device path point to the default the input Fv file\r
1914 //\r
1915 TempDevicePath = *DevicePath;\r
1916 LastDeviceNode = TempDevicePath;\r
1917 while (!IsDevicePathEnd (TempDevicePath)) {\r
1918 LastDeviceNode = TempDevicePath;\r
1919 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
1920 }\r
1921 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (\r
1922 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode\r
1923 );\r
1924 if (GuidPoint == NULL) {\r
1925 //\r
1926 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED\r
1927 //\r
1928 return EFI_UNSUPPORTED;\r
1929 }\r
1930 if (!CompareGuid (GuidPoint, FileGuid)) {\r
1931 //\r
1932 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED\r
1933 //\r
1934 return EFI_UNSUPPORTED;\r
1935 }\r
1936\r
1937 //\r
1938 // Check whether the input Fv file device path is valid\r
1939 //\r
1940 TempDevicePath = *DevicePath;\r
1941 FoundFvHandle = NULL;\r
1942 Status = gBS->LocateDevicePath (\r
1943 &gEfiFirmwareVolume2ProtocolGuid,\r
1944 &TempDevicePath,\r
1945 &FoundFvHandle\r
1946 );\r
1947 if (!EFI_ERROR (Status)) {\r
1948 Status = gBS->HandleProtocol (\r
1949 FoundFvHandle,\r
1950 &gEfiFirmwareVolume2ProtocolGuid,\r
1951 (VOID **) &Fv\r
1952 );\r
1953 if (!EFI_ERROR (Status)) {\r
1954 //\r
1955 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there\r
1956 //\r
1957 Status = Fv->ReadFile (\r
1958 Fv,\r
1959 FileGuid,\r
1960 NULL,\r
1961 &Size,\r
1962 &Type,\r
1963 &Attributes,\r
1964 &AuthenticationStatus\r
1965 );\r
1966 if (!EFI_ERROR (Status)) {\r
1967 return EFI_ALREADY_STARTED;\r
1968 }\r
1969 }\r
1970 }\r
1971\r
1972 //\r
1973 // Look for the input wanted FV file in current FV\r
1974 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV\r
1975 //\r
1976 FindFvFile = FALSE;\r
1977 FoundFvHandle = NULL;\r
1978 Status = gBS->HandleProtocol (\r
1979 mBdsImageHandle,\r
1980 &gEfiLoadedImageProtocolGuid,\r
1981 (VOID **) &LoadedImage\r
1982 );\r
1983 if (!EFI_ERROR (Status)) {\r
1984 Status = gBS->HandleProtocol (\r
1985 LoadedImage->DeviceHandle,\r
1986 &gEfiFirmwareVolume2ProtocolGuid,\r
1987 (VOID **) &Fv\r
1988 );\r
1989 if (!EFI_ERROR (Status)) {\r
1990 Status = Fv->ReadFile (\r
1991 Fv,\r
1992 FileGuid,\r
1993 NULL,\r
1994 &Size,\r
1995 &Type,\r
1996 &Attributes,\r
1997 &AuthenticationStatus\r
1998 );\r
1999 if (!EFI_ERROR (Status)) {\r
2000 FindFvFile = TRUE;\r
2001 FoundFvHandle = LoadedImage->DeviceHandle;\r
2002 }\r
2003 }\r
2004 }\r
2005 //\r
2006 // Second, if fail to find, try to enumerate all FV\r
2007 //\r
2008 if (!FindFvFile) {\r
2009 FvHandleBuffer = NULL;\r
2010 gBS->LocateHandleBuffer (\r
2011 ByProtocol,\r
2012 &gEfiFirmwareVolume2ProtocolGuid,\r
2013 NULL,\r
2014 &FvHandleCount,\r
2015 &FvHandleBuffer\r
2016 );\r
2017 for (Index = 0; Index < FvHandleCount; Index++) {\r
2018 gBS->HandleProtocol (\r
2019 FvHandleBuffer[Index],\r
2020 &gEfiFirmwareVolume2ProtocolGuid,\r
2021 (VOID **) &Fv\r
2022 );\r
2023\r
2024 Status = Fv->ReadFile (\r
2025 Fv,\r
2026 FileGuid,\r
2027 NULL,\r
2028 &Size,\r
2029 &Type,\r
2030 &Attributes,\r
2031 &AuthenticationStatus\r
2032 );\r
2033 if (EFI_ERROR (Status)) {\r
2034 //\r
2035 // Skip if input Fv file not in the FV\r
2036 //\r
2037 continue;\r
2038 }\r
2039 FindFvFile = TRUE;\r
2040 FoundFvHandle = FvHandleBuffer[Index];\r
2041 break;\r
2042 }\r
2043\r
2044 if (FvHandleBuffer != NULL) {\r
128efbbc 2045 FreePool (FvHandleBuffer);\r
5c08e117 2046 }\r
2047 }\r
2048\r
2049 if (FindFvFile) {\r
2050 //\r
2051 // Build the shell device path\r
2052 //\r
2053 NewDevicePath = DevicePathFromHandle (FoundFvHandle);\r
2054 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);\r
2055 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);\r
2056 *DevicePath = NewDevicePath;\r
2057 return EFI_SUCCESS;\r
2058 }\r
2059 return EFI_NOT_FOUND;\r
2060}\r