aboutsummaryrefslogtreecommitdiffstats
path: root/src/romfile.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2012-05-31 00:20:55 -0400
committerKevin O'Connor <kevin@koconnor.net>2012-06-02 20:06:18 -0400
commit59d6ca52a7eba5b1f4f2becf70fd446dccaf0a2e (patch)
treea90ca99b49e14bd73076a35183be1a14143534e6 /src/romfile.c
parent51d6ba3e6fac2e78679145fe34f3861225670ae9 (diff)
downloadseabios-59d6ca52a7eba5b1f4f2becf70fd446dccaf0a2e.tar.gz
Cache romfile entries.
Create a 'struct romfile_s' and populate a list of all romfiles at start of init. Caching the romfiles both simplifies the code and makes it more efficient. Also, convert the ramdisk code to use romfile helpers instead of directly accessing cbfs. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/romfile.c')
-rw-r--r--src/romfile.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/romfile.c b/src/romfile.c
new file mode 100644
index 00000000..b732e298
--- /dev/null
+++ b/src/romfile.c
@@ -0,0 +1,96 @@
+// Access to pseudo "file" interface for configuration information.
+//
+// Copyright (C) 2012 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU LGPLv3 license.
+
+#include "config.h" // CONFIG_*
+#include "util.h" // dprintf
+
+static struct romfile_s *RomfileRoot;
+
+void
+romfile_add(struct romfile_s *file)
+{
+ file->next = RomfileRoot;
+ RomfileRoot = file;
+}
+
+// Search for the specified file.
+static struct romfile_s *
+__romfile_findprefix(const char *prefix, int prefixlen, struct romfile_s *prev)
+{
+ struct romfile_s *cur = RomfileRoot;
+ if (prev)
+ cur = prev->next;
+ while (cur) {
+ if (memcmp(prefix, cur->name, prefixlen) == 0)
+ return cur;
+ cur = cur->next;
+ }
+ return NULL;
+}
+
+struct romfile_s *
+romfile_findprefix(const char *prefix, struct romfile_s *prev)
+{
+ return __romfile_findprefix(prefix, strlen(prefix), prev);
+}
+
+struct romfile_s *
+romfile_find(const char *name)
+{
+ return __romfile_findprefix(name, strlen(name) + 1, NULL);
+}
+
+// Helper function to find, malloc_tmphigh, and copy a romfile. This
+// function adds a trailing zero to the malloc'd copy.
+void *
+romfile_loadfile(const char *name, int *psize)
+{
+ struct romfile_s *file = romfile_find(name);
+ if (!file)
+ return NULL;
+
+ int filesize = file->size;
+ if (!filesize)
+ return NULL;
+
+ char *data = malloc_tmphigh(filesize+1);
+ if (!data) {
+ warn_noalloc();
+ return NULL;
+ }
+
+ dprintf(5, "Copying romfile '%s' (len %d)\n", name, filesize);
+ int ret = file->copy(file, data, filesize);
+ if (ret < 0) {
+ free(data);
+ return NULL;
+ }
+ if (psize)
+ *psize = filesize;
+ data[filesize] = '\0';
+ return data;
+}
+
+// Attempt to load an integer from the given file - return 'defval'
+// if unsuccesful.
+u64
+romfile_loadint(const char *name, u64 defval)
+{
+ struct romfile_s *file = romfile_find(name);
+ if (!file)
+ return defval;
+
+ int filesize = file->size;
+ if (!filesize || filesize > sizeof(u64) || (filesize & (filesize-1)))
+ // Doesn't look like a valid integer.
+ return defval;
+
+ u64 val = 0;
+ int ret = file->copy(file, &val, sizeof(val));
+ if (ret < 0)
+ return defval;
+ return val;
+}