u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
* [PATCH] efi_loader: Fix loaded image alignment
@ 2021-10-11 12:10 Ilias Apalodimas
  2021-10-11 13:45 ` Heinrich Schuchardt
  2021-10-12  9:45 ` Vincent Stehlé
  0 siblings, 2 replies; 6+ messages in thread
From: Ilias Apalodimas @ 2021-10-11 12:10 UTC (permalink / raw)
  To: xypron.glpk
  Cc: ardb, Vincent.Stehle, Ilias Apalodimas, Alexander Graf, u-boot

We are ignoring the alignment communicated via the PE/COFF header.
Starting 5.10 the Linux kernel will loudly complain about it. For more
details look at [1] (in linux kernel).

So add a function that can allocate aligned EFI memory and use it for our
relocated loaded image.

[1] c32ac11da3f83 ("efi/libstub: arm64: Double check image alignment at entry")

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 include/efi_loader.h              |  2 ++
 lib/efi_loader/efi_image_loader.c | 12 ++++----
 lib/efi_loader/efi_memory.c       | 50 +++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index c440962fe522..5cdc72345e52 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -675,6 +675,8 @@ struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid);
 #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
 /* Generic EFI memory allocator, call this to get memory */
 void *efi_alloc(uint64_t len, int memory_type);
+/* Allocate pages on the specified alignment */
+void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align);
 /* More specific EFI memory allocator, called by EFI payloads */
 efi_status_t efi_allocate_pages(enum efi_allocate_type type,
 				enum efi_memory_type memory_type,
diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
index e9572d4d5dbb..eb95580538cc 100644
--- a/lib/efi_loader/efi_image_loader.c
+++ b/lib/efi_loader/efi_image_loader.c
@@ -898,9 +898,9 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
 		image_base = opt->ImageBase;
 		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
 		handle->image_type = opt->Subsystem;
-		virt_size = ALIGN(virt_size, opt->SectionAlignment);
-		efi_reloc = efi_alloc(virt_size,
-				      loaded_image_info->image_code_type);
+		efi_reloc = efi_alloc_aligned_pages(virt_size,
+						    loaded_image_info->image_code_type,
+						    opt->SectionAlignment);
 		if (!efi_reloc) {
 			log_err("Out of memory\n");
 			ret = EFI_OUT_OF_RESOURCES;
@@ -914,9 +914,9 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
 		image_base = opt->ImageBase;
 		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
 		handle->image_type = opt->Subsystem;
-		virt_size = ALIGN(virt_size, opt->SectionAlignment);
-		efi_reloc = efi_alloc(virt_size,
-				      loaded_image_info->image_code_type);
+		efi_reloc = efi_alloc_aligned_pages(virt_size,
+						    loaded_image_info->image_code_type,
+						    opt->SectionAlignment);
 		if (!efi_reloc) {
 			log_err("Out of memory\n");
 			ret = EFI_OUT_OF_RESOURCES;
diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
index f4acbee4f9b6..49d36d369aaa 100644
--- a/lib/efi_loader/efi_memory.c
+++ b/lib/efi_loader/efi_memory.c
@@ -549,6 +549,56 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 	return ret;
 }
 
+/**
+ * efi_alloc_aligned_pages - allocate
+ *
+ * @len		len in bytes
+ * @memory_type	usage type of the allocated memory
+ * @align	alignment in bytes
+ * Return:	aligned memory or NULL
+ */
+void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
+{
+	u64 req_pages = efi_size_in_pages(len);
+	u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
+	u64 free_pages = 0;
+	u64 aligned_mem;
+	efi_status_t r;
+	u64 mem;
+
+	if (align & (align - 1))
+		return NULL;
+
+	if (true_pages < req_pages)
+		return NULL;
+
+	if (align < EFI_PAGE_SIZE) {
+		r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
+				       req_pages, &mem);
+		return (r == EFI_SUCCESS) ? (void *)mem : NULL;
+	}
+
+	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
+			       true_pages, &mem);
+	if (r != EFI_SUCCESS)
+		return NULL;
+
+	aligned_mem = ALIGN(mem, align);
+	/* Free pages before alignment */
+	free_pages = efi_size_in_pages(aligned_mem - mem);
+	if (free_pages)
+		efi_free_pages(mem, free_pages);
+
+	/* Free trailing pages */
+	free_pages = true_pages - (req_pages + free_pages);
+	if (free_pages) {
+		mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
+		efi_free_pages(mem, free_pages);
+	}
+
+	return (void *)aligned_mem;
+}
+
 /**
  * efi_allocate_pool - allocate memory from pool
  *
-- 
2.33.0


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

* Re: [PATCH] efi_loader: Fix loaded image alignment
  2021-10-11 12:10 [PATCH] efi_loader: Fix loaded image alignment Ilias Apalodimas
@ 2021-10-11 13:45 ` Heinrich Schuchardt
  2021-10-11 13:50   ` Ilias Apalodimas
  2021-10-12  9:45 ` Vincent Stehlé
  1 sibling, 1 reply; 6+ messages in thread
From: Heinrich Schuchardt @ 2021-10-11 13:45 UTC (permalink / raw)
  To: Ilias Apalodimas; +Cc: ardb, Vincent.Stehle, Alexander Graf, u-boot



On 10/11/21 14:10, Ilias Apalodimas wrote:
> We are ignoring the alignment communicated via the PE/COFF header.
> Starting 5.10 the Linux kernel will loudly complain about it. For more
> details look at [1] (in linux kernel).
> 
> So add a function that can allocate aligned EFI memory and use it for our
> relocated loaded image.
> 
> [1] c32ac11da3f83 ("efi/libstub: arm64: Double check image alignment at entry")
> 
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> ---
>   include/efi_loader.h              |  2 ++
>   lib/efi_loader/efi_image_loader.c | 12 ++++----
>   lib/efi_loader/efi_memory.c       | 50 +++++++++++++++++++++++++++++++
>   3 files changed, 58 insertions(+), 6 deletions(-)
> 
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index c440962fe522..5cdc72345e52 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -675,6 +675,8 @@ struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid);
>   #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
>   /* Generic EFI memory allocator, call this to get memory */
>   void *efi_alloc(uint64_t len, int memory_type);
> +/* Allocate pages on the specified alignment */
> +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align);
>   /* More specific EFI memory allocator, called by EFI payloads */
>   efi_status_t efi_allocate_pages(enum efi_allocate_type type,
>   				enum efi_memory_type memory_type,
> diff --git a/lib/efi_loader/efi_image_loader.c b/lib/efi_loader/efi_image_loader.c
> index e9572d4d5dbb..eb95580538cc 100644
> --- a/lib/efi_loader/efi_image_loader.c
> +++ b/lib/efi_loader/efi_image_loader.c
> @@ -898,9 +898,9 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
>   		image_base = opt->ImageBase;
>   		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
>   		handle->image_type = opt->Subsystem;
> -		virt_size = ALIGN(virt_size, opt->SectionAlignment);
> -		efi_reloc = efi_alloc(virt_size,
> -				      loaded_image_info->image_code_type);
> +		efi_reloc = efi_alloc_aligned_pages(virt_size,
> +						    loaded_image_info->image_code_type,
> +						    opt->SectionAlignment);
>   		if (!efi_reloc) {
>   			log_err("Out of memory\n");
>   			ret = EFI_OUT_OF_RESOURCES;
> @@ -914,9 +914,9 @@ efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle,
>   		image_base = opt->ImageBase;
>   		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
>   		handle->image_type = opt->Subsystem;
> -		virt_size = ALIGN(virt_size, opt->SectionAlignment);
> -		efi_reloc = efi_alloc(virt_size,
> -				      loaded_image_info->image_code_type);
> +		efi_reloc = efi_alloc_aligned_pages(virt_size,
> +						    loaded_image_info->image_code_type,
> +						    opt->SectionAlignment);
>   		if (!efi_reloc) {
>   			log_err("Out of memory\n");
>   			ret = EFI_OUT_OF_RESOURCES;
> diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
> index f4acbee4f9b6..49d36d369aaa 100644
> --- a/lib/efi_loader/efi_memory.c
> +++ b/lib/efi_loader/efi_memory.c
> @@ -549,6 +549,56 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
>   	return ret;
>   }
>   
> +/**
> + * efi_alloc_aligned_pages - allocate
> + *
> + * @len		len in bytes
> + * @memory_type	usage type of the allocated memory
> + * @align	alignment in bytes
> + * Return:	aligned memory or NULL
> + */
> +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
> +{
> +	u64 req_pages = efi_size_in_pages(len);
> +	u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
> +	u64 free_pages = 0;

The assigned value is never used. Please, remove the assignment.

> +	u64 aligned_mem;
> +	efi_status_t r;
> +	u64 mem;
> +

Please add a comment:

	/* Align must be a power of two */

I can apply these changes when merging.

Otherwise

Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

> +	if (align & (align - 1))
> +		return NULL;
> +
> +	if (true_pages < req_pages)
> +		return NULL;
> +
> +	if (align < EFI_PAGE_SIZE) {
> +		r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
> +				       req_pages, &mem);
> +		return (r == EFI_SUCCESS) ? (void *)mem : NULL;
> +	}
> +
> +	r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type,
> +			       true_pages, &mem);
> +	if (r != EFI_SUCCESS)
> +		return NULL;
> +
> +	aligned_mem = ALIGN(mem, align);
> +	/* Free pages before alignment */
> +	free_pages = efi_size_in_pages(aligned_mem - mem);
> +	if (free_pages)
> +		efi_free_pages(mem, free_pages);
> +
> +	/* Free trailing pages */
> +	free_pages = true_pages - (req_pages + free_pages);
> +	if (free_pages) {
> +		mem = aligned_mem + req_pages * EFI_PAGE_SIZE;
> +		efi_free_pages(mem, free_pages);
> +	}
> +
> +	return (void *)aligned_mem;
> +}
> +
>   /**
>    * efi_allocate_pool - allocate memory from pool
>    *
> 

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

* Re: [PATCH] efi_loader: Fix loaded image alignment
  2021-10-11 13:45 ` Heinrich Schuchardt
@ 2021-10-11 13:50   ` Ilias Apalodimas
  2021-10-11 14:07     ` Ard Biesheuvel
  0 siblings, 1 reply; 6+ messages in thread
From: Ilias Apalodimas @ 2021-10-11 13:50 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Ard Biesheuvel, Vincent Stehle, Alexander Graf, U-Boot Mailing List

Hi Heinrich,

On Mon, 11 Oct 2021 at 16:45, Heinrich Schuchardt
<heinrich.schuchardt@canonical.com> wrote:
>
>
>
> On 10/11/21 14:10, Ilias Apalodimas wrote:
> > We are ignoring the alignment communicated via the PE/COFF header.
> > Starting 5.10 the Linux kernel will loudly complain about it. For more
> > details look at [1] (in linux kernel).
> >
> > So add a function that can allocate aligned EFI memory and use it for our
> > relocated loaded image.
> >
> > [1] c32ac11da3f83 ("efi/libstub: arm64: Double check image alignment at entry")
> >
> > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > ---

[...]

> > + */
> > +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
> > +{
> > +     u64 req_pages = efi_size_in_pages(len);
> > +     u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
> > +     u64 free_pages = 0;
>
> The assigned value is never used. Please, remove the assignment.
>
> > +     u64 aligned_mem;
> > +     efi_status_t r;
> > +     u64 mem;
> > +
>
> Please add a comment:
>
>         /* Align must be a power of two */
>
> I can apply these changes when merging.

Ok the changes seem fine to me.   Wait a few days in case Ard sees
this, so he can verify the changes are what the kernel expects.

Thanks
/Ilias
>
> Otherwise
>
[...]

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

* Re: [PATCH] efi_loader: Fix loaded image alignment
  2021-10-11 13:50   ` Ilias Apalodimas
@ 2021-10-11 14:07     ` Ard Biesheuvel
  2021-10-11 14:19       ` Ilias Apalodimas
  0 siblings, 1 reply; 6+ messages in thread
From: Ard Biesheuvel @ 2021-10-11 14:07 UTC (permalink / raw)
  To: Ilias Apalodimas
  Cc: Heinrich Schuchardt, Vincent Stehle, Alexander Graf, U-Boot Mailing List

On Mon, 11 Oct 2021 at 15:51, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Heinrich,
>
> On Mon, 11 Oct 2021 at 16:45, Heinrich Schuchardt
> <heinrich.schuchardt@canonical.com> wrote:
> >
> >
> >
> > On 10/11/21 14:10, Ilias Apalodimas wrote:
> > > We are ignoring the alignment communicated via the PE/COFF header.
> > > Starting 5.10 the Linux kernel will loudly complain about it. For more
> > > details look at [1] (in linux kernel).
> > >
> > > So add a function that can allocate aligned EFI memory and use it for our
> > > relocated loaded image.
> > >
> > > [1] c32ac11da3f83 ("efi/libstub: arm64: Double check image alignment at entry")
> > >
> > > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > > ---
>
> [...]
>
> > > + */
> > > +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
> > > +{
> > > +     u64 req_pages = efi_size_in_pages(len);
> > > +     u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
> > > +     u64 free_pages = 0;
> >
> > The assigned value is never used. Please, remove the assignment.
> >
> > > +     u64 aligned_mem;
> > > +     efi_status_t r;
> > > +     u64 mem;
> > > +
> >
> > Please add a comment:
> >
> >         /* Align must be a power of two */
> >
> > I can apply these changes when merging.
>
> Ok the changes seem fine to me.   Wait a few days in case Ard sees
> this, so he can verify the changes are what the kernel expects.
>

Should be fine if it results in the correct alignment, and makes the
error message go away.

Acked-by: Ard Biesheuvel <ardb@kernel.org>

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

* Re: [PATCH] efi_loader: Fix loaded image alignment
  2021-10-11 14:07     ` Ard Biesheuvel
@ 2021-10-11 14:19       ` Ilias Apalodimas
  0 siblings, 0 replies; 6+ messages in thread
From: Ilias Apalodimas @ 2021-10-11 14:19 UTC (permalink / raw)
  To: Ard Biesheuvel
  Cc: Heinrich Schuchardt, Vincent Stehle, Alexander Graf, U-Boot Mailing List

On Mon, 11 Oct 2021 at 17:07, Ard Biesheuvel <ardb@kernel.org> wrote:
>
> On Mon, 11 Oct 2021 at 15:51, Ilias Apalodimas
> <ilias.apalodimas@linaro.org> wrote:
> >
> > Hi Heinrich,
> >
> > On Mon, 11 Oct 2021 at 16:45, Heinrich Schuchardt
> > <heinrich.schuchardt@canonical.com> wrote:
> > >
> > >
> > >
> > > On 10/11/21 14:10, Ilias Apalodimas wrote:
> > > > We are ignoring the alignment communicated via the PE/COFF header.
> > > > Starting 5.10 the Linux kernel will loudly complain about it. For more
> > > > details look at [1] (in linux kernel).
> > > >
> > > > So add a function that can allocate aligned EFI memory and use it for our
> > > > relocated loaded image.
> > > >
> > > > [1] c32ac11da3f83 ("efi/libstub: arm64: Double check image alignment at entry")
> > > >
> > > > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > > > ---
> >
> > [...]
> >
> > > > + */
> > > > +void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
> > > > +{
> > > > +     u64 req_pages = efi_size_in_pages(len);
> > > > +     u64 true_pages = req_pages + efi_size_in_pages(align) - 1;
> > > > +     u64 free_pages = 0;
> > >
> > > The assigned value is never used. Please, remove the assignment.
> > >
> > > > +     u64 aligned_mem;
> > > > +     efi_status_t r;
> > > > +     u64 mem;
> > > > +
> > >
> > > Please add a comment:
> > >
> > >         /* Align must be a power of two */
> > >
> > > I can apply these changes when merging.
> >
> > Ok the changes seem fine to me.   Wait a few days in case Ard sees
> > this, so he can verify the changes are what the kernel expects.
> >
>
> Should be fine if it results in the correct alignment, and makes the
> error message go away.

Yea both of those stand

>
> Acked-by: Ard Biesheuvel <ardb@kernel.org>

Thanks!

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

* Re: [PATCH] efi_loader: Fix loaded image alignment
  2021-10-11 12:10 [PATCH] efi_loader: Fix loaded image alignment Ilias Apalodimas
  2021-10-11 13:45 ` Heinrich Schuchardt
@ 2021-10-12  9:45 ` Vincent Stehlé
  1 sibling, 0 replies; 6+ messages in thread
From: Vincent Stehlé @ 2021-10-12  9:45 UTC (permalink / raw)
  To: Ilias Apalodimas; +Cc: xypron.glpk, ardb, Alexander Graf, u-boot

On Mon, Oct 11, 2021 at 03:10:23PM +0300, Ilias Apalodimas wrote:
> We are ignoring the alignment communicated via the PE/COFF header.
> Starting 5.10 the Linux kernel will loudly complain about it. For more
> details look at [1] (in linux kernel).
> 
> So add a function that can allocate aligned EFI memory and use it for our
> relocated loaded image.

Hi Ilias,

Thank you for this fix.

I verified that Linux v5.14.3 EFI stub complains about not being aligned to 64k
without this fix and is happy with it, on the following systems:

- qemu with U-Boot latest (after v2021.10)
- Pine64 ROCKPro64 with U-Boot "near" v2021.07[1]
- Lenovo Leez P710 with U-Boot v2021.07[2]
- Compulab IOT-GATE-iMX8 with U-Boot "near" v2021.10-rc3[3]

Feel free to add (or not):

  Tested-by: Vincent Stehlé <vincent.stehle@arm.com>

Best regards,

Vincent Stehlé
System Architect - Arm

[1]: https://gitlab.arm.com/systemready/firmware-build/rk3399-manifest/-/blob/rockpro64-21.09/README.md
[2]: https://gitlab.arm.com/systemready/firmware-build/rk3399-manifest/-/blob/leez-21.08/README.md
[3]: https://git.linaro.org/people/paul.liu/systemready/build-scripts.git/tree/docs/iotgateimx8_building_running.md

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

end of thread, other threads:[~2021-10-12  9:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-11 12:10 [PATCH] efi_loader: Fix loaded image alignment Ilias Apalodimas
2021-10-11 13:45 ` Heinrich Schuchardt
2021-10-11 13:50   ` Ilias Apalodimas
2021-10-11 14:07     ` Ard Biesheuvel
2021-10-11 14:19       ` Ilias Apalodimas
2021-10-12  9:45 ` Vincent Stehlé

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).