From 5c69b8405bd3e9289d6b77b2eacdfceb76e4cf82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 6 Sep 2012 22:00:51 +0200 Subject: [PATCH] Added fallback model string parsing for Intel CPUs. * When we do not have a predefined model string, we now try to parse the reported model string into something that is at least usable, and should look comparable to what we have now. * For models where the parsed type string is acceptable, we could remove the predefined ones. --- headers/private/shared/cpu_type.h | 62 ++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/headers/private/shared/cpu_type.h b/headers/private/shared/cpu_type.h index 2c1f7c6f77..7a4f7f6d01 100644 --- a/headers/private/shared/cpu_type.h +++ b/headers/private/shared/cpu_type.h @@ -1,5 +1,5 @@ /* - * Copyright 2004-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2004-2012, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ @@ -12,6 +12,7 @@ #include + #ifdef __cplusplus extern "C" { #endif @@ -27,6 +28,58 @@ int32 get_rounded_cpu_speed(void); #endif +#if __INTEL__ +/*! Tries to parse an Intel CPU ID string to match our usual naming scheme. + Note, this function is not thread safe, and must only be called once + at a time. +*/ +static const char* +parse_intel(const char* name) +{ + static char buffer[49]; + + // ignore initial spaces + int index = 0; + for (; name[index] != '\0'; index++) { + if (name[index] != ' ') + break; + } + + // ignore vendor + for (; name[index] != '\0'; index++) { + if (name[index] == ' ') { + index++; + break; + } + } + + // parse model + int outIndex = 0; + for (; name[index] != '\0'; index++) { + if (!strncmp(&name[index], "(R)", 3)) { + outIndex += strlcpy(&buffer[outIndex], "®", + sizeof(buffer) - outIndex); + index += 2; + } else if (!strncmp(&name[index], "(TM)", 4)) { + outIndex += strlcpy(&buffer[outIndex], "™", + sizeof(buffer) - outIndex); + index += 3; + } else if (!strncmp(&name[index], " CPU", 4)) { + // Cut out the CPU string + index += 3; + } else if (!strncmp(&name[index], " @", 2)) { + // Cut off the remainder + break; + } else + buffer[outIndex++] = name[index]; + } + + buffer[outIndex] = '\0'; + return buffer; +} +#endif + + const char * get_cpu_vendor_string(enum cpu_types type) { @@ -61,7 +114,7 @@ get_cpu_vendor_string(enum cpu_types type) #ifdef __INTEL__ -/* Parameter 'name' needs to point to an allocated array of 49 characters. */ +/*! Parameter 'name' needs to point to an allocated array of 49 characters. */ void get_cpuid_model_string(char *name) { @@ -345,6 +398,11 @@ get_cpu_model_string(system_info *info) #endif /* __INTEL__ */ default: + if ((info->cpu_type & B_CPU_x86_VENDOR_MASK) == B_CPU_INTEL_x86) { + // Fallback to manual parsing of the model string + get_cpuid_model_string(cpuidName); + return parse_intel(cpuidName); + } return NULL; } }