qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v7 0/2] aarch64: Allow -kernel option to take a gzip-compressed kernel.
@ 2014-08-08 19:17 Richard W.M. Jones
  2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 1/2] loader: Add load_image_gzipped function Richard W.M. Jones
  2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 2/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
  0 siblings, 2 replies; 5+ messages in thread
From: Richard W.M. Jones @ 2014-08-08 19:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: peter.maydell, peter.crosthwaite, alex.bennee

Since v6:

 - No changes to the code.

 - Rebased against HEAD.

 - Retested on aarch64.

 - Added Reviewed-by tag.

Rich.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v7 1/2] loader: Add load_image_gzipped function.
  2014-08-08 19:17 [Qemu-devel] [PATCH v7 0/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
@ 2014-08-08 19:17 ` Richard W.M. Jones
  2014-08-12  1:51   ` Peter Crosthwaite
  2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 2/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Richard W.M. Jones @ 2014-08-08 19:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: peter.maydell, peter.crosthwaite, alex.bennee

As the name suggests this lets you load a ROM/disk image that is
gzipped.  It is uncompressed before storing it in guest memory.

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
---
 hw/core/loader.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h |  1 +
 2 files changed, 49 insertions(+)

diff --git a/hw/core/loader.c b/hw/core/loader.c
index 2bf6b8f..83136e8 100644
--- a/hw/core/loader.c
+++ b/hw/core/loader.c
@@ -577,6 +577,54 @@ int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
     return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK);
 }
 
+/* This simply prevents g_malloc in the function below from allocating
+ * a huge amount of memory, by placing a limit on the maximum
+ * uncompressed image size that load_image_gzipped will read.
+ */
+#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
+
+/* Load a gzip-compressed kernel. */
+int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
+{
+    uint8_t *compressed_data = NULL;
+    uint8_t *data = NULL;
+    gsize len;
+    ssize_t bytes;
+    int ret = -1;
+
+    if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
+                             NULL)) {
+        goto out;
+    }
+
+    /* Is it a gzip-compressed file? */
+    if (len < 2 ||
+        compressed_data[0] != 0x1f ||
+        compressed_data[1] != 0x8b ) {
+        goto out;
+    }
+
+    if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
+        max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
+    }
+
+    data = g_malloc(max_sz);
+    bytes = gunzip(data, max_sz, compressed_data, len);
+    if (bytes < 0) {
+        fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
+                filename);
+        goto out;
+    }
+
+    rom_add_blob_fixed(filename, data, bytes, addr);
+    ret = bytes;
+
+ out:
+    g_free(compressed_data);
+    g_free(data);
+    return ret;
+}
+
 /*
  * Functions for reboot-persistent memory regions.
  *  - used for vga bios and option roms.
diff --git a/include/hw/loader.h b/include/hw/loader.h
index 796cbf9..00c9117 100644
--- a/include/hw/loader.h
+++ b/include/hw/loader.h
@@ -15,6 +15,7 @@ int get_image_size(const char *filename);
 int load_image(const char *filename, uint8_t *addr); /* deprecated */
 int load_image_targphys(const char *filename, hwaddr,
                         uint64_t max_sz);
+int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
 
 #define ELF_LOAD_FAILED       -1
 #define ELF_LOAD_NOT_ELF      -2
-- 
2.0.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v7 2/2] aarch64: Allow -kernel option to take a gzip-compressed kernel.
  2014-08-08 19:17 [Qemu-devel] [PATCH v7 0/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
  2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 1/2] loader: Add load_image_gzipped function Richard W.M. Jones
@ 2014-08-08 19:17 ` Richard W.M. Jones
  2014-08-12  1:50   ` Peter Crosthwaite
  1 sibling, 1 reply; 5+ messages in thread
From: Richard W.M. Jones @ 2014-08-08 19:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: peter.maydell, peter.crosthwaite, alex.bennee

On aarch64 it is the bootloader's job to uncompress the kernel.  UEFI
and u-boot bootloaders do this automatically when the kernel is
gzip-compressed.

However the qemu -kernel option does not do this.  The following
command does not work:

  qemu-system-aarch64 [...] -kernel /boot/vmlinuz

because it tries to execute the gzip-compressed data.

This commit lets gzip-compressed kernels be uncompressed
transparently.

Currently this is only done when emulating aarch64.

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
---
 hw/arm/boot.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index 1241761..c71c4d5 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -448,6 +448,7 @@ static void do_cpu_reset(void *opaque)
 void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
 {
     CPUState *cs = CPU(cpu);
+    int allow_compressed_kernels = 0;
     int kernel_size;
     int initrd_size;
     int is_linux = 0;
@@ -469,6 +470,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         primary_loader = bootloader_aarch64;
         kernel_load_offset = KERNEL64_LOAD_ADDR;
         elf_machine = EM_AARCH64;
+        allow_compressed_kernels = 1;
     } else {
         primary_loader = bootloader;
         kernel_load_offset = KERNEL_LOAD_ADDR;
@@ -514,6 +516,13 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
                                   &is_linux);
     }
+    /* On aarch64, it's the bootloader's job to uncompress the kernel. */
+    if (allow_compressed_kernels && kernel_size < 0) {
+        entry = info->loader_start + kernel_load_offset;
+        kernel_size = load_image_gzipped(info->kernel_filename, entry,
+                                         info->ram_size - kernel_load_offset);
+        is_linux = 1;
+    }
     if (kernel_size < 0) {
         entry = info->loader_start + kernel_load_offset;
         kernel_size = load_image_targphys(info->kernel_filename, entry,
-- 
2.0.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v7 2/2] aarch64: Allow -kernel option to take a gzip-compressed kernel.
  2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 2/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
@ 2014-08-12  1:50   ` Peter Crosthwaite
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Crosthwaite @ 2014-08-12  1:50 UTC (permalink / raw)
  To: Richard W.M. Jones
  Cc: Peter Maydell, Alex Bennée, qemu-devel@nongnu.org Developers

On Sat, Aug 9, 2014 at 5:17 AM, Richard W.M. Jones <rjones@redhat.com> wrote:
> On aarch64 it is the bootloader's job to uncompress the kernel.  UEFI
> and u-boot bootloaders do this automatically when the kernel is
> gzip-compressed.
>
> However the qemu -kernel option does not do this.  The following
> command does not work:
>
>   qemu-system-aarch64 [...] -kernel /boot/vmlinuz
>
> because it tries to execute the gzip-compressed data.
>
> This commit lets gzip-compressed kernels be uncompressed
> transparently.
>
> Currently this is only done when emulating aarch64.
>
> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  hw/arm/boot.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/hw/arm/boot.c b/hw/arm/boot.c
> index 1241761..c71c4d5 100644
> --- a/hw/arm/boot.c
> +++ b/hw/arm/boot.c
> @@ -448,6 +448,7 @@ static void do_cpu_reset(void *opaque)
>  void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
>  {
>      CPUState *cs = CPU(cpu);
> +    int allow_compressed_kernels = 0;

Use a bool. And I think this can be better names is_aarch64 (or just
dropped completely and users use arm_feature(ARM_FEATURE_AARCH64)
inline).

Otherwise:

Reviewed-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>

>      int kernel_size;
>      int initrd_size;
>      int is_linux = 0;
> @@ -469,6 +470,7 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
>          primary_loader = bootloader_aarch64;
>          kernel_load_offset = KERNEL64_LOAD_ADDR;
>          elf_machine = EM_AARCH64;
> +        allow_compressed_kernels = 1;
>      } else {
>          primary_loader = bootloader;
>          kernel_load_offset = KERNEL_LOAD_ADDR;
> @@ -514,6 +516,13 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
>          kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
>                                    &is_linux);
>      }
> +    /* On aarch64, it's the bootloader's job to uncompress the kernel. */
> +    if (allow_compressed_kernels && kernel_size < 0) {
> +        entry = info->loader_start + kernel_load_offset;
> +        kernel_size = load_image_gzipped(info->kernel_filename, entry,
> +                                         info->ram_size - kernel_load_offset);
> +        is_linux = 1;
> +    }
>      if (kernel_size < 0) {
>          entry = info->loader_start + kernel_load_offset;
>          kernel_size = load_image_targphys(info->kernel_filename, entry,
> --
> 2.0.4
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v7 1/2] loader: Add load_image_gzipped function.
  2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 1/2] loader: Add load_image_gzipped function Richard W.M. Jones
@ 2014-08-12  1:51   ` Peter Crosthwaite
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Crosthwaite @ 2014-08-12  1:51 UTC (permalink / raw)
  To: Richard W.M. Jones
  Cc: Peter Maydell, Alex Bennée, qemu-devel@nongnu.org Developers

On Sat, Aug 9, 2014 at 5:17 AM, Richard W.M. Jones <rjones@redhat.com> wrote:
> As the name suggests this lets you load a ROM/disk image that is
> gzipped.  It is uncompressed before storing it in guest memory.
>
> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

Reviewed-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>

> ---
>  hw/core/loader.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
>  include/hw/loader.h |  1 +
>  2 files changed, 49 insertions(+)
>
> diff --git a/hw/core/loader.c b/hw/core/loader.c
> index 2bf6b8f..83136e8 100644
> --- a/hw/core/loader.c
> +++ b/hw/core/loader.c
> @@ -577,6 +577,54 @@ int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
>      return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK);
>  }
>
> +/* This simply prevents g_malloc in the function below from allocating
> + * a huge amount of memory, by placing a limit on the maximum
> + * uncompressed image size that load_image_gzipped will read.
> + */
> +#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
> +
> +/* Load a gzip-compressed kernel. */
> +int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
> +{
> +    uint8_t *compressed_data = NULL;
> +    uint8_t *data = NULL;
> +    gsize len;
> +    ssize_t bytes;
> +    int ret = -1;
> +
> +    if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
> +                             NULL)) {
> +        goto out;
> +    }
> +
> +    /* Is it a gzip-compressed file? */
> +    if (len < 2 ||
> +        compressed_data[0] != 0x1f ||
> +        compressed_data[1] != 0x8b ) {
> +        goto out;
> +    }
> +
> +    if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
> +        max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
> +    }
> +
> +    data = g_malloc(max_sz);
> +    bytes = gunzip(data, max_sz, compressed_data, len);
> +    if (bytes < 0) {
> +        fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
> +                filename);
> +        goto out;
> +    }
> +
> +    rom_add_blob_fixed(filename, data, bytes, addr);
> +    ret = bytes;
> +
> + out:
> +    g_free(compressed_data);
> +    g_free(data);
> +    return ret;
> +}
> +
>  /*
>   * Functions for reboot-persistent memory regions.
>   *  - used for vga bios and option roms.
> diff --git a/include/hw/loader.h b/include/hw/loader.h
> index 796cbf9..00c9117 100644
> --- a/include/hw/loader.h
> +++ b/include/hw/loader.h
> @@ -15,6 +15,7 @@ int get_image_size(const char *filename);
>  int load_image(const char *filename, uint8_t *addr); /* deprecated */
>  int load_image_targphys(const char *filename, hwaddr,
>                          uint64_t max_sz);
> +int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz);
>
>  #define ELF_LOAD_FAILED       -1
>  #define ELF_LOAD_NOT_ELF      -2
> --
> 2.0.4
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-08-12  1:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-08 19:17 [Qemu-devel] [PATCH v7 0/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 1/2] loader: Add load_image_gzipped function Richard W.M. Jones
2014-08-12  1:51   ` Peter Crosthwaite
2014-08-08 19:17 ` [Qemu-devel] [PATCH v7 2/2] aarch64: Allow -kernel option to take a gzip-compressed kernel Richard W.M. Jones
2014-08-12  1:50   ` Peter Crosthwaite

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).