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
|
// SPDX-License-Identifier: MIT
/*
* Copyright 2006-2012 Red Hat, Inc.
* Copyright 2018-2020 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
*
* Author: Adam Jackson <ajax@nwnk.net>
* Maintainer: Hans Verkuil <hverkuil-cisco@xs4all.nl>
*/
#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "edid-decode.h"
#define STR(x) #x
#define STRING(x) STR(x)
static edid_state state;
static unsigned char edid[EDID_PAGE_SIZE * EDID_MAX_BLOCKS];
static bool odd_hex_digits;
enum output_format {
OUT_FMT_DEFAULT,
OUT_FMT_HEX,
OUT_FMT_RAW,
OUT_FMT_CARRAY,
OUT_FMT_XML,
};
/*
* Options
* Please keep in alphabetical order of the short option.
* That makes it easier to see which options are still free.
*/
enum Option {
OptCheck = 'c',
OptCheckInline = 'C',
OptHelp = 'h',
OptNativeTimings = 'n',
OptOutputFormat = 'o',
OptPreferredTimings = 'p',
OptPhysicalAddress = 'P',
OptLongTimings = 'L',
OptShortTimings = 'S',
OptFBModeTimings = 'F',
OptXModeLineTimings = 'X',
OptV4L2Timings = 'V',
OptSkipHexDump = 's',
OptSkipSHA = 128,
OptHideSerialNumbers,
OptVersion,
OptLast = 256
};
static char options[OptLast];
static struct option long_options[] = {
{ "help", no_argument, 0, OptHelp },
{ "output-format", required_argument, 0, OptOutputFormat },
{ "native-timings", no_argument, 0, OptNativeTimings },
{ "preferred-timings", no_argument, 0, OptPreferredTimings },
{ "physical-address", no_argument, 0, OptPhysicalAddress },
{ "skip-hex-dump", no_argument, 0, OptSkipHexDump },
{ "skip-sha", no_argument, 0, OptSkipSHA },
{ "hide-serial-numbers", no_argument, 0, OptHideSerialNumbers },
{ "version", no_argument, 0, OptVersion },
{ "check-inline", no_argument, 0, OptCheckInline },
{ "check", no_argument, 0, OptCheck },
{ "short-timings", no_argument, 0, OptShortTimings },
{ "long-timings", no_argument, 0, OptLongTimings },
{ "xmodeline", no_argument, 0, OptXModeLineTimings },
{ "fbmode", no_argument, 0, OptFBModeTimings },
{ "v4l2-timings", no_argument, 0, OptV4L2Timings },
{ 0, 0, 0, 0 }
};
static void usage(void)
{
printf("Usage: edid-decode <options> [in [out]]\n"
" [in] EDID file to parse. Read from standard input if none given\n"
" or if the input filename is '-'.\n"
" [out] Output the read EDID to this file. Write to standard output\n"
" if the output filename is '-'.\n"
"\nOptions:\n"
" -o, --output-format <fmt>\n"
" if [out] is specified, then write the EDID in this format\n"
" <fmt> is one of:\n"
" hex: hex numbers in ascii text (default for stdout)\n"
" raw: binary data (default unless writing to stdout)\n"
" carray: c-program struct\n"
" xml: XML data\n"
" -c, --check check if the EDID conforms to the standards, failures and\n"
" warnings are reported at the end.\n"
" -C, --check-inline check if the EDID conforms to the standards, failures and\n"
" warnings are reported inline.\n"
" -n, --native-timings report the native timings\n"
" -p, --preferred-timings report the preferred timings\n"
" -P, --physical-address only report the CEC physical address\n"
" -S, --short-timings report all video timings in a short format\n"
" -L, --long-timings report all video timings in a long format\n"
" -X, --xmodeline report all long video timings in Xorg.conf format\n"
" -F, --fbmode report all long video timings in fb.modes format\n"
" -V, --v4l2-timings report all long video timings in v4l2-dv-timings.h format\n"
" -s, --skip-hex-dump skip the initial hex dump of the EDID\n"
" --skip-sha skip the SHA report\n"
" --hide-serial-numbers replace serial numbers with '...'\n"
" --version show the edid-decode version (SHA)\n"
" -h, --help display this help message\n");
}
static std::string s_msgs[EDID_MAX_BLOCKS + 1][2];
void msg(bool is_warn, const char *fmt, ...)
{
char buf[1024] = "";
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
if (is_warn)
state.warnings++;
else
state.failures++;
if (state.data_block.empty())
s_msgs[state.block_nr][is_warn] += std::string(" ") + buf;
else
s_msgs[state.block_nr][is_warn] += " " + state.data_block + ": " + buf;
if (options[OptCheckInline])
printf("%s: %s", is_warn ? "WARN" : "FAIL", buf);
}
static void show_msgs(bool is_warn)
{
printf("\n%s:\n\n", is_warn ? "Warnings" : "Failures");
for (unsigned i = 0; i < state.num_blocks; i++) {
if (s_msgs[i][is_warn].empty())
continue;
printf("Block %u, %s:\n%s",
i, block_name(edid[i * EDID_PAGE_SIZE]).c_str(),
s_msgs[i][is_warn].c_str());
}
if (s_msgs[EDID_MAX_BLOCKS][is_warn].empty())
return;
printf("EDID:\n%s",
s_msgs[EDID_MAX_BLOCKS][is_warn].c_str());
}
void do_checksum(const char *prefix, const unsigned char *x, size_t len)
{
unsigned char check = x[len - 1];
unsigned char sum = 0;
unsigned i;
printf("%sChecksum: 0x%02hhx", prefix, check);
for (i = 0; i < len-1; i++)
sum += x[i];
if ((unsigned char)(check + sum) != 0) {
printf(" (should be 0x%02x)\n", -sum & 0xff);
fail("Invalid checksum 0x%02x (should be 0x%02x).\n",
check, -sum & 0xff);
return;
}
printf("\n");
}
static unsigned gcd(unsigned a, unsigned b)
{
while (b) {
unsigned t = b;
b = a % b;
a = t;
}
return a;
}
void calc_ratio(struct timings *t)
{
unsigned d = gcd(t->hact, t->vact);
if (d == 0) {
t->hratio = t->vratio = 0;
return;
}
t->hratio = t->hact / d;
t->vratio = t->vact / d;
}
std::string edid_state::dtd_type(unsigned cnt)
{
unsigned len = std::to_string(cta.preparse_total_dtds).length();
char buf[16];
sprintf(buf, "DTD %*u", len, cnt);
return buf;
}
bool edid_state::match_timings(const timings &t1, const timings &t2)
{
if (t1.hact != t2.hact ||
t1.vact != t2.vact ||
t1.rb != t2.rb ||
t1.interlaced != t2.interlaced ||
t1.hfp != t2.hfp ||
t1.hbp != t2.hbp ||
t1.hsync != t2.hsync ||
t1.pos_pol_hsync != t2.pos_pol_hsync ||
t1.hratio != t2.hratio ||
t1.vfp != t2.vfp ||
t1.vbp != t2.vbp ||
t1.vsync != t2.vsync ||
t1.pos_pol_vsync != t2.pos_pol_vsync ||
t1.vratio != t2.vratio ||
t1.pixclk_khz != t2.pixclk_khz)
return false;
return true;
}
static void or_str(std::string &s, const std::string &flag, unsigned &num_flags)
{
if (!num_flags)
s = flag;
else if (num_flags % 2 == 0)
s = s + " | \\\n\t\t" + flag;
else
s = s + " | " + flag;
num_flags++;
}
static void print_modeline(unsigned indent, const struct timings *t, double refresh)
{
unsigned offset = (!t->even_vtotal && t->interlaced) ? 1 : 0;
printf("%*sModeline \"%ux%u_%.2f%s\" %.3f %u %u %u %u %u %u %u %u %cHSync",
indent, "",
t->hact, t->vact, refresh,
t->interlaced ? "i" : "", t->pixclk_khz / 1000.0,
t->hact, t->hact + t->hfp, t->hact + t->hfp + t->hsync,
t->hact + t->hfp + t->hsync + t->hbp,
t->vact, t->vact + t->vfp, t->vact + t->vfp + t->vsync,
t->vact + t->vfp + t->vsync + t->vbp + offset,
t->pos_pol_hsync ? '+' : '-');
if (!t->no_pol_vsync)
printf(" %cVSync", t->pos_pol_vsync ? '+' : '-');
if (t->interlaced)
printf(" Interlace");
printf("\n");
}
static void print_fbmode(unsigned indent, const struct timings *t,
double refresh, double hor_freq_khz)
{
printf("%*smode \"%ux%u-%u%s\"\n",
indent, "",
t->hact, t->vact,
(unsigned)(0.5 + (t->interlaced ? refresh / 2.0 : refresh)),
t->interlaced ? "-lace" : "");
printf("%*s# D: %.2f MHz, H: %.3f kHz, V: %.2f Hz\n",
indent + 8, "",
t->pixclk_khz / 1000.0, hor_freq_khz, refresh);
printf("%*sgeometry %u %u %u %u 32\n",
indent + 8, "",
t->hact, t->vact, t->hact, t->vact);
unsigned mult = t->interlaced ? 2 : 1;
unsigned offset = !t->even_vtotal && t->interlaced;
printf("%*stimings %llu %d %d %d %u %u %u\n",
indent + 8, "",
(unsigned long long)(1000000000.0 / (double)(t->pixclk_khz) + 0.5),
t->hbp, t->hfp, mult * t->vbp, mult * t->vfp + offset, t->hsync, mult * t->vsync);
if (t->interlaced)
printf("%*slaced true\n", indent + 8, "");
if (t->pos_pol_hsync)
printf("%*shsync high\n", indent + 8, "");
if (t->pos_pol_vsync)
printf("%*svsync high\n", indent + 8, "");
printf("%*sendmode\n", indent, "");
}
static void print_v4l2_timing(const struct timings *t,
double refresh, const char *type)
{
printf("\t#define V4L2_DV_BT_%uX%u%c%u_%02u { \\\n",
t->hact, t->vact, t->interlaced ? 'I' : 'P',
(unsigned)refresh, (unsigned)(0.5 + 100.0 * (refresh - (unsigned)refresh)));
printf("\t\t.type = V4L2_DV_BT_656_1120, \\\n");
printf("\t\tV4L2_INIT_BT_TIMINGS(%u, %u, %u, ",
t->hact, t->vact, t->interlaced);
if (!t->pos_pol_hsync && !t->pos_pol_vsync)
printf("0, \\\n");
else if (t->pos_pol_hsync && t->pos_pol_vsync)
printf("\\\n\t\t\tV4L2_DV_HSYNC_POS_POL | V4L2_DV_VSYNC_POS_POL, \\\n");
else if (t->pos_pol_hsync)
printf("V4L2_DV_HSYNC_POS_POL, \\\n");
else
printf("V4L2_DV_VSYNC_POS_POL, \\\n");
printf("\t\t\t%lluULL, %d, %u, %d, %u, %u, %d, %u, %u, %d, \\\n",
t->pixclk_khz * 1000ULL, t->hfp, t->hsync, t->hbp,
t->vfp, t->vsync, t->vbp,
t->interlaced ? t->vfp : 0,
t->interlaced ? t->vsync : 0,
t->interlaced ? t->vbp + !t->even_vtotal : 0);
std::string flags;
unsigned num_flags = 0;
unsigned vic = 0;
unsigned hdmi_vic = 0;
const char *std = "0";
if (t->interlaced && !t->even_vtotal)
or_str(flags, "V4L2_DV_FL_HALF_LINE", num_flags);
if (!memcmp(type, "VIC", 3)) {
or_str(flags, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags);
or_str(flags, "V4L2_DV_FL_IS_CE_VIDEO", num_flags);
vic = strtoul(type + 4, 0, 0);
}
if (!memcmp(type, "HDMI VIC", 8)) {
or_str(flags, "V4L2_DV_FL_HAS_HDMI_VIC", num_flags);
or_str(flags, "V4L2_DV_FL_IS_CE_VIDEO", num_flags);
hdmi_vic = strtoul(type + 9, 0, 0);
vic = hdmi_vic_to_vic(hdmi_vic);
if (vic)
or_str(flags, "V4L2_DV_FL_HAS_CEA861_VIC", num_flags);
}
if (vic && (fmod(refresh, 6)) == 0.0)
or_str(flags, "V4L2_DV_FL_CAN_REDUCE_FPS", num_flags);
if (t->rb)
or_str(flags, "V4L2_DV_FL_REDUCED_BLANKING", num_flags);
if (t->hratio && t->vratio)
or_str(flags, "V4L2_DV_FL_HAS_PICTURE_ASPECT", num_flags);
if (!memcmp(type, "VIC", 3) || !memcmp(type, "HDMI VIC", 8))
std = "V4L2_DV_BT_STD_CEA861";
else if (!memcmp(type, "DMT", 3))
std = "V4L2_DV_BT_STD_DMT";
else if (!memcmp(type, "CVT", 3))
std = "V4L2_DV_BT_STD_CVT";
else if (!memcmp(type, "GTF", 3))
std = "V4L2_DV_BT_STD_GTF";
printf("\t\t\t%s, \\\n", std);
printf("\t\t\t%s, \\\n", flags.empty() ? "0" : flags.c_str());
printf("\t\t\t{ %u, %u }, %u, %u) \\\n",
t->hratio, t->vratio, vic, hdmi_vic);
printf("\t}\n");
}
static void print_detailed_timing(unsigned indent, const struct timings *t)
{
printf("%*sHfront %4d Hsync %3u Hback %3d Hpol %s",
indent, "",
t->hfp, t->hsync, t->hbp, t->pos_pol_hsync ? "P" : "N");
if (t->hborder)
printf(" Hborder %u", t->hborder);
printf("\n");
printf("%*sVfront %4u Vsync %3u Vback %3d",
indent, "", t->vfp, t->vsync, t->vbp);
if (!t->no_pol_vsync)
printf(" Vpol %s", t->pos_pol_vsync ? "P" : "N");
if (t->vborder)
printf(" Vborder %u", t->vborder);
if (t->even_vtotal) {
printf(" Both Fields");
} else if (t->interlaced) {
printf(" Vfront +0.5 Odd Field\n");
printf("%*sVfront %4d Vsync %3u Vback %3d",
indent, "", t->vfp, t->vsync, t->vbp);
if (!t->no_pol_vsync)
printf(" Vpol %s", t->pos_pol_vsync ? "P" : "N");
if (t->vborder)
printf(" Vborder %u", t->vborder);
printf(" Vback +0.5 Even Field");
}
printf("\n");
}
bool edid_state::print_timings(const char *prefix, const struct timings *t,
const char *type, const char *flags,
bool detailed)
{
if (!t) {
// Should not happen
fail("Unknown video timings.\n");
return false;
}
if (detailed && options[OptShortTimings])
detailed = false;
if (options[OptLongTimings])
detailed = true;
unsigned vact = t->vact;
unsigned hbl = t->hfp + t->hsync + t->hbp;
unsigned vbl = t->vfp + t->vsync + t->vbp;
unsigned htotal = t->hact + hbl;
double hor_freq_khz = htotal ? (double)t->pixclk_khz / htotal : 0;
if (t->interlaced)
vact /= 2;
if (t->ycbcr420)
hor_freq_khz /= 2;
double vtotal = vact + vbl;
bool ok = true;
if (!t->hact || !hbl || !t->hfp || !t->hsync ||
!vact || !vbl || (!t->vfp && !t->interlaced && !t->even_vtotal) || !t->vsync) {
fail("0 values in the video timing:\n"
" Horizontal Active/Blanking %u/%u\n"
" Horizontal Frontporch/Sync Width %u/%u\n"
" Vertical Active/Blanking %u/%u\n"
" Vertical Frontporch/Sync Width %u/%u\n",
t->hact, hbl, t->hfp, t->hsync, vact, vbl, t->vfp, t->vsync);
ok = false;
}
if (t->even_vtotal)
vtotal = vact + t->vfp + t->vsync + t->vbp;
else if (t->interlaced)
vtotal = vact + t->vfp + t->vsync + t->vbp + 0.5;
double refresh = (double)t->pixclk_khz * 1000.0 / (htotal * vtotal);
std::string s;
if (t->rb) {
s = "RB";
if (t->rb == 2)
s += "v2";
else if (t->rb == 3)
s += "v3";
}
add_str(s, flags);
if (t->hsize_mm || t->vsize_mm)
add_str(s, std::to_string(t->hsize_mm) + " mm x " + std::to_string(t->vsize_mm) + " mm");
if (!s.empty())
s = " (" + s + ")";
unsigned pixclk_khz = t->pixclk_khz / (t->ycbcr420 ? 2 : 1);
char buf[10];
sprintf(buf, "%u%s", t->vact, t->interlaced ? "i" : "");
printf("%s%s: %5ux%-5s %7.3f Hz %3u:%-3u %7.3f kHz %7.3f MHz%s\n",
prefix, type,
t->hact, buf,
refresh,
t->hratio, t->vratio,
hor_freq_khz,
pixclk_khz / 1000.0,
s.c_str());
unsigned len = strlen(prefix) + 2;
if (!t->ycbcr420 && detailed && options[OptXModeLineTimings])
print_modeline(len, t, refresh);
else if (!t->ycbcr420 && detailed && options[OptFBModeTimings])
print_fbmode(len, t, refresh, hor_freq_khz);
else if (!t->ycbcr420 && detailed && options[OptV4L2Timings])
print_v4l2_timing(t, refresh, type);
else if (detailed)
print_detailed_timing(len + strlen(type) + 6, t);
if (t->ycbcr420 && t->pixclk_khz < 590000)
warn_once("Some YCbCr 4:2:0 timings are invalid for HDMI (which requires an RGB timings pixel rate >= 590 MHz).\n");
if (t->hfp <= 0)
fail("0 or negative horizontal front porch.\n");
if (t->hbp <= 0)
fail("0 or negative horizontal back porch.\n");
if (t->vbp <= 0)
fail("0 or negative vertical back porch.\n");
if ((!base.max_display_width_mm && t->hsize_mm) ||
(!base.max_display_height_mm && t->vsize_mm)) {
fail("Mismatch of image size vs display size: image size is set, but not display size.\n");
} else if (!t->hsize_mm && !t->vsize_mm) {
/* this is valid */
} else if (t->hsize_mm > base.max_display_width_mm + 9 ||
t->vsize_mm > base.max_display_height_mm + 9) {
fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
} else if (t->hsize_mm < base.max_display_width_mm - 9 &&
t->vsize_mm < base.max_display_height_mm - 9) {
fail("Mismatch of image size %ux%u mm vs display size %ux%u mm.\n",
t->hsize_mm, t->vsize_mm, base.max_display_width_mm, base.max_display_height_mm);
}
if (refresh) {
min_vert_freq_hz = min(min_vert_freq_hz, refresh);
max_vert_freq_hz = max(max_vert_freq_hz, refresh);
}
if (pixclk_khz && (t->hact + hbl)) {
min_hor_freq_hz = min(min_hor_freq_hz, (pixclk_khz * 1000) / (t->hact + hbl));
max_hor_freq_hz = max(max_hor_freq_hz, (pixclk_khz * 1000) / (t->hact + hbl));
max_pixclk_khz = max(max_pixclk_khz, pixclk_khz);
}
return ok;
}
std::string utohex(unsigned char x)
{
char buf[10];
sprintf(buf, "0x%02hhx", x);
return buf;
}
const char *oui_name(unsigned oui, bool reverse)
{
if (reverse)
oui = (oui >> 16) | (oui & 0xff00) | ((oui & 0xff) << 16);
switch (oui) {
case 0x00001a: return "AMD";
case 0x000c03: return "HDMI";
case 0x00044b: return "NVIDIA";
case 0x000c6e: return "ASUS";
case 0x0010fa: return "Apple";
case 0x0014b9: return "MSTAR";
case 0x00d046: return "Dolby";
case 0x00e047: return "InFocus";
case 0x3a0292: return "VESA";
case 0x90848b: return "HDR10+";
case 0xc45dd8: return "HDMI Forum";
case 0xca125c: return "Microsoft";
default: return NULL;
}
}
std::string ouitohex(unsigned oui)
{
char buf[32];
sprintf(buf, "%02X-%02X-%02X", (oui >> 16) & 0xff, (oui >> 8) & 0xff, oui & 0xff);
return buf;
}
bool memchk(const unsigned char *x, unsigned len, unsigned char v)
{
for (unsigned i = 0; i < len; i++)
if (x[i] != v)
return false;
return true;
}
void hex_block(const char *prefix, const unsigned char *x,
unsigned length, bool show_ascii, unsigned step)
{
unsigned i, j;
if (!length)
return;
for (i = 0; i < length; i += step) {
unsigned len = min(step, length - i);
printf("%s", prefix);
for (j = 0; j < len; j++)
printf("%s%02x", j ? " " : "", x[i + j]);
if (show_ascii) {
for (j = len; j < step; j++)
printf(" ");
printf(" '");
for (j = 0; j < len; j++)
printf("%c", x[i + j] >= ' ' && x[i + j] <= '~' ? x[i + j] : '.');
printf("'");
}
printf("\n");
}
}
static bool edid_add_byte(const char *s, bool two_digits = true)
{
char buf[3];
if (state.edid_size == sizeof(edid))
return false;
buf[0] = s[0];
buf[1] = two_digits ? s[1] : 0;
buf[2] = 0;
edid[state.edid_size++] = strtoul(buf, NULL, 16);
return true;
}
static bool extract_edid_quantumdata(const char *start)
{
/* Parse QuantumData 980 EDID files */
do {
start = strstr(start, ">");
if (!start)
return false;
start++;
for (unsigned i = 0; start[i] && start[i + 1] && i < 256; i += 2)
if (!edid_add_byte(start + i))
return false;
start = strstr(start, "<BLOCK");
} while (start);
return state.edid_size;
}
static const char *ignore_chars = ",:;";
static bool extract_edid_hex(const char *s, bool require_two_digits = true)
{
for (; *s; s++) {
if (isspace(*s) || strchr(ignore_chars, *s))
continue;
if (*s == '0' && tolower(s[1]) == 'x') {
s++;
continue;
}
/* Read one or two hex digits from the log */
if (!isxdigit(s[0])) {
if (state.edid_size && state.edid_size % 128 == 0)
break;
return false;
}
if (require_two_digits && !isxdigit(s[1])) {
odd_hex_digits = true;
return false;
}
if (!edid_add_byte(s, isxdigit(s[1])))
return false;
if (isxdigit(s[1]))
s++;
}
return state.edid_size;
}
static bool extract_edid_xrandr(const char *start)
{
static const char indentation1[] = " ";
static const char indentation2[] = "\t\t";
/* Used to detect that we've gone past the EDID property */
static const char half_indentation1[] = " ";
static const char half_indentation2[] = "\t";
const char *indentation;
const char *s;
for (;;) {
unsigned j;
/* Get the next start of the line of EDID hex, assuming spaces for indentation */
s = strstr(start, indentation = indentation1);
/* Did we skip the start of another property? */
if (s && s > strstr(start, half_indentation1))
break;
/* If we failed, retry assuming tabs for indentation */
if (!s) {
s = strstr(start, indentation = indentation2);
/* Did we skip the start of another property? */
if (s && s > strstr(start, half_indentation2))
break;
}
if (!s)
break;
start = s + strlen(indentation);
for (j = 0; j < 16; j++, start += 2) {
/* Read a %02x from the log */
if (!isxdigit(start[0]) || !isxdigit(start[1])) {
if (j)
break;
return false;
}
if (!edid_add_byte(start))
return false;
}
}
return state.edid_size;
}
static bool extract_edid_xorg(const char *start)
{
bool find_first_num = true;
for (; *start; start++) {
if (find_first_num) {
const char *s;
/* skip ahead to the : */
s = strstr(start, ": \t");
if (!s)
s = strstr(start, ": ");
if (!s)
break;
start = s;
/* and find the first number */
while (!isxdigit(start[1]))
start++;
find_first_num = false;
continue;
} else {
/* Read a %02x from the log */
if (!isxdigit(*start)) {
find_first_num = true;
continue;
}
if (!edid_add_byte(start))
return false;
start++;
}
}
return state.edid_size;
}
static bool extract_edid(int fd, FILE *error)
{
std::vector<char> edid_data;
char buf[EDID_PAGE_SIZE];
for (;;) {
ssize_t i = read(fd, buf, sizeof(buf));
if (i < 0)
return false;
if (i == 0)
break;
edid_data.insert(edid_data.end(), buf, buf + i);
}
if (edid_data.empty())
return false;
const char *data = &edid_data[0];
const char *start;
/* Look for edid-decode output */
start = strstr(data, "EDID (hex):");
if (!start)
start = strstr(data, "edid-decode (hex):");
if (start)
return extract_edid_hex(strchr(start, ':'));
/* Look for C-array */
start = strstr(data, "unsigned char edid[] = {");
if (start)
return extract_edid_hex(strchr(start, '{') + 1, false);
/* Look for QuantumData EDID output */
start = strstr(data, "<BLOCK");
if (start)
return extract_edid_quantumdata(start);
/* Look for xrandr --verbose output (lines of 16 hex bytes) */
start = strstr(data, "EDID_DATA:");
if (!start)
start = strstr(data, "EDID:");
if (start)
return extract_edid_xrandr(start);
/* Look for an EDID in an Xorg.0.log file */
start = strstr(data, "EDID (in hex):");
if (start)
start = strstr(start, "(II)");
if (start)
return extract_edid_xorg(start);
unsigned i;
/* Is the EDID provided in hex? */
for (i = 0; i < 32 && (isspace(data[i]) || strchr(ignore_chars, data[i]) ||
tolower(data[i]) == 'x' || isxdigit(data[i])); i++);
if (i == 32)
return extract_edid_hex(data);
/* Assume binary */
if (edid_data.size() > sizeof(edid)) {
fprintf(error, "Binary EDID length %zu is greater than %zu.\n",
edid_data.size(), sizeof(edid));
return false;
}
memcpy(edid, data, edid_data.size());
state.edid_size = edid_data.size();
return true;
}
static unsigned char crc_calc(const unsigned char *b)
{
unsigned char sum = 0;
unsigned i;
for (i = 0; i < 127; i++)
sum += b[i];
return 256 - sum;
}
static int crc_ok(const unsigned char *b)
{
return crc_calc(b) == b[127];
}
static void hexdumpedid(FILE *f, const unsigned char *edid, unsigned size)
{
unsigned b, i, j;
for (b = 0; b < size / 128; b++) {
const unsigned char *buf = edid + 128 * b;
if (b)
fprintf(f, "\n");
for (i = 0; i < 128; i += 0x10) {
fprintf(f, "%02x", buf[i]);
for (j = 1; j < 0x10; j++) {
fprintf(f, " %02x", buf[i + j]);
}
fprintf(f, "\n");
}
if (!crc_ok(buf))
fprintf(f, "Block %u has a checksum error (should be 0x%02x).\n",
b, crc_calc(buf));
}
}
static void carraydumpedid(FILE *f, const unsigned char *edid, unsigned size)
{
unsigned b, i, j;
fprintf(f, "const unsigned char edid[] = {\n");
for (b = 0; b < size / 128; b++) {
const unsigned char *buf = edid + 128 * b;
if (b)
fprintf(f, "\n");
for (i = 0; i < 128; i += 8) {
fprintf(f, "\t0x%02x,", buf[i]);
for (j = 1; j < 8; j++) {
fprintf(f, " 0x%02x,", buf[i + j]);
}
fprintf(f, "\n");
}
if (!crc_ok(buf))
fprintf(f, "\t/* Block %u has a checksum error (should be 0x%02x). */\n",
b, crc_calc(buf));
}
fprintf(f, "};\n");
}
// This format can be read by the QuantumData EDID editor
static void xmldumpedid(FILE *f, const unsigned char *edid, unsigned size)
{
fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
fprintf(f, "<DATAOBJ>\n");
fprintf(f, " <HEADER TYPE=\"DID\" VERSION=\"1.0\"/>\n");
fprintf(f, " <DATA>\n");
for (unsigned b = 0; b < size / 128; b++) {
const unsigned char *buf = edid + 128 * b;
fprintf(f, " <BLOCK%u>", b);
for (unsigned i = 0; i < 128; i++)
fprintf(f, "%02X", buf[i]);
fprintf(f, "</BLOCK%u>\n", b);
}
fprintf(f, " </DATA>\n");
fprintf(f, "</DATAOBJ>\n");
}
static int edid_to_file(const char *to_file, enum output_format out_fmt)
{
FILE *out;
if (!strcmp(to_file, "-")) {
to_file = "stdout";
out = stdout;
} else if ((out = fopen(to_file, "w")) == NULL) {
perror(to_file);
return -1;
}
if (out_fmt == OUT_FMT_DEFAULT)
out_fmt = out == stdout ? OUT_FMT_HEX : OUT_FMT_RAW;
switch (out_fmt) {
default:
case OUT_FMT_HEX:
hexdumpedid(out, edid, state.edid_size);
break;
case OUT_FMT_RAW:
fwrite(edid, state.edid_size, 1, out);
break;
case OUT_FMT_CARRAY:
carraydumpedid(out, edid, state.edid_size);
break;
case OUT_FMT_XML:
xmldumpedid(out, edid, state.edid_size);
break;
}
if (out != stdout)
fclose(out);
return 0;
}
static int edid_from_file(const char *from_file, FILE *error)
{
#ifdef O_BINARY
// Windows compatibility
int flags = O_RDONLY | O_BINARY;
#else
int flags = O_RDONLY;
#endif
int fd;
if (!strcmp(from_file, "-")) {
from_file = "stdin";
fd = 0;
} else if ((fd = open(from_file, flags)) == -1) {
perror(from_file);
return -1;
}
odd_hex_digits = false;
if (!extract_edid(fd, error)) {
fprintf(error, "EDID extract of '%s' failed: ", from_file);
if (odd_hex_digits)
fprintf(error, "odd number of hexadecimal digits.\n");
else
fprintf(error, "unknown format.\n");
return -1;
}
if (state.edid_size % EDID_PAGE_SIZE) {
fprintf(error, "EDID length %u is not a multiple of %u.\n",
state.edid_size, EDID_PAGE_SIZE);
return -1;
}
state.num_blocks = state.edid_size / EDID_PAGE_SIZE;
if (fd != 0)
close(fd);
if (memcmp(edid, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", 8)) {
fprintf(error, "No EDID header found in '%s'.\n", from_file);
return -1;
}
return 0;
}
/* generic extension code */
std::string block_name(unsigned char block)
{
char buf[10];
switch (block) {
case 0x00: return "Base EDID";
case 0x02: return "CTA-861 Extension Block";
case 0x10: return "Video Timing Extension Block";
case 0x20: return "EDID 2.0 Extension Block";
case 0x40: return "Display Information Extension Block";
case 0x50: return "Localized String Extension Block";
case 0x60: return "Microdisplay Interface Extension Block";
case 0x70: return "DisplayID Extension Block";
case 0xf0: return "Block Map Extension Block";
case 0xff: return "Manufacturer-Specific Extension Block";
default:
sprintf(buf, " 0x%02x", block);
return std::string("Unknown EDID Extension Block") + buf;
}
}
void edid_state::parse_block_map(const unsigned char *x)
{
unsigned last_valid_block_tag = 0;
bool fail_once = false;
unsigned offset = 1;
unsigned i;
if (block_nr == 1)
block_map.saw_block_1 = true;
else if (!block_map.saw_block_1)
fail("No EDID Block Map Extension found in block 1.\n");
else if (block_nr == 128)
block_map.saw_block_128 = true;
if (block_nr > 1)
offset = 128;
for (i = 1; i < 127; i++) {
unsigned block = offset + i;
if (x[i]) {
last_valid_block_tag++;
if (i != last_valid_block_tag && !fail_once) {
fail("Valid block tags are not consecutive.\n");
fail_once = true;
}
printf(" Block %3u: %s\n", block, block_name(x[i]).c_str());
if (block >= num_blocks) {
if (!fail_once)
fail("Invalid block number %u.\n", block);
fail_once = true;
} else if (x[i] != edid[block * EDID_PAGE_SIZE]) {
fail("Block %u tag mismatch: expected 0x%02x, but got 0x%02x.\n",
block, edid[block * EDID_PAGE_SIZE], x[i]);
}
} else if (block < num_blocks) {
fail("Block %u tag mismatch: expected 0x%02x, but got 0x00.\n",
block, edid[block * EDID_PAGE_SIZE]);
}
}
}
void edid_state::preparse_extension(const unsigned char *x)
{
switch (x[0]) {
case 0x02:
has_cta = true;
preparse_cta_block(x);
break;
case 0x70:
has_dispid = true;
preparse_displayid_block(x);
break;
}
}
void edid_state::parse_extension(const unsigned char *x)
{
block = block_name(x[0]);
data_block.clear();
printf("\n");
if (block_nr && x[0] == 0)
block = "Unknown EDID Extension Block 0x00";
printf("Block %u, %s:\n", block_nr, block.c_str());
switch (x[0]) {
case 0x02:
parse_cta_block(x);
break;
case 0x10:
parse_vtb_ext_block(x);
break;
case 0x20:
fail("Deprecated extension block for EDID 2.0, do not use.\n");
break;
case 0x40:
parse_di_ext_block(x);
break;
case 0x50:
parse_ls_ext_block(x);
break;
case 0x70:
parse_displayid_block(x);
break;
case 0xf0:
parse_block_map(x);
if (block_nr != 1 && block_nr != 128)
fail("Must be used in block 1 and 128.\n");
break;
default:
hex_block(" ", x, EDID_PAGE_SIZE);
fail("Unknown Extension Block.\n");
break;
}
data_block.clear();
do_checksum("", x, EDID_PAGE_SIZE);
}
int edid_state::parse_edid()
{
hide_serial_numbers = options[OptHideSerialNumbers];
for (unsigned i = 1; i < num_blocks; i++)
preparse_extension(edid + i * EDID_PAGE_SIZE);
if (options[OptPhysicalAddress]) {
printf("%x.%x.%x.%x\n",
(cta.preparsed_phys_addr >> 12) & 0xf,
(cta.preparsed_phys_addr >> 8) & 0xf,
(cta.preparsed_phys_addr >> 4) & 0xf,
cta.preparsed_phys_addr & 0xf);
return 0;
}
if (!options[OptSkipHexDump]) {
printf("edid-decode (hex):\n\n");
for (unsigned i = 0; i < num_blocks; i++) {
hex_block("", edid + i * EDID_PAGE_SIZE, EDID_PAGE_SIZE, false);
printf("\n");
}
printf("----------------\n\n");
}
block = block_name(0x00);
printf("Block %u, %s:\n", block_nr, block.c_str());
parse_base_block(edid);
for (unsigned i = 1; i < num_blocks; i++) {
block_nr++;
printf("\n----------------\n");
parse_extension(edid + i * EDID_PAGE_SIZE);
}
block = "";
block_nr = EDID_MAX_BLOCKS;
if (has_cta)
cta_resolve_svrs();
if (options[OptPreferredTimings] && base.preferred_timing.is_valid()) {
printf("\n----------------\n");
printf("\nPreferred Video Timing if only Block 0 is parsed:\n");
print_timings(" ", base.preferred_timing, true);
}
if (options[OptNativeTimings] &&
base.preferred_timing.is_valid() && base.preferred_is_also_native) {
printf("\n----------------\n");
printf("\nNative Video Timing if only Block 0 is parsed:\n");
print_timings(" ", base.preferred_timing, true);
}
if (options[OptPreferredTimings] && !cta.preferred_timings.empty()) {
printf("\n----------------\n");
printf("\nPreferred Video Timing%s if Block 0 and CTA-861 Blocks are parsed:\n",
cta.preferred_timings.size() > 1 ? "s" : "");
for (vec_timings_ext::iterator iter = cta.preferred_timings.begin();
iter != cta.preferred_timings.end(); ++iter)
print_timings(" ", *iter, true);
}
if (options[OptNativeTimings] && !cta.native_timings.empty()) {
printf("\n----------------\n");
printf("\nNative Video Timing%s if Block 0 and CTA-861 Blocks are parsed:\n",
cta.native_timings.size() > 1 ? "s" : "");
for (vec_timings_ext::iterator iter = cta.native_timings.begin();
iter != cta.native_timings.end(); ++iter)
print_timings(" ", *iter, true);
}
if (options[OptPreferredTimings] && !dispid.preferred_timings.empty()) {
printf("\n----------------\n");
printf("\nPreferred Video Timing%s if Block 0 and DisplayID Blocks are parsed:\n",
dispid.preferred_timings.size() > 1 ? "s" : "");
for (vec_timings_ext::iterator iter = dispid.preferred_timings.begin();
iter != dispid.preferred_timings.end(); ++iter)
print_timings(" ", *iter, true);
}
if (!options[OptCheck] && !options[OptCheckInline])
return 0;
check_base_block();
if (has_cta)
check_cta_blocks();
if (has_dispid)
check_displayid_blocks();
printf("\n----------------\n");
if (!options[OptSkipSHA] && strlen(STRING(SHA))) {
printf("\nedid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
}
if (options[OptCheck]) {
if (warnings)
show_msgs(true);
if (failures)
show_msgs(false);
}
printf("\nEDID conformity: %s\n", failures ? "FAIL" : "PASS");
return failures ? -2 : 0;
}
int main(int argc, char **argv)
{
char short_options[26 * 2 * 2 + 1];
enum output_format out_fmt = OUT_FMT_DEFAULT;
int ret;
while (1) {
int option_index = 0;
unsigned idx = 0;
unsigned i;
for (i = 0; long_options[i].name; i++) {
if (!isalpha(long_options[i].val))
continue;
short_options[idx++] = long_options[i].val;
if (long_options[i].has_arg == required_argument)
short_options[idx++] = ':';
}
short_options[idx] = 0;
int ch = getopt_long(argc, argv, short_options,
long_options, &option_index);
if (ch == -1)
break;
options[ch] = 1;
switch (ch) {
case OptHelp:
usage();
return -1;
case OptOutputFormat:
if (!strcmp(optarg, "hex")) {
out_fmt = OUT_FMT_HEX;
} else if (!strcmp(optarg, "raw")) {
out_fmt = OUT_FMT_RAW;
} else if (!strcmp(optarg, "carray")) {
out_fmt = OUT_FMT_CARRAY;
} else if (!strcmp(optarg, "xml")) {
out_fmt = OUT_FMT_XML;
} else {
usage();
exit(1);
}
break;
case ':':
fprintf(stderr, "Option '%s' requires a value.\n",
argv[optind]);
usage();
return -1;
case '?':
fprintf(stderr, "Unknown argument '%s'.\n",
argv[optind]);
usage();
return -1;
}
}
if (optind == argc && options[OptVersion]) {
if (strlen(STRING(SHA)))
printf("edid-decode SHA: %s %s\n", STRING(SHA), STRING(DATE));
else
printf("edid-decode SHA: not available\n");
return 0;
}
if (optind == argc)
ret = edid_from_file("-", stdout);
else
ret = edid_from_file(argv[optind], argv[optind + 1] ? stderr : stdout);
if (ret && options[OptPhysicalAddress]) {
printf("f.f.f.f\n");
return 0;
}
if (optind < argc - 1)
return ret ? ret : edid_to_file(argv[optind + 1], out_fmt);
return ret ? ret : state.parse_edid();
}
#ifdef __EMSCRIPTEN__
/*
* The surrounding JavaScript implementation will call this function
* each time it wants to decode an EDID. So this should reset all the
* state and start over.
*/
extern "C" int parse_edid(const char *input)
{
for (unsigned i = 0; i < EDID_MAX_BLOCKS + 1; i++) {
s_msgs[i][0].clear();
s_msgs[i][1].clear();
}
options[OptCheck] = 1;
options[OptPreferredTimings] = 1;
options[OptNativeTimings] = 1;
state = edid_state();
int ret = edid_from_file(input, stderr);
return ret ? ret : state.parse_edid();
}
#endif
|