efi_load_initrd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2020, Linaro Limited
  4. */
  5. #include <common.h>
  6. #include <env.h>
  7. #include <malloc.h>
  8. #include <mapmem.h>
  9. #include <dm.h>
  10. #include <fs.h>
  11. #include <efi_loader.h>
  12. #include <efi_load_initrd.h>
  13. static const efi_guid_t efi_guid_load_file2_protocol =
  14. EFI_LOAD_FILE2_PROTOCOL_GUID;
  15. static efi_status_t EFIAPI
  16. efi_load_file2_initrd(struct efi_load_file_protocol *this,
  17. struct efi_device_path *file_path, bool boot_policy,
  18. efi_uintn_t *buffer_size, void *buffer);
  19. static const struct efi_load_file_protocol efi_lf2_protocol = {
  20. .load_file = efi_load_file2_initrd,
  21. };
  22. /*
  23. * Device path defined by Linux to identify the handle providing the
  24. * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
  25. */
  26. static const struct efi_initrd_dp dp = {
  27. .vendor = {
  28. {
  29. DEVICE_PATH_TYPE_MEDIA_DEVICE,
  30. DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
  31. sizeof(dp.vendor),
  32. },
  33. EFI_INITRD_MEDIA_GUID,
  34. },
  35. .end = {
  36. DEVICE_PATH_TYPE_END,
  37. DEVICE_PATH_SUB_TYPE_END,
  38. sizeof(dp.end),
  39. }
  40. };
  41. /**
  42. * get_file_size() - retrieve the size of initramfs, set efi status on error
  43. *
  44. * @dev: device to read from, e.g. "mmc"
  45. * @part: device partition, e.g. "0:1"
  46. * @file: name of file
  47. * @status: EFI exit code in case of failure
  48. *
  49. * Return: size of file
  50. */
  51. static loff_t get_file_size(const char *dev, const char *part, const char *file,
  52. efi_status_t *status)
  53. {
  54. loff_t sz = 0;
  55. int ret;
  56. ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
  57. if (ret) {
  58. *status = EFI_NO_MEDIA;
  59. goto out;
  60. }
  61. ret = fs_size(file, &sz);
  62. if (ret) {
  63. sz = 0;
  64. *status = EFI_NOT_FOUND;
  65. goto out;
  66. }
  67. out:
  68. return sz;
  69. }
  70. /**
  71. * efi_load_file2initrd() - load initial RAM disk
  72. *
  73. * This function implements the LoadFile service of the EFI_LOAD_FILE2_PROTOCOL
  74. * in order to load an initial RAM disk requested by the Linux kernel stub.
  75. *
  76. * See the UEFI spec for details.
  77. *
  78. * @this: EFI_LOAD_FILE2_PROTOCOL instance
  79. * @file_path: media device path of the file, "" in this case
  80. * @boot_policy: must be false
  81. * @buffer_size: size of allocated buffer
  82. * @buffer: buffer to load the file
  83. *
  84. * Return: status code
  85. */
  86. static efi_status_t EFIAPI
  87. efi_load_file2_initrd(struct efi_load_file_protocol *this,
  88. struct efi_device_path *file_path, bool boot_policy,
  89. efi_uintn_t *buffer_size, void *buffer)
  90. {
  91. const char *filespec = CONFIG_EFI_INITRD_FILESPEC;
  92. efi_status_t status = EFI_NOT_FOUND;
  93. loff_t file_sz = 0, read_sz = 0;
  94. char *dev, *part, *file;
  95. char *s;
  96. int ret;
  97. EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
  98. buffer_size, buffer);
  99. s = strdup(filespec);
  100. if (!s)
  101. goto out;
  102. if (!this || this != &efi_lf2_protocol ||
  103. !buffer_size) {
  104. status = EFI_INVALID_PARAMETER;
  105. goto out;
  106. }
  107. if (file_path->type != dp.end.type ||
  108. file_path->sub_type != dp.end.sub_type) {
  109. status = EFI_INVALID_PARAMETER;
  110. goto out;
  111. }
  112. if (boot_policy) {
  113. status = EFI_UNSUPPORTED;
  114. goto out;
  115. }
  116. /*
  117. * expect a string with three space separated parts:
  118. *
  119. * * a block device type, e.g. "mmc"
  120. * * a device and partition identifier, e.g. "0:1"
  121. * * a file path on the block device, e.g. "/boot/initrd.cpio.gz"
  122. */
  123. dev = strsep(&s, " ");
  124. if (!dev)
  125. goto out;
  126. part = strsep(&s, " ");
  127. if (!part)
  128. goto out;
  129. file = strsep(&s, " ");
  130. if (!file)
  131. goto out;
  132. file_sz = get_file_size(dev, part, file, &status);
  133. if (!file_sz)
  134. goto out;
  135. if (!buffer || *buffer_size < file_sz) {
  136. status = EFI_BUFFER_TOO_SMALL;
  137. *buffer_size = file_sz;
  138. } else {
  139. ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
  140. if (ret) {
  141. status = EFI_NO_MEDIA;
  142. goto out;
  143. }
  144. ret = fs_read(file, map_to_sysmem(buffer), 0, *buffer_size,
  145. &read_sz);
  146. if (ret || read_sz != file_sz)
  147. goto out;
  148. *buffer_size = read_sz;
  149. status = EFI_SUCCESS;
  150. }
  151. out:
  152. free(s);
  153. return EFI_EXIT(status);
  154. }
  155. /**
  156. * efi_initrd_register() - create handle for loading initial RAM disk
  157. *
  158. * This function creates a new handle and installs a Linux specific vendor
  159. * device path and an EFI_LOAD_FILE_2_PROTOCOL. Linux uses the device path
  160. * to identify the handle and then calls the LoadFile service of the
  161. * EFI_LOAD_FILE_2_PROTOCOL to read the initial RAM disk.
  162. *
  163. * Return: status code
  164. */
  165. efi_status_t efi_initrd_register(void)
  166. {
  167. efi_handle_t efi_initrd_handle = NULL;
  168. efi_status_t ret;
  169. ret = EFI_CALL(efi_install_multiple_protocol_interfaces
  170. (&efi_initrd_handle,
  171. /* initramfs */
  172. &efi_guid_device_path, &dp,
  173. /* LOAD_FILE2 */
  174. &efi_guid_load_file2_protocol,
  175. (void *)&efi_lf2_protocol,
  176. NULL));
  177. return ret;
  178. }