Re: [PATCH v2 04/16] x86/compressed: efi-mixed: move efi32_pe_entry into .text section

From: Ard Biesheuvel
Date: Thu Nov 17 2022 - 11:07:35 EST


On Thu, 17 Nov 2022 at 16:57, Borislav Petkov <bp@xxxxxxxxx> wrote:
>
> On Wed, Sep 21, 2022 at 04:54:10PM +0200, Ard Biesheuvel wrote:
> > /*
> > * We need to set the image_offset variable here since startup_32() will
> > * use it before we get to the 64-bit efi_pe_entry() in C code.
> > */
> > - subl %esi, %ebx
> > - movl %ebx, rva(image_offset)(%ebp) // save image_offset
> > + subl %esi, %ebp // calculate image_offset
> > + movl %ebp, (image_offset - 1b)(%ebx) // save image_offset
>
> All looks ok, just one question: what was the reason for that
> image_offset thing?
>
> I see:
>
> 1887c9b653f9 ("efi/x86: Decompress at start of PE image load address")
>
> It says that if the kernel is loaded as a PE executable using
> LoadImage() we don't know where that image will be loaded each time so
> we're saving that offset for later when relocating (or not) the kernel?
>
> All part of those improvements:
>
> https://lore.kernel.org/all/20200301230537.2247550-1-nivedita@xxxxxxxxxxxx/
>
> Am I close?
>

Yes.

The x86 boot protocol does not require that the setup data block comes
right before the image, it just receives the address in %esi

When doing PE boot, this is guaranteed, and so we can reuse the memory
before the image.

> I.e., that image_offset is purely a kernel thing and not something EFI
> LoadImage's inner workings mandate...? It doesn't seem so from where I'm
> standing but lemme doublecheck still.
>

No this has nothing do with the EFI in particular, only with how the
x86 boot image is constructed and wrapped into a PE/COFF executable.