efi_load_initrd.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2020, Linaro Limited
  4. */
  5. #define LOG_CATEGORY LOGC_EFI
  6. #include <common.h>
  7. #include <efi_loader.h>
  8. #include <efi_load_initrd.h>
  9. #include <efi_variable.h>
  10. #include <fs.h>
  11. #include <malloc.h>
  12. #include <mapmem.h>
  13. static efi_status_t EFIAPI
  14. efi_load_file2_initrd(struct efi_load_file_protocol *this,
  15. struct efi_device_path *file_path, bool boot_policy,
  16. efi_uintn_t *buffer_size, void *buffer);
  17. static const struct efi_load_file_protocol efi_lf2_protocol = {
  18. .load_file = efi_load_file2_initrd,
  19. };
  20. /*
  21. * Device path defined by Linux to identify the handle providing the
  22. * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
  23. */
  24. static const struct efi_initrd_dp dp_lf2_handle = {
  25. .vendor = {
  26. {
  27. DEVICE_PATH_TYPE_MEDIA_DEVICE,
  28. DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
  29. sizeof(dp_lf2_handle.vendor),
  30. },
  31. EFI_INITRD_MEDIA_GUID,
  32. },
  33. .end = {
  34. DEVICE_PATH_TYPE_END,
  35. DEVICE_PATH_SUB_TYPE_END,
  36. sizeof(dp_lf2_handle.end),
  37. }
  38. };
  39. static efi_handle_t efi_initrd_handle;
  40. /**
  41. * get_initrd_fp() - Get initrd device path from a FilePathList device path
  42. *
  43. * @initrd_fp: the final initrd filepath
  44. *
  45. * Return: status code. Caller must free initrd_fp
  46. */
  47. static efi_status_t get_initrd_fp(struct efi_device_path **initrd_fp)
  48. {
  49. struct efi_device_path *dp = NULL;
  50. /*
  51. * if bootmgr is setup with and initrd, the device path will be
  52. * in the FilePathList[] of our load options in Boot####.
  53. * The first device path of the multi instance device path will
  54. * start with a VenMedia and the initrds will follow.
  55. *
  56. * If the device path is not found return EFI_INVALID_PARAMETER.
  57. * We can then use this specific return value and not install the
  58. * protocol, while allowing the boot to continue
  59. */
  60. dp = efi_get_dp_from_boot(efi_lf2_initrd_guid);
  61. if (!dp)
  62. return EFI_INVALID_PARAMETER;
  63. *initrd_fp = dp;
  64. return EFI_SUCCESS;
  65. }
  66. /**
  67. * efi_load_file2_initrd() - load initial RAM disk
  68. *
  69. * This function implements the LoadFile service of the EFI_LOAD_FILE2_PROTOCOL
  70. * in order to load an initial RAM disk requested by the Linux kernel stub.
  71. *
  72. * See the UEFI spec for details.
  73. *
  74. * @this: EFI_LOAD_FILE2_PROTOCOL instance
  75. * @file_path: media device path of the file, "" in this case
  76. * @boot_policy: must be false
  77. * @buffer_size: size of allocated buffer
  78. * @buffer: buffer to load the file
  79. *
  80. * Return: status code
  81. */
  82. static efi_status_t EFIAPI
  83. efi_load_file2_initrd(struct efi_load_file_protocol *this,
  84. struct efi_device_path *file_path, bool boot_policy,
  85. efi_uintn_t *buffer_size, void *buffer)
  86. {
  87. struct efi_device_path *initrd_fp = NULL;
  88. efi_status_t ret = EFI_NOT_FOUND;
  89. struct efi_file_handle *f = NULL;
  90. efi_uintn_t bs;
  91. EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
  92. buffer_size, buffer);
  93. if (!this || this != &efi_lf2_protocol ||
  94. !buffer_size) {
  95. ret = EFI_INVALID_PARAMETER;
  96. goto out;
  97. }
  98. if (file_path->type != dp_lf2_handle.end.type ||
  99. file_path->sub_type != dp_lf2_handle.end.sub_type) {
  100. ret = EFI_INVALID_PARAMETER;
  101. goto out;
  102. }
  103. if (boot_policy) {
  104. ret = EFI_UNSUPPORTED;
  105. goto out;
  106. }
  107. ret = get_initrd_fp(&initrd_fp);
  108. if (ret != EFI_SUCCESS)
  109. goto out;
  110. /* Open file */
  111. f = efi_file_from_path(initrd_fp);
  112. if (!f) {
  113. log_err("Can't find initrd specified in Boot####\n");
  114. ret = EFI_NOT_FOUND;
  115. goto out;
  116. }
  117. /* Get file size */
  118. ret = efi_file_size(f, &bs);
  119. if (ret != EFI_SUCCESS)
  120. goto out;
  121. if (!buffer || *buffer_size < bs) {
  122. ret = EFI_BUFFER_TOO_SMALL;
  123. *buffer_size = bs;
  124. } else {
  125. ret = EFI_CALL(f->read(f, &bs, (void *)(uintptr_t)buffer));
  126. *buffer_size = bs;
  127. }
  128. out:
  129. efi_free_pool(initrd_fp);
  130. if (f)
  131. EFI_CALL(f->close(f));
  132. return EFI_EXIT(ret);
  133. }
  134. /**
  135. * check_initrd() - Determine if the file defined as an initrd in Boot####
  136. * load_options device path is present
  137. *
  138. * Return: status code
  139. */
  140. static efi_status_t check_initrd(void)
  141. {
  142. struct efi_device_path *initrd_fp = NULL;
  143. struct efi_file_handle *f;
  144. efi_status_t ret;
  145. ret = get_initrd_fp(&initrd_fp);
  146. if (ret != EFI_SUCCESS)
  147. goto out;
  148. /*
  149. * If the file is not found, but the file path is set, return an error
  150. * and trigger the bootmgr fallback
  151. */
  152. f = efi_file_from_path(initrd_fp);
  153. if (!f) {
  154. log_err("Can't find initrd specified in Boot####\n");
  155. ret = EFI_NOT_FOUND;
  156. goto out;
  157. }
  158. EFI_CALL(f->close(f));
  159. out:
  160. efi_free_pool(initrd_fp);
  161. return ret;
  162. }
  163. /**
  164. * efi_initrd_register() - create handle for loading initial RAM disk
  165. *
  166. * This function creates a new handle and installs a Linux specific vendor
  167. * device path and an EFI_LOAD_FILE2_PROTOCOL. Linux uses the device path
  168. * to identify the handle and then calls the LoadFile service of the
  169. * EFI_LOAD_FILE2_PROTOCOL to read the initial RAM disk.
  170. *
  171. * Return: status code
  172. */
  173. efi_status_t efi_initrd_register(void)
  174. {
  175. efi_status_t ret;
  176. /*
  177. * Allow the user to continue if Boot#### file path is not set for
  178. * an initrd
  179. */
  180. ret = check_initrd();
  181. if (ret == EFI_INVALID_PARAMETER)
  182. return EFI_SUCCESS;
  183. if (ret != EFI_SUCCESS)
  184. return ret;
  185. ret = efi_install_multiple_protocol_interfaces(&efi_initrd_handle,
  186. /* initramfs */
  187. &efi_guid_device_path, &dp_lf2_handle,
  188. /* LOAD_FILE2 */
  189. &efi_guid_load_file2_protocol,
  190. &efi_lf2_protocol,
  191. NULL);
  192. return ret;
  193. }
  194. /**
  195. * efi_initrd_deregister() - delete the handle for loading initial RAM disk
  196. *
  197. * This will delete the handle containing the Linux specific vendor device
  198. * path and EFI_LOAD_FILE2_PROTOCOL for loading an initrd
  199. *
  200. * Return: status code
  201. */
  202. efi_status_t efi_initrd_deregister(void)
  203. {
  204. efi_status_t ret;
  205. if (!efi_initrd_handle)
  206. return EFI_SUCCESS;
  207. ret = efi_uninstall_multiple_protocol_interfaces(efi_initrd_handle,
  208. /* initramfs */
  209. &efi_guid_device_path,
  210. &dp_lf2_handle,
  211. /* LOAD_FILE2 */
  212. &efi_guid_load_file2_protocol,
  213. &efi_lf2_protocol,
  214. NULL);
  215. efi_initrd_handle = NULL;
  216. return ret;
  217. }