1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
10 efi_status_t
check_platform_features(void)
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("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(void)
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_bs_call(allocate_pool
, EFI_RUNTIME_SERVICES_DATA
,
41 sizeof(*si
), (void **)&si
);
43 if (status
!= EFI_SUCCESS
)
46 status
= efi_bs_call(install_configuration_table
,
47 &screen_info_guid
, si
);
48 if (status
== EFI_SUCCESS
)
51 efi_bs_call(free_pool
, si
);
55 void free_screen_info(struct screen_info
*si
)
60 efi_bs_call(install_configuration_table
, &screen_info_guid
, NULL
);
61 efi_bs_call(free_pool
, si
);
64 static efi_status_t
reserve_kernel_base(unsigned long dram_base
,
65 unsigned long *reserve_addr
,
66 unsigned long *reserve_size
)
68 efi_physical_addr_t alloc_addr
;
69 efi_memory_desc_t
*memory_map
;
70 unsigned long nr_pages
, map_size
, desc_size
, buff_size
;
74 struct efi_boot_memmap map
= {
76 .map_size
= &map_size
,
77 .desc_size
= &desc_size
,
80 .buff_size
= &buff_size
,
84 * Reserve memory for the uncompressed kernel image. This is
85 * all that prevents any future allocations from conflicting
86 * with the kernel. Since we can't tell from the compressed
87 * image how much DRAM the kernel actually uses (due to BSS
88 * size uncertainty) we allocate the maximum possible size.
89 * Do this very early, as prints can cause memory allocations
90 * that may conflict with this.
92 alloc_addr
= dram_base
+ MAX_UNCOMP_KERNEL_SIZE
;
93 nr_pages
= MAX_UNCOMP_KERNEL_SIZE
/ EFI_PAGE_SIZE
;
94 status
= efi_bs_call(allocate_pages
, EFI_ALLOCATE_MAX_ADDRESS
,
95 EFI_BOOT_SERVICES_DATA
, nr_pages
, &alloc_addr
);
96 if (status
== EFI_SUCCESS
) {
97 if (alloc_addr
== dram_base
) {
98 *reserve_addr
= alloc_addr
;
99 *reserve_size
= MAX_UNCOMP_KERNEL_SIZE
;
103 * If we end up here, the allocation succeeded but starts below
104 * dram_base. This can only occur if the real base of DRAM is
105 * not a multiple of 128 MB, in which case dram_base will have
106 * been rounded up. Since this implies that a part of the region
107 * was already occupied, we need to fall through to the code
108 * below to ensure that the existing allocations don't conflict.
109 * For this reason, we use EFI_BOOT_SERVICES_DATA above and not
110 * EFI_LOADER_DATA, which we wouldn't able to distinguish from
111 * allocations that we want to disallow.
116 * If the allocation above failed, we may still be able to proceed:
117 * if the only allocations in the region are of types that will be
118 * released to the OS after ExitBootServices(), the decompressor can
119 * safely overwrite them.
121 status
= efi_get_memory_map(&map
);
122 if (status
!= EFI_SUCCESS
) {
123 pr_efi_err("reserve_kernel_base(): Unable to retrieve memory map.\n");
127 for (l
= 0; l
< map_size
; l
+= desc_size
) {
128 efi_memory_desc_t
*desc
;
131 desc
= (void *)memory_map
+ l
;
132 start
= desc
->phys_addr
;
133 end
= start
+ desc
->num_pages
* EFI_PAGE_SIZE
;
135 /* Skip if entry does not intersect with region */
136 if (start
>= dram_base
+ MAX_UNCOMP_KERNEL_SIZE
||
140 switch (desc
->type
) {
141 case EFI_BOOT_SERVICES_CODE
:
142 case EFI_BOOT_SERVICES_DATA
:
143 /* Ignore types that are released to the OS anyway */
146 case EFI_CONVENTIONAL_MEMORY
:
147 /* Skip soft reserved conventional memory */
148 if (efi_soft_reserve_enabled() &&
149 (desc
->attribute
& EFI_MEMORY_SP
))
153 * Reserve the intersection between this entry and the
156 start
= max(start
, (u64
)dram_base
);
157 end
= min(end
, (u64
)dram_base
+ MAX_UNCOMP_KERNEL_SIZE
);
159 status
= efi_bs_call(allocate_pages
,
160 EFI_ALLOCATE_ADDRESS
,
162 (end
- start
) / EFI_PAGE_SIZE
,
164 if (status
!= EFI_SUCCESS
) {
165 pr_efi_err("reserve_kernel_base(): alloc failed.\n");
170 case EFI_LOADER_CODE
:
171 case EFI_LOADER_DATA
:
173 * These regions may be released and reallocated for
174 * another purpose (including EFI_RUNTIME_SERVICE_DATA)
175 * at any time during the execution of the OS loader,
176 * so we cannot consider them as safe.
180 * Treat any other allocation in the region as unsafe */
181 status
= EFI_OUT_OF_RESOURCES
;
186 status
= EFI_SUCCESS
;
188 efi_bs_call(free_pool
, memory_map
);
192 efi_status_t
handle_kernel_image(unsigned long *image_addr
,
193 unsigned long *image_size
,
194 unsigned long *reserve_addr
,
195 unsigned long *reserve_size
,
196 unsigned long dram_base
,
197 efi_loaded_image_t
*image
)
199 unsigned long kernel_base
;
203 * Verify that the DRAM base address is compatible with the ARM
204 * boot protocol, which determines the base of DRAM by masking
205 * off the low 27 bits of the address at which the zImage is
206 * loaded. These assumptions are made by the decompressor,
207 * before any memory map is available.
209 kernel_base
= round_up(dram_base
, SZ_128M
);
212 * Note that some platforms (notably, the Raspberry Pi 2) put
213 * spin-tables and other pieces of firmware at the base of RAM,
214 * abusing the fact that the window of TEXT_OFFSET bytes at the
215 * base of the kernel image is only partially used at the moment.
216 * (Up to 5 pages are used for the swapper page tables)
218 kernel_base
+= TEXT_OFFSET
- 5 * PAGE_SIZE
;
220 status
= reserve_kernel_base(kernel_base
, reserve_addr
, reserve_size
);
221 if (status
!= EFI_SUCCESS
) {
222 pr_efi_err("Unable to allocate memory for uncompressed kernel.\n");
227 * Relocate the zImage, so that it appears in the lowest 128 MB
230 *image_addr
= (unsigned long)image
->image_base
;
231 *image_size
= image
->image_size
;
232 status
= efi_relocate_kernel(image_addr
, *image_size
, *image_size
,
233 kernel_base
+ MAX_UNCOMP_KERNEL_SIZE
, 0, 0);
234 if (status
!= EFI_SUCCESS
) {
235 pr_efi_err("Failed to relocate kernel.\n");
236 efi_free(*reserve_size
, *reserve_addr
);
242 * Check to see if we were able to allocate memory low enough
243 * in memory. The kernel determines the base of DRAM from the
244 * address at which the zImage is loaded.
246 if (*image_addr
+ *image_size
> dram_base
+ ZIMAGE_OFFSET_LIMIT
) {
247 pr_efi_err("Failed to relocate kernel, no low memory available.\n");
248 efi_free(*reserve_size
, *reserve_addr
);
250 efi_free(*image_size
, *image_addr
);
252 return EFI_LOAD_ERROR
;