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