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