dcavalca / rpms / qemu

Forked from rpms/qemu 11 months ago
Clone

Blame 0001-loader-Add-load_image_gzipped-function.patch

4ced99
From ddf2a3a69486376897ae654c8f1f0aa8cbae6c24 Mon Sep 17 00:00:00 2001
4ced99
From: "Richard W.M. Jones" <rjones@redhat.com>
4ced99
Date: Mon, 4 Aug 2014 12:25:08 +0100
4ced99
Subject: [PATCH 1/2] loader: Add load_image_gzipped function.
4ced99
MIME-Version: 1.0
4ced99
Content-Type: text/plain; charset=UTF-8
4ced99
Content-Transfer-Encoding: 8bit
4ced99
4ced99
As the name suggests this lets you load a ROM/disk image that is
4ced99
gzipped.  It is uncompressed before storing it in guest memory.
4ced99
4ced99
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
4ced99
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
4ced99
---
4ced99
 hw/core/loader.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
4ced99
 include/hw/loader.h |  1 +
4ced99
 2 files changed, 49 insertions(+)
4ced99
4ced99
diff --git a/hw/core/loader.c b/hw/core/loader.c
4ced99
index 2bf6b8f..83136e8 100644
4ced99
--- a/hw/core/loader.c
4ced99
+++ b/hw/core/loader.c
4ced99
@@ -577,6 +577,54 @@ int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
4ced99
     return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK);
4ced99
 }
4ced99
 
4ced99
+/* This simply prevents g_malloc in the function below from allocating
4ced99
+ * a huge amount of memory, by placing a limit on the maximum
4ced99
+ * uncompressed image size that load_image_gzipped will read.
4ced99
+ */
4ced99
+#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
4ced99
+
4ced99
+/* Load a gzip-compressed kernel. */
4ced99
+int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
4ced99
+{
4ced99
+    uint8_t *compressed_data = NULL;
4ced99
+    uint8_t *data = NULL;
4ced99
+    gsize len;
4ced99
+    ssize_t bytes;
4ced99
+    int ret = -1;
4ced99
+
4ced99
+    if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
4ced99
+                             NULL)) {
4ced99
+        goto out;
4ced99
+    }
4ced99
+
4ced99
+    /* Is it a gzip-compressed file? */
4ced99
+    if (len < 2 ||
4ced99
+        compressed_data[0] != 0x1f ||
4ced99
+        compressed_data[1] != 0x8b ) {
4ced99
+        goto out;
4ced99
+    }
4ced99
+
4ced99
+    if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
4ced99
+        max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
4ced99
+    }
4ced99
+
4ced99
+    data = g_malloc(max_sz);
4ced99
+    bytes = gunzip(data, max_sz, compressed_data, len);
4ced99
+    if (bytes < 0) {
4ced99
+        fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
4ced99
+                filename);
4ced99
+        goto out;
4ced99
+    }
4ced99
+
4ced99
+    rom_add_blob_fixed(filename, data, bytes, addr);
4ced99
+    ret = bytes;
4ced99
+
4ced99
+ out:
4ced99
+    g_free(compressed_data);
4ced99
+    g_free(data);
4ced99
+    return ret;
4ced99
+}
4ced99
+
4ced99
 /*
4ced99
  * Functions for reboot-persistent memory regions.
4ced99
  *  - used for vga bios and option roms.
4ced99
diff --git a/include/hw/loader.h b/include/hw/loader.h
4ced99
index 796cbf9..00c9117 100644
4ced99
--- a/include/hw/loader.h
4ced99
+++ b/include/hw/loader.h
4ced99
@@ -15,6 +15,7 @@ int get_image_size(const char *filename);
4ced99
 int load_image(const char *filename, uint8_t *addr); /* deprecated */
4ced99
 int load_image_targphys(const char *filename, hwaddr,
4ced99
                         uint64_t max_sz);
4ced99
+int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
4ced99
 
4ced99
 #define ELF_LOAD_FAILED       -1
4ced99
 #define ELF_LOAD_NOT_ELF      -2
4ced99
-- 
4ced99
2.0.4
4ced99