There is no reason to allocate/copy/free memory for dmi_table inside function. It's more convenient to do it outside. In this case we can use dmi_decode function with already allocated memory like in case with dmifs library. The same is for dmi_table_dump().
Signed-off-by: Ivan Khoronzhuk ivan.khoronzhuk@linaro.org --- dmidecode.c | 70 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 29 deletions(-)
diff --git a/dmidecode.c b/dmidecode.c index 4e4a82f..2fedddc 100644 --- a/dmidecode.c +++ b/dmidecode.c @@ -4351,25 +4351,15 @@ static void dmi_table_string(const struct dmi_header *h, const u8 *data, u16 ver } }
-static void dmi_table_dump(u32 base, u16 len, const char *devmem) +static void dmi_table_dump(u8 *buf, u16 len) { - u8 *buf; - - if ((buf = mem_chunk(base, len, devmem)) == NULL) - { - fprintf(stderr, "Failed to read table, sorry.\n"); - return; - } - if (!(opt.flags & FLAG_QUIET)) printf("# Writing %d bytes to %s.\n", len, opt.dumpfile); write_dump(32, len, buf, opt.dumpfile, 0); - free(buf); }
-static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem) +static void dmi_table(u32 base, u8 *buf, u16 len, u16 num, u16 ver) { - u8 *buf; u8 *data; int i = 0;
@@ -4382,7 +4372,7 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem)
if (opt.flags & FLAG_DUMP_BIN) { - dmi_table_dump(base, len, devmem); + dmi_table_dump(buf, len); return; }
@@ -4398,16 +4388,6 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem) printf("\n"); }
- if ((buf = mem_chunk(base, len, devmem)) == NULL) - { - fprintf(stderr, "Table is unreachable, sorry." -#ifndef USE_MMAP - " Try compiling dmidecode with -DUSE_MMAP." -#endif - "\n"); - return; - } - data = buf; while (i < num && data+4 <= buf + len) /* 4 is the length of an SMBIOS structure header */ { @@ -4485,8 +4465,6 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem) "announced, structures occupy %d bytes.\n", len, (unsigned int)(data - buf)); } - - free(buf); }
/* @@ -4524,6 +4502,9 @@ static void smbios_in_dumpfile(unsigned char *smbios) static int smbios_decode(u8 *buf, const char *devmem) { u16 ver; + u16 len; + u32 base; + u8 *dmibuf;
if (!checksum(buf, buf[0x05]) || memcmp(buf + 0x10, "_DMI_", 5) != 0 @@ -4532,8 +4513,21 @@ static int smbios_decode(u8 *buf, const char *devmem)
ver = get_smbios_version(buf);
- dmi_table(DWORD(buf + 0x18), WORD(buf + 0x16), WORD(buf + 0x1C), - ver, devmem); + base = DWORD(buf + 0x18); + len = WORD(buf + 0x16); + dmibuf = mem_chunk(base, len, devmem); + if (!dmibuf) { + fprintf(stderr, "Table is unreachable, sorry." +#ifndef USE_MMAP + " Try compiling dmidecode with -DUSE_MMAP." +#endif + "\n"); + return 0; + } + + dmi_table(base, dmibuf, len, WORD(buf + 0x1C), ver); + + free(dmibuf);
if (opt.flags & FLAG_DUMP_BIN) smbios_in_dumpfile(buf); @@ -4543,6 +4537,10 @@ static int smbios_decode(u8 *buf, const char *devmem)
static int legacy_decode(u8 *buf, const char *devmem) { + u16 len; + u32 base; + u8 *dmibuf; + if (!checksum(buf, 0x0F)) return 0;
@@ -4550,8 +4548,22 @@ static int legacy_decode(u8 *buf, const char *devmem) printf("Legacy DMI %u.%u present.\n", buf[0x0E] >> 4, buf[0x0E] & 0x0F);
- dmi_table(DWORD(buf + 0x08), WORD(buf + 0x06), WORD(buf + 0x0C), - ((buf[0x0E] & 0xF0) << 4) + (buf[0x0E] & 0x0F), devmem); + len = WORD(buf + 0x06); + base = DWORD(buf + 0x08); + dmibuf = mem_chunk(base, len, devmem); + if (!dmibuf) { + fprintf(stderr, "Table is unreachable, sorry." +#ifndef USE_MMAP + " Try compiling dmidecode with -DUSE_MMAP." +#endif + "\n"); + return 0; + } + + dmi_table(base, dmibuf, len, WORD(buf + 0x0C), + ((buf[0x0E] & 0xF0) << 4) + (buf[0x0E] & 0x0F)); + + free(dmibuf);
if (opt.flags & FLAG_DUMP_BIN) {