efi_load_initrd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. i.e "mmc"
  45. * @part: device partition. i.e "0:1"
  46. * @file: name fo 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. * load_file2() - get information about random number generation
  72. *
  73. * This function implement the LoadFile2() service in order to load an initram
  74. * disk requested by the Linux kernel stub.
  75. * See the UEFI spec for details.
  76. *
  77. * @this: loadfile2 protocol instance
  78. * @file_path: relative path of the file. "" in this case
  79. * @boot_policy: must be false for Loadfile2
  80. * @buffer_size: size of allocated buffer
  81. * @buffer: buffer to load the file
  82. *
  83. * Return: status code
  84. */
  85. static efi_status_t EFIAPI
  86. efi_load_file2_initrd(struct efi_load_file_protocol *this,
  87. struct efi_device_path *file_path, bool boot_policy,
  88. efi_uintn_t *buffer_size, void *buffer)
  89. {
  90. const char *filespec = CONFIG_EFI_INITRD_FILESPEC;
  91. efi_status_t status = EFI_NOT_FOUND;
  92. loff_t file_sz = 0, read_sz = 0;
  93. char *dev, *part, *file;
  94. char *s;
  95. int ret;
  96. EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
  97. buffer_size, buffer);
  98. s = strdup(filespec);
  99. if (!s)
  100. goto out;
  101. if (!this || this != &efi_lf2_protocol ||
  102. !buffer_size) {
  103. status = EFI_INVALID_PARAMETER;
  104. goto out;
  105. }
  106. if (file_path->type != dp.end.type ||
  107. file_path->sub_type != dp.end.sub_type) {
  108. status = EFI_INVALID_PARAMETER;
  109. goto out;
  110. }
  111. if (boot_policy) {
  112. status = EFI_UNSUPPORTED;
  113. goto out;
  114. }
  115. /* expect something like 'mmc 0:1 initrd.cpio.gz' */
  116. dev = strsep(&s, " ");
  117. if (!dev)
  118. goto out;
  119. part = strsep(&s, " ");
  120. if (!part)
  121. goto out;
  122. file = strsep(&s, " ");
  123. if (!file)
  124. goto out;
  125. file_sz = get_file_size(dev, part, file, &status);
  126. if (!file_sz)
  127. goto out;
  128. if (!buffer || *buffer_size < file_sz) {
  129. status = EFI_BUFFER_TOO_SMALL;
  130. *buffer_size = file_sz;
  131. } else {
  132. ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
  133. if (ret) {
  134. status = EFI_NO_MEDIA;
  135. goto out;
  136. }
  137. ret = fs_read(file, map_to_sysmem(buffer), 0, *buffer_size,
  138. &read_sz);
  139. if (ret || read_sz != file_sz)
  140. goto out;
  141. *buffer_size = read_sz;
  142. status = EFI_SUCCESS;
  143. }
  144. out:
  145. free(s);
  146. return EFI_EXIT(status);
  147. }
  148. /**
  149. * efi_initrd_register() - Register a handle and loadfile2 protocol
  150. *
  151. * This function creates a new handle and installs a linux specific GUID
  152. * to handle initram disk loading during boot.
  153. * See the UEFI spec for details.
  154. *
  155. * Return: status code
  156. */
  157. efi_status_t efi_initrd_register(void)
  158. {
  159. efi_handle_t efi_initrd_handle = NULL;
  160. efi_status_t ret;
  161. /*
  162. * Set up the handle with the EFI_LOAD_FILE2_PROTOCOL which Linux may
  163. * use to load the initial ramdisk.
  164. */
  165. ret = EFI_CALL(efi_install_multiple_protocol_interfaces
  166. (&efi_initrd_handle,
  167. /* initramfs */
  168. &efi_guid_device_path, &dp,
  169. /* LOAD_FILE2 */
  170. &efi_guid_load_file2_protocol,
  171. (void *)&efi_lf2_protocol,
  172. NULL));
  173. return ret;
  174. }