Be sure to add "void" to all function prototypes that take no args.
[seabios.git] / src / coreboot.c
index 82c9f8a5499ebf6e9cda11424582e97ff63d926c..7ad46cedbd3b310898b3536695572332edd19ad6 100644 (file)
@@ -11,6 +11,7 @@
 #include "mptable.h" // MPTABLE_SIGNATURE
 #include "biosvar.h" // GET_EBDA
 #include "lzmadecode.h" // LzmaDecode
+#include "smbios.h" // smbios_init
 
 
 /****************************************************************
@@ -121,7 +122,7 @@ static struct cb_memory *CBMemTable;
 
 // Populate max ram and e820 map info by scanning for a coreboot table.
 static void
-coreboot_fill_map()
+coreboot_fill_map(void)
 {
     dprintf(3, "Attempting to find coreboot table\n");
 
@@ -281,7 +282,7 @@ scan_tables(u32 start, u32 size)
 }
 
 void
-coreboot_copy_biostable()
+coreboot_copy_biostable(void)
 {
     struct cb_memory *cbm = CBMemTable;
     if (! CONFIG_COREBOOT || !cbm)
@@ -311,6 +312,7 @@ coreboot_copy_biostable()
  * ulzma
  ****************************************************************/
 
+// Uncompress data in flash to an area of memory.
 static int
 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
 {
@@ -349,11 +351,6 @@ ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
  * Coreboot flash format
  ****************************************************************/
 
-// XXX - optimize
-#define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \
-                  (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24))
-#define htonl(x) ntohl(x)
-
 #define CBFS_HEADER_MAGIC 0x4F524243
 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
 #define CBFS_VERSION1 0x31313131
@@ -371,7 +368,7 @@ struct cbfs_header {
 static struct cbfs_header *CBHDR;
 
 static void
-cbfs_setup()
+cbfs_setup(void)
 {
     if (! CONFIG_COREBOOT_FLASH)
         return;
@@ -398,165 +395,153 @@ struct cbfs_file {
     char filename[0];
 } PACKED;
 
+// Verify a cbfs entry looks valid.
 static struct cbfs_file *
-cbfs_search(struct cbfs_file *file)
+cbfs_verify(struct cbfs_file *file)
 {
-    for (;;) {
-        if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
-            return NULL;
-        u64 magic = file->magic;
-        if (magic == CBFS_FILE_MAGIC) {
-            dprintf(5, "Found CBFS file %s\n", file->filename);
-            return file;
-        }
-        if (magic == 0)
-            return NULL;
-        file = (void*)file + ntohl(CBHDR->align);
+    if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
+        return NULL;
+    u64 magic = file->magic;
+    if (magic == CBFS_FILE_MAGIC) {
+        dprintf(5, "Found CBFS file %s\n", file->filename);
+        return file;
     }
+    return NULL;
 }
 
+// Return the first file in the CBFS archive
 static struct cbfs_file *
-cbfs_getfirst()
+cbfs_getfirst(void)
 {
     if (! CBHDR)
         return NULL;
-    return cbfs_search((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
+    return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
 }
 
+// Return the file after the given file.
 static struct cbfs_file *
 cbfs_getnext(struct cbfs_file *file)
 {
     file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
-    return cbfs_search(file);
+    return cbfs_verify(file);
 }
 
-static struct cbfs_file *
+// Find the file with the given filename.
+struct cbfs_file *
 cbfs_findfile(const char *fname)
 {
     dprintf(3, "Searching CBFS for %s\n", fname);
     struct cbfs_file *file;
-    for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
+    for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
         if (strcmp(fname, file->filename) == 0)
             return file;
-    }
     return NULL;
 }
 
-static int
-data_copy(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
+// Find next file with the given filename prefix.
+struct cbfs_file *
+cbfs_findprefix(const char *prefix, struct cbfs_file *last)
 {
-    dprintf(3, "Copying data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
-    if (srclen > maxlen) {
-        dprintf(1, "File too big to copy\n");
-        return -1;
-    }
-    memcpy(dst, src, srclen);
-    return srclen;
+    if (! CONFIG_COREBOOT_FLASH)
+        return NULL;
+
+    dprintf(3, "Searching CBFS for prefix %s\n", prefix);
+    int len = strlen(prefix);
+    struct cbfs_file *file;
+    if (! last)
+        file = cbfs_getfirst();
+    else
+        file = cbfs_getnext(last);
+    for (; file; file = cbfs_getnext(file))
+        if (memcmp(prefix, file->filename, len) == 0)
+            return file;
+    return NULL;
 }
 
-// Copy a file to memory (uncompressing if necessary)
-static int
-cbfs_copyfile(void *dst, u32 maxlen, const char *fname)
+// Find a file with the given filename (possibly with ".lzma" extension).
+static struct cbfs_file *
+cbfs_finddatafile(const char *fname)
 {
-    dprintf(3, "Searching CBFS for data file %s\n", fname);
     int fnlen = strlen(fname);
-    struct cbfs_file *file;
-    for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
-        if (memcmp(fname, file->filename, fnlen) != 0)
-            continue;
-        u32 size = ntohl(file->len);
-        void *src = (void*)file + ntohl(file->offset);
-        if (file->filename[fnlen] == '\0')
-            return data_copy(dst, maxlen, src, size);
-        if (strcmp(&file->filename[fnlen], ".lzma") == 0)
-            return ulzma(dst, maxlen, src, size);
+    struct cbfs_file *file = NULL;
+    for (;;) {
+        file = cbfs_findprefix(fname, file);
+        if (!file)
+            return NULL;
+        if (file->filename[fnlen] == '\0'
+            || strcmp(&file->filename[fnlen], ".lzma") == 0)
+            return file;
     }
-    return -1;
 }
 
-const char *
-cbfs_findNprefix(const char *prefix, int n)
+// Determine whether the file has a ".lzma" extension.
+static int
+cbfs_iscomp(struct cbfs_file *file)
 {
-    if (! CONFIG_COREBOOT_FLASH)
-        return NULL;
-
-    dprintf(3, "Searching CBFS for prefix %s\n", prefix);
-    int len = strlen(prefix);
-    struct cbfs_file *file;
-    for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
-        if (memcmp(prefix, file->filename, len) == 0) {
-            if (n <= 0)
-                return file->filename;
-            n--;
-        }
-    }
-    return NULL;
+    int fnamelen = strlen(file->filename);
+    return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
 }
 
-static char
-getHex(u8 x)
+// Return the filename of a given file.
+const char *
+cbfs_filename(struct cbfs_file *file)
 {
-    if (x <= 9)
-        return '0' + x;
-    return 'a' + x - 10;
+    return file->filename;
 }
 
-static u32
-hexify4(u16 x)
+// Determine the uncompressed size of a datafile.
+u32
+cbfs_datasize(struct cbfs_file *file)
 {
-    return ((getHex(x&0xf) << 24)
-            | (getHex((x>>4)&0xf) << 16)
-            | (getHex((x>>8)&0xf) << 8)
-            | (getHex(x>>12)));
+    void *src = (void*)file + ntohl(file->offset);
+    if (cbfs_iscomp(file))
+        return *(u32*)(src + LZMA_PROPERTIES_SIZE);
+    return ntohl(file->len);
 }
 
+// Copy a file to memory (uncompressing if necessary)
 int
-cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
+cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
 {
-    if (! CONFIG_COREBOOT_FLASH)
+    if (! CONFIG_COREBOOT_FLASH || !file)
         return -1;
 
-    char fname[17];
-    // Ughh - poor man's sprintf of "pci%04x,%04x.rom"
-    *(u32*)fname = 0x20696370; // "pci "
-    *(u32*)&fname[3] = hexify4(vendev);
-    fname[7] = ',';
-    *(u32*)&fname[8] = hexify4(vendev >> 16);
-    *(u32*)&fname[12] = 0x6d6f722e; // ".rom"
-    fname[16] = '\0';
-
-    return cbfs_copyfile(dst, maxlen, fname);
+    u32 size = ntohl(file->len);
+    void *src = (void*)file + ntohl(file->offset);
+    if (cbfs_iscomp(file)) {
+        // Compressed - copy to temp ram and uncompress it.
+        u32 asize = ALIGN(size, 4);
+        void *temp = malloc_tmphigh(asize);
+        if (!temp)
+            return -1;
+        iomemcpy(temp, src, asize);
+        int ret = ulzma(dst, maxlen, temp, size);
+        yield();
+        free(temp);
+        return ret;
+    }
+
+    // Not compressed.
+    dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
+    if (size > maxlen) {
+        dprintf(1, "File too big to copy\n");
+        return -1;
+    }
+    iomemcpy(dst, src, size);
+    return size;
 }
 
-// Copy the next file with the given prefix - starting at pos 'last'.
-struct cbfs_file *
-cbfs_copyfile_prefix(void *dst, u32 maxlen, const char *prefix
-                     , struct cbfs_file *last)
+// Find and copy the optionrom for the given vendor/device id.
+int
+cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
 {
     if (! CONFIG_COREBOOT_FLASH)
-        return NULL;
-    dprintf(3, "Searching CBFS for data file prefix %s\n", prefix);
-    struct cbfs_file *file;
-    if (! last)
-        file = cbfs_getfirst();
-    else
-        file = cbfs_getnext(last);
-    int prefixlen = strlen(prefix);
-    for (; file; file = cbfs_getnext(file)) {
-        if (memcmp(prefix, file->filename, prefixlen) != 0)
-            continue;
-        u32 size = ntohl(file->len);
-        void *src = (void*)file + ntohl(file->offset);
-        int fnamelen = strlen(file->filename);
-        int rv;
-        if (fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0)
-            rv = ulzma(dst, maxlen, src, size);
-        else
-            rv = data_copy(dst, maxlen, src, size);
-        if (rv >= 0)
-            return file;
-    }
-    return NULL;
+        return -1;
+
+    char fname[17];
+    snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
+             , (u16)vendev, vendev >> 16);
+    return cbfs_copyfile(cbfs_finddatafile(fname), dst, maxlen);
 }
 
 struct cbfs_payload_segment {
@@ -579,14 +564,11 @@ struct cbfs_payload {
 };
 
 void
-cbfs_run_payload(const char *filename)
+cbfs_run_payload(struct cbfs_file *file)
 {
-    if (! CONFIG_COREBOOT_FLASH)
-        return;
-    dprintf(1, "Run %s\n", filename);
-    struct cbfs_file *file = cbfs_findfile(filename);
-    if (!file)
+    if (!CONFIG_COREBOOT_FLASH || !file)
         return;
+    dprintf(1, "Run %s\n", file->filename);
     struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
     struct cbfs_payload_segment *seg = pay->segments;
     for (;;) {