diff options
author | Burt P <pburt0@gmail.com> | 2019-06-30 18:14:51 -0500 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2019-07-02 17:36:25 -0700 |
commit | fa17fdf3cc463c990a1e84150c8766ec49a9c6ce (patch) | |
tree | b711bd37176156e49e41f27e2538b41d7e0306e0 | |
parent | d7750592038e79a602c329a42a813fa098e5f6ad (diff) |
dmi_util/memory devices: fixes for 32-bit systems
Signed-off-by: Burt P <pburt0@gmail.com>
-rw-r--r-- | hardinfo/dmi_util.c | 32 | ||||
-rw-r--r-- | includes/dmi_util.h | 15 | ||||
-rw-r--r-- | modules/devices/dmi_memory.c | 122 | ||||
-rw-r--r-- | modules/devices/spd-decode.c | 12 |
4 files changed, 93 insertions, 88 deletions
diff --git a/hardinfo/dmi_util.c b/hardinfo/dmi_util.c index 627d1cd0..c82e86b4 100644 --- a/hardinfo/dmi_util.c +++ b/hardinfo/dmi_util.c @@ -239,7 +239,7 @@ static char *dd_cache[128] = {}; void dmidecode_cache_free() { int i; for(i = 0; i < 128; i++) g_free(dd_cache[i]); } -char *dmidecode_read(const unsigned long *dmi_type) { +char *dmidecode_read(const dmi_type *type) { gchar *ret = NULL; gchar full_path[PATH_MAX]; gboolean spawned; @@ -247,10 +247,10 @@ char *dmidecode_read(const unsigned long *dmi_type) { int i = 0; - if (dmi_type) { - if (dd_cache[*dmi_type]) - return g_strdup(dd_cache[*dmi_type]); - snprintf(full_path, PATH_MAX, "dmidecode -t %lu", *dmi_type); + if (type) { + if (dd_cache[*type]) + return g_strdup(dd_cache[*type]); + snprintf(full_path, PATH_MAX, "dmidecode -t %"PRId32, *type); } else { if (dd_cache[127]) return g_strdup(dd_cache[127]); @@ -269,8 +269,8 @@ char *dmidecode_read(const unsigned long *dmi_type) { } if (ret) { - if (*dmi_type) - dd_cache[*dmi_type] = g_strdup(ret); + if (*type) + dd_cache[*type] = g_strdup(ret); else dd_cache[127] = g_strdup(ret); } @@ -278,25 +278,25 @@ char *dmidecode_read(const unsigned long *dmi_type) { return ret; } -dmi_handle_list *dmi_handle_list_add(dmi_handle_list *hl, unsigned int new_handle) { +dmi_handle_list *dmi_handle_list_add(dmi_handle_list *hl, dmi_handle new_handle) { if (!hl) { hl = malloc(sizeof(dmi_handle_list)); hl->count = 1; - hl->handles = malloc(sizeof(unsigned long) * hl->count); + hl->handles = malloc(sizeof(dmi_handle) * hl->count); } else { hl->count++; - hl->handles = realloc(hl->handles, sizeof(unsigned long) * hl->count); + hl->handles = realloc(hl->handles, sizeof(dmi_handle) * hl->count); } hl->handles[hl->count - 1] = new_handle; return hl; } -dmi_handle_list *dmidecode_handles(const unsigned long *dmi_type) { +dmi_handle_list *dmidecode_handles(const dmi_type *type) { gchar *full = NULL, *p = NULL, *next_nl = NULL; dmi_handle_list *hl = NULL; unsigned int ch = 0; - full = dmidecode_read(dmi_type); + full = dmidecode_read(type); if (full) { p = full; while(next_nl = strchr(p, '\n')) { @@ -317,7 +317,7 @@ void dmi_handle_list_free(dmi_handle_list *hl) { free(hl); } -char *dmidecode_match(const char *name, const unsigned long *dmi_type, const unsigned long *handle) { +char *dmidecode_match(const char *name, const dmi_type *type, const dmi_handle *handle) { gchar *ret = NULL, *full = NULL, *p = NULL, *next_nl = NULL; unsigned int ch = 0; int ln = 0; @@ -325,7 +325,7 @@ char *dmidecode_match(const char *name, const unsigned long *dmi_type, const uns if (!name) return NULL; ln = strlen(name); - full = dmidecode_read(dmi_type); + full = dmidecode_read(type); if (full) { p = full; while(next_nl = strchr(p, '\n')) { @@ -351,7 +351,7 @@ char *dmidecode_match(const char *name, const unsigned long *dmi_type, const uns return ret; } -dmi_handle_list *dmidecode_match_value(const char *name, const char *value, const unsigned long *dmi_type) { +dmi_handle_list *dmidecode_match_value(const char *name, const char *value, const dmi_type *type) { dmi_handle_list *hl = NULL; gchar *full = NULL, *p = NULL, *next_nl = NULL; unsigned int ch = 0; @@ -361,7 +361,7 @@ dmi_handle_list *dmidecode_match_value(const char *name, const char *value, cons ln = strlen(name); lnv = (value) ? strlen(value) : 0; - full = dmidecode_read(dmi_type); + full = dmidecode_read(type); if (full) { p = full; while(next_nl = strchr(p, '\n')) { diff --git a/includes/dmi_util.h b/includes/dmi_util.h index 3b6fd3f4..d557d58b 100644 --- a/includes/dmi_util.h +++ b/includes/dmi_util.h @@ -21,6 +21,11 @@ #ifndef __DMI_UTIL_H__ #define __DMI_UTIL_H__ +#include <inttypes.h> + +typedef uint32_t dmi_handle; +typedef uint32_t dmi_type; + /* -1 = yes, but will be ignored * 0 = no * 1 = yes */ @@ -34,22 +39,22 @@ char *dmi_get_str_abs(const char *id_str); /* include nonsense */ char *dmi_chassis_type_str(int chassis_type, gboolean with_val); typedef struct { - unsigned long count; - unsigned long *handles; + uint32_t count; + dmi_handle *handles; } dmi_handle_list; /* get a list of handles that match a dmi_type */ -dmi_handle_list *dmidecode_handles(const unsigned long *dmi_type); +dmi_handle_list *dmidecode_handles(const dmi_type *type); void dmi_handle_list_free(dmi_handle_list *hl); /* get a list of handles which have a name, and/or optional value, and/or limit to an optional dmi_type * dmidecode_match_value("Name", NULL, NULL) : all dmi handles with an item called "Name" * dmidecode_match_value("Name", "Value", NULL) : all dmi_handles with an item called "Name" that has a value of "Value" * dmidecode_match_value("Name", "Value", &dt) : all dmi_handles with "Name: Value" that are of type stored in dt */ -dmi_handle_list *dmidecode_match_value(const char *name, const char *value, const unsigned long *dmi_type); +dmi_handle_list *dmidecode_match_value(const char *name, const char *value, const dmi_type *type); /* get the first value for name, limiting to optional dmi_type and/or optional handle */ -char *dmidecode_match(const char *name, const unsigned long *dmi_type, const unsigned long *handle); +char *dmidecode_match(const char *name, const dmi_type *type, const dmi_handle *handle); void dmidecode_cache_free(); diff --git a/modules/devices/dmi_memory.c b/modules/devices/dmi_memory.c index 057a00b5..182f520c 100644 --- a/modules/devices/dmi_memory.c +++ b/modules/devices/dmi_memory.c @@ -25,6 +25,8 @@ #include "dt_util.h" /* for appf() */ #define dmi_spd_msg(...) /* fprintf (stderr, __VA_ARGS__) */ +typedef uint64_t dmi_mem_size; + #include "spd-decode.c" gboolean no_handles = FALSE; @@ -37,8 +39,8 @@ static const char empty_mem_str[] = "No Module Installed"; static const char unknown_mfgr_str[] = "<BAD INDEX>"; static const char mobo_location[] = "System Board Or Motherboard"; static const char mobo_shorter[] = "Mainboard"; -static const unsigned long dta = 16; /* array */ -static const unsigned long dtm = 17; /* socket */ +static const dmi_type dta = 16; /* array */ +static const dmi_type dtm = 17; /* socket */ #define UNKIFNULL2(f) ((f) ? f : _("(Unknown)")) #define UNKIFEMPTY2(f) ((*f) ? f : _("(Unknown)")) @@ -54,20 +56,46 @@ const char *problem_marker() { return as_text; } +dmi_mem_size dmi_read_memory_str_to_MiB(const char *memstr) { + dmi_mem_size ret = 0, v = 0; + char l[6] = ""; + /* dmidecode units: "bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB" */ + int mc = sscanf(memstr, "%"PRId64" %6s", &v, l); + if (mc == 2) { + if (SEQ(l, "ZB")) ret = v * 1024 * 1024 * 1024 * 1024 * 1024; + else if (SEQ(l, "EB")) ret = v * 1024 * 1024 * 1024 * 1024; + else if (SEQ(l, "PB")) ret = v * 1024 * 1024 * 1024; + else if (SEQ(l, "TB")) ret = v * 1024 * 1024; + else if (SEQ(l, "GB")) ret = v * 1024; + else if (SEQ(l, "MB")) ret = v; + else if (SEQ(l, "kB")) { + /* should never appear */ + if (v % 1024) dmi_spd_msg("OMG kB!"); + ret = v / 1024; + } + else if (SEQ(l, "bytes")) { + /* should never appear */ + if (v % 1024) dmi_spd_msg("OMG bytes!"); + ret = v / (1024 * 1024); + } + } + return ret; +} + typedef struct { - unsigned long array_handle; + dmi_handle array_handle; gboolean is_main_memory; gchar *locator; gchar *use; gchar *ecc; int devs; int devs_populated; - long int size_MiB_max; - long int size_MiB_present; + dmi_mem_size size_MiB_max; + dmi_mem_size size_MiB_present; int ram_types; /* bits using enum RamType */ } dmi_mem_array; -dmi_mem_array *dmi_mem_array_new(unsigned long h) { +dmi_mem_array *dmi_mem_array_new(dmi_handle h) { dmi_mem_array *s = g_new0(dmi_mem_array, 1); s->array_handle = h; s->use = dmidecode_match("Use", &dta, &h); @@ -83,14 +111,7 @@ dmi_mem_array *dmi_mem_array_new(unsigned long h) { gchar *array_max_size = dmidecode_match("Maximum Capacity", &dta, &h); if (array_max_size) { - long int v = 0; - char l[3] = ""; - int mc = sscanf(array_max_size, "%"PRId64" %2s", &v, l); - if (mc == 2) { - if (SEQ(l, "TB")) s->size_MiB_max = v * 1024 * 1024; - else if (SEQ(l, "GB")) s->size_MiB_max = v * 1024; - else if (SEQ(l, "MB")) s->size_MiB_max = v; - } + s->size_MiB_max = dmi_read_memory_str_to_MiB(array_max_size); g_free(array_max_size); } gchar *array_devs = dmidecode_match("Number Of Devices", &dta, &h); @@ -109,14 +130,14 @@ void dmi_mem_array_free(dmi_mem_array* s) { } typedef struct dmi_mem_socket { - unsigned long handle; - unsigned long array_handle; + dmi_handle handle; + dmi_handle array_handle; gboolean populated; gchar *locator; gchar *full_locator; gchar *short_locator; gchar *size_str; - long int size_MiB; + dmi_mem_size size_MiB; gchar *type; gchar *type_detail; @@ -146,10 +167,10 @@ typedef struct { GSList *arrays; GSList *sockets; GSList *spd; - long int spd_size_MiB; + dmi_mem_size spd_size_MiB; int spd_ram_types; /* bits using enum RamType */ - long int system_memory_MiB; + dmi_mem_size system_memory_MiB; int system_memory_ram_types; /* bits using enum RamType */ /* ->short_locator is unique among *sockets */ @@ -169,35 +190,14 @@ gboolean null_if_empty(gchar **str) { return TRUE; } -dmi_mem_socket *dmi_mem_socket_new(unsigned long h) { +dmi_mem_socket *dmi_mem_socket_new(dmi_handle h) { dmi_mem_socket *s = g_new0(dmi_mem_socket, 1); s->handle = h; s->locator = dmidecode_match("Locator", &dtm, &h); s->size_str = dmidecode_match("Size", &dtm, &h); - if (s->size_str) { - long int v = 0; - char l[3] = ""; - /* dmidecode units: "bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB" */ - int mc = sscanf(s->size_str, "%"PRId64" %2s", &v, l); - if (mc == 2) { - if (SEQ(l, "ZB")) s->size_MiB = v * 1024 * 1024 * 1024 * 1024 * 1024; - else if (SEQ(l, "EB")) s->size_MiB = v * 1024 * 1024 * 1024 * 1024; - else if (SEQ(l, "PB")) s->size_MiB = v * 1024 * 1024 * 1024; - else if (SEQ(l, "TB")) s->size_MiB = v * 1024 * 1024; - else if (SEQ(l, "GB")) s->size_MiB = v * 1024; - else if (SEQ(l, "MB")) s->size_MiB = v; - else if (SEQ(l, "kB")) { - /* should never appear */ - if (v % 1024) dmi_spd_msg("OMG kB!"); - s->size_MiB = v / 1024; - } - else if (SEQ(l, "by")) { - /* should never appear */ - if (v % 1024) dmi_spd_msg("OMG bytes!"); - s->size_MiB = v / (1024 * 1024); - } - } - } + if (s->size_str) + s->size_MiB = dmi_read_memory_str_to_MiB(s->size_str); + s->bank_locator = dmidecode_match("Bank Locator", &dtm, &h); STR_IGNORE(s->bank_locator, "Unknown"); null_if_empty(&s->bank_locator); @@ -214,8 +214,8 @@ dmi_mem_socket *dmi_mem_socket_new(unsigned long h) { } } - gchar *ah_str = g_strdup_printf("0x%lx", s->array_handle); - gchar *h_str = g_strdup_printf("0x%lx", s->handle); + gchar *ah_str = g_strdup_printf("0x%"PRIx32, s->array_handle); + gchar *h_str = g_strdup_printf("0x%"PRIx32, s->handle); s->short_locator = g_strdup_printf("%s \u27A4 %s", s->array_locator ? s->array_locator : ah_str, s->locator ? s->locator : h_str); @@ -385,9 +385,9 @@ dmi_mem *dmi_mem_new() { dmi_handle_list *hla = dmidecode_handles(&dta); if (hla) { - unsigned long i = 0; + int i = 0; for(i = 0; i < hla->count; i++) { - unsigned long h = hla->handles[i]; + dmi_handle h = hla->handles[i]; m->arrays = g_slist_append(m->arrays, dmi_mem_array_new(h)); } dmi_handle_list_free(hla); @@ -395,9 +395,9 @@ dmi_mem *dmi_mem_new() { dmi_handle_list *hlm = dmidecode_handles(&dtm); if (hlm) { - unsigned long i = 0; + int i = 0; for(i = 0; i < hlm->count; i++) { - unsigned long h = hlm->handles[i]; + dmi_handle h = hlm->handles[i]; m->sockets = g_slist_append(m->sockets, dmi_mem_socket_new(h)); } dmi_handle_list_free(hlm); @@ -572,7 +572,7 @@ gchar *make_spd_section(spd_data *spd) { if (!spd->size_MiB) size_str = g_strdup(_("(Unknown)")); else - size_str = g_strdup_printf("%d %s", spd->size_MiB, _("MiB") ); + size_str = g_strdup_printf("%"PRId64" %s", spd->size_MiB, _("MiB") ); gchar *mfg_date_str = NULL; if (spd->year) @@ -659,7 +659,7 @@ gchar *memory_devices_get_info() { } gchar *details = g_strdup_printf("[%s]\n" - "%s=0x%lx\n" + "%s=0x%"PRIx32"\n" "%s=%s\n" "%s=%s\n" "%s=%s\n" @@ -703,12 +703,12 @@ gchar *memory_devices_get_info() { else if (!s->size_MiB) size_str = g_strdup(s->size_str); else - size_str = g_strdup_printf("%ld %s", s->size_MiB, _("MiB") ); + size_str = g_strdup_printf("%"PRId64" %s", s->size_MiB, _("MiB") ); gchar *spd = s->spd ? make_spd_section(s->spd) : NULL; gchar *details = g_strdup_printf("[%s]\n" - "%s=0x%lx, 0x%lx\n" + "%s=0x%"PRIx32", 0x%"PRIx32"\n" "%s=%s\n" "%s=%s\n" "%s=%s\n" @@ -757,7 +757,7 @@ gchar *memory_devices_get_info() { g_free(size_str); } else { gchar *details = g_strdup_printf("[%s]\n" - "%s=0x%lx, 0x%lx\n" + "%s=0x%"PRIx32", 0x%"PRIx32"\n" "%s=%s\n" "%s=%s\n" "%s=%s\n", @@ -830,7 +830,7 @@ gchar *memory_devices_get_info() { if (!s->size_MiB) size_str = g_strdup(_("(Unknown)")); else - size_str = g_strdup_printf("%d %s", s->size_MiB, _("MiB") ); + size_str = g_strdup_printf("%"PRId64" %s", s->size_MiB, _("MiB") ); gchar *details = make_spd_section(s); @@ -877,21 +877,21 @@ gchar *memory_devices_get_system_memory_types_str() { return ret; } -long int memory_devices_get_system_memory_MiB() { +int memory_devices_get_system_memory_MiB() { dmi_mem *mem = dmi_mem_new(); - long int ret = mem->system_memory_MiB; + int ret = (int)mem->system_memory_MiB; dmi_mem_free(mem); return ret; } gchar *memory_devices_get_system_memory_str() { gchar *ret = NULL; - long int m = memory_devices_get_system_memory_MiB(); + dmi_mem_size m = memory_devices_get_system_memory_MiB(); if (m) { if (m > 1024 && (m % 1024 == 0) ) - ret = g_strdup_printf("%ld %s", m/1024, _("GiB")); + ret = g_strdup_printf("%"PRId64" %s", m/1024, _("GiB")); else - ret = g_strdup_printf("%ld %s", m, _("MiB")); + ret = g_strdup_printf("%"PRId64" %s", m, _("MiB")); } return ret; } diff --git a/modules/devices/spd-decode.c b/modules/devices/spd-decode.c index b819c3cf..b0834481 100644 --- a/modules/devices/spd-decode.c +++ b/modules/devices/spd-decode.c @@ -85,7 +85,7 @@ typedef struct { const char *form_factor; char type_detail[256]; - int size_MiB; + dmi_mem_size size_MiB; int spd_rev_major; // bytes[1] >> 4 int spd_rev_minor; // bytes[1] & 0xf @@ -122,7 +122,7 @@ static int parity(int value) { return (0x6996 >> value) & 1; } -static void decode_sdr_module_size(unsigned char *bytes, int *size) { +static void decode_sdr_module_size(unsigned char *bytes, dmi_mem_size *size) { int i, k = 0; i = (bytes[3] & 0x0f) + (bytes[4] & 0x0f) - 17; @@ -331,7 +331,7 @@ static void decode_ddr_module_speed(unsigned char *bytes, float *ddrclk, int *pc if (pcclk) *pcclk = pc; } -static void decode_ddr_module_size(unsigned char *bytes, int *size) { +static void decode_ddr_module_size(unsigned char *bytes, dmi_mem_size *size) { int i, k; i = (bytes[3] & 0x0f) + (bytes[4] & 0x0f) - 17; @@ -435,7 +435,7 @@ static void decode_ddr2_module_speed(unsigned char *bytes, float *ddr_clock, int if (pc2_speed) { *pc2_speed = pcclk; } } -static void decode_ddr2_module_size(unsigned char *bytes, int *size) { +static void decode_ddr2_module_size(unsigned char *bytes, dmi_mem_size *size) { int i, k; i = (bytes[3] & 0x0f) + (bytes[4] & 0x0f) - 17; @@ -518,7 +518,7 @@ static void decode_ddr3_module_speed(unsigned char *bytes, float *ddr_clock, int if (pc3_speed) { *pc3_speed = pcclk; } } -static void decode_ddr3_module_size(unsigned char *bytes, int *size) { +static void decode_ddr3_module_size(unsigned char *bytes, dmi_mem_size *size) { unsigned int sdr_capacity = 256 << (bytes[4] & 0xF); unsigned int sdr_width = 4 << (bytes[7] & 0x7); unsigned int bus_width = 8 << (bytes[8] & 0x7); @@ -776,7 +776,7 @@ static void decode_ddr4_module_spd_timings(unsigned char *bytes, float speed, ch } } -static void decode_ddr4_module_size(unsigned char *bytes, int *size) { +static void decode_ddr4_module_size(unsigned char *bytes, dmi_mem_size *size) { int sdrcap = 256 << (bytes[4] & 15); int buswidth = 8 << (bytes[13] & 7); int sdrwidth = 4 << (bytes[12] & 7); |