[SeaBIOS] [PATCH v3 3/6] romfile: add support for constant files.

Gerd Hoffmann kraxel at redhat.com
Mon Sep 18 10:47:21 CEST 2017


Signed-off-by: Gerd Hoffmann <kraxel at redhat.com>
---
 src/romfile.h |  2 ++
 src/romfile.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/src/romfile.h b/src/romfile.h
index c6d62a1ddc..3e0f820047 100644
--- a/src/romfile.h
+++ b/src/romfile.h
@@ -16,4 +16,6 @@ struct romfile_s *romfile_find(const char *name);
 void *romfile_loadfile(const char *name, int *psize);
 u64 romfile_loadint(const char *name, u64 defval);
 
+void const_romfile_add_int(char *name, u32 value);
+
 #endif // romfile.h
diff --git a/src/romfile.c b/src/romfile.c
index 42261a624c..b598274edc 100644
--- a/src/romfile.c
+++ b/src/romfile.c
@@ -98,3 +98,49 @@ romfile_loadint(const char *name, u64 defval)
         return defval;
     return val;
 }
+
+struct const_romfile_s {
+    struct romfile_s file;
+    void *data;
+};
+
+static int
+const_read_file(struct romfile_s *file, void *dst, u32 maxlen)
+{
+    if (file->size > maxlen)
+        return -1;
+    struct const_romfile_s *cfile;
+    cfile = container_of(file, struct const_romfile_s, file);
+    if (maxlen > file->size)
+        maxlen = file->size;
+    memcpy(dst, cfile->data, maxlen);
+    return file->size;
+}
+
+static void
+const_romfile_add(char *name, void *data, int size)
+{
+    struct const_romfile_s *cfile = malloc_tmp(sizeof(*cfile));
+    if (!cfile) {
+        warn_noalloc();
+        return;
+    }
+    memset(cfile, 0, sizeof(*cfile));
+    strtcpy(cfile->file.name, name, sizeof(cfile->file.name));
+    cfile->file.size = size;
+    cfile->file.copy = const_read_file;
+    cfile->data = data;
+    romfile_add(&cfile->file);
+}
+
+void
+const_romfile_add_int(char *name, u32 value)
+{
+    u32 *data = malloc_tmp(sizeof(*data));
+    if (!data) {
+        warn_noalloc();
+        return;
+    }
+    *data = value;
+    const_romfile_add(name, data, sizeof(*data));
+}
-- 
2.9.3




More information about the SeaBIOS mailing list