9ae3a8
From 54cd29fdf716fa676866b75b7faa32aff0cf7dac Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <54cd29fdf716fa676866b75b7faa32aff0cf7dac.1387298827.git.minovotn@redhat.com>
9ae3a8
In-Reply-To: <3ed0fb61a3dc912ef036d7ef450bed192090709e.1387298827.git.minovotn@redhat.com>
9ae3a8
References: <3ed0fb61a3dc912ef036d7ef450bed192090709e.1387298827.git.minovotn@redhat.com>
9ae3a8
From: "Michael S. Tsirkin" <mst@redhat.com>
9ae3a8
Date: Tue, 17 Dec 2013 15:18:34 +0100
9ae3a8
Subject: [PATCH 37/56] i386: add bios linker/loader
9ae3a8
9ae3a8
RH-Author: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
Message-id: <1387293161-4085-38-git-send-email-mst@redhat.com>
9ae3a8
Patchwork-id: 56343
9ae3a8
O-Subject: [PATCH qemu-kvm RHEL7.0 v2 37/57] i386: add bios linker/loader
9ae3a8
Bugzilla: 1034876
9ae3a8
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
RH-Acked-by: Marcel Apfelbaum <marcel.a@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
9ae3a8
This adds a dynamic bios linker/loader.
9ae3a8
This will be used by acpi table generation
9ae3a8
code to:
9ae3a8
    - load each table in the appropriate memory segment
9ae3a8
    - link tables to each other
9ae3a8
    - fix up checksums after said linking
9ae3a8
9ae3a8
Reviewed-by: Gerd Hoffmann <kraxel@redhat.com>
9ae3a8
Tested-by: Gerd Hoffmann <kraxel@redhat.com>
9ae3a8
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
Tested-by: Igor Mammedov <imammedo@redhat.com>
9ae3a8
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
(cherry picked from commit bc70232918ad3fb45c2b5423455a5de6bc7efdef)
9ae3a8
---
9ae3a8
 hw/i386/bios-linker-loader.h |  27 ++++++++
9ae3a8
 hw/i386/bios-linker-loader.c | 158 +++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 hw/i386/Makefile.objs        |   1 +
9ae3a8
 3 files changed, 186 insertions(+)
9ae3a8
 create mode 100644 hw/i386/bios-linker-loader.h
9ae3a8
 create mode 100644 hw/i386/bios-linker-loader.c
9ae3a8
9ae3a8
Signed-off-by: Michal Novotny <minovotn@redhat.com>
9ae3a8
---
9ae3a8
 hw/i386/Makefile.objs        |   1 +
9ae3a8
 hw/i386/bios-linker-loader.c | 158 +++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 hw/i386/bios-linker-loader.h |  27 ++++++++
9ae3a8
 3 files changed, 186 insertions(+)
9ae3a8
 create mode 100644 hw/i386/bios-linker-loader.c
9ae3a8
 create mode 100644 hw/i386/bios-linker-loader.h
9ae3a8
9ae3a8
diff --git a/hw/i386/Makefile.objs b/hw/i386/Makefile.objs
9ae3a8
index 013d250..71be2da 100644
9ae3a8
--- a/hw/i386/Makefile.objs
9ae3a8
+++ b/hw/i386/Makefile.objs
9ae3a8
@@ -4,6 +4,7 @@ obj-y += pc.o pc_piix.o pc_q35.o
9ae3a8
 obj-$(CONFIG_XEN) += xen_domainbuild.o xen_machine_pv.o
9ae3a8
 
9ae3a8
 obj-y += kvmvapic.o
9ae3a8
+obj-y += bios-linker-loader.o
9ae3a8
 
9ae3a8
 iasl-option=$(shell if test -z "`$(1) $(2) 2>&1 > /dev/null`" \
9ae3a8
     ; then echo "$(2)"; else echo "$(3)"; fi ;)
9ae3a8
diff --git a/hw/i386/bios-linker-loader.c b/hw/i386/bios-linker-loader.c
9ae3a8
new file mode 100644
9ae3a8
index 0000000..0833853
9ae3a8
--- /dev/null
9ae3a8
+++ b/hw/i386/bios-linker-loader.c
9ae3a8
@@ -0,0 +1,158 @@
9ae3a8
+/* Dynamic linker/loader of ACPI tables
9ae3a8
+ *
9ae3a8
+ * Copyright (C) 2013 Red Hat Inc
9ae3a8
+ *
9ae3a8
+ * Author: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
+ *
9ae3a8
+ * This program is free software; you can redistribute it and/or modify
9ae3a8
+ * it under the terms of the GNU General Public License as published by
9ae3a8
+ * the Free Software Foundation; either version 2 of the License, or
9ae3a8
+ * (at your option) any later version.
9ae3a8
+
9ae3a8
+ * This program is distributed in the hope that it will be useful,
9ae3a8
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9ae3a8
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9ae3a8
+ * GNU General Public License for more details.
9ae3a8
+
9ae3a8
+ * You should have received a copy of the GNU General Public License along
9ae3a8
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
9ae3a8
+ */
9ae3a8
+
9ae3a8
+#include "bios-linker-loader.h"
9ae3a8
+#include "hw/nvram/fw_cfg.h"
9ae3a8
+
9ae3a8
+#include <string.h>
9ae3a8
+#include <assert.h>
9ae3a8
+#include "qemu/bswap.h"
9ae3a8
+
9ae3a8
+#define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
9ae3a8
+
9ae3a8
+struct BiosLinkerLoaderEntry {
9ae3a8
+    uint32_t command;
9ae3a8
+    union {
9ae3a8
+        /*
9ae3a8
+         * COMMAND_ALLOCATE - allocate a table from @alloc.file
9ae3a8
+         * subject to @alloc.align alignment (must be power of 2)
9ae3a8
+         * and @alloc.zone (can be HIGH or FSEG) requirements.
9ae3a8
+         *
9ae3a8
+         * Must appear exactly once for each file, and before
9ae3a8
+         * this file is referenced by any other command.
9ae3a8
+         */
9ae3a8
+        struct {
9ae3a8
+            char file[BIOS_LINKER_LOADER_FILESZ];
9ae3a8
+            uint32_t align;
9ae3a8
+            uint8_t zone;
9ae3a8
+        } alloc;
9ae3a8
+
9ae3a8
+        /*
9ae3a8
+         * COMMAND_ADD_POINTER - patch the table (originating from
9ae3a8
+         * @dest_file) at @pointer.offset, by adding a pointer to the table
9ae3a8
+         * originating from @src_file. 1,2,4 or 8 byte unsigned
9ae3a8
+         * addition is used depending on @pointer.size.
9ae3a8
+         */
9ae3a8
+        struct {
9ae3a8
+            char dest_file[BIOS_LINKER_LOADER_FILESZ];
9ae3a8
+            char src_file[BIOS_LINKER_LOADER_FILESZ];
9ae3a8
+            uint32_t offset;
9ae3a8
+            uint8_t size;
9ae3a8
+        } pointer;
9ae3a8
+
9ae3a8
+        /*
9ae3a8
+         * COMMAND_ADD_CHECKSUM - calculate checksum of the range specified by
9ae3a8
+         * @cksum_start and @cksum_length fields,
9ae3a8
+         * and then add the value at @cksum.offset.
9ae3a8
+         * Checksum simply sums -X for each byte X in the range
9ae3a8
+         * using 8-bit math.
9ae3a8
+         */
9ae3a8
+        struct {
9ae3a8
+            char file[BIOS_LINKER_LOADER_FILESZ];
9ae3a8
+            uint32_t offset;
9ae3a8
+            uint32_t start;
9ae3a8
+            uint32_t length;
9ae3a8
+        } cksum;
9ae3a8
+
9ae3a8
+        /* padding */
9ae3a8
+        char pad[124];
9ae3a8
+    };
9ae3a8
+} QEMU_PACKED;
9ae3a8
+typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry;
9ae3a8
+
9ae3a8
+enum {
9ae3a8
+    BIOS_LINKER_LOADER_COMMAND_ALLOCATE     = 0x1,
9ae3a8
+    BIOS_LINKER_LOADER_COMMAND_ADD_POINTER  = 0x2,
9ae3a8
+    BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3,
9ae3a8
+};
9ae3a8
+
9ae3a8
+enum {
9ae3a8
+    BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
9ae3a8
+    BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
9ae3a8
+};
9ae3a8
+
9ae3a8
+GArray *bios_linker_loader_init(void)
9ae3a8
+{
9ae3a8
+    return g_array_new(false, true /* clear */, sizeof(BiosLinkerLoaderEntry));
9ae3a8
+}
9ae3a8
+
9ae3a8
+/* Free linker wrapper and return the linker array. */
9ae3a8
+void *bios_linker_loader_cleanup(GArray *linker)
9ae3a8
+{
9ae3a8
+    return g_array_free(linker, false);
9ae3a8
+}
9ae3a8
+
9ae3a8
+void bios_linker_loader_alloc(GArray *linker,
9ae3a8
+                              const char *file,
9ae3a8
+                              uint32_t alloc_align,
9ae3a8
+                              bool alloc_fseg)
9ae3a8
+{
9ae3a8
+    BiosLinkerLoaderEntry entry;
9ae3a8
+
9ae3a8
+    memset(&entry, 0, sizeof entry);
9ae3a8
+    strncpy(entry.alloc.file, file, sizeof entry.alloc.file - 1);
9ae3a8
+    entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE);
9ae3a8
+    entry.alloc.align = cpu_to_le32(alloc_align);
9ae3a8
+    entry.alloc.zone = cpu_to_le32(alloc_fseg ?
9ae3a8
+                                    BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG :
9ae3a8
+                                    BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH);
9ae3a8
+
9ae3a8
+    /* Alloc entries must come first, so prepend them */
9ae3a8
+    g_array_prepend_val(linker, entry);
9ae3a8
+}
9ae3a8
+
9ae3a8
+void bios_linker_loader_add_checksum(GArray *linker, const char *file,
9ae3a8
+                                     void *table,
9ae3a8
+                                     void *start, unsigned size,
9ae3a8
+                                     uint8_t *checksum)
9ae3a8
+{
9ae3a8
+    BiosLinkerLoaderEntry entry;
9ae3a8
+
9ae3a8
+    memset(&entry, 0, sizeof entry);
9ae3a8
+    strncpy(entry.cksum.file, file, sizeof entry.cksum.file - 1);
9ae3a8
+    entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM);
9ae3a8
+    entry.cksum.offset = cpu_to_le32(checksum - (uint8_t *)table);
9ae3a8
+    entry.cksum.start = cpu_to_le32((uint8_t *)start - (uint8_t *)table);
9ae3a8
+    entry.cksum.length = cpu_to_le32(size);
9ae3a8
+
9ae3a8
+    g_array_append_val(linker, entry);
9ae3a8
+}
9ae3a8
+
9ae3a8
+void bios_linker_loader_add_pointer(GArray *linker,
9ae3a8
+                                    const char *dest_file,
9ae3a8
+                                    const char *src_file,
9ae3a8
+                                    GArray *table, void *pointer,
9ae3a8
+                                    uint8_t pointer_size)
9ae3a8
+{
9ae3a8
+    BiosLinkerLoaderEntry entry;
9ae3a8
+
9ae3a8
+    memset(&entry, 0, sizeof entry);
9ae3a8
+    strncpy(entry.pointer.dest_file, dest_file,
9ae3a8
+            sizeof entry.pointer.dest_file - 1);
9ae3a8
+    strncpy(entry.pointer.src_file, src_file,
9ae3a8
+            sizeof entry.pointer.src_file - 1);
9ae3a8
+    entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_POINTER);
9ae3a8
+    entry.pointer.offset = cpu_to_le32((gchar *)pointer - table->data);
9ae3a8
+    entry.pointer.size = pointer_size;
9ae3a8
+    assert(pointer_size == 1 || pointer_size == 2 ||
9ae3a8
+           pointer_size == 4 || pointer_size == 8);
9ae3a8
+
9ae3a8
+    g_array_append_val(linker, entry);
9ae3a8
+}
9ae3a8
diff --git a/hw/i386/bios-linker-loader.h b/hw/i386/bios-linker-loader.h
9ae3a8
new file mode 100644
9ae3a8
index 0000000..498c0af
9ae3a8
--- /dev/null
9ae3a8
+++ b/hw/i386/bios-linker-loader.h
9ae3a8
@@ -0,0 +1,27 @@
9ae3a8
+#ifndef BIOS_LINKER_LOADER_H
9ae3a8
+#define BIOS_LINKER_LOADER_H
9ae3a8
+
9ae3a8
+#include <glib.h>
9ae3a8
+#include <stdbool.h>
9ae3a8
+#include <inttypes.h>
9ae3a8
+
9ae3a8
+GArray *bios_linker_loader_init(void);
9ae3a8
+
9ae3a8
+void bios_linker_loader_alloc(GArray *linker,
9ae3a8
+                              const char *file,
9ae3a8
+                              uint32_t alloc_align,
9ae3a8
+                              bool alloc_fseg);
9ae3a8
+
9ae3a8
+void bios_linker_loader_add_checksum(GArray *linker, const char *file,
9ae3a8
+                                     void *table,
9ae3a8
+                                     void *start, unsigned size,
9ae3a8
+                                     uint8_t *checksum);
9ae3a8
+
9ae3a8
+void bios_linker_loader_add_pointer(GArray *linker,
9ae3a8
+                                    const char *dest_file,
9ae3a8
+                                    const char *src_file,
9ae3a8
+                                    GArray *table, void *pointer,
9ae3a8
+                                    uint8_t pointer_size);
9ae3a8
+
9ae3a8
+void *bios_linker_loader_cleanup(GArray *linker);
9ae3a8
+#endif
9ae3a8
-- 
9ae3a8
1.7.11.7
9ae3a8