render / rpms / qemu

Forked from rpms/qemu 10 months ago
Clone

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

46f3a5
From 6665e04d68d58a93d75a51a0840534f3a0ad2402 Mon Sep 17 00:00:00 2001
4ced99
From: "Richard W.M. Jones" <rjones@redhat.com>
723d95
Date: Tue, 19 Aug 2014 18:56:28 +0100
723d95
Subject: [PATCH] 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>
723d95
Reviewed-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
723d95
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
723d95
Message-id: 1407831259-2115-2-git-send-email-rjones@redhat.com
723d95
[PMM: removed stray space before ')']
723d95
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
723d95
723d95
(cherry picked from commit 235e74afcb85285a8e35e75f0cb6e6811267bb75)
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
723d95
index 2bf6b8f..0fde699 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 ||
723d95
+        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