Skip to contentSkip to conversation

xen/arm: Support compressed uImages

PATCH In review Michal Orzel 2 participants 8 messages Feb 2, 2023 – Feb 8, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: Support compressed uImages

This series adds support for booting gzip compressed images with u-boot header. Currently Xen does not support such images because we are trying to decompress the kernel before probing uImage header.

The problem can be solved using 2 different approaches:
1) Split uImage probing into 2 stages. The first stage is called before decompression, does the usual probing and sets up correctly module start address and size by taking the uImage header size into account. The second stage is called after decompression to update the zimage.{kernel_addr,len}.

2) Call the decompression function (modified to take gzip offset) from within the uImage probing to avoid the split and to make the function self-containing. This way the only case for falling through to try to probe other image types is when there is no u-boot header detected.

In this series the second approach is taken that results in a better looking code.

Michal Orzel (2):
xen/arm: Move kernel_uimage_probe definition after kernel_decompress xen/arm: Add support for booting gzip compressed uImages

 docs/misc/arm/booting.txt |   3 -
 xen/arch/arm/kernel.c     | 225 ++++++++++++++++++++++++--------------
 2 files changed, 144 insertions(+), 84 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 xen/arm: Move kernel_uimage_probe definition after kernel_decompress

In a follow-up patch, we will be calling kernel_decompress function from within kernel_uimage_probe to support booting compressed images with u-boot header. Therefore, move the kernel_uimage_probe definition after kernel_decompress so that the latter is visible to the former.

No functional change intended.

---
Changes in v2:
- add Rb and Ab

---
 xen/arch/arm/kernel.c | 146 +++++++++++++++++++++---------------------
 1 file changed, 73 insertions(+), 73 deletions(-)
xen/arch/arm/kernel.c +73 -73
index 36081e73f140..068fbf88e492 100644
@@ -186,6 +186,79 @@ static void __init kernel_zimage_load(struct kernel_info *info)
     iounmap(kernel);
 }
 
+static __init uint32_t output_length(char *image, unsigned long image_len)
+{
+    return *(uint32_t *)&image[image_len - 4];
+}
+
+static __init int kernel_decompress(struct bootmodule *mod)
+{
+    char *output, *input;
+    char magic[2];
+    int rc;
+    unsigned int kernel_order_out;
+    paddr_t output_size;
+    struct page_info *pages;
+    mfn_t mfn;
+    int i;
+    paddr_t addr = mod->start;
+    paddr_t size = mod->size;
+
+    if ( size < 2 )
+        return -EINVAL;
+
+    copy_from_paddr(magic, addr, sizeof(magic));
+
+    /* only gzip is supported */
+    if ( !gzip_check(magic, size) )
+        return -EINVAL;
+
+    input = ioremap_cache(addr, size);
+    if ( input == NULL )
+        return -EFAULT;
+
+    output_size = output_length(input, size);
+    kernel_order_out = get_order_from_bytes(output_size);
+    pages = alloc_domheap_pages(NULL, kernel_order_out, 0);
+    if ( pages == NULL )
+    {
+        iounmap(input);
+        return -ENOMEM;
+    }
+    mfn = page_to_mfn(pages);
+    output = __vmap(&mfn, 1 << kernel_order_out, 1, 1, PAGE_HYPERVISOR, VMAP_DEFAULT);
+
+    rc = perform_gunzip(output, input, size);
+    clean_dcache_va_range(output, output_size);
+    iounmap(input);
+    vunmap(output);
+
+    if ( rc )
+    {
+        free_domheap_pages(pages, kernel_order_out);
+        return rc;
+    }
+
+    mod->start = page_to_maddr(pages);
+    mod->size = output_size;
+
+    /*
+     * Need to free pages after output_size here because they won't be
+     * freed by discard_initial_modules
+     */
+    i = PFN_UP(output_size);
+    for ( ; i < (1 << kernel_order_out); i++ )
+        free_domheap_page(pages + i);
+
+    /*
+     * Free the original kernel, update the pointers to the
+     * decompressed kernel
+     */
+    fw_unreserved_regions(addr, addr + size, init_domheap_pages, 0);
+
+    return 0;
+}
+
 /*
  * Uimage CPU Architecture Codes
  */
@@ -289,79 +362,6 @@ static int __init kernel_uimage_probe(struct kernel_info *info,
     return 0;
 }
 
-static __init uint32_t output_length(char *image, unsigned long image_len)
-{
-    return *(uint32_t *)&image[image_len - 4];
-}
-
-static __init int kernel_decompress(struct bootmodule *mod)
-{
-    char *output, *input;
-    char magic[2];
-    int rc;
-    unsigned int kernel_order_out;
-    paddr_t output_size;
-    struct page_info *pages;
-    mfn_t mfn;
-    int i;
-    paddr_t addr = mod->start;
-    paddr_t size = mod->size;
-
-    if ( size < 2 )
-        return -EINVAL;
-
-    copy_from_paddr(magic, addr, sizeof(magic));
-
-    /* only gzip is supported */
-    if ( !gzip_check(magic, size) )
-        return -EINVAL;
-
-    input = ioremap_cache(addr, size);
-    if ( input == NULL )
-        return -EFAULT;
-
-    output_size = output_length(input, size);
-    kernel_order_out = get_order_from_bytes(output_size);
-    pages = alloc_domheap_pages(NULL, kernel_order_out, 0);
-    if ( pages == NULL )
-    {
-        iounmap(input);
-        return -ENOMEM;
-    }
-    mfn = page_to_mfn(pages);
-    output = __vmap(&mfn, 1 << kernel_order_out, 1, 1, PAGE_HYPERVISOR, VMAP_DEFAULT);
-
-    rc = perform_gunzip(output, input, size);
-    clean_dcache_va_range(output, output_size);
-    iounmap(input);
-    vunmap(output);
-
-    if ( rc )
-    {
-        free_domheap_pages(pages, kernel_order_out);
-        return rc;
-    }
-
-    mod->start = page_to_maddr(pages);
-    mod->size = output_size;
-
-    /*
-     * Need to free pages after output_size here because they won't be
-     * freed by discard_initial_modules
-     */
-    i = PFN_UP(output_size);
-    for ( ; i < (1 << kernel_order_out); i++ )
-        free_domheap_page(pages + i);
-
-    /*
-     * Free the original kernel, update the pointers to the
-     * decompressed kernel
-     */
-    fw_unreserved_regions(addr, addr + size, init_domheap_pages, 0);
-
-    return 0;
-}
-
 #ifdef CONFIG_ARM_64
 /*
  * Check if the image is a 64-bit Image.
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/2 xen/arm: Add support for booting gzip compressed uImages

In reply to Michal Orzel

At the moment, Xen does not support booting gzip compressed uImages. This is because we are trying to decompress the kernel before probing the u-boot header. This leads to a failure as the header always appears at the top of the image (and therefore obscuring the gzip header).

Move the call to kernel_uimage_probe before kernel_decompress and make the function self-containing by taking the following actions:
- take a pointer to struct bootmodule as a parameter,
- check the comp field of a u-boot header to determine compression type,
- in case of compressed image, call kernel_decompress passing uImage header size as an offset to gzip header,
- set up zimage.{kernel_addr,len} accordingly,
- return -ENOENT in case of a u-boot header not found to distinguish it amongst other return values and make it the only case for falling through to try to probe other image types.

Modify kernel_decompress to take an additional parameter being an offset to a gzip header from start address. This is needed so that a function can first operate on a region containing actually compressed kernel (in case of compressed uImage, size of u-boot header is an offset to a gzip header) and then at the end pass the entire region (as it was before taking an offset into account) to fw_unreserved_regions for freeing.

This approach avoids splitting the uImage probing into 2 stages (executed before and after decompression) which otherwise would be necessary to properly parse header, update boot module start and size before decompression and update zimage.{kernel_addr,len} afterwards.

Remove the limitation from the booting.txt documentation.

---
Changes in v2:
- modify kernel_decompress to take an offset to gzip header

---
 docs/misc/arm/booting.txt |  3 --
 xen/arch/arm/kernel.c     | 81 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 72 insertions(+), 12 deletions(-)
docs/misc/arm/booting.txt +0 -3
index bd7bfe7f284a..02f7bb65ec6d 100644
@@ -50,9 +50,6 @@ Also, it is to be noted that if user provides the legacy image header on
 top of zImage or Image header, then Xen uses the attributes of legacy
 image header to determine the load address, entry point, etc.
 
-Known limitation: compressed kernels with a uboot headers are not
-working.
-
 
 Firmware/bootloader requirements
 --------------------------------
xen/arch/arm/kernel.c +72 -9

145 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2 2/2] xen/arm: Add support for booting gzip compressed uImages

Hi Michal,

Quoted 17 lines · Michal Orzel
xen/arch/arm/kernel.c +4 -1
@@ -265,11 +284,14 @@ static __init int kernel_decompress(struct bootmodule *mod)
  #define IH_ARCH_ARM             2       /* ARM          */
  #define IH_ARCH_ARM64           22      /* ARM64        */
 
+/* uImage Compression Types */
+#define IH_COMP_GZIP            1
+
  /*
   * Check if the image is a uImage and setup kernel_info
   */
  static int __init kernel_uimage_probe(struct kernel_info *info,
-                                      paddr_t addr, paddr_t size)
+                                      struct bootmodule *mod)
  {
      struct {
          __be32 magic;   /* Image Header Magic Number */

xen/arch/arm/kernel.c static int __init kernel_uimage_probe(struct kernel_info *info, · line 316

Quoted 8 lines
xen/arch/arm/kernel.c +2 -0
@@ -287,6 +309,8 @@ static int __init kernel_uimage_probe(struct kernel_info *info,
      } uimage;
 
      uint32_t len;
+    paddr_t addr = mod->start;
+    paddr_t size = mod->size;
 
      if ( size < sizeof(uimage) )
          return -EINVAL;

Shouldn't we return -ENOENT here?

The rest look good to me.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

Re: [PATCH v2 2/2] xen/arm: Add support for booting gzip compressed uImages

Hi Julien,

Quoted 24 lines · Julien Grall

Hi Michal,

Quoted 19 lines · Michal Orzel
xen/arch/arm/kernel.c +4 -1
@@ -265,11 +284,14 @@ static __init int kernel_decompress(struct bootmodule *mod)
  #define IH_ARCH_ARM             2       /* ARM          */
  #define IH_ARCH_ARM64           22      /* ARM64        */
 
+/* uImage Compression Types */
+#define IH_COMP_GZIP            1
+
  /*
   * Check if the image is a uImage and setup kernel_info
   */
  static int __init kernel_uimage_probe(struct kernel_info *info,
-                                      paddr_t addr, paddr_t size)
+                                      struct bootmodule *mod)
  {
      struct {
          __be32 magic;   /* Image Header Magic Number */
@@ -287,6 +309,8 @@ static int __init kernel_uimage_probe(struct kernel_info *info,
      } uimage;
 
Quoted 8 lines

xen/arch/arm/kernel.c static int __init kernel_uimage_probe(struct kernel_info *info, · line 316

Quoted 6 lines
xen/arch/arm/kernel.c +2 -0
      uint32_t len;
+    paddr_t addr = mod->start;
+    paddr_t size = mod->size;
 
      if ( size < sizeof(uimage) )
          return -EINVAL;

Shouldn't we return -ENOENT here?

Frankly speaking, I do not want to fall through in such a case. If a kernel size is less than 64B, something is wrong, isn't it? I am not sure if Xen would handle a kernel whose size is less than a page.

I do not like the whole falling through in kernel_probe even in case of obvious violations. But this is something not related to this series so I added to my TODO to properly handle the return types from kernel_probe path. If you really think, we should return -ENOENT here, then ok (although I do not like it). Could this be done on commit if you insist on that?

Quoted 2 lines

The rest look good to me.

Thanks,

Quoted 5 lines

Cheers,

--
Julien Grall

~Michal

#5 lore raw

Re: [PATCH v2 2/2] xen/arm: Add support for booting gzip compressed uImages

Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 30 lines · Julien Grall
Quoted 28 lines

Hi Michal,

Quoted 23 lines · Michal Orzel
xen/arch/arm/kernel.c +6 -1
@@ -265,11 +284,14 @@ static __init int kernel_decompress(struct bootmodule *mod)
   #define IH_ARCH_ARM             2       /* ARM          */
   #define IH_ARCH_ARM64           22      /* ARM64        */
 
+/* uImage Compression Types */
+#define IH_COMP_GZIP            1
+
   /*
    * Check if the image is a uImage and setup kernel_info
    */
   static int __init kernel_uimage_probe(struct kernel_info *info,
-                                      paddr_t addr, paddr_t size)
+                                      struct bootmodule *mod)
   {
       struct {
           __be32 magic;   /* Image Header Magic Number */
@@ -287,6 +309,8 @@ static int __init kernel_uimage_probe(struct kernel_info *info,
       } uimage;
 
       uint32_t len;
+    paddr_t addr = mod->start;
+    paddr_t size = mod->size;
 
Quoted 6 lines
Quoted 4 lines

xen/arch/arm/kernel.c static int __init kernel_uimage_probe(struct kernel_info *info, · line 316

Quoted 2 lines
xen/arch/arm/kernel.c +0 -0
       if ( size < sizeof(uimage) )
           return -EINVAL;

Shouldn't we return -ENOENT here?

Frankly speaking, I do not want to fall through in such a case. If a kernel size is less than 64B, something is wrong, isn't it?

I agree something is likely wrong but this should not be the job of kernel_uimage_probe() to enforce this for everyone.

To give a concrete example, let's imagine we decide to re-order the call so kernel_uimage_probe() happens *after* an new header A than would require 128 bytes (the number is made up).

It would be wrong for A to return -EINVAL because the other protocol may require a smaller size. The same goes here even at least for consistency.

So if you really want to enforce a minimum size, then such check should be in the caller. Then each probe could return -ENOENT if the size is too small.

Quoted 1 line

I am not sure if Xen would handle a kernel whose size is less than a page.

I don't see any reason why it would not be.

Quoted 5 lines

I do not like the whole falling through in kernel_probe even in case of obvious violations. But this is something not related to this series so I added to my TODO to properly handle the return types from kernel_probe path. If you really think, we should return -ENOENT here, then ok (although I do not like it). Could this be done on commit if you insist on that?

See above for an alternative proposal. Depending on the version we settle on I can do it on commit (but this is not going to happen today as OSSTEst is still blocked).

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

Re: [PATCH v2 2/2] xen/arm: Add support for booting gzip compressed uImages

Hi Julien,

Quoted 63 lines · Julien Grall
Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 30 lines · Julien Grall
Quoted 28 lines

Hi Michal,

Quoted 23 lines · Michal Orzel
xen/arch/arm/kernel.c +6 -1
@@ -265,11 +284,14 @@ static __init int kernel_decompress(struct bootmodule *mod)
   #define IH_ARCH_ARM             2       /* ARM          */
   #define IH_ARCH_ARM64           22      /* ARM64        */
 
+/* uImage Compression Types */
+#define IH_COMP_GZIP            1
+
   /*
    * Check if the image is a uImage and setup kernel_info
    */
   static int __init kernel_uimage_probe(struct kernel_info *info,
-                                      paddr_t addr, paddr_t size)
+                                      struct bootmodule *mod)
   {
       struct {
           __be32 magic;   /* Image Header Magic Number */
@@ -287,6 +309,8 @@ static int __init kernel_uimage_probe(struct kernel_info *info,
       } uimage;
 
       uint32_t len;
+    paddr_t addr = mod->start;
+    paddr_t size = mod->size;
 
Quoted 6 lines
Quoted 4 lines

xen/arch/arm/kernel.c static int __init kernel_uimage_probe(struct kernel_info *info, · line 316

Quoted 2 lines
xen/arch/arm/kernel.c +0 -0
       if ( size < sizeof(uimage) )
           return -EINVAL;

Shouldn't we return -ENOENT here?

Frankly speaking, I do not want to fall through in such a case. If a kernel size is less than 64B, something is wrong, isn't it?

I agree something is likely wrong but this should not be the job of kernel_uimage_probe() to enforce this for everyone.

To give a concrete example, let's imagine we decide to re-order the call so kernel_uimage_probe() happens *after* an new header A than would require 128 bytes (the number is made up).

It would be wrong for A to return -EINVAL because the other protocol may require a smaller size. The same goes here even at least for consistency.

So if you really want to enforce a minimum size, then such check should be in the caller. Then each probe could return -ENOENT if the size is too small.

Quoted 1 line

I am not sure if Xen would handle a kernel whose size is less than a page.

I don't see any reason why it would not be.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

I do not like the whole falling through in kernel_probe even in case of obvious violations. But this is something not related to this series so I added to my TODO to properly handle the return types from kernel_probe path. If you really think, we should return -ENOENT here, then ok (although I do not like it). Could this be done on commit if you insist on that?

See above for an alternative proposal. Depending on the version we settle on I can do it on commit (but this is not going to happen today as OSSTEst is still blocked).

Ok, lets stick to your original suggestion with s/-EINVAL/-ENOENT/
and I will come up with something for a future patch as this will require more changes
to make it generic. I also do not like at all the fact that we are not informing the user about the error code when calling panic from construct_{dom0,domU}...

Quoted 5 lines

Cheers,

--
Julien Grall

~Michal

Reviewed-by
#7 lore raw

Re: [PATCH v2 2/2] xen/arm: Add support for booting gzip compressed uImages

Quoted 1 line · Michal Orzel

Hi Julien,

Hi,

Quoted 69 lines · Julien Grall
Quoted 68 lines · Michal Orzel
Quoted 1 line

Hi Julien,

Hi Michal,

Quoted 30 lines · Julien Grall
Quoted 28 lines

Hi Michal,

Quoted 23 lines · Michal Orzel
xen/arch/arm/kernel.c +6 -1
@@ -265,11 +284,14 @@ static __init int kernel_decompress(struct bootmodule *mod)
    #define IH_ARCH_ARM             2       /* ARM          */
    #define IH_ARCH_ARM64           22      /* ARM64        */
 
+/* uImage Compression Types */
+#define IH_COMP_GZIP            1
+
    /*
     * Check if the image is a uImage and setup kernel_info
     */
    static int __init kernel_uimage_probe(struct kernel_info *info,
-                                      paddr_t addr, paddr_t size)
+                                      struct bootmodule *mod)
    {
        struct {
            __be32 magic;   /* Image Header Magic Number */
@@ -287,6 +309,8 @@ static int __init kernel_uimage_probe(struct kernel_info *info,
        } uimage;
 
        uint32_t len;
+    paddr_t addr = mod->start;
+    paddr_t size = mod->size;
 
Quoted 6 lines
Quoted 4 lines

xen/arch/arm/kernel.c static int __init kernel_uimage_probe(struct kernel_info *info, · line 316

Quoted 2 lines
xen/arch/arm/kernel.c +0 -0
        if ( size < sizeof(uimage) )
            return -EINVAL;

Shouldn't we return -ENOENT here?

Frankly speaking, I do not want to fall through in such a case. If a kernel size is less than 64B, something is wrong, isn't it?

I agree something is likely wrong but this should not be the job of kernel_uimage_probe() to enforce this for everyone.

To give a concrete example, let's imagine we decide to re-order the call so kernel_uimage_probe() happens *after* an new header A than would require 128 bytes (the number is made up).

It would be wrong for A to return -EINVAL because the other protocol may require a smaller size. The same goes here even at least for consistency.

So if you really want to enforce a minimum size, then such check should be in the caller. Then each probe could return -ENOENT if the size is too small.

Quoted 1 line

I am not sure if Xen would handle a kernel whose size is less than a page.

I don't see any reason why it would not be.

Quoted 5 lines

I do not like the whole falling through in kernel_probe even in case of obvious violations. But this is something not related to this series so I added to my TODO to properly handle the return types from kernel_probe path. If you really think, we should return -ENOENT here, then ok (although I do not like it). Could this be done on commit if you insist on that?

Quoted 4 lines
Quoted 3 lines

See above for an alternative proposal. Depending on the version we settle on I can do it on commit (but this is not going to happen today as OSSTEst is still blocked).

Ok, lets stick to your original suggestion with s/-EINVAL/-ENOENT/

It looks like I didn't gave my reviewed-by. So:

I will change it on commit. As we discussed on IRC, even if there is no arm32 testing in OSSTest, I expect this to only impact boot and therefore should be caught by the Gitlab CI.

You already done some testing [1], so I will commit it now.

Quoted 3 lines

and I will come up with something for a future patch as this will require more changes
to make it generic. I also do not like at all the fact that we are not informing the user about the error code when calling panic from construct_{dom0,domU}...

I saw you posted it. I will add it in my queue of patches to review.

Cheers,

[1] https://gitlab.com/xen-project/people/morzel/xen-orzelmichal/-/pipelines/770984105

Signature · 1 line
Julien Grall