efi_load_initrd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. char *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 *pos;
  96. int ret;
  97. EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
  98. buffer_size, buffer);
  99. filespec = strdup(CONFIG_EFI_INITRD_FILESPEC);
  100. if (!filespec)
  101. goto out;
  102. pos = filespec;
  103. if (!this || this != &efi_lf2_protocol ||
  104. !buffer_size) {
  105. status = EFI_INVALID_PARAMETER;
  106. goto out;
  107. }
  108. if (file_path->type != dp.end.type ||
  109. file_path->sub_type != dp.end.sub_type) {
  110. status = EFI_INVALID_PARAMETER;
  111. goto out;
  112. }
  113. if (boot_policy) {
  114. status = EFI_UNSUPPORTED;
  115. goto out;
  116. }
  117. /*
  118. * expect a string with three space separated parts:
  119. *
  120. * * a block device type, e.g. "mmc"
  121. * * a device and partition identifier, e.g. "0:1"
  122. * * a file path on the block device, e.g. "/boot/initrd.cpio.gz"
  123. */
  124. dev = strsep(&pos, " ");
  125. if (!dev)
  126. goto out;
  127. part = strsep(&pos, " ");
  128. if (!part)
  129. goto out;
  130. file = strsep(&pos, " ");
  131. if (!file)
  132. goto out;
  133. file_sz = get_file_size(dev, part, file, &status);
  134. if (!file_sz)
  135. goto out;
  136. if (!buffer || *buffer_size < file_sz) {
  137. status = EFI_BUFFER_TOO_SMALL;
  138. *buffer_size = file_sz;
  139. } else {
  140. ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
  141. if (ret) {
  142. status = EFI_NO_MEDIA;
  143. goto out;
  144. }
  145. ret = fs_read(file, map_to_sysmem(buffer), 0, *buffer_size,
  146. &read_sz);
  147. if (ret || read_sz != file_sz)
  148. goto out;
  149. *buffer_size = read_sz;
  150. status = EFI_SUCCESS;
  151. }
  152. out:
  153. free(filespec);
  154. return EFI_EXIT(status);
  155. }
  156. /**
  157. * efi_initrd_register() - create handle for loading initial RAM disk
  158. *
  159. * This function creates a new handle and installs a Linux specific vendor
  160. * device path and an EFI_LOAD_FILE_2_PROTOCOL. Linux uses the device path
  161. * to identify the handle and then calls the LoadFile service of the
  162. * EFI_LOAD_FILE_2_PROTOCOL to read the initial RAM disk.
  163. *
  164. * Return: status code
  165. */
  166. efi_status_t efi_initrd_register(void)
  167. {
  168. efi_handle_t efi_initrd_handle = NULL;
  169. efi_status_t ret;
  170. ret = EFI_CALL(efi_install_multiple_protocol_interfaces
  171. (&efi_initrd_handle,
  172. /* initramfs */
  173. &efi_guid_device_path, &dp,
  174. /* LOAD_FILE2 */
  175. &efi_guid_load_file2_protocol,
  176. (void *)&efi_lf2_protocol,
  177. NULL));
  178. return ret;
  179. }