From 71d660d7b17bc3b066c14eaa3a23555284b585d6 Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Tue, 24 Nov 2020 10:03:21 +0100 Subject: [PATCH] [v0.88][ARM] Fetch raw soc name from cpuinfo or android (if supported) --- src/arm/soc.c | 59 ++++++++++++++++++++++++++++++++--------------- src/common/udev.c | 33 ++++++++++++++++++++++++++ src/common/udev.h | 1 + 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/src/arm/soc.c b/src/arm/soc.c index f55687e..48d4b4b 100644 --- a/src/arm/soc.c +++ b/src/arm/soc.c @@ -3,6 +3,8 @@ #include #include "soc.h" +#include "../common/udev.h" +#include "../common/global.h" #define STRING_UNKNOWN "Unknown" @@ -13,40 +15,61 @@ static inline int android_property_get(const char* key, char* value) { return __system_property_get(key, value); } -char* android_guess_soc() { - char* raw_name = NULL; +struct system_on_chip* guess_soc_from_android() { + struct system_on_chip* soc = NULL; char tmp[100]; int property_len = 0; property_len = android_property_get("ro.mediatek.platform", (char *) &tmp); if(property_len > 0) { - raw_name = malloc(sizeof(char) * (property_len + 1)); - strcpy(raw_name, tmp); - raw_name[property_len] = '\0'; - return raw_name; + soc = malloc(sizeof(struct system_on_chip)); + soc->raw_name = malloc(sizeof(char) * (property_len + 1)); + strncpy(soc->raw_name, tmp, property_len + 1); + soc->raw_name[property_len] = '\0'; + return soc; } property_len = android_property_get("ro.product.board", (char *) &tmp); if(property_len > 0) { - raw_name = malloc(sizeof(char) * (property_len + 1)); - strcpy(raw_name, tmp); - raw_name[property_len] = '\0'; - return raw_name; + soc = malloc(sizeof(struct system_on_chip)); + soc->raw_name = malloc(sizeof(char) * (property_len + 1)); + strncpy(soc->raw_name, tmp, property_len + 1); + soc->raw_name[property_len] = '\0'; + return soc; } - return NULL; + return soc; } #endif -struct system_on_chip* get_soc() { - struct system_on_chip* soc = malloc(sizeof(struct system_on_chip)); - soc->raw_name = NULL; +struct system_on_chip* guess_soc_from_cpuinfo() { + struct system_on_chip* soc = NULL; -#ifdef __ANDROID__ - soc->raw_name = android_guess_soc(); -#endif + char* tmp = get_hardware_from_cpuinfo(&strlen); + if(tmp != NULL) { + soc = malloc(sizeof(struct system_on_chip)); + soc->raw_name = tmp; + } + + return soc; +} - if(soc->raw_name == NULL) { +struct system_on_chip* get_soc() { + struct system_on_chip* soc = NULL; + + soc = guess_soc_from_cpuinfo(); + if(soc == NULL) { + printWarn("SoC detection failed using /proc/cpuinfo"); +#ifdef __ANDROID__ + soc = guess_soc_from_android(); + if(soc == NULL) { + printWarn("SoC detection failed using Android"); + } +#endif + } + + if(soc == NULL) { + soc = malloc(sizeof(struct system_on_chip)); soc->raw_name = malloc(sizeof(char) * (strlen(STRING_UNKNOWN)+1)); snprintf(soc->raw_name, strlen(STRING_UNKNOWN)+1, STRING_UNKNOWN); } diff --git a/src/common/udev.c b/src/common/udev.c index 541e271..d790d05 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -96,6 +96,7 @@ long get_min_freq_from_file(uint32_t core) { #define CPUINFO_CPU_VARIANT_STR "CPU variant\t: " #define CPUINFO_CPU_PART_STR "CPU part\t: " #define CPUINFO_CPU_REVISION_STR "CPU revision\t: " +#define CPUINFO_HARDWARE_STR "Hardware\t: " #define CPUINFO_CPU_STRING "processor" @@ -250,4 +251,36 @@ uint32_t get_midr_from_cpuinfo(uint32_t core, bool* success) { return midr; } +char* get_hardware_from_cpuinfo() { + int fd = open(_PATH_CPUINFO, O_RDONLY); + + if(fd == -1) { + perror("open"); + return NULL; + } + + //File exists, read it + int bytes_read = 0; + int offset = 0; + int block = 128; + char* buf = malloc(sizeof(char)*DEFAULT_FILE_SIZE); + memset(buf, 0, sizeof(char)*DEFAULT_FILE_SIZE); + + while ( (bytes_read = read(fd, buf+offset, block)) > 0 ) { + offset += bytes_read; + } + + char* tmp1 = strstr(buf, CPUINFO_HARDWARE_STR); + if(tmp1 == NULL) return NULL; + tmp1 = tmp1 + strlen(CPUINFO_HARDWARE_STR); + char* tmp2 = strstr(tmp1, "\n"); + + int strlen = (1 + (tmp2-tmp1)); + char* hardware = malloc(sizeof(char) * strlen); + memset(hardware, 0, sizeof(char) * strlen); + strncpy(hardware, tmp1, tmp2-tmp1); + + return hardware; +} + #endif /* ARCH_ARM */ diff --git a/src/common/udev.h b/src/common/udev.h index 6fea39c..cacae59 100644 --- a/src/common/udev.h +++ b/src/common/udev.h @@ -11,6 +11,7 @@ long get_min_freq_from_file(uint32_t core); #define UNKNOWN -1 int get_ncores_from_cpuinfo(); uint32_t get_midr_from_cpuinfo(uint32_t core, bool* success); +char* get_hardware_from_cpuinfo(); #endif #endif