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 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
|
/** @file
Console Platform DXE Driver, install Console Device Guids and update Console
Environment Variables.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "ConPlatform.h"
EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {
ConPlatformTextInDriverBindingSupported,
ConPlatformTextInDriverBindingStart,
ConPlatformTextInDriverBindingStop,
0xa,
NULL,
NULL
};
EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {
ConPlatformTextOutDriverBindingSupported,
ConPlatformTextOutDriverBindingStart,
ConPlatformTextOutDriverBindingStop,
0xa,
NULL,
NULL
};
//
// Values from Usb Inteface Association Descriptor Device
// Class Code and Usage Model specification (iadclasscode_r10.pdf)
// from Usb.org
//
#define USB_BASE_CLASS_MISCELLANEOUS 0xEF
#define USB_MISCELLANEOUS_SUBCLASS_COMMON 0x02
#define USB_MISCELLANEOUS_PROTOCOL_IAD 0x01
/**
Entrypoint of this module.
This function is the entrypoint of this module. It installs Driver Binding
Protocols together with Component Name Protocols.
@param ImageHandle The firmware allocated handle for the EFI image.
@param SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
**/
EFI_STATUS
EFIAPI
InitializeConPlatform (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
Status = EfiLibInstallDriverBindingComponentName2 (
ImageHandle,
SystemTable,
&gConPlatformTextInDriverBinding,
ImageHandle,
&gConPlatformComponentName,
&gConPlatformComponentName2
);
ASSERT_EFI_ERROR (Status);
Status = EfiLibInstallDriverBindingComponentName2 (
ImageHandle,
SystemTable,
&gConPlatformTextOutDriverBinding,
NULL,
&gConPlatformComponentName,
&gConPlatformComponentName2
);
ASSERT_EFI_ERROR (Status);
return EFI_SUCCESS;
}
/**
Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to test.
@param RemainingDevicePath Optional parameter use to pick a specific child
device to start.
@retval EFI_SUCCESS This driver supports this device.
@retval other This driver does not support this device.
**/
EFI_STATUS
EFIAPI
ConPlatformTextInDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
)
{
return ConPlatformDriverBindingSupported (
This,
ControllerHandle,
&gEfiSimpleTextInProtocolGuid
);
}
/**
Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to test.
@param RemainingDevicePath Optional parameter use to pick a specific child
device to start.
@retval EFI_SUCCESS This driver supports this device.
@retval other This driver does not support this device.
**/
EFI_STATUS
EFIAPI
ConPlatformTextOutDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
)
{
return ConPlatformDriverBindingSupported (
This,
ControllerHandle,
&gEfiSimpleTextOutProtocolGuid
);
}
/**
Test to see if the specified protocol is supported on ControllerHandle.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to test.
@param ProtocolGuid The specfic protocol.
@retval EFI_SUCCESS This driver supports this device.
@retval other This driver does not support this device.
**/
EFI_STATUS
ConPlatformDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN EFI_GUID *ProtocolGuid
)
{
EFI_STATUS Status;
VOID *Interface;
//
// Test to see if this is a physical device by checking if
// it has a Device Path Protocol.
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
NULL,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Test to see if this device supports the specified Protocol.
//
Status = gBS->OpenProtocol (
ControllerHandle,
ProtocolGuid,
(VOID **)&Interface,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
gBS->CloseProtocol (
ControllerHandle,
ProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
return EFI_SUCCESS;
}
/**
Start this driver on the device for console input.
Start this driver on ControllerHandle by opening Simple Text Input Protocol,
reading Device Path, and installing Console In Devcice GUID on ControllerHandle.
Append its device path into the console environment variables ConInDev.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to bind driver to
@param RemainingDevicePath Optional parameter use to pick a specific child
device to start.
@retval EFI_SUCCESS This driver is added to ControllerHandle
@retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
@retval other This driver does not support this device.
**/
EFI_STATUS
EFIAPI
ConPlatformTextInDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;
BOOLEAN IsInConInVariable;
//
// Get the Device Path Protocol so the environment variables can be updated
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevicePath,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Open the Simple Text Input Protocol BY_DRIVER
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiSimpleTextInProtocolGuid,
(VOID **)&TextIn,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Check if the device path is in ConIn Variable
//
IsInConInVariable = FALSE;
Status = ConPlatformUpdateDeviceVariable (
L"ConIn",
DevicePath,
Check
);
if (!EFI_ERROR (Status)) {
IsInConInVariable = TRUE;
}
//
// Append the device path to the ConInDev environment variable
//
ConPlatformUpdateDeviceVariable (
L"ConInDev",
DevicePath,
Append
);
//
// If the device path is an instance in the ConIn environment variable,
// then install EfiConsoleInDeviceGuid onto ControllerHandle
//
if (IsInConInVariable) {
gBS->InstallMultipleProtocolInterfaces (
&ControllerHandle,
&gEfiConsoleInDeviceGuid,
NULL,
NULL
);
} else {
gBS->CloseProtocol (
ControllerHandle,
&gEfiSimpleTextInProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
}
return EFI_SUCCESS;
}
/**
Start this driver on the device for console output and standard error output.
Start this driver on ControllerHandle by opening Simple Text Output Protocol,
reading Device Path, and installing Console Out Devcic GUID, Standard Error
Device GUID on ControllerHandle.
Append its device path into the console environment variables ConOutDev, ErrOutDev.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to bind driver to
@param RemainingDevicePath Optional parameter use to pick a specific child
device to start.
@retval EFI_SUCCESS This driver is added to ControllerHandle
@retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
@retval other This driver does not support this device
**/
EFI_STATUS
EFIAPI
ConPlatformTextOutDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
BOOLEAN NeedClose;
BOOLEAN IsInConOutVariable;
BOOLEAN IsInErrOutVariable;
NeedClose = TRUE;
//
// Get the Device Path Protocol so the environment variables can be updated
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevicePath,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Open the Simple Text Output Protocol BY_DRIVER
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiSimpleTextOutProtocolGuid,
(VOID **)&TextOut,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Check if the device path is in ConOut & ErrOut Variable
//
IsInConOutVariable = FALSE;
Status = ConPlatformUpdateDeviceVariable (
L"ConOut",
DevicePath,
Check
);
if (!EFI_ERROR (Status)) {
IsInConOutVariable = TRUE;
}
IsInErrOutVariable = FALSE;
Status = ConPlatformUpdateDeviceVariable (
L"ErrOut",
DevicePath,
Check
);
if (!EFI_ERROR (Status)) {
IsInErrOutVariable = TRUE;
}
//
// Append the device path to the ConOutDev and ErrOutDev environment variable.
// For GOP device path, append the sibling device path as well.
//
if (!ConPlatformUpdateGopCandidate (DevicePath)) {
ConPlatformUpdateDeviceVariable (
L"ConOutDev",
DevicePath,
Append
);
//
// Then append the device path to the ErrOutDev environment variable
//
ConPlatformUpdateDeviceVariable (
L"ErrOutDev",
DevicePath,
Append
);
}
//
// If the device path is an instance in the ConOut environment variable,
// then install EfiConsoleOutDeviceGuid onto ControllerHandle
//
if (IsInConOutVariable) {
NeedClose = FALSE;
Status = gBS->InstallMultipleProtocolInterfaces (
&ControllerHandle,
&gEfiConsoleOutDeviceGuid,
NULL,
NULL
);
}
//
// If the device path is an instance in the ErrOut environment variable,
// then install EfiStandardErrorDeviceGuid onto ControllerHandle
//
if (IsInErrOutVariable) {
NeedClose = FALSE;
gBS->InstallMultipleProtocolInterfaces (
&ControllerHandle,
&gEfiStandardErrorDeviceGuid,
NULL,
NULL
);
}
if (NeedClose) {
gBS->CloseProtocol (
ControllerHandle,
&gEfiSimpleTextOutProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
}
return EFI_SUCCESS;
}
/**
Stop this driver on ControllerHandle by removing Console In Devcice GUID
and closing the Simple Text Input protocol on ControllerHandle.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to stop driver on
@param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
children is zero stop the entire bus driver.
@param ChildHandleBuffer List of Child Handles to Stop.
@retval EFI_SUCCESS This driver is removed ControllerHandle
@retval other This driver was not removed from this device
**/
EFI_STATUS
EFIAPI
ConPlatformTextInDriverBindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
//
// Get the Device Path Protocol firstly
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevicePath,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
//
// If there is device path on ControllerHandle
//
if (!EFI_ERROR (Status)) {
//
// Remove DevicePath from ConInDev if exists.
//
ConPlatformUpdateDeviceVariable (
L"ConInDev",
DevicePath,
Delete
);
}
//
// Uninstall the Console Device GUIDs from Controller Handle
//
ConPlatformUnInstallProtocol (
This,
ControllerHandle,
&gEfiConsoleInDeviceGuid
);
//
// Close the Simple Text Input Protocol
//
gBS->CloseProtocol (
ControllerHandle,
&gEfiSimpleTextInProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
return EFI_SUCCESS;
}
/**
Stop this driver on ControllerHandle by removing Console Out Devcice GUID
and closing the Simple Text Output protocol on ControllerHandle.
@param This Protocol instance pointer.
@param ControllerHandle Handle of device to stop driver on
@param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
children is zero stop the entire bus driver.
@param ChildHandleBuffer List of Child Handles to Stop.
@retval EFI_SUCCESS This driver is removed ControllerHandle
@retval other This driver was not removed from this device
**/
EFI_STATUS
EFIAPI
ConPlatformTextOutDriverBindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
//
// Get the Device Path Protocol firstly
//
Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
(VOID **)&DevicePath,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
//
// Remove DevicePath from ConOutDev and ErrOutDev if exists.
//
ConPlatformUpdateDeviceVariable (
L"ConOutDev",
DevicePath,
Delete
);
ConPlatformUpdateDeviceVariable (
L"ErrOutDev",
DevicePath,
Delete
);
}
//
// Uninstall the Console Device GUIDs from Controller Handle
//
ConPlatformUnInstallProtocol (
This,
ControllerHandle,
&gEfiConsoleOutDeviceGuid
);
ConPlatformUnInstallProtocol (
This,
ControllerHandle,
&gEfiStandardErrorDeviceGuid
);
//
// Close the Simple Text Output Protocol
//
gBS->CloseProtocol (
ControllerHandle,
&gEfiSimpleTextOutProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
return EFI_SUCCESS;
}
/**
Uninstall the specified protocol.
@param This Protocol instance pointer.
@param Handle Handle of device to uninstall protocol on.
@param ProtocolGuid The specified protocol need to be uninstalled.
**/
VOID
ConPlatformUnInstallProtocol (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Handle,
IN EFI_GUID *ProtocolGuid
)
{
EFI_STATUS Status;
Status = gBS->OpenProtocol (
Handle,
ProtocolGuid,
NULL,
This->DriverBindingHandle,
Handle,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL
);
if (!EFI_ERROR (Status)) {
gBS->UninstallMultipleProtocolInterfaces (
Handle,
ProtocolGuid,
NULL,
NULL
);
}
return;
}
/**
Get the necessary size of buffer and read the variable.
First get the necessary size of buffer. Then read the
EFI variable (Name) and return a dynamically allocated
buffer. On failure return NULL.
@param Name String part of EFI variable name
@return Dynamically allocated memory that contains a copy of the EFI variable.
Caller is repsoncible freeing the buffer. Return NULL means Variable
was not read.
**/
VOID *
ConPlatformGetVariable (
IN CHAR16 *Name
)
{
EFI_STATUS Status;
VOID *Buffer;
UINTN BufferSize;
BufferSize = 0;
Buffer = NULL;
//
// Test to see if the variable exists. If it doesn't, return NULL.
//
Status = gRT->GetVariable (
Name,
&gEfiGlobalVariableGuid,
NULL,
&BufferSize,
Buffer
);
if (Status == EFI_BUFFER_TOO_SMALL) {
//
// Allocate the buffer to return
//
Buffer = AllocatePool (BufferSize);
if (Buffer == NULL) {
return NULL;
}
//
// Read variable into the allocated buffer.
//
Status = gRT->GetVariable (
Name,
&gEfiGlobalVariableGuid,
NULL,
&BufferSize,
Buffer
);
if (EFI_ERROR (Status)) {
FreePool (Buffer);
//
// To make sure Buffer is NULL if any error occurs.
//
Buffer = NULL;
}
}
return Buffer;
}
/**
Function returns TRUE when the two input device paths point to the two
GOP child handles that have the same parent.
@param Left A pointer to a device path data structure.
@param Right A pointer to a device path data structure.
@retval TRUE Left and Right share the same parent.
@retval FALSE Left and Right don't share the same parent or either of them is not
a GOP device path.
**/
BOOLEAN
IsGopSibling (
IN EFI_DEVICE_PATH_PROTOCOL *Left,
IN EFI_DEVICE_PATH_PROTOCOL *Right
)
{
EFI_DEVICE_PATH_PROTOCOL *NodeLeft;
EFI_DEVICE_PATH_PROTOCOL *NodeRight;
for (NodeLeft = Left; !IsDevicePathEndType (NodeLeft); NodeLeft = NextDevicePathNode (NodeLeft)) {
if (((DevicePathType (NodeLeft) == ACPI_DEVICE_PATH) && (DevicePathSubType (NodeLeft) == ACPI_ADR_DP)) ||
((DevicePathType (NodeLeft) == HARDWARE_DEVICE_PATH) && (DevicePathSubType (NodeLeft) == HW_CONTROLLER_DP) &&
(DevicePathType (NextDevicePathNode (NodeLeft)) == ACPI_DEVICE_PATH) && (DevicePathSubType (NextDevicePathNode (NodeLeft)) == ACPI_ADR_DP)))
{
break;
}
}
if (IsDevicePathEndType (NodeLeft)) {
return FALSE;
}
for (NodeRight = Right; !IsDevicePathEndType (NodeRight); NodeRight = NextDevicePathNode (NodeRight)) {
if (((DevicePathType (NodeRight) == ACPI_DEVICE_PATH) && (DevicePathSubType (NodeRight) == ACPI_ADR_DP)) ||
((DevicePathType (NodeRight) == HARDWARE_DEVICE_PATH) && (DevicePathSubType (NodeRight) == HW_CONTROLLER_DP) &&
(DevicePathType (NextDevicePathNode (NodeRight)) == ACPI_DEVICE_PATH) && (DevicePathSubType (NextDevicePathNode (NodeRight)) == ACPI_ADR_DP)))
{
break;
}
}
if (IsDevicePathEndType (NodeRight)) {
return FALSE;
}
if (((UINTN)NodeLeft - (UINTN)Left) != ((UINTN)NodeRight - (UINTN)Right)) {
return FALSE;
}
return (BOOLEAN)(CompareMem (Left, Right, (UINTN)NodeLeft - (UINTN)Left) == 0);
}
/**
Check whether a USB device match the specified USB Class device path. This
function follows "Load Option Processing" behavior in UEFI specification.
@param UsbIo USB I/O protocol associated with the USB device.
@param UsbClass The USB Class device path to match.
@retval TRUE The USB device match the USB Class device path.
@retval FALSE The USB device does not match the USB Class device path.
**/
BOOLEAN
MatchUsbClass (
IN EFI_USB_IO_PROTOCOL *UsbIo,
IN USB_CLASS_DEVICE_PATH *UsbClass
)
{
EFI_STATUS Status;
EFI_USB_DEVICE_DESCRIPTOR DevDesc;
EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
UINT8 DeviceClass;
UINT8 DeviceSubClass;
UINT8 DeviceProtocol;
if ((DevicePathType (UsbClass) != MESSAGING_DEVICE_PATH) ||
(DevicePathSubType (UsbClass) != MSG_USB_CLASS_DP))
{
return FALSE;
}
//
// Check Vendor Id and Product Id.
//
Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
if (EFI_ERROR (Status)) {
return FALSE;
}
if ((UsbClass->VendorId != 0xffff) &&
(UsbClass->VendorId != DevDesc.IdVendor))
{
return FALSE;
}
if ((UsbClass->ProductId != 0xffff) &&
(UsbClass->ProductId != DevDesc.IdProduct))
{
return FALSE;
}
DeviceClass = DevDesc.DeviceClass;
DeviceSubClass = DevDesc.DeviceSubClass;
DeviceProtocol = DevDesc.DeviceProtocol;
if ((DeviceClass == 0) ||
((DeviceClass == USB_BASE_CLASS_MISCELLANEOUS) &&
(DeviceSubClass == USB_MISCELLANEOUS_SUBCLASS_COMMON) &&
(DeviceProtocol == USB_MISCELLANEOUS_PROTOCOL_IAD)))
{
//
// If Class in Device Descriptor is set to 0 (Device), or
// Class/SubClass/Protocol is 0xEF/0x02/0x01 (IAD), use the Class, SubClass
// and Protocol in Interface Descriptor instead.
//
Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
if (EFI_ERROR (Status)) {
return FALSE;
}
DeviceClass = IfDesc.InterfaceClass;
DeviceSubClass = IfDesc.InterfaceSubClass;
DeviceProtocol = IfDesc.InterfaceProtocol;
}
//
// Check Class, SubClass and Protocol.
//
if ((UsbClass->DeviceClass != 0xff) &&
(UsbClass->DeviceClass != DeviceClass))
{
return FALSE;
}
if ((UsbClass->DeviceSubClass != 0xff) &&
(UsbClass->DeviceSubClass != DeviceSubClass))
{
return FALSE;
}
if ((UsbClass->DeviceProtocol != 0xff) &&
(UsbClass->DeviceProtocol != DeviceProtocol))
{
return FALSE;
}
return TRUE;
}
/**
Check whether a USB device match the specified USB WWID device path. This
function follows "Load Option Processing" behavior in UEFI specification.
@param UsbIo USB I/O protocol associated with the USB device.
@param UsbWwid The USB WWID device path to match.
@retval TRUE The USB device match the USB WWID device path.
@retval FALSE The USB device does not match the USB WWID device path.
**/
BOOLEAN
MatchUsbWwid (
IN EFI_USB_IO_PROTOCOL *UsbIo,
IN USB_WWID_DEVICE_PATH *UsbWwid
)
{
EFI_STATUS Status;
EFI_USB_DEVICE_DESCRIPTOR DevDesc;
EFI_USB_INTERFACE_DESCRIPTOR IfDesc;
UINT16 *LangIdTable;
UINT16 TableSize;
UINT16 Index;
CHAR16 *CompareStr;
UINTN CompareLen;
CHAR16 *SerialNumberStr;
UINTN Length;
if ((DevicePathType (UsbWwid) != MESSAGING_DEVICE_PATH) ||
(DevicePathSubType (UsbWwid) != MSG_USB_WWID_DP))
{
return FALSE;
}
//
// Check Vendor Id and Product Id.
//
Status = UsbIo->UsbGetDeviceDescriptor (UsbIo, &DevDesc);
if (EFI_ERROR (Status)) {
return FALSE;
}
if ((DevDesc.IdVendor != UsbWwid->VendorId) ||
(DevDesc.IdProduct != UsbWwid->ProductId))
{
return FALSE;
}
//
// Check Interface Number.
//
Status = UsbIo->UsbGetInterfaceDescriptor (UsbIo, &IfDesc);
if (EFI_ERROR (Status)) {
return FALSE;
}
if (IfDesc.InterfaceNumber != UsbWwid->InterfaceNumber) {
return FALSE;
}
//
// Check Serial Number.
//
if (DevDesc.StrSerialNumber == 0) {
return FALSE;
}
//
// Get all supported languages.
//
TableSize = 0;
LangIdTable = NULL;
Status = UsbIo->UsbGetSupportedLanguages (UsbIo, &LangIdTable, &TableSize);
if (EFI_ERROR (Status) || (TableSize == 0) || (LangIdTable == NULL)) {
return FALSE;
}
//
// Serial number in USB WWID device path is the last 64-or-less UTF-16 characters.
//
CompareStr = (CHAR16 *)(UINTN)(UsbWwid + 1);
CompareLen = (DevicePathNodeLength (UsbWwid) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16);
if (CompareStr[CompareLen - 1] == L'\0') {
CompareLen--;
}
//
// Compare serial number in each supported language.
//
for (Index = 0; Index < TableSize / sizeof (UINT16); Index++) {
SerialNumberStr = NULL;
Status = UsbIo->UsbGetStringDescriptor (
UsbIo,
LangIdTable[Index],
DevDesc.StrSerialNumber,
&SerialNumberStr
);
if (EFI_ERROR (Status) || (SerialNumberStr == NULL)) {
continue;
}
Length = StrLen (SerialNumberStr);
if ((Length >= CompareLen) &&
(CompareMem (SerialNumberStr + Length - CompareLen, CompareStr, CompareLen * sizeof (CHAR16)) == 0))
{
FreePool (SerialNumberStr);
return TRUE;
}
FreePool (SerialNumberStr);
}
return FALSE;
}
/**
Compare whether a full console device path matches a USB shortform device path.
@param[in] FullPath Full console device path.
@param[in] ShortformPath Short-form device path. Short-form device node may in the beginning or in the middle.
@retval TRUE The full console device path matches the short-form device path.
@retval FALSE The full console device path doesn't match the short-form device path.
**/
BOOLEAN
MatchUsbShortformDevicePath (
IN EFI_DEVICE_PATH_PROTOCOL *FullPath,
IN EFI_DEVICE_PATH_PROTOCOL *ShortformPath
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *ShortformNode;
UINTN ParentDevicePathSize;
EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
EFI_USB_IO_PROTOCOL *UsbIo;
EFI_HANDLE Handle;
for ( ShortformNode = ShortformPath
; !IsDevicePathEnd (ShortformNode)
; ShortformNode = NextDevicePathNode (ShortformNode)
)
{
if ((DevicePathType (ShortformNode) == MESSAGING_DEVICE_PATH) &&
((DevicePathSubType (ShortformNode) == MSG_USB_CLASS_DP) ||
(DevicePathSubType (ShortformNode) == MSG_USB_WWID_DP))
)
{
break;
}
}
//
// Skip further compare when it's not a shortform device path.
//
if (IsDevicePathEnd (ShortformNode)) {
return FALSE;
}
//
// Compare the parent device path when the ShortformPath doesn't start with short-form node.
//
ParentDevicePathSize = (UINTN)ShortformNode - (UINTN)ShortformPath;
RemainingDevicePath = FullPath;
Status = gBS->LocateDevicePath (&gEfiUsbIoProtocolGuid, &RemainingDevicePath, &Handle);
if (EFI_ERROR (Status)) {
return FALSE;
}
if (ParentDevicePathSize != 0) {
if ((ParentDevicePathSize > (UINTN)RemainingDevicePath - (UINTN)FullPath) ||
(CompareMem (FullPath, ShortformPath, ParentDevicePathSize) != 0))
{
return FALSE;
}
}
//
// Compar the USB layer.
//
Status = gBS->HandleProtocol (
Handle,
&gEfiUsbIoProtocolGuid,
(VOID **)&UsbIo
);
ASSERT_EFI_ERROR (Status);
return MatchUsbClass (UsbIo, (USB_CLASS_DEVICE_PATH *)ShortformNode) ||
MatchUsbWwid (UsbIo, (USB_WWID_DEVICE_PATH *)ShortformNode);
}
/**
Function compares a device path data structure to that of all the nodes of a
second device path instance.
@param Multi A pointer to a multi-instance device path data structure.
@param Single A pointer to a single-instance device path data structure.
@param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
points to the remaining device path data structure.
(remaining device path = Multi - Single.)
@param Delete If TRUE, means removing Single from Multi.
If FALSE, the routine just check whether Single matches
with any instance in Multi.
@retval EFI_SUCCESS If the Single is contained within Multi.
@retval EFI_NOT_FOUND If the Single is not contained within Multi.
@retval EFI_INVALID_PARAMETER Multi is NULL.
@retval EFI_INVALID_PARAMETER Single is NULL.
@retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.
**/
EFI_STATUS
ConPlatformMatchDevicePaths (
IN EFI_DEVICE_PATH_PROTOCOL *Multi,
IN EFI_DEVICE_PATH_PROTOCOL *Single,
OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,
IN BOOLEAN Delete
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePath;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;
EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
UINTN Size;
//
// The passed in DevicePath should not be NULL
//
if ((Multi == NULL) || (Single == NULL)) {
return EFI_INVALID_PARAMETER;
}
//
// If performing Delete operation, the NewDevicePath must not be NULL.
//
if (Delete && (NewDevicePath == NULL)) {
return EFI_INVALID_PARAMETER;
}
TempDevicePath1 = NULL;
DevicePath = Multi;
DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
//
// Search for the match of 'Single' in 'Multi'
//
while (DevicePathInst != NULL) {
if ((CompareMem (Single, DevicePathInst, Size) == 0) ||
IsGopSibling (Single, DevicePathInst) || MatchUsbShortformDevicePath (Single, DevicePathInst))
{
if (!Delete) {
//
// If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
//
FreePool (DevicePathInst);
return EFI_SUCCESS;
}
} else {
if (Delete) {
//
// If the node of Multi does not match Single, then added it back to the result.
// That is, the node matching Single will be dropped and deleted from result.
//
TempDevicePath2 = AppendDevicePathInstance (
TempDevicePath1,
DevicePathInst
);
if (TempDevicePath1 != NULL) {
FreePool (TempDevicePath1);
}
TempDevicePath1 = TempDevicePath2;
}
}
FreePool (DevicePathInst);
DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
}
if (Delete) {
//
// Return the new device path data structure with specified node deleted.
//
*NewDevicePath = TempDevicePath1;
return EFI_SUCCESS;
}
return EFI_NOT_FOUND;
}
/**
Update console environment variables.
@param VariableName Console environment variables, ConOutDev, ConInDev
ErrOutDev, ConIn ,ConOut or ErrOut.
@param DevicePath Console devcie's device path.
@param Operation Variable operations, including APPEND, CHECK and DELETE.
@retval EFI_SUCCESS Variable operates successfully.
@retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
@retval other Variable updating failed.
**/
EFI_STATUS
ConPlatformUpdateDeviceVariable (
IN CHAR16 *VariableName,
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
IN CONPLATFORM_VAR_OPERATION Operation
)
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
Status = EFI_SUCCESS;
VariableDevicePath = NULL;
NewVariableDevicePath = NULL;
//
// Get Variable according to variable name.
// The memory for Variable is allocated within ConPlatformGetVarible(),
// it is the caller's responsibility to free the memory before return.
//
VariableDevicePath = ConPlatformGetVariable (VariableName);
// At this point, VariableDevicePath may be null. This is expected.
if (Operation != Delete) {
//
// Match specified DevicePath in Console Variable.
//
Status = ConPlatformMatchDevicePaths (
VariableDevicePath,
DevicePath,
NULL,
FALSE
);
if ((Operation == Check) || (!EFI_ERROR (Status))) {
//
// Branch here includes 2 cases:
// 1. Operation is CHECK, simply return Status.
// 2. Operation is APPEND, and device path already exists in variable, also return.
//
if (VariableDevicePath != NULL) {
FreePool (VariableDevicePath);
}
return Status;
}
//
// We reach here to append a device path that does not exist in variable.
//
Status = EFI_SUCCESS;
NewVariableDevicePath = AppendDevicePathInstance (
VariableDevicePath,
DevicePath
);
if (NewVariableDevicePath == NULL) {
Status = EFI_OUT_OF_RESOURCES;
}
} else {
//
// We reach here to remove DevicePath from the environment variable that
// is a multi-instance device path.
//
Status = ConPlatformMatchDevicePaths (
VariableDevicePath,
DevicePath,
&NewVariableDevicePath,
TRUE
);
}
if (VariableDevicePath != NULL) {
FreePool (VariableDevicePath);
}
if (EFI_ERROR (Status)) {
return Status;
}
if (NewVariableDevicePath != NULL) {
//
// Update Console Environment Variable.
//
Status = gRT->SetVariable (
VariableName,
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
GetDevicePathSize (NewVariableDevicePath),
NewVariableDevicePath
);
FreePool (NewVariableDevicePath);
}
return Status;
}
/**
Update ConOutDev and ErrOutDev variables to add the device path of
GOP controller itself and the sibling controllers.
@param DevicePath Pointer to device's device path.
@retval TRUE The devcie is a GOP device.
@retval FALSE The devcie is not a GOP device.
**/
BOOLEAN
ConPlatformUpdateGopCandidate (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
)
{
EFI_STATUS Status;
EFI_HANDLE PciHandle;
EFI_HANDLE GopHandle;
EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
//
// Check whether it's a GOP device.
//
TempDevicePath = DevicePath;
Status = gBS->LocateDevicePath (&gEfiGraphicsOutputProtocolGuid, &TempDevicePath, &GopHandle);
if (EFI_ERROR (Status)) {
return FALSE;
}
//
// Get the parent PciIo handle in order to find all the children
//
Status = gBS->LocateDevicePath (&gEfiPciIoProtocolGuid, &DevicePath, &PciHandle);
if (EFI_ERROR (Status)) {
return FALSE;
}
TempDevicePath = EfiBootManagerGetGopDevicePath (PciHandle);
if (TempDevicePath != NULL) {
ConPlatformUpdateDeviceVariable (L"ConOutDev", TempDevicePath, Append);
ConPlatformUpdateDeviceVariable (L"ErrOutDev", TempDevicePath, Append);
}
return TRUE;
}
|