dmidecode-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/4] dmidecode: Write the whole dump file at once


From: Jean Delvare
Subject: [PATCH 3/4] dmidecode: Write the whole dump file at once
Date: Tue, 7 Feb 2023 15:34:32 +0100

When option --dump-bin is used, write the whole dump file at once,
instead of opening and closing the file separately for the table
and then for the entry point.

As the file writing function is no longer generic, it gets moved
from util.c to dmidecode.c.

One minor functional change resulting from the new implementation is
that the entry point is written first now, so the messages printed
are swapped.

Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
 dmidecode.c |   69 ++++++++++++++++++++++++++++++++++++++++++++----------------
 util.c      |   40 ----------------------------------
 util.h      |    1 
 3 files changed, 51 insertions(+), 59 deletions(-)

--- dmidecode.orig/util.c
+++ dmidecode/util.c
@@ -259,46 +259,6 @@ void *mem_chunk(off_t base, size_t len,
        return p;
 }
 
-int write_dump(size_t base, size_t len, const void *data, const char 
*dumpfile, int add)
-{
-       FILE *f;
-
-       f = fopen(dumpfile, add ? "r+b" : "wb");
-       if (!f)
-       {
-               fprintf(stderr, "%s: ", dumpfile);
-               perror("fopen");
-               return -1;
-       }
-
-       if (fseek(f, base, SEEK_SET) != 0)
-       {
-               fprintf(stderr, "%s: ", dumpfile);
-               perror("fseek");
-               goto err_close;
-       }
-
-       if (fwrite(data, len, 1, f) != 1)
-       {
-               fprintf(stderr, "%s: ", dumpfile);
-               perror("fwrite");
-               goto err_close;
-       }
-
-       if (fclose(f))
-       {
-               fprintf(stderr, "%s: ", dumpfile);
-               perror("fclose");
-               return -1;
-       }
-
-       return 0;
-
-err_close:
-       fclose(f);
-       return -1;
-}
-
 /* Returns end - start + 1, assuming start < end */
 u64 u64_range(u64 start, u64 end)
 {
--- dmidecode.orig/util.h
+++ dmidecode/util.h
@@ -27,5 +27,4 @@
 int checksum(const u8 *buf, size_t len);
 void *read_file(off_t base, size_t *len, const char *filename);
 void *mem_chunk(off_t base, size_t len, const char *devmem);
-int write_dump(size_t base, size_t len, const void *data, const char 
*dumpfile, int add);
 u64 u64_range(u64 start, u64 end);
--- dmidecode.orig/dmidecode.c
+++ dmidecode/dmidecode.c
@@ -5410,11 +5410,56 @@ static void dmi_table_string(const struc
        }
 }
 
-static void dmi_table_dump(const u8 *buf, u32 len)
+static int dmi_table_dump(const u8 *ep, u32 ep_len, const u8 *table,
+                         u32 table_len)
 {
+       FILE *f;
+
+       f = fopen(opt.dumpfile, "wb");
+       if (!f)
+       {
+               fprintf(stderr, "%s: ", opt.dumpfile);
+               perror("fopen");
+               return -1;
+       }
+
+       if (!(opt.flags & FLAG_QUIET))
+               pr_comment("Writing %d bytes to %s.", ep_len, opt.dumpfile);
+       if (fwrite(ep, ep_len, 1, f) != 1)
+       {
+               fprintf(stderr, "%s: ", opt.dumpfile);
+               perror("fwrite");
+               goto err_close;
+       }
+
+       if (fseek(f, 32, SEEK_SET) != 0)
+       {
+               fprintf(stderr, "%s: ", opt.dumpfile);
+               perror("fseek");
+               goto err_close;
+       }
+
        if (!(opt.flags & FLAG_QUIET))
-               pr_comment("Writing %d bytes to %s.", len, opt.dumpfile);
-       write_dump(32, len, buf, opt.dumpfile, 0);
+               pr_comment("Writing %d bytes to %s.", table_len, opt.dumpfile);
+       if (fwrite(table, table_len, 1, f) != 1)
+       {
+               fprintf(stderr, "%s: ", opt.dumpfile);
+               perror("fwrite");
+               goto err_close;
+       }
+
+       if (fclose(f))
+       {
+               fprintf(stderr, "%s: ", opt.dumpfile);
+               perror("fclose");
+               return -1;
+       }
+
+       return 0;
+
+err_close:
+       fclose(f);
+       return -1;
 }
 
 static void dmi_table_decode(u8 *buf, u32 len, u16 num, u16 ver, u32 flags)
@@ -5709,11 +5754,7 @@ static int smbios3_decode(u8 *buf, const
                memcpy(crafted, buf, 32);
                overwrite_smbios3_address(crafted);
 
-               dmi_table_dump(table, len);
-               if (!(opt.flags & FLAG_QUIET))
-                       pr_comment("Writing %d bytes to %s.", crafted[0x06],
-                                  opt.dumpfile);
-               write_dump(0, crafted[0x06], crafted, opt.dumpfile, 1);
+               dmi_table_dump(crafted, crafted[0x06], table, len);
        }
        else
        {
@@ -5796,11 +5837,7 @@ static int smbios_decode(u8 *buf, const
                memcpy(crafted, buf, 32);
                overwrite_dmi_address(crafted + 0x10);
 
-               dmi_table_dump(table, len);
-               if (!(opt.flags & FLAG_QUIET))
-                       pr_comment("Writing %d bytes to %s.", crafted[0x05],
-                                  opt.dumpfile);
-               write_dump(0, crafted[0x05], crafted, opt.dumpfile, 1);
+               dmi_table_dump(crafted, crafted[0x05], table, len);
        }
        else
        {
@@ -5841,11 +5878,7 @@ static int legacy_decode(u8 *buf, const
                memcpy(crafted, buf, 16);
                overwrite_dmi_address(crafted);
 
-               dmi_table_dump(table, len);
-               if (!(opt.flags & FLAG_QUIET))
-                       pr_comment("Writing %d bytes to %s.", 0x0F,
-                                  opt.dumpfile);
-               write_dump(0, 0x0F, crafted, opt.dumpfile, 1);
+               dmi_table_dump(crafted, 0x0F, table, len);
        }
        else
        {

-- 
Jean Delvare
SUSE L3 Support



reply via email to

[Prev in Thread] Current Thread [Next in Thread]