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