-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathediddecode.cpp
More file actions
executable file
·1073 lines (908 loc) · 27 KB
/
ediddecode.cpp
File metadata and controls
executable file
·1073 lines (908 loc) · 27 KB
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
// 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 "ediddecode.h"
#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static char *manufacturer_name(const unsigned char *x)
{
static char name[4];
name[0] = ((x[0] & 0x7c) >> 2) + '@';
name[1] = ((x[0] & 0x03) << 3) + ((x[1] & 0xe0) >> 5) + '@';
name[2] = (x[1] & 0x1f) + '@';
name[3] = 0;
if (!isupper(name[0]) || !isupper(name[1]) || !isupper(name[2]))
fail("Manufacturer name field contains garbage.\n");
return name;
}
/* extract a string from a detailed subblock, checking for termination */
char *extract_string(const unsigned char *x, unsigned len)
{
static char s[EDID_PAGE_SIZE];
bool seen_newline = false;
unsigned i;
memset(s, 0, sizeof(s));
for (i = 0; i < len; i++) {
if (seen_newline) {
if (x[i] != 0x20) {
fail("Non-space after newline.\n");
return s;
}
} else if (isgraph(x[i]) || x[i] == 0x20) {
s[i] = x[i];
} else if (x[i] == 0x0a) {
seen_newline = true;
if (!i)
fail("Empty string.\n");
else if (s[i - 1] == 0x20)
fail("One or more trailing spaces.\n");
} else {
fail("Non-printable character.\n");
return s;
}
}
/* Does the string end with a space? */
if (!seen_newline && s[len - 1] == 0x20)
fail("One or more trailing spaces.\n");
return s;
}
void edid_state::detailed_block(const unsigned char *x,QString &str)
{
static const unsigned char zero_descr[18] = { 0 };
unsigned cnt;
unsigned i;
base.detailed_block_cnt++;
if (x[0] || x[1]) {
// detailed_timings(" ", x);
return;
}
data_block = "Display Descriptor #" + std::to_string(base.detailed_block_cnt);
/* Monitor descriptor block, not detailed timing descriptor. */
if (x[2] != 0) {
/* 1.3, 3.10.3 */
fail("Monitor descriptor block has byte 2 nonzero (0x%02x).\n", x[2]);
}
if ((base.edid_minor < 4 || x[3] != 0xfd) && x[4] != 0x00) {
/* 1.3, 3.10.3 */
fail("Monitor descriptor block has byte 4 nonzero (0x%02x).\n", x[4]);
}
base.seen_non_detailed_descriptor = true;
if (base.edid_minor == 0)
fail("Has descriptor blocks other than detailed timings.\n");
if (!memcmp(x, zero_descr, sizeof(zero_descr))) {
// data_block = "Empty Descriptor";
return;
}
switch (x[3]) {
case 0x0e:
// detailed_epi(x);
return;
case 0x10:
return;
case 0xf7:
return;
case 0xf8:
// data_block = "CVT 3 Byte Timing Codes";
return;
case 0xf9:
// data_block = "Display Color Management Data";
return;
case 0xfa:
// data_block = "Standard Timing Identifications";
return;
case 0xfb: {
// data_block = "Color Point Data";
return;
}
case 0xfc:
data_block = "Display Product Name";
base.has_name_descriptor = 1;
printf(" %s: '%s'\n", data_block.c_str(), extract_string(x + 5, 13));
return;
case 0xfd:
// detailed_display_range_limits(x);
return;
case 0xfe:
if (!base.has_spwg || base.detailed_block_cnt < 3) {
data_block = "Alphanumeric Data String";
str = QString(extract_string(x + 5, 13));
printf(" %s: '%s'\n", data_block.c_str(), extract_string(x + 5, 13));
return;
}
// data_block = "SPWG Descriptor #3";
return;
case 0xff: {
// data_block = "Display Product Serial Number";
return;
}
default:
printf(" %s Display Descriptor (0x%02hhx):",
x[3] <= 0x0f ? "Manufacturer-Specified" : "Unknown", x[3]);
// hex_block(" ", x + 2, 16);
if (x[3] > 0x0f)
fail("Unknown Type 0x%02hhx.\n", x[3]);
return;
}
}
/*
* The sRGB chromaticities are (x, y):
* red: 0.640, 0.330
* green: 0.300, 0.600
* blue: 0.150, 0.060
* white: 0.3127, 0.3290
*/
static const unsigned char srgb_chromaticity[10] = {
0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54
};
void edid_state::parse_base_block(const unsigned char *x,QString &str)
{
time_t the_time;
struct tm *ptm;
int analog;
unsigned col_x, col_y;
bool has_preferred_timing = false;
data_block = "EDID Structure Version & Revision";
printf(" %s: %hhu.%hhu\n", data_block.c_str(), x[0x12], x[0x13]);
if (x[0x12] == 1) {
base.edid_minor = x[0x13];
if (base.edid_minor > 4)
warn("Unknown EDID minor version %u, assuming 1.4 conformance.\n", base.edid_minor);
if (base.edid_minor < 3)
warn("EDID 1.%u is deprecated, do not use.\n", base.edid_minor);
} else {
fail("Unknown EDID major version.\n");
}
base.serial_number = x[0x0c] + (x[0x0d] << 8) + (x[0x0e] << 16) + (x[0x0f] << 24);
data_block = "Vendor & Product Identification";
printf(" %s:\n", data_block.c_str());
printf(" Manufacturer: %s\n Model: %x\n", manufacturer_name(x + 0x08), (x[0x0a] + (x[0x0b] << 8)));
if (base.serial_number) {
unsigned sn = base.serial_number;
printf(" Serial Number: %u (0x%08x)\n", sn, sn);
// This is a list of known dummy values that are often used in EDIDs:
switch (sn) {
case 1:
case 0x01010101:
case 1010101:
case 0x5445:
case 0x80000000:
case 20000080:
case 8888:
case 6666:
warn("The serial number is one of the known dummy values, it should probably be set to 0.\n");
break;
}
}
time(&the_time);
ptm = localtime(&the_time);
base.week = x[0x10];
base.year = x[0x11];
unsigned char week = base.week;
int year = 1990 + base.year;
if (week) {
if (base.edid_minor <= 3 && week == 0xff)
fail("EDID 1.3 does not support week 0xff.\n");
// The max week is 53 in EDID 1.3 and 54 in EDID 1.4.
// No idea why there is a difference.
if (base.edid_minor <= 3 && week == 54)
fail("EDID 1.3 does not support week 54.\n");
if (week != 0xff && week > 54)
fail("Invalid week of manufacture (> 54).\n");
}
if (year - 1 > ptm->tm_year + 1900)
fail("The year is more than one year in the future.\n");
if (week == 0xff)
printf(" Model year: %d\n", year);
else if (replace_unique_ids)
printf(" Made in: 2000\n");
else if (week)
printf(" Made in: week %hhu of %d\n", week, year);
else
printf(" Made in: %d\n", year);
/* display section */
// data_block = "Basic Display Parameters & Features";
// printf(" %s:\n", data_block.c_str());
// if (x[0x14] & 0x80) {
// analog = 0;
// printf(" Digital display\n");
// if (base.edid_minor >= 4) {
// if ((x[0x14] & 0x70) == 0x00)
// printf(" Color depth is undefined\n");
// else if ((x[0x14] & 0x70) == 0x70)
// fail("Color Bit Depth set to reserved value.\n");
// else
// printf(" Bits per primary color channel: %u\n",
// ((x[0x14] & 0x70) >> 3) + 4);
// printf(" ");
// switch (x[0x14] & 0x0f) {
// case 0x00: printf("Digital interface is not defined\n"); break;
// case 0x01: printf("DVI interface\n"); break;
// case 0x02: printf("HDMI-a interface\n"); break;
// case 0x03: printf("HDMI-b interface\n"); break;
// case 0x04: printf("MDDI interface\n"); break;
// case 0x05: printf("DisplayPort interface\n"); break;
// default:
// printf("Unknown interface: 0x%02x\n", x[0x14] & 0x0f);
// fail("Digital Video Interface Standard set to reserved value 0x%02x.\n", x[0x14] & 0x0f);
// break;
// }
// } else if (base.edid_minor >= 2) {
// if (x[0x14] & 0x01) {
// printf(" DFP 1.x compatible TMDS\n");
// }
// if (x[0x14] & 0x7e)
// fail("Digital Video Interface Standard set to reserved value 0x%02x.\n", x[0x14] & 0x7e);
// } else if (x[0x14] & 0x7f) {
// fail("Digital Video Interface Standard set to reserved value 0x%02x.\n", x[0x14] & 0x7f);
// }
// } else {
// static const char * const voltages[] = {
// "0.700 : 0.300 : 1.000 V p-p",
// "0.714 : 0.286 : 1.000 V p-p",
// "1.000 : 0.400 : 1.400 V p-p",
// "0.700 : 0.000 : 0.700 V p-p"
// };
// unsigned voltage = (x[0x14] & 0x60) >> 5;
// unsigned sync = (x[0x14] & 0x0f);
// analog = 1;
// printf(" Analog display\n");
// printf(" Signal Level Standard: %s\n", voltages[voltage]);
// if (x[0x14] & 0x10)
// printf(" Blank-to-black setup/pedestal\n");
// else
// printf(" Blank level equals black level\n");
// if (sync)
// printf(" Sync:%s%s%s%s\n",
// sync & 0x08 ? " Separate" : "",
// sync & 0x04 ? " Composite" : "",
// sync & 0x02 ? " SyncOnGreen" : "",
// sync & 0x01 ? " Serration" : "");
// }
if (x[0x15] && x[0x16]) {
printf(" Maximum image size: %u cm x %u cm\n", x[0x15], x[0x16]);
base.max_display_width_mm = x[0x15] * 10;
base.max_display_height_mm = x[0x16] * 10;
image_width = base.max_display_width_mm * 10;
image_height = base.max_display_height_mm * 10;
if (x[0x15] < 10 || x[0x16] < 10)
warn("Dubious maximum image size (%ux%u is smaller than 10x10 cm).\n",
x[0x15], x[0x16]);
}
// else if (base.edid_minor >= 4 && (x[0x15] || x[0x16])) {
// if (x[0x15])
// printf(" Aspect ratio: %.2f (landscape)\n", (x[0x15] + 99) / 100.0);
// else
// printf(" Aspect ratio: %.2f (portrait)\n", 100.0 / (x[0x16] + 99));
// } else {
// /* Either or both can be zero for 1.3 and before */
// printf(" Image size is variable\n");
// }
// if (x[0x17] == 0xff)
// printf(" Gamma is defined in an extension block\n");
// else
// printf(" Gamma: %.2f\n", (x[0x17] + 100.0) / 100.0);
// if (x[0x18] & 0xe0) {
// printf(" DPMS levels:");
// if (x[0x18] & 0x80) printf(" Standby");
// if (x[0x18] & 0x40) printf(" Suspend");
// if (x[0x18] & 0x20) printf(" Off");
// printf("\n");
// }
// if (analog || base.edid_minor < 4) {
// printf(" ");
// switch (x[0x18] & 0x18) {
// case 0x00: printf("Monochrome or grayscale display\n"); break;
// case 0x08: printf("RGB color display\n"); break;
// case 0x10: printf("Non-RGB color display\n"); break;
// case 0x18: printf("Undefined display color type\n"); break;
// }
// } else {
// printf(" Supported color formats: RGB 4:4:4");
// if (x[0x18] & 0x08)
// printf(", YCrCb 4:4:4");
// if (x[0x18] & 0x10)
// printf(", YCrCb 4:2:2");
// printf("\n");
// }
// if (x[0x18] & 0x04) {
// printf(" Default (sRGB) color space is primary color space\n");
// if (memcmp(x + 0x19, srgb_chromaticity, sizeof(srgb_chromaticity)))
// fail("sRGB is signaled, but the chromaticities do not match.\n");
// if (x[0x17] != 120)
// warn("sRGB is signaled, but the gamma != 2.2.\n");
// base.uses_srgb = true;
// } else if (!memcmp(x + 0x19, srgb_chromaticity, sizeof(srgb_chromaticity))) {
// fail("The chromaticities match sRGB, but sRGB is not signaled.\n");
// base.uses_srgb = true;
// }
// if (base.edid_minor >= 4) {
// /* 1.4 always has a preferred timing and this bit means something else. */
// has_preferred_timing = true;
// base.preferred_is_also_native = x[0x18] & 0x02;
// printf(" First detailed timing %s the native pixel format and preferred refresh rate\n",
// base.preferred_is_also_native ? "includes" : "does not include");
// } else {
// if (x[0x18] & 0x02) {
// printf(" First detailed timing is the preferred timing\n");
// has_preferred_timing = true;
// // 1.3 recommends that the preferred timing corresponds to the
// // native timing, but it is not a requirement.
// // That said, we continue with the assumption that it actually
// // is the native timing.
// base.preferred_is_also_native = true;
// } else if (base.edid_minor == 3) {
// fail("EDID 1.3 requires that the first detailed timing is the preferred timing.\n");
// }
// }
if (x[0x18] & 0x01) {
if (base.edid_minor >= 4) {
base.supports_continuous_freq = true;
printf(" Display supports continuous frequencies\n");
} else {
printf(" Supports GTF timings within operating range\n");
base.supports_gtf = true;
}
}
// data_block = "Color Characteristics";
// printf(" %s:\n", data_block.c_str());
// col_x = (x[0x1b] << 2) | (x[0x19] >> 6);
// col_y = (x[0x1c] << 2) | ((x[0x19] >> 4) & 3);
// printf(" Red : 0.%04u, 0.%04u\n",
// (col_x * 10000) / 1024, (col_y * 10000) / 1024);
// col_x = (x[0x1d] << 2) | ((x[0x19] >> 2) & 3);
// col_y = (x[0x1e] << 2) | (x[0x19] & 3);
// printf(" Green: 0.%04u, 0.%04u\n",
// (col_x * 10000) / 1024, (col_y * 10000) / 1024);
// col_x = (x[0x1f] << 2) | (x[0x1a] >> 6);
// col_y = (x[0x20] << 2) | ((x[0x1a] >> 4) & 3);
// printf(" Blue : 0.%04u, 0.%04u\n",
// (col_x * 10000) / 1024, (col_y * 10000) / 1024);
// col_x = (x[0x21] << 2) | ((x[0x1a] >> 2) & 3);
// col_y = (x[0x22] << 2) | (x[0x1a] & 3);
// printf(" White: 0.%04u, 0.%04u\n",
// (col_x * 10000) / 1024, (col_y * 10000) / 1024);
// data_block = "Established Timings I & II";
// if (x[0x23] || x[0x24] || x[0x25]) {
// printf(" %s:\n", data_block.c_str());
// for (unsigned i = 0; i < ARRAY_SIZE(established_timings12); i++) {
// if (x[0x23 + i / 8] & (1 << (7 - i % 8))) {
// unsigned char dmt_id = established_timings12[i].dmt_id;
// const struct timings *t;
// char type[16];
// if (dmt_id) {
// sprintf(type, "DMT 0x%02x", dmt_id);
// // t = find_dmt_id(dmt_id);
// } else {
// t = &established_timings12[i].t;
// sprintf(type, "%-8s", established_timings12[i].type);
// }
// }
// }
// } else {
// printf(" %s: none\n", data_block.c_str());
// }
base.has_640x480p60_est_timing = x[0x23] & 0x20;
// data_block = "Standard Timings";
// bool found = false;
// for (unsigned i = 0; i < 8; i++) {
// if (x[0x26 + i * 2] != 0x01 || x[0x26 + i * 2 + 1] != 0x01) {
// found = true;
// break;
// }
// }
// if (found) {
// printf(" %s:\n", data_block.c_str());
// for (unsigned i = 0; i < 8; i++)
// print_standard_timing(" ", x[0x26 + i * 2], x[0x26 + i * 2 + 1]);
// } else {
// printf(" %s: none\n", data_block.c_str());
// }
// /* 18 byte descriptors */
// if (has_preferred_timing && !x[0x36] && !x[0x37])
// fail("Missing preferred timing.\n");
// /* Look for SPWG Noteboook Panel EDID data blocks */
// if ((x[0x36] || x[0x37]) &&
// (x[0x48] || x[0x49]) &&
// !x[0x5a] && !x[0x5b] && x[0x5d] == 0xfe &&
// !x[0x6c] && !x[0x6d] && x[0x6f] == 0xfe &&
// (x[0x79] == 1 || x[0x79] == 2) && x[0x7a] <= 1)
// base.has_spwg = true;
// for (unsigned i = 0; i < (base.has_spwg ? 2 : 4); i++)
// if (x[0x36 + i * 18] || x[0x37 + i * 18])
// cta.preparsed_total_dtds++;
data_block = "Detailed Timing Descriptors";
printf(" %s:\n", data_block.c_str());
detailed_block(x + 0x36,str);
detailed_block(x + 0x48,str);
detailed_block(x + 0x5a,str);
detailed_block(x + 0x6c,str);
// base.has_spwg = false;
// if (!base.preferred_is_also_native) {
// cta.native_timings.clear();
// base.preferred_timing = timings_ext();
// }
data_block = block;
// if (x[0x7e])
// printf(" Extension blocks: %u\n", x[0x7e]);
block = block_name(0x00);
data_block.clear();
// do_checksum("", x, EDID_PAGE_SIZE);
// if (base.edid_minor >= 3) {
// if (!base.has_name_descriptor)
// msg(base.edid_minor >= 4, "Missing Display Product Name.\n");
// if ((base.edid_minor == 3 || base.supports_continuous_freq) &&
// !base.has_display_range_descriptor)
// fail("Missing Display Range Limits Descriptor.\n");
// }
}
//-------------------------
#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,
};
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 replace_checksum(unsigned char *x, size_t len)
// {
// unsigned char sum = 0;
// unsigned i;
// for (i = 0; i < len - 1; i++)
// sum += x[i];
// x[len - 1] = -sum & 0xff;
// }
void do_checksum(const char *prefix, const unsigned char *x, size_t len, unsigned unused_bytes)
{
unsigned char check = x[len - 1];
unsigned char sum = 0;
unsigned i;
for (i = 0; i < len - 1; i++)
sum += x[i];
printf("%sChecksum: 0x%02hhx", prefix, check);
if ((unsigned char)(check + sum) != 0) {
printf(" (should be 0x%02x)", -sum & 0xff);
fail("Invalid checksum 0x%02x (should be 0x%02x).\n",
check, -sum & 0xff);
}
if (unused_bytes)
printf(" Unused space in Extension Block: %u byte%s",
unused_bytes, unused_bytes > 1 ? "s" : "");
printf("\n");
}
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;
if (t->hratio == 8 && t->vratio == 5) {
t->hratio = 16;
t->vratio = 10;
}
}
std::string edid_state::dtd_type(unsigned cnt)
{
unsigned len = std::to_string(cta.preparsed_total_dtds).length();
char buf[16];
sprintf(buf, "DTD %*u", len, cnt);
return buf;
}
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++;
}
std::string utohex(unsigned char x)
{
char buf[10];
sprintf(buf, "0x%02hhx", x);
return buf;
}
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;
}
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 *errorr)
{
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()) {
state.edid_size = 0;
return false;
}
// Ensure it is safely terminated by a 0 char
edid_data.push_back('\0');
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);
// Drop the extra '\0' byte since we now assume binary data
edid_data.pop_back();
/* 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 int edid_from_file(const char *from_file, FILE *error)
{
int flags = O_RDONLY;
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)) {
if (!state.edid_size) {
fprintf(error, "EDID of '%s' was empty.\n", from_file);
return -1;
}
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;
}
int edid_decode_alphanumeric_data_string(const char *from_file, QString &str)
{
int ret = edid_from_file(from_file, stdout);
if (ret) {
return ret;
}
return state.parse_edid(str);
}
/* 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;
}
}
int edid_state::parse_edid(QString &str)
{
block = block_name(0x00);
// printf("Block %u, %s:\n", block_nr, block.c_str());
parse_base_block(edid,str);
// 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 (cta.has_svrs)
// cta_resolve_svrs();
// if (options[OptPreferredTimings])
// print_preferred_timings();
// print_native_res();
// check_base_block(edid);
// if (has_cta)
// check_cta_blocks();