arm32-stub.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/efi.h>
  10. #include <asm/efi.h>
  11. #include "efistub.h"
  12. efi_status_t check_platform_features(efi_system_table_t *sys_table_arg)
  13. {
  14. int block;
  15. /* non-LPAE kernels can run anywhere */
  16. if (!IS_ENABLED(CONFIG_ARM_LPAE))
  17. return EFI_SUCCESS;
  18. /* LPAE kernels need compatible hardware */
  19. block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0);
  20. if (block < 5) {
  21. pr_efi_err(sys_table_arg, "This LPAE kernel is not supported by your CPU\n");
  22. return EFI_UNSUPPORTED;
  23. }
  24. return EFI_SUCCESS;
  25. }
  26. static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
  27. struct screen_info *alloc_screen_info(efi_system_table_t *sys_table_arg)
  28. {
  29. struct screen_info *si;
  30. efi_status_t status;
  31. /*
  32. * Unlike on arm64, where we can directly fill out the screen_info
  33. * structure from the stub, we need to allocate a buffer to hold
  34. * its contents while we hand over to the kernel proper from the
  35. * decompressor.
  36. */
  37. status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
  38. sizeof(*si), (void **)&si);
  39. if (status != EFI_SUCCESS)
  40. return NULL;
  41. status = efi_call_early(install_configuration_table,
  42. &screen_info_guid, si);
  43. if (status == EFI_SUCCESS)
  44. return si;
  45. efi_call_early(free_pool, si);
  46. return NULL;
  47. }
  48. void free_screen_info(efi_system_table_t *sys_table_arg, struct screen_info *si)
  49. {
  50. if (!si)
  51. return;
  52. efi_call_early(install_configuration_table, &screen_info_guid, NULL);
  53. efi_call_early(free_pool, si);
  54. }
  55. static efi_status_t reserve_kernel_base(efi_system_table_t *sys_table_arg,
  56. unsigned long dram_base,
  57. unsigned long *reserve_addr,
  58. unsigned long *reserve_size)
  59. {
  60. efi_physical_addr_t alloc_addr;
  61. efi_memory_desc_t *memory_map;
  62. unsigned long nr_pages, map_size, desc_size, buff_size;
  63. efi_status_t status;
  64. unsigned long l;
  65. struct efi_boot_memmap map = {
  66. .map = &memory_map,
  67. .map_size = &map_size,
  68. .desc_size = &desc_size,
  69. .desc_ver = NULL,
  70. .key_ptr = NULL,
  71. .buff_size = &buff_size,
  72. };
  73. /*
  74. * Reserve memory for the uncompressed kernel image. This is
  75. * all that prevents any future allocations from conflicting
  76. * with the kernel. Since we can't tell from the compressed
  77. * image how much DRAM the kernel actually uses (due to BSS
  78. * size uncertainty) we allocate the maximum possible size.
  79. * Do this very early, as prints can cause memory allocations
  80. * that may conflict with this.
  81. */
  82. alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE;
  83. nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE;
  84. status = efi_call_early(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
  85. EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr);
  86. if (status == EFI_SUCCESS) {
  87. if (alloc_addr == dram_base) {
  88. *reserve_addr = alloc_addr;
  89. *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
  90. return EFI_SUCCESS;
  91. }
  92. /*
  93. * If we end up here, the allocation succeeded but starts below
  94. * dram_base. This can only occur if the real base of DRAM is
  95. * not a multiple of 128 MB, in which case dram_base will have
  96. * been rounded up. Since this implies that a part of the region
  97. * was already occupied, we need to fall through to the code
  98. * below to ensure that the existing allocations don't conflict.
  99. * For this reason, we use EFI_BOOT_SERVICES_DATA above and not
  100. * EFI_LOADER_DATA, which we wouldn't able to distinguish from
  101. * allocations that we want to disallow.
  102. */
  103. }
  104. /*
  105. * If the allocation above failed, we may still be able to proceed:
  106. * if the only allocations in the region are of types that will be
  107. * released to the OS after ExitBootServices(), the decompressor can
  108. * safely overwrite them.
  109. */
  110. status = efi_get_memory_map(sys_table_arg, &map);
  111. if (status != EFI_SUCCESS) {
  112. pr_efi_err(sys_table_arg,
  113. "reserve_kernel_base(): Unable to retrieve memory map.\n");
  114. return status;
  115. }
  116. for (l = 0; l < map_size; l += desc_size) {
  117. efi_memory_desc_t *desc;
  118. u64 start, end;
  119. desc = (void *)memory_map + l;
  120. start = desc->phys_addr;
  121. end = start + desc->num_pages * EFI_PAGE_SIZE;
  122. /* Skip if entry does not intersect with region */
  123. if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE ||
  124. end <= dram_base)
  125. continue;
  126. switch (desc->type) {
  127. case EFI_BOOT_SERVICES_CODE:
  128. case EFI_BOOT_SERVICES_DATA:
  129. /* Ignore types that are released to the OS anyway */
  130. continue;
  131. case EFI_CONVENTIONAL_MEMORY:
  132. /*
  133. * Reserve the intersection between this entry and the
  134. * region.
  135. */
  136. start = max(start, (u64)dram_base);
  137. end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE);
  138. status = efi_call_early(allocate_pages,
  139. EFI_ALLOCATE_ADDRESS,
  140. EFI_LOADER_DATA,
  141. (end - start) / EFI_PAGE_SIZE,
  142. &start);
  143. if (status != EFI_SUCCESS) {
  144. pr_efi_err(sys_table_arg,
  145. "reserve_kernel_base(): alloc failed.\n");
  146. goto out;
  147. }
  148. break;
  149. case EFI_LOADER_CODE:
  150. case EFI_LOADER_DATA:
  151. /*
  152. * These regions may be released and reallocated for
  153. * another purpose (including EFI_RUNTIME_SERVICE_DATA)
  154. * at any time during the execution of the OS loader,
  155. * so we cannot consider them as safe.
  156. */
  157. default:
  158. /*
  159. * Treat any other allocation in the region as unsafe */
  160. status = EFI_OUT_OF_RESOURCES;
  161. goto out;
  162. }
  163. }
  164. status = EFI_SUCCESS;
  165. out:
  166. efi_call_early(free_pool, memory_map);
  167. return status;
  168. }
  169. efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
  170. unsigned long *image_addr,
  171. unsigned long *image_size,
  172. unsigned long *reserve_addr,
  173. unsigned long *reserve_size,
  174. unsigned long dram_base,
  175. efi_loaded_image_t *image)
  176. {
  177. efi_status_t status;
  178. /*
  179. * Verify that the DRAM base address is compatible with the ARM
  180. * boot protocol, which determines the base of DRAM by masking
  181. * off the low 27 bits of the address at which the zImage is
  182. * loaded. These assumptions are made by the decompressor,
  183. * before any memory map is available.
  184. */
  185. dram_base = round_up(dram_base, SZ_128M);
  186. status = reserve_kernel_base(sys_table, dram_base, reserve_addr,
  187. reserve_size);
  188. if (status != EFI_SUCCESS) {
  189. pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n");
  190. return status;
  191. }
  192. /*
  193. * Relocate the zImage, so that it appears in the lowest 128 MB
  194. * memory window.
  195. */
  196. *image_size = image->image_size;
  197. status = efi_relocate_kernel(sys_table, image_addr, *image_size,
  198. *image_size,
  199. dram_base + MAX_UNCOMP_KERNEL_SIZE, 0);
  200. if (status != EFI_SUCCESS) {
  201. pr_efi_err(sys_table, "Failed to relocate kernel.\n");
  202. efi_free(sys_table, *reserve_size, *reserve_addr);
  203. *reserve_size = 0;
  204. return status;
  205. }
  206. /*
  207. * Check to see if we were able to allocate memory low enough
  208. * in memory. The kernel determines the base of DRAM from the
  209. * address at which the zImage is loaded.
  210. */
  211. if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) {
  212. pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n");
  213. efi_free(sys_table, *reserve_size, *reserve_addr);
  214. *reserve_size = 0;
  215. efi_free(sys_table, *image_size, *image_addr);
  216. *image_size = 0;
  217. return EFI_LOAD_ERROR;
  218. }
  219. return EFI_SUCCESS;
  220. }