acpi: add option, load_header, to -acpitable in order to load acpi header.

Submitted by Isaku Yamahata on June 15, 2010, 3:49 a.m.

Details

Message ID 20100615034949.GV23473@valinux.co.jp
State New
Headers show

Commit Message

Isaku Yamahata June 15, 2010, 3:49 a.m.
This patch adds load_header option to -acpitable in order to
load acpi table which includes acpi header.
With this option and with seabios patches, alternative dsdt table
can be passed to BIOS.

Also fix potential buffer overflow.
There is no guarantee that file size remains same when loading.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 hw/acpi.c       |   43 +++++++++++++++++++++++++++++++++----------
 qemu-options.hx |    2 ++
 2 files changed, 35 insertions(+), 10 deletions(-)

Patch hide | download patch | download mbox

diff --git a/hw/acpi.c b/hw/acpi.c
index c7044b1..682f4c7 100644
--- a/hw/acpi.c
+++ b/hw/acpi.c
@@ -47,11 +47,25 @@  static int acpi_checksum(const uint8_t *data, int len)
 int acpi_table_add(const char *t)
 {
     static const char *dfl_id = "QEMUQEMU";
+    int load_header = 0;
     char buf[1024], *p, *f;
     struct acpi_table_header acpi_hdr;
+    uint32_t length;
+    struct acpi_table_header *acpi_hdr_p;
     unsigned long val;
     size_t off;
 
+    if (strncmp(t, "load_header", strlen("load_header")) == 0) {
+        /* the files includes acpi header to load.
+         * the acpi header options, sig, rev, ... will be ignored.
+         */
+        load_header = 1;
+        t += strlen("load_header");
+        if (*t == ',') {
+            t++;
+        }
+    }
+
     memset(&acpi_hdr, 0, sizeof(acpi_hdr));
   
     if (get_param_value(buf, sizeof(buf), "sig", t)) {
@@ -108,7 +122,10 @@  int acpi_table_add(const char *t)
          buf[0] = '\0';
     }
 
-    acpi_hdr.length = sizeof(acpi_hdr);
+    length = sizeof(acpi_hdr);
+    if (load_header) {
+        length = 0;
+    }
 
     f = buf;
     while (buf[0]) {
@@ -120,7 +137,7 @@  int acpi_table_add(const char *t)
             fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
             goto out;
         }
-        acpi_hdr.length += s.st_size;
+        length += s.st_size;
         if (!n)
             break;
         *n = ':';
@@ -131,15 +148,19 @@  int acpi_table_add(const char *t)
         acpi_tables_len = sizeof(uint16_t);
         acpi_tables = qemu_mallocz(acpi_tables_len);
     }
+    acpi_tables = qemu_realloc(acpi_tables,
+                               acpi_tables_len + sizeof(uint16_t) + length);
     p = acpi_tables + acpi_tables_len;
-    acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
-    acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
+    acpi_tables_len += sizeof(uint16_t) + length;
 
-    acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
-    *(uint16_t*)p = acpi_hdr.length;
+    *(uint16_t*)p = cpu_to_le32(length);
     p += sizeof(uint16_t);
-    memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
-    off = sizeof(acpi_hdr);
+    if (load_header) {
+        off = 0;
+    } else {
+        off = sizeof(acpi_hdr);
+        memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
+    }
 
     f = buf;
     while (buf[0]) {
@@ -167,7 +188,7 @@  int acpi_table_add(const char *t)
                 close(fd);
                 goto out;
             }
-        } while(s.st_size);
+        } while(s.st_size && off < length);
 
         close(fd);
         if (!n)
@@ -175,7 +196,9 @@  int acpi_table_add(const char *t)
         f = n + 1;
     }
 
-    ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
+    acpi_hdr_p = (struct acpi_table_header*)p;
+    acpi_hdr_p->length = cpu_to_le32(length);
+    acpi_hdr_p->checksum = acpi_checksum((uint8_t*)p, off);
     /* increase number of tables */
     (*(uint16_t*)acpi_tables) =
 	    cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
diff --git a/qemu-options.hx b/qemu-options.hx
index a6928b7..cbb68ba 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -900,9 +900,11 @@  ETEXI
 
 DEF("acpitable", HAS_ARG, QEMU_OPTION_acpitable,
     "-acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,data=file1[:file2]...]\n"
+    "-acpitable [load_header][,data=file1[:file2]...]\n"
     "                ACPI table description\n", QEMU_ARCH_I386)
 STEXI
 @item -acpitable [sig=@var{str}][,rev=@var{n}][,oem_id=@var{str}][,oem_table_id=@var{str}][,oem_rev=@var{n}] [,asl_compiler_id=@var{str}][,asl_compiler_rev=@var{n}][,data=@var{file1}[:@var{file2}]...]
+@item -acpitable [load_header][,data=@var{file1}[:@var{file2}]...]
 @findex -acpitable
 Add ACPI table with specified header fields and context from specified files.
 ETEXI