Skip to content

Commit 1887c9b

Browse files
nivedita76Ingo Molnar
authored andcommitted
efi/x86: Decompress at start of PE image load address
When booted via PE loader, define image_offset to hold the offset of startup_32() from the start of the PE image, and use it as the start of the decompression buffer. [ mingo: Fixed the grammar in the comments. ] Signed-off-by: Arvind Sankar <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent 8ef44be commit 1887c9b

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

arch/x86/boot/compressed/head_32.S

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ SYM_FUNC_START(startup_32)
100100

101101
#ifdef CONFIG_RELOCATABLE
102102
movl %edx, %ebx
103+
104+
#ifdef CONFIG_EFI_STUB
105+
/*
106+
* If we were loaded via the EFI LoadImage service, startup_32() will be at an
107+
* offset to the start of the space allocated for the image. efi_pe_entry() will
108+
* set up image_offset to tell us where the image actually starts, so that we
109+
* can use the full available buffer.
110+
* image_offset = startup_32 - image_base
111+
* Otherwise image_offset will be zero and has no effect on the calculations.
112+
*/
113+
subl image_offset(%edx), %ebx
114+
#endif
115+
103116
movl BP_kernel_alignment(%esi), %eax
104117
decl %eax
105118
addl %eax, %ebx
@@ -226,6 +239,10 @@ SYM_DATA_START_LOCAL(gdt)
226239
.quad 0x00cf92000000ffff /* __KERNEL_DS */
227240
SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end)
228241

242+
#ifdef CONFIG_EFI_STUB
243+
SYM_DATA(image_offset, .long 0)
244+
#endif
245+
229246
/*
230247
* Stack and heap for uncompression
231248
*/

arch/x86/boot/compressed/head_64.S

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ SYM_FUNC_START(startup_32)
9999

100100
#ifdef CONFIG_RELOCATABLE
101101
movl %ebp, %ebx
102+
103+
#ifdef CONFIG_EFI_STUB
104+
/*
105+
* If we were loaded via the EFI LoadImage service, startup_32 will be at an
106+
* offset to the start of the space allocated for the image. efi_pe_entry will
107+
* set up image_offset to tell us where the image actually starts, so that we
108+
* can use the full available buffer.
109+
* image_offset = startup_32 - image_base
110+
* Otherwise image_offset will be zero and has no effect on the calculations.
111+
*/
112+
subl image_offset(%ebp), %ebx
113+
#endif
114+
102115
movl BP_kernel_alignment(%esi), %eax
103116
decl %eax
104117
addl %eax, %ebx
@@ -111,9 +124,8 @@ SYM_FUNC_START(startup_32)
111124
1:
112125

113126
/* Target address to relocate to for decompression */
114-
movl BP_init_size(%esi), %eax
115-
subl $_end, %eax
116-
addl %eax, %ebx
127+
addl BP_init_size(%esi), %ebx
128+
subl $_end, %ebx
117129

118130
/*
119131
* Prepare for entering 64 bit mode
@@ -299,6 +311,20 @@ SYM_CODE_START(startup_64)
299311
/* Start with the delta to where the kernel will run at. */
300312
#ifdef CONFIG_RELOCATABLE
301313
leaq startup_32(%rip) /* - $startup_32 */, %rbp
314+
315+
#ifdef CONFIG_EFI_STUB
316+
/*
317+
* If we were loaded via the EFI LoadImage service, startup_32 will be at an
318+
* offset to the start of the space allocated for the image. efi_pe_entry will
319+
* set up image_offset to tell us where the image actually starts, so that we
320+
* can use the full available buffer.
321+
* image_offset = startup_32 - image_base
322+
* Otherwise image_offset will be zero and has no effect on the calculations.
323+
*/
324+
movl image_offset(%rip), %eax
325+
subq %rax, %rbp
326+
#endif
327+
302328
movl BP_kernel_alignment(%rsi), %eax
303329
decl %eax
304330
addq %rax, %rbp
@@ -647,6 +673,10 @@ SYM_DATA_START_LOCAL(gdt)
647673
.quad 0x0000000000000000 /* TS continued */
648674
SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end)
649675

676+
#ifdef CONFIG_EFI_STUB
677+
SYM_DATA(image_offset, .long 0)
678+
#endif
679+
650680
#ifdef CONFIG_EFI_MIXED
651681
SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0)
652682
SYM_DATA(efi_is64, .byte 1)
@@ -712,6 +742,12 @@ SYM_FUNC_START(efi32_pe_entry)
712742
movl -4(%ebp), %esi // loaded_image
713743
movl LI32_image_base(%esi), %esi // loaded_image->image_base
714744
movl %ebx, %ebp // startup_32 for efi32_pe_stub_entry
745+
/*
746+
* We need to set the image_offset variable here since startup_32() will
747+
* use it before we get to the 64-bit efi_pe_entry() in C code.
748+
*/
749+
subl %esi, %ebx
750+
movl %ebx, image_offset(%ebp) // save image_offset
715751
jmp efi32_pe_stub_entry
716752

717753
2: popl %edi // restore callee-save registers

drivers/firmware/efi/libstub/x86-stub.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
static efi_system_table_t *sys_table;
2121
extern const bool efi_is64;
22+
extern u32 image_offset;
2223

2324
__pure efi_system_table_t *efi_system_table(void)
2425
{
@@ -365,6 +366,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
365366
struct boot_params *boot_params;
366367
struct setup_header *hdr;
367368
efi_loaded_image_t *image;
369+
void *image_base;
368370
efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
369371
int options_size = 0;
370372
efi_status_t status;
@@ -385,7 +387,10 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
385387
efi_exit(handle, status);
386388
}
387389

388-
hdr = &((struct boot_params *)efi_table_attr(image, image_base))->hdr;
390+
image_base = efi_table_attr(image, image_base);
391+
image_offset = (void *)startup_32 - image_base;
392+
393+
hdr = &((struct boot_params *)image_base)->hdr;
389394
above4g = hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G;
390395

391396
status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params,
@@ -400,7 +405,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
400405
hdr = &boot_params->hdr;
401406

402407
/* Copy the second sector to boot_params */
403-
memcpy(&hdr->jump, efi_table_attr(image, image_base) + 512, 512);
408+
memcpy(&hdr->jump, image_base + 512, 512);
404409

405410
/*
406411
* Fill out some of the header fields ourselves because the
@@ -727,7 +732,7 @@ unsigned long efi_main(efi_handle_t handle,
727732
* If the kernel isn't already loaded at the preferred load
728733
* address, relocate it.
729734
*/
730-
if (bzimage_addr != hdr->pref_address) {
735+
if (bzimage_addr - image_offset != hdr->pref_address) {
731736
status = efi_relocate_kernel(&bzimage_addr,
732737
hdr->init_size, hdr->init_size,
733738
hdr->pref_address,
@@ -737,6 +742,12 @@ unsigned long efi_main(efi_handle_t handle,
737742
efi_printk("efi_relocate_kernel() failed!\n");
738743
goto fail;
739744
}
745+
/*
746+
* Now that we've copied the kernel elsewhere, we no longer
747+
* have a set up block before startup_32(), so reset image_offset
748+
* to zero in case it was set earlier.
749+
*/
750+
image_offset = 0;
740751
}
741752

742753
/*

0 commit comments

Comments
 (0)