blob: 1bf0580ef0908f0f72ded53f1d4658666663eea0 [file] [log] [blame]
Marat Dukhan006461a2017-08-24 16:10:46 -07001#include <stdbool.h>
2#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5
6#include <arm/android/api.h>
7#include <log.h>
8
9
10#define CPUINFO_COUNT_OF(x) (sizeof(x) / sizeof(0[x]))
11
12
13static inline bool is_ascii_whitespace(char c) {
14 switch (c) {
15 case ' ':
16 case '\t':
17 case '\r':
18 case '\n':
19 return true;
20 default:
21 return false;
22 }
23}
24
25static inline bool is_ascii_alphabetic(char c) {
26 const char lower_c = c | '\x20';
27 return (uint8_t) (lower_c - 'a') <= (uint8_t) ('z' - 'a');
28}
29
30static inline bool is_ascii_alphabetic_uppercase(char c) {
31 return (uint8_t) (c - 'A') <= (uint8_t) ('Z' - 'A');
32}
33
34static inline bool is_ascii_numeric(char c) {
35 return (uint8_t) (c - '0') < 10;
36}
37
Marat Dukhan006461a2017-08-24 16:10:46 -070038static inline uint16_t load_u16le(const void* ptr) {
39#if defined(__ARM_ARCH_7A__) || defined(__aarch64__)
40 return *((const uint16_t*) ptr);
41#else
42 const uint8_t* byte_ptr = (const uint8_t*) ptr;
43 return ((uint16_t) byte_ptr[1] << 8) | (uint16_t) byte_ptr[0];
44#endif
45}
46
47static inline uint32_t load_u24le(const void* ptr) {
48#if defined(__ARM_ARCH_7A__) || defined(__aarch64__)
49 return ((uint32_t) ((const uint8_t*) ptr)[2] << 16) | ((uint32_t) *((const uint16_t*) ptr));
50#else
51 const uint8_t* byte_ptr = (const uint8_t*) ptr;
52 return ((uint32_t) byte_ptr[2] << 16) | ((uint32_t) byte_ptr[1] << 8) | (uint32_t) byte_ptr[0];
53#endif
54}
55
56static inline uint32_t load_u32le(const void* ptr) {
57#if defined(__ARM_ARCH_7A__) || defined(__aarch64__)
58 return *((const uint32_t*) ptr);
59#else
60 return ((uint32_t) ((const uint8_t*) ptr)[3] << 24) | load_u24le(ptr);
61#endif
62}
63
64/*
65 * Map from ARM chipset series ID to ARM chipset vendor ID.
66 * This map is used to avoid storing vendor IDs in tables.
67 */
68static enum cpuinfo_arm_chipset_vendor chipset_series_vendor[cpuinfo_arm_chipset_series_max] = {
69 [cpuinfo_arm_chipset_series_unknown] = cpuinfo_arm_chipset_vendor_unknown,
70 [cpuinfo_arm_chipset_series_qualcomm_qsd] = cpuinfo_arm_chipset_vendor_qualcomm,
71 [cpuinfo_arm_chipset_series_qualcomm_msm] = cpuinfo_arm_chipset_vendor_qualcomm,
72 [cpuinfo_arm_chipset_series_qualcomm_apq] = cpuinfo_arm_chipset_vendor_qualcomm,
73 [cpuinfo_arm_chipset_series_qualcomm_snapdragon] = cpuinfo_arm_chipset_vendor_qualcomm,
74 [cpuinfo_arm_chipset_series_mediatek_mt] = cpuinfo_arm_chipset_vendor_mediatek,
75 [cpuinfo_arm_chipset_series_samsung_exynos] = cpuinfo_arm_chipset_vendor_samsung,
76 [cpuinfo_arm_chipset_series_hisilicon_k3v] = cpuinfo_arm_chipset_vendor_hisilicon,
77 [cpuinfo_arm_chipset_series_hisilicon_hi] = cpuinfo_arm_chipset_vendor_hisilicon,
78 [cpuinfo_arm_chipset_series_hisilicon_kirin] = cpuinfo_arm_chipset_vendor_hisilicon,
79 [cpuinfo_arm_chipset_series_actions_atm] = cpuinfo_arm_chipset_vendor_actions,
80 [cpuinfo_arm_chipset_series_allwinner_a] = cpuinfo_arm_chipset_vendor_allwinner,
81 [cpuinfo_arm_chipset_series_amlogic_aml] = cpuinfo_arm_chipset_vendor_amlogic,
82 [cpuinfo_arm_chipset_series_amlogic_s] = cpuinfo_arm_chipset_vendor_amlogic,
83 [cpuinfo_arm_chipset_series_broadcom_bcm] = cpuinfo_arm_chipset_vendor_broadcom,
84 [cpuinfo_arm_chipset_series_lg_nuclun] = cpuinfo_arm_chipset_vendor_lg,
85 [cpuinfo_arm_chipset_series_leadcore_lc] = cpuinfo_arm_chipset_vendor_leadcore,
86 [cpuinfo_arm_chipset_series_marvell_pxa] = cpuinfo_arm_chipset_vendor_marvell,
87 [cpuinfo_arm_chipset_series_mstar_6a] = cpuinfo_arm_chipset_vendor_mstar,
88 [cpuinfo_arm_chipset_series_novathor_u] = cpuinfo_arm_chipset_vendor_novathor,
89 [cpuinfo_arm_chipset_series_nvidia_tegra_t] = cpuinfo_arm_chipset_vendor_nvidia,
90 [cpuinfo_arm_chipset_series_nvidia_tegra_ap] = cpuinfo_arm_chipset_vendor_nvidia,
91 [cpuinfo_arm_chipset_series_nvidia_tegra_sl] = cpuinfo_arm_chipset_vendor_nvidia,
92 [cpuinfo_arm_chipset_series_pinecone_surge_s] = cpuinfo_arm_chipset_vendor_pinecone,
93 [cpuinfo_arm_chipset_series_renesas_mp] = cpuinfo_arm_chipset_vendor_renesas,
94 [cpuinfo_arm_chipset_series_rockchip_rk] = cpuinfo_arm_chipset_vendor_rockchip,
95 [cpuinfo_arm_chipset_series_spreadtrum_sc] = cpuinfo_arm_chipset_vendor_spreadtrum,
96 [cpuinfo_arm_chipset_series_telechips_tcc] = cpuinfo_arm_chipset_vendor_telechips,
97 [cpuinfo_arm_chipset_series_texas_instruments_omap] = cpuinfo_arm_chipset_vendor_texas_instruments,
98 [cpuinfo_arm_chipset_series_wondermedia_wm] = cpuinfo_arm_chipset_vendor_wondermedia,
99};
100
101/**
102 * Tries to match /(MSM|APQ)\d{4}([A-Z\-]*)/ signature (case-insensitive) for Qualcomm MSM and APQ chipsets.
103 * If match successful, extracts model information into \p chipset argument.
104 *
105 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform
106 * or ro.chipname) to match.
107 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform or
108 * ro.chipname) to match.
109 * @param[out] chipset - location where chipset information will be stored upon a successful match.
110 *
111 * @returns true if signature matched, false otherwise.
112 */
113static bool match_msm_apq(
114 const char* start, const char* end,
115 struct cpuinfo_arm_chipset chipset[restrict static 1])
116{
117 /* Expect at least 7 symbols: 3 symbols "MSM" or "APQ" + 4 digits */
118 if (start + 7 > end) {
119 return false;
120 }
121
122 /* Check that string starts with "MSM" or "APQ", case-insensitive.
123 * The first three characters are loaded as 24-bit little endian word, binary ORed with 0x20 to convert to lower
124 * case, and compared to "MSM" and "APQ" strings as integers.
125 */
126 const uint32_t series_signature = UINT32_C(0x00202020) | load_u24le(start);
127 enum cpuinfo_arm_chipset_series series;
128 switch (series_signature) {
129 case UINT32_C(0x6D736D): /* "msm" = reverse("msm") */
130 series = cpuinfo_arm_chipset_series_qualcomm_msm;
131 break;
132 case UINT32_C(0x717061): /* "qpa" = reverse("apq") */
133 series = cpuinfo_arm_chipset_series_qualcomm_apq;
134 break;
135 default:
136 return false;
137 }
138
139 /* Sometimes there is a space ' ' following the MSM/APQ series */
140 const char* pos = start + 3;
141 if (*pos == ' ') {
142 pos++;
143
144 /* Expect at least 4 more symbols (4-digit model number) */
145 if (pos + 4 > end) {
146 return false;
147 }
148 }
149
150 /* Validate and parse 4-digit model number */
151 uint32_t model = 0;
152 for (uint32_t i = 0; i < 4; i++) {
153 const uint32_t digit = (uint32_t) (uint8_t) (*pos++) - '0';
154 if (digit >= 10) {
155 /* Not really a digit */
156 return false;
157 }
158 model = model * 10 + digit;
159 }
160
161 /* Suffix is optional, so if we got to this point, parsing is successful. Commit parsed chipset. */
162 *chipset = (struct cpuinfo_arm_chipset) {
163 .vendor = cpuinfo_arm_chipset_vendor_qualcomm,
164 .series = series,
165 .model = model,
166 };
167
168 /* Parse as many suffix characters as match the pattern [A-Za-z\-] */
169 for (uint32_t i = 0; i < CPUINFO_ARM_CHIPSET_SUFFIX_MAX; i++) {
170 if (pos + i == end) {
171 break;
172 }
173
174 const char c = pos[i];
175 if (is_ascii_alphabetic(c)) {
176 /* Matched a letter [A-Za-z] */
177 chipset->suffix[i] = c & '\xDF';
178 } else if (c == '-') {
179 /* Matched a dash '-' */
180 chipset->suffix[i] = c;
181 } else {
182 /* Neither of [A-Za-z\-] */
183 break;
184 }
185 }
186 return true;
187}
188
189/**
190 * Tries to match /SDM\d{3}$/ signature for Qualcomm Snapdragon chipsets.
191 * If match successful, extracts model information into \p chipset argument.
192 *
193 * @param start - start of the /proc/cpuinfo Hardware string to match.
194 * @param end - end of the /proc/cpuinfo Hardware string to match.
195 * @param[out] chipset - location where chipset information will be stored upon a successful match.
196 *
197 * @returns true if signature matched, false otherwise.
198 */
199static bool match_sdm(
200 const char* start, const char* end,
201 struct cpuinfo_arm_chipset chipset[restrict static 1])
202{
203 /* Expect exactly 6 symbols: 3 symbols "SDM" + 3 digits */
204 if (start + 6 != end) {
205 return false;
206 }
207
208 /* Check that string starts with "SDM".
209 * The first three characters are loaded and compared as 24-bit little endian word.
210 */
211 const uint32_t expected_sdm = load_u24le(start);
212 if (expected_sdm != UINT32_C(0x004D4453) /* "MDS" = reverse("SDM") */) {
213 return false;
214 }
215
216 /* Validate and parse 3-digit model number */
217 uint32_t model = 0;
218 for (uint32_t i = 3; i < 6; i++) {
219 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
220 if (digit >= 10) {
221 /* Not really a digit */
222 return false;
223 }
224 model = model * 10 + digit;
225 }
226
227 /* Return parsed chipset. */
228 *chipset = (struct cpuinfo_arm_chipset) {
229 .vendor = cpuinfo_arm_chipset_vendor_qualcomm,
230 .series = cpuinfo_arm_chipset_series_qualcomm_snapdragon,
231 .model = model,
232 };
233 return true;
234}
235
236/**
237 * Tries to match /Samsung Exynos\d{4}$/ signature (case-insensitive) for Samsung Exynos chipsets.
238 * If match successful, extracts model information into \p chipset argument.
239 *
240 * @param start - start of the /proc/cpuinfo Hardware string to match.
241 * @param end - end of the /proc/cpuinfo Hardware string to match.
242 * @param[out] chipset - location where chipset information will be stored upon a successful match.
243 *
244 * @returns true if signature matched, false otherwise.
245 */
246static bool match_samsung_exynos(
247 const char* start, const char* end,
248 struct cpuinfo_arm_chipset chipset[restrict static 1])
249{
250 /*
251 * Expect at 18-19 symbols:
252 * - "Samsung" (7 symbols) + space + "Exynos" (6 symbols) + optional space 4-digit model number
253 */
254 const size_t length = end - start;
255 switch (length) {
256 case 18:
257 case 19:
258 break;
259 default:
260 return false;
261 }
262
263 /*
264 * Check that the string starts with "samsung exynos", case-insensitive.
265 * Blocks of 4 characters are loaded and compared as little-endian 32-bit word.
266 * Case-insensitive characters are binary ORed with 0x20 to convert them to lowercase.
267 */
268 const uint32_t expected_sams = UINT32_C(0x20202000) | load_u32le(start);
269 if (expected_sams != UINT32_C(0x736D6153) /* "smaS" = reverse("Sams") */) {
270 return false;
271 }
272 const uint32_t expected_ung = UINT32_C(0x00202020) | load_u32le(start + 4);
273 if (expected_ung != UINT32_C(0x20676E75) /* " ung" = reverse("ung ") */) {
274 return false;
275 }
276 const uint32_t expected_exyn = UINT32_C(0x20202000) | load_u32le(start + 8);
277 if (expected_exyn != UINT32_C(0x6E797845) /* "nyxE" = reverse("Exyn") */) {
278 return false;
279 }
280 const uint16_t expected_os = UINT16_C(0x2020) | load_u16le(start + 12);
281 if (expected_os != UINT16_C(0x736F) /* "so" = reverse("os") */) {
282 return false;
283 }
284
285 const char* pos = start + 14;
286
287 /* There can be a space ' ' following the "Exynos" string */
288 if (*pos == ' ') {
289 pos++;
290
291 /* If optional space if present, we expect exactly 19 characters */
292 if (length != 19) {
293 return false;
294 }
295 }
296
297 /* Validate and parse 4-digit model number */
298 uint32_t model = 0;
299 for (uint32_t i = 0; i < 4; i++) {
300 const uint32_t digit = (uint32_t) (uint8_t) (*pos++) - '0';
301 if (digit >= 10) {
302 /* Not really a digit */
303 return start;
304 }
305 model = model * 10 + digit;
306 }
307
308 /* Return parsed chipset */
309 *chipset = (struct cpuinfo_arm_chipset) {
310 .vendor = cpuinfo_arm_chipset_vendor_samsung,
311 .series = cpuinfo_arm_chipset_series_samsung_exynos,
312 .model = model,
313 };
314 return pos;
315}
316
317/**
318 * Tries to match /exynos\d{4}$/ signature for Samsung Exynos chipsets.
319 * If match successful, extracts model information into \p chipset argument.
320 *
321 * @param start - start of the platform identifier (ro.board.platform or ro.chipname) to match.
322 * @param end - end of the platform identifier (ro.board.platform or ro.chipname) to match.
323 * @param[out] chipset - location where chipset information will be stored upon a successful match.
324 *
325 * @returns true if signature matched, false otherwise.
326 */
327static bool match_exynos(
328 const char* start, const char* end,
329 struct cpuinfo_arm_chipset chipset[restrict static 1])
330{
331 /* Expect exactly 10 symbols: "exynos" (6 symbols) + 4-digit model number */
332 if (start + 10 != end) {
333 return false;
334 }
335
336 /* Load first 4 bytes as little endian 32-bit word */
337 const uint32_t expected_exyn = load_u32le(start);
338 if (expected_exyn != UINT32_C(0x6E797865) /* "nyxe" = reverse("exyn") */ ) {
339 return false;
340 }
341
342 /* Load next 2 bytes as little endian 16-bit word */
343 const uint16_t expected_os = load_u16le(start + 4);
344 if (expected_os != UINT16_C(0x736F) /* "so" = reverse("os") */ ) {
345 return false;
346 }
347
348 /* Check and parse 4-digit model number */
349 uint32_t model = 0;
350 for (uint32_t i = 6; i < 10; i++) {
351 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
352 if (digit >= 10) {
353 /* Not really a digit */
354 return false;
355 }
356 model = model * 10 + digit;
357 }
358
359 /* Return parsed chipset. */
360 *chipset = (struct cpuinfo_arm_chipset) {
361 .vendor = cpuinfo_arm_chipset_vendor_samsung,
362 .series = cpuinfo_arm_chipset_series_samsung_exynos,
363 .model = model,
364 };
365 return true;
366}
367
368/**
369 * Tries to match /universal\d{4}$/ signature for Samsung Exynos chipsets.
370 * If match successful, extracts model information into \p chipset argument.
371 *
372 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board or ro.chipname)
373 * to match.
374 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board or ro.chipname)
375 * to match.
376 * @param[out] chipset - location where chipset information will be stored upon a successful match.
377 *
378 * @returns true if signature matched, false otherwise.
379 */
380static bool match_universal(
381 const char* start, const char* end,
382 struct cpuinfo_arm_chipset chipset[restrict static 1])
383{
384 /* Expect exactly 13 symbols: "universal" (9 symbols) + 4-digit model number */
385 if (start + 13 != end) {
386 return false;
387 }
388
389 /*
390 * Check that the string starts with "universal".
391 * Blocks of 4 characters are loaded and compared as little-endian 32-bit word.
392 * Case-insensitive characters are binary ORed with 0x20 to convert them to lowercase.
393 */
394 const uint8_t expected_u = UINT8_C(0x20) | (uint8_t) start[0];
395 if (expected_u != UINT8_C(0x75) /* "u" */) {
396 return false;
397 }
398 const uint32_t expected_nive = UINT32_C(0x20202020) | load_u32le(start + 1);
399 if (expected_nive != UINT32_C(0x6576696E) /* "evin" = reverse("nive") */ ) {
400 return false;
401 }
402 const uint32_t expected_ersa = UINT32_C(0x20202020) | load_u32le(start + 5);
403 if (expected_ersa != UINT32_C(0x6C617372) /* "lasr" = reverse("rsal") */) {
404 return false;
405 }
406
407 /* Validate and parse 4-digit model number */
408 uint32_t model = 0;
409 for (uint32_t i = 9; i < 13; i++) {
410 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
411 if (digit >= 10) {
412 /* Not really a digit */
413 return false;
414 }
415 model = model * 10 + digit;
416 }
417
418 /* Return parsed chipset. */
419 *chipset = (struct cpuinfo_arm_chipset) {
420 .vendor = cpuinfo_arm_chipset_vendor_samsung,
421 .series = cpuinfo_arm_chipset_series_samsung_exynos,
422 .model = model,
423 };
424 return true;
425}
426
427/**
428 * Compares, case insensitively, a string to known values "SMDK4210" and "SMDK4x12" for Samsung Exynos chipsets.
429 * If platform identifier matches one of the SMDK* values, extracts model information into \p chipset argument.
430 * For "SMDK4x12" match, decodes the chipset name using number of cores.
431 *
432 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string or ro.product.board) to match.
433 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string or ro.product.board) to match.
434 * @param cores - number of cores in the chipset.
435 * @param[out] chipset - location where chipset information will be stored upon a successful match.
436 *
437 * @returns true if signature matched, false otherwise.
438 */
439static bool match_and_parse_smdk(
440 const char* start, const char* end, uint32_t cores,
441 struct cpuinfo_arm_chipset chipset[restrict static 1])
442{
443 /* Expect exactly 8 symbols: "SMDK" (4 symbols) + 4-digit model number */
444 if (start + 8 != end) {
445 return false;
446 }
447
448 /*
449 * Check that string starts with "MT" (case-insensitive).
450 * The first four characters are loaded as a 32-bit little endian word and converted to lowercase.
451 */
452 const uint32_t expected_smdk = UINT32_C(0x20202020) | load_u32le(start);
453 if (expected_smdk != UINT32_C(0x6B646D73) /* "kdms" = reverse("smdk") */) {
454 return false;
455 }
456
457 /*
458 * Check that string ends with "4210" or "4x12".
459 * The last four characters are loaded and compared as a 32-bit little endian word.
460 */
461 uint32_t model = 0;
462 const uint32_t expected_model = load_u32le(start + 4);
463 switch (expected_model) {
464 case UINT32_C(0x30313234): /* "0124" = reverse("4210") */
465 model = 4210;
466 break;
467 case UINT32_C(0x32317834): /* "21x4" = reverse("4x12") */
468 switch (cores) {
469 case 2:
470 model = 4212;
471 break;
472 case 4:
473 model = 4412;
474 break;
475 default:
476 cpuinfo_log_warning("system reported invalid %"PRIu32"-core Exynos 4x12 chipset", cores);
477 }
478 }
479
480 if (model == 0) {
481 return false;
482 }
483
484 *chipset = (struct cpuinfo_arm_chipset) {
485 .vendor = cpuinfo_arm_chipset_vendor_samsung,
486 .series = cpuinfo_arm_chipset_series_samsung_exynos,
487 .model = model,
488 };
489 return true;
490}
491
492/**
493 * Tries to match /MTK?\d{4}[A-Z]*$/ signature for MediaTek MT chipsets.
494 * If match successful, extracts model information into \p chipset argument.
495 *
496 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform,
497 * ro.mediatek.platform, or ro.chipname) to match.
498 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board, ro.board.platform,
499 * ro.mediatek.platform, or ro.chipname) to match.
500 * @param match_end - indicates if the function should attempt to match through the end of the string and fail if there
501 * are unparsed characters in the end, or match only MTK signature, model number, and some of the
502 * suffix characters (the ones that pass validation).
503 * @param[out] chipset - location where chipset information will be stored upon a successful match.
504 *
505 * @returns true if signature matched, false otherwise.
506 */
507static bool match_mt(
508 const char* start, const char* end, bool match_end,
509 struct cpuinfo_arm_chipset chipset[restrict static 1])
510{
511 /* Expect at least 6 symbols: "MT" (2 symbols) + 4-digit model number */
512 if (start + 6 > end) {
513 return false;
514 }
515
516 /*
517 * Check that string starts with "MT" (case-insensitive).
518 * The first two characters are loaded as 16-bit little endian word and converted to lowercase.
519 */
520 const uint16_t mt = UINT16_C(0x2020) | load_u16le(start);
521 if (mt != UINT16_C(0x746D) /* "tm" */) {
522 return false;
523 }
524
525
526 /* Some images report "MTK" rather than "MT" */
527 const char* pos = start + 2;
528 if (((uint8_t) *pos | UINT8_C(0x20)) == (uint8_t) 'k') {
529 pos++;
530
531 /* Expect 4 more symbols after "MTK" (4-digit model number) */
532 if (pos + 4 > end) {
533 return false;
534 }
535 }
536
537 /* Validate and parse 4-digit model number */
538 uint32_t model = 0;
539 for (uint32_t i = 0; i < 4; i++) {
540 const uint32_t digit = (uint32_t) (uint8_t) (*pos++) - '0';
541 if (digit >= 10) {
542 /* Not really a digit */
543 return false;
544 }
545 model = model * 10 + digit;
546 }
547
548 /* Record parsed chipset. This implicitly zeroes-out suffix, which will be parsed later. */
549 *chipset = (struct cpuinfo_arm_chipset) {
550 .vendor = cpuinfo_arm_chipset_vendor_mediatek,
551 .series = cpuinfo_arm_chipset_series_mediatek_mt,
552 .model = model,
553 };
554
555 if (match_end) {
556 /* Check that the potential suffix does not exceed maximum length */
557 const size_t suffix_length = end - pos;
558 if (suffix_length > CPUINFO_ARM_CHIPSET_SUFFIX_MAX) {
559 return false;
560 }
561
562 /* Validate suffix characters and copy them to chipset structure */
563 for (size_t i = 0; i < suffix_length; i++) {
564 const char c = (*pos++);
565 if (is_ascii_alphabetic(c)) {
566 /* Matched a letter [A-Za-z], convert to uppercase */
567 chipset->suffix[i] = c & '\xDF';
568 } else if (c == '/') {
569 /* Matched a slash '/' */
570 chipset->suffix[i] = c;
571 } else {
572 /* Invalid suffix character (neither of [A-Za-z/]) */
573 return false;
574 }
575 }
576 } else {
577 /* Validate and parse as many suffix characters as we can */
578 for (size_t i = 0; i < CPUINFO_ARM_CHIPSET_SUFFIX_MAX; i++) {
579 if (pos + i == end) {
580 break;
581 }
582
583 const char c = pos[i];
584 if (is_ascii_alphabetic(c)) {
585 /* Matched a letter [A-Za-z], convert to uppercase */
586 chipset->suffix[i] = c & '\xDF';
587 } else if (c == '/') {
588 /* Matched a slash '/' */
589 chipset->suffix[i] = c;
590 } else {
591 /* Invalid suffix character (neither of [A-Za-z/]). This marks the end of the suffix. */
592 break;
593 }
594 }
595 }
596 /* All suffix characters successfully validated and copied to chipset data */
597 return true;
598}
599
600/**
601 * Tries to match /Kirin\s?\d{3}$/ signature for HiSilicon Kirin chipsets.
602 * If match successful, extracts model information into \p chipset argument.
603 *
604 * @param start - start of the /proc/cpuinfo Hardware string to match.
605 * @param end - end of the /proc/cpuinfo Hardware string to match.
606 * @param[out] chipset - location where chipset information will be stored upon a successful match.
607 *
608 * @returns true if signature matched, false otherwise.
609 */
610static bool match_kirin(
611 const char* start, const char* end,
612 struct cpuinfo_arm_chipset chipset[restrict static 1])
613{
614 /* Expect 8-9 symbols: "Kirin" (5 symbols) + optional whitespace (1 symbol) + 3-digit model number */
615 const size_t length = end - start;
616 switch (length) {
617 case 8:
618 case 9:
619 break;
620 default:
621 return false;
622 }
623
624 /* Check that the string starts with "Kirin". Symbols 1-5 are loaded and compared as little-endian 32-bit word. */
625 if (start[0] != 'K') {
626 return false;
627 }
628 const uint32_t irin = load_u32le(start + 1);
629 if (irin != UINT32_C(0x6E697269) /* "niri" = reverse("irin") */) {
630 return false;
631 }
632
633 /* Check for optional whitespace after "Kirin" */
634 if (is_ascii_whitespace(start[5])) {
635 /* When whitespace is present after "Kirin", expect 9 symbols total */
636 if (length != 9) {
637 return false;
638 }
639 }
640
641 /* Validate and parse 3-digit model number */
642 uint32_t model = 0;
643 for (int32_t i = 0; i < 3; i++) {
644 const uint32_t digit = (uint32_t) (uint8_t) end[i - 3] - '0';
645 if (digit >= 10) {
646 /* Not really a digit */
647 return start;
648 }
649 model = model * 10 + digit;
650 }
651
652 /*
653 * Thats it, return parsed chipset.
654 * Technically, Kirin 910T has a suffix, but it never appears in the form of "910T" string.
655 * Instead, Kirin 910T devices report "hi6620oem" string (handled outside of this function).
656 */
657 *chipset = (struct cpuinfo_arm_chipset) {
658 .vendor = cpuinfo_arm_chipset_vendor_hisilicon,
659 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
660 .model = model,
661 };
662 return true;
663}
664
665/**
666 * Tries to match /rk\d{4}[a-z]?$/ signature for Rockchip RK chipsets.
667 * If match successful, extracts model information into \p chipset argument.
668 *
669 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string or ro.board.platform) to match.
670 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string or ro.board.platform) to match.
671 * @param[out] chipset - location where chipset information will be stored upon a successful match.
672 *
673 * @returns true if signature matched, false otherwise.
674 */
675static bool match_rk(
676 const char* start, const char* end,
677 struct cpuinfo_arm_chipset chipset[restrict static 1])
678{
679 /* Expect 6-7 symbols: "RK" (2 symbols) + 4-digit model number + optional 1-letter suffix */
680 const size_t length = end - start;
681 switch (length) {
682 case 6:
683 case 7:
684 break;
685 default:
686 return false;
687 }
688
689 /*
690 * Check that string starts with "RK" (case-insensitive).
691 * The first two characters are loaded as 16-bit little endian word and converted to lowercase.
692 */
693 const uint16_t expected_rk = UINT16_C(0x2020) | load_u16le(start);
694 if (expected_rk != UINT16_C(0x6B72) /* "kr" = reverse("rk") */) {
695 return false;
696 }
697
698 /* Validate and parse 4-digit model number */
699 uint32_t model = 0;
700 for (uint32_t i = 2; i < 6; i++) {
701 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
702 if (digit >= 10) {
703 /* Not really a digit */
704 return start;
705 }
706 model = model * 10 + digit;
707 }
708
709 /* Parse optional suffix */
710 char suffix = 0;
711 if (length == 7) {
712 /* Parse the suffix letter */
713 const char c = start[6];
714 if (is_ascii_alphabetic(c)) {
715 /* Convert to upper case */
716 suffix = c & '\xDF';
717 } else {
718 /* Invalid suffix character */
719 return false;
720 }
721 }
722
723 /* Return parsed chipset */
724 *chipset = (struct cpuinfo_arm_chipset) {
725 .vendor = cpuinfo_arm_chipset_vendor_rockchip,
726 .series = cpuinfo_arm_chipset_series_rockchip_rk,
727 .model = model,
728 .suffix = {
729 [0] = suffix,
730 },
731 };
732 return true;
733}
734
735/**
736 * Tries to match, case-insentitively, /sc\d{4}[a-z]*|scx15$/ signature for Spreadtrum SC chipsets.
737 * If match successful, extracts model information into \p chipset argument.
738 *
739 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board,
740 * ro.board.platform, or ro.chipname) to match.
741 * @param end - end of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board,
742 * ro.board.platform, or ro.chipname) to match.
743 * @param[out] chipset - location where chipset information will be stored upon a successful match.
744 *
745 * @returns true if signature matched, false otherwise.
746 */
747static bool match_sc(
748 const char* start, const char* end,
749 struct cpuinfo_arm_chipset chipset[restrict static 1])
750{
751 /* Expect at least 5 symbols: "scx15" */
752 if (start + 5 > end) {
753 return false;
754 }
755
756 /*
757 * Check that string starts with "SC" (case-insensitive).
758 * The first two characters are loaded as 16-bit little endian word and converted to lowercase.
759 */
760 const uint16_t expected_sc = UINT16_C(0x2020) | load_u16le(start);
761 if (expected_sc != UINT16_C(0x6373) /* "cs" = reverse("sc") */) {
762 return false;
763 }
764
765 /* Special case: "scx" prefix (SC7715 reported as "scx15") */
766 if ((start[2] | '\x20') == 'x') {
767 /* Expect exactly 5 characters: "scx15" */
768 if (start + 5 != end) {
769 return false;
770 }
771
772 /* Check that string ends with "15" */
773 const uint16_t expected_15 = load_u16le(start + 3);
774 if (expected_15 != UINT16_C(0x3531) /* "51" = reverse("15") */ ) {
775 return false;
776 }
777
778 *chipset = (struct cpuinfo_arm_chipset) {
779 .vendor = cpuinfo_arm_chipset_vendor_spreadtrum,
780 .series = cpuinfo_arm_chipset_series_spreadtrum_sc,
781 .model = 7715,
782 };
783 return true;
784 }
785
786 /* Expect at least 6 symbols: "SC" (2 symbols) + 4-digit model number */
787 if (start + 6 > end) {
788 return false;
789 }
790
791 /* Validate and parse 4-digit model number */
792 uint32_t model = 0;
793 for (uint32_t i = 2; i < 6; i++) {
794 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
795 if (digit >= 10) {
796 /* Not really a digit */
797 return false;
798 }
799 model = model * 10 + digit;
800 }
801
802 /* Write parsed chipset */
803 *chipset = (struct cpuinfo_arm_chipset) {
804 .vendor = cpuinfo_arm_chipset_vendor_spreadtrum,
805 .series = cpuinfo_arm_chipset_series_spreadtrum_sc,
806 .model = model,
807 };
808
809 /* Validate and copy suffix letters. If suffix is too long, truncate at CPUINFO_ARM_CHIPSET_SUFFIX_MAX letters. */
810 const char* suffix = start + 6;
811 for (size_t i = 0; i < CPUINFO_ARM_CHIPSET_SUFFIX_MAX; i++) {
812 if (suffix + i == end) {
813 break;
814 }
815
816 const char c = suffix[i];
817 if (!is_ascii_alphabetic(c)) {
818 /* Invalid suffix character */
819 return false;
820 }
821 /* Convert suffix letter to uppercase */
822 chipset->suffix[i] = c & '\xDF';
823 }
824 return true;
825}
826
827/**
828 * Tries to match /lc\d{4}[a-z]?$/ signature for Leadcore LC chipsets.
829 * If match successful, extracts model information into \p chipset argument.
830 *
831 * @param start - start of the platform identifier (ro.product.board or ro.board.platform) to match.
832 * @param end - end of the platform identifier (ro.product.board or ro.board.platform) to match.
833 * @param[out] chipset - location where chipset information will be stored upon a successful match.
834 *
835 * @returns true if signature matched, false otherwise.
836 */
837static bool match_lc(
838 const char* start, const char* end,
839 struct cpuinfo_arm_chipset chipset[restrict static 1])
840{
841 /* Expect at 6-7 symbols: "lc" (2 symbols) + 4-digit model number + optional 1-letter suffix */
842 const size_t length = end - start;
843 switch (length) {
844 case 6:
845 case 7:
846 break;
847 default:
848 return false;
849 }
850
851 /* Check that string starts with "lc". The first two characters are loaded as 16-bit little endian word */
852 const uint16_t expected_lc = load_u16le(start);
853 if (expected_lc != UINT16_C(0x636C) /* "cl" = reverse("lc") */) {
854 return false;
855 }
856
857 /* Validate and parse 4-digit model number */
858 uint32_t model = 0;
859 for (uint32_t i = 2; i < 6; i++) {
860 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
861 if (digit >= 10) {
862 /* Not really a digit */
863 return false;
864 }
865 model = model * 10 + digit;
866 }
867
868 /* Parse optional suffix letter */
869 char suffix = 0;
870 if (length == 7) {
871 const char c = start[6];
872 if (is_ascii_alphabetic(c)) {
873 /* Convert to uppercase */
874 chipset->suffix[0] = c & '\xDF';
875 } else {
876 /* Invalid suffix character */
877 return false;
878 }
879 }
880
881 /* Return parsed chipset */
882 *chipset = (struct cpuinfo_arm_chipset) {
883 .vendor = cpuinfo_arm_chipset_vendor_leadcore,
884 .series = cpuinfo_arm_chipset_series_leadcore_lc,
885 .model = model,
886 .suffix = {
887 [0] = suffix,
888 },
889 };
890 return true;
891}
892
893/**
894 * Tries to match /PXA(\d{3,4}|1L88)$/ signature for Marvell PXA chipsets.
895 * If match successful, extracts model information into \p chipset argument.
896 *
897 * @param start - start of the platform identifier (/proc/cpuinfo Hardware string, ro.product.board or ro.chipname)
898 * to match.
899 * @param end - end of the platform identifier (/proc/cpuinfo Hardaware string, ro.product.board or ro.chipname) to
900 * match.
901 * @param[out] chipset - location where chipset information will be stored upon a successful match.
902 *
903 * @returns true if signature matched, false otherwise.
904 */
905static bool match_pxa(
906 const char* start, const char* end,
907 struct cpuinfo_arm_chipset chipset[restrict static 1])
908{
909 /* Expect 6-7 symbols: "PXA" (3 symbols) + 3-4 digit model number */
910 const size_t length = end - start;
911 switch (length) {
912 case 6:
913 case 7:
914 break;
915 default:
916 return false;
917 }
918
919 /* Check that the string starts with "PXA". Symbols 1-3 are loaded and compared as little-endian 16-bit word. */
920 if (start[0] != 'P') {
921 return false;
922 }
923 const uint16_t expected_xa = load_u16le(start + 1);
924 if (expected_xa != UINT16_C(0x4158) /* "AX" = reverse("XA") */) {
925 return false;
926 }
927
928 uint32_t model = 0;
929
930
931 /* Check for a very common typo: "PXA1L88" for "PXA1088" */
932 if (length == 7) {
933 /* Load 4 model "number" symbols as a little endian 32-bit word and compare to "1L88" */
934 const uint32_t expected_1L88 = load_u32le(start + 3);
935 if (expected_1L88 == UINT32_C(0x38384C31) /* "88L1" = reverse("1L88") */) {
936 model = 1088;
937 goto write_chipset;
938 }
939 }
940
941 /* Check and parse 3-4 digit model number */
942 for (uint32_t i = 3; i < length; i++) {
943 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
944 if (digit >= 10) {
945 /* Not really a digit */
946 return false;
947 }
948 model = model * 10 + digit;
949 }
950
951 /* Return parsed chipset. */
952write_chipset:
953 *chipset = (struct cpuinfo_arm_chipset) {
954 .vendor = cpuinfo_arm_chipset_vendor_marvell,
955 .series = cpuinfo_arm_chipset_series_marvell_pxa,
956 .model = model,
957 };
958 return true;
959}
960
961/**
962 * Tries to match /OMAP\d{4}$/ signature for Texas Instruments OMAP chipsets.
963 * If match successful, extracts model information into \p chipset argument.
964 *
965 * @param start - start of the /proc/cpuinfo Hardware string to match.
966 * @param end - end of the /proc/cpuinfo Hardaware string to match.
967 * @param[out] chipset - location where chipset information will be stored upon a successful match.
968 *
969 * @returns true if signature matched, false otherwise.
970 */
971static bool match_omap(
972 const char* start, const char* end,
973 struct cpuinfo_arm_chipset chipset[restrict static 1])
974{
975 /* Expect exactly 8 symbols: "OMAP" (4 symbols) + 4-digit model number */
976 if (start + 8 != end) {
977 return false;
978 }
979
980 /* Check that the string starts with "OMAP". Symbols 0-4 are loaded and compared as little-endian 32-bit word. */
981 const uint32_t expected_omap = load_u32le(start);
982 if (expected_omap != UINT32_C(0x50414D4F) /* "PAMO" = reverse("OMAP") */) {
983 return false;
984 }
985
986 /* Validate and parse 4-digit model number */
987 uint32_t model = 0;
988 for (uint32_t i = 4; i < 8; i++) {
989 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
990 if (digit >= 10) {
991 /* Not really a digit */
992 return false;
993 }
994 model = model * 10 + digit;
995 }
996
997 /* Return parsed chipset. */
998 *chipset = (struct cpuinfo_arm_chipset) {
999 .vendor = cpuinfo_arm_chipset_vendor_texas_instruments,
1000 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
1001 .model = model,
1002 };
1003 return true;
1004}
1005
1006/**
1007 * Compares platform identifier string to known values for Broadcom chipsets.
1008 * If the string matches one of the known values, the function decodes Broadcom chipset from frequency and number of
1009 * cores into \p chipset argument.
1010 *
1011 * @param start - start of the platform identifier (ro.product.board or ro.board.platform) to match.
1012 * @param end - end of the platform identifier (ro.product.board or ro.board.platform) to match.
1013 * @param cores - number of cores in the chipset.
1014 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
1015 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1016 *
1017 * @returns true if signature matched (even if exact model can't be decoded), false otherwise.
1018 */
1019static bool match_and_parse_broadcom(
1020 const char* start, const char* end, uint32_t cores, uint32_t max_cpu_freq_max,
1021 struct cpuinfo_arm_chipset chipset[restrict static 1])
1022{
1023 /* Expect 4-6 symbols: "java" (4 symbols), "rhea" (4 symbols), "capri" (5 symbols), or "hawaii" (6 symbols) */
1024 const size_t length = end - start;
1025 switch (length) {
1026 case 4:
1027 case 5:
1028 case 6:
1029 break;
1030 default:
1031 return false;
1032 }
1033
1034 /*
1035 * Compare the platform identifier to known values for Broadcom chipsets:
1036 * - "rhea"
1037 * - "java"
1038 * - "capri"
1039 * - "hawaii"
1040 * Upon a successful match, decode chipset name from frequency and number of cores.
1041 */
1042 uint32_t model = 0;
1043 char suffix = 0;
1044 const uint32_t expected_platform = load_u32le(start);
1045 switch (expected_platform) {
1046 case UINT32_C(0x61656872): /* "aehr" = reverse("rhea") */
1047 if (length == 4) {
1048 /*
1049 * Detected "rhea" platform:
1050 * - 1 core @ 849999 KHz -> BCM21654
1051 * - 1 core @ 999999 KHz -> BCM21654G
1052 */
1053 if (cores == 1) {
1054 model = 21654;
1055 if (max_cpu_freq_max >= 999999) {
1056 suffix = 'G';
1057 }
1058 }
1059 }
1060 break;
1061 case UINT32_C(0x6176616A): /* "avaj" = reverse("java") */
1062 if (length == 4) {
1063 /*
1064 * Detected "java" platform:
1065 * - 4 cores -> BCM23550
1066 */
1067 if (cores == 4) {
1068 model = 23550;
1069 }
1070 }
1071 break;
1072 case UINT32_C(0x61776168): /* "awah" = reverse("hawa") */
1073 if (length == 6) {
1074 /* Check that string equals "hawaii" */
1075 const uint16_t expected_ii = load_u16le(start + 4);
1076 if (expected_ii == UINT16_C(0x6969) /* "ii" */ ) {
1077 /*
1078 * Detected "hawaii" platform:
1079 * - 1 core -> BCM21663
1080 * - 2 cores @ 999999 KHz -> BCM21664
1081 * - 2 cores @ 1200000 KHz -> BCM21664T
1082 */
1083 switch (cores) {
1084 case 1:
1085 model = 21663;
1086 break;
1087 case 2:
1088 model = 21664;
1089 if (max_cpu_freq_max >= 1200000) {
1090 suffix = 'T';
1091 }
1092 break;
1093 }
1094 }
1095 }
1096 break;
1097 case UINT32_C(0x72706163): /* "rpac" = reverse("capr") */
1098 if (length == 5) {
1099 /* Check that string equals "capri" */
1100 if (start[4] == 'i') {
1101 /*
1102 * Detected "capri" platform:
1103 * - 2 cores -> BCM28155
1104 */
1105 if (cores == 2) {
1106 model = 28155;
1107 }
1108 }
1109 }
1110 break;
1111 }
1112
1113 if (model != 0) {
1114 /* Chipset was successfully decoded */
1115 *chipset = (struct cpuinfo_arm_chipset) {
1116 .vendor = cpuinfo_arm_chipset_vendor_broadcom,
1117 .series = cpuinfo_arm_chipset_series_broadcom_bcm,
1118 .model = model,
1119 .suffix = {
1120 [0] = suffix,
1121 },
1122 };
1123 }
1124 return model != 0;
1125}
1126
1127struct sunxi_map_entry {
1128 uint8_t sunxi;
1129 uint8_t cores;
1130 uint8_t model;
1131 char suffix;
1132};
1133
1134static const struct sunxi_map_entry sunxi_map_entries[] = {
1135 {
1136 /* ("sun4i", 1) -> "A10" */
1137 .sunxi = 4,
1138 .cores = 1,
1139 .model = 10,
1140 },
1141 {
1142 /* ("sun5i", 1) -> "A13" */
1143 .sunxi = 5,
1144 .cores = 1,
1145 .model = 13,
1146 },
1147 {
1148 /* ("sun6i", 4) -> "A31" */
1149 .sunxi = 6,
1150 .cores = 4,
1151 .model = 31,
1152 },
1153 {
1154 /* ("sun7i", 2) -> "A20" */
1155 .sunxi = 7,
1156 .cores = 2,
1157 .model = 20,
1158
1159 },
1160 {
1161 /* ("sun8i", 2) -> "A23" */
1162 .sunxi = 8,
1163 .cores = 2,
1164 .model = 23,
1165 },
1166 {
1167 /* ("sun8i", 4) -> "A33" */
1168 .sunxi = 8,
1169 .cores = 4,
1170 .model = 33,
1171 },
1172 {
1173 /* ("sun8i", 8) -> "A83T" */
1174 .sunxi = 8,
1175 .cores = 8,
1176 .model = 83,
1177 .suffix = 'T',
1178 },
1179 {
1180 /* ("sun9i", 8) -> "A80" */
1181 .sunxi = 9,
1182 .cores = 8,
1183 .model = 80,
1184 },
1185 {
1186 /* ("sun50i", 4) -> "A64" */
1187 .sunxi = 50,
1188 .cores = 4,
1189 .model = 64,
1190 },
1191};
1192
1193/**
1194 * Tries to match /proc/cpuinfo Hardware string to Allwinner /sun\d+i/ signature.
1195 * If the string matches signature, the function decodes Allwinner chipset from the number in the signature and the
1196 * number of cores, and stores it in \p chipset argument.
1197 *
1198 * @param start - start of the /proc/cpuinfo Hardware string to match.
1199 * @param end - end of the /proc/cpuinfo Hardware string to match.
1200 * @param cores - number of cores in the chipset.
1201 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1202 *
1203 * @returns true if signature matched (even if exact model can't be decoded), false otherwise.
1204 */
1205static bool match_and_parse_sunxi(
1206 const char* start, const char* end, uint32_t cores,
1207 struct cpuinfo_arm_chipset chipset[restrict static 1])
1208{
1209 /* Expect at least 5 symbols: "sun" (3 symbols) + platform id (1-2 digits) + "i" (1 symbol) */
1210 if (start + 5 > end) {
1211 return false;
1212 }
1213
1214 /* Compare the first 3 characters to "sun" */
1215 if (start[0] != 's') {
1216 return false;
1217 }
1218 const uint16_t expected_un = load_u16le(start + 1);
1219 if (expected_un != UINT16_C(0x6E75) /* "nu" = reverse("un") */) {
1220 return false;
1221 }
1222
1223 /* Check and parse the first (required) digit of the sunXi platform id */
1224 uint32_t sunxi_platform = 0;
1225 {
1226 const uint32_t digit = (uint32_t) (uint8_t) start[3] - '0';
1227 if (digit >= 10) {
1228 /* Not really a digit */
1229 return false;
1230 }
1231 sunxi_platform = digit;
1232 }
1233
1234 /* Parse optional second digit of the sunXi platform id */
1235 const char* pos = start + 4;
1236 {
1237 const uint32_t digit = (uint32_t) (uint8_t) (*pos) - '0';
1238 if (digit < 10) {
1239 sunxi_platform = sunxi_platform * 10 + digit;
1240 if (++pos == end) {
1241 /* Expected one more character, final 'i' letter */
1242 return false;
1243 }
1244 }
1245 }
1246
1247 /* Validate the final 'i' letter */
1248 if (*pos != 'i') {
1249 return false;
1250 }
1251
1252 /* Compare sunXi platform id and number of cores to tabluted values to decode chipset name */
1253 uint32_t model = 0;
1254 char suffix = 0;
1255 for (size_t i = 0; i < CPUINFO_COUNT_OF(sunxi_map_entries); i++) {
1256 if (sunxi_platform == sunxi_map_entries[i].sunxi && cores == sunxi_map_entries[i].cores) {
1257 model = sunxi_map_entries[i].model;
1258 suffix = sunxi_map_entries[i].suffix;
1259 break;
1260 }
1261 }
1262
1263 if (model == 0) {
1264 cpuinfo_log_info("unrecognized %"PRIu32"-core Allwinner sun%"PRIu32" platform", cores, sunxi_platform);
1265 }
1266 /* Create chipset name from decoded data */
1267 *chipset = (struct cpuinfo_arm_chipset) {
1268 .vendor = cpuinfo_arm_chipset_vendor_allwinner,
1269 .series = cpuinfo_arm_chipset_series_allwinner_a,
1270 .model = model,
1271 .suffix = {
1272 [0] = suffix,
1273 },
1274 };
1275 return true;
1276}
1277
1278/**
1279 * Compares /proc/cpuinfo Hardware string to "WMT" signature.
1280 * If the string matches signature, the function decodes WonderMedia chipset from frequency and number of cores into
1281 * \p chipset argument.
1282 *
1283 * @param start - start of the /proc/cpuinfo Hardware string to match.
1284 * @param end - end of the /proc/cpuinfo Hardware string to match.
1285 * @param cores - number of cores in the chipset.
1286 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
1287 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1288 *
1289 * @returns true if signature matched (even if exact model can't be decoded), false otherwise.
1290 */
1291static bool match_and_parse_wmt(
1292 const char* start, const char* end, uint32_t cores, uint32_t max_cpu_freq_max,
1293 struct cpuinfo_arm_chipset chipset[restrict static 1])
1294{
1295 /* Expected 3 symbols: "WMT" */
1296 if (start + 3 != end) {
1297 return false;
1298 }
1299
1300 /* Compare string to "WMT" */
1301 if (start[0] != 'W') {
1302 return false;
1303 }
1304 const uint16_t expected_mt = load_u16le(start + 1);
1305 if (expected_mt != UINT16_C(0x544D) /* "TM" = reverse("MT") */) {
1306 return false;
1307 }
1308
1309 /* Decode chipset name from frequency and number of cores */
1310 uint32_t model = 0;
1311 switch (cores) {
1312 case 1:
1313 switch (max_cpu_freq_max) {
1314 case 1008000:
1315 /* 1 core @ 1008000 KHz -> WM8950 */
1316 model = 8950;
1317 break;
1318 case 1200000:
1319 /* 1 core @ 1200000 KHz -> WM8850 */
1320 model = 8850;
1321 break;
1322 }
1323 break;
1324 case 2:
1325 if (max_cpu_freq_max == 1500000) {
1326 /* 2 cores @ 1500000 KHz -> WM8880 */
1327 model = 8880;
1328 }
1329 break;
1330 }
1331
1332 if (model == 0) {
1333 cpuinfo_log_info("unrecognized WonderMedia platform with %"PRIu32" cores at %"PRIu32" KHz",
1334 cores, max_cpu_freq_max);
1335 }
1336 *chipset = (struct cpuinfo_arm_chipset) {
1337 .vendor = cpuinfo_arm_chipset_vendor_wondermedia,
1338 .series = cpuinfo_arm_chipset_series_wondermedia_wm,
1339 .model = model,
1340 };
1341 return true;
1342}
1343
1344struct huawei_map_entry {
1345 uint32_t platform;
1346 uint32_t model;
1347};
1348
1349static const struct huawei_map_entry huawei_platform_map[] = {
1350 {
1351 /* "BAC" -> Kirin 659 */
1352 .platform = UINT32_C(0x00434142), /* "\0CAB" = reverse("BAC\0") */
1353 .model = 659,
1354 },
1355 {
1356 /* "DUK" -> Kirin 960 */
1357 .platform = UINT32_C(0x004B5544), /* "\0KUD" = reverse("DUK\0") */
1358 .model = 960,
1359 },
1360 {
1361 /* "EVA" -> Kirin 955 */
1362 .platform = UINT32_C(0x00415645), /* "\0AVE" = reverse("EVA\0") */
1363 .model = 955,
1364 },
1365 {
1366 /* "FRD" -> Kirin 950 */
1367 .platform = UINT32_C(0x00445246), /* "\0DRF" = reverse("FRD\0") */
1368 .model = 950,
1369 },
1370 {
1371 /* "KNT" -> Kirin 950 */
1372 .platform = UINT32_C(0x00544E4B), /* "\0TNK" = reverse("KNT\0") */
1373 .model = 950,
1374 },
1375 {
1376 /* "LON" -> Kirin 960 */
1377 .platform = UINT32_C(0x004E4F4C), /* "\0NOL" = reverse("LON\0") */
1378 .model = 960,
1379 },
1380 {
1381 /* "MHA" -> Kirin 960 */
1382 .platform = UINT32_C(0x0041484D), /* "\0AHM" = reverse("MHA\0") */
1383 .model = 960,
1384 },
1385 {
1386 /* "NXT" -> Kirin 950 */
1387 .platform = UINT32_C(0x0054584E), /* "\0TXN" = reverse("NXT\0") */
1388 .model = 950,
1389 },
1390 {
1391 /* "STF" -> Kirin 960 */
1392 .platform = UINT32_C(0x00465453), /* "\0FTS" = reverse("STF\0") */
1393 .model = 960,
1394 },
1395 {
1396 /* "VIE" -> Kirin 955 */
1397 .platform = UINT32_C(0x00454956), /* "\0EIV" = reverse("VIE\0") */
1398 .model = 955,
1399 },
1400 {
1401 /* "VKY" -> Kirin 960 */
1402 .platform = UINT32_C(0x00594B56), /* "\0YKV" = reverse("VKY\0") */
1403 .model = 960,
1404 },
1405 {
1406 /* "VTR" -> Kirin 960 */
1407 .platform = UINT32_C(0x00525456), /* "\0RTV" = reverse("VTR\0") */
1408 .model = 960,
1409 },
1410};
1411
1412/**
1413 * Tries to match ro.product.board string to Huawei /([A-Z]{3})(\-[A-Z]?L\d{2})$/ signature where \1 is one of the
1414 * known values for Huawei devices, which do not report chipset name elsewhere.
1415 * If the string matches signature, the function decodes chipset (always HiSilicon Kirin for matched devices) from
1416 * the number in the signature and stores it in \p chipset argument.
1417 *
1418 * @param start - start of the ro.product.board string to match.
1419 * @param end - end of the ro.product.board string to match.
1420 * @param[out] chipset - location where chipset information will be stored upon a successful match and decoding.
1421 *
1422 * @returns true if signature matched, false otherwise.
1423 */
1424static bool match_and_parse_huawei(
1425 const char* start, const char* end,
1426 struct cpuinfo_arm_chipset chipset[restrict static 1])
1427{
1428 /*
1429 * Expect length of either 3, 7 or 8, exactly:
1430 * - 3-letter platform identifier (see huawei_platform_map)
1431 * - 3-letter platform identifier + '-' + 'L' + two digits
1432 * - 3-letter platform identifier + '-' + capital letter + 'L' + two digits
1433 */
1434 const size_t length = end - start;
1435 switch (length) {
1436 case 3:
1437 case 7:
1438 case 8:
1439 break;
1440 default:
1441 return false;
1442 }
1443
1444 /*
1445 * Try to find the first three-letter substring in among the tabulated entries for Huawei devices.
1446 * The first three letters are loaded and compared as a little-endian 24-bit word.
1447 */
1448 uint32_t model = 0;
1449 const uint32_t target_platform_id = load_u24le(start);
1450 for (uint32_t i = 0; i < CPUINFO_COUNT_OF(huawei_platform_map); i++) {
1451 if (huawei_platform_map[i].platform == target_platform_id) {
1452 model = huawei_platform_map[i].model;
1453 break;
1454 }
1455 }
1456
1457 if (model == 0) {
1458 /* Platform does not match the tabulated Huawei entries */
1459 return false;
1460 }
1461
1462 if (length > 3) {
1463 /*
1464 * Check that:
1465 * - The symbol after platform id is a dash
1466 * - The symbol after it is an uppercase letter. For 7-symbol strings, the symbol is just 'L'.
1467 */
1468 if (start[3] != '-' || !is_ascii_alphabetic_uppercase(start[4])) {
1469 return false;
1470 }
1471
1472 /* Check that the last 3 entries are /L\d\d/ */
1473 if (end[-3] != 'L' || !is_ascii_numeric(end[-2]) || !is_ascii_numeric(end[-1])) {
1474 return false;
1475 }
1476 }
1477
1478 /* All checks succeeded, commit chipset name */
1479 *chipset = (struct cpuinfo_arm_chipset) {
1480 .vendor = cpuinfo_arm_chipset_vendor_hisilicon,
1481 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1482 .model = model,
1483 };
1484 return true;
1485}
1486
1487/**
1488 * Tries to match /tcc\d{3}x$/ signature for Telechips TCCXXXx chipsets.
1489 * If match successful, extracts model information into \p chipset argument.
1490 *
1491 * @param start - start of the /proc/cpuinfo Hardware string to match.
1492 * @param end - end of the /proc/cpuinfo Hardware string to match.
1493 * @param[out] chipset - location where chipset information will be stored upon a successful match.
1494 *
1495 * @returns true if signature matched, false otherwise.
1496 */
1497static bool match_tcc(
1498 const char* start, const char* end,
1499 struct cpuinfo_arm_chipset chipset[restrict static 1])
1500{
1501 /* Expect exactly 7 symbols: "tcc" (3 symbols) + 3-digit model number + fixed "x" suffix */
1502 if (start + 7 != end) {
1503 return false;
1504 }
1505
1506 /* Quick check for the first character */
1507 if (start[0] != 't') {
1508 return false;
1509 }
1510
1511 /* Load the next 2 bytes as little endian 16-bit word */
1512 const uint16_t expected_cc = load_u16le(start + 1);
1513 if (expected_cc != UINT16_C(0x6363) /* "cc" */ ) {
1514 return false;
1515 }
1516
1517 /* Check and parse 3-digit model number */
1518 uint32_t model = 0;
1519 for (uint32_t i = 3; i < 6; i++) {
1520 const uint32_t digit = (uint32_t) (uint8_t) start[i] - '0';
1521 if (digit >= 10) {
1522 /* Not really a digit */
1523 return false;
1524 }
1525 model = model * 10 + digit;
1526 }
1527
1528 /* Check the fixed 'x' suffix in the end */
1529 if (start[6] != 'x') {
1530 return false;
1531 }
1532
1533 /* Commit parsed chipset. */
1534 *chipset = (struct cpuinfo_arm_chipset) {
1535 .vendor = cpuinfo_arm_chipset_vendor_telechips,
1536 .series = cpuinfo_arm_chipset_series_telechips_tcc,
1537 .model = model,
Marat Dukhan63a6a102017-08-24 21:28:02 -07001538 .suffix = {
1539 [0] = 'X'
1540 },
Marat Dukhan006461a2017-08-24 16:10:46 -07001541 };
1542 return true;
1543}
1544
1545/*
1546 * Compares ro.board.platform string to nVidia Tegra signatures ("tegra" and "tegra3")
1547 * This check has effect on how /proc/cpuinfo Hardware string is interpreted.
1548 *
1549 * @param start - start of the ro.board.platform string to check.
1550 * @param end - end of the ro.board.platform string to check.
1551 *
1552 * @returns true if the string matches an nVidia Tegra signature, and false otherwise
1553 */
1554static bool is_tegra(const char* start, const char* end) {
1555 /* Expect 5 ("tegra") or 6 ("tegra3") symbols */
1556 const size_t length = end - start;
1557 switch (length) {
1558 case 5:
1559 case 6:
1560 break;
1561 default:
1562 return false;
1563 }
1564
1565 /* Check that the first 5 characters match "tegra" */
1566 if (start[0] != 't') {
1567 return false;
1568 }
1569 const uint32_t expected_egra = load_u32le(start + 1);
1570 if (expected_egra != UINT32_C(0x61726765) /* "arge" = reverse("egra") */) {
1571 return false;
1572 }
1573
1574 /* Check if the string is either "tegra" (length = 5) or "tegra3" (length != 5) and last character is '3' */
1575 return (length == 5 || start[5] == '3');
1576}
1577
1578struct special_map_entry {
1579 const char* platform;
1580 uint16_t model;
1581 uint8_t series;
1582 char suffix;
1583};
1584
1585static const struct special_map_entry special_hardware_map_entries[] = {
1586 {
1587 /* "k3v2oem1" -> HiSilicon K3V2 */
1588 .platform = "k3v2oem1",
1589 .series = cpuinfo_arm_chipset_series_hisilicon_k3v,
1590 .model = 2,
1591 },
1592 {
1593 /* "hi6620oem" -> HiSilicon Kirin 910T */
1594 .platform = "hi6620oem",
1595 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1596 .model = 910,
1597 .suffix = 'T'
1598 },
1599 {
1600 /* "hi6250" -> HiSilicon Kirin 650 */
1601 .platform = "hi6250",
1602 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1603 .model = 650,
1604 },
1605 {
1606 /* "hi6210sft" -> HiSilicon Kirin 620 */
1607 .platform = "hi6210sft",
1608 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1609 .model = 620,
1610 },
1611 {
1612 /* "hi3751" -> HiSilicon Hi3751 */
1613 .platform = "hi3751",
1614 .series = cpuinfo_arm_chipset_series_hisilicon_hi,
1615 .model = 3751,
1616 },
1617 {
1618 /* "hi3635" -> HiSilicon Kirin 930 */
1619 .platform = "hi3635",
1620 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1621 .model = 930,
1622 },
1623 {
1624 /* "hi3630" -> HiSilicon Kirin 920 */
1625 .platform = "hi3630",
1626 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
1627 .model = 920,
1628 },
1629 {
1630 /* "gs705a" -> Actions ATM7059A */
1631 .platform = "gs705a",
1632 .series = cpuinfo_arm_chipset_series_actions_atm,
1633 .model = 7059,
1634 .suffix = 'A',
1635 },
1636 {
1637 /* "gs702a" -> Actions ATM7029 */
1638 .platform = "gs702a",
1639 .series = cpuinfo_arm_chipset_series_actions_atm,
1640 .model = 7029,
1641 },
1642 {
1643 /* "gs702c" -> Actions ATM7029B */
1644 .platform = "gs702c",
1645 .series = cpuinfo_arm_chipset_series_actions_atm,
1646 .model = 7029,
1647 .suffix = 'B'
1648 },
1649 {
1650 /* "Amlogic Meson8" -> Amlogic S812 */
1651 .platform = "Amlogic Meson8",
1652 .series = cpuinfo_arm_chipset_series_amlogic_s,
1653 .model = 812,
1654 },
1655 {
1656 /* "Amlogic Meson8B" -> Amlogic S805 */
1657 .platform = "Amlogic Meson8B",
1658 .series = cpuinfo_arm_chipset_series_amlogic_s,
1659 .model = 805,
1660 },
1661 {
1662 /* "mapphone_CDMA" -> Texas Instruments OMAP4430 */
1663 .platform = "mapphone_CDMA",
1664 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
1665 .model = 4430,
1666 },
1667 {
1668 /* "Tuna" (Samsung Galaxy Nexus) -> Texas Instruments OMAP4460 */
1669 .platform = "Tuna",
1670 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
1671 .model = 4460,
1672 },
1673 {
1674 /* "Manta" (Samsung Nexus 10) -> Samsung Exynos 5250 */
1675 .platform = "Manta",
1676 .series = cpuinfo_arm_chipset_series_samsung_exynos,
1677 .model = 5250,
1678 },
1679 {
1680 /* "Odin" -> LG Nuclun 7111 */
1681 .platform = "Odin",
1682 .series = cpuinfo_arm_chipset_series_lg_nuclun,
1683 .model = 7111,
1684 },
1685 {
1686 /* "Madison" -> MStar 6A338 */
1687 .platform = "Madison",
1688 .series = cpuinfo_arm_chipset_series_mstar_6a,
1689 .model = 338,
1690 },
1691};
1692
1693static const struct special_map_entry tegra_hardware_map_entries[] = {
1694 {
1695 /* "cardhu" (nVidia Cardhu developer tablet) -> Tegra T30 */
1696 .platform = "cardhu",
1697 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1698 .model = 30,
1699 },
1700 {
1701 /* "p3" (Samsung Galaxy Tab 8.9) -> Tegra T20 */
1702 .platform = "p3",
1703 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1704 .model = 20,
1705 },
1706 {
1707 /* "endeavoru" (HTC One X) -> Tegra AP33 */
1708 .platform = "endeavoru",
1709 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1710 .model = 33,
1711 },
1712 {
1713 /* "evitareul" (HTC One X+) -> Tegra T33 */
1714 .platform = "evitareul",
1715 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1716 .model = 33,
1717 },
1718 {
1719 /* "enrc2b" (HTC One X+) -> Tegra T33 */
1720 .platform = "enrc2b",
1721 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1722 .model = 33,
1723 },
1724 {
1725 /* "mozart" (Asus Transformer Pad TF701T) -> Tegra T114 */
1726 .platform = "mozart",
1727 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1728 .model = 114,
1729 },
1730 {
1731 /* "tn8" (nVidia Shield Tablet K1) -> Tegra T124 */
1732 .platform = "tn8",
1733 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1734 .model = 124,
1735 },
1736 {
1737 /* "mocha" (Xiaomi Mi Pad) -> Tegra T124 */
1738 .platform = "mocha",
1739 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1740 .model = 124,
1741 },
1742 {
1743 /* "stingray" (Motorola XOOM) -> Tegra AP20H */
1744 .platform = "stingray",
1745 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1746 .model = 20,
1747 .suffix = 'H',
1748 },
1749 {
1750 /* "Ceres" (Wiko Highway 4G) -> Tegra SL460N */
1751 .platform = "Ceres",
1752 .series = cpuinfo_arm_chipset_series_nvidia_tegra_sl,
1753 .model = 460,
1754 .suffix = 'N',
1755 },
1756 {
1757 /* "chagall" (Fujitsu Stylistic M532) -> Tegra T30 */
1758 .platform = "chagall",
1759 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1760 .model = 30,
1761 },
1762 {
1763 /* "ventana" (Asus Transformer TF101) -> Tegra T20 */
1764 .platform = "ventana",
1765 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1766 .model = 20,
1767 },
1768 {
1769 /* "bobsleigh" (Fujitsu Arrows Tab F-05E) -> Tegra T33 */
1770 .platform = "bobsleigh",
1771 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1772 .model = 33,
1773 },
1774 {
1775 /* "tegra_fjdev103" (Fujitsu Arrows V F-04E) -> Tegra T33 */
1776 .platform = "tegra_fjdev103",
1777 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1778 .model = 33,
1779 },
1780 {
1781 /* "nbx03" (Sony Tablet S) -> Tegra T20 */
1782 .platform = "nbx03",
1783 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1784 .model = 20,
1785 },
1786 {
1787 /* "txs03" (Sony Xperia Tablet S) -> Tegra T30L */
1788 .platform = "txs03",
1789 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1790 .model = 30,
1791 .suffix = 'L',
1792 },
1793 {
1794 /* "x3" (LG Optimus 4X HD P880) -> Tegra AP33 */
1795 .platform = "x3",
1796 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1797 .model = 33,
1798 },
1799 {
1800 /* "BIRCH" (HP Slate 7 Plus) -> Tegra T30L */
1801 .platform = "BIRCH",
1802 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1803 .model = 30,
1804 .suffix = 'L',
1805 },
1806 {
1807 /* "macallan" (HP Slate 8 Pro) -> Tegra T114 */
1808 .platform = "macallan",
1809 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1810 .model = 114,
1811 },
1812 {
1813 /* "tostab12BL" (Toshiba AT10-A "Excite Pure") -> Tegra T30L */
1814 .platform = "tostab12BL",
1815 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1816 .model = 30,
1817 .suffix = 'L',
1818 },
1819 {
1820 /* "tostab12BA" (Toshiba AT10-LE-A "Excite Pro") -> Tegra T114 */
1821 .platform = "tostab12BA",
1822 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1823 .model = 114,
1824 },
1825 {
1826 /* "picasso" (Acer Iconia Tab A500) -> Tegra AP20H */
1827 .platform = "picasso",
1828 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1829 .model = 20,
1830 .suffix = 'H',
1831 },
1832 {
1833 /* "picasso_e" (Acer Iconia Tab A200) -> Tegra AP20H */
1834 .platform = "picasso_e",
1835 .series = cpuinfo_arm_chipset_series_nvidia_tegra_ap,
1836 .model = 20,
1837 .suffix = 'H',
1838 },
1839 {
1840 /* "picasso_e2" (Acer Iconia Tab A210) -> Tegra T30L */
1841 .platform = "picasso_e2",
1842 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1843 .model = 30,
1844 .suffix = 'L',
1845 },
1846 {
1847 /* "picasso_m" (Acer Iconia Tab A510) -> Tegra T30 */
1848 .platform = "picasso_m",
1849 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1850 .model = 30,
1851 },
1852 {
1853 /* "picasso_mf" (Acer Iconia Tab A700) -> Tegra T30 */
1854 .platform = "picasso_mf",
1855 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1856 .model = 30,
1857 },
1858 {
1859 /* "avalon" (Toshiba AT300 "Excite 10") -> Tegra T30L */
1860 .platform = "avalon",
1861 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1862 .model = 30,
1863 .suffix = 'L',
1864 },
1865 {
1866 /* "NS_14T004" (iRiver NS-14T004) -> Tegra T30L */
1867 .platform = "NS_14T004",
1868 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
1869 .model = 30,
1870 .suffix = 'L',
1871 },
1872};
1873
1874/*
1875 * Decodes chipset name from /proc/cpuinfo Hardware string.
1876 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
1877 *
1878 * @param[in] platform - /proc/cpuinfo Hardware string.
1879 * @param cores - number of cores in the chipset.
1880 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
1881 *
1882 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
1883 * and series identifiers.
1884 */
1885struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_proc_cpuinfo_hardware(
1886 const char hardware[restrict static CPUINFO_HARDWARE_VALUE_MAX],
1887 uint32_t cores, uint32_t max_cpu_freq_max, bool is_tegra)
1888{
1889 struct cpuinfo_arm_chipset chipset;
1890 const size_t hardware_length = strnlen(hardware, CPUINFO_HARDWARE_VALUE_MAX);
1891 const char* hardware_end = hardware + hardware_length;
1892
1893 if (is_tegra) {
1894 /*
1895 * nVidia Tegra-specific path: compare /proc/cpuinfo Hardware string to
1896 * tabulated Hardware values for popular chipsets/devices with Tegra chipsets.
1897 * This path is only used when ro.board.platform indicates a Tegra chipset
1898 * (albeit does not indicate which exactly Tegra chipset).
1899 */
1900 for (size_t i = 0; i < CPUINFO_COUNT_OF(tegra_hardware_map_entries); i++) {
1901 if (strncmp(tegra_hardware_map_entries[i].platform, hardware, hardware_length) == 0 &&
1902 tegra_hardware_map_entries[i].platform[hardware_length] == 0)
1903 {
1904 cpuinfo_log_debug(
1905 "found /proc/cpuinfo Hardware string \"%.*s\" in nVidia Tegra chipset table",
1906 (int) hardware_length, hardware);
1907 /* Create chipset name from entry */
1908 return (struct cpuinfo_arm_chipset) {
1909 .vendor = chipset_series_vendor[tegra_hardware_map_entries[i].series],
1910 .series = (enum cpuinfo_arm_chipset_series) tegra_hardware_map_entries[i].series,
1911 .model = tegra_hardware_map_entries[i].model,
1912 .suffix = {
1913 [0] = tegra_hardware_map_entries[i].suffix,
1914 },
1915 };
1916 }
1917 }
1918 } else {
1919 /* Generic path: consider all other vendors */
1920
1921 bool word_start = true;
1922 for (const char* pos = hardware; pos != hardware_end; pos++) {
1923 const char c = *pos;
1924 switch (c) {
1925 case ' ':
1926 case '\t':
1927 case ',':
1928 word_start = true;
1929 break;
1930 default:
1931 if (word_start && is_ascii_alphabetic(c)) {
1932 /* Check Qualcomm MSM/APQ signature */
1933 if (match_msm_apq(pos, hardware_end, &chipset)) {
1934 cpuinfo_log_debug(
1935 "matched Qualcomm MSM/APQ signature in /proc/cpuinfo Hardware string \"%.*s\"",
1936 (int) hardware_length, hardware);
1937 return chipset;
1938 }
1939
1940 /* Check SDMxxx (Qualcomm Snapdragon) signature */
1941 if (match_sdm(pos, hardware_end, &chipset)) {
1942 cpuinfo_log_debug(
1943 "matched Qualcomm SDM signature in /proc/cpuinfo Hardware string \"%.*s\"",
1944 (int) hardware_length, hardware);
1945 return chipset;
1946 }
1947
1948 /* Check MediaTek MT signature */
1949 if (match_mt(pos, hardware_end, true, &chipset)) {
1950 cpuinfo_log_debug(
1951 "matched MediaTek MT signature in /proc/cpuinfo Hardware string \"%.*s\"",
1952 (int) hardware_length, hardware);
1953 return chipset;
1954 }
1955
1956 /* Check HiSilicon Kirin signature */
1957 if (match_kirin(pos, hardware_end, &chipset)) {
1958 cpuinfo_log_debug(
1959 "matched HiSilicon Kirin signature in /proc/cpuinfo Hardware string \"%.*s\"",
1960 (int) hardware_length, hardware);
1961 return chipset;
1962 }
1963
1964 /* Check Rockchip RK signature */
1965 if (match_rk(pos, hardware_end, &chipset)) {
1966 cpuinfo_log_debug(
1967 "matched Rockchip RK signature in /proc/cpuinfo Hardware string \"%.*s\"",
1968 (int) hardware_length, hardware);
1969 return chipset;
1970 }
1971 }
1972 word_start = false;
1973 break;
1974 }
1975 }
1976
1977 /* Check Samsung Exynos signature */
1978 if (match_samsung_exynos(hardware, hardware_end, &chipset)) {
1979 cpuinfo_log_debug(
1980 "matched Samsung Exynos signature in /proc/cpuinfo Hardware string \"%.*s\"",
1981 (int) hardware_length, hardware);
1982 return chipset;
1983 }
1984
1985 /* Check universalXXXX (Samsung Exynos) signature */
1986 if (match_universal(hardware, hardware_end, &chipset)) {
1987 cpuinfo_log_debug(
1988 "matched UNIVERSAL (Samsung Exynos) signature in /proc/cpuinfo Hardware string \"%.*s\"",
1989 (int) hardware_length, hardware);
1990 return chipset;
1991 }
1992
1993 /* Match /SMDK(4410|4x12)$/ */
1994 if (match_and_parse_smdk(hardware, hardware_end, cores, &chipset)) {
1995 cpuinfo_log_debug(
1996 "matched SMDK (Samsung Exynos) signature in /proc/cpuinfo Hardware string \"%.*s\"",
1997 (int) hardware_length, hardware);
1998 return chipset;
1999 }
2000
2001 /* Check Spreadtrum SC signature */
2002 if (match_sc(hardware, hardware_end, &chipset)) {
2003 cpuinfo_log_debug(
2004 "matched Spreadtrum SC signature in /proc/cpuinfo Hardware string \"%.*s\"",
2005 (int) hardware_length, hardware);
2006 return chipset;
2007 }
2008
2009 /* Check Marvell PXA signature */
2010 if (match_pxa(hardware, hardware_end, &chipset)) {
2011 cpuinfo_log_debug(
2012 "matched Marvell PXA signature in /proc/cpuinfo Hardware string \"%.*s\"",
2013 (int) hardware_length, hardware);
2014 return chipset;
2015 }
2016
2017 /* Match /sun\d+i/ signature and map to Allwinner chipset name */
2018 if (match_and_parse_sunxi(hardware, hardware_end, cores, &chipset)) {
2019 cpuinfo_log_debug(
2020 "matched sunxi (Allwinner Ax) signature in /proc/cpuinfo Hardware string \"%.*s\"",
2021 (int) hardware_length, hardware);
2022 return chipset;
2023 }
2024
2025 /* Check Texas Instruments OMAP signature */
2026 if (match_omap(hardware, hardware_end, &chipset)) {
2027 cpuinfo_log_debug(
2028 "matched Texas Instruments OMAP signature in /proc/cpuinfo Hardware string \"%.*s\"",
2029 (int) hardware_length, hardware);
2030 return chipset;
2031 }
2032
2033 /* Check WonderMedia WMT signature and decode chipset from frequency and number of cores */
2034 if (match_and_parse_wmt(hardware, hardware_end, cores, max_cpu_freq_max, &chipset)) {
2035 cpuinfo_log_debug(
2036 "matched WonderMedia WMT signature in /proc/cpuinfo Hardware string \"%.*s\"",
2037 (int) hardware_length, hardware);
2038 return chipset;
2039 }
2040
2041 /* Check Telechips TCC signature */
2042 if (match_tcc(hardware, hardware_end, &chipset)) {
2043 cpuinfo_log_debug(
2044 "matched Telechips TCC signature in /proc/cpuinfo Hardware string \"%.*s\"",
2045 (int) hardware_length, hardware);
2046 return chipset;
2047 }
2048
2049 /* Compare to tabulated Hardware values for popular chipsets/devices which can't be otherwise detected */
2050 for (size_t i = 0; i < CPUINFO_COUNT_OF(special_hardware_map_entries); i++) {
2051 if (strncmp(special_hardware_map_entries[i].platform, hardware, hardware_length) == 0 &&
2052 special_hardware_map_entries[i].platform[hardware_length] == 0)
2053 {
2054 cpuinfo_log_debug(
2055 "found /proc/cpuinfo Hardware string \"%.*s\" in special chipset table",
2056 (int) hardware_length, hardware);
2057 /* Create chipset name from entry */
2058 return (struct cpuinfo_arm_chipset) {
2059 .vendor = chipset_series_vendor[special_hardware_map_entries[i].series],
2060 .series = (enum cpuinfo_arm_chipset_series) special_hardware_map_entries[i].series,
2061 .model = special_hardware_map_entries[i].model,
2062 .suffix = {
2063 [0] = special_hardware_map_entries[i].suffix,
2064 },
2065 };
2066 }
2067 }
2068 }
2069
2070 return (struct cpuinfo_arm_chipset) {
2071 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2072 .series = cpuinfo_arm_chipset_series_unknown,
2073 };
2074}
2075
2076static const struct special_map_entry special_board_map_entries[] = {
2077 {
2078 /* "hi6250" -> HiSilicon Kirin 650 */
2079 .platform = "hi6250",
2080 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2081 .model = 650,
2082 },
2083 {
2084 /* "hi6210sft" -> HiSilicon Kirin 620 */
2085 .platform = "hi6210sft",
2086 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2087 .model = 620,
2088 },
2089 {
2090 /* "hi3650" -> HiSilicon Kirin 950 */
2091 .platform = "hi3650",
2092 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2093 .model = 950,
2094 },
2095 {
2096 /* "hi3635" -> HiSilicon Kirin 930 */
2097 .platform = "hi3635",
2098 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2099 .model = 930,
2100 },
2101 {
2102 /* "hi3630" -> HiSilicon Kirin 920 */
2103 .platform = "hi3630",
2104 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2105 .model = 920,
2106 },
2107 {
2108 /* "mp523x" -> Renesas MP5232 */
2109 .platform = "mp523x",
2110 .series = cpuinfo_arm_chipset_series_renesas_mp,
2111 .model = 5232,
2112 },
2113 {
2114 /* "piranha" -> Texas Instruments OMAP4430 */
2115 .platform = "piranha",
2116 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
2117 .model = 4430,
2118 },
2119 {
2120 /* "BEETHOVEN" (Huawei MadiaPad M3) -> HiSilicon Kirin 950 */
2121 .platform = "BEETHOVEN",
2122 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2123 .model = 950,
2124 },
2125 {
2126 /* "hws7701u" (Huawei MediaPad 7 Youth) -> Rockchip RK3168 */
2127 .platform = "hws7701u",
2128 .series = cpuinfo_arm_chipset_series_rockchip_rk,
2129 .model = 3168,
2130 },
2131 {
2132 /* "g2mv" (LG G2 mini LTE) -> nVidia Tegra SL460N */
2133 .platform = "g2mv",
2134 .series = cpuinfo_arm_chipset_series_nvidia_tegra_sl,
2135 .model = 460,
2136 .suffix = 'N',
2137 },
2138 {
2139 /* "K00F" (Asus MeMO Pad 10) -> Rockchip RK3188 */
2140 .platform = "K00F",
2141 .series = cpuinfo_arm_chipset_series_rockchip_rk,
2142 .model = 3188,
2143 },
2144 {
2145 /* "T7H" (HP Slate 7) -> Rockchip RK3066 */
2146 .platform = "T7H",
2147 .series = cpuinfo_arm_chipset_series_rockchip_rk,
2148 .model = 3066,
2149 },
2150 {
2151 /* "tuna" (Samsung Galaxy Nexus) -> Texas Instruments OMAP4460 */
2152 .platform = "tuna",
2153 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
2154 .model = 4460,
2155 },
2156 {
2157 /* "grouper" (Asus Nexus 7 2012) -> nVidia Tegra T30L */
2158 .platform = "grouper",
2159 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2160 .model = 30,
2161 .suffix = 'L',
2162 },
2163 {
2164 /* "flounder" (HTC Nexus 9) -> nVidia Tegra T132 */
2165 .platform = "flounder",
2166 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2167 .model = 132,
2168 },
2169 {
2170 /* "dragon" (Google Pixel C) -> nVidia Tegra T210 */
2171 .platform = "dragon",
2172 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2173 .model = 210,
2174 },
2175 {
2176 /* "sailfish" (Google Pixel) -> Qualcomm MSM8996PRO */
2177 .platform = "sailfish",
2178 .series = cpuinfo_arm_chipset_series_qualcomm_msm,
2179 .model = 8996,
2180 .suffix = 'P',
2181 },
2182 {
2183 /* "marlin" (Google Pixel XL) -> Qualcomm MSM8996PRO */
2184 .platform = "marlin",
2185 .series = cpuinfo_arm_chipset_series_qualcomm_msm,
2186 .model = 8996,
2187 .suffix = 'P',
2188 },
2189};
2190
2191/*
2192 * Decodes chipset name from ro.product.board Android system property.
2193 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
2194 *
2195 * @param[in] platform - ro.product.board value.
2196 * @param cores - number of cores in the chipset.
2197 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2198 *
2199 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2200 * and series identifiers.
2201 */
2202struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_product_board(
2203 const char ro_product_board[restrict static CPUINFO_BUILD_PROP_VALUE_MAX],
2204 uint32_t cores, uint32_t max_cpu_freq_max)
2205{
2206 struct cpuinfo_arm_chipset chipset;
2207 const char* board = ro_product_board;
2208 const size_t board_length = strnlen(ro_product_board, CPUINFO_BUILD_PROP_VALUE_MAX);
2209 const char* board_end = ro_product_board + board_length;
2210
2211 /* Check Qualcomm MSM/APQ signature */
2212 if (match_msm_apq(board, board_end, &chipset)) {
2213 cpuinfo_log_debug(
2214 "matched Qualcomm MSM/APQ signature in ro.product.board string \"%.*s\"", (int) board_length, board);
2215 return chipset;
2216 }
2217
2218 /* Check universaXXXX (Samsung Exynos) signature */
2219 if (match_universal(board, board_end, &chipset)) {
2220 cpuinfo_log_debug(
2221 "matched UNIVERSAL (Samsung Exynos) signature in ro.product.board string \"%.*s\"",
2222 (int) board_length, board);
2223 return chipset;
2224 }
2225
2226 /* Check SMDK (Samsung Exynos) signature */
2227 if (match_and_parse_smdk(board, board_end, cores, &chipset)) {
2228 cpuinfo_log_debug(
2229 "matched SMDK (Samsung Exynos) signature in ro.product.board string \"%.*s\"",
2230 (int) board_length, board);
2231 return chipset;
2232 }
2233
2234 /* Check MediaTek MT signature */
2235 if (match_mt(board, board_end, true, &chipset)) {
2236 cpuinfo_log_debug(
2237 "matched MediaTek MT signature in ro.product.board string \"%.*s\"",
2238 (int) board_length, board);
2239 return chipset;
2240 }
2241
2242 /* Check Spreadtrum SC signature */
2243 if (match_sc(board, board_end, &chipset)) {
2244 cpuinfo_log_debug(
2245 "matched Spreadtrum SC signature in ro.product.board string \"%.*s\"",
2246 (int) board_length, board);
2247 return chipset;
2248 }
2249
2250 /* Check Marvell PXA signature */
2251 if (match_pxa(board, board_end, &chipset)) {
2252 cpuinfo_log_debug(
2253 "matched Marvell PXA signature in ro.product.board string \"%.*s\"",
2254 (int) board_length, board);
2255 return chipset;
2256 }
2257
2258 /* Check Leadcore LCxxxx signature */
2259 if (match_lc(board, board_end, &chipset)) {
2260 cpuinfo_log_debug(
2261 "matched Leadcore LC signature in ro.product.board string \"%.*s\"",
2262 (int) board_length, board);
2263 return chipset;
2264 }
2265
2266 /*
2267 * Compare to tabulated ro.product.board values for Broadcom chipsets and decode chipset from frequency and
2268 * number of cores.
2269 */
2270 if (match_and_parse_broadcom(board, board_end, cores, max_cpu_freq_max, &chipset)) {
2271 cpuinfo_log_debug(
2272 "found ro.product.board string \"%.*s\" in Broadcom chipset table",
2273 (int) board_length, board);
2274 return chipset;
2275 }
2276
2277 /* Compare to tabulated ro.product.board values for Huawei devices which don't report chipset elsewhere */
2278 if (match_and_parse_huawei(board, board_end, &chipset)) {
2279 cpuinfo_log_debug(
2280 "found ro.product.board string \"%.*s\" in Huawei chipset table",
2281 (int) board_length, board);
2282 return chipset;
2283 }
2284
2285 /* Compare to tabulated ro.product.board values for popular chipsets/devices which can't be otherwise detected */
2286 for (size_t i = 0; i < CPUINFO_COUNT_OF(special_board_map_entries); i++) {
2287 if (strncmp(special_board_map_entries[i].platform, board, board_length) == 0 &&
2288 special_board_map_entries[i].platform[board_length] == 0)
2289 {
2290 cpuinfo_log_debug(
2291 "found ro.product.board string \"%.*s\" in special chipset table",
2292 (int) board_length, board);
2293 /* Create chipset name from entry */
2294 return (struct cpuinfo_arm_chipset) {
2295 .vendor = chipset_series_vendor[special_board_map_entries[i].series],
2296 .series = (enum cpuinfo_arm_chipset_series) special_board_map_entries[i].series,
2297 .model = special_board_map_entries[i].model,
2298 .suffix = {
2299 [0] = special_board_map_entries[i].suffix,
2300 /* The suffix of MSM8996PRO is truncated at the first letter, reconstruct it here. */
2301 [1] = special_board_map_entries[i].suffix == 'P' ? 'R' : 0,
2302 [2] = special_board_map_entries[i].suffix == 'P' ? 'O' : 0,
2303 },
2304 };
2305 }
2306 }
2307
2308 return (struct cpuinfo_arm_chipset) {
2309 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2310 .series = cpuinfo_arm_chipset_series_unknown,
2311 };
2312}
2313
2314struct amlogic_map_entry {
2315 char ro_board_platform[6];
2316 uint16_t model;
2317 uint8_t series;
2318 char suffix[3];
2319};
2320
2321static const struct amlogic_map_entry amlogic_map_entries[] = {
2322 {
2323 /* "meson3" -> Amlogic AML8726-M */
2324 .ro_board_platform = "meson3",
2325 .series = cpuinfo_arm_chipset_series_amlogic_aml,
2326 .model = 8726,
2327 .suffix = "-M",
2328 },
2329 {
2330 /* "meson6" -> Amlogic AML8726-MX */
2331 .ro_board_platform = "meson6",
2332 .series = cpuinfo_arm_chipset_series_amlogic_aml,
2333 .model = 8726,
2334 .suffix = "-MX",
2335 },
2336 {
2337 /* "meson8" -> Amlogic S805 */
2338 .ro_board_platform = "meson8",
2339 .series = cpuinfo_arm_chipset_series_amlogic_s,
2340 .model = 805,
2341 },
2342 {
2343 /* "gxbaby" -> Amlogic S905 */
2344 .ro_board_platform = "gxbaby",
2345 .series = cpuinfo_arm_chipset_series_amlogic_s,
2346 .model = 905,
2347 },
2348 {
2349 /* "gxl" -> Amlogic S905X */
2350 .ro_board_platform = "gxl",
2351 .series = cpuinfo_arm_chipset_series_amlogic_s,
2352 .model = 905,
2353 .suffix = "X",
2354 },
2355 {
2356 /* "gxm" -> Amlogic S912 */
2357 .ro_board_platform = "gxm",
2358 .series = cpuinfo_arm_chipset_series_amlogic_s,
2359 .model = 912,
2360 },
2361};
2362
2363static const struct special_map_entry special_platform_map_entries[] = {
2364 {
2365 /* "hi6620oem" -> HiSilicon Kirin 910T */
2366 .platform = "hi6620oem",
2367 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2368 .model = 910,
2369 .suffix = 'T',
2370 },
2371 {
2372 /* "hi6250" -> HiSilicon Kirin 650 */
2373 .platform = "hi6250",
2374 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2375 .model = 650,
2376 },
2377 {
2378 /* "hi6210sft" -> HiSilicon Kirin 620 */
2379 .platform = "hi6210sft",
2380 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2381 .model = 620,
2382 },
2383 {
2384 /* "hi3650" -> HiSilicon Kirin 950 */
2385 .platform = "hi3650",
2386 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2387 .model = 950,
2388 },
2389 {
2390 /* "hi3635" -> HiSilicon Kirin 930 */
2391 .platform = "hi3635",
2392 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2393 .model = 930,
2394 },
2395 {
2396 /* "hi3630" -> HiSilicon Kirin 920 */
2397 .platform = "hi3630",
2398 .series = cpuinfo_arm_chipset_series_hisilicon_kirin,
2399 .model = 920,
2400 },
2401 {
2402 /* "k3v2oem1" -> HiSilicon K3V2 */
2403 .platform = "k3v2oem1",
2404 .series = cpuinfo_arm_chipset_series_hisilicon_k3v,
2405 .model = 2,
2406 },
2407 {
2408 /* "k3v200" -> HiSilicon K3V2 */
2409 .platform = "k3v200",
2410 .series = cpuinfo_arm_chipset_series_hisilicon_k3v,
2411 .model = 2,
2412 },
2413 {
2414 /* "montblanc" -> NovaThor U8500 */
2415 .platform = "montblanc",
2416 .series = cpuinfo_arm_chipset_series_novathor_u,
2417 .model = 8500,
2418 },
2419 {
2420 /* "song" -> Pinecone Surge S1 */
2421 .platform = "song",
2422 .series = cpuinfo_arm_chipset_series_pinecone_surge_s,
2423 .model = 1,
2424 },
2425 {
2426 /* "tegra132" -> nVidia Tegra T132 */
2427 .platform = "tegra132",
2428 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2429 .model = 132,
2430 },
2431 {
2432 /* "tegra210_dragon" -> nVidia Tegra T210 */
2433 .platform = "tegra210_dragon",
2434 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2435 .model = 210,
2436 },
2437 {
2438 /* "tegra4" -> nVidia Tegra T114 */
2439 .platform = "tegra4",
2440 .series = cpuinfo_arm_chipset_series_nvidia_tegra_t,
2441 .model = 114,
2442 },
2443 {
2444 /* "s5pc100" -> Samsung Exynos 3110 */
2445 .platform = "s5pc100",
2446 .series = cpuinfo_arm_chipset_series_samsung_exynos,
2447 .model = 3110,
2448 },
2449};
2450
2451/*
2452 * Decodes chipset name from ro.board.platform Android system property.
2453 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
2454 *
2455 * @param[in] platform - ro.board.platform value.
2456 * @param cores - number of cores in the chipset.
2457 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2458 *
2459 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2460 * and series identifiers.
2461 */
2462struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_board_platform(
2463 const char platform[restrict static CPUINFO_BUILD_PROP_VALUE_MAX],
2464 uint32_t cores, uint32_t max_cpu_freq_max)
2465{
2466 struct cpuinfo_arm_chipset chipset;
2467 const size_t platform_length = strnlen(platform, CPUINFO_BUILD_PROP_VALUE_MAX);
2468 const char* platform_end = platform + platform_length;
2469
2470 /* Check Qualcomm MSM/APQ signature */
2471 if (match_msm_apq(platform, platform_end, &chipset)) {
2472 cpuinfo_log_debug(
2473 "matched Qualcomm MSM/APQ signature in ro.board.platform string \"%.*s\"",
2474 (int) platform_length, platform);
2475 return chipset;
2476 }
2477
2478 /* Check exynosXXXX (Samsung Exynos) signature */
2479 if (match_exynos(platform, platform_end, &chipset)) {
2480 cpuinfo_log_debug(
2481 "matched exynosXXXX (Samsung Exynos) signature in ro.board.platform string \"%.*s\"",
2482 (int) platform_length, platform);
2483 return chipset;
2484 }
2485
2486 /* Check MediaTek MT signature */
2487 if (match_mt(platform, platform_end, true, &chipset)) {
2488 cpuinfo_log_debug(
2489 "matched MediaTek MT signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2490 return chipset;
2491 }
2492
2493 /* Check Spreadtrum SC signature */
2494 if (match_sc(platform, platform_end, &chipset)) {
2495 cpuinfo_log_debug(
2496 "matched Spreadtrum SC signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2497 return chipset;
2498 }
2499
2500 /* Check Rockchip RK signature */
2501 if (match_rk(platform, platform_end, &chipset)) {
2502 cpuinfo_log_debug(
2503 "matched Rockchip RK signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2504 return chipset;
2505 }
2506
2507 /* Check Leadcore LCxxxx signature */
2508 if (match_lc(platform, platform_end, &chipset)) {
2509 cpuinfo_log_debug(
2510 "matched Leadcore LC signature in ro.board.platform string \"%.*s\"", (int) platform_length, platform);
2511 return chipset;
2512 }
2513
2514 /* Compare to tabulated ro.board.platform values for Huawei devices which don't report chipset elsewhere */
2515 if (match_and_parse_huawei(platform, platform_end, &chipset)) {
2516 cpuinfo_log_debug(
2517 "found ro.board.platform string \"%.*s\" in Huawei chipset table",
2518 (int) platform_length, platform);
2519 return chipset;
2520 }
2521
2522 /*
2523 * Compare to known ro.board.platform values for Broadcom devices and
2524 * detect chipset from frequency and number of cores
2525 */
2526 if (match_and_parse_broadcom(platform, platform_end, cores, max_cpu_freq_max, &chipset)) {
2527 cpuinfo_log_debug(
2528 "found ro.board.platform string \"%.*s\" in Broadcom chipset table",
2529 (int) platform_length, platform);
2530 return chipset;
2531 }
2532
2533 /*
2534 * Compare to ro.board.platform value ("omap4") for OMAP4xxx chipsets.
2535 * Upon successful match, detect OMAP4430 from frequency and number of cores.
2536 */
2537 if (platform_length == 5 && cores == 2 && max_cpu_freq_max == 1008000 && memcmp(platform, "omap4", 5) == 0) {
2538 cpuinfo_log_debug(
2539 "matched Texas Instruments OMAP4 signature in ro.board.platform string \"%.*s\"",
2540 (int) platform_length, platform);
2541
2542 return (struct cpuinfo_arm_chipset) {
2543 .vendor = cpuinfo_arm_chipset_vendor_texas_instruments,
2544 .series = cpuinfo_arm_chipset_series_texas_instruments_omap,
2545 .model = 4430,
2546 };
2547 }
2548
2549 /*
2550 * Compare to tabulated ro.board.platform values for Amlogic chipsets/devices which can't be otherwise detected.
2551 * The tabulated Amlogic ro.board.platform values have not more than 6 characters.
2552 */
2553 if (platform_length <= 6) {
2554 for (size_t i = 0; i < CPUINFO_COUNT_OF(amlogic_map_entries); i++) {
2555 if (strncmp(amlogic_map_entries[i].ro_board_platform, platform, 6) == 0) {
2556 cpuinfo_log_debug(
2557 "found ro.board.platform string \"%.*s\" in Amlogic chipset table",
2558 (int) platform_length, platform);
2559 /* Create chipset name from entry */
2560 return (struct cpuinfo_arm_chipset) {
2561 .vendor = cpuinfo_arm_chipset_vendor_amlogic,
2562 .series = (enum cpuinfo_arm_chipset_series) amlogic_map_entries[i].series,
2563 .model = amlogic_map_entries[i].model,
2564 .suffix = {
2565 [0] = amlogic_map_entries[i].suffix[0],
2566 [1] = amlogic_map_entries[i].suffix[1],
2567 [2] = amlogic_map_entries[i].suffix[2],
2568 },
2569 };
2570 }
2571 }
2572 }
2573
2574 /* Compare to tabulated ro.board.platform values for popular chipsets/devices which can't be otherwise detected */
2575 for (size_t i = 0; i < CPUINFO_COUNT_OF(special_platform_map_entries); i++) {
2576 if (strncmp(special_platform_map_entries[i].platform, platform, platform_length) == 0 &&
2577 special_platform_map_entries[i].platform[platform_length] == 0)
2578 {
2579 /* Create chipset name from entry */
2580 cpuinfo_log_debug(
2581 "found ro.board.platform string \"%.*s\" in special chipset table", (int) platform_length, platform);
2582 return (struct cpuinfo_arm_chipset) {
2583 .vendor = chipset_series_vendor[special_platform_map_entries[i].series],
2584 .series = (enum cpuinfo_arm_chipset_series) special_platform_map_entries[i].series,
2585 .model = special_platform_map_entries[i].model,
2586 .suffix = {
2587 [0] = special_platform_map_entries[i].suffix,
2588 },
2589 };
2590 }
2591 }
2592
2593 /* None of the ro.board.platform signatures matched, indicate unknown chipset */
2594 return (struct cpuinfo_arm_chipset) {
2595 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2596 .series = cpuinfo_arm_chipset_series_unknown,
2597 };
2598}
2599
2600/*
2601 * Decodes chipset name from ro.mediatek.platform Android system property.
2602 *
2603 * @param[in] platform - ro.mediatek.platform value.
2604 *
2605 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2606 * and series identifiers.
2607 */
2608struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_mediatek_platform(
2609 const char platform[restrict static CPUINFO_BUILD_PROP_VALUE_MAX])
2610{
2611 struct cpuinfo_arm_chipset chipset;
2612 const char* platform_end = platform + strnlen(platform, CPUINFO_BUILD_PROP_VALUE_MAX);;
2613
2614 /* Check MediaTek MT signature */
2615 if (match_mt(platform, platform_end, false, &chipset)) {
2616 return chipset;
2617 }
2618
2619 return (struct cpuinfo_arm_chipset) {
2620 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2621 .series = cpuinfo_arm_chipset_series_unknown,
2622 };
2623}
2624
2625/*
2626 * Decodes chipset name from ro.chipname Android system property.
2627 *
2628 * @param[in] chipname - ro.chipname value.
2629 *
2630 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
2631 * and series identifiers.
2632 */
2633struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset_from_ro_chipname(
2634 const char chipname[restrict static CPUINFO_BUILD_PROP_VALUE_MAX])
2635{
2636 struct cpuinfo_arm_chipset chipset;
2637 const size_t chipname_length = strnlen(chipname, CPUINFO_BUILD_PROP_VALUE_MAX);
2638 const char* chipname_end = chipname + chipname_length;
2639
2640 /* Check Qualcomm MSM/APQ signatures */
2641 if (match_msm_apq(chipname, chipname_end, &chipset)) {
2642 cpuinfo_log_debug(
2643 "matched Qualcomm MSM/APQ signature in ro.chipname string \"%.*s\"",
2644 (int) chipname_length, chipname);
2645 return chipset;
2646 }
2647
2648 /* Check exynosXXXX (Samsung Exynos) signature */
2649 if (match_exynos(chipname, chipname_end, &chipset)) {
2650 cpuinfo_log_debug(
2651 "matched exynosXXXX (Samsung Exynos) signature in ro.chipname string \"%.*s\"",
2652 (int) chipname_length, chipname);
2653 return chipset;
2654 }
2655
2656 /* Check universalXXXX (Samsung Exynos) signature */
2657 if (match_universal(chipname, chipname_end, &chipset)) {
2658 cpuinfo_log_debug(
2659 "matched UNIVERSAL (Samsung Exynos) signature in ro.chipname Hardware string \"%.*s\"",
2660 (int) chipname_length, chipname);
2661 return chipset;
2662 }
2663
2664 /* Check MediaTek MT signature */
2665 if (match_mt(chipname, chipname_end, true, &chipset)) {
2666 cpuinfo_log_debug(
2667 "matched MediaTek MT signature in ro.chipname string \"%.*s\"",
2668 (int) chipname_length, chipname);
2669 return chipset;
2670 }
2671
2672 /* Check Spreadtrum SC signature */
2673 if (match_sc(chipname, chipname_end, &chipset)) {
2674 cpuinfo_log_debug(
2675 "matched Spreadtrum SC signature in ro.chipname string \"%.*s\"",
2676 (int) chipname_length, chipname);
2677 return chipset;
2678 }
2679
2680 /* Check Marvell PXA signature */
2681 if (match_pxa(chipname, chipname_end, &chipset)) {
2682 cpuinfo_log_debug(
2683 "matched Marvell PXA signature in ro.chipname string \"%.*s\"",
2684 (int) chipname_length, chipname);
2685 return chipset;
2686 }
2687
2688 /* Compare to ro.chipname value ("mp523x") for Renesas MP5232 which can't be otherwise detected */
2689 if (chipname_length == 6 && memcmp(chipname, "mp523x", 6) == 0) {
2690 cpuinfo_log_debug(
2691 "matched Renesas MP5232 signature in ro.chipname string \"%.*s\"",
2692 (int) chipname_length, chipname);
2693
2694 return (struct cpuinfo_arm_chipset) {
2695 .vendor = cpuinfo_arm_chipset_vendor_renesas,
2696 .series = cpuinfo_arm_chipset_series_renesas_mp,
2697 .model = 5232,
2698 };
2699 }
2700
2701 return (struct cpuinfo_arm_chipset) {
2702 .vendor = cpuinfo_arm_chipset_vendor_unknown,
2703 .series = cpuinfo_arm_chipset_series_unknown,
2704 };
2705}
2706
2707/*
2708 * Fix common bugs, typos, and renames in chipset name.
2709 *
2710 * @param[in,out] chipset - chipset name to fix.
2711 * @param cores - number of cores in the chipset.
2712 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
2713 */
2714void cpuinfo_arm_fixup_chipset(
2715 struct cpuinfo_arm_chipset chipset[restrict static 1], uint32_t cores, uint32_t max_cpu_freq_max)
2716{
2717 switch (chipset->series) {
2718 case cpuinfo_arm_chipset_series_qualcomm_msm:
2719 /* Check if there is suffix */
2720 if (chipset->suffix[0] == 0) {
2721 /* No suffix, but the model may be misreported */
2722 switch (chipset->model) {
2723 case 8216:
2724 /* MSM8216 was renamed to MSM8916 */
2725 cpuinfo_log_info("reinterpreted MSM8216 chipset as MSM8916");
2726 chipset->model = 8916;
2727 break;
2728 case 8916:
2729 /* Common bug: MSM8939 (Octa-core) reported as MSM8916 (Quad-core) */
2730 switch (cores) {
2731 case 4:
2732 break;
2733 case 8:
2734 cpuinfo_log_info("reinterpreted MSM8916 chipset with 8 cores as MSM8939");
2735 chipset->model = 8939;
2736 break;
2737 default:
2738 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2739 cores, chipset->model);
2740 chipset->model = 0;
2741 }
2742 break;
2743 case 8937:
2744 /* Common bug: MSM8917 (Quad-core) reported as MSM8937 (Octa-core) */
2745 switch (cores) {
2746 case 4:
2747 cpuinfo_log_info("reinterpreted MSM8937 chipset with 4 cores as MSM8917");
2748 chipset->model = 8917;
2749 break;
2750 case 8:
2751 break;
2752 default:
2753 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2754 cores, chipset->model);
2755 chipset->model = 0;
2756 }
2757 break;
2758 case 8960:
2759 /* Common bug: APQ8064 (Quad-core) reported as MSM8960 (Dual-core) */
2760 switch (cores) {
2761 case 2:
2762 break;
2763 case 4:
2764 cpuinfo_log_info("reinterpreted MSM8960 chipset with 4 cores as APQ8064");
2765 chipset->series = cpuinfo_arm_chipset_series_qualcomm_apq;
2766 chipset->model = 8064;
2767 break;
2768 default:
2769 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2770 cores, chipset->model);
2771 chipset->model = 0;
2772 }
2773 break;
2774 case 8996:
2775 /* Common bug: MSM8994 (Octa-core) reported as MSM8996 (Quad-core) */
2776 switch (cores) {
2777 case 4:
2778 break;
2779 case 8:
2780 cpuinfo_log_info("reinterpreted MSM8996 chipset with 8 cores as MSM8994");
2781 chipset->model = 8994;
2782 break;
2783 default:
2784 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2785 cores, chipset->model);
2786 chipset->model = 0;
2787 }
2788 break;
2789 case 8610:
2790 /* Common bug: MSM8212 (Quad-core) reported as MSM8610 (Dual-core) */
2791 switch (cores) {
2792 case 2:
2793 break;
2794 case 4:
2795 cpuinfo_log_info("reinterpreted MSM8610 chipset with 4 cores as MSM8212");
2796 chipset->model = 8212;
2797 break;
2798 default:
2799 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MSM%"PRIu32" chipset",
2800 cores, chipset->model);
2801 chipset->model = 0;
2802 }
2803 break;
2804 }
2805 } else {
2806 /* Suffix may need correction */
2807 const uint32_t suffix_word = load_u32le(chipset->suffix);
2808 if (suffix_word == UINT32_C(0x004D534D) /* "\0MSM" = reverse("MSM\0") */) {
2809 /*
2810 * Common bug: model name repeated twice, e.g. "MSM8916MSM8916"
2811 * In this case, model matching code parses the second "MSM" as a suffix
2812 */
2813 chipset->suffix[0] = 0;
2814 chipset->suffix[1] = 0;
2815 chipset->suffix[2] = 0;
2816 } else {
2817 switch (chipset->model) {
2818 case 8976:
2819 /* MSM8976SG -> MSM8976PRO */
2820 if (suffix_word == UINT32_C(0x00004753) /* "\0\0GS" = reverse("SG\0\0") */ ) {
2821 chipset->suffix[0] = 'P';
2822 chipset->suffix[1] = 'R';
2823 chipset->suffix[2] = 'O';
2824 }
2825 break;
2826 case 8996:
2827 /* MSM8996PRO -> MSM8996PRO-AB or MSM8996PRO-AC */
2828 if (suffix_word == UINT32_C(0x004F5250) /* "\0ORP" = reverse("PRO\0") */ ) {
2829 chipset->suffix[3] = '-';
2830 chipset->suffix[4] = 'A';
2831 chipset->suffix[5] = 'B' + (char) (max_cpu_freq_max >= 2188800);
2832 }
2833 break;
2834 }
2835 }
2836 }
2837 break;
2838 case cpuinfo_arm_chipset_series_qualcomm_apq:
2839 {
2840 /* Suffix may need correction */
2841 const uint32_t expected_apq = load_u32le(chipset->suffix);
2842 if (expected_apq == UINT32_C(0x00515041) /* "\0QPA" = reverse("APQ\0") */) {
2843 /*
2844 * Common bug: model name repeated twice, e.g. "APQ8016APQ8016"
2845 * In this case, model matching code parses the second "APQ" as a suffix
2846 */
2847 chipset->suffix[0] = 0;
2848 chipset->suffix[1] = 0;
2849 chipset->suffix[2] = 0;
2850 }
2851 break;
2852 }
2853 case cpuinfo_arm_chipset_series_samsung_exynos:
2854 if (chipset->model == 7580) {
2855 /* Common bug: Exynos 7578 (Quad-core) reported as Exynos 7580 (Octa-core) */
2856 switch (cores) {
2857 case 4:
2858 cpuinfo_log_info("reinterpreted Exynos 7580 chipset with 4 cores as Exynos 7578");
2859 chipset->model = 7578;
2860 break;
2861 case 8:
2862 break;
2863 default:
2864 cpuinfo_log_warning("system reported invalid %"PRIu32"-core Exynos 7580 chipset", cores);
2865 chipset->model = 0;
2866 }
2867 }
2868 break;
2869 case cpuinfo_arm_chipset_series_mediatek_mt:
2870 if (chipset->model == 6752) {
2871 /* Common bug: MT6732 (Quad-core) reported as MT6752 (Octa-core) */
2872 switch (cores) {
2873 case 4:
2874 cpuinfo_log_info("reinterpreted MT6752 chipset with 4 cores as MT6732");
2875 chipset->model = 6732;
2876 break;
2877 case 8:
2878 break;
2879 default:
2880 cpuinfo_log_warning("system reported invalid %"PRIu32"-core MT6752 chipset", cores);
2881 chipset->model = 0;
2882 }
2883 }
2884 if (chipset->suffix[0] == 'T') {
2885 /* Normalization: "TURBO" and "TRUBO" (apparently a typo) -> "T" */
2886 const uint32_t suffix_word = load_u32le(chipset->suffix + 1);
2887 switch (suffix_word) {
2888 case UINT32_C(0x4F425255): /* "OBRU" = reverse("URBO") */
2889 case UINT32_C(0x4F425552): /* "OBUR" = reverse("RUBO") */
2890 if (chipset->suffix[5] == 0) {
2891 chipset->suffix[1] = 0;
2892 chipset->suffix[2] = 0;
2893 chipset->suffix[3] = 0;
2894 chipset->suffix[4] = 0;
2895 }
2896 break;
2897 }
2898 }
2899 break;
2900 default:
2901 break;
2902 }
2903}
2904
2905/* Map from ARM chipset vendor ID to its string representation */
2906static const char* chipset_vendor_string[cpuinfo_arm_chipset_vendor_max] = {
2907 [cpuinfo_arm_chipset_vendor_unknown] = "Unknown",
2908 [cpuinfo_arm_chipset_vendor_qualcomm] = "Qualcomm",
2909 [cpuinfo_arm_chipset_vendor_mediatek] = "MediaTek",
2910 [cpuinfo_arm_chipset_vendor_samsung] = "Samsung",
2911 [cpuinfo_arm_chipset_vendor_hisilicon] = "HiSilicon",
2912 [cpuinfo_arm_chipset_vendor_actions] = "Actions",
2913 [cpuinfo_arm_chipset_vendor_allwinner] = "Allwinner",
2914 [cpuinfo_arm_chipset_vendor_amlogic] = "Amlogic",
2915 [cpuinfo_arm_chipset_vendor_broadcom] = "Broadcom",
2916 [cpuinfo_arm_chipset_vendor_lg] = "LG",
2917 [cpuinfo_arm_chipset_vendor_leadcore] = "Leadcore",
2918 [cpuinfo_arm_chipset_vendor_marvell] = "Marvell",
2919 [cpuinfo_arm_chipset_vendor_mstar] = "MStar",
2920 [cpuinfo_arm_chipset_vendor_novathor] = "NovaThor",
2921 [cpuinfo_arm_chipset_vendor_nvidia] = "nVidia",
2922 [cpuinfo_arm_chipset_vendor_pinecone] = "Pinecone",
2923 [cpuinfo_arm_chipset_vendor_renesas] = "Renesas",
2924 [cpuinfo_arm_chipset_vendor_rockchip] = "Rockchip",
2925 [cpuinfo_arm_chipset_vendor_spreadtrum] = "Spreadtrum",
2926 [cpuinfo_arm_chipset_vendor_telechips] = "Telechips",
2927 [cpuinfo_arm_chipset_vendor_texas_instruments] = "Texas Instruments",
2928 [cpuinfo_arm_chipset_vendor_wondermedia] = "WonderMedia",
2929};
2930
2931/* Map from ARM chipset series ID to its string representation */
2932static const char* chipset_series_string[cpuinfo_arm_chipset_series_max] = {
2933 [cpuinfo_arm_chipset_series_unknown] = NULL,
2934 [cpuinfo_arm_chipset_series_qualcomm_qsd] = "QSD",
2935 [cpuinfo_arm_chipset_series_qualcomm_msm] = "MSM",
2936 [cpuinfo_arm_chipset_series_qualcomm_apq] = "APQ",
2937 [cpuinfo_arm_chipset_series_qualcomm_snapdragon] = "Snapdragon ",
2938 [cpuinfo_arm_chipset_series_mediatek_mt] = "MT",
2939 [cpuinfo_arm_chipset_series_samsung_exynos] = "Exynos ",
2940 [cpuinfo_arm_chipset_series_hisilicon_k3v] = "K3V",
2941 [cpuinfo_arm_chipset_series_hisilicon_hi] = "Hi",
2942 [cpuinfo_arm_chipset_series_hisilicon_kirin] = "Kirin ",
2943 [cpuinfo_arm_chipset_series_actions_atm] = "ATM",
2944 [cpuinfo_arm_chipset_series_allwinner_a] = "A",
2945 [cpuinfo_arm_chipset_series_amlogic_aml] = "AML",
2946 [cpuinfo_arm_chipset_series_amlogic_s] = "S",
2947 [cpuinfo_arm_chipset_series_broadcom_bcm] = "BCM",
2948 [cpuinfo_arm_chipset_series_lg_nuclun] = "Nuclun ",
2949 [cpuinfo_arm_chipset_series_leadcore_lc] = "LC",
2950 [cpuinfo_arm_chipset_series_marvell_pxa] = "PXA",
2951 [cpuinfo_arm_chipset_series_mstar_6a] = "6A",
2952 [cpuinfo_arm_chipset_series_novathor_u] = "U",
2953 [cpuinfo_arm_chipset_series_nvidia_tegra_t] = "Tegra T",
2954 [cpuinfo_arm_chipset_series_nvidia_tegra_ap] = "Tegra AP",
2955 [cpuinfo_arm_chipset_series_nvidia_tegra_sl] = "Tegra SL",
2956 [cpuinfo_arm_chipset_series_pinecone_surge_s] = "Surge S",
2957 [cpuinfo_arm_chipset_series_renesas_mp] = "MP",
2958 [cpuinfo_arm_chipset_series_rockchip_rk] = "RK",
2959 [cpuinfo_arm_chipset_series_spreadtrum_sc] = "SC",
2960 [cpuinfo_arm_chipset_series_telechips_tcc] = "TCC",
2961 [cpuinfo_arm_chipset_series_texas_instruments_omap] = "OMAP",
2962 [cpuinfo_arm_chipset_series_wondermedia_wm] = "WM",
2963};
2964
2965/* Convert chipset name represented by cpuinfo_arm_chipset structure to a string representation */
2966void cpuinfo_arm_chipset_to_string(
2967 const struct cpuinfo_arm_chipset chipset[restrict static 1],
2968 char name[restrict static CPUINFO_ARM_CHIPSET_NAME_MAX])
2969{
2970 enum cpuinfo_arm_chipset_vendor vendor = chipset->vendor;
2971 if (vendor >= cpuinfo_arm_chipset_vendor_max) {
2972 vendor = cpuinfo_arm_chipset_vendor_unknown;
2973 }
2974 enum cpuinfo_arm_chipset_series series = chipset->series;
2975 if (series >= cpuinfo_arm_chipset_series_max) {
2976 series = cpuinfo_arm_chipset_series_unknown;
2977 }
2978 const char* vendor_string = chipset_vendor_string[vendor];
2979 const char* series_string = chipset_series_string[series];
2980 const uint32_t model = chipset->model;
2981 if (model == 0) {
2982 if (series == cpuinfo_arm_chipset_series_unknown) {
2983 strncpy(name, vendor_string, CPUINFO_ARM_CHIPSET_NAME_MAX);
2984 } else {
2985 snprintf(name, CPUINFO_ARM_CHIPSET_NAME_MAX,
2986 "%s %s", vendor_string, series_string);
2987 }
2988 } else {
2989 const size_t suffix_length = strnlen(chipset->suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX);
2990 snprintf(name, CPUINFO_ARM_CHIPSET_NAME_MAX,
2991 "%s %s%"PRIu32"%.*s", vendor_string, series_string, model, (int) suffix_length, chipset->suffix);
2992 }
2993}
2994
2995static inline struct cpuinfo_arm_chipset disambiguate_qualcomm_chipset(
2996 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
2997 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
2998 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1],
2999 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3000{
3001 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3002 return *ro_chipname_chipset;
3003 }
3004 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3005 return *proc_cpuinfo_hardware_chipset;
3006 }
3007 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3008 return *ro_product_board_chipset;
3009 }
3010 return *ro_board_platform_chipset;
3011}
3012
3013static inline struct cpuinfo_arm_chipset disambiguate_mediatek_chipset(
3014 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3015 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3016 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1],
3017 const struct cpuinfo_arm_chipset ro_mediatek_platform_chipset[restrict static 1],
3018 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3019{
3020 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3021 return *ro_chipname_chipset;
3022 }
3023 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3024 return *proc_cpuinfo_hardware_chipset;
3025 }
3026 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3027 return *ro_product_board_chipset;
3028 }
3029 if (ro_board_platform_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3030 return *ro_board_platform_chipset;
3031 }
3032 return *ro_mediatek_platform_chipset;
3033}
3034
3035static inline struct cpuinfo_arm_chipset disambiguate_hisilicon_chipset(
3036 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3037 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3038 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1])
3039{
3040 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3041 return *proc_cpuinfo_hardware_chipset;
3042 }
3043 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3044 return *ro_product_board_chipset;
3045 }
3046 return *ro_board_platform_chipset;
3047}
3048
3049static inline struct cpuinfo_arm_chipset disambiguate_amlogic_chipset(
3050 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3051 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1])
3052{
3053 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3054 return *proc_cpuinfo_hardware_chipset;
3055 }
3056 return *ro_board_platform_chipset;
3057}
3058
3059static inline struct cpuinfo_arm_chipset disambiguate_marvell_chipset(
3060 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3061 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3062 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3063{
3064 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3065 return *ro_chipname_chipset;
3066 }
3067 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3068 return *ro_product_board_chipset;
3069 }
3070 return *proc_cpuinfo_hardware_chipset;
3071}
3072
3073static inline struct cpuinfo_arm_chipset disambiguate_rockchip_chipset(
3074 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3075 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3076 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1])
3077{
3078 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3079 return *ro_product_board_chipset;
3080 }
3081 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3082 return *proc_cpuinfo_hardware_chipset;
3083 }
3084 return *ro_board_platform_chipset;
3085}
3086
3087static inline struct cpuinfo_arm_chipset disambiguate_spreadtrum_chipset(
3088 const struct cpuinfo_arm_chipset proc_cpuinfo_hardware_chipset[restrict static 1],
3089 const struct cpuinfo_arm_chipset ro_product_board_chipset[restrict static 1],
3090 const struct cpuinfo_arm_chipset ro_board_platform_chipset[restrict static 1],
3091 const struct cpuinfo_arm_chipset ro_chipname_chipset[restrict static 1])
3092{
3093 if (ro_chipname_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3094 return *ro_chipname_chipset;
3095 }
3096 if (ro_product_board_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3097 return *ro_product_board_chipset;
3098 }
3099 if (proc_cpuinfo_hardware_chipset->series != cpuinfo_arm_chipset_series_unknown) {
3100 return *proc_cpuinfo_hardware_chipset;
3101 }
3102 return *ro_board_platform_chipset;
3103}
3104
3105/*
3106 * Decodes chipset name from Android system properties:
3107 * - /proc/cpuinfo Hardware string
3108 * - ro.product.board
3109 * - ro.board.platform
3110 * - ro.mediatek.platform
3111 * - ro.chipname
3112 * For some chipsets, the function relies frequency and on number of cores for chipset detection.
3113 *
3114 * @param[in] properties - structure with the Android system properties described above.
3115 * @param cores - number of cores in the chipset.
3116 * @param max_cpu_freq_max - maximum of /sys/devices/system/cpu/cpu<number>/cpofreq/cpu_freq_max values.
3117 *
3118 * @returns Decoded chipset name. If chipset could not be decoded, the resulting structure would use `unknown` vendor
3119 * and series identifiers.
3120 */
3121struct cpuinfo_arm_chipset cpuinfo_arm_android_decode_chipset(
3122 const struct cpuinfo_android_properties properties[restrict static 1],
3123 uint32_t cores,
3124 uint32_t max_cpu_freq_max)
3125{
3126 struct cpuinfo_arm_chipset chipset = {
3127 .vendor = cpuinfo_arm_chipset_vendor_unknown,
3128 .series = cpuinfo_arm_chipset_series_unknown,
3129 };
3130
3131 const bool tegra_platform = is_tegra(
3132 properties->ro_board_platform,
3133 properties->ro_board_platform + strnlen(properties->ro_board_platform, CPUINFO_BUILD_PROP_VALUE_MAX));
3134
3135 struct cpuinfo_arm_chipset chipsets[cpuinfo_android_chipset_property_max] = {
3136 [cpuinfo_android_chipset_property_proc_cpuinfo_hardware] =
3137 cpuinfo_arm_android_decode_chipset_from_proc_cpuinfo_hardware(
3138 properties->proc_cpuinfo_hardware, cores, max_cpu_freq_max, tegra_platform),
3139 [cpuinfo_android_chipset_property_ro_product_board] =
3140 cpuinfo_arm_android_decode_chipset_from_ro_product_board(
3141 properties->ro_product_board, cores, max_cpu_freq_max),
3142 [cpuinfo_android_chipset_property_ro_board_platform] =
3143 cpuinfo_arm_android_decode_chipset_from_ro_board_platform(
3144 properties->ro_board_platform, cores, max_cpu_freq_max),
3145 [cpuinfo_android_chipset_property_ro_mediatek_platform] =
3146 cpuinfo_arm_android_decode_chipset_from_ro_mediatek_platform(properties->ro_mediatek_platform),
3147 [cpuinfo_android_chipset_property_ro_chipname] =
3148 cpuinfo_arm_android_decode_chipset_from_ro_chipname(properties->ro_chipname),
3149 };
3150 enum cpuinfo_arm_chipset_vendor vendor = cpuinfo_arm_chipset_vendor_unknown;
3151 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3152 const enum cpuinfo_arm_chipset_vendor decoded_vendor = chipsets[i].vendor;
3153 if (decoded_vendor != cpuinfo_arm_chipset_vendor_unknown) {
3154 if (vendor == cpuinfo_arm_chipset_vendor_unknown) {
3155 vendor = decoded_vendor;
3156 } else if (vendor != decoded_vendor) {
3157 /* Parsing different system properties produces different chipset vendors. This situation is rare. */
3158 cpuinfo_log_error(
3159 "chipset detection failed: different chipset vendors reported in different system properties");
3160 goto finish;
3161 }
3162 }
3163 }
3164 if (vendor == cpuinfo_arm_chipset_vendor_unknown) {
3165 cpuinfo_log_warning(
3166 "chipset detection failed: none of the system properties matched known signatures");
3167 goto finish;
3168 }
3169
3170 /* Fix common bugs in reported chipsets */
3171 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3172 cpuinfo_arm_fixup_chipset(&chipsets[i], cores, max_cpu_freq_max);
3173 }
3174
3175 /*
3176 * Propagate suffixes: consider all pairs of chipsets, if both chipsets in the pair are from the same series,
3177 * and one's suffix is a prefix of another's chipset suffix, use the longest suffix.
3178 */
3179 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3180 const size_t chipset_i_suffix_length = strnlen(chipsets[i].suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX);
3181 for (size_t j = 0; j < i; j++) {
3182 if (chipsets[i].series == chipsets[j].series) {
3183 const size_t chipset_j_suffix_length = strnlen(chipsets[j].suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX);
3184 if (chipset_i_suffix_length != chipset_j_suffix_length) {
3185 const size_t common_prefix_length = (chipset_i_suffix_length < chipset_j_suffix_length) ?
3186 chipset_i_suffix_length : chipset_j_suffix_length;
3187 if (common_prefix_length == 0 ||
3188 memcmp(chipsets[i].suffix, chipsets[j].suffix, common_prefix_length) == 0)
3189 {
3190 if (chipset_i_suffix_length > chipset_j_suffix_length) {
3191 memcpy(chipsets[j].suffix, chipsets[i].suffix, chipset_i_suffix_length);
3192 } else {
3193 memcpy(chipsets[i].suffix, chipsets[j].suffix, chipset_j_suffix_length);
3194 }
3195 }
3196 }
3197 }
3198 }
3199 }
3200
3201 for (size_t i = 0; i < cpuinfo_android_chipset_property_max; i++) {
3202 if (chipsets[i].series != cpuinfo_arm_chipset_series_unknown) {
3203 if (chipset.series == cpuinfo_arm_chipset_series_unknown) {
3204 chipset = chipsets[i];
3205 } else if (chipsets[i].series != chipset.series || chipsets[i].model != chipset.model ||
3206 strncmp(chipsets[i].suffix, chipset.suffix, CPUINFO_ARM_CHIPSET_SUFFIX_MAX) != 0)
3207 {
3208 cpuinfo_log_info(
3209 "different chipsets reported in different system properties; "
3210 "vendor-specific disambiguation heuristic would be used");
3211 switch (vendor) {
3212 case cpuinfo_arm_chipset_vendor_qualcomm:
3213 return disambiguate_qualcomm_chipset(
3214 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3215 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3216 &chipsets[cpuinfo_android_chipset_property_ro_board_platform],
3217 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3218 case cpuinfo_arm_chipset_vendor_mediatek:
3219 return disambiguate_mediatek_chipset(
3220 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3221 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3222 &chipsets[cpuinfo_android_chipset_property_ro_board_platform],
3223 &chipsets[cpuinfo_android_chipset_property_ro_mediatek_platform],
3224 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3225 case cpuinfo_arm_chipset_vendor_hisilicon:
3226 return disambiguate_hisilicon_chipset(
3227 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3228 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3229 &chipsets[cpuinfo_android_chipset_property_ro_board_platform]);
3230 case cpuinfo_arm_chipset_vendor_amlogic:
3231 return disambiguate_amlogic_chipset(
3232 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3233 &chipsets[cpuinfo_android_chipset_property_ro_board_platform]);
3234 case cpuinfo_arm_chipset_vendor_marvell:
3235 return disambiguate_marvell_chipset(
3236 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3237 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3238 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3239 case cpuinfo_arm_chipset_vendor_rockchip:
3240 return disambiguate_rockchip_chipset(
3241 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3242 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3243 &chipsets[cpuinfo_android_chipset_property_ro_board_platform]);
3244 case cpuinfo_arm_chipset_vendor_spreadtrum:
3245 return disambiguate_spreadtrum_chipset(
3246 &chipsets[cpuinfo_android_chipset_property_proc_cpuinfo_hardware],
3247 &chipsets[cpuinfo_android_chipset_property_ro_product_board],
3248 &chipsets[cpuinfo_android_chipset_property_ro_board_platform],
3249 &chipsets[cpuinfo_android_chipset_property_ro_chipname]);
3250 default:
3251 cpuinfo_log_error(
3252 "chipset detection failed: "
3253 "could not disambiguate different chipsets reported in different system properties");
3254 /* chipset variable contains valid, but inconsistent chipset information, overwrite it */
3255 chipset = (struct cpuinfo_arm_chipset) {
3256 .vendor = cpuinfo_arm_chipset_vendor_unknown,
3257 .series = cpuinfo_arm_chipset_series_unknown,
3258 };
3259 goto finish;
3260 }
3261 }
3262 }
3263 }
3264
3265finish:
3266 return chipset;
3267}