1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
10 efi_status_t
check_platform_features(efi_system_table_t
*sys_table_arg
)
14 /* non-LPAE kernels can run anywhere */
15 if (!IS_ENABLED(CONFIG_ARM_LPAE
))
18 /* LPAE kernels need compatible hardware */
19 block
= cpuid_feature_extract(CPUID_EXT_MMFR0
, 0);
21 pr_efi_err(sys_table_arg
, "This LPAE kernel is not supported by your CPU\n");
22 return EFI_UNSUPPORTED
;
27 static efi_guid_t screen_info_guid
= LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID
;
29 struct screen_info
*alloc_screen_info(efi_system_table_t
*sys_table_arg
)
31 struct screen_info
*si
;
35 * Unlike on arm64, where we can directly fill out the screen_info
36 * structure from the stub, we need to allocate a buffer to hold
37 * its contents while we hand over to the kernel proper from the
40 status
= efi_call_early(allocate_pool
, EFI_RUNTIME_SERVICES_DATA
,
41 sizeof(*si
), (void **)&si
);
43 if (status
!= EFI_SUCCESS
)
46 status
= efi_call_early(install_configuration_table
,
47 &screen_info_guid
, si
);
48 if (status
== EFI_SUCCESS
)
51 efi_call_early(free_pool
, si
);
55 void free_screen_info(efi_system_table_t
*sys_table_arg
, struct screen_info
*si
)
60 efi_call_early(install_configuration_table
, &screen_info_guid
, NULL
);
61 efi_call_early(free_pool
, si
);
64 static efi_status_t
reserve_kernel_base(efi_system_table_t
*sys_table_arg
,
65 unsigned long dram_base
,
66 unsigned long *reserve_addr
,
67 unsigned long *reserve_size
)
69 efi_physical_addr_t alloc_addr
;
70 efi_memory_desc_t
*memory_map
;
71 unsigned long nr_pages
, map_size
, desc_size
, buff_size
;
75 struct efi_boot_memmap map
= {
77 .map_size
= &map_size
,
78 .desc_size
= &desc_size
,
81 .buff_size
= &buff_size
,
85 * Reserve memory for the uncompressed kernel image. This is
86 * all that prevents any future allocations from conflicting
87 * with the kernel. Since we can't tell from the compressed
88 * image how much DRAM the kernel actually uses (due to BSS
89 * size uncertainty) we allocate the maximum possible size.
90 * Do this very early, as prints can cause memory allocations
91 * that may conflict with this.
93 alloc_addr
= dram_base
+ MAX_UNCOMP_KERNEL_SIZE
;
94 nr_pages
= MAX_UNCOMP_KERNEL_SIZE
/ EFI_PAGE_SIZE
;
95 status
= efi_call_early(allocate_pages
, EFI_ALLOCATE_MAX_ADDRESS
,
96 EFI_BOOT_SERVICES_DATA
, nr_pages
, &alloc_addr
);
97 if (status
== EFI_SUCCESS
) {
98 if (alloc_addr
== dram_base
) {
99 *reserve_addr
= alloc_addr
;
100 *reserve_size
= MAX_UNCOMP_KERNEL_SIZE
;
104 * If we end up here, the allocation succeeded but starts below
105 * dram_base. This can only occur if the real base of DRAM is
106 * not a multiple of 128 MB, in which case dram_base will have
107 * been rounded up. Since this implies that a part of the region
108 * was already occupied, we need to fall through to the code
109 * below to ensure that the existing allocations don't conflict.
110 * For this reason, we use EFI_BOOT_SERVICES_DATA above and not
111 * EFI_LOADER_DATA, which we wouldn't able to distinguish from
112 * allocations that we want to disallow.
117 * If the allocation above failed, we may still be able to proceed:
118 * if the only allocations in the region are of types that will be
119 * released to the OS after ExitBootServices(), the decompressor can
120 * safely overwrite them.
122 status
= efi_get_memory_map(sys_table_arg
, &map
);
123 if (status
!= EFI_SUCCESS
) {
124 pr_efi_err(sys_table_arg
,
125 "reserve_kernel_base(): Unable to retrieve memory map.\n");
129 for (l
= 0; l
< map_size
; l
+= desc_size
) {
130 efi_memory_desc_t
*desc
;
133 desc
= (void *)memory_map
+ l
;
134 start
= desc
->phys_addr
;
135 end
= start
+ desc
->num_pages
* EFI_PAGE_SIZE
;
137 /* Skip if entry does not intersect with region */
138 if (start
>= dram_base
+ MAX_UNCOMP_KERNEL_SIZE
||
142 switch (desc
->type
) {
143 case EFI_BOOT_SERVICES_CODE
:
144 case EFI_BOOT_SERVICES_DATA
:
145 /* Ignore types that are released to the OS anyway */
148 case EFI_CONVENTIONAL_MEMORY
:
150 * Reserve the intersection between this entry and the
153 start
= max(start
, (u64
)dram_base
);
154 end
= min(end
, (u64
)dram_base
+ MAX_UNCOMP_KERNEL_SIZE
);
156 status
= efi_call_early(allocate_pages
,
157 EFI_ALLOCATE_ADDRESS
,
159 (end
- start
) / EFI_PAGE_SIZE
,
161 if (status
!= EFI_SUCCESS
) {
162 pr_efi_err(sys_table_arg
,
163 "reserve_kernel_base(): alloc failed.\n");
168 case EFI_LOADER_CODE
:
169 case EFI_LOADER_DATA
:
171 * These regions may be released and reallocated for
172 * another purpose (including EFI_RUNTIME_SERVICE_DATA)
173 * at any time during the execution of the OS loader,
174 * so we cannot consider them as safe.
178 * Treat any other allocation in the region as unsafe */
179 status
= EFI_OUT_OF_RESOURCES
;
184 status
= EFI_SUCCESS
;
186 efi_call_early(free_pool
, memory_map
);
190 efi_status_t
handle_kernel_image(efi_system_table_t
*sys_table
,
191 unsigned long *image_addr
,
192 unsigned long *image_size
,
193 unsigned long *reserve_addr
,
194 unsigned long *reserve_size
,
195 unsigned long dram_base
,
196 efi_loaded_image_t
*image
)
201 * Verify that the DRAM base address is compatible with the ARM
202 * boot protocol, which determines the base of DRAM by masking
203 * off the low 27 bits of the address at which the zImage is
204 * loaded. These assumptions are made by the decompressor,
205 * before any memory map is available.
207 dram_base
= round_up(dram_base
, SZ_128M
);
209 status
= reserve_kernel_base(sys_table
, dram_base
, reserve_addr
,
211 if (status
!= EFI_SUCCESS
) {
212 pr_efi_err(sys_table
, "Unable to allocate memory for uncompressed kernel.\n");
217 * Relocate the zImage, so that it appears in the lowest 128 MB
220 *image_size
= image
->image_size
;
221 status
= efi_relocate_kernel(sys_table
, image_addr
, *image_size
,
223 dram_base
+ MAX_UNCOMP_KERNEL_SIZE
, 0);
224 if (status
!= EFI_SUCCESS
) {
225 pr_efi_err(sys_table
, "Failed to relocate kernel.\n");
226 efi_free(sys_table
, *reserve_size
, *reserve_addr
);
232 * Check to see if we were able to allocate memory low enough
233 * in memory. The kernel determines the base of DRAM from the
234 * address at which the zImage is loaded.
236 if (*image_addr
+ *image_size
> dram_base
+ ZIMAGE_OFFSET_LIMIT
) {
237 pr_efi_err(sys_table
, "Failed to relocate kernel, no low memory available.\n");
238 efi_free(sys_table
, *reserve_size
, *reserve_addr
);
240 efi_free(sys_table
, *image_size
, *image_addr
);
242 return EFI_LOAD_ERROR
;