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