Skip to content

Commit 81a0bc3

Browse files
Roy Franzard
authored andcommitted
ARM: add UEFI stub support
This patch adds EFI stub support for the ARM Linux kernel. The EFI stub operates similarly to the x86 and arm64 stubs: it is a shim between the EFI firmware and the normal zImage entry point, and sets up the environment that the zImage is expecting. This includes optionally loading the initrd and device tree from the system partition based on the kernel command line. Signed-off-by: Roy Franz <roy.franz@linaro.org> Tested-by: Ryan Harkin <ryan.harkin@linaro.org> Reviewed-by: Matt Fleming <matt@codeblueprint.co.uk> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
1 parent da58fb6 commit 81a0bc3

9 files changed

Lines changed: 331 additions & 4 deletions

File tree

‎arch/arm/Kconfig‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,25 @@ config AUTO_ZRELADDR
20412041
0xf8000000. This assumes the zImage being placed in the first 128MB
20422042
from start of memory.
20432043

2044+
config EFI_STUB
2045+
bool
2046+
2047+
config EFI
2048+
bool "UEFI runtime support"
2049+
depends on OF && !CPU_BIG_ENDIAN && MMU && AUTO_ZRELADDR && !XIP_KERNEL
2050+
select UCS2_STRING
2051+
select EFI_PARAMS_FROM_FDT
2052+
select EFI_STUB
2053+
select EFI_ARMSTUB
2054+
select EFI_RUNTIME_WRAPPERS
2055+
---help---
2056+
This option provides support for runtime services provided
2057+
by UEFI firmware (such as non-volatile variables, realtime
2058+
clock, and platform reset). A UEFI stub is also provided to
2059+
allow the kernel to be booted as an EFI application. This
2060+
is only useful for kernels that may run on systems that have
2061+
UEFI firmware.
2062+
20442063
endmenu
20452064

20462065
menu "CPU Power Management"

‎arch/arm/boot/compressed/Makefile‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ if [ $(words $(ZRELADDR)) -gt 1 -a "$(CONFIG_AUTO_ZRELADDR)" = "" ]; then \
167167
false; \
168168
fi
169169

170+
efi-obj-$(CONFIG_EFI_STUB) := $(objtree)/drivers/firmware/efi/libstub/lib.a
171+
170172
$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
171173
$(addprefix $(obj)/, $(OBJS)) $(lib1funcs) $(ashldi3) \
172-
$(bswapsdi2) FORCE
174+
$(bswapsdi2) $(efi-obj-y) FORCE
173175
@$(check_for_multiple_zreladdr)
174176
$(call if_changed,ld)
175177
@$(check_for_bad_syms)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (C) 2013-2015 Linaro Ltd
3+
* Authors: Roy Franz <roy.franz@linaro.org>
4+
* Ard Biesheuvel <ard.biesheuvel@linaro.org>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
.macro __nop
12+
#ifdef CONFIG_EFI_STUB
13+
@ This is almost but not quite a NOP, since it does clobber the
14+
@ condition flags. But it is the best we can do for EFI, since
15+
@ PE/COFF expects the magic string "MZ" at offset 0, while the
16+
@ ARM/Linux boot protocol expects an executable instruction
17+
@ there.
18+
.inst 'M' | ('Z' << 8) | (0x1310 << 16) @ tstne r0, #0x4d000
19+
#else
20+
mov r0, r0
21+
#endif
22+
.endm
23+
24+
.macro __EFI_HEADER
25+
#ifdef CONFIG_EFI_STUB
26+
b __efi_start
27+
28+
.set start_offset, __efi_start - start
29+
.org start + 0x3c
30+
@
31+
@ The PE header can be anywhere in the file, but for
32+
@ simplicity we keep it together with the MSDOS header
33+
@ The offset to the PE/COFF header needs to be at offset
34+
@ 0x3C in the MSDOS header.
35+
@ The only 2 fields of the MSDOS header that are used are this
36+
@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
37+
@
38+
.long pe_header - start @ Offset to the PE header.
39+
40+
pe_header:
41+
.ascii "PE\0\0"
42+
43+
coff_header:
44+
.short 0x01c2 @ ARM or Thumb
45+
.short 2 @ nr_sections
46+
.long 0 @ TimeDateStamp
47+
.long 0 @ PointerToSymbolTable
48+
.long 1 @ NumberOfSymbols
49+
.short section_table - optional_header
50+
@ SizeOfOptionalHeader
51+
.short 0x306 @ Characteristics.
52+
@ IMAGE_FILE_32BIT_MACHINE |
53+
@ IMAGE_FILE_DEBUG_STRIPPED |
54+
@ IMAGE_FILE_EXECUTABLE_IMAGE |
55+
@ IMAGE_FILE_LINE_NUMS_STRIPPED
56+
57+
optional_header:
58+
.short 0x10b @ PE32 format
59+
.byte 0x02 @ MajorLinkerVersion
60+
.byte 0x14 @ MinorLinkerVersion
61+
.long _end - __efi_start @ SizeOfCode
62+
.long 0 @ SizeOfInitializedData
63+
.long 0 @ SizeOfUninitializedData
64+
.long efi_stub_entry - start @ AddressOfEntryPoint
65+
.long start_offset @ BaseOfCode
66+
.long 0 @ data
67+
68+
extra_header_fields:
69+
.long 0 @ ImageBase
70+
.long 0x200 @ SectionAlignment
71+
.long 0x200 @ FileAlignment
72+
.short 0 @ MajorOperatingSystemVersion
73+
.short 0 @ MinorOperatingSystemVersion
74+
.short 0 @ MajorImageVersion
75+
.short 0 @ MinorImageVersion
76+
.short 0 @ MajorSubsystemVersion
77+
.short 0 @ MinorSubsystemVersion
78+
.long 0 @ Win32VersionValue
79+
80+
.long _end - start @ SizeOfImage
81+
.long start_offset @ SizeOfHeaders
82+
.long 0 @ CheckSum
83+
.short 0xa @ Subsystem (EFI application)
84+
.short 0 @ DllCharacteristics
85+
.long 0 @ SizeOfStackReserve
86+
.long 0 @ SizeOfStackCommit
87+
.long 0 @ SizeOfHeapReserve
88+
.long 0 @ SizeOfHeapCommit
89+
.long 0 @ LoaderFlags
90+
.long 0x6 @ NumberOfRvaAndSizes
91+
92+
.quad 0 @ ExportTable
93+
.quad 0 @ ImportTable
94+
.quad 0 @ ResourceTable
95+
.quad 0 @ ExceptionTable
96+
.quad 0 @ CertificationTable
97+
.quad 0 @ BaseRelocationTable
98+
99+
section_table:
100+
@
101+
@ The EFI application loader requires a relocation section
102+
@ because EFI applications must be relocatable. This is a
103+
@ dummy section as far as we are concerned.
104+
@
105+
.ascii ".reloc\0\0"
106+
.long 0 @ VirtualSize
107+
.long 0 @ VirtualAddress
108+
.long 0 @ SizeOfRawData
109+
.long 0 @ PointerToRawData
110+
.long 0 @ PointerToRelocations
111+
.long 0 @ PointerToLineNumbers
112+
.short 0 @ NumberOfRelocations
113+
.short 0 @ NumberOfLineNumbers
114+
.long 0x42100040 @ Characteristics
115+
116+
.ascii ".text\0\0\0"
117+
.long _end - __efi_start @ VirtualSize
118+
.long __efi_start @ VirtualAddress
119+
.long _edata - __efi_start @ SizeOfRawData
120+
.long __efi_start @ PointerToRawData
121+
.long 0 @ PointerToRelocations
122+
.long 0 @ PointerToLineNumbers
123+
.short 0 @ NumberOfRelocations
124+
.short 0 @ NumberOfLineNumbers
125+
.long 0xe0500020 @ Characteristics
126+
127+
.align 9
128+
__efi_start:
129+
#endif
130+
.endm

‎arch/arm/boot/compressed/head.S‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <asm/assembler.h>
1313
#include <asm/v7m.h>
1414

15+
#include "efi-header.S"
16+
1517
AR_CLASS( .arch armv7-a )
1618
M_CLASS( .arch armv7-m )
1719

@@ -126,7 +128,7 @@
126128
start:
127129
.type start,#function
128130
.rept 7
129-
mov r0, r0
131+
__nop
130132
.endr
131133
ARM( mov r0, r0 )
132134
ARM( b 1f )
@@ -139,7 +141,8 @@ start:
139141
.word 0x04030201 @ endianness flag
140142

141143
THUMB( .thumb )
142-
1:
144+
1: __EFI_HEADER
145+
143146
ARM_BE8( setend be ) @ go BE8 if compiled for BE8
144147
AR_CLASS( mrs r9, cpsr )
145148
#ifdef CONFIG_ARM_VIRT_EXT
@@ -1353,6 +1356,53 @@ __enter_kernel:
13531356

13541357
reloc_code_end:
13551358

1359+
#ifdef CONFIG_EFI_STUB
1360+
.align 2
1361+
_start: .long start - .
1362+
1363+
ENTRY(efi_stub_entry)
1364+
@ allocate space on stack for passing current zImage address
1365+
@ and for the EFI stub to return of new entry point of
1366+
@ zImage, as EFI stub may copy the kernel. Pointer address
1367+
@ is passed in r2. r0 and r1 are passed through from the
1368+
@ EFI firmware to efi_entry
1369+
adr ip, _start
1370+
ldr r3, [ip]
1371+
add r3, r3, ip
1372+
stmfd sp!, {r3, lr}
1373+
mov r2, sp @ pass zImage address in r2
1374+
bl efi_entry
1375+
1376+
@ Check for error return from EFI stub. r0 has FDT address
1377+
@ or error code.
1378+
cmn r0, #1
1379+
beq efi_load_fail
1380+
1381+
@ Preserve return value of efi_entry() in r4
1382+
mov r4, r0
1383+
bl cache_clean_flush
1384+
bl cache_off
1385+
1386+
@ Set parameters for booting zImage according to boot protocol
1387+
@ put FDT address in r2, it was returned by efi_entry()
1388+
@ r1 is the machine type, and r0 needs to be 0
1389+
mov r0, #0
1390+
mov r1, #0xFFFFFFFF
1391+
mov r2, r4
1392+
1393+
@ Branch to (possibly) relocated zImage that is in [sp]
1394+
ldr lr, [sp]
1395+
ldr ip, =start_offset
1396+
add lr, lr, ip
1397+
mov pc, lr @ no mode switch
1398+
1399+
efi_load_fail:
1400+
@ Return EFI_LOAD_ERROR to EFI firmware on error.
1401+
ldr r0, =0x80000001
1402+
ldmfd sp!, {ip, pc}
1403+
ENDPROC(efi_stub_entry)
1404+
#endif
1405+
13561406
.align
13571407
.section ".stack", "aw", %nobits
13581408
.L_user_stack: .space 4096

‎arch/arm/boot/compressed/vmlinux.lds.S‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ SECTIONS
4848
*(.rodata)
4949
*(.rodata.*)
5050
}
51+
.data : {
52+
/*
53+
* The EFI stub always executes from RAM, and runs strictly before the
54+
* decompressor, so we can make an exception for its r/w data, and keep it
55+
*/
56+
*(.data.efistub)
57+
}
5158
.piggydata : {
5259
*(.piggydata)
5360
}

‎arch/arm/include/asm/efi.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,27 @@ void efi_virtmap_unload(void);
5757
#define efi_init()
5858
#endif /* CONFIG_EFI */
5959

60+
/* arch specific definitions used by the stub code */
61+
62+
#define efi_call_early(f, ...) sys_table_arg->boottime->f(__VA_ARGS__)
63+
64+
/*
65+
* A reasonable upper bound for the uncompressed kernel size is 32 MBytes,
66+
* so we will reserve that amount of memory. We have no easy way to tell what
67+
* the actuall size of code + data the uncompressed kernel will use.
68+
* If this is insufficient, the decompressor will relocate itself out of the
69+
* way before performing the decompression.
70+
*/
71+
#define MAX_UNCOMP_KERNEL_SIZE SZ_32M
72+
73+
/*
74+
* The kernel zImage should preferably be located between 32 MB and 128 MB
75+
* from the base of DRAM. The min address leaves space for a maximal size
76+
* uncompressed image, and the max address is due to how the zImage decompressor
77+
* picks a destination address.
78+
*/
79+
#define ZIMAGE_OFFSET_LIMIT SZ_128M
80+
#define MIN_ZIMAGE_OFFSET MAX_UNCOMP_KERNEL_SIZE
81+
#define MAX_FDT_OFFSET ZIMAGE_OFFSET_LIMIT
82+
6083
#endif /* _ASM_ARM_EFI_H */

‎drivers/firmware/efi/libstub/Makefile‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
3434
lib-$(CONFIG_EFI_ARMSTUB) += arm-stub.o fdt.o string.o \
3535
$(patsubst %.c,lib-%.o,$(arm-deps))
3636

37+
lib-$(CONFIG_ARM) += arm32-stub.o
3738
lib-$(CONFIG_ARM64) += arm64-stub.o
3839
CFLAGS_arm64-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
3940

@@ -67,3 +68,11 @@ quiet_cmd_stubcopy = STUBCPY $@
6768
$(OBJDUMP) -r $@ | grep $(STUBCOPY_RELOC-y) \
6869
&& (echo >&2 "$@: absolute symbol references not allowed in the EFI stub"; \
6970
rm -f $@; /bin/false); else /bin/false; fi
71+
72+
#
73+
# ARM discards the .data section because it disallows r/w data in the
74+
# decompressor. So move our .data to .data.efistub, which is preserved
75+
# explicitly by the decompressor linker script.
76+
#
77+
STUBCOPY_FLAGS-$(CONFIG_ARM) += --rename-section .data=.data.efistub
78+
STUBCOPY_RELOC-$(CONFIG_ARM) := R_ARM_ABS

‎drivers/firmware/efi/libstub/arm-stub.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
303303
* The value chosen is the largest non-zero power of 2 suitable for this purpose
304304
* both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
305305
* be mapped efficiently.
306+
* Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
307+
* map everything below 1 GB.
306308
*/
307-
#define EFI_RT_VIRTUAL_BASE 0x40000000
309+
#define EFI_RT_VIRTUAL_BASE SZ_512M
308310

309311
static int cmp_mem_desc(const void *l, const void *r)
310312
{

0 commit comments

Comments
 (0)