]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/Override/IntelFrameworkModulePkg/Library/GenericBdsLib/BdsBoot.c
282ab8e5b0ec49b3a4b6a4d6c098078f47415049
[mirror_edk2.git] / Vlv2TbltDevicePkg / Override / IntelFrameworkModulePkg / Library / GenericBdsLib / BdsBoot.c
1 /** @file
2 BDS Lib functions which relate with create or process the boot option.
3
4 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalBdsLib.h"
16 #include "String.h"
17 #include <Library/NetLib.h>
18 #include "Library/DebugLib.h"
19
20 BOOLEAN mEnumBootDevice = FALSE;
21 EFI_HII_HANDLE gBdsLibStringPackHandle = NULL;
22
23 /**
24
25 End Perf entry of BDS
26
27 @param Event The triggered event.
28 @param Context Context for this event.
29
30 **/
31 VOID
32 EFIAPI
33 BmEndOfBdsPerfCode (
34 IN EFI_EVENT Event,
35 IN VOID *Context
36 )
37 {
38 //
39 // Record the performance data for End of BDS
40 //
41 PERF_END(NULL, "BDS", NULL, 0);
42
43 return ;
44 }
45
46 /**
47 The constructor function register UNI strings into imageHandle.
48
49 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
50
51 @param ImageHandle The firmware allocated handle for the EFI image.
52 @param SystemTable A pointer to the EFI System Table.
53
54 @retval EFI_SUCCESS The constructor successfully added string package.
55 @retval Other value The constructor can't add string package.
56
57 **/
58 EFI_STATUS
59 EFIAPI
60 GenericBdsLibConstructor (
61 IN EFI_HANDLE ImageHandle,
62 IN EFI_SYSTEM_TABLE *SystemTable
63 )
64 {
65
66 gBdsLibStringPackHandle = HiiAddPackages (
67 &gBdsLibStringPackageGuid,
68 ImageHandle,
69 GenericBdsLibStrings,
70 NULL
71 );
72
73 ASSERT (gBdsLibStringPackHandle != NULL);
74
75 return EFI_SUCCESS;
76 }
77
78 /**
79 Deletete the Boot Option from EFI Variable. The Boot Order Arrray
80 is also updated.
81
82 @param OptionNumber The number of Boot option want to be deleted.
83 @param BootOrder The Boot Order array.
84 @param BootOrderSize The size of the Boot Order Array.
85
86 @retval EFI_SUCCESS The Boot Option Variable was found and removed
87 @retval EFI_UNSUPPORTED The Boot Option Variable store was inaccessible
88 @retval EFI_NOT_FOUND The Boot Option Variable was not found
89 **/
90 EFI_STATUS
91 EFIAPI
92 BdsDeleteBootOption (
93 IN UINTN OptionNumber,
94 IN OUT UINT16 *BootOrder,
95 IN OUT UINTN *BootOrderSize
96 )
97 {
98 CHAR16 BootOption[9];
99 UINTN Index;
100 EFI_STATUS Status;
101
102 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", OptionNumber);
103 Status = gRT->SetVariable (
104 BootOption,
105 &gEfiGlobalVariableGuid,
106 0,
107 0,
108 NULL
109 );
110 //
111 // Deleting variable with existing variable implementation shouldn't fail.
112 //
113 ASSERT_EFI_ERROR (Status);
114
115 //
116 // adjust boot order array
117 //
118 for (Index = 0; Index < *BootOrderSize / sizeof (UINT16); Index++) {
119 if (BootOrder[Index] == OptionNumber) {
120 CopyMem (&BootOrder[Index], &BootOrder[Index+1], *BootOrderSize - (Index+1) * sizeof (UINT16));
121 *BootOrderSize -= sizeof (UINT16);
122 break;
123 }
124 }
125
126 return Status;
127 }
128 /**
129
130 Translate the first n characters of an Ascii string to
131 Unicode characters. The count n is indicated by parameter
132 Size. If Size is greater than the length of string, then
133 the entire string is translated.
134
135
136 @param AStr Pointer to input Ascii string.
137 @param Size The number of characters to translate.
138 @param UStr Pointer to output Unicode string buffer.
139
140 **/
141 VOID
142 AsciiToUnicodeSize (
143 IN UINT8 *AStr,
144 IN UINTN Size,
145 OUT UINT16 *UStr
146 )
147 {
148 UINTN Idx;
149
150 Idx = 0;
151 while (AStr[Idx] != 0) {
152 UStr[Idx] = (CHAR16) AStr[Idx];
153 if (Idx == Size) {
154 break;
155 }
156
157 Idx++;
158 }
159 UStr[Idx] = 0;
160 }
161
162 /**
163 Build Legacy Device Name String according.
164
165 @param CurBBSEntry BBS Table.
166 @param Index Index.
167 @param BufSize The buffer size.
168 @param BootString The output string.
169
170 **/
171 VOID
172 BdsBuildLegacyDevNameString (
173 IN BBS_TABLE *CurBBSEntry,
174 IN UINTN Index,
175 IN UINTN BufSize,
176 OUT CHAR16 *BootString
177 )
178 {
179 CHAR16 *Fmt;
180 CHAR16 *Type;
181 UINT8 *StringDesc;
182 CHAR16 Temp[80];
183
184 switch (Index) {
185 //
186 // Primary Master
187 //
188 case 1:
189 Fmt = L"Primary Master %s";
190 break;
191
192 //
193 // Primary Slave
194 //
195 case 2:
196 Fmt = L"Primary Slave %s";
197 break;
198
199 //
200 // Secondary Master
201 //
202 case 3:
203 Fmt = L"Secondary Master %s";
204 break;
205
206 //
207 // Secondary Slave
208 //
209 case 4:
210 Fmt = L"Secondary Slave %s";
211 break;
212
213 default:
214 Fmt = L"%s";
215 break;
216 }
217
218 switch (CurBBSEntry->DeviceType) {
219 case BBS_FLOPPY:
220 Type = L"Floppy";
221 break;
222
223 case BBS_HARDDISK:
224 Type = L"Harddisk";
225 break;
226
227 case BBS_CDROM:
228 Type = L"CDROM";
229 break;
230
231 case BBS_PCMCIA:
232 Type = L"PCMCIAe";
233 break;
234
235 case BBS_USB:
236 Type = L"USB";
237 break;
238
239 case BBS_EMBED_NETWORK:
240 Type = L"Network";
241 break;
242
243 case BBS_BEV_DEVICE:
244 Type = L"BEVe";
245 break;
246
247 case BBS_UNKNOWN:
248 default:
249 Type = L"Unknown";
250 break;
251 }
252 //
253 // If current BBS entry has its description then use it.
254 //
255 StringDesc = (UINT8 *) (UINTN) ((CurBBSEntry->DescStringSegment << 4) + CurBBSEntry->DescStringOffset);
256 if (NULL != StringDesc) {
257 //
258 // Only get fisrt 32 characters, this is suggested by BBS spec
259 //
260 AsciiToUnicodeSize (StringDesc, 32, Temp);
261 Fmt = L"%s";
262 Type = Temp;
263 }
264
265 //
266 // BbsTable 16 entries are for onboard IDE.
267 // Set description string for SATA harddisks, Harddisk 0 ~ Harddisk 11
268 //
269 if (Index >= 5 && Index <= 16 && (CurBBSEntry->DeviceType == BBS_HARDDISK || CurBBSEntry->DeviceType == BBS_CDROM)) {
270 Fmt = L"%s %d";
271 UnicodeSPrint (BootString, BufSize, Fmt, Type, Index - 5);
272 } else {
273 UnicodeSPrint (BootString, BufSize, Fmt, Type);
274 }
275 }
276
277 /**
278
279 Create a legacy boot option for the specified entry of
280 BBS table, save it as variable, and append it to the boot
281 order list.
282
283
284 @param CurrentBbsEntry Pointer to current BBS table.
285 @param CurrentBbsDevPath Pointer to the Device Path Protocol instance of BBS
286 @param Index Index of the specified entry in BBS table.
287 @param BootOrderList On input, the original boot order list.
288 On output, the new boot order list attached with the
289 created node.
290 @param BootOrderListSize On input, the original size of boot order list.
291 On output, the size of new boot order list.
292
293 @retval EFI_SUCCESS Boot Option successfully created.
294 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
295 @retval Other Error occurs while setting variable.
296
297 **/
298 EFI_STATUS
299 BdsCreateLegacyBootOption (
300 IN BBS_TABLE *CurrentBbsEntry,
301 IN EFI_DEVICE_PATH_PROTOCOL *CurrentBbsDevPath,
302 IN UINTN Index,
303 IN OUT UINT16 **BootOrderList,
304 IN OUT UINTN *BootOrderListSize
305 )
306 {
307 EFI_STATUS Status;
308 UINT16 CurrentBootOptionNo;
309 UINT16 BootString[10];
310 CHAR16 BootDesc[100];
311 CHAR8 HelpString[100];
312 UINT16 *NewBootOrderList;
313 UINTN BufferSize;
314 UINTN StringLen;
315 VOID *Buffer;
316 UINT8 *Ptr;
317 UINT16 CurrentBbsDevPathSize;
318 UINTN BootOrderIndex;
319 UINTN BootOrderLastIndex;
320 UINTN ArrayIndex;
321 BOOLEAN IndexNotFound;
322 BBS_BBS_DEVICE_PATH *NewBbsDevPathNode;
323
324 if ((*BootOrderList) == NULL) {
325 CurrentBootOptionNo = 0;
326 } else {
327 for (ArrayIndex = 0; ArrayIndex < (UINTN) (*BootOrderListSize / sizeof (UINT16)); ArrayIndex++) {
328 IndexNotFound = TRUE;
329 for (BootOrderIndex = 0; BootOrderIndex < (UINTN) (*BootOrderListSize / sizeof (UINT16)); BootOrderIndex++) {
330 if ((*BootOrderList)[BootOrderIndex] == ArrayIndex) {
331 IndexNotFound = FALSE;
332 break;
333 }
334 }
335
336 if (!IndexNotFound) {
337 continue;
338 } else {
339 break;
340 }
341 }
342
343 CurrentBootOptionNo = (UINT16) ArrayIndex;
344 }
345
346 UnicodeSPrint (
347 BootString,
348 sizeof (BootString),
349 L"Boot%04x",
350 CurrentBootOptionNo
351 );
352
353 BdsBuildLegacyDevNameString (CurrentBbsEntry, Index, sizeof (BootDesc), BootDesc);
354
355 //
356 // Create new BBS device path node with description string
357 //
358 UnicodeStrToAsciiStr (BootDesc, HelpString);
359
360 StringLen = AsciiStrLen (HelpString);
361 NewBbsDevPathNode = AllocateZeroPool (sizeof (BBS_BBS_DEVICE_PATH) + StringLen);
362 if (NewBbsDevPathNode == NULL) {
363 return EFI_OUT_OF_RESOURCES;
364 }
365 CopyMem (NewBbsDevPathNode, CurrentBbsDevPath, sizeof (BBS_BBS_DEVICE_PATH));
366 CopyMem (NewBbsDevPathNode->String, HelpString, StringLen + 1);
367 SetDevicePathNodeLength (&(NewBbsDevPathNode->Header), sizeof (BBS_BBS_DEVICE_PATH) + StringLen);
368
369 //
370 // Create entire new CurrentBbsDevPath with end node
371 //
372 CurrentBbsDevPath = AppendDevicePathNode (
373 NULL,
374 (EFI_DEVICE_PATH_PROTOCOL *) NewBbsDevPathNode
375 );
376 if (CurrentBbsDevPath == NULL) {
377 FreePool (NewBbsDevPathNode);
378 return EFI_OUT_OF_RESOURCES;
379 }
380
381 CurrentBbsDevPathSize = (UINT16) (GetDevicePathSize (CurrentBbsDevPath));
382
383 BufferSize = sizeof (UINT32) +
384 sizeof (UINT16) +
385 StrSize (BootDesc) +
386 CurrentBbsDevPathSize +
387 sizeof (BBS_TABLE) +
388 sizeof (UINT16);
389
390 Buffer = AllocateZeroPool (BufferSize);
391 if (Buffer == NULL) {
392 FreePool (NewBbsDevPathNode);
393 FreePool (CurrentBbsDevPath);
394 return EFI_OUT_OF_RESOURCES;
395 }
396
397 Ptr = (UINT8 *) Buffer;
398
399 *((UINT32 *) Ptr) = LOAD_OPTION_ACTIVE;
400 Ptr += sizeof (UINT32);
401
402 *((UINT16 *) Ptr) = CurrentBbsDevPathSize;
403 Ptr += sizeof (UINT16);
404
405 CopyMem (
406 Ptr,
407 BootDesc,
408 StrSize (BootDesc)
409 );
410 Ptr += StrSize (BootDesc);
411
412 CopyMem (
413 Ptr,
414 CurrentBbsDevPath,
415 CurrentBbsDevPathSize
416 );
417 Ptr += CurrentBbsDevPathSize;
418
419 CopyMem (
420 Ptr,
421 CurrentBbsEntry,
422 sizeof (BBS_TABLE)
423 );
424
425 Ptr += sizeof (BBS_TABLE);
426 *((UINT16 *) Ptr) = (UINT16) Index;
427
428 Status = gRT->SetVariable (
429 BootString,
430 &gEfiGlobalVariableGuid,
431 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
432 BufferSize,
433 Buffer
434 );
435
436 FreePool (Buffer);
437
438 Buffer = NULL;
439
440 NewBootOrderList = AllocateZeroPool (*BootOrderListSize + sizeof (UINT16));
441 if (NULL == NewBootOrderList) {
442 FreePool (NewBbsDevPathNode);
443 FreePool (CurrentBbsDevPath);
444 return EFI_OUT_OF_RESOURCES;
445 }
446
447 if (*BootOrderList != NULL) {
448 CopyMem (NewBootOrderList, *BootOrderList, *BootOrderListSize);
449 FreePool (*BootOrderList);
450 }
451
452 BootOrderLastIndex = (UINTN) (*BootOrderListSize / sizeof (UINT16));
453 NewBootOrderList[BootOrderLastIndex] = CurrentBootOptionNo;
454 *BootOrderListSize += sizeof (UINT16);
455 *BootOrderList = NewBootOrderList;
456
457 FreePool (NewBbsDevPathNode);
458 FreePool (CurrentBbsDevPath);
459 return Status;
460 }
461
462 /**
463 Check if the boot option is a legacy one.
464
465 @param BootOptionVar The boot option data payload.
466 @param BbsEntry The BBS Table.
467 @param BbsIndex The table index.
468
469 @retval TRUE It is a legacy boot option.
470 @retval FALSE It is not a legacy boot option.
471
472 **/
473 BOOLEAN
474 BdsIsLegacyBootOption (
475 IN UINT8 *BootOptionVar,
476 OUT BBS_TABLE **BbsEntry,
477 OUT UINT16 *BbsIndex
478 )
479 {
480 UINT8 *Ptr;
481 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
482 BOOLEAN Ret;
483 UINT16 DevPathLen;
484
485 Ptr = BootOptionVar;
486 Ptr += sizeof (UINT32);
487 DevPathLen = *(UINT16 *) Ptr;
488 Ptr += sizeof (UINT16);
489 Ptr += StrSize ((UINT16 *) Ptr);
490 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;
491 if ((BBS_DEVICE_PATH == DevicePath->Type) && (BBS_BBS_DP == DevicePath->SubType)) {
492 Ptr += DevPathLen;
493 *BbsEntry = (BBS_TABLE *) Ptr;
494 Ptr += sizeof (BBS_TABLE);
495 *BbsIndex = *(UINT16 *) Ptr;
496 Ret = TRUE;
497 } else {
498 *BbsEntry = NULL;
499 Ret = FALSE;
500 }
501
502 return Ret;
503 }
504
505 /**
506 Delete all the invalid legacy boot options.
507
508 @retval EFI_SUCCESS All invalide legacy boot options are deleted.
509 @retval EFI_OUT_OF_RESOURCES Fail to allocate necessary memory.
510 @retval EFI_NOT_FOUND Fail to retrive variable of boot order.
511 **/
512 EFI_STATUS
513 EFIAPI
514 BdsDeleteAllInvalidLegacyBootOptions (
515 VOID
516 )
517 {
518 UINT16 *BootOrder;
519 UINT8 *BootOptionVar;
520 UINTN BootOrderSize;
521 UINTN BootOptionSize;
522 EFI_STATUS Status;
523 UINT16 HddCount;
524 UINT16 BbsCount;
525 HDD_INFO *LocalHddInfo;
526 BBS_TABLE *LocalBbsTable;
527 BBS_TABLE *BbsEntry;
528 UINT16 BbsIndex;
529 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
530 UINTN Index;
531 UINT16 BootOption[10];
532 UINT16 BootDesc[100];
533 BOOLEAN DescStringMatch;
534
535 Status = EFI_SUCCESS;
536 BootOrder = NULL;
537 BootOrderSize = 0;
538 HddCount = 0;
539 BbsCount = 0;
540 LocalHddInfo = NULL;
541 LocalBbsTable = NULL;
542 BbsEntry = NULL;
543
544 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
545 if (EFI_ERROR (Status)) {
546 return Status;
547 }
548
549 BootOrder = BdsLibGetVariableAndSize (
550 L"BootOrder",
551 &gEfiGlobalVariableGuid,
552 &BootOrderSize
553 );
554 if (BootOrder == NULL) {
555 return EFI_NOT_FOUND;
556 }
557
558 LegacyBios->GetBbsInfo (
559 LegacyBios,
560 &HddCount,
561 &LocalHddInfo,
562 &BbsCount,
563 &LocalBbsTable
564 );
565
566 Index = 0;
567 while (Index < BootOrderSize / sizeof (UINT16)) {
568 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
569 BootOptionVar = BdsLibGetVariableAndSize (
570 BootOption,
571 &gEfiGlobalVariableGuid,
572 &BootOptionSize
573 );
574 if (NULL == BootOptionVar) {
575 BootOptionSize = 0;
576 Status = gRT->GetVariable (
577 BootOption,
578 &gEfiGlobalVariableGuid,
579 NULL,
580 &BootOptionSize,
581 BootOptionVar
582 );
583 if (Status == EFI_NOT_FOUND) {
584 //
585 // Update BootOrder
586 //
587 BdsDeleteBootOption (
588 BootOrder[Index],
589 BootOrder,
590 &BootOrderSize
591 );
592 continue;
593 } else {
594 FreePool (BootOrder);
595 return EFI_OUT_OF_RESOURCES;
596 }
597 }
598
599 //
600 // Skip Non-Legacy boot option
601 //
602 if (!BdsIsLegacyBootOption (BootOptionVar, &BbsEntry, &BbsIndex)) {
603 if (BootOptionVar!= NULL) {
604 FreePool (BootOptionVar);
605 }
606 Index++;
607 continue;
608 }
609
610 if (BbsIndex < BbsCount) {
611 //
612 // Check if BBS Description String is changed
613 //
614 DescStringMatch = FALSE;
615 BdsBuildLegacyDevNameString (
616 &LocalBbsTable[BbsIndex],
617 BbsIndex,
618 sizeof (BootDesc),
619 BootDesc
620 );
621
622 if (StrCmp (BootDesc, (UINT16*)(BootOptionVar + sizeof (UINT32) + sizeof (UINT16))) == 0) {
623 DescStringMatch = TRUE;
624 }
625
626 if (!((LocalBbsTable[BbsIndex].BootPriority == BBS_IGNORE_ENTRY) ||
627 (LocalBbsTable[BbsIndex].BootPriority == BBS_DO_NOT_BOOT_FROM)) &&
628 (LocalBbsTable[BbsIndex].DeviceType == BbsEntry->DeviceType) &&
629 DescStringMatch) {
630 Index++;
631 continue;
632 }
633 }
634
635 if (BootOptionVar != NULL) {
636 FreePool (BootOptionVar);
637 }
638 //
639 // should delete
640 //
641 BdsDeleteBootOption (
642 BootOrder[Index],
643 BootOrder,
644 &BootOrderSize
645 );
646 }
647
648 //
649 // Adjust the number of boot options.
650 //
651 Status = gRT->SetVariable (
652 L"BootOrder",
653 &gEfiGlobalVariableGuid,
654 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
655 BootOrderSize,
656 BootOrder
657 );
658 //
659 // Shrinking variable with existing variable implementation shouldn't fail.
660 //
661 ASSERT_EFI_ERROR (Status);
662 FreePool (BootOrder);
663
664 return Status;
665 }
666
667 /**
668 Find all legacy boot option by device type.
669
670 @param BootOrder The boot order array.
671 @param BootOptionNum The number of boot option.
672 @param DevType Device type.
673 @param DevName Device name.
674 @param Attribute The boot option attribute.
675 @param BbsIndex The BBS table index.
676 @param OptionNumber The boot option index.
677
678 @retval TRUE The Legacy boot option is found.
679 @retval FALSE The legacy boot option is not found.
680
681 **/
682 BOOLEAN
683 BdsFindLegacyBootOptionByDevTypeAndName (
684 IN UINT16 *BootOrder,
685 IN UINTN BootOptionNum,
686 IN UINT16 DevType,
687 IN CHAR16 *DevName,
688 OUT UINT32 *Attribute,
689 OUT UINT16 *BbsIndex,
690 OUT UINT16 *OptionNumber
691 )
692 {
693 UINTN Index;
694 CHAR16 BootOption[9];
695 UINTN BootOptionSize;
696 UINT8 *BootOptionVar;
697 BBS_TABLE *BbsEntry;
698 BOOLEAN Found;
699
700 BbsEntry = NULL;
701 Found = FALSE;
702
703 if (NULL == BootOrder) {
704 return Found;
705 }
706
707 //
708 // Loop all boot option from variable
709 //
710 for (Index = 0; Index < BootOptionNum; Index++) {
711 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", (UINTN) BootOrder[Index]);
712 BootOptionVar = BdsLibGetVariableAndSize (
713 BootOption,
714 &gEfiGlobalVariableGuid,
715 &BootOptionSize
716 );
717 if (NULL == BootOptionVar) {
718 continue;
719 }
720
721 //
722 // Skip Non-legacy boot option
723 //
724 if (!BdsIsLegacyBootOption (BootOptionVar, &BbsEntry, BbsIndex)) {
725 FreePool (BootOptionVar);
726 continue;
727 }
728
729 if (
730 (BbsEntry->DeviceType != DevType) ||
731 (StrCmp (DevName, (CHAR16*)(BootOptionVar + sizeof (UINT32) + sizeof (UINT16))) != 0)
732 ) {
733 FreePool (BootOptionVar);
734 continue;
735 }
736
737 *Attribute = *(UINT32 *) BootOptionVar;
738 *OptionNumber = BootOrder[Index];
739 Found = TRUE;
740 FreePool (BootOptionVar);
741 break;
742 }
743
744 return Found;
745 }
746
747 /**
748 Create a legacy boot option.
749
750 @param BbsItem The BBS Table entry.
751 @param Index Index of the specified entry in BBS table.
752 @param BootOrderList The boot order list.
753 @param BootOrderListSize The size of boot order list.
754
755 @retval EFI_OUT_OF_RESOURCE No enough memory.
756 @retval EFI_SUCCESS The function complete successfully.
757 @return Other value if the legacy boot option is not created.
758
759 **/
760 EFI_STATUS
761 BdsCreateOneLegacyBootOption (
762 IN BBS_TABLE *BbsItem,
763 IN UINTN Index,
764 IN OUT UINT16 **BootOrderList,
765 IN OUT UINTN *BootOrderListSize
766 )
767 {
768 BBS_BBS_DEVICE_PATH BbsDevPathNode;
769 EFI_STATUS Status;
770 EFI_DEVICE_PATH_PROTOCOL *DevPath;
771
772 DevPath = NULL;
773
774 //
775 // Create device path node.
776 //
777 BbsDevPathNode.Header.Type = BBS_DEVICE_PATH;
778 BbsDevPathNode.Header.SubType = BBS_BBS_DP;
779 SetDevicePathNodeLength (&BbsDevPathNode.Header, sizeof (BBS_BBS_DEVICE_PATH));
780 BbsDevPathNode.DeviceType = BbsItem->DeviceType;
781 CopyMem (&BbsDevPathNode.StatusFlag, &BbsItem->StatusFlags, sizeof (UINT16));
782
783 DevPath = AppendDevicePathNode (
784 NULL,
785 (EFI_DEVICE_PATH_PROTOCOL *) &BbsDevPathNode
786 );
787 if (NULL == DevPath) {
788 return EFI_OUT_OF_RESOURCES;
789 }
790
791 Status = BdsCreateLegacyBootOption (
792 BbsItem,
793 DevPath,
794 Index,
795 BootOrderList,
796 BootOrderListSize
797 );
798 BbsItem->BootPriority = 0x00;
799
800 FreePool (DevPath);
801
802 return Status;
803 }
804
805 /**
806 Add the legacy boot options from BBS table if they do not exist.
807
808 @retval EFI_SUCCESS The boot options are added successfully
809 or they are already in boot options.
810 @retval EFI_NOT_FOUND No legacy boot options is found.
811 @retval EFI_OUT_OF_RESOURCE No enough memory.
812 @return Other value LegacyBoot options are not added.
813 **/
814 EFI_STATUS
815 EFIAPI
816 BdsAddNonExistingLegacyBootOptions (
817 VOID
818 )
819 {
820 UINT16 *BootOrder;
821 UINTN BootOrderSize;
822 EFI_STATUS Status;
823 CHAR16 Desc[100];
824 UINT16 HddCount;
825 UINT16 BbsCount;
826 HDD_INFO *LocalHddInfo;
827 BBS_TABLE *LocalBbsTable;
828 UINT16 BbsIndex;
829 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
830 UINT16 Index;
831 UINT32 Attribute;
832 UINT16 OptionNumber;
833 BOOLEAN Exist;
834
835 HddCount = 0;
836 BbsCount = 0;
837 LocalHddInfo = NULL;
838 LocalBbsTable = NULL;
839
840 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
841 if (EFI_ERROR (Status)) {
842 return Status;
843 }
844
845 LegacyBios->GetBbsInfo (
846 LegacyBios,
847 &HddCount,
848 &LocalHddInfo,
849 &BbsCount,
850 &LocalBbsTable
851 );
852
853 BootOrder = BdsLibGetVariableAndSize (
854 L"BootOrder",
855 &gEfiGlobalVariableGuid,
856 &BootOrderSize
857 );
858 if (BootOrder == NULL) {
859 BootOrderSize = 0;
860 }
861
862 for (Index = 0; Index < BbsCount; Index++) {
863 if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||
864 (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)
865 ) {
866 continue;
867 }
868
869 BdsBuildLegacyDevNameString (&LocalBbsTable[Index], Index, sizeof (Desc), Desc);
870
871 Exist = BdsFindLegacyBootOptionByDevTypeAndName (
872 BootOrder,
873 BootOrderSize / sizeof (UINT16),
874 LocalBbsTable[Index].DeviceType,
875 Desc,
876 &Attribute,
877 &BbsIndex,
878 &OptionNumber
879 );
880 if (!Exist) {
881 //
882 // Not found such type of legacy device in boot options or we found but it's disabled
883 // so we have to create one and put it to the tail of boot order list
884 //
885 Status = BdsCreateOneLegacyBootOption (
886 &LocalBbsTable[Index],
887 Index,
888 &BootOrder,
889 &BootOrderSize
890 );
891 if (!EFI_ERROR (Status)) {
892 ASSERT (BootOrder != NULL);
893 BbsIndex = Index;
894 OptionNumber = BootOrder[BootOrderSize / sizeof (UINT16) - 1];
895 }
896 }
897
898 ASSERT (BbsIndex == Index);
899 }
900
901 Status = gRT->SetVariable (
902 L"BootOrder",
903 &gEfiGlobalVariableGuid,
904 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
905 BootOrderSize,
906 BootOrder
907 );
908 if (BootOrder != NULL) {
909 FreePool (BootOrder);
910 }
911
912 return Status;
913 }
914
915 /**
916 Fill the device order buffer.
917
918 @param BbsTable The BBS table.
919 @param BbsType The BBS Type.
920 @param BbsCount The BBS Count.
921 @param Buf device order buffer.
922
923 @return The device order buffer.
924
925 **/
926 UINT16 *
927 BdsFillDevOrderBuf (
928 IN BBS_TABLE *BbsTable,
929 IN BBS_TYPE BbsType,
930 IN UINTN BbsCount,
931 OUT UINT16 *Buf
932 )
933 {
934 UINTN Index;
935
936 for (Index = 0; Index < BbsCount; Index++) {
937 if (BbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) {
938 continue;
939 }
940
941 if (BbsTable[Index].DeviceType != BbsType) {
942 continue;
943 }
944
945 *Buf = (UINT16) (Index & 0xFF);
946 Buf++;
947 }
948
949 return Buf;
950 }
951
952 /**
953 Create the device order buffer.
954
955 @param BbsTable The BBS table.
956 @param BbsCount The BBS Count.
957
958 @retval EFI_SUCCES The buffer is created and the EFI variable named
959 VAR_LEGACY_DEV_ORDER and gEfiLegacyDevOrderVariableGuid is
960 set correctly.
961 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
962 @retval EFI_DEVICE_ERROR Fail to add the device order into EFI variable fail
963 because of hardware error.
964 **/
965 EFI_STATUS
966 BdsCreateDevOrder (
967 IN BBS_TABLE *BbsTable,
968 IN UINT16 BbsCount
969 )
970 {
971 UINTN Index;
972 UINTN FDCount;
973 UINTN HDCount;
974 UINTN CDCount;
975 UINTN NETCount;
976 UINTN BEVCount;
977 UINTN TotalSize;
978 UINTN HeaderSize;
979 LEGACY_DEV_ORDER_ENTRY *DevOrder;
980 LEGACY_DEV_ORDER_ENTRY *DevOrderPtr;
981 EFI_STATUS Status;
982
983 FDCount = 0;
984 HDCount = 0;
985 CDCount = 0;
986 NETCount = 0;
987 BEVCount = 0;
988 TotalSize = 0;
989 HeaderSize = sizeof (BBS_TYPE) + sizeof (UINT16);
990 DevOrder = NULL;
991 Status = EFI_SUCCESS;
992
993 //
994 // Count all boot devices
995 //
996 for (Index = 0; Index < BbsCount; Index++) {
997 if (BbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) {
998 continue;
999 }
1000
1001 switch (BbsTable[Index].DeviceType) {
1002 case BBS_FLOPPY:
1003 FDCount++;
1004 break;
1005
1006 case BBS_HARDDISK:
1007 HDCount++;
1008 break;
1009
1010 case BBS_CDROM:
1011 CDCount++;
1012 break;
1013
1014 case BBS_EMBED_NETWORK:
1015 NETCount++;
1016 break;
1017
1018 case BBS_BEV_DEVICE:
1019 BEVCount++;
1020 break;
1021
1022 default:
1023 break;
1024 }
1025 }
1026
1027 TotalSize += (HeaderSize + sizeof (UINT16) * FDCount);
1028 TotalSize += (HeaderSize + sizeof (UINT16) * HDCount);
1029 TotalSize += (HeaderSize + sizeof (UINT16) * CDCount);
1030 TotalSize += (HeaderSize + sizeof (UINT16) * NETCount);
1031 TotalSize += (HeaderSize + sizeof (UINT16) * BEVCount);
1032
1033 //
1034 // Create buffer to hold all boot device order
1035 //
1036 DevOrder = AllocateZeroPool (TotalSize);
1037 if (NULL == DevOrder) {
1038 return EFI_OUT_OF_RESOURCES;
1039 }
1040 DevOrderPtr = DevOrder;
1041
1042 DevOrderPtr->BbsType = BBS_FLOPPY;
1043 DevOrderPtr->Length = (UINT16) (sizeof (DevOrderPtr->Length) + FDCount * sizeof (UINT16));
1044 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_FLOPPY, BbsCount, DevOrderPtr->Data);
1045
1046 DevOrderPtr->BbsType = BBS_HARDDISK;
1047 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + HDCount * sizeof (UINT16));
1048 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_HARDDISK, BbsCount, DevOrderPtr->Data);
1049
1050 DevOrderPtr->BbsType = BBS_CDROM;
1051 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + CDCount * sizeof (UINT16));
1052 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_CDROM, BbsCount, DevOrderPtr->Data);
1053
1054 DevOrderPtr->BbsType = BBS_EMBED_NETWORK;
1055 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + NETCount * sizeof (UINT16));
1056 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_EMBED_NETWORK, BbsCount, DevOrderPtr->Data);
1057
1058 DevOrderPtr->BbsType = BBS_BEV_DEVICE;
1059 DevOrderPtr->Length = (UINT16) (sizeof (UINT16) + BEVCount * sizeof (UINT16));
1060 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) BdsFillDevOrderBuf (BbsTable, BBS_BEV_DEVICE, BbsCount, DevOrderPtr->Data);
1061
1062 ASSERT (TotalSize == (UINTN) ((UINT8 *) DevOrderPtr - (UINT8 *) DevOrder));
1063
1064 //
1065 // Save device order for legacy boot device to variable.
1066 //
1067 Status = gRT->SetVariable (
1068 VAR_LEGACY_DEV_ORDER,
1069 &gEfiLegacyDevOrderVariableGuid,
1070 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1071 TotalSize,
1072 DevOrder
1073 );
1074 FreePool (DevOrder);
1075
1076 return Status;
1077 }
1078
1079 /**
1080 Add the legacy boot devices from BBS table into
1081 the legacy device boot order.
1082
1083 @retval EFI_SUCCESS The boot devices are added successfully.
1084 @retval EFI_NOT_FOUND The legacy boot devices are not found.
1085 @retval EFI_OUT_OF_RESOURCES Memmory or storage is not enough.
1086 @retval EFI_DEVICE_ERROR Fail to add the legacy device boot order into EFI variable
1087 because of hardware error.
1088 **/
1089 EFI_STATUS
1090 EFIAPI
1091 BdsUpdateLegacyDevOrder (
1092 VOID
1093 )
1094 {
1095 LEGACY_DEV_ORDER_ENTRY *DevOrder;
1096 LEGACY_DEV_ORDER_ENTRY *NewDevOrder;
1097 LEGACY_DEV_ORDER_ENTRY *Ptr;
1098 LEGACY_DEV_ORDER_ENTRY *NewPtr;
1099 UINTN DevOrderSize;
1100 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
1101 EFI_STATUS Status;
1102 UINT16 HddCount;
1103 UINT16 BbsCount;
1104 HDD_INFO *LocalHddInfo;
1105 BBS_TABLE *LocalBbsTable;
1106 UINTN Index;
1107 UINTN Index2;
1108 UINTN *Idx;
1109 UINTN FDCount;
1110 UINTN HDCount;
1111 UINTN CDCount;
1112 UINTN NETCount;
1113 UINTN BEVCount;
1114 UINTN TotalSize;
1115 UINTN HeaderSize;
1116 UINT16 *NewFDPtr;
1117 UINT16 *NewHDPtr;
1118 UINT16 *NewCDPtr;
1119 UINT16 *NewNETPtr;
1120 UINT16 *NewBEVPtr;
1121 UINT16 *NewDevPtr;
1122 UINTN FDIndex;
1123 UINTN HDIndex;
1124 UINTN CDIndex;
1125 UINTN NETIndex;
1126 UINTN BEVIndex;
1127
1128 Idx = NULL;
1129 FDCount = 0;
1130 HDCount = 0;
1131 CDCount = 0;
1132 NETCount = 0;
1133 BEVCount = 0;
1134 TotalSize = 0;
1135 HeaderSize = sizeof (BBS_TYPE) + sizeof (UINT16);
1136 FDIndex = 0;
1137 HDIndex = 0;
1138 CDIndex = 0;
1139 NETIndex = 0;
1140 BEVIndex = 0;
1141 NewDevPtr = NULL;
1142
1143 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
1144 if (EFI_ERROR (Status)) {
1145 return Status;
1146 }
1147
1148 Status = LegacyBios->GetBbsInfo (
1149 LegacyBios,
1150 &HddCount,
1151 &LocalHddInfo,
1152 &BbsCount,
1153 &LocalBbsTable
1154 );
1155 if (EFI_ERROR (Status)) {
1156 return Status;
1157 }
1158
1159 DevOrder = BdsLibGetVariableAndSize (
1160 VAR_LEGACY_DEV_ORDER,
1161 &gEfiLegacyDevOrderVariableGuid,
1162 &DevOrderSize
1163 );
1164 if (NULL == DevOrder) {
1165 return BdsCreateDevOrder (LocalBbsTable, BbsCount);
1166 }
1167 //
1168 // First we figure out how many boot devices with same device type respectively
1169 //
1170 for (Index = 0; Index < BbsCount; Index++) {
1171 if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||
1172 (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)
1173 ) {
1174 continue;
1175 }
1176
1177 switch (LocalBbsTable[Index].DeviceType) {
1178 case BBS_FLOPPY:
1179 FDCount++;
1180 break;
1181
1182 case BBS_HARDDISK:
1183 HDCount++;
1184 break;
1185
1186 case BBS_CDROM:
1187 CDCount++;
1188 break;
1189
1190 case BBS_EMBED_NETWORK:
1191 NETCount++;
1192 break;
1193
1194 case BBS_BEV_DEVICE:
1195 BEVCount++;
1196 break;
1197
1198 default:
1199 break;
1200 }
1201 }
1202
1203 TotalSize += (HeaderSize + FDCount * sizeof (UINT16));
1204 TotalSize += (HeaderSize + HDCount * sizeof (UINT16));
1205 TotalSize += (HeaderSize + CDCount * sizeof (UINT16));
1206 TotalSize += (HeaderSize + NETCount * sizeof (UINT16));
1207 TotalSize += (HeaderSize + BEVCount * sizeof (UINT16));
1208
1209 NewDevOrder = AllocateZeroPool (TotalSize);
1210 if (NULL == NewDevOrder) {
1211 return EFI_OUT_OF_RESOURCES;
1212 }
1213
1214
1215
1216 //
1217 // copy FD
1218 //
1219 Ptr = DevOrder;
1220 NewPtr = NewDevOrder;
1221 NewPtr->BbsType = Ptr->BbsType;
1222 NewPtr->Length = (UINT16) (sizeof (UINT16) + FDCount * sizeof (UINT16));
1223 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {
1224 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
1225 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
1226 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_FLOPPY
1227 ) {
1228 continue;
1229 }
1230
1231 NewPtr->Data[FDIndex] = Ptr->Data[Index];
1232 FDIndex++;
1233 }
1234 NewFDPtr = NewPtr->Data;
1235
1236 //
1237 // copy HD
1238 //
1239 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
1240 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
1241 NewPtr->BbsType = Ptr->BbsType;
1242 NewPtr->Length = (UINT16) (sizeof (UINT16) + HDCount * sizeof (UINT16));
1243 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {
1244 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
1245 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
1246 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
1247 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_HARDDISK
1248 ) {
1249 continue;
1250 }
1251
1252 NewPtr->Data[HDIndex] = Ptr->Data[Index];
1253 HDIndex++;
1254 }
1255 NewHDPtr = NewPtr->Data;
1256
1257 //
1258 // copy CD
1259 //
1260 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
1261 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
1262 NewPtr->BbsType = Ptr->BbsType;
1263 NewPtr->Length = (UINT16) (sizeof (UINT16) + CDCount * sizeof (UINT16));
1264 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {
1265 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
1266 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
1267 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
1268 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_CDROM
1269 ) {
1270 continue;
1271 }
1272
1273 NewPtr->Data[CDIndex] = Ptr->Data[Index];
1274 CDIndex++;
1275 }
1276 NewCDPtr = NewPtr->Data;
1277
1278 //
1279 // copy NET
1280 //
1281 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
1282 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
1283 NewPtr->BbsType = Ptr->BbsType;
1284 NewPtr->Length = (UINT16) (sizeof (UINT16) + NETCount * sizeof (UINT16));
1285 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {
1286 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
1287 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
1288 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
1289 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_EMBED_NETWORK
1290 ) {
1291 continue;
1292 }
1293
1294 NewPtr->Data[NETIndex] = Ptr->Data[Index];
1295 NETIndex++;
1296 }
1297 NewNETPtr = NewPtr->Data;
1298
1299 //
1300 // copy BEV
1301 //
1302 Ptr = (LEGACY_DEV_ORDER_ENTRY *) (&Ptr->Data[Ptr->Length / sizeof (UINT16) - 1]);
1303 NewPtr = (LEGACY_DEV_ORDER_ENTRY *) (&NewPtr->Data[NewPtr->Length / sizeof (UINT16) -1]);
1304 NewPtr->BbsType = Ptr->BbsType;
1305 NewPtr->Length = (UINT16) (sizeof (UINT16) + BEVCount * sizeof (UINT16));
1306 for (Index = 0; Index < Ptr->Length / sizeof (UINT16) - 1; Index++) {
1307 if (LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_IGNORE_ENTRY ||
1308 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_DO_NOT_BOOT_FROM ||
1309 LocalBbsTable[Ptr->Data[Index] & 0xFF].BootPriority == BBS_LOWEST_PRIORITY ||
1310 LocalBbsTable[Ptr->Data[Index] & 0xFF].DeviceType != BBS_BEV_DEVICE
1311 ) {
1312 continue;
1313 }
1314
1315 NewPtr->Data[BEVIndex] = Ptr->Data[Index];
1316 BEVIndex++;
1317 }
1318 NewBEVPtr = NewPtr->Data;
1319
1320 for (Index = 0; Index < BbsCount; Index++) {
1321 if ((LocalBbsTable[Index].BootPriority == BBS_IGNORE_ENTRY) ||
1322 (LocalBbsTable[Index].BootPriority == BBS_DO_NOT_BOOT_FROM)
1323 ) {
1324 continue;
1325 }
1326
1327 switch (LocalBbsTable[Index].DeviceType) {
1328 case BBS_FLOPPY:
1329 Idx = &FDIndex;
1330 NewDevPtr = NewFDPtr;
1331 break;
1332
1333 case BBS_HARDDISK:
1334 Idx = &HDIndex;
1335 NewDevPtr = NewHDPtr;
1336 break;
1337
1338 case BBS_CDROM:
1339 Idx = &CDIndex;
1340 NewDevPtr = NewCDPtr;
1341 break;
1342
1343 case BBS_EMBED_NETWORK:
1344 Idx = &NETIndex;
1345 NewDevPtr = NewNETPtr;
1346 break;
1347
1348 case BBS_BEV_DEVICE:
1349 Idx = &BEVIndex;
1350 NewDevPtr = NewBEVPtr;
1351 break;
1352
1353 default:
1354 Idx = NULL;
1355 break;
1356 }
1357 //
1358 // at this point we have copied those valid indexes to new buffer
1359 // and we should check if there is any new appeared boot device
1360 //
1361 if (Idx != NULL) {
1362 for (Index2 = 0; Index2 < *Idx; Index2++) {
1363 if ((NewDevPtr[Index2] & 0xFF) == (UINT16) Index) {
1364 break;
1365 }
1366 }
1367
1368 if (Index2 == *Idx) {
1369 //
1370 // Index2 == *Idx means we didn't find Index
1371 // so Index is a new appeared device's index in BBS table
1372 // insert it before disabled indexes.
1373 //
1374 for (Index2 = 0; Index2 < *Idx; Index2++) {
1375 if ((NewDevPtr[Index2] & 0xFF00) == 0xFF00) {
1376 break;
1377 }
1378 }
1379 CopyMem (&NewDevPtr[Index2 + 1], &NewDevPtr[Index2], (*Idx - Index2) * sizeof (UINT16));
1380 NewDevPtr[Index2] = (UINT16) (Index & 0xFF);
1381 (*Idx)++;
1382 }
1383 }
1384 }
1385
1386 FreePool (DevOrder);
1387
1388 Status = gRT->SetVariable (
1389 VAR_LEGACY_DEV_ORDER,
1390 &gEfiLegacyDevOrderVariableGuid,
1391 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1392 TotalSize,
1393 NewDevOrder
1394 );
1395 FreePool (NewDevOrder);
1396
1397 return Status;
1398 }
1399
1400 /**
1401 Set Boot Priority for specified device type.
1402
1403 @param DeviceType The device type.
1404 @param BbsIndex The BBS index to set the highest priority. Ignore when -1.
1405 @param LocalBbsTable The BBS table.
1406 @param Priority The prority table.
1407
1408 @retval EFI_SUCCESS The function completes successfully.
1409 @retval EFI_NOT_FOUND Failed to find device.
1410 @retval EFI_OUT_OF_RESOURCES Failed to get the efi variable of device order.
1411
1412 **/
1413 EFI_STATUS
1414 BdsSetBootPriority4SameTypeDev (
1415 IN UINT16 DeviceType,
1416 IN UINTN BbsIndex,
1417 IN OUT BBS_TABLE *LocalBbsTable,
1418 IN OUT UINT16 *Priority
1419 )
1420 {
1421 LEGACY_DEV_ORDER_ENTRY *DevOrder;
1422 LEGACY_DEV_ORDER_ENTRY *DevOrderPtr;
1423 UINTN DevOrderSize;
1424 UINTN Index;
1425
1426 DevOrder = BdsLibGetVariableAndSize (
1427 VAR_LEGACY_DEV_ORDER,
1428 &gEfiLegacyDevOrderVariableGuid,
1429 &DevOrderSize
1430 );
1431 if (NULL == DevOrder) {
1432 return EFI_OUT_OF_RESOURCES;
1433 }
1434
1435 DevOrderPtr = DevOrder;
1436 while ((UINT8 *) DevOrderPtr < (UINT8 *) DevOrder + DevOrderSize) {
1437 if (DevOrderPtr->BbsType == DeviceType) {
1438 break;
1439 }
1440
1441 DevOrderPtr = (LEGACY_DEV_ORDER_ENTRY *) ((UINTN) DevOrderPtr + sizeof (BBS_TYPE) + DevOrderPtr->Length);
1442 }
1443
1444 if ((UINT8 *) DevOrderPtr >= (UINT8 *) DevOrder + DevOrderSize) {
1445 FreePool (DevOrder);
1446 return EFI_NOT_FOUND;
1447 }
1448
1449 if (BbsIndex != (UINTN) -1) {
1450 LocalBbsTable[BbsIndex].BootPriority = *Priority;
1451 (*Priority)++;
1452 }
1453 //
1454 // If the high byte of the DevIndex is 0xFF, it indicates that this device has been disabled.
1455 //
1456 for (Index = 0; Index < DevOrderPtr->Length / sizeof (UINT16) - 1; Index++) {
1457 if ((DevOrderPtr->Data[Index] & 0xFF00) == 0xFF00) {
1458 //
1459 // LocalBbsTable[DevIndex[Index] & 0xFF].BootPriority = BBS_DISABLED_ENTRY;
1460 //
1461 } else if (DevOrderPtr->Data[Index] != BbsIndex) {
1462 LocalBbsTable[DevOrderPtr->Data[Index]].BootPriority = *Priority;
1463 (*Priority)++;
1464 }
1465 }
1466
1467 FreePool (DevOrder);
1468 return EFI_SUCCESS;
1469 }
1470
1471 /**
1472 Print the BBS Table.
1473
1474 @param LocalBbsTable The BBS table.
1475 @param BbsCount The count of entry in BBS table.
1476 **/
1477 VOID
1478 PrintBbsTable (
1479 IN BBS_TABLE *LocalBbsTable,
1480 IN UINT16 BbsCount
1481 )
1482 {
1483 UINT16 Idx;
1484
1485 DEBUG ((DEBUG_ERROR, "\n"));
1486 DEBUG ((DEBUG_ERROR, " NO Prio bb/dd/ff cl/sc Type Stat segm:offs\n"));
1487 DEBUG ((DEBUG_ERROR, "=============================================\n"));
1488 for (Idx = 0; Idx < BbsCount; Idx++) {
1489 if ((LocalBbsTable[Idx].BootPriority == BBS_IGNORE_ENTRY) ||
1490 (LocalBbsTable[Idx].BootPriority == BBS_DO_NOT_BOOT_FROM) ||
1491 (LocalBbsTable[Idx].BootPriority == BBS_LOWEST_PRIORITY)
1492 ) {
1493 continue;
1494 }
1495
1496 DEBUG (
1497 (DEBUG_ERROR,
1498 " %02x: %04x %02x/%02x/%02x %02x/%02x %04x %04x %04x:%04x\n",
1499 (UINTN) Idx,
1500 (UINTN) LocalBbsTable[Idx].BootPriority,
1501 (UINTN) LocalBbsTable[Idx].Bus,
1502 (UINTN) LocalBbsTable[Idx].Device,
1503 (UINTN) LocalBbsTable[Idx].Function,
1504 (UINTN) LocalBbsTable[Idx].Class,
1505 (UINTN) LocalBbsTable[Idx].SubClass,
1506 (UINTN) LocalBbsTable[Idx].DeviceType,
1507 (UINTN) * (UINT16 *) &LocalBbsTable[Idx].StatusFlags,
1508 (UINTN) LocalBbsTable[Idx].BootHandlerSegment,
1509 (UINTN) LocalBbsTable[Idx].BootHandlerOffset,
1510 (UINTN) ((LocalBbsTable[Idx].MfgStringSegment << 4) + LocalBbsTable[Idx].MfgStringOffset),
1511 (UINTN) ((LocalBbsTable[Idx].DescStringSegment << 4) + LocalBbsTable[Idx].DescStringOffset))
1512 );
1513 }
1514
1515 DEBUG ((DEBUG_ERROR, "\n"));
1516 }
1517
1518 /**
1519 Set the boot priority for BBS entries based on boot option entry and boot order.
1520
1521 @param Entry The boot option is to be checked for refresh BBS table.
1522
1523 @retval EFI_SUCCESS The boot priority for BBS entries is refreshed successfully.
1524 @retval EFI_NOT_FOUND BBS entries can't be found.
1525 @retval EFI_OUT_OF_RESOURCES Failed to get the legacy device boot order.
1526 **/
1527 EFI_STATUS
1528 EFIAPI
1529 BdsRefreshBbsTableForBoot (
1530 IN BDS_COMMON_OPTION *Entry
1531 )
1532 {
1533 EFI_STATUS Status;
1534 UINT16 BbsIndex;
1535 UINT16 HddCount;
1536 UINT16 BbsCount;
1537 HDD_INFO *LocalHddInfo;
1538 BBS_TABLE *LocalBbsTable;
1539 UINT16 DevType;
1540 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
1541 UINTN Index;
1542 UINT16 Priority;
1543 UINT16 *BootOrder;
1544 UINTN BootOrderSize;
1545 UINT8 *BootOptionVar;
1546 UINTN BootOptionSize;
1547 CHAR16 BootOption[9];
1548 UINT8 *Ptr;
1549 UINT16 DevPathLen;
1550 EFI_DEVICE_PATH_PROTOCOL *DevPath;
1551 UINT16 *DeviceType;
1552 UINTN DeviceTypeCount;
1553 UINTN DeviceTypeIndex;
1554
1555 HddCount = 0;
1556 BbsCount = 0;
1557 LocalHddInfo = NULL;
1558 LocalBbsTable = NULL;
1559 DevType = BBS_UNKNOWN;
1560
1561 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
1562 if (EFI_ERROR (Status)) {
1563 return Status;
1564 }
1565
1566 LegacyBios->GetBbsInfo (
1567 LegacyBios,
1568 &HddCount,
1569 &LocalHddInfo,
1570 &BbsCount,
1571 &LocalBbsTable
1572 );
1573 //
1574 // First, set all the present devices' boot priority to BBS_UNPRIORITIZED_ENTRY
1575 // We will set them according to the settings setup by user
1576 //
1577 for (Index = 0; Index < BbsCount; Index++) {
1578 if (!((BBS_IGNORE_ENTRY == LocalBbsTable[Index].BootPriority) ||
1579 (BBS_DO_NOT_BOOT_FROM == LocalBbsTable[Index].BootPriority) ||
1580 (BBS_LOWEST_PRIORITY == LocalBbsTable[Index].BootPriority))) {
1581 LocalBbsTable[Index].BootPriority = BBS_UNPRIORITIZED_ENTRY;
1582 }
1583 }
1584 //
1585 // boot priority always starts at 0
1586 //
1587 Priority = 0;
1588 if (Entry->LoadOptionsSize == sizeof (BBS_TABLE) + sizeof (UINT16)) {
1589 //
1590 // If Entry stands for a legacy boot option, we prioritize the devices with the same type first.
1591 //
1592 DevType = ((BBS_TABLE *) Entry->LoadOptions)->DeviceType;
1593 BbsIndex = *(UINT16 *) ((BBS_TABLE *) Entry->LoadOptions + 1);
1594 Status = BdsSetBootPriority4SameTypeDev (
1595 DevType,
1596 BbsIndex,
1597 LocalBbsTable,
1598 &Priority
1599 );
1600 if (EFI_ERROR (Status)) {
1601 return Status;
1602 }
1603 }
1604 //
1605 // we have to set the boot priority for other BBS entries with different device types
1606 //
1607 BootOrder = BdsLibGetVariableAndSize (
1608 L"BootOrder",
1609 &gEfiGlobalVariableGuid,
1610 &BootOrderSize
1611 );
1612 DeviceType = AllocatePool (BootOrderSize + sizeof (UINT16));
1613 ASSERT (DeviceType != NULL);
1614
1615 DeviceType[0] = DevType;
1616 DeviceTypeCount = 1;
1617 for (Index = 0; ((BootOrder != NULL) && (Index < BootOrderSize / sizeof (UINT16))); Index++) {
1618 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
1619 BootOptionVar = BdsLibGetVariableAndSize (
1620 BootOption,
1621 &gEfiGlobalVariableGuid,
1622 &BootOptionSize
1623 );
1624 if (NULL == BootOptionVar) {
1625 continue;
1626 }
1627
1628 Ptr = BootOptionVar;
1629
1630 Ptr += sizeof (UINT32);
1631 DevPathLen = *(UINT16 *) Ptr;
1632 Ptr += sizeof (UINT16);
1633 Ptr += StrSize ((UINT16 *) Ptr);
1634 DevPath = (EFI_DEVICE_PATH_PROTOCOL *) Ptr;
1635 if (BBS_DEVICE_PATH != DevPath->Type || BBS_BBS_DP != DevPath->SubType) {
1636 FreePool (BootOptionVar);
1637 continue;
1638 }
1639
1640 Ptr += DevPathLen;
1641 DevType = ((BBS_TABLE *) Ptr)->DeviceType;
1642 for (DeviceTypeIndex = 0; DeviceTypeIndex < DeviceTypeCount; DeviceTypeIndex++) {
1643 if (DeviceType[DeviceTypeIndex] == DevType) {
1644 break;
1645 }
1646 }
1647 if (DeviceTypeIndex < DeviceTypeCount) {
1648 //
1649 // We don't want to process twice for a device type
1650 //
1651 FreePool (BootOptionVar);
1652 continue;
1653 }
1654
1655 DeviceType[DeviceTypeCount] = DevType;
1656 DeviceTypeCount++;
1657
1658 Status = BdsSetBootPriority4SameTypeDev (
1659 DevType,
1660 (UINTN) -1,
1661 LocalBbsTable,
1662 &Priority
1663 );
1664 FreePool (BootOptionVar);
1665 if (EFI_ERROR (Status)) {
1666 break;
1667 }
1668 }
1669
1670 FreePool (DeviceType);
1671
1672 if (BootOrder != NULL) {
1673 FreePool (BootOrder);
1674 }
1675
1676 DEBUG_CODE_BEGIN();
1677 PrintBbsTable (LocalBbsTable, BbsCount);
1678 DEBUG_CODE_END();
1679
1680 return Status;
1681 }
1682
1683 /**
1684 Boot the legacy system with the boot option
1685
1686 @param Option The legacy boot option which have BBS device path
1687
1688 @retval EFI_UNSUPPORTED There is no legacybios protocol, do not support
1689 legacy boot.
1690 @retval EFI_STATUS Return the status of LegacyBios->LegacyBoot ().
1691
1692 **/
1693 EFI_STATUS
1694 BdsLibDoLegacyBoot (
1695 IN BDS_COMMON_OPTION *Option
1696 )
1697 {
1698 EFI_STATUS Status;
1699 EFI_LEGACY_BIOS_PROTOCOL *LegacyBios;
1700 EFI_EVENT LegacyBootEvent;
1701
1702 Status = gBS->LocateProtocol (&gEfiLegacyBiosProtocolGuid, NULL, (VOID **) &LegacyBios);
1703 if (EFI_ERROR (Status)) {
1704 //
1705 // If no LegacyBios protocol we do not support legacy boot
1706 //
1707 return EFI_UNSUPPORTED;
1708 }
1709 //
1710 // Notes: if we separate the int 19, then we don't need to refresh BBS
1711 //
1712 BdsRefreshBbsTableForBoot (Option);
1713
1714 //
1715 // Write boot to OS performance data for legacy boot.
1716 //
1717 PERF_CODE (
1718 //
1719 // Create an event to be signalled when Legacy Boot occurs to write performance data.
1720 //
1721 Status = EfiCreateEventLegacyBootEx(
1722 TPL_NOTIFY,
1723 BmEndOfBdsPerfCode,
1724 NULL,
1725 &LegacyBootEvent
1726 );
1727 ASSERT_EFI_ERROR (Status);
1728 );
1729
1730 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Legacy Boot: %S\n", Option->Description));
1731 return LegacyBios->LegacyBoot (
1732 LegacyBios,
1733 (BBS_BBS_DEVICE_PATH *) Option->DevicePath,
1734 Option->LoadOptionsSize,
1735 Option->LoadOptions
1736 );
1737 }
1738
1739 /**
1740 Internal function to check if the input boot option is a valid EFI NV Boot####.
1741
1742 @param OptionToCheck Boot option to be checked.
1743
1744 @retval TRUE This boot option matches a valid EFI NV Boot####.
1745 @retval FALSE If not.
1746
1747 **/
1748 BOOLEAN
1749 IsBootOptionValidNVVarialbe (
1750 IN BDS_COMMON_OPTION *OptionToCheck
1751 )
1752 {
1753 LIST_ENTRY TempList;
1754 BDS_COMMON_OPTION *BootOption;
1755 BOOLEAN Valid;
1756 CHAR16 OptionName[20];
1757
1758 Valid = FALSE;
1759
1760 InitializeListHead (&TempList);
1761 UnicodeSPrint (OptionName, sizeof (OptionName), L"Boot%04x", OptionToCheck->BootCurrent);
1762
1763 BootOption = BdsLibVariableToOption (&TempList, OptionName);
1764 if (BootOption == NULL) {
1765 return FALSE;
1766 }
1767
1768 //
1769 // If the Boot Option Number and Device Path matches, OptionToCheck matches a
1770 // valid EFI NV Boot####.
1771 //
1772 if ((OptionToCheck->BootCurrent == BootOption->BootCurrent) &&
1773 (CompareMem (OptionToCheck->DevicePath, BootOption->DevicePath, GetDevicePathSize (OptionToCheck->DevicePath)) == 0))
1774 {
1775 Valid = TRUE;
1776 }
1777
1778 FreePool (BootOption);
1779
1780 return Valid;
1781 }
1782
1783 /**
1784 Check whether a USB device match the specified USB Class device path. This
1785 function follows "Load Option Processing" behavior in UEFI specification.
1786
1787 @param UsbIo USB I/O protocol associated with the USB device.
1788 @param UsbClass The USB Class device path to match.
1789
1790 @retval TRUE The USB device match the USB Class device path.
1791 @retval FALSE The USB device does not match the USB Class device path.
1792
1793 **/
1794 BOOLEAN
1795 BdsMatchUsbClass (
1796 IN EFI_USB_IO_PROTOCOL *UsbIo,
1797 IN USB_CLASS_DEVICE_PATH *UsbClass
1798 )
1799 {
1800 EFI_STATUS Status;
1801 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
1802 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
1803 UINT8 DeviceClass;
1804 UINT8 DeviceSubClass;
1805 UINT8 DeviceProtocol;
1806
1807 if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||
1808 (DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP)){
1809 return FALSE;
1810 }
1811
1812 //
1813 // Check Vendor Id and Product Id.
1814 //
1815 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
1816 if (EFI_ERROR (Status)) {
1817 return FALSE;
1818 }
1819
1820 if ((UsbClass->VendorId != 0xffff) &&
1821 (UsbClass->VendorId != DevDesc.IdVendor)) {
1822 return FALSE;
1823 }
1824
1825 if ((UsbClass->ProductId != 0xffff) &&
1826 (UsbClass->ProductId != DevDesc.IdProduct)) {
1827 return FALSE;
1828 }
1829
1830 DeviceClass = DevDesc.DeviceClass;
1831 DeviceSubClass = DevDesc.DeviceSubClass;
1832 DeviceProtocol = DevDesc.DeviceProtocol;
1833 if (DeviceClass == 0) {
1834 //
1835 // If Class in Device Descriptor is set to 0, use the Class, SubClass and
1836 // Protocol in Interface Descriptor instead.
1837 //
1838 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
1839 if (EFI_ERROR (Status)) {
1840 return FALSE;
1841 }
1842
1843 DeviceClass = IfDesc.InterfaceClass;
1844 DeviceSubClass = IfDesc.InterfaceSubClass;
1845 DeviceProtocol = IfDesc.InterfaceProtocol;
1846 }
1847
1848 //
1849 // Check Class, SubClass and Protocol.
1850 //
1851 if ((UsbClass->DeviceClass != 0xff) &&
1852 (UsbClass->DeviceClass != DeviceClass)) {
1853 return FALSE;
1854 }
1855
1856 if ((UsbClass->DeviceSubClass != 0xff) &&
1857 (UsbClass->DeviceSubClass != DeviceSubClass)) {
1858 return FALSE;
1859 }
1860
1861 if ((UsbClass->DeviceProtocol != 0xff) &&
1862 (UsbClass->DeviceProtocol != DeviceProtocol)) {
1863 return FALSE;
1864 }
1865
1866 return TRUE;
1867 }
1868
1869 /**
1870 Check whether a USB device match the specified USB WWID device path. This
1871 function follows "Load Option Processing" behavior in UEFI specification.
1872
1873 @param UsbIo USB I/O protocol associated with the USB device.
1874 @param UsbWwid The USB WWID device path to match.
1875
1876 @retval TRUE The USB device match the USB WWID device path.
1877 @retval FALSE The USB device does not match the USB WWID device path.
1878
1879 **/
1880 BOOLEAN
1881 BdsMatchUsbWwid (
1882 IN EFI_USB_IO_PROTOCOL *UsbIo,
1883 IN USB_WWID_DEVICE_PATH *UsbWwid
1884 )
1885 {
1886 EFI_STATUS Status;
1887 EFI_USB_DEVICE_DESCRIPTOR DevDesc;
1888 EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
1889 UINT16 *LangIdTable;
1890 UINT16 TableSize;
1891 UINT16 Index;
1892 CHAR16 *CompareStr;
1893 UINTN CompareLen;
1894 CHAR16 *SerialNumberStr;
1895 UINTN Length;
1896
1897 if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||
1898 (DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP )){
1899 return FALSE;
1900 }
1901
1902 //
1903 // Check Vendor Id and Product Id.
1904 //
1905 Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
1906 if (EFI_ERROR (Status)) {
1907 return FALSE;
1908 }
1909 if ((DevDesc.IdVendor != UsbWwid->VendorId) ||
1910 (DevDesc.IdProduct != UsbWwid->ProductId)) {
1911 return FALSE;
1912 }
1913
1914 //
1915 // Check Interface Number.
1916 //
1917 Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
1918 if (EFI_ERROR (Status)) {
1919 return FALSE;
1920 }
1921 if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {
1922 return FALSE;
1923 }
1924
1925 //
1926 // Check Serial Number.
1927 //
1928 if (DevDesc.StrSerialNumber == 0) {
1929 return FALSE;
1930 }
1931
1932 //
1933 // Get all supported languages.
1934 //
1935 TableSize = 0;
1936 LangIdTable = NULL;
1937 Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);
1938 if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {
1939 return FALSE;
1940 }
1941
1942 //
1943 // Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
1944 //
1945 CompareStr = (CHAR16 *) (UINTN) (UsbWwid + 1);
1946 CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);
1947 if (CompareStr[CompareLen - 1] == L'\0') {
1948 CompareLen--;
1949 }
1950
1951 //
1952 // Compare serial number in each supported language.
1953 //
1954 for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {
1955 SerialNumberStr = NULL;
1956 Status = UsbIo->UsbGetStringDescriptor (
1957 UsbIo,
1958 LangIdTable[Index],
1959 DevDesc.StrSerialNumber,
1960 &SerialNumberStr
1961 );
1962 if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {
1963 continue;
1964 }
1965
1966 Length = StrLen (SerialNumberStr);
1967 if ((Length >= CompareLen) &&
1968 (CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0)) {
1969 FreePool (SerialNumberStr);
1970 return TRUE;
1971 }
1972
1973 FreePool (SerialNumberStr);
1974 }
1975
1976 return FALSE;
1977 }
1978
1979 /**
1980 Find a USB device path which match the specified short-form device path start
1981 with USB Class or USB WWID device path and load the boot file then return the
1982 image handle. If ParentDevicePath is NULL, this function will search in all USB
1983 devices of the platform. If ParentDevicePath is not NULL,this function will only
1984 search in its child devices.
1985
1986 @param ParentDevicePath The device path of the parent.
1987 @param ShortFormDevicePath The USB Class or USB WWID device path to match.
1988
1989 @return The image Handle if find load file from specified short-form device path
1990 or NULL if not found.
1991
1992 **/
1993 EFI_HANDLE *
1994 BdsFindUsbDevice (
1995 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
1996 IN EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath
1997 )
1998 {
1999 EFI_STATUS Status;
2000 UINTN UsbIoHandleCount;
2001 EFI_HANDLE *UsbIoHandleBuffer;
2002 EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;
2003 EFI_USB_IO_PROTOCOL *UsbIo;
2004 UINTN Index;
2005 UINTN ParentSize;
2006 UINTN Size;
2007 EFI_HANDLE ImageHandle;
2008 EFI_HANDLE Handle;
2009 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
2010 EFI_DEVICE_PATH_PROTOCOL *NextDevicePath;
2011
2012 FullDevicePath = NULL;
2013 ImageHandle = NULL;
2014
2015 //
2016 // Get all UsbIo Handles.
2017 //
2018 UsbIoHandleCount = 0;
2019 UsbIoHandleBuffer = NULL;
2020 Status = gBS->LocateHandleBuffer (
2021 ByProtocol,
2022 &gEfiUsbIoProtocolGuid,
2023 NULL,
2024 &UsbIoHandleCount,
2025 &UsbIoHandleBuffer
2026 );
2027 if (EFI_ERROR (Status) || (UsbIoHandleCount == 0) || (UsbIoHandleBuffer == NULL)) {
2028 return NULL;
2029 }
2030
2031 ParentSize = (ParentDevicePath == NULL) ? 0 : GetDevicePathSize (ParentDevicePath);
2032 for (Index = 0; Index < UsbIoHandleCount; Index++) {
2033 //
2034 // Get the Usb IO interface.
2035 //
2036 Status = gBS->HandleProtocol(
2037 UsbIoHandleBuffer[Index],
2038 &gEfiUsbIoProtocolGuid,
2039 (VOID **) &UsbIo
2040 );
2041 if (EFI_ERROR (Status)) {
2042 continue;
2043 }
2044
2045 UsbIoDevicePath = DevicePathFromHandle (UsbIoHandleBuffer[Index]);
2046 if (UsbIoDevicePath == NULL) {
2047 continue;
2048 }
2049
2050 if (ParentDevicePath != NULL) {
2051 //
2052 // Compare starting part of UsbIoHandle's device path with ParentDevicePath.
2053 //
2054 Size = GetDevicePathSize (UsbIoDevicePath);
2055 if ((Size < ParentSize) ||
2056 (CompareMem (UsbIoDevicePath, ParentDevicePath, ParentSize - END_DEVICE_PATH_LENGTH) != 0)) {
2057 continue;
2058 }
2059 }
2060
2061 if (BdsMatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *) ShortFormDevicePath) ||
2062 BdsMatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *) ShortFormDevicePath)) {
2063 //
2064 // Try to find if there is the boot file in this DevicePath
2065 //
2066 NextDevicePath = NextDevicePathNode (ShortFormDevicePath);
2067 if (!IsDevicePathEnd (NextDevicePath)) {
2068 FullDevicePath = AppendDevicePath (UsbIoDevicePath, NextDevicePath);
2069 //
2070 // Connect the full device path, so that Simple File System protocol
2071 // could be installed for this USB device.
2072 //
2073 BdsLibConnectDevicePath (FullDevicePath);
2074 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
2075 Status = gBS->LoadImage (
2076 TRUE,
2077 gImageHandle,
2078 FullDevicePath,
2079 NULL,
2080 0,
2081 &ImageHandle
2082 );
2083 FreePool (FullDevicePath);
2084 } else {
2085 FullDevicePath = UsbIoDevicePath;
2086 Status = EFI_NOT_FOUND;
2087 }
2088
2089 //
2090 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2091 // and load the image according to the default boot behavior for removable device.
2092 //
2093 if (EFI_ERROR (Status)) {
2094 //
2095 // check if there is a bootable removable media could be found in this device path ,
2096 // and get the bootable media handle
2097 //
2098 Handle = BdsLibGetBootableHandle(UsbIoDevicePath);
2099 if (Handle == NULL) {
2100 continue;
2101 }
2102 //
2103 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2104 // machinename is ia32, ia64, x64, ...
2105 //
2106 FullDevicePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
2107 if (FullDevicePath != NULL) {
2108 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
2109 Status = gBS->LoadImage (
2110 TRUE,
2111 gImageHandle,
2112 FullDevicePath,
2113 NULL,
2114 0,
2115 &ImageHandle
2116 );
2117 if (EFI_ERROR (Status)) {
2118 //
2119 // The DevicePath failed, and it's not a valid
2120 // removable media device.
2121 //
2122 continue;
2123 }
2124 } else {
2125 continue;
2126 }
2127 }
2128 break;
2129 }
2130 }
2131
2132 FreePool (UsbIoHandleBuffer);
2133 return ImageHandle;
2134 }
2135
2136 /**
2137 Expand USB Class or USB WWID device path node to be full device path of a USB
2138 device in platform then load the boot file on this full device path and return the
2139 image handle.
2140
2141 This function support following 4 cases:
2142 1) Boot Option device path starts with a USB Class or USB WWID device path,
2143 and there is no Media FilePath device path in the end.
2144 In this case, it will follow Removable Media Boot Behavior.
2145 2) Boot Option device path starts with a USB Class or USB WWID device path,
2146 and ended with Media FilePath device path.
2147 3) Boot Option device path starts with a full device path to a USB Host Controller,
2148 contains a USB Class or USB WWID device path node, while not ended with Media
2149 FilePath device path. In this case, it will follow Removable Media Boot Behavior.
2150 4) Boot Option device path starts with a full device path to a USB Host Controller,
2151 contains a USB Class or USB WWID device path node, and ended with Media
2152 FilePath device path.
2153
2154 @param DevicePath The Boot Option device path.
2155
2156 @return The image handle of boot file, or NULL if there is no boot file found in
2157 the specified USB Class or USB WWID device path.
2158
2159 **/
2160 EFI_HANDLE *
2161 BdsExpandUsbShortFormDevicePath (
2162 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
2163 )
2164 {
2165 EFI_HANDLE *ImageHandle;
2166 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
2167 EFI_DEVICE_PATH_PROTOCOL *ShortFormDevicePath;
2168
2169 //
2170 // Search for USB Class or USB WWID device path node.
2171 //
2172 ShortFormDevicePath = NULL;
2173 ImageHandle = NULL;
2174 TempDevicePath = DevicePath;
2175 while (!IsDevicePathEnd (TempDevicePath)) {
2176 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&
2177 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||
2178 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {
2179 ShortFormDevicePath = TempDevicePath;
2180 break;
2181 }
2182 TempDevicePath = NextDevicePathNode (TempDevicePath);
2183 }
2184
2185 if (ShortFormDevicePath == NULL) {
2186 //
2187 // No USB Class or USB WWID device path node found, do nothing.
2188 //
2189 return NULL;
2190 }
2191
2192 if (ShortFormDevicePath == DevicePath) {
2193 //
2194 // Boot Option device path starts with USB Class or USB WWID device path.
2195 //
2196 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);
2197 if (ImageHandle == NULL) {
2198 //
2199 // Failed to find a match in existing devices, connect the short form USB
2200 // device path and try again.
2201 //
2202 BdsLibConnectUsbDevByShortFormDP (0xff, ShortFormDevicePath);
2203 ImageHandle = BdsFindUsbDevice (NULL, ShortFormDevicePath);
2204 }
2205 } else {
2206 //
2207 // Boot Option device path contains USB Class or USB WWID device path node.
2208 //
2209
2210 //
2211 // Prepare the parent device path for search.
2212 //
2213 TempDevicePath = DuplicateDevicePath (DevicePath);
2214 ASSERT (TempDevicePath != NULL);
2215 SetDevicePathEndNode (((UINT8 *) TempDevicePath) + ((UINTN) ShortFormDevicePath - (UINTN) DevicePath));
2216
2217 //
2218 // The USB Host Controller device path is already in Boot Option device path
2219 // and USB Bus driver already support RemainingDevicePath starts with USB
2220 // Class or USB WWID device path, so just search in existing USB devices and
2221 // doesn't perform ConnectController here.
2222 //
2223 ImageHandle = BdsFindUsbDevice (TempDevicePath, ShortFormDevicePath);
2224 FreePool (TempDevicePath);
2225 }
2226
2227 return ImageHandle;
2228 }
2229
2230 /**
2231 Process the boot option follow the UEFI specification and
2232 special treat the legacy boot option with BBS_DEVICE_PATH.
2233
2234 @param Option The boot option need to be processed
2235 @param DevicePath The device path which describe where to load the
2236 boot image or the legacy BBS device path to boot
2237 the legacy OS
2238 @param ExitDataSize The size of exit data.
2239 @param ExitData Data returned when Boot image failed.
2240
2241 @retval EFI_SUCCESS Boot from the input boot option successfully.
2242 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2243
2244 **/
2245 EFI_STATUS
2246 EFIAPI
2247 BdsLibBootViaBootOption (
2248 IN BDS_COMMON_OPTION *Option,
2249 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
2250 OUT UINTN *ExitDataSize,
2251 OUT CHAR16 **ExitData OPTIONAL
2252 )
2253 {
2254 EFI_STATUS Status;
2255 EFI_STATUS StatusLogo;
2256 EFI_HANDLE Handle;
2257 EFI_HANDLE ImageHandle;
2258 EFI_DEVICE_PATH_PROTOCOL *FilePath;
2259 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
2260 EFI_DEVICE_PATH_PROTOCOL *WorkingDevicePath;
2261 EFI_ACPI_S3_SAVE_PROTOCOL *AcpiS3Save;
2262 LIST_ENTRY TempBootLists;
2263 EFI_BOOT_LOGO_PROTOCOL *BootLogo;
2264
2265 *ExitDataSize = 0;
2266 *ExitData = NULL;
2267
2268 //
2269 // Notes: this code can be remove after the s3 script table
2270 // hook on the event EVT_SIGNAL_READY_TO_BOOT or
2271 // EVT_SIGNAL_LEGACY_BOOT
2272 //
2273 Status = gBS->LocateProtocol (&gEfiAcpiS3SaveProtocolGuid, NULL, (VOID **) &AcpiS3Save);
2274 if (!EFI_ERROR (Status)) {
2275 AcpiS3Save->S3Save (AcpiS3Save, NULL);
2276 }
2277 //
2278 // If it's Device Path that starts with a hard drive path, append it with the front part to compose a
2279 // full device path
2280 //
2281 WorkingDevicePath = NULL;
2282 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
2283 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)) {
2284 WorkingDevicePath = BdsExpandPartitionPartialDevicePathToFull (
2285 (HARDDRIVE_DEVICE_PATH *)DevicePath
2286 );
2287 if (WorkingDevicePath != NULL) {
2288 DevicePath = WorkingDevicePath;
2289 }
2290 }
2291
2292 //
2293 // Set Boot Current
2294 //
2295 if (IsBootOptionValidNVVarialbe (Option)) {
2296 //
2297 // For a temporary boot (i.e. a boot by selected a EFI Shell using "Boot From File"), Boot Current is actually not valid.
2298 // In this case, "BootCurrent" is not created.
2299 // Only create the BootCurrent variable when it points to a valid Boot#### variable.
2300 //
2301 SetVariableAndReportStatusCodeOnError (
2302 L"BootCurrent",
2303 &gEfiGlobalVariableGuid,
2304 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
2305 sizeof (UINT16),
2306 &Option->BootCurrent
2307 );
2308 }
2309
2310 //
2311 // Report Status Code to indicate ReadyToBoot event will be signalled
2312 //
2313 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_READY_TO_BOOT_EVENT));
2314
2315 //
2316 // Signal the EVT_SIGNAL_READY_TO_BOOT event
2317 //
2318 EfiSignalEventReadyToBoot();
2319
2320 //
2321 // Expand USB Class or USB WWID device path node to be full device path of a USB
2322 // device in platform then load the boot file on this full device path and get the
2323 // image handle.
2324 //
2325 ImageHandle = BdsExpandUsbShortFormDevicePath (DevicePath);
2326
2327 //
2328 // Adjust the different type memory page number just before booting
2329 // and save the updated info into the variable for next boot to use
2330 //
2331 BdsSetMemoryTypeInformationVariable ();
2332
2333 //
2334 // By expanding the USB Class or WWID device path, the ImageHandle has returnned.
2335 // Here get the ImageHandle for the non USB class or WWID device path.
2336 //
2337 if (ImageHandle == NULL) {
2338 ASSERT (Option->DevicePath != NULL);
2339 if ((DevicePathType (Option->DevicePath) == BBS_DEVICE_PATH) &&
2340 (DevicePathSubType (Option->DevicePath) == BBS_BBS_DP)
2341 ) {
2342 //
2343 // Check to see if we should legacy BOOT. If yes then do the legacy boot
2344 //
2345 return BdsLibDoLegacyBoot (Option);
2346 }
2347
2348 //
2349 // If the boot option point to Internal FV shell, make sure it is valid
2350 //
2351 Status = BdsLibUpdateFvFileDevicePath (&DevicePath, PcdGetPtr(PcdShellFile));
2352 if (!EFI_ERROR(Status)) {
2353 if (Option->DevicePath != NULL) {
2354 FreePool(Option->DevicePath);
2355 }
2356 Option->DevicePath = AllocateZeroPool (GetDevicePathSize (DevicePath));
2357 ASSERT(Option->DevicePath != NULL);
2358 CopyMem (Option->DevicePath, DevicePath, GetDevicePathSize (DevicePath));
2359 //
2360 // Update the shell boot option
2361 //
2362 InitializeListHead (&TempBootLists);
2363 BdsLibRegisterNewOption (&TempBootLists, DevicePath, L"EFI Internal Shell", L"BootOrder");
2364
2365 //
2366 // free the temporary device path created by BdsLibUpdateFvFileDevicePath()
2367 //
2368 FreePool (DevicePath);
2369 DevicePath = Option->DevicePath;
2370 }
2371
2372 DEBUG_CODE_BEGIN();
2373
2374 if (Option->Description == NULL) {
2375 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting from unknown device path\n"));
2376 } else {
2377 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Booting %S\n", Option->Description));
2378 }
2379
2380 DEBUG_CODE_END();
2381
2382 //
2383 // Report status code for OS Loader LoadImage.
2384 //
2385 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
2386 Status = gBS->LoadImage (
2387 TRUE,
2388 gImageHandle,
2389 DevicePath,
2390 NULL,
2391 0,
2392 &ImageHandle
2393 );
2394
2395 //
2396 // If we didn't find an image directly, we need to try as if it is a removable device boot option
2397 // and load the image according to the default boot behavior for removable device.
2398 //
2399 if (EFI_ERROR (Status)) {
2400 //
2401 // check if there is a bootable removable media could be found in this device path ,
2402 // and get the bootable media handle
2403 //
2404 Handle = BdsLibGetBootableHandle(DevicePath);
2405 if (Handle != NULL) {
2406 //
2407 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
2408 // machinename is ia32, ia64, x64, ...
2409 //
2410 FilePath = FileDevicePath (Handle, EFI_REMOVABLE_MEDIA_FILE_NAME);
2411 if (FilePath != NULL) {
2412 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderLoad));
2413 Status = gBS->LoadImage (
2414 TRUE,
2415 gImageHandle,
2416 FilePath,
2417 NULL,
2418 0,
2419 &ImageHandle
2420 );
2421 }
2422 }
2423 }
2424 }
2425 //
2426 // Provide the image with it's load options
2427 //
2428 if ((ImageHandle == NULL) || (EFI_ERROR(Status))) {
2429 //
2430 // Report Status Code to indicate that the failure to load boot option
2431 //
2432 REPORT_STATUS_CODE (
2433 EFI_ERROR_CODE | EFI_ERROR_MINOR,
2434 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR)
2435 );
2436 goto Done;
2437 }
2438
2439 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **) &ImageInfo);
2440 ASSERT_EFI_ERROR (Status);
2441
2442 if (Option->LoadOptionsSize != 0) {
2443 ImageInfo->LoadOptionsSize = Option->LoadOptionsSize;
2444 ImageInfo->LoadOptions = Option->LoadOptions;
2445 }
2446
2447 //
2448 // Clean to NULL because the image is loaded directly from the firmwares boot manager.
2449 //
2450 ImageInfo->ParentHandle = NULL;
2451
2452 //
2453 // Before calling the image, enable the Watchdog Timer for
2454 // the 5 Minute period
2455 //
2456 gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
2457
2458 //
2459 // Write boot to OS performance data for UEFI boot
2460 //
2461 PERF_CODE (
2462 BmEndOfBdsPerfCode (NULL, NULL);
2463 );
2464
2465 //
2466 // Report status code for OS Loader StartImage.
2467 //
2468 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdProgressCodeOsLoaderStart));
2469
2470 Status = gBS->StartImage (ImageHandle, ExitDataSize, ExitData);
2471 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Image Return Status = %r\n", Status));
2472 if (EFI_ERROR (Status)) {
2473 //
2474 // Report Status Code to indicate that boot failure
2475 //
2476 REPORT_STATUS_CODE (
2477 EFI_ERROR_CODE | EFI_ERROR_MINOR,
2478 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_EC_BOOT_OPTION_FAILED)
2479 );
2480 }
2481
2482 //
2483 // Clear the Watchdog Timer after the image returns
2484 //
2485 gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
2486
2487 Done:
2488 //
2489 // Set Logo status invalid after trying one boot option
2490 //
2491 BootLogo = NULL;
2492 StatusLogo = gBS->LocateProtocol (&gEfiBootLogoProtocolGuid, NULL, (VOID **) &BootLogo);
2493 if (!EFI_ERROR (StatusLogo) && (BootLogo != NULL)) {
2494 BootLogo->SetBootLogo (BootLogo, NULL, 0, 0, 0, 0);
2495 }
2496
2497 //
2498 // Clear Boot Current
2499 // Deleting variable with current implementation shouldn't fail.
2500 //
2501 gRT->SetVariable (
2502 L"BootCurrent",
2503 &gEfiGlobalVariableGuid,
2504 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
2505 0,
2506 NULL
2507 );
2508
2509 return Status;
2510 }
2511
2512
2513 /**
2514 Expand a device path that starts with a hard drive media device path node to be a
2515 full device path that includes the full hardware path to the device. We need
2516 to do this so it can be booted. As an optimization the front match (the part point
2517 to the partition node. E.g. ACPI() /PCI()/ATA()/Partition() ) is saved in a variable
2518 so a connect all is not required on every boot. All successful history device path
2519 which point to partition node (the front part) will be saved.
2520
2521 @param HardDriveDevicePath EFI Device Path to boot, if it starts with a hard
2522 drive media device path.
2523 @return A Pointer to the full device path or NULL if a valid Hard Drive devic path
2524 cannot be found.
2525
2526 **/
2527 EFI_DEVICE_PATH_PROTOCOL *
2528 EFIAPI
2529 BdsExpandPartitionPartialDevicePathToFull (
2530 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
2531 )
2532 {
2533 EFI_STATUS Status;
2534 UINTN BlockIoHandleCount;
2535 EFI_HANDLE *BlockIoBuffer;
2536 EFI_DEVICE_PATH_PROTOCOL *FullDevicePath;
2537 EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath;
2538 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2539 UINTN Index;
2540 UINTN InstanceNum;
2541 EFI_DEVICE_PATH_PROTOCOL *CachedDevicePath;
2542 EFI_DEVICE_PATH_PROTOCOL *TempNewDevicePath;
2543 UINTN CachedDevicePathSize;
2544 BOOLEAN DeviceExist;
2545 BOOLEAN NeedAdjust;
2546 EFI_DEVICE_PATH_PROTOCOL *Instance;
2547 UINTN Size;
2548
2549 FullDevicePath = NULL;
2550 //
2551 // Check if there is prestore HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2552 // If exist, search the front path which point to partition node in the variable instants.
2553 // If fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, reconnect all and search in all system
2554 //
2555 GetVariable2 (
2556 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
2557 &gHdBootDevicePathVariablGuid,
2558 (VOID **) &CachedDevicePath,
2559 &CachedDevicePathSize
2560 );
2561
2562 //
2563 // Delete the invalid HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable.
2564 //
2565 if ((CachedDevicePath != NULL) && !IsDevicePathValid (CachedDevicePath, CachedDevicePathSize)) {
2566 FreePool (CachedDevicePath);
2567 CachedDevicePath = NULL;
2568 Status = gRT->SetVariable (
2569 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
2570 &gHdBootDevicePathVariablGuid,
2571 0,
2572 0,
2573 NULL
2574 );
2575 ASSERT_EFI_ERROR (Status);
2576 }
2577
2578 if (CachedDevicePath != NULL) {
2579 TempNewDevicePath = CachedDevicePath;
2580 DeviceExist = FALSE;
2581 NeedAdjust = FALSE;
2582 do {
2583 //
2584 // Check every instance of the variable
2585 // First, check whether the instance contain the partition node, which is needed for distinguishing multi
2586 // partial partition boot option. Second, check whether the instance could be connected.
2587 //
2588 Instance = GetNextDevicePathInstance (&TempNewDevicePath, &Size);
2589 if (MatchPartitionDevicePathNode (Instance, HardDriveDevicePath)) {
2590 //
2591 // Connect the device path instance, the device path point to hard drive media device path node
2592 // e.g. ACPI() /PCI()/ATA()/Partition()
2593 //
2594 Status = BdsLibConnectDevicePath (Instance);
2595 if (!EFI_ERROR (Status)) {
2596 DeviceExist = TRUE;
2597 break;
2598 }
2599 }
2600 //
2601 // Come here means the first instance is not matched
2602 //
2603 NeedAdjust = TRUE;
2604 FreePool(Instance);
2605 } while (TempNewDevicePath != NULL);
2606
2607 if (DeviceExist) {
2608 //
2609 // Find the matched device path.
2610 // Append the file path information from the boot option and return the fully expanded device path.
2611 //
2612 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
2613 FullDevicePath = AppendDevicePath (Instance, DevicePath);
2614
2615 //
2616 // Adjust the HD_BOOT_DEVICE_PATH_VARIABLE_NAME instances sequence if the matched one is not first one.
2617 //
2618 if (NeedAdjust) {
2619 //
2620 // First delete the matched instance.
2621 //
2622 TempNewDevicePath = CachedDevicePath;
2623 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, Instance );
2624 FreePool (TempNewDevicePath);
2625
2626 //
2627 // Second, append the remaining path after the matched instance
2628 //
2629 TempNewDevicePath = CachedDevicePath;
2630 CachedDevicePath = AppendDevicePathInstance (Instance, CachedDevicePath );
2631 FreePool (TempNewDevicePath);
2632 //
2633 // Save the matching Device Path so we don't need to do a connect all next time
2634 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2635 //
2636 Status = gRT->SetVariable (
2637 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
2638 &gHdBootDevicePathVariablGuid,
2639 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
2640 GetDevicePathSize (CachedDevicePath),
2641 CachedDevicePath
2642 );
2643 }
2644
2645 FreePool (Instance);
2646 FreePool (CachedDevicePath);
2647 return FullDevicePath;
2648 }
2649 }
2650
2651 //
2652 // If we get here we fail to find or HD_BOOT_DEVICE_PATH_VARIABLE_NAME not exist, and now we need
2653 // to search all devices in the system for a matched partition
2654 //
2655 BdsLibConnectAllDriversToAllControllers ();
2656 Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &BlockIoHandleCount, &BlockIoBuffer);
2657 if (EFI_ERROR (Status) || BlockIoHandleCount == 0 || BlockIoBuffer == NULL) {
2658 //
2659 // If there was an error or there are no device handles that support
2660 // the BLOCK_IO Protocol, then return.
2661 //
2662 return NULL;
2663 }
2664 //
2665 // Loop through all the device handles that support the BLOCK_IO Protocol
2666 //
2667 for (Index = 0; Index < BlockIoHandleCount; Index++) {
2668
2669 Status = gBS->HandleProtocol (BlockIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &BlockIoDevicePath);
2670 if (EFI_ERROR (Status) || BlockIoDevicePath == NULL) {
2671 continue;
2672 }
2673
2674 if (MatchPartitionDevicePathNode (BlockIoDevicePath, HardDriveDevicePath)) {
2675 //
2676 // Find the matched partition device path
2677 //
2678 DevicePath = NextDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *) HardDriveDevicePath);
2679 FullDevicePath = AppendDevicePath (BlockIoDevicePath, DevicePath);
2680
2681 //
2682 // Save the matched partition device path in HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2683 //
2684 if (CachedDevicePath != NULL) {
2685 //
2686 // Save the matched partition device path as first instance of HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable
2687 //
2688 if (BdsLibMatchDevicePaths (CachedDevicePath, BlockIoDevicePath)) {
2689 TempNewDevicePath = CachedDevicePath;
2690 CachedDevicePath = BdsLibDelPartMatchInstance (CachedDevicePath, BlockIoDevicePath);
2691 FreePool(TempNewDevicePath);
2692 }
2693
2694 if (CachedDevicePath != NULL) {
2695 TempNewDevicePath = CachedDevicePath;
2696 CachedDevicePath = AppendDevicePathInstance (BlockIoDevicePath, CachedDevicePath);
2697 FreePool(TempNewDevicePath);
2698 } else {
2699 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);
2700 }
2701
2702 //
2703 // Here limit the device path instance number to 12, which is max number for a system support 3 IDE controller
2704 // If the user try to boot many OS in different HDs or partitions, in theory,
2705 // the HD_BOOT_DEVICE_PATH_VARIABLE_NAME variable maybe become larger and larger.
2706 //
2707 InstanceNum = 0;
2708 ASSERT (CachedDevicePath != NULL);
2709 TempNewDevicePath = CachedDevicePath;
2710 while (!IsDevicePathEnd (TempNewDevicePath)) {
2711 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
2712 //
2713 // Parse one instance
2714 //
2715 while (!IsDevicePathEndType (TempNewDevicePath)) {
2716 TempNewDevicePath = NextDevicePathNode (TempNewDevicePath);
2717 }
2718 InstanceNum++;
2719 //
2720 // If the CachedDevicePath variable contain too much instance, only remain 12 instances.
2721 //
2722 if (InstanceNum >= 12) {
2723 SetDevicePathEndNode (TempNewDevicePath);
2724 break;
2725 }
2726 }
2727 } else {
2728 CachedDevicePath = DuplicateDevicePath (BlockIoDevicePath);
2729 }
2730
2731 //
2732 // Save the matching Device Path so we don't need to do a connect all next time
2733 // Failure to set the variable only impacts the performance when next time expanding the short-form device path.
2734 //
2735 Status = gRT->SetVariable (
2736 HD_BOOT_DEVICE_PATH_VARIABLE_NAME,
2737 &gHdBootDevicePathVariablGuid,
2738 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
2739 GetDevicePathSize (CachedDevicePath),
2740 CachedDevicePath
2741 );
2742
2743 break;
2744 }
2745 }
2746
2747 if (CachedDevicePath != NULL) {
2748 FreePool (CachedDevicePath);
2749 }
2750 if (BlockIoBuffer != NULL) {
2751 FreePool (BlockIoBuffer);
2752 }
2753 return FullDevicePath;
2754 }
2755
2756 /**
2757 Check whether there is a instance in BlockIoDevicePath, which contain multi device path
2758 instances, has the same partition node with HardDriveDevicePath device path
2759
2760 @param BlockIoDevicePath Multi device path instances which need to check
2761 @param HardDriveDevicePath A device path which starts with a hard drive media
2762 device path.
2763
2764 @retval TRUE There is a matched device path instance.
2765 @retval FALSE There is no matched device path instance.
2766
2767 **/
2768 BOOLEAN
2769 EFIAPI
2770 MatchPartitionDevicePathNode (
2771 IN EFI_DEVICE_PATH_PROTOCOL *BlockIoDevicePath,
2772 IN HARDDRIVE_DEVICE_PATH *HardDriveDevicePath
2773 )
2774 {
2775 HARDDRIVE_DEVICE_PATH *TmpHdPath;
2776 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2777 BOOLEAN Match;
2778 EFI_DEVICE_PATH_PROTOCOL *BlockIoHdDevicePathNode;
2779
2780 if ((BlockIoDevicePath == NULL) || (HardDriveDevicePath == NULL)) {
2781 return FALSE;
2782 }
2783
2784 //
2785 // Make PreviousDevicePath == the device path node before the end node
2786 //
2787 DevicePath = BlockIoDevicePath;
2788 BlockIoHdDevicePathNode = NULL;
2789
2790 //
2791 // find the partition device path node
2792 //
2793 while (!IsDevicePathEnd (DevicePath)) {
2794 if ((DevicePathType (DevicePath) == MEDIA_DEVICE_PATH) &&
2795 (DevicePathSubType (DevicePath) == MEDIA_HARDDRIVE_DP)
2796 ) {
2797 BlockIoHdDevicePathNode = DevicePath;
2798 break;
2799 }
2800
2801 DevicePath = NextDevicePathNode (DevicePath);
2802 }
2803
2804 if (BlockIoHdDevicePathNode == NULL) {
2805 return FALSE;
2806 }
2807 //
2808 // See if the harddrive device path in blockio matches the orig Hard Drive Node
2809 //
2810 TmpHdPath = (HARDDRIVE_DEVICE_PATH *) BlockIoHdDevicePathNode;
2811 Match = FALSE;
2812
2813 //
2814 // Check for the match
2815 //
2816 if ((TmpHdPath->MBRType == HardDriveDevicePath->MBRType) &&
2817 (TmpHdPath->SignatureType == HardDriveDevicePath->SignatureType)) {
2818 switch (TmpHdPath->SignatureType) {
2819 case SIGNATURE_TYPE_GUID:
2820 Match = CompareGuid ((EFI_GUID *)TmpHdPath->Signature, (EFI_GUID *)HardDriveDevicePath->Signature);
2821 break;
2822 case SIGNATURE_TYPE_MBR:
2823 Match = (BOOLEAN)(*((UINT32 *)(&(TmpHdPath->Signature[0]))) == ReadUnaligned32((UINT32 *)(&(HardDriveDevicePath->Signature[0]))));
2824 break;
2825 default:
2826 Match = FALSE;
2827 break;
2828 }
2829 }
2830
2831 return Match;
2832 }
2833
2834 /**
2835 Delete the boot option associated with the handle passed in.
2836
2837 @param Handle The handle which present the device path to create
2838 boot option
2839
2840 @retval EFI_SUCCESS Delete the boot option success
2841 @retval EFI_NOT_FOUND If the Device Path is not found in the system
2842 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2843 @retval Other Error return value from SetVariable()
2844
2845 **/
2846 EFI_STATUS
2847 BdsLibDeleteOptionFromHandle (
2848 IN EFI_HANDLE Handle
2849 )
2850 {
2851 UINT16 *BootOrder;
2852 UINT8 *BootOptionVar;
2853 UINTN BootOrderSize;
2854 UINTN BootOptionSize;
2855 EFI_STATUS Status;
2856 UINTN Index;
2857 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
2858 UINTN DevicePathSize;
2859 UINTN OptionDevicePathSize;
2860 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2861 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
2862 UINT8 *TempPtr;
2863
2864 Status = EFI_SUCCESS;
2865 BootOrder = NULL;
2866 BootOrderSize = 0;
2867
2868 //
2869 // Check "BootOrder" variable, if no, means there is no any boot order.
2870 //
2871 BootOrder = BdsLibGetVariableAndSize (
2872 L"BootOrder",
2873 &gEfiGlobalVariableGuid,
2874 &BootOrderSize
2875 );
2876 if (BootOrder == NULL) {
2877 return EFI_NOT_FOUND;
2878 }
2879
2880 //
2881 // Convert device handle to device path protocol instance
2882 //
2883 DevicePath = DevicePathFromHandle (Handle);
2884 if (DevicePath == NULL) {
2885 return EFI_NOT_FOUND;
2886 }
2887 DevicePathSize = GetDevicePathSize (DevicePath);
2888
2889 //
2890 // Loop all boot order variable and find the matching device path
2891 //
2892 Index = 0;
2893 while (Index < BootOrderSize / sizeof (UINT16)) {
2894 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
2895 BootOptionVar = BdsLibGetVariableAndSize (
2896 BootOption,
2897 &gEfiGlobalVariableGuid,
2898 &BootOptionSize
2899 );
2900
2901 if (BootOptionVar == NULL) {
2902 FreePool (BootOrder);
2903 return EFI_OUT_OF_RESOURCES;
2904 }
2905
2906 if (!ValidateOption(BootOptionVar, BootOptionSize)) {
2907 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);
2908 FreePool (BootOptionVar);
2909 Index++;
2910 continue;
2911 }
2912
2913 TempPtr = BootOptionVar;
2914 TempPtr += sizeof (UINT32) + sizeof (UINT16);
2915 TempPtr += StrSize ((CHAR16 *) TempPtr);
2916 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
2917 OptionDevicePathSize = GetDevicePathSize (OptionDevicePath);
2918
2919 //
2920 // Check whether the device path match
2921 //
2922 if ((OptionDevicePathSize == DevicePathSize) &&
2923 (CompareMem (DevicePath, OptionDevicePath, DevicePathSize) == 0)) {
2924 BdsDeleteBootOption (BootOrder[Index], BootOrder, &BootOrderSize);
2925 FreePool (BootOptionVar);
2926 break;
2927 }
2928
2929 FreePool (BootOptionVar);
2930 Index++;
2931 }
2932
2933 //
2934 // Adjust number of boot option for "BootOrder" variable.
2935 //
2936 Status = gRT->SetVariable (
2937 L"BootOrder",
2938 &gEfiGlobalVariableGuid,
2939 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
2940 BootOrderSize,
2941 BootOrder
2942 );
2943 //
2944 // Shrinking variable with existing variable implementation shouldn't fail.
2945 //
2946 ASSERT_EFI_ERROR (Status);
2947
2948 FreePool (BootOrder);
2949
2950 return Status;
2951 }
2952
2953
2954 /**
2955 Delete all invalid EFI boot options.
2956
2957 @retval EFI_SUCCESS Delete all invalid boot option success
2958 @retval EFI_NOT_FOUND Variable "BootOrder" is not found
2959 @retval EFI_OUT_OF_RESOURCES Lack of memory resource
2960 @retval Other Error return value from SetVariable()
2961
2962 **/
2963 EFI_STATUS
2964 BdsDeleteAllInvalidEfiBootOption (
2965 VOID
2966 )
2967 {
2968 UINT16 *BootOrder;
2969 UINT8 *BootOptionVar;
2970 UINTN BootOrderSize;
2971 UINTN BootOptionSize;
2972 EFI_STATUS Status;
2973 UINTN Index;
2974 UINTN Index2;
2975 UINT16 BootOption[BOOT_OPTION_MAX_CHAR];
2976 EFI_DEVICE_PATH_PROTOCOL *OptionDevicePath;
2977 UINT8 *TempPtr;
2978 CHAR16 *Description;
2979 BOOLEAN Corrupted;
2980
2981 Status = EFI_SUCCESS;
2982 BootOrder = NULL;
2983 Description = NULL;
2984 OptionDevicePath = NULL;
2985 BootOrderSize = 0;
2986 Corrupted = FALSE;
2987
2988 //
2989 // Check "BootOrder" variable firstly, this variable hold the number of boot options
2990 //
2991 BootOrder = BdsLibGetVariableAndSize (
2992 L"BootOrder",
2993 &gEfiGlobalVariableGuid,
2994 &BootOrderSize
2995 );
2996 if (NULL == BootOrder) {
2997 return EFI_NOT_FOUND;
2998 }
2999
3000 Index = 0;
3001 while (Index < BootOrderSize / sizeof (UINT16)) {
3002 UnicodeSPrint (BootOption, sizeof (BootOption), L"Boot%04x", BootOrder[Index]);
3003 BootOptionVar = BdsLibGetVariableAndSize (
3004 BootOption,
3005 &gEfiGlobalVariableGuid,
3006 &BootOptionSize
3007 );
3008 if (NULL == BootOptionVar) {
3009 FreePool (BootOrder);
3010 return EFI_OUT_OF_RESOURCES;
3011 }
3012
3013 if (!ValidateOption(BootOptionVar, BootOptionSize)) {
3014 Corrupted = TRUE;
3015 } else {
3016 TempPtr = BootOptionVar;
3017 TempPtr += sizeof (UINT32) + sizeof (UINT16);
3018 Description = (CHAR16 *) TempPtr;
3019 TempPtr += StrSize ((CHAR16 *) TempPtr);
3020 OptionDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) TempPtr;
3021
3022 //
3023 // Skip legacy boot option (BBS boot device)
3024 //
3025 if ((DevicePathType (OptionDevicePath) == BBS_DEVICE_PATH) &&
3026 (DevicePathSubType (OptionDevicePath) == BBS_BBS_DP)) {
3027 FreePool (BootOptionVar);
3028 Index++;
3029 continue;
3030 }
3031 }
3032
3033 if (Corrupted || !BdsLibIsValidEFIBootOptDevicePathExt (OptionDevicePath, FALSE, Description)) {
3034 //
3035 // Delete this invalid boot option "Boot####"
3036 //
3037 Status = gRT->SetVariable (
3038 BootOption,
3039 &gEfiGlobalVariableGuid,
3040 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
3041 0,
3042 NULL
3043 );
3044 //
3045 // Deleting variable with current variable implementation shouldn't fail.
3046 //
3047 ASSERT_EFI_ERROR (Status);
3048 //
3049 // Mark this boot option in boot order as deleted
3050 //
3051 BootOrder[Index] = 0xffff;
3052 Corrupted = FALSE;
3053 }
3054
3055 FreePool (BootOptionVar);
3056 Index++;
3057 }
3058
3059 //
3060 // Adjust boot order array
3061 //
3062 Index2 = 0;
3063 for (Index = 0; Index < BootOrderSize / sizeof (UINT16); Index++) {
3064 if (BootOrder[Index] != 0xffff) {
3065 BootOrder[Index2] = BootOrder[Index];
3066 Index2 ++;
3067 }
3068 }
3069 Status = gRT->SetVariable (
3070 L"BootOrder",
3071 &gEfiGlobalVariableGuid,
3072 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
3073 Index2 * sizeof (UINT16),
3074 BootOrder
3075 );
3076 //
3077 // Shrinking variable with current variable implementation shouldn't fail.
3078 //
3079 ASSERT_EFI_ERROR (Status);
3080
3081 FreePool (BootOrder);
3082
3083 return Status;
3084 }
3085
3086
3087 /**
3088 For EFI boot option, BDS separate them as six types:
3089 1. Network - The boot option points to the SimpleNetworkProtocol device.
3090 Bds will try to automatically create this type boot option when enumerate.
3091 2. Shell - The boot option points to internal flash shell.
3092 Bds will try to automatically create this type boot option when enumerate.
3093 3. Removable BlockIo - The boot option only points to the removable media
3094 device, like USB flash disk, DVD, Floppy etc.
3095 These device should contain a *removable* blockIo
3096 protocol in their device handle.
3097 Bds will try to automatically create this type boot option
3098 when enumerate.
3099 4. Fixed BlockIo - The boot option only points to a Fixed blockIo device,
3100 like HardDisk.
3101 These device should contain a *fixed* blockIo
3102 protocol in their device handle.
3103 BDS will skip fixed blockIo devices, and NOT
3104 automatically create boot option for them. But BDS
3105 will help to delete those fixed blockIo boot option,
3106 whose description rule conflict with other auto-created
3107 boot options.
3108 5. Non-BlockIo Simplefile - The boot option points to a device whose handle
3109 has SimpleFileSystem Protocol, but has no blockio
3110 protocol. These devices do not offer blockIo
3111 protocol, but BDS still can get the
3112 \EFI\BOOT\boot{machinename}.EFI by SimpleFileSystem
3113 Protocol.
3114 6. File - The boot option points to a file. These boot options are usually
3115 created by user manually or OS loader. BDS will not delete or modify
3116 these boot options.
3117
3118 This function will enumerate all possible boot device in the system, and
3119 automatically create boot options for Network, Shell, Removable BlockIo,
3120 and Non-BlockIo Simplefile devices.
3121 It will only execute once of every boot.
3122
3123 @param BdsBootOptionList The header of the link list which indexed all
3124 current boot options
3125
3126 @retval EFI_SUCCESS Finished all the boot device enumerate and create
3127 the boot option base on that boot device
3128
3129 @retval EFI_OUT_OF_RESOURCES Failed to enumerate the boot device and create the boot option list
3130 **/
3131 EFI_STATUS
3132 EFIAPI
3133 BdsLibEnumerateAllBootOption (
3134 IN OUT LIST_ENTRY *BdsBootOptionList
3135 )
3136 {
3137 EFI_STATUS Status;
3138 UINT16 FloppyNumber;
3139 UINT16 HarddriveNumber;
3140 UINT16 CdromNumber;
3141 UINT16 UsbNumber;
3142 UINT16 MiscNumber;
3143 UINT16 ScsiNumber;
3144 UINT16 NonBlockNumber;
3145 UINTN NumberBlockIoHandles;
3146 EFI_HANDLE *BlockIoHandles;
3147 EFI_BLOCK_IO_PROTOCOL *BlkIo;
3148 BOOLEAN Removable[2];
3149 UINTN RemovableIndex;
3150 UINTN Index;
3151 UINTN NumOfLoadFileHandles;
3152 EFI_HANDLE *LoadFileHandles;
3153 UINTN FvHandleCount;
3154 EFI_HANDLE *FvHandleBuffer;
3155 EFI_FV_FILETYPE Type;
3156 UINTN Size;
3157 EFI_FV_FILE_ATTRIBUTES Attributes;
3158 UINT32 AuthenticationStatus;
3159 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
3160 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
3161 UINTN DevicePathType;
3162 CHAR16 Buffer[40];
3163 EFI_HANDLE *FileSystemHandles;
3164 UINTN NumberFileSystemHandles;
3165 BOOLEAN NeedDelete;
3166 EFI_IMAGE_DOS_HEADER DosHeader;
3167 CHAR8 *PlatLang;
3168 CHAR8 *LastLang;
3169 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
3170 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
3171 CHAR16 *MacStr;
3172 CHAR16 *IPverStr;
3173 EFI_HANDLE *NetworkHandles;
3174 UINTN BufferSize;
3175
3176 FloppyNumber = 0;
3177 HarddriveNumber = 0;
3178 CdromNumber = 0;
3179 UsbNumber = 0;
3180 MiscNumber = 0;
3181 ScsiNumber = 0;
3182 PlatLang = NULL;
3183 LastLang = NULL;
3184 ZeroMem (Buffer, sizeof (Buffer));
3185
3186 //
3187 // If the boot device enumerate happened, just get the boot
3188 // device from the boot order variable
3189 //
3190 if (mEnumBootDevice) {
3191 GetVariable2 (LAST_ENUM_LANGUAGE_VARIABLE_NAME, &gLastEnumLangGuid, (VOID**)&LastLang, NULL);
3192 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatLang, NULL);
3193 ASSERT (PlatLang != NULL);
3194 if ((LastLang != NULL) && (AsciiStrCmp (LastLang, PlatLang) == 0)) {
3195 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
3196 FreePool (LastLang);
3197 FreePool (PlatLang);
3198 return Status;
3199 } else {
3200 Status = gRT->SetVariable (
3201 LAST_ENUM_LANGUAGE_VARIABLE_NAME,
3202 &gLastEnumLangGuid,
3203 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
3204 AsciiStrSize (PlatLang),
3205 PlatLang
3206 );
3207 //
3208 // Failure to set the variable only impacts the performance next time enumerating the boot options.
3209 //
3210
3211 if (LastLang != NULL) {
3212 FreePool (LastLang);
3213 }
3214 FreePool (PlatLang);
3215 }
3216 }
3217
3218 //
3219 // Notes: this dirty code is to get the legacy boot option from the
3220 // BBS table and create to variable as the EFI boot option, it should
3221 // be removed after the CSM can provide legacy boot option directly
3222 //
3223 REFRESH_LEGACY_BOOT_OPTIONS;
3224
3225 //
3226 // Delete invalid boot option
3227 //
3228 BdsDeleteAllInvalidEfiBootOption ();
3229
3230 //
3231 // Parse removable media followed by fixed media.
3232 // The Removable[] array is used by the for-loop below to create removable media boot options
3233 // at first, and then to create fixed media boot options.
3234 //
3235 Removable[0] = FALSE;
3236 Removable[1] = TRUE;
3237
3238 gBS->LocateHandleBuffer (
3239 ByProtocol,
3240 &gEfiBlockIoProtocolGuid,
3241 NULL,
3242 &NumberBlockIoHandles,
3243 &BlockIoHandles
3244 );
3245
3246 for (RemovableIndex = 0; RemovableIndex < 2; RemovableIndex++) {
3247 for (Index = 0; Index < NumberBlockIoHandles; Index++) {
3248 Status = gBS->HandleProtocol (
3249 BlockIoHandles[Index],
3250 &gEfiBlockIoProtocolGuid,
3251 (VOID **) &BlkIo
3252 );
3253 //
3254 // skip the logical partition
3255 //
3256 if (EFI_ERROR (Status) || BlkIo->Media->LogicalPartition) {
3257 continue;
3258 }
3259
3260 //
3261 // firstly fixed block io then the removable block io
3262 //
3263 if (BlkIo->Media->RemovableMedia == Removable[RemovableIndex]) {
3264 continue;
3265 }
3266 DevicePath = DevicePathFromHandle (BlockIoHandles[Index]);
3267 DevicePathType = BdsGetBootTypeFromDevicePath (DevicePath);
3268
3269 switch (DevicePathType) {
3270 case BDS_EFI_ACPI_FLOPPY_BOOT:
3271 if (FloppyNumber != 0) {
3272 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)), FloppyNumber);
3273 } else {
3274 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_FLOPPY)));
3275 }
3276 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
3277 FloppyNumber++;
3278 break;
3279
3280 //
3281 // Assume a removable SATA device should be the DVD/CD device, a fixed SATA device should be the Hard Drive device.
3282 //
3283 case BDS_EFI_MESSAGE_ATAPI_BOOT:
3284 case BDS_EFI_MESSAGE_SATA_BOOT:
3285 if (BlkIo->Media->RemovableMedia) {
3286 if (CdromNumber != 0) {
3287 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)), CdromNumber);
3288 } else {
3289 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_CD_DVD)));
3290 }
3291 CdromNumber++;
3292 } else {
3293 if (HarddriveNumber != 0) {
3294 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)), HarddriveNumber);
3295 } else {
3296 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_HARDDRIVE)));
3297 }
3298 HarddriveNumber++;
3299 }
3300 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Buffer: %S\n", Buffer));
3301 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
3302 break;
3303
3304 case BDS_EFI_MESSAGE_USB_DEVICE_BOOT:
3305 if (UsbNumber != 0) {
3306 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)), UsbNumber);
3307 } else {
3308 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_USB)));
3309 }
3310 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
3311 UsbNumber++;
3312 break;
3313
3314 case BDS_EFI_MESSAGE_SCSI_BOOT:
3315 if (ScsiNumber != 0) {
3316 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)), ScsiNumber);
3317 } else {
3318 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_SCSI)));
3319 }
3320 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
3321 ScsiNumber++;
3322 break;
3323
3324 case BDS_EFI_MESSAGE_MISC_BOOT:
3325 default:
3326 if (MiscNumber != 0) {
3327 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)), MiscNumber);
3328 } else {
3329 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_MISC)));
3330 }
3331 BdsLibBuildOptionFromHandle (BlockIoHandles[Index], BdsBootOptionList, Buffer);
3332 MiscNumber++;
3333 break;
3334 }
3335 }
3336 }
3337
3338 if (NumberBlockIoHandles != 0) {
3339 FreePool (BlockIoHandles);
3340 }
3341
3342 //
3343 // If there is simple file protocol which does not consume block Io protocol, create a boot option for it here.
3344 //
3345 NonBlockNumber = 0;
3346 gBS->LocateHandleBuffer (
3347 ByProtocol,
3348 &gEfiSimpleFileSystemProtocolGuid,
3349 NULL,
3350 &NumberFileSystemHandles,
3351 &FileSystemHandles
3352 );
3353 for (Index = 0; Index < NumberFileSystemHandles; Index++) {
3354 Status = gBS->HandleProtocol (
3355 FileSystemHandles[Index],
3356 &gEfiBlockIoProtocolGuid,
3357 (VOID **) &BlkIo
3358 );
3359 if (!EFI_ERROR (Status)) {
3360 //
3361 // Skip if the file system handle supports a BlkIo protocol,
3362 //
3363 continue;
3364 }
3365
3366 //
3367 // Do the removable Media thing. \EFI\BOOT\boot{machinename}.EFI
3368 // machinename is ia32, ia64, x64, ...
3369 //
3370 Hdr.Union = &HdrData;
3371 NeedDelete = TRUE;
3372 Status = BdsLibGetImageHeader (
3373 FileSystemHandles[Index],
3374 EFI_REMOVABLE_MEDIA_FILE_NAME,
3375 &DosHeader,
3376 Hdr
3377 );
3378 if (!EFI_ERROR (Status) &&
3379 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
3380 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3381 NeedDelete = FALSE;
3382 }
3383
3384 if (NeedDelete) {
3385 //
3386 // No such file or the file is not a EFI application, delete this boot option
3387 //
3388 BdsLibDeleteOptionFromHandle (FileSystemHandles[Index]);
3389 } else {
3390 if (NonBlockNumber != 0) {
3391 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s %d", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)), NonBlockNumber);
3392 } else {
3393 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NON_BLOCK)));
3394 }
3395 BdsLibBuildOptionFromHandle (FileSystemHandles[Index], BdsBootOptionList, Buffer);
3396 NonBlockNumber++;
3397 }
3398 }
3399
3400 if (NumberFileSystemHandles != 0) {
3401 FreePool (FileSystemHandles);
3402 }
3403
3404 //
3405 // Parse Network Boot Device
3406 //
3407 NumOfLoadFileHandles = 0;
3408 //
3409 // Search Load File protocol for PXE boot option.
3410 //
3411 gBS->LocateHandleBuffer (
3412 ByProtocol,
3413 &gEfiLoadFileProtocolGuid,
3414 NULL,
3415 &NumOfLoadFileHandles,
3416 &LoadFileHandles
3417 );
3418
3419 for (Index = 0; Index < NumOfLoadFileHandles; Index++) {
3420
3421 //
3422 //Locate EFI_DEVICE_PATH_PROTOCOL to dynamically get IPv4/IPv6 protocol information.
3423 //
3424
3425 Status = gBS->HandleProtocol (
3426 LoadFileHandles[Index],
3427 &gEfiDevicePathProtocolGuid,
3428 (VOID **) &DevicePath
3429 );
3430
3431 ASSERT_EFI_ERROR (Status);
3432
3433 while (!IsDevicePathEnd (DevicePath)) {
3434 if ((DevicePath->Type == MESSAGING_DEVICE_PATH) &&
3435 (DevicePath->SubType == MSG_IPv4_DP)) {
3436
3437 //
3438 //Get handle infomation
3439 //
3440 BufferSize = 0;
3441 NetworkHandles = NULL;
3442 Status = gBS->LocateHandle (
3443 ByProtocol,
3444 &gEfiSimpleNetworkProtocolGuid,
3445 NULL,
3446 &BufferSize,
3447 NetworkHandles
3448 );
3449
3450 if (Status == EFI_BUFFER_TOO_SMALL) {
3451 NetworkHandles = AllocateZeroPool(BufferSize);
3452 if (NetworkHandles == NULL) {
3453 return (EFI_OUT_OF_RESOURCES);
3454 }
3455 Status = gBS->LocateHandle(
3456 ByProtocol,
3457 &gEfiSimpleNetworkProtocolGuid,
3458 NULL,
3459 &BufferSize,
3460 NetworkHandles
3461 );
3462 }
3463
3464 //
3465 //Get the MAC string
3466 //
3467 Status = NetLibGetMacString (
3468 *NetworkHandles,
3469 NULL,
3470 &MacStr
3471 );
3472 if (EFI_ERROR (Status)) {
3473 return Status;
3474 }
3475 IPverStr = L" IPv4";
3476 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s%s%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)),MacStr,IPverStr);
3477 break;
3478 }
3479 if((DevicePath->Type == MESSAGING_DEVICE_PATH) &&
3480 (DevicePath->SubType == MSG_IPv6_DP)) {
3481
3482 //
3483 //Get handle infomation
3484 //
3485 BufferSize = 0;
3486 NetworkHandles = NULL;
3487 Status = gBS->LocateHandle (
3488 ByProtocol,
3489 &gEfiSimpleNetworkProtocolGuid,
3490 NULL,
3491 &BufferSize,
3492 NetworkHandles
3493 );
3494
3495 if (Status == EFI_BUFFER_TOO_SMALL) {
3496 NetworkHandles = AllocateZeroPool(BufferSize);
3497 if (NetworkHandles == NULL) {
3498 return (EFI_OUT_OF_RESOURCES);
3499 }
3500 Status = gBS->LocateHandle(
3501 ByProtocol,
3502 &gEfiSimpleNetworkProtocolGuid,
3503 NULL,
3504 &BufferSize,
3505 NetworkHandles
3506 );
3507 }
3508
3509 //
3510 //Get the MAC string
3511 //
3512 Status = NetLibGetMacString (
3513 *NetworkHandles,
3514 NULL,
3515 &MacStr
3516 );
3517 if (EFI_ERROR (Status)) {
3518 return Status;
3519 }
3520 IPverStr = L" IPv6";
3521 UnicodeSPrint (Buffer, sizeof (Buffer), L"%s%s%s", BdsLibGetStringById (STRING_TOKEN (STR_DESCRIPTION_NETWORK)),MacStr,IPverStr);
3522 break;
3523 }
3524 DevicePath = NextDevicePathNode (DevicePath);
3525 }
3526
3527 BdsLibBuildOptionFromHandle (LoadFileHandles[Index], BdsBootOptionList, Buffer);
3528 }
3529
3530 if (NumOfLoadFileHandles != 0) {
3531 FreePool (LoadFileHandles);
3532 }
3533
3534 //
3535 // Check if we have on flash shell
3536 //
3537 gBS->LocateHandleBuffer (
3538 ByProtocol,
3539 &gEfiFirmwareVolume2ProtocolGuid,
3540 NULL,
3541 &FvHandleCount,
3542 &FvHandleBuffer
3543 );
3544 for (Index = 0; Index < FvHandleCount; Index++) {
3545 gBS->HandleProtocol (
3546 FvHandleBuffer[Index],
3547 &gEfiFirmwareVolume2ProtocolGuid,
3548 (VOID **) &Fv
3549 );
3550
3551 Status = Fv->ReadFile (
3552 Fv,
3553 PcdGetPtr(PcdShellFile),
3554 NULL,
3555 &Size,
3556 &Type,
3557 &Attributes,
3558 &AuthenticationStatus
3559 );
3560 if (EFI_ERROR (Status)) {
3561 //
3562 // Skip if no shell file in the FV
3563 //
3564 continue;
3565 }
3566 //
3567 // Build the shell boot option
3568 //
3569 BdsLibBuildOptionFromShell (FvHandleBuffer[Index], BdsBootOptionList);
3570 }
3571
3572 if (FvHandleCount != 0) {
3573 FreePool (FvHandleBuffer);
3574 }
3575 //
3576 // Make sure every boot only have one time
3577 // boot device enumerate
3578 //
3579 Status = BdsLibBuildOptionFromVar (BdsBootOptionList, L"BootOrder");
3580 mEnumBootDevice = TRUE;
3581
3582 return Status;
3583 }
3584
3585 /**
3586 Build the boot option with the handle parsed in
3587
3588 @param Handle The handle which present the device path to create
3589 boot option
3590 @param BdsBootOptionList The header of the link list which indexed all
3591 current boot options
3592 @param String The description of the boot option.
3593
3594 **/
3595 VOID
3596 EFIAPI
3597 BdsLibBuildOptionFromHandle (
3598 IN EFI_HANDLE Handle,
3599 IN LIST_ENTRY *BdsBootOptionList,
3600 IN CHAR16 *String
3601 )
3602 {
3603 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
3604
3605 DevicePath = DevicePathFromHandle (Handle);
3606
3607 //
3608 // Create and register new boot option
3609 //
3610 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, String, L"BootOrder");
3611 }
3612
3613
3614 /**
3615 Build the on flash shell boot option with the handle parsed in.
3616
3617 @param Handle The handle which present the device path to create
3618 on flash shell boot option
3619 @param BdsBootOptionList The header of the link list which indexed all
3620 current boot options
3621
3622 **/
3623 VOID
3624 EFIAPI
3625 BdsLibBuildOptionFromShell (
3626 IN EFI_HANDLE Handle,
3627 IN OUT LIST_ENTRY *BdsBootOptionList
3628 )
3629 {
3630 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
3631 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH ShellNode;
3632
3633 DevicePath = DevicePathFromHandle (Handle);
3634
3635 //
3636 // Build the shell device path
3637 //
3638 EfiInitializeFwVolDevicepathNode (&ShellNode, PcdGetPtr(PcdShellFile));
3639
3640 DevicePath = AppendDevicePathNode (DevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &ShellNode);
3641
3642 //
3643 // Create and register the shell boot option
3644 //
3645 BdsLibRegisterNewOption (BdsBootOptionList, DevicePath, L"EFI Internal Shell", L"BootOrder");
3646
3647 }
3648
3649 /**
3650 Boot from the UEFI spec defined "BootNext" variable.
3651
3652 **/
3653 VOID
3654 EFIAPI
3655 BdsLibBootNext (
3656 VOID
3657 )
3658 {
3659 EFI_STATUS Status;
3660 UINT16 *BootNext;
3661 UINTN BootNextSize;
3662 CHAR16 Buffer[20];
3663 BDS_COMMON_OPTION *BootOption;
3664 LIST_ENTRY TempList;
3665 UINTN ExitDataSize;
3666 CHAR16 *ExitData;
3667
3668 //
3669 // Init the boot option name buffer and temp link list
3670 //
3671 InitializeListHead (&TempList);
3672 ZeroMem (Buffer, sizeof (Buffer));
3673
3674 BootNext = BdsLibGetVariableAndSize (
3675 L"BootNext",
3676 &gEfiGlobalVariableGuid,
3677 &BootNextSize
3678 );
3679
3680 //
3681 // Clear the boot next variable first
3682 //
3683 if (BootNext != NULL) {
3684 Status = gRT->SetVariable (
3685 L"BootNext",
3686 &gEfiGlobalVariableGuid,
3687 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
3688 0,
3689 NULL
3690 );
3691 //
3692 // Deleting variable with current variable implementation shouldn't fail.
3693 //
3694 ASSERT_EFI_ERROR (Status);
3695
3696 //
3697 // Start to build the boot option and try to boot
3698 //
3699 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", *BootNext);
3700 BootOption = BdsLibVariableToOption (&TempList, Buffer);
3701 ASSERT (BootOption != NULL);
3702 BdsLibConnectDevicePath (BootOption->DevicePath);
3703 BdsLibBootViaBootOption (BootOption, BootOption->DevicePath, &ExitDataSize, &ExitData);
3704 FreePool(BootOption);
3705 FreePool(BootNext);
3706 }
3707
3708 }
3709
3710 /**
3711 Return the bootable media handle.
3712 First, check the device is connected
3713 Second, check whether the device path point to a device which support SimpleFileSystemProtocol,
3714 Third, detect the the default boot file in the Media, and return the removable Media handle.
3715
3716 @param DevicePath Device Path to a bootable device
3717
3718 @return The bootable media handle. If the media on the DevicePath is not bootable, NULL will return.
3719
3720 **/
3721 EFI_HANDLE
3722 EFIAPI
3723 BdsLibGetBootableHandle (
3724 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
3725 )
3726 {
3727 EFI_STATUS Status;
3728 EFI_TPL OldTpl;
3729 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
3730 EFI_DEVICE_PATH_PROTOCOL *DupDevicePath;
3731 EFI_HANDLE Handle;
3732 EFI_BLOCK_IO_PROTOCOL *BlockIo;
3733 VOID *Buffer;
3734 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
3735 UINTN Size;
3736 UINTN TempSize;
3737 EFI_HANDLE ReturnHandle;
3738 EFI_HANDLE *SimpleFileSystemHandles;
3739
3740 UINTN NumberSimpleFileSystemHandles;
3741 UINTN Index;
3742 EFI_IMAGE_DOS_HEADER DosHeader;
3743 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
3744 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
3745
3746 UpdatedDevicePath = DevicePath;
3747
3748 //
3749 // Enter to critical section to protect the acquired BlockIo instance
3750 // from getting released due to the USB mass storage hotplug event
3751 //
3752 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
3753
3754 //
3755 // Check whether the device is connected
3756 //
3757 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &UpdatedDevicePath, &Handle);
3758 if (EFI_ERROR (Status)) {
3759 //
3760 // Skip the case that the boot option point to a simple file protocol which does not consume block Io protocol,
3761 //
3762 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &UpdatedDevicePath, &Handle);
3763 if (EFI_ERROR (Status)) {
3764 //
3765 // Fail to find the proper BlockIo and simple file protocol, maybe because device not present, we need to connect it firstly
3766 //
3767 UpdatedDevicePath = DevicePath;
3768 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
3769 gBS->ConnectController (Handle, NULL, NULL, TRUE);
3770 }
3771 } else {
3772 //
3773 // For removable device boot option, its contained device path only point to the removable device handle,
3774 // should make sure all its children handles (its child partion or media handles) are created and connected.
3775 //
3776 gBS->ConnectController (Handle, NULL, NULL, TRUE);
3777 //
3778 // Get BlockIo protocol and check removable attribute
3779 //
3780 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
3781 ASSERT_EFI_ERROR (Status);
3782
3783 //
3784 // Issue a dummy read to the device to check for media change.
3785 // When the removable media is changed, any Block IO read/write will
3786 // cause the BlockIo protocol be reinstalled and EFI_MEDIA_CHANGED is
3787 // returned. After the Block IO protocol is reinstalled, subsequent
3788 // Block IO read/write will success.
3789 //
3790 Buffer = AllocatePool (BlockIo->Media->BlockSize);
3791 if (Buffer != NULL) {
3792 BlockIo->ReadBlocks (
3793 BlockIo,
3794 BlockIo->Media->MediaId,
3795 0,
3796 BlockIo->Media->BlockSize,
3797 Buffer
3798 );
3799 FreePool(Buffer);
3800 }
3801 }
3802
3803 //
3804 // Detect the the default boot file from removable Media
3805 //
3806
3807 //
3808 // 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
3809 // Try to locate the USB node device path first, if fail then use its previous PCI node to search
3810 //
3811 DupDevicePath = DuplicateDevicePath (DevicePath);
3812 ASSERT (DupDevicePath != NULL);
3813
3814 UpdatedDevicePath = DupDevicePath;
3815 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &UpdatedDevicePath, &Handle);
3816 //
3817 // 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
3818 // Acpi()/Pci()/Usb() --> Acpi()/Pci()
3819 //
3820 if ((DevicePathType (UpdatedDevicePath) == MESSAGING_DEVICE_PATH) &&
3821 (DevicePathSubType (UpdatedDevicePath) == MSG_USB_DP)) {
3822 //
3823 // Remove the usb node, let the device path only point to PCI node
3824 //
3825 SetDevicePathEndNode (UpdatedDevicePath);
3826 UpdatedDevicePath = DupDevicePath;
3827 } else {
3828 UpdatedDevicePath = DevicePath;
3829 }
3830
3831 //
3832 // Get the device path size of boot option
3833 //
3834 Size = GetDevicePathSize(UpdatedDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
3835 ReturnHandle = NULL;
3836 gBS->LocateHandleBuffer (
3837 ByProtocol,
3838 &gEfiSimpleFileSystemProtocolGuid,
3839 NULL,
3840 &NumberSimpleFileSystemHandles,
3841 &SimpleFileSystemHandles
3842 );
3843 for (Index = 0; Index < NumberSimpleFileSystemHandles; Index++) {
3844 //
3845 // Get the device path size of SimpleFileSystem handle
3846 //
3847 TempDevicePath = DevicePathFromHandle (SimpleFileSystemHandles[Index]);
3848 TempSize = GetDevicePathSize (TempDevicePath)- sizeof (EFI_DEVICE_PATH_PROTOCOL); // minus the end node
3849 //
3850 // Check whether the device path of boot option is part of the SimpleFileSystem handle's device path
3851 //
3852 if (Size <= TempSize && CompareMem (TempDevicePath, UpdatedDevicePath, Size)==0) {
3853 //
3854 // Load the default boot file \EFI\BOOT\boot{machinename}.EFI from removable Media
3855 // machinename is ia32, ia64, x64, ...
3856 //
3857 Hdr.Union = &HdrData;
3858 Status = BdsLibGetImageHeader (
3859 SimpleFileSystemHandles[Index],
3860 EFI_REMOVABLE_MEDIA_FILE_NAME,
3861 &DosHeader,
3862 Hdr
3863 );
3864 if (!EFI_ERROR (Status) &&
3865 EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Hdr.Pe32->FileHeader.Machine) &&
3866 Hdr.Pe32->OptionalHeader.Subsystem == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3867 ReturnHandle = SimpleFileSystemHandles[Index];
3868 break;
3869 }
3870 }
3871 }
3872
3873 FreePool(DupDevicePath);
3874
3875 if (SimpleFileSystemHandles != NULL) {
3876 FreePool(SimpleFileSystemHandles);
3877 }
3878
3879 gBS->RestoreTPL (OldTpl);
3880
3881 return ReturnHandle;
3882 }
3883
3884 /**
3885 Check to see if the network cable is plugged in. If the DevicePath is not
3886 connected it will be connected.
3887
3888 @param DevicePath Device Path to check
3889
3890 @retval TRUE DevicePath points to an Network that is connected
3891 @retval FALSE DevicePath does not point to a bootable network
3892
3893 **/
3894 BOOLEAN
3895 BdsLibNetworkBootWithMediaPresent (
3896 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
3897 )
3898 {
3899 EFI_STATUS Status;
3900 EFI_DEVICE_PATH_PROTOCOL *UpdatedDevicePath;
3901 EFI_HANDLE Handle;
3902 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
3903 BOOLEAN MediaPresent;
3904 UINT32 InterruptStatus;
3905
3906 MediaPresent = FALSE;
3907
3908 UpdatedDevicePath = DevicePath;
3909 //
3910 // Locate Load File Protocol for PXE boot option first
3911 //
3912 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
3913 if (EFI_ERROR (Status)) {
3914 //
3915 // Device not present so see if we need to connect it
3916 //
3917 Status = BdsLibConnectDevicePath (DevicePath);
3918 if (!EFI_ERROR (Status)) {
3919 //
3920 // This one should work after we did the connect
3921 //
3922 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &UpdatedDevicePath, &Handle);
3923 }
3924 }
3925
3926 if (!EFI_ERROR (Status)) {
3927 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **)&Snp);
3928 if (EFI_ERROR (Status)) {
3929 //
3930 // Failed to open SNP from this handle, try to get SNP from parent handle
3931 //
3932 UpdatedDevicePath = DevicePathFromHandle (Handle);
3933 if (UpdatedDevicePath != NULL) {
3934 Status = gBS->LocateDevicePath (&gEfiSimpleNetworkProtocolGuid, &UpdatedDevicePath, &Handle);
3935 if (!EFI_ERROR (Status)) {
3936 //
3937 // SNP handle found, get SNP from it
3938 //
3939 Status = gBS->HandleProtocol (Handle, &gEfiSimpleNetworkProtocolGuid, (VOID **) &Snp);
3940 }
3941 }
3942 }
3943
3944 if (!EFI_ERROR (Status)) {
3945 if (Snp->Mode->MediaPresentSupported) {
3946 if (Snp->Mode->State == EfiSimpleNetworkInitialized) {
3947 //
3948 // Invoke Snp->GetStatus() to refresh the media status
3949 //
3950 Snp->GetStatus (Snp, &InterruptStatus, NULL);
3951
3952 //
3953 // In case some one else is using the SNP check to see if it's connected
3954 //
3955 MediaPresent = Snp->Mode->MediaPresent;
3956 } else {
3957 //
3958 // No one is using SNP so we need to Start and Initialize so
3959 // MediaPresent will be valid.
3960 //
3961 Status = Snp->Start (Snp);
3962 if (!EFI_ERROR (Status)) {
3963 Status = Snp->Initialize (Snp, 0, 0);
3964 if (!EFI_ERROR (Status)) {
3965 MediaPresent = Snp->Mode->MediaPresent;
3966 Snp->Shutdown (Snp);
3967 }
3968 Snp->Stop (Snp);
3969 }
3970 }
3971 } else {
3972 MediaPresent = TRUE;
3973 }
3974 }
3975 }
3976
3977 return MediaPresent;
3978 }
3979
3980 /**
3981 For a bootable Device path, return its boot type.
3982
3983 @param DevicePath The bootable device Path to check
3984
3985 @retval BDS_EFI_MEDIA_HD_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3986 which subtype is MEDIA_HARDDRIVE_DP
3987 @retval BDS_EFI_MEDIA_CDROM_BOOT If given device path contains MEDIA_DEVICE_PATH type device path node
3988 which subtype is MEDIA_CDROM_DP
3989 @retval BDS_EFI_ACPI_FLOPPY_BOOT If given device path contains ACPI_DEVICE_PATH type device path node
3990 which HID is floppy device.
3991 @retval BDS_EFI_MESSAGE_ATAPI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3992 and its last device path node's subtype is MSG_ATAPI_DP.
3993 @retval BDS_EFI_MESSAGE_SCSI_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3994 and its last device path node's subtype is MSG_SCSI_DP.
3995 @retval BDS_EFI_MESSAGE_USB_DEVICE_BOOT If given device path contains MESSAGING_DEVICE_PATH type device path node
3996 and its last device path node's subtype is MSG_USB_DP.
3997 @retval BDS_EFI_MESSAGE_MISC_BOOT If the device path not contains any media device path node, and
3998 its last device path node point to a message device path node.
3999 @retval BDS_LEGACY_BBS_BOOT If given device path contains BBS_DEVICE_PATH type device path node.
4000 @retval BDS_EFI_UNSUPPORT An EFI Removable BlockIO device path not point to a media and message device,
4001
4002 **/
4003 UINT32
4004 EFIAPI
4005 BdsGetBootTypeFromDevicePath (
4006 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
4007 )
4008 {
4009 ACPI_HID_DEVICE_PATH *Acpi;
4010 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
4011 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
4012 UINT32 BootType;
4013
4014 if (NULL == DevicePath) {
4015 return BDS_EFI_UNSUPPORT;
4016 }
4017
4018 TempDevicePath = DevicePath;
4019
4020 while (!IsDevicePathEndType (TempDevicePath)) {
4021 switch (DevicePathType (TempDevicePath)) {
4022 case BBS_DEVICE_PATH:
4023 return BDS_LEGACY_BBS_BOOT;
4024 case MEDIA_DEVICE_PATH:
4025 if (DevicePathSubType (TempDevicePath) == MEDIA_HARDDRIVE_DP) {
4026 return BDS_EFI_MEDIA_HD_BOOT;
4027 } else if (DevicePathSubType (TempDevicePath) == MEDIA_CDROM_DP) {
4028 return BDS_EFI_MEDIA_CDROM_BOOT;
4029 }
4030 break;
4031 case ACPI_DEVICE_PATH:
4032 Acpi = (ACPI_HID_DEVICE_PATH *) TempDevicePath;
4033 if (EISA_ID_TO_NUM (Acpi->HID) == 0x0604) {
4034 return BDS_EFI_ACPI_FLOPPY_BOOT;
4035 }
4036 break;
4037 case MESSAGING_DEVICE_PATH:
4038 //
4039 // Get the last device path node
4040 //
4041 LastDeviceNode = NextDevicePathNode (TempDevicePath);
4042 if (DevicePathSubType(LastDeviceNode) == MSG_DEVICE_LOGICAL_UNIT_DP) {
4043 //
4044 // if the next node type is Device Logical Unit, which specify the Logical Unit Number (LUN),
4045 // skip it
4046 //
4047 LastDeviceNode = NextDevicePathNode (LastDeviceNode);
4048 }
4049 //
4050 // if the device path not only point to driver device, it is not a messaging device path,
4051 //
4052 if (!IsDevicePathEndType (LastDeviceNode)) {
4053 break;
4054 }
4055
4056 switch (DevicePathSubType (TempDevicePath)) {
4057 case MSG_ATAPI_DP:
4058 BootType = BDS_EFI_MESSAGE_ATAPI_BOOT;
4059 break;
4060
4061 case MSG_USB_DP:
4062 BootType = BDS_EFI_MESSAGE_USB_DEVICE_BOOT;
4063 break;
4064
4065 case MSG_SCSI_DP:
4066 BootType = BDS_EFI_MESSAGE_SCSI_BOOT;
4067 break;
4068
4069 case MSG_SATA_DP:
4070 BootType = BDS_EFI_MESSAGE_SATA_BOOT;
4071 break;
4072
4073 case MSG_MAC_ADDR_DP:
4074 case MSG_VLAN_DP:
4075 case MSG_IPv4_DP:
4076 case MSG_IPv6_DP:
4077 BootType = BDS_EFI_MESSAGE_MAC_BOOT;
4078 break;
4079
4080 default:
4081 BootType = BDS_EFI_MESSAGE_MISC_BOOT;
4082 break;
4083 }
4084 return BootType;
4085
4086 default:
4087 break;
4088 }
4089 TempDevicePath = NextDevicePathNode (TempDevicePath);
4090 }
4091
4092 return BDS_EFI_UNSUPPORT;
4093 }
4094
4095 /**
4096 Check whether the Device path in a boot option point to a valid bootable device,
4097 And if CheckMedia is true, check the device is ready to boot now.
4098
4099 @param DevPath the Device path in a boot option
4100 @param CheckMedia if true, check the device is ready to boot now.
4101
4102 @retval TRUE the Device path is valid
4103 @retval FALSE the Device path is invalid .
4104
4105 **/
4106 BOOLEAN
4107 EFIAPI
4108 BdsLibIsValidEFIBootOptDevicePath (
4109 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
4110 IN BOOLEAN CheckMedia
4111 )
4112 {
4113 return BdsLibIsValidEFIBootOptDevicePathExt (DevPath, CheckMedia, NULL);
4114 }
4115
4116 /**
4117 Check whether the Device path in a boot option point to a valid bootable device,
4118 And if CheckMedia is true, check the device is ready to boot now.
4119 If Description is not NULL and the device path point to a fixed BlockIo
4120 device, check the description whether conflict with other auto-created
4121 boot options.
4122
4123 @param DevPath the Device path in a boot option
4124 @param CheckMedia if true, check the device is ready to boot now.
4125 @param Description the description in a boot option
4126
4127 @retval TRUE the Device path is valid
4128 @retval FALSE the Device path is invalid .
4129
4130 **/
4131 BOOLEAN
4132 EFIAPI
4133 BdsLibIsValidEFIBootOptDevicePathExt (
4134 IN EFI_DEVICE_PATH_PROTOCOL *DevPath,
4135 IN BOOLEAN CheckMedia,
4136 IN CHAR16 *Description
4137 )
4138 {
4139 EFI_STATUS Status;
4140 EFI_HANDLE Handle;
4141 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
4142 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
4143 EFI_BLOCK_IO_PROTOCOL *BlockIo;
4144
4145 TempDevicePath = DevPath;
4146 LastDeviceNode = DevPath;
4147
4148 //
4149 // Check if it's a valid boot option for network boot device.
4150 // Check if there is EfiLoadFileProtocol installed.
4151 // If yes, that means there is a boot option for network.
4152 //
4153 Status = gBS->LocateDevicePath (
4154 &gEfiLoadFileProtocolGuid,
4155 &TempDevicePath,
4156 &Handle
4157 );
4158 if (EFI_ERROR (Status)) {
4159 //
4160 // Device not present so see if we need to connect it
4161 //
4162 TempDevicePath = DevPath;
4163 BdsLibConnectDevicePath (TempDevicePath);
4164 Status = gBS->LocateDevicePath (
4165 &gEfiLoadFileProtocolGuid,
4166 &TempDevicePath,
4167 &Handle
4168 );
4169 }
4170
4171 if (!EFI_ERROR (Status)) {
4172 if (!IsDevicePathEnd (TempDevicePath)) {
4173 //
4174 // LoadFile protocol is not installed on handle with exactly the same DevPath
4175 //
4176 return FALSE;
4177 }
4178
4179 if (CheckMedia) {
4180 //
4181 // Test if it is ready to boot now
4182 //
4183 if (BdsLibNetworkBootWithMediaPresent(DevPath)) {
4184 return TRUE;
4185 }
4186 } else {
4187 return TRUE;
4188 }
4189 }
4190
4191 //
4192 // If the boot option point to a file, it is a valid EFI boot option,
4193 // and assume it is ready to boot now
4194 //
4195 while (!IsDevicePathEnd (TempDevicePath)) {
4196 //
4197 // If there is USB Class or USB WWID device path node, treat it as valid EFI
4198 // Boot Option. BdsExpandUsbShortFormDevicePath () will be used to expand it
4199 // to full device path.
4200 //
4201 if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&
4202 ((DevicePathSubType (TempDevicePath) == MSG_USB_CLASS_DP) ||
4203 (DevicePathSubType (TempDevicePath) == MSG_USB_WWID_DP))) {
4204 return TRUE;
4205 }
4206
4207 LastDeviceNode = TempDevicePath;
4208 TempDevicePath = NextDevicePathNode (TempDevicePath);
4209 }
4210 if ((DevicePathType (LastDeviceNode) == MEDIA_DEVICE_PATH) &&
4211 (DevicePathSubType (LastDeviceNode) == MEDIA_FILEPATH_DP)) {
4212 return TRUE;
4213 }
4214
4215 //
4216 // Check if it's a valid boot option for internal FV application
4217 //
4218 if (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode) != NULL) {
4219 //
4220 // If the boot option point to internal FV application, make sure it is valid
4221 //
4222 TempDevicePath = DevPath;
4223 Status = BdsLibUpdateFvFileDevicePath (
4224 &TempDevicePath,
4225 EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode)
4226 );
4227 if (Status == EFI_ALREADY_STARTED) {
4228 return TRUE;
4229 } else {
4230 if (Status == EFI_SUCCESS) {
4231 FreePool (TempDevicePath);
4232 }
4233 return FALSE;
4234 }
4235 }
4236
4237 //
4238 // If the boot option point to a blockIO device:
4239 // if it is a removable blockIo device, it is valid.
4240 // if it is a fixed blockIo device, check its description confliction.
4241 //
4242 TempDevicePath = DevPath;
4243 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
4244 if (EFI_ERROR (Status)) {
4245 //
4246 // Device not present so see if we need to connect it
4247 //
4248 Status = BdsLibConnectDevicePath (DevPath);
4249 if (!EFI_ERROR (Status)) {
4250 //
4251 // Try again to get the Block Io protocol after we did the connect
4252 //
4253 TempDevicePath = DevPath;
4254 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &TempDevicePath, &Handle);
4255 }
4256 }
4257
4258 if (!EFI_ERROR (Status)) {
4259 Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
4260 if (!EFI_ERROR (Status)) {
4261 if (CheckMedia) {
4262 //
4263 // Test if it is ready to boot now
4264 //
4265 if (BdsLibGetBootableHandle (DevPath) != NULL) {
4266 return TRUE;
4267 }
4268 } else {
4269 return TRUE;
4270 }
4271 }
4272 } else {
4273 //
4274 // 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,
4275 //
4276 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &TempDevicePath, &Handle);
4277 if (!EFI_ERROR (Status)) {
4278 if (CheckMedia) {
4279 //
4280 // Test if it is ready to boot now
4281 //
4282 if (BdsLibGetBootableHandle (DevPath) != NULL) {
4283 return TRUE;
4284 }
4285 } else {
4286 return TRUE;
4287 }
4288 }
4289 }
4290
4291 return FALSE;
4292 }
4293
4294
4295 /**
4296 According to a file guild, check a Fv file device path is valid. If it is invalid,
4297 try to return the valid device path.
4298 FV address maybe changes for memory layout adjust from time to time, use this function
4299 could promise the Fv file device path is right.
4300
4301 @param DevicePath on input, the Fv file device path need to check on
4302 output, the updated valid Fv file device path
4303 @param FileGuid the Fv file guild
4304
4305 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
4306 parameter
4307 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
4308 guild at all
4309 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it is
4310 valid
4311 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
4312 and return the updated device path in DevicePath
4313
4314 **/
4315 EFI_STATUS
4316 EFIAPI
4317 BdsLibUpdateFvFileDevicePath (
4318 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
4319 IN EFI_GUID *FileGuid
4320 )
4321 {
4322 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
4323 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
4324 EFI_STATUS Status;
4325 EFI_GUID *GuidPoint;
4326 UINTN Index;
4327 UINTN FvHandleCount;
4328 EFI_HANDLE *FvHandleBuffer;
4329 EFI_FV_FILETYPE Type;
4330 UINTN Size;
4331 EFI_FV_FILE_ATTRIBUTES Attributes;
4332 UINT32 AuthenticationStatus;
4333 BOOLEAN FindFvFile;
4334 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
4335 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
4336 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;
4337 EFI_HANDLE FoundFvHandle;
4338 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
4339
4340 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
4341 return EFI_INVALID_PARAMETER;
4342 }
4343 if (FileGuid == NULL) {
4344 return EFI_INVALID_PARAMETER;
4345 }
4346
4347 //
4348 // Check whether the device path point to the default the input Fv file
4349 //
4350 TempDevicePath = *DevicePath;
4351 LastDeviceNode = TempDevicePath;
4352 while (!IsDevicePathEnd (TempDevicePath)) {
4353 LastDeviceNode = TempDevicePath;
4354 TempDevicePath = NextDevicePathNode (TempDevicePath);
4355 }
4356 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
4357 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
4358 );
4359 if (GuidPoint == NULL) {
4360 //
4361 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
4362 //
4363 return EFI_UNSUPPORTED;
4364 }
4365 if (!CompareGuid (GuidPoint, FileGuid)) {
4366 //
4367 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
4368 //
4369 return EFI_UNSUPPORTED;
4370 }
4371
4372 //
4373 // Check whether the input Fv file device path is valid
4374 //
4375 TempDevicePath = *DevicePath;
4376 FoundFvHandle = NULL;
4377 Status = gBS->LocateDevicePath (
4378 &gEfiFirmwareVolume2ProtocolGuid,
4379 &TempDevicePath,
4380 &FoundFvHandle
4381 );
4382 if (!EFI_ERROR (Status)) {
4383 Status = gBS->HandleProtocol (
4384 FoundFvHandle,
4385 &gEfiFirmwareVolume2ProtocolGuid,
4386 (VOID **) &Fv
4387 );
4388 if (!EFI_ERROR (Status)) {
4389 //
4390 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
4391 //
4392 Status = Fv->ReadFile (
4393 Fv,
4394 FileGuid,
4395 NULL,
4396 &Size,
4397 &Type,
4398 &Attributes,
4399 &AuthenticationStatus
4400 );
4401 if (!EFI_ERROR (Status)) {
4402 return EFI_ALREADY_STARTED;
4403 }
4404 }
4405 }
4406
4407 //
4408 // Look for the input wanted FV file in current FV
4409 // First, try to look for in Bds own FV. Bds and input wanted FV file usually are in the same FV
4410 //
4411 FindFvFile = FALSE;
4412 FoundFvHandle = NULL;
4413 Status = gBS->HandleProtocol (
4414 gImageHandle,
4415 &gEfiLoadedImageProtocolGuid,
4416 (VOID **) &LoadedImage
4417 );
4418 if (!EFI_ERROR (Status)) {
4419 Status = gBS->HandleProtocol (
4420 LoadedImage->DeviceHandle,
4421 &gEfiFirmwareVolume2ProtocolGuid,
4422 (VOID **) &Fv
4423 );
4424 if (!EFI_ERROR (Status)) {
4425 Status = Fv->ReadFile (
4426 Fv,
4427 FileGuid,
4428 NULL,
4429 &Size,
4430 &Type,
4431 &Attributes,
4432 &AuthenticationStatus
4433 );
4434 if (!EFI_ERROR (Status)) {
4435 FindFvFile = TRUE;
4436 FoundFvHandle = LoadedImage->DeviceHandle;
4437 }
4438 }
4439 }
4440 //
4441 // Second, if fail to find, try to enumerate all FV
4442 //
4443 if (!FindFvFile) {
4444 FvHandleBuffer = NULL;
4445 gBS->LocateHandleBuffer (
4446 ByProtocol,
4447 &gEfiFirmwareVolume2ProtocolGuid,
4448 NULL,
4449 &FvHandleCount,
4450 &FvHandleBuffer
4451 );
4452 for (Index = 0; Index < FvHandleCount; Index++) {
4453 gBS->HandleProtocol (
4454 FvHandleBuffer[Index],
4455 &gEfiFirmwareVolume2ProtocolGuid,
4456 (VOID **) &Fv
4457 );
4458
4459 Status = Fv->ReadFile (
4460 Fv,
4461 FileGuid,
4462 NULL,
4463 &Size,
4464 &Type,
4465 &Attributes,
4466 &AuthenticationStatus
4467 );
4468 if (EFI_ERROR (Status)) {
4469 //
4470 // Skip if input Fv file not in the FV
4471 //
4472 continue;
4473 }
4474 FindFvFile = TRUE;
4475 FoundFvHandle = FvHandleBuffer[Index];
4476 break;
4477 }
4478
4479 if (FvHandleBuffer != NULL) {
4480 FreePool (FvHandleBuffer);
4481 }
4482 }
4483
4484 if (FindFvFile) {
4485 //
4486 // Build the shell device path
4487 //
4488 NewDevicePath = DevicePathFromHandle (FoundFvHandle);
4489 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
4490 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
4491 ASSERT (NewDevicePath != NULL);
4492 *DevicePath = NewDevicePath;
4493 return EFI_SUCCESS;
4494 }
4495 return EFI_NOT_FOUND;
4496 }