From: Karel Zak Date: Wed, 20 Nov 2019 10:25:32 +0000 (+0100) Subject: lscpu: top-level DMI function refactoring X-Git-Tag: v2.35-rc1~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=241f3f014e4b93b8d97611c10ef861ff289047f8;p=thirdparty%2Futil-linux.git lscpu: top-level DMI function refactoring Let's keep /dev/mem code together and do not mix it with /sys firmware stuff. Signed-off-by: Karel Zak --- diff --git a/sys-utils/lscpu-dmi.c b/sys-utils/lscpu-dmi.c index 82d8ed4430..edf0f31e0d 100644 --- a/sys-utils/lscpu-dmi.c +++ b/sys-utils/lscpu-dmi.c @@ -42,16 +42,6 @@ struct dmi_header uint8_t *data; }; -static int checksum(const uint8_t *buf, size_t len) -{ - uint8_t sum = 0; - size_t a; - - for (a = 0; a < len; a++) - sum += buf[a]; - return (sum == 0); -} - static void *get_mem_chunk(size_t base, size_t len, const char *devmem) { void *p = NULL; @@ -167,6 +157,16 @@ done: return rc; } +static int checksum(const uint8_t *buf, size_t len) +{ + uint8_t sum = 0; + size_t a; + + for (a = 0; a < len; a++) + sum += buf[a]; + return (sum == 0); +} + #if defined(__x86_64__) || defined(__i386__) static int hypervisor_decode_legacy(uint8_t *buf, const char *devmem) { @@ -191,18 +191,6 @@ static int hypervisor_decode_smbios(uint8_t *buf, const char *devmem) devmem); } -static int hypervisor_decode_sysfw(void) -{ - static char const sys_fw_dmi_tables[] = _PATH_SYS_DMI; - struct stat st; - - if (stat(sys_fw_dmi_tables, &st)) - return -1; - - return hypervisor_from_dmi_table(0, st.st_size, st.st_size / 4, - sys_fw_dmi_tables); -} - /* * Probe for EFI interface */ @@ -241,26 +229,12 @@ static int address_from_efi(size_t *address) return ret; } -int read_hypervisor_dmi(void) +static int read_hypervisor_dmi_from_devmem(void) { int rc = HYPER_NONE; uint8_t *buf = NULL; size_t fp = 0; - if (sizeof(uint8_t) != 1 - || sizeof(uint16_t) != 2 - || sizeof(uint32_t) != 4 - || '\0' != 0) - goto done; - - /* -1 : no DMI in /sys, - * 0 : DMI exist, nothing detected (HYPER_NONE) - * >0 : hypervisor detected - */ - rc = hypervisor_decode_sysfw(); - if (rc >= HYPER_NONE) - goto done; - /* First try EFI (ia64, Intel-based Mac) */ switch (address_from_efi(&fp)) { case EFI_NOT_FOUND: @@ -301,5 +275,38 @@ memory_scan: #endif done: free(buf); + return rc; +} + +static int read_hypervisor_dmi_from_sysfw(void) +{ + static char const sys_fw_dmi_tables[] = _PATH_SYS_DMI; + struct stat st; + + if (stat(sys_fw_dmi_tables, &st)) + return -1; + + return hypervisor_from_dmi_table(0, st.st_size, st.st_size / 4, + sys_fw_dmi_tables); +} + +int read_hypervisor_dmi(void) +{ + int rc; + + if (sizeof(uint8_t) != 1 + || sizeof(uint16_t) != 2 + || sizeof(uint32_t) != 4 + || '\0' != 0) + return HYPER_NONE; + + /* -1 : no DMI in /sys, + * 0 : DMI exist, nothing detected (HYPER_NONE) + * >0 : hypervisor detected + */ + rc = read_hypervisor_dmi_from_sysfw(); + if (rc < 0) + rc = read_hypervisor_dmi_from_devmem(); + return rc < 0 ? HYPER_NONE : rc; }