1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
|
/** @file
Library functions which relate with boot option description.
Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "InternalBm.h"
#define VENDOR_IDENTIFICATION_OFFSET 3
#define VENDOR_IDENTIFICATION_LENGTH 8
#define PRODUCT_IDENTIFICATION_OFFSET 11
#define PRODUCT_IDENTIFICATION_LENGTH 16
CONST UINT16 mBmUsbLangId = 0x0409; // English
CHAR16 mBmUefiPrefix[] = L"UEFI ";
CHAR16 mBootDescGenericManufacturer[] = L"Generic";
CHAR16 mBootDescSd[] = L"SD Device";
CHAR16 mBootDescEmmc[] = L"eMMC Device";
CHAR16 mBootDescEmmcUserData[] = L"eMMC User Data";
CHAR16 mBootDescEmmcBoot1[] = L"eMMC Boot 1";
CHAR16 mBootDescEmmcBoot2[] = L"eMMC Boot 2";
CHAR16 mBootDescEmmcGp1[] = L"eMMC GP 1";
CHAR16 mBootDescEmmcGp2[] = L"eMMC GP 2";
CHAR16 mBootDescEmmcGp3[] = L"eMMC GP 3";
CHAR16 mBootDescEmmcGp4[] = L"eMMC GP 4";
typedef struct {
UINT8 Id;
CHAR16 *Name;
} BM_SDMMC_MANUFACTURER;
BM_SDMMC_MANUFACTURER mSdManufacturers[] = {
{ 0x01, L"Panasonic" },
{ 0x02, L"Toshiba/Kingston/Viking" },
{ 0x03, L"SanDisk" },
{ 0x08, L"Silicon Power" },
{ 0x18, L"Infineon" },
{ 0x1b, L"Transcend/Samsung" },
{ 0x1c, L"Transcend" },
{ 0x1d, L"Corsair/AData" },
{ 0x1e, L"Transcend" },
{ 0x1f, L"Kingston" },
{ 0x27, L"Delkin/Phison" },
{ 0x28, L"Lexar" },
{ 0x30, L"SanDisk" },
{ 0x31, L"Silicon Power" },
{ 0x33, L"STMicroelectronics" },
{ 0x41, L"Kingston" },
{ 0x6f, L"STMicroelectronics" },
{ 0x74, L"Transcend" },
{ 0x76, L"Patriot" },
{ 0x82, L"Gobe/Sony" },
{ 0x9c, L"Angelbird/Hoodman" },
};
BM_SDMMC_MANUFACTURER mMmcManufacturers[] = {
{ 0x00, L"SanDisk" },
{ 0x02, L"Kingston/SanDisk" },
{ 0x03, L"Toshiba" },
{ 0x11, L"Toshiba" },
{ 0x13, L"Micron" },
{ 0x15, L"Samsung" },
{ 0x37, L"KingMax" },
{ 0x44, L"ATP" },
{ 0x45, L"SanDisk" },
{ 0x2c, L"Kingston" },
{ 0x70, L"Kingston" },
{ 0x88, L"Foresee" },
{ 0x9b, L"YMTC" },
{ 0xd6, L"Foresee" },
{ 0xfe, L"Micron" },
};
LIST_ENTRY mPlatformBootDescriptionHandlers = INITIALIZE_LIST_HEAD_VARIABLE (mPlatformBootDescriptionHandlers);
/**
For a bootable Device path, return its boot type.
@param DevicePath The bootable device Path to check
@retval AcpiFloppyBoot If given device path contains ACPI_DEVICE_PATH type device path node
which HID is floppy device.
@retval MessageAtapiBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_ATAPI_DP.
@retval MessageSataBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_SATA_DP.
@retval MessageScsiBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_SCSI_DP.
@retval MessageUsbBoot If given device path contains MESSAGING_DEVICE_PATH type device path node
and its last device path node's subtype is MSG_USB_DP.
@retval BmMiscBoot If tiven device path doesn't match the above condition.
**/
BM_BOOT_TYPE
BmDevicePathType (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_DEVICE_PATH_PROTOCOL *Node;
EFI_DEVICE_PATH_PROTOCOL *NextNode;
ASSERT (DevicePath != NULL);
for (Node = DevicePath; !IsDevicePathEndType (Node); Node = NextDevicePathNode (Node)) {
switch (DevicePathType (Node)) {
case ACPI_DEVICE_PATH:
if (EISA_ID_TO_NUM (((ACPI_HID_DEVICE_PATH *)Node)->HID) == 0x0604) {
return BmAcpiFloppyBoot;
}
break;
case HARDWARE_DEVICE_PATH:
if (DevicePathSubType (Node) == HW_CONTROLLER_DP) {
return BmHardwareDeviceBoot;
}
break;
case MESSAGING_DEVICE_PATH:
//
// Skip LUN device node
//
NextNode = Node;
do {
NextNode = NextDevicePathNode (NextNode);
} while (
(DevicePathType (NextNode) == MESSAGING_DEVICE_PATH) &&
(DevicePathSubType (NextNode) == MSG_DEVICE_LOGICAL_UNIT_DP)
);
//
// If the device path not only point to driver device, it is not a messaging device path,
//
if (!IsDevicePathEndType (NextNode)) {
continue;
}
switch (DevicePathSubType (Node)) {
case MSG_ATAPI_DP:
return BmMessageAtapiBoot;
break;
case MSG_SATA_DP:
return BmMessageSataBoot;
break;
case MSG_USB_DP:
return BmMessageUsbBoot;
break;
case MSG_SCSI_DP:
return BmMessageScsiBoot;
break;
}
}
}
return BmMiscBoot;
}
/**
Eliminate the extra spaces in the Str to one space.
@param Str Input string info.
**/
VOID
BmEliminateExtraSpaces (
IN CHAR16 *Str
)
{
UINTN Index;
UINTN ActualIndex;
for (Index = 0, ActualIndex = 0; Str[Index] != L'\0'; Index++) {
if ((Str[Index] != L' ') || ((ActualIndex > 0) && (Str[ActualIndex - 1] != L' '))) {
Str[ActualIndex++] = Str[Index];
}
}
Str[ActualIndex] = L'\0';
}
/**
Swap a byte array.
@param Source Input byte array.
@param Length The size of Source in bytes.
**/
VOID
BmSwapBytes (
IN UINT8 *Source,
IN UINTN Length
)
{
UINTN Index;
UINT8 Temp;
UINTN Count;
Count = Length / 2;
for (Index = 0; Index < Count; ++Index) {
Temp = Source[Index];
Source[Index] = Source[Length - 1 - Index];
Source[Length - 1 - Index] = Temp;
}
}
/**
Get the SD/MMC manufacturer name from an ID.
@param Id Manufacturer ID.
@param IsMmc Boolean indicating whether the ID is for SD or eMMC.
@return The manufacturer string.
**/
CHAR16 *
BmGetSdMmcManufacturerName (
IN UINT8 Id,
IN BOOLEAN IsMmc
)
{
BM_SDMMC_MANUFACTURER *List;
UINT8 Count;
UINTN Index;
List = IsMmc ? mMmcManufacturers : mSdManufacturers;
Count = IsMmc ? ARRAY_SIZE (mMmcManufacturers)
: ARRAY_SIZE (mSdManufacturers);
for (Index = 0; Index < Count; ++Index) {
if (List[Index].Id == Id) {
return List[Index].Name;
}
}
return mBootDescGenericManufacturer;
}
/**
Get the eMMC partition type from a controller path.
@param DevicePath Pointer to a CONTROLLER_DEVICE_PATH.
@return The description string.
**/
CHAR16 *
BmGetEmmcTypeDescription (
CONTROLLER_DEVICE_PATH *DevicePath
)
{
switch (DevicePath->ControllerNumber) {
case EmmcPartitionUserData:
return mBootDescEmmcUserData;
case EmmcPartitionBoot1:
return mBootDescEmmcBoot1;
case EmmcPartitionBoot2:
return mBootDescEmmcBoot2;
case EmmcPartitionGP1:
return mBootDescEmmcGp1;
case EmmcPartitionGP2:
return mBootDescEmmcGp2;
case EmmcPartitionGP3:
return mBootDescEmmcGp3;
case EmmcPartitionGP4:
return mBootDescEmmcGp4;
default:
break;
}
return mBootDescEmmc;
}
/**
Get an SD/MMC boot description.
@param ManufacturerName Manufacturer name string.
@param ProductName Product name from CID.
@param ProductNameLength Length of ProductName.
@param SerialNumber Serial number from CID.
@param DeviceType Device type string (e.g. SD or an eMMC partition).
@return The description string.
**/
CHAR16 *
BmGetSdMmcDescription (
IN CHAR16 *ManufacturerName,
IN UINT8 *ProductName,
IN UINT8 ProductNameLength,
IN UINT8 SerialNumber[4],
IN CHAR16 *DeviceType
)
{
CHAR16 *Desc;
UINTN DescSize;
DescSize = StrSize (ManufacturerName) - sizeof (CHAR16) // "Samsung"
+ sizeof (CHAR16) // " "
+ ProductNameLength * sizeof (CHAR16) // "BJTD4R"
+ sizeof (CHAR16) // " "
+ sizeof (UINT32) * 2 * sizeof (CHAR16) // "00000000"
+ sizeof (CHAR16) // " "
+ StrSize (DeviceType); // "eMMC User Data\0"
Desc = AllocateZeroPool (DescSize);
if (Desc == NULL) {
return NULL;
}
BmSwapBytes (ProductName, ProductNameLength);
UnicodeSPrint (
Desc,
DescSize,
L"%s %.*a %02x%02x%02x%02x %s",
ManufacturerName,
ProductNameLength,
ProductName,
SerialNumber[0],
SerialNumber[1],
SerialNumber[2],
SerialNumber[3],
DeviceType
);
return Desc;
}
/**
Try to get the controller's ATA/ATAPI description.
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetDescriptionFromDiskInfo (
IN EFI_HANDLE Handle
)
{
UINTN Index;
EFI_STATUS Status;
EFI_DISK_INFO_PROTOCOL *DiskInfo;
UINT32 BufferSize;
EFI_ATAPI_IDENTIFY_DATA IdentifyData;
EFI_SCSI_INQUIRY_DATA InquiryData;
SD_CID SdCid;
EMMC_CID EmmcCid;
CHAR16 *Description;
UINTN Length;
CONST UINTN ModelNameLength = 40;
CONST UINTN SerialNumberLength = 20;
CHAR8 *StrPtr;
UINT8 Temp;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
Description = NULL;
Status = gBS->HandleProtocol (
Handle,
&gEfiDiskInfoProtocolGuid,
(VOID **)&DiskInfo
);
if (EFI_ERROR (Status)) {
return NULL;
}
if (CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoAhciInterfaceGuid) ||
CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoIdeInterfaceGuid))
{
BufferSize = sizeof (EFI_ATAPI_IDENTIFY_DATA);
Status = DiskInfo->Identify (
DiskInfo,
&IdentifyData,
&BufferSize
);
if (!EFI_ERROR (Status)) {
Description = AllocateZeroPool ((ModelNameLength + SerialNumberLength + 2) * sizeof (CHAR16));
ASSERT (Description != NULL);
for (Index = 0; Index + 1 < ModelNameLength; Index += 2) {
Description[Index] = (CHAR16)IdentifyData.ModelName[Index + 1];
Description[Index + 1] = (CHAR16)IdentifyData.ModelName[Index];
}
Length = Index;
Description[Length++] = L' ';
for (Index = 0; Index + 1 < SerialNumberLength; Index += 2) {
Description[Length + Index] = (CHAR16)IdentifyData.SerialNo[Index + 1];
Description[Length + Index + 1] = (CHAR16)IdentifyData.SerialNo[Index];
}
Length += Index;
Description[Length++] = L'\0';
ASSERT (Length == ModelNameLength + SerialNumberLength + 2);
BmEliminateExtraSpaces (Description);
}
} else if (CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoScsiInterfaceGuid) ||
CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoUfsInterfaceGuid))
{
BufferSize = sizeof (EFI_SCSI_INQUIRY_DATA);
Status = DiskInfo->Inquiry (
DiskInfo,
&InquiryData,
&BufferSize
);
if (!EFI_ERROR (Status)) {
Description = AllocateZeroPool ((VENDOR_IDENTIFICATION_LENGTH + PRODUCT_IDENTIFICATION_LENGTH + 2) * sizeof (CHAR16));
ASSERT (Description != NULL);
//
// Per SCSI spec, EFI_SCSI_INQUIRY_DATA.Reserved_5_95[3 - 10] save the Verdor identification
// EFI_SCSI_INQUIRY_DATA.Reserved_5_95[11 - 26] save the product identification,
// Here combine the vendor identification and product identification to the description.
//
StrPtr = (CHAR8 *)(&InquiryData.Reserved_5_95[VENDOR_IDENTIFICATION_OFFSET]);
Temp = StrPtr[VENDOR_IDENTIFICATION_LENGTH];
StrPtr[VENDOR_IDENTIFICATION_LENGTH] = '\0';
AsciiStrToUnicodeStrS (StrPtr, Description, VENDOR_IDENTIFICATION_LENGTH + 1);
StrPtr[VENDOR_IDENTIFICATION_LENGTH] = Temp;
//
// Add one space at the middle of vendor information and product information.
//
Description[VENDOR_IDENTIFICATION_LENGTH] = L' ';
StrPtr = (CHAR8 *)(&InquiryData.Reserved_5_95[PRODUCT_IDENTIFICATION_OFFSET]);
StrPtr[PRODUCT_IDENTIFICATION_LENGTH] = '\0';
AsciiStrToUnicodeStrS (StrPtr, Description + VENDOR_IDENTIFICATION_LENGTH + 1, PRODUCT_IDENTIFICATION_LENGTH + 1);
BmEliminateExtraSpaces (Description);
}
} else if (CompareGuid (&DiskInfo->Interface, &gEfiDiskInfoSdMmcInterfaceGuid)) {
DevicePath = DevicePathFromHandle (Handle);
if (DevicePath == NULL) {
return NULL;
}
while (!IsDevicePathEnd (DevicePath) && (DevicePathType (DevicePath) != MESSAGING_DEVICE_PATH)) {
DevicePath = NextDevicePathNode (DevicePath);
}
if (IsDevicePathEnd (DevicePath)) {
return NULL;
}
if (DevicePathSubType (DevicePath) == MSG_SD_DP) {
BufferSize = sizeof (SD_CID);
Status = DiskInfo->Inquiry (DiskInfo, &SdCid, &BufferSize);
if (EFI_ERROR (Status)) {
return NULL;
}
Description = BmGetSdMmcDescription (
BmGetSdMmcManufacturerName (SdCid.ManufacturerId, FALSE),
SdCid.ProductName,
ARRAY_SIZE (SdCid.ProductName),
SdCid.ProductSerialNumber,
mBootDescSd
);
} else if (DevicePathSubType (DevicePath) == MSG_EMMC_DP) {
BufferSize = sizeof (EMMC_CID);
Status = DiskInfo->Inquiry (DiskInfo, &EmmcCid, &BufferSize);
if (EFI_ERROR (Status)) {
return NULL;
}
Description = mBootDescEmmc;
DevicePath = NextDevicePathNode (DevicePath);
if (DevicePath->SubType == HW_CONTROLLER_DP) {
Description = BmGetEmmcTypeDescription ((CONTROLLER_DEVICE_PATH *)DevicePath);
}
Description = BmGetSdMmcDescription (
BmGetSdMmcManufacturerName (EmmcCid.ManufacturerId, TRUE),
EmmcCid.ProductName,
ARRAY_SIZE (EmmcCid.ProductName),
EmmcCid.ProductSerialNumber,
Description
);
} else {
return NULL;
}
Description = AllocateCopyPool (StrSize (Description), Description);
}
return Description;
}
/**
Try to get the controller's USB description.
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetUsbDescription (
IN EFI_HANDLE Handle
)
{
EFI_STATUS Status;
EFI_USB_IO_PROTOCOL *UsbIo;
CHAR16 NullChar;
CHAR16 *Manufacturer;
CHAR16 *Product;
CHAR16 *SerialNumber;
CHAR16 *Description;
EFI_USB_DEVICE_DESCRIPTOR DevDesc;
UINTN DescMaxSize;
Status = gBS->HandleProtocol (
Handle,
&gEfiUsbIoProtocolGuid,
(VOID **)&UsbIo
);
if (EFI_ERROR (Status)) {
return NULL;
}
NullChar = L'\0';
Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
if (EFI_ERROR (Status)) {
return NULL;
}
Status = UsbIo->UsbGetStringDescriptor (
UsbIo,
mBmUsbLangId,
DevDesc.StrManufacturer,
&Manufacturer
);
if (EFI_ERROR (Status)) {
Manufacturer = &NullChar;
}
Status = UsbIo->UsbGetStringDescriptor (
UsbIo,
mBmUsbLangId,
DevDesc.StrProduct,
&Product
);
if (EFI_ERROR (Status)) {
Product = &NullChar;
}
Status = UsbIo->UsbGetStringDescriptor (
UsbIo,
mBmUsbLangId,
DevDesc.StrSerialNumber,
&SerialNumber
);
if (EFI_ERROR (Status)) {
SerialNumber = &NullChar;
}
if ((Manufacturer == &NullChar) &&
(Product == &NullChar) &&
(SerialNumber == &NullChar)
)
{
return NULL;
}
DescMaxSize = StrSize (Manufacturer) + StrSize (Product) + StrSize (SerialNumber);
Description = AllocateZeroPool (DescMaxSize);
ASSERT (Description != NULL);
StrCatS (Description, DescMaxSize/sizeof (CHAR16), Manufacturer);
StrCatS (Description, DescMaxSize/sizeof (CHAR16), L" ");
StrCatS (Description, DescMaxSize/sizeof (CHAR16), Product);
StrCatS (Description, DescMaxSize/sizeof (CHAR16), L" ");
StrCatS (Description, DescMaxSize/sizeof (CHAR16), SerialNumber);
if (Manufacturer != &NullChar) {
FreePool (Manufacturer);
}
if (Product != &NullChar) {
FreePool (Product);
}
if (SerialNumber != &NullChar) {
FreePool (SerialNumber);
}
BmEliminateExtraSpaces (Description);
return Description;
}
/**
Return the description for network boot device.
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetNetworkDescription (
IN EFI_HANDLE Handle
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
MAC_ADDR_DEVICE_PATH *Mac;
VLAN_DEVICE_PATH *Vlan;
EFI_DEVICE_PATH_PROTOCOL *Ip;
EFI_DEVICE_PATH_PROTOCOL *Uri;
CHAR16 *Description;
UINTN DescriptionSize;
Status = gBS->OpenProtocol (
Handle,
&gEfiLoadFileProtocolGuid,
NULL,
gImageHandle,
Handle,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL
);
if (EFI_ERROR (Status)) {
return NULL;
}
Status = gBS->OpenProtocol (
Handle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevicePath,
gImageHandle,
Handle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status) || (DevicePath == NULL)) {
return NULL;
}
//
// The PXE device path is like:
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv4(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)
//
// The HTTP device path is like:
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv4(...)[/Dns(...)][/Uri(...)]/Uri(...)
// ....../Mac(...)[/Vlan(...)][/Wi-Fi(...)]/IPv6(...)[/Dns(...)][/Uri(...)]/Uri(...)
//
while (!IsDevicePathEnd (DevicePath) &&
((DevicePathType (DevicePath) != MESSAGING_DEVICE_PATH) ||
(DevicePathSubType (DevicePath) != MSG_MAC_ADDR_DP))
)
{
DevicePath = NextDevicePathNode (DevicePath);
}
if (IsDevicePathEnd (DevicePath)) {
return NULL;
}
Mac = (MAC_ADDR_DEVICE_PATH *)DevicePath;
DevicePath = NextDevicePathNode (DevicePath);
//
// Locate the optional Vlan node
//
if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&
(DevicePathSubType (DevicePath) == MSG_VLAN_DP)
)
{
Vlan = (VLAN_DEVICE_PATH *)DevicePath;
DevicePath = NextDevicePathNode (DevicePath);
} else {
Vlan = NULL;
}
//
// Skip the optional Wi-Fi node
//
if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&
(DevicePathSubType (DevicePath) == MSG_WIFI_DP)
)
{
DevicePath = NextDevicePathNode (DevicePath);
}
//
// Locate the IP node
//
if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&
((DevicePathSubType (DevicePath) == MSG_IPv4_DP) ||
(DevicePathSubType (DevicePath) == MSG_IPv6_DP))
)
{
Ip = DevicePath;
DevicePath = NextDevicePathNode (DevicePath);
} else {
Ip = NULL;
}
//
// Skip the optional DNS node
//
if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&
(DevicePathSubType (DevicePath) == MSG_DNS_DP)
)
{
DevicePath = NextDevicePathNode (DevicePath);
}
//
// Locate the URI node
//
if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&
(DevicePathSubType (DevicePath) == MSG_URI_DP)
)
{
Uri = DevicePath;
DevicePath = NextDevicePathNode (DevicePath);
} else {
Uri = NULL;
}
//
// Build description like below:
// "PXEv6 (MAC:112233445566 VLAN1)"
// "HTTPv4 (MAC:112233445566)"
//
DescriptionSize = sizeof (L"HTTPv6 (MAC:112233445566 VLAN65535)");
Description = AllocatePool (DescriptionSize);
ASSERT (Description != NULL);
UnicodeSPrint (
Description,
DescriptionSize,
(Vlan == NULL) ?
L"%sv%d (MAC:%02x%02x%02x%02x%02x%02x)" :
L"%sv%d (MAC:%02x%02x%02x%02x%02x%02x VLAN%d)",
(Uri == NULL) ? L"PXE" : L"HTTP",
((Ip == NULL) || (DevicePathSubType (Ip) == MSG_IPv4_DP)) ? 4 : 6,
Mac->MacAddress.Addr[0],
Mac->MacAddress.Addr[1],
Mac->MacAddress.Addr[2],
Mac->MacAddress.Addr[3],
Mac->MacAddress.Addr[4],
Mac->MacAddress.Addr[5],
(Vlan == NULL) ? 0 : Vlan->VlanId
);
return Description;
}
/**
Return the boot description for LoadFile
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetLoadFileDescription (
IN EFI_HANDLE Handle
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *FilePath;
EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
CHAR16 *Description;
EFI_LOAD_FILE_PROTOCOL *LoadFile;
Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID **)&LoadFile);
if (EFI_ERROR (Status)) {
return NULL;
}
//
// Get the file name
//
Description = NULL;
Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&FilePath);
if (!EFI_ERROR (Status)) {
DevicePathNode = FilePath;
while (!IsDevicePathEnd (DevicePathNode)) {
if ((DevicePathNode->Type == MEDIA_DEVICE_PATH) && (DevicePathNode->SubType == MEDIA_FILEPATH_DP)) {
Description = (CHAR16 *)(DevicePathNode + 1);
break;
}
DevicePathNode = NextDevicePathNode (DevicePathNode);
}
}
if (Description != NULL) {
return AllocateCopyPool (StrSize (Description), Description);
}
return NULL;
}
/**
Return the boot description for NVME boot device.
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetNvmeDescription (
IN EFI_HANDLE Handle
)
{
EFI_STATUS Status;
EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *NvmePassthru;
EFI_DEV_PATH_PTR DevicePath;
EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket;
EFI_NVM_EXPRESS_COMMAND Command;
EFI_NVM_EXPRESS_COMPLETION Completion;
NVME_ADMIN_CONTROLLER_DATA ControllerData;
CHAR16 *Description;
CHAR16 *Char;
UINTN Index;
Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **)&DevicePath.DevPath);
if (EFI_ERROR (Status)) {
return NULL;
}
Status = gBS->LocateDevicePath (&gEfiNvmExpressPassThruProtocolGuid, &DevicePath.DevPath, &Handle);
if (EFI_ERROR (Status) ||
(DevicePathType (DevicePath.DevPath) != MESSAGING_DEVICE_PATH) ||
(DevicePathSubType (DevicePath.DevPath) != MSG_NVME_NAMESPACE_DP))
{
//
// Do not return description when the Handle is not a child of NVME controller.
//
return NULL;
}
//
// Send ADMIN_IDENTIFY command to NVME controller to get the model and serial number.
//
Status = gBS->HandleProtocol (Handle, &gEfiNvmExpressPassThruProtocolGuid, (VOID **)&NvmePassthru);
ASSERT_EFI_ERROR (Status);
ZeroMem (&CommandPacket, sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));
ZeroMem (&Command, sizeof (EFI_NVM_EXPRESS_COMMAND));
ZeroMem (&Completion, sizeof (EFI_NVM_EXPRESS_COMPLETION));
Command.Cdw0.Opcode = NVME_ADMIN_IDENTIFY_CMD;
//
// According to Nvm Express 1.1 spec Figure 38, When not used, the field shall be cleared to 0h.
// For the Identify command, the Namespace Identifier is only used for the Namespace data structure.
//
Command.Nsid = 0;
CommandPacket.NvmeCmd = &Command;
CommandPacket.NvmeCompletion = &Completion;
CommandPacket.TransferBuffer = &ControllerData;
CommandPacket.TransferLength = sizeof (ControllerData);
CommandPacket.CommandTimeout = EFI_TIMER_PERIOD_SECONDS (5);
CommandPacket.QueueType = NVME_ADMIN_QUEUE;
//
// Set bit 0 (Cns bit) to 1 to identify a controller
//
Command.Cdw10 = 1;
Command.Flags = CDW10_VALID;
Status = NvmePassthru->PassThru (
NvmePassthru,
0,
&CommandPacket,
NULL
);
if (EFI_ERROR (Status)) {
return NULL;
}
Description = AllocateZeroPool (
(ARRAY_SIZE (ControllerData.Mn) + 1
+ ARRAY_SIZE (ControllerData.Sn) + 1
+ MAXIMUM_VALUE_CHARACTERS + 1
) * sizeof (CHAR16)
);
if (Description != NULL) {
Char = Description;
for (Index = 0; Index < ARRAY_SIZE (ControllerData.Mn); Index++) {
*(Char++) = (CHAR16)ControllerData.Mn[Index];
}
*(Char++) = L' ';
for (Index = 0; Index < ARRAY_SIZE (ControllerData.Sn); Index++) {
*(Char++) = (CHAR16)ControllerData.Sn[Index];
}
*(Char++) = L' ';
UnicodeValueToStringS (
Char,
sizeof (CHAR16) * (MAXIMUM_VALUE_CHARACTERS + 1),
0,
DevicePath.NvmeNamespace->NamespaceId,
0
);
BmEliminateExtraSpaces (Description);
}
return Description;
}
/**
Return the boot description for the controller based on the type.
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetMiscDescription (
IN EFI_HANDLE Handle
)
{
EFI_STATUS Status;
CHAR16 *Description;
EFI_BLOCK_IO_PROTOCOL *BlockIo;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Fs;
switch (BmDevicePathType (DevicePathFromHandle (Handle))) {
case BmAcpiFloppyBoot:
Description = L"Floppy";
break;
case BmMessageAtapiBoot:
case BmMessageSataBoot:
Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
ASSERT_EFI_ERROR (Status);
//
// Assume a removable SATA device should be the DVD/CD device
//
Description = BlockIo->Media->RemovableMedia ? L"DVD/CDROM" : L"Hard Drive";
break;
case BmMessageUsbBoot:
Description = L"USB Device";
break;
case BmMessageScsiBoot:
Description = L"SCSI Device";
break;
case BmHardwareDeviceBoot:
Status = gBS->HandleProtocol (Handle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
if (!EFI_ERROR (Status)) {
Description = BlockIo->Media->RemovableMedia ? L"Removable Disk" : L"Hard Drive";
} else {
Description = L"Misc Device";
}
break;
default:
Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&Fs);
if (!EFI_ERROR (Status)) {
Description = L"Non-Block Boot Device";
} else {
Description = L"Misc Device";
}
break;
}
return AllocateCopyPool (StrSize (Description), Description);
}
/**
Register the platform provided boot description handler.
@param Handler The platform provided boot description handler
@retval EFI_SUCCESS The handler was registered successfully.
@retval EFI_ALREADY_STARTED The handler was already registered.
@retval EFI_OUT_OF_RESOURCES There is not enough resource to perform the registration.
**/
EFI_STATUS
EFIAPI
EfiBootManagerRegisterBootDescriptionHandler (
IN EFI_BOOT_MANAGER_BOOT_DESCRIPTION_HANDLER Handler
)
{
LIST_ENTRY *Link;
BM_BOOT_DESCRIPTION_ENTRY *Entry;
for ( Link = GetFirstNode (&mPlatformBootDescriptionHandlers)
; !IsNull (&mPlatformBootDescriptionHandlers, Link)
; Link = GetNextNode (&mPlatformBootDescriptionHandlers, Link)
)
{
Entry = CR (Link, BM_BOOT_DESCRIPTION_ENTRY, Link, BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE);
if (Entry->Handler == Handler) {
return EFI_ALREADY_STARTED;
}
}
Entry = AllocatePool (sizeof (BM_BOOT_DESCRIPTION_ENTRY));
if (Entry == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Entry->Signature = BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE;
Entry->Handler = Handler;
InsertTailList (&mPlatformBootDescriptionHandlers, &Entry->Link);
return EFI_SUCCESS;
}
BM_GET_BOOT_DESCRIPTION mBmBootDescriptionHandlers[] = {
BmGetUsbDescription,
BmGetDescriptionFromDiskInfo,
BmGetNetworkDescription,
BmGetLoadFileDescription,
BmGetNvmeDescription,
BmGetMiscDescription
};
/**
Return the boot description for the controller.
@param Handle Controller handle.
@return The description string.
**/
CHAR16 *
BmGetBootDescription (
IN EFI_HANDLE Handle
)
{
LIST_ENTRY *Link;
BM_BOOT_DESCRIPTION_ENTRY *Entry;
CHAR16 *Description;
CHAR16 *DefaultDescription;
CHAR16 *Temp;
UINTN Index;
//
// Firstly get the default boot description
//
DefaultDescription = NULL;
for (Index = 0; Index < ARRAY_SIZE (mBmBootDescriptionHandlers); Index++) {
DefaultDescription = mBmBootDescriptionHandlers[Index](Handle);
if (DefaultDescription != NULL) {
//
// Avoid description confusion between UEFI & Legacy boot option by adding "UEFI " prefix
// ONLY for core provided boot description handler.
//
Temp = AllocatePool (StrSize (DefaultDescription) + sizeof (mBmUefiPrefix));
ASSERT (Temp != NULL);
StrCpyS (Temp, (StrSize (DefaultDescription) + sizeof (mBmUefiPrefix)) / sizeof (CHAR16), mBmUefiPrefix);
StrCatS (Temp, (StrSize (DefaultDescription) + sizeof (mBmUefiPrefix)) / sizeof (CHAR16), DefaultDescription);
FreePool (DefaultDescription);
DefaultDescription = Temp;
break;
}
}
ASSERT (DefaultDescription != NULL);
//
// Secondly query platform for the better boot description
//
for ( Link = GetFirstNode (&mPlatformBootDescriptionHandlers)
; !IsNull (&mPlatformBootDescriptionHandlers, Link)
; Link = GetNextNode (&mPlatformBootDescriptionHandlers, Link)
)
{
Entry = CR (Link, BM_BOOT_DESCRIPTION_ENTRY, Link, BM_BOOT_DESCRIPTION_ENTRY_SIGNATURE);
Description = Entry->Handler (Handle, DefaultDescription);
if (Description != NULL) {
FreePool (DefaultDescription);
return Description;
}
}
return DefaultDescription;
}
/**
Enumerate all boot option descriptions and append " 2"/" 3"/... to make
unique description.
@param BootOptions Array of boot options.
@param BootOptionCount Count of boot options.
**/
VOID
BmMakeBootOptionDescriptionUnique (
EFI_BOOT_MANAGER_LOAD_OPTION *BootOptions,
UINTN BootOptionCount
)
{
UINTN Base;
UINTN Index;
UINTN DescriptionSize;
UINTN MaxSuffixSize;
BOOLEAN *Visited;
UINTN MatchCount;
if (BootOptionCount == 0) {
return;
}
//
// Calculate the maximum buffer size for the number suffix.
// The initial sizeof (CHAR16) is for the blank space before the number.
//
MaxSuffixSize = sizeof (CHAR16);
for (Index = BootOptionCount; Index != 0; Index = Index / 10) {
MaxSuffixSize += sizeof (CHAR16);
}
Visited = AllocateZeroPool (sizeof (BOOLEAN) * BootOptionCount);
ASSERT (Visited != NULL);
for (Base = 0; Base < BootOptionCount; Base++) {
if (!Visited[Base]) {
MatchCount = 1;
Visited[Base] = TRUE;
DescriptionSize = StrSize (BootOptions[Base].Description);
for (Index = Base + 1; Index < BootOptionCount; Index++) {
if (!Visited[Index] && (StrCmp (BootOptions[Base].Description, BootOptions[Index].Description) == 0)) {
Visited[Index] = TRUE;
MatchCount++;
FreePool (BootOptions[Index].Description);
BootOptions[Index].Description = AllocatePool (DescriptionSize + MaxSuffixSize);
UnicodeSPrint (
BootOptions[Index].Description,
DescriptionSize + MaxSuffixSize,
L"%s %d",
BootOptions[Base].Description,
MatchCount
);
}
}
}
}
FreePool (Visited);
}
|