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