arm-stub.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * EFI stub implementation that is shared by arm and arm64 architectures.
  3. * This should be #included by the EFI stub implementation files.
  4. *
  5. * Copyright (C) 2013,2014 Linaro Limited
  6. * Roy Franz <roy.franz@linaro.org
  7. * Copyright (C) 2013 Red Hat, Inc.
  8. * Mark Salter <msalter@redhat.com>
  9. *
  10. * This file is part of the Linux kernel, and is made available under the
  11. * terms of the GNU General Public License version 2.
  12. *
  13. */
  14. static int __init efi_secureboot_enabled(efi_system_table_t *sys_table_arg)
  15. {
  16. static efi_guid_t const var_guid __initconst = EFI_GLOBAL_VARIABLE_GUID;
  17. static efi_char16_t const var_name[] __initconst = {
  18. 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 };
  19. efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable;
  20. unsigned long size = sizeof(u8);
  21. efi_status_t status;
  22. u8 val;
  23. status = f_getvar((efi_char16_t *)var_name, (efi_guid_t *)&var_guid,
  24. NULL, &size, &val);
  25. switch (status) {
  26. case EFI_SUCCESS:
  27. return val;
  28. case EFI_NOT_FOUND:
  29. return 0;
  30. default:
  31. return 1;
  32. }
  33. }
  34. static efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
  35. void *__image, void **__fh)
  36. {
  37. efi_file_io_interface_t *io;
  38. efi_loaded_image_t *image = __image;
  39. efi_file_handle_t *fh;
  40. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  41. efi_status_t status;
  42. void *handle = (void *)(unsigned long)image->device_handle;
  43. status = sys_table_arg->boottime->handle_protocol(handle,
  44. &fs_proto, (void **)&io);
  45. if (status != EFI_SUCCESS) {
  46. efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
  47. return status;
  48. }
  49. status = io->open_volume(io, &fh);
  50. if (status != EFI_SUCCESS)
  51. efi_printk(sys_table_arg, "Failed to open volume\n");
  52. *__fh = fh;
  53. return status;
  54. }
  55. static efi_status_t efi_file_close(void *handle)
  56. {
  57. efi_file_handle_t *fh = handle;
  58. return fh->close(handle);
  59. }
  60. static efi_status_t
  61. efi_file_read(void *handle, unsigned long *size, void *addr)
  62. {
  63. efi_file_handle_t *fh = handle;
  64. return fh->read(handle, size, addr);
  65. }
  66. static efi_status_t
  67. efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
  68. efi_char16_t *filename_16, void **handle, u64 *file_sz)
  69. {
  70. efi_file_handle_t *h, *fh = __fh;
  71. efi_file_info_t *info;
  72. efi_status_t status;
  73. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  74. unsigned long info_sz;
  75. status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0);
  76. if (status != EFI_SUCCESS) {
  77. efi_printk(sys_table_arg, "Failed to open file: ");
  78. efi_char16_printk(sys_table_arg, filename_16);
  79. efi_printk(sys_table_arg, "\n");
  80. return status;
  81. }
  82. *handle = h;
  83. info_sz = 0;
  84. status = h->get_info(h, &info_guid, &info_sz, NULL);
  85. if (status != EFI_BUFFER_TOO_SMALL) {
  86. efi_printk(sys_table_arg, "Failed to get file info size\n");
  87. return status;
  88. }
  89. grow:
  90. status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA,
  91. info_sz, (void **)&info);
  92. if (status != EFI_SUCCESS) {
  93. efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
  94. return status;
  95. }
  96. status = h->get_info(h, &info_guid, &info_sz,
  97. info);
  98. if (status == EFI_BUFFER_TOO_SMALL) {
  99. sys_table_arg->boottime->free_pool(info);
  100. goto grow;
  101. }
  102. *file_sz = info->file_size;
  103. sys_table_arg->boottime->free_pool(info);
  104. if (status != EFI_SUCCESS)
  105. efi_printk(sys_table_arg, "Failed to get initrd info\n");
  106. return status;
  107. }
  108. static void efi_char16_printk(efi_system_table_t *sys_table_arg,
  109. efi_char16_t *str)
  110. {
  111. struct efi_simple_text_output_protocol *out;
  112. out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
  113. out->output_string(out, str);
  114. }
  115. /*
  116. * This function handles the architcture specific differences between arm and
  117. * arm64 regarding where the kernel image must be loaded and any memory that
  118. * must be reserved. On failure it is required to free all
  119. * all allocations it has made.
  120. */
  121. static efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
  122. unsigned long *image_addr,
  123. unsigned long *image_size,
  124. unsigned long *reserve_addr,
  125. unsigned long *reserve_size,
  126. unsigned long dram_base,
  127. efi_loaded_image_t *image);
  128. /*
  129. * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
  130. * that is described in the PE/COFF header. Most of the code is the same
  131. * for both archictectures, with the arch-specific code provided in the
  132. * handle_kernel_image() function.
  133. */
  134. unsigned long __init efi_entry(void *handle, efi_system_table_t *sys_table,
  135. unsigned long *image_addr)
  136. {
  137. efi_loaded_image_t *image;
  138. efi_status_t status;
  139. unsigned long image_size = 0;
  140. unsigned long dram_base;
  141. /* addr/point and size pairs for memory management*/
  142. unsigned long initrd_addr;
  143. u64 initrd_size = 0;
  144. unsigned long fdt_addr = 0; /* Original DTB */
  145. u64 fdt_size = 0; /* We don't get size from configuration table */
  146. char *cmdline_ptr = NULL;
  147. int cmdline_size = 0;
  148. unsigned long new_fdt_addr;
  149. efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
  150. unsigned long reserve_addr = 0;
  151. unsigned long reserve_size = 0;
  152. /* Check if we were booted by the EFI firmware */
  153. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  154. goto fail;
  155. pr_efi(sys_table, "Booting Linux Kernel...\n");
  156. /*
  157. * Get a handle to the loaded image protocol. This is used to get
  158. * information about the running image, such as size and the command
  159. * line.
  160. */
  161. status = sys_table->boottime->handle_protocol(handle,
  162. &loaded_image_proto, (void *)&image);
  163. if (status != EFI_SUCCESS) {
  164. pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
  165. goto fail;
  166. }
  167. dram_base = get_dram_base(sys_table);
  168. if (dram_base == EFI_ERROR) {
  169. pr_efi_err(sys_table, "Failed to find DRAM base\n");
  170. goto fail;
  171. }
  172. status = handle_kernel_image(sys_table, image_addr, &image_size,
  173. &reserve_addr,
  174. &reserve_size,
  175. dram_base, image);
  176. if (status != EFI_SUCCESS) {
  177. pr_efi_err(sys_table, "Failed to relocate kernel\n");
  178. goto fail;
  179. }
  180. /*
  181. * Get the command line from EFI, using the LOADED_IMAGE
  182. * protocol. We are going to copy the command line into the
  183. * device tree, so this can be allocated anywhere.
  184. */
  185. cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
  186. if (!cmdline_ptr) {
  187. pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
  188. goto fail_free_image;
  189. }
  190. /*
  191. * Unauthenticated device tree data is a security hazard, so
  192. * ignore 'dtb=' unless UEFI Secure Boot is disabled.
  193. */
  194. if (efi_secureboot_enabled(sys_table)) {
  195. pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
  196. } else {
  197. status = handle_cmdline_files(sys_table, image, cmdline_ptr,
  198. "dtb=",
  199. ~0UL, (unsigned long *)&fdt_addr,
  200. (unsigned long *)&fdt_size);
  201. if (status != EFI_SUCCESS) {
  202. pr_efi_err(sys_table, "Failed to load device tree!\n");
  203. goto fail_free_cmdline;
  204. }
  205. }
  206. if (!fdt_addr)
  207. /* Look for a device tree configuration table entry. */
  208. fdt_addr = (uintptr_t)get_fdt(sys_table);
  209. status = handle_cmdline_files(sys_table, image, cmdline_ptr,
  210. "initrd=", dram_base + SZ_512M,
  211. (unsigned long *)&initrd_addr,
  212. (unsigned long *)&initrd_size);
  213. if (status != EFI_SUCCESS)
  214. pr_efi_err(sys_table, "Failed initrd from command line!\n");
  215. new_fdt_addr = fdt_addr;
  216. status = allocate_new_fdt_and_exit_boot(sys_table, handle,
  217. &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
  218. initrd_addr, initrd_size, cmdline_ptr,
  219. fdt_addr, fdt_size);
  220. /*
  221. * If all went well, we need to return the FDT address to the
  222. * calling function so it can be passed to kernel as part of
  223. * the kernel boot protocol.
  224. */
  225. if (status == EFI_SUCCESS)
  226. return new_fdt_addr;
  227. pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
  228. efi_free(sys_table, initrd_size, initrd_addr);
  229. efi_free(sys_table, fdt_size, fdt_addr);
  230. fail_free_cmdline:
  231. efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
  232. fail_free_image:
  233. efi_free(sys_table, image_size, *image_addr);
  234. efi_free(sys_table, reserve_size, reserve_addr);
  235. fail:
  236. return EFI_ERROR;
  237. }