blob: 82e5427317f2d5701cf7d50350e83ca58c17195e [file] [log] [blame]
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "DisplayIdentification"
19
20#include <algorithm>
21#include <cctype>
22#include <numeric>
23#include <optional>
Ryan Prichard3b17f282024-02-08 02:31:50 -080024#include <span>
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070025
26#include <log/log.h>
27
Alec Mouriff793872022-01-13 17:45:06 -080028#include <ui/DisplayIdentification.h>
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070029
30namespace android {
31namespace {
32
Alec Mouriff793872022-01-13 17:45:06 -080033template <class T>
34inline T load(const void* p) {
35 static_assert(std::is_integral<T>::value, "T must be integral");
36
37 T r;
38 std::memcpy(&r, p, sizeof(r));
39 return r;
40}
41
42uint64_t rotateByAtLeast1(uint64_t val, uint8_t shift) {
43 return (val >> shift) | (val << (64 - shift));
44}
45
46uint64_t shiftMix(uint64_t val) {
47 return val ^ (val >> 47);
48}
49
Yi Kong93926f62024-02-20 00:39:46 +080050__attribute__((no_sanitize("unsigned-integer-overflow")))
Alec Mouriff793872022-01-13 17:45:06 -080051uint64_t hash64Len16(uint64_t u, uint64_t v) {
52 constexpr uint64_t kMul = 0x9ddfea08eb382d69;
53 uint64_t a = (u ^ v) * kMul;
54 a ^= (a >> 47);
55 uint64_t b = (v ^ a) * kMul;
56 b ^= (b >> 47);
57 b *= kMul;
58 return b;
59}
60
Yi Kong93926f62024-02-20 00:39:46 +080061__attribute__((no_sanitize("unsigned-integer-overflow")))
Alec Mouriff793872022-01-13 17:45:06 -080062uint64_t hash64Len0To16(const char* s, uint64_t len) {
63 constexpr uint64_t k2 = 0x9ae16a3b2f90404f;
64 constexpr uint64_t k3 = 0xc949d7c7509e6557;
65
66 if (len > 8) {
67 const uint64_t a = load<uint64_t>(s);
68 const uint64_t b = load<uint64_t>(s + len - 8);
69 return hash64Len16(a, rotateByAtLeast1(b + len, static_cast<uint8_t>(len))) ^ b;
70 }
71 if (len >= 4) {
72 const uint32_t a = load<uint32_t>(s);
73 const uint32_t b = load<uint32_t>(s + len - 4);
74 return hash64Len16(len + (a << 3), b);
75 }
76 if (len > 0) {
77 const unsigned char a = static_cast<unsigned char>(s[0]);
78 const unsigned char b = static_cast<unsigned char>(s[len >> 1]);
79 const unsigned char c = static_cast<unsigned char>(s[len - 1]);
80 const uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
81 const uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
82 return shiftMix(y * k2 ^ z * k3) * k2;
83 }
84 return k2;
85}
86
Ryan Prichard3b17f282024-02-08 02:31:50 -080087using byte_view = std::span<const uint8_t>;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070088
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010089constexpr size_t kEdidBlockSize = 128;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070090constexpr size_t kEdidHeaderLength = 5;
91
Dominik Laskowski075d3172018-05-24 15:50:06 -070092constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
93
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070094std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
Ryan Prichard3b17f282024-02-08 02:31:50 -080095 if (static_cast<size_t>(view.size()) < kEdidHeaderLength || view[0] || view[1] || view[2] ||
96 view[4]) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070097 return {};
98 }
99
100 return view[3];
101}
102
103std::string_view parseEdidText(const byte_view& view) {
104 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
105 text = text.substr(0, text.find('\n'));
106
107 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
108 ALOGW("Invalid EDID: ASCII text is not printable.");
109 return {};
110 }
111
112 return text;
113}
114
115// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
116template <size_t I>
117char getPnpLetter(uint16_t id) {
118 static_assert(I < 3);
119 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
120 return letter < 'A' || letter > 'Z' ? '\0' : letter;
121}
122
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200123DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
124 DeviceProductInfo info;
Marin Shalamanov359a7e72020-02-17 17:03:07 +0100125 info.name.assign(edid.displayName);
126 info.productId = std::to_string(edid.productId);
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200127 info.manufacturerPnpId = edid.pnpId;
128
129 constexpr uint8_t kModelYearFlag = 0xff;
130 constexpr uint32_t kYearOffset = 1990;
131
132 const auto year = edid.manufactureOrModelYear + kYearOffset;
133 if (edid.manufactureWeek == kModelYearFlag) {
134 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
135 } else if (edid.manufactureWeek == 0) {
136 DeviceProductInfo::ManufactureYear date;
137 date.year = year;
138 info.manufactureOrModelDate = date;
139 } else {
140 DeviceProductInfo::ManufactureWeekAndYear date;
141 date.year = year;
142 date.week = edid.manufactureWeek;
143 info.manufactureOrModelDate = date;
144 }
145
Marin Shalamanov896e6302020-04-06 16:11:25 +0200146 if (edid.cea861Block && edid.cea861Block->hdmiVendorDataBlock) {
147 const auto& address = edid.cea861Block->hdmiVendorDataBlock->physicalAddress;
148 info.relativeAddress = {address.a, address.b, address.c, address.d};
149 }
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200150 return info;
151}
152
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100153Cea861ExtensionBlock parseCea861Block(const byte_view& block) {
154 Cea861ExtensionBlock cea861Block;
155
156 constexpr size_t kRevisionNumberOffset = 1;
157 cea861Block.revisionNumber = block[kRevisionNumberOffset];
158
159 constexpr size_t kDetailedTimingDescriptorsOffset = 2;
160 const size_t dtdStart =
161 std::min(kEdidBlockSize, static_cast<size_t>(block[kDetailedTimingDescriptorsOffset]));
162
163 // Parse data blocks.
164 for (size_t dataBlockOffset = 4; dataBlockOffset < dtdStart;) {
165 const uint8_t header = block[dataBlockOffset];
166 const uint8_t tag = header >> 5;
167 const size_t bodyLength = header & 0b11111;
168 constexpr size_t kDataBlockHeaderSize = 1;
169 const size_t dataBlockSize = bodyLength + kDataBlockHeaderSize;
170
Ryan Prichard3b17f282024-02-08 02:31:50 -0800171 if (static_cast<size_t>(block.size()) < dataBlockOffset + dataBlockSize) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100172 ALOGW("Invalid EDID: CEA 861 data block is truncated.");
173 break;
174 }
175
176 const byte_view dataBlock(block.data() + dataBlockOffset, dataBlockSize);
177 constexpr uint8_t kVendorSpecificDataBlockTag = 0x3;
178
179 if (tag == kVendorSpecificDataBlockTag) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200180 const uint32_t ieeeRegistrationId = static_cast<uint32_t>(
181 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16));
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100182 constexpr uint32_t kHdmiIeeeRegistrationId = 0xc03;
183
184 if (ieeeRegistrationId == kHdmiIeeeRegistrationId) {
185 const uint8_t a = dataBlock[4] >> 4;
186 const uint8_t b = dataBlock[4] & 0b1111;
187 const uint8_t c = dataBlock[5] >> 4;
188 const uint8_t d = dataBlock[5] & 0b1111;
189 cea861Block.hdmiVendorDataBlock =
190 HdmiVendorDataBlock{.physicalAddress = HdmiPhysicalAddress{a, b, c, d}};
191 } else {
192 ALOGV("Ignoring vendor specific data block for vendor with IEEE OUI %x",
193 ieeeRegistrationId);
194 }
195 } else {
196 ALOGV("Ignoring CEA-861 data block with tag %x", tag);
197 }
198 dataBlockOffset += bodyLength + kDataBlockHeaderSize;
199 }
200
201 return cea861Block;
202}
203
Dominik Laskowski34157762018-10-31 13:07:19 -0700204} // namespace
205
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700206bool isEdid(const DisplayIdentificationData& data) {
207 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
208 return data.size() >= sizeof(kMagic) &&
209 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
210}
211
212std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100213 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700214 ALOGW("Invalid EDID: structure is truncated.");
215 // Attempt parsing even if EDID is malformed.
216 } else {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100217 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
218 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700219 "Invalid EDID: structure does not checksum.");
220 }
221
222 constexpr size_t kManufacturerOffset = 8;
223 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
224 ALOGE("Invalid EDID: manufacturer ID is truncated.");
225 return {};
226 }
227
228 // Plug and play ID encoded as big-endian 16-bit value.
229 const uint16_t manufacturerId =
Marin Shalamanova524a092020-07-27 21:39:55 +0200230 static_cast<uint16_t>((edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1]);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700231
232 const auto pnpId = getPnpId(manufacturerId);
233 if (!pnpId) {
234 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
235 return {};
236 }
237
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200238 constexpr size_t kProductIdOffset = 10;
239 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
240 ALOGE("Invalid EDID: product ID is truncated.");
241 return {};
242 }
Marin Shalamanova524a092020-07-27 21:39:55 +0200243 const uint16_t productId =
244 static_cast<uint16_t>(edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8));
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200245
246 constexpr size_t kManufactureWeekOffset = 16;
247 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
248 ALOGE("Invalid EDID: manufacture week is truncated.");
249 return {};
250 }
251 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
252 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
253 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
254
255 constexpr size_t kManufactureYearOffset = 17;
256 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
257 ALOGE("Invalid EDID: manufacture year is truncated.");
258 return {};
259 }
260 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
261 ALOGW_IF(manufactureOrModelYear <= 0xf,
262 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
263
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700264 constexpr size_t kDescriptorOffset = 54;
265 if (edid.size() < kDescriptorOffset) {
266 ALOGE("Invalid EDID: descriptors are missing.");
267 return {};
268 }
269
270 byte_view view(edid.data(), edid.size());
Ryan Prichard3b17f282024-02-08 02:31:50 -0800271 view = view.subspan(kDescriptorOffset);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700272
273 std::string_view displayName;
274 std::string_view serialNumber;
275 std::string_view asciiText;
276
277 constexpr size_t kDescriptorCount = 4;
278 constexpr size_t kDescriptorLength = 18;
279
280 for (size_t i = 0; i < kDescriptorCount; i++) {
Ryan Prichard3b17f282024-02-08 02:31:50 -0800281 if (static_cast<size_t>(view.size()) < kDescriptorLength) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700282 break;
283 }
284
285 if (const auto type = getEdidDescriptorType(view)) {
286 byte_view descriptor(view.data(), kDescriptorLength);
Ryan Prichard3b17f282024-02-08 02:31:50 -0800287 descriptor = descriptor.subspan(kEdidHeaderLength);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700288
289 switch (*type) {
290 case 0xfc:
291 displayName = parseEdidText(descriptor);
292 break;
293 case 0xfe:
294 asciiText = parseEdidText(descriptor);
295 break;
296 case 0xff:
297 serialNumber = parseEdidText(descriptor);
298 break;
299 }
300 }
301
Ryan Prichard3b17f282024-02-08 02:31:50 -0800302 view = view.subspan(kDescriptorLength);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700303 }
304
Dominik Laskowski17337962020-03-02 15:51:15 -0800305 std::string_view modelString = displayName;
306
307 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700308 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800309 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700310 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800311 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700312 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800313 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700314 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800315 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700316 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
317 return {};
318 }
319
Dominik Laskowski17337962020-03-02 15:51:15 -0800320 // Hash model string instead of using product code or (integer) serial number, since the latter
Jason Macnak4afe8572021-07-16 13:57:41 -0700321 // have been observed to change on some displays with multiple inputs. Use a stable hash instead
322 // of std::hash which is only required to be same within a single execution of a program.
323 const uint32_t modelHash = static_cast<uint32_t>(cityHash64Len0To16(modelString));
Dominik Laskowski17337962020-03-02 15:51:15 -0800324
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100325 // Parse extension blocks.
326 std::optional<Cea861ExtensionBlock> cea861Block;
327 if (edid.size() < kEdidBlockSize) {
328 ALOGW("Invalid EDID: block 0 is truncated.");
329 } else {
330 constexpr size_t kNumExtensionsOffset = 126;
331 const size_t numExtensions = edid[kNumExtensionsOffset];
332 view = byte_view(edid.data(), edid.size());
333 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
Ryan Prichard3b17f282024-02-08 02:31:50 -0800334 view = view.subspan(kEdidBlockSize);
335 if (static_cast<size_t>(view.size()) < kEdidBlockSize) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100336 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
337 break;
338 }
339
340 const byte_view block(view.data(), kEdidBlockSize);
341 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
342 "Invalid EDID: block %zu does not checksum.", blockNumber);
343 const uint8_t tag = block[0];
344
345 constexpr uint8_t kCea861BlockTag = 0x2;
346 if (tag == kCea861BlockTag) {
347 cea861Block = parseCea861Block(block);
348 } else {
349 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
350 }
351 }
352 }
353
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200354 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200355 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800356 .pnpId = *pnpId,
357 .modelHash = modelHash,
358 .displayName = displayName,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100359 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200360 .manufactureWeek = manufactureWeek,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100361 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700362}
363
364std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
365 const char a = getPnpLetter<0>(manufacturerId);
366 const char b = getPnpLetter<1>(manufacturerId);
367 const char c = getPnpLetter<2>(manufacturerId);
368 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
369}
370
Marin Shalamanova524a092020-07-27 21:39:55 +0200371std::optional<PnpId> getPnpId(PhysicalDisplayId displayId) {
372 return getPnpId(displayId.getManufacturerId());
Dominik Laskowski34157762018-10-31 13:07:19 -0700373}
374
Dominik Laskowski075d3172018-05-24 15:50:06 -0700375std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
376 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700377 if (!isEdid(data)) {
378 ALOGE("Display identification data has unknown format.");
379 return {};
380 }
381
382 const auto edid = parseEdid(data);
383 if (!edid) {
384 return {};
385 }
386
Marin Shalamanova524a092020-07-27 21:39:55 +0200387 const auto displayId = PhysicalDisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
Dominik Laskowski17337962020-03-02 15:51:15 -0800388 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200389 .name = std::string(edid->displayName),
390 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700391}
392
Marin Shalamanova524a092020-07-27 21:39:55 +0200393PhysicalDisplayId getVirtualDisplayId(uint32_t id) {
394 return PhysicalDisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700395}
396
Alec Mouriff793872022-01-13 17:45:06 -0800397uint64_t cityHash64Len0To16(std::string_view sv) {
398 auto len = sv.length();
399 if (len > 16) {
400 ALOGE("%s called with length %zu. Only hashing the first 16 chars", __FUNCTION__, len);
401 len = 16;
402 }
403 return hash64Len0To16(sv.data(), len);
404}
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800405
Yi Kong93926f62024-02-20 00:39:46 +0800406} // namespace android