1 // Access to pseudo "file" interface for configuration information.
3 // Copyright (C) 2012 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "config.h" // CONFIG_*
8 #include "malloc.h" // free
9 #include "output.h" // dprintf
10 #include "romfile.h" // struct romfile_s
11 #include "string.h" // memcmp
13 static struct romfile_s *RomfileRoot VARVERIFY32INIT;
16 romfile_add(struct romfile_s *file)
18 dprintf(3, "Add romfile: %s (size=%d)\n", file->name, file->size);
19 file->next = RomfileRoot;
23 // Search for the specified file.
24 static struct romfile_s *
25 __romfile_findprefix(const char *prefix, int prefixlen, struct romfile_s *prev)
27 struct romfile_s *cur = RomfileRoot;
31 if (memcmp(prefix, cur->name, prefixlen) == 0)
39 romfile_findprefix(const char *prefix, struct romfile_s *prev)
41 return __romfile_findprefix(prefix, strlen(prefix), prev);
45 romfile_find(const char *name)
47 return __romfile_findprefix(name, strlen(name) + 1, NULL);
50 // Helper function to find, malloc_tmphigh, and copy a romfile. This
51 // function adds a trailing zero to the malloc'd copy.
53 romfile_loadfile(const char *name, int *psize)
55 struct romfile_s *file = romfile_find(name);
59 int filesize = file->size;
63 char *data = malloc_tmphigh(filesize+1);
69 dprintf(5, "Copying romfile '%s' (len %d)\n", name, filesize);
70 int ret = file->copy(file, data, filesize);
77 data[filesize] = '\0';
81 // Attempt to load an integer from the given file - return 'defval'
84 romfile_loadint(const char *name, u64 defval)
86 struct romfile_s *file = romfile_find(name);
90 int filesize = file->size;
91 if (!filesize || filesize > sizeof(u64) || (filesize & (filesize-1)))
92 // Doesn't look like a valid integer.
96 int ret = file->copy(file, &val, sizeof(val));