efi_device_path_utilities.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI device path interface
  4. *
  5. * Copyright (c) 2017 Leif Lindholm
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. const efi_guid_t efi_guid_device_path_utilities_protocol =
  10. EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
  11. /*
  12. * Get size of a device path.
  13. *
  14. * This function implements the GetDevicePathSize service of the device path
  15. * utilities protocol. The device path length includes the end of path tag
  16. * which may be an instance end.
  17. *
  18. * See the Unified Extensible Firmware Interface (UEFI) specification
  19. * for details.
  20. *
  21. * @device_path device path
  22. * @return size in bytes
  23. */
  24. static efi_uintn_t EFIAPI get_device_path_size(
  25. const struct efi_device_path *device_path)
  26. {
  27. efi_uintn_t sz = 0;
  28. EFI_ENTRY("%pD", device_path);
  29. /* size includes the END node: */
  30. if (device_path)
  31. sz = efi_dp_size(device_path) + sizeof(struct efi_device_path);
  32. return EFI_EXIT(sz);
  33. }
  34. /*
  35. * Duplicate a device path.
  36. *
  37. * This function implements the DuplicateDevicePath service of the device path
  38. * utilities protocol.
  39. *
  40. * The UEFI spec does not indicate what happens to the end tag. We follow the
  41. * EDK2 logic: In case the device path ends with an end of instance tag, the
  42. * copy will also end with an end of instance tag.
  43. *
  44. * See the Unified Extensible Firmware Interface (UEFI) specification
  45. * for details.
  46. *
  47. * @device_path device path
  48. * @return copy of the device path
  49. */
  50. static struct efi_device_path * EFIAPI duplicate_device_path(
  51. const struct efi_device_path *device_path)
  52. {
  53. EFI_ENTRY("%pD", device_path);
  54. return EFI_EXIT(efi_dp_dup(device_path));
  55. }
  56. /*
  57. * Append device path.
  58. *
  59. * This function implements the AppendDevicePath service of the device path
  60. * utilities protocol.
  61. *
  62. * See the Unified Extensible Firmware Interface (UEFI) specification
  63. * for details.
  64. *
  65. * @src1 1st device path
  66. * @src2 2nd device path
  67. * @return concatenated device path
  68. */
  69. static struct efi_device_path * EFIAPI append_device_path(
  70. const struct efi_device_path *src1,
  71. const struct efi_device_path *src2)
  72. {
  73. EFI_ENTRY("%pD, %pD", src1, src2);
  74. return EFI_EXIT(efi_dp_append(src1, src2));
  75. }
  76. /*
  77. * Append device path node.
  78. *
  79. * This function implements the AppendDeviceNode service of the device path
  80. * utilities protocol.
  81. *
  82. * See the Unified Extensible Firmware Interface (UEFI) specification
  83. * for details.
  84. *
  85. * @device_path device path
  86. * @device_node device node
  87. * @return concatenated device path
  88. */
  89. static struct efi_device_path * EFIAPI append_device_node(
  90. const struct efi_device_path *device_path,
  91. const struct efi_device_path *device_node)
  92. {
  93. EFI_ENTRY("%pD, %p", device_path, device_node);
  94. return EFI_EXIT(efi_dp_append_node(device_path, device_node));
  95. }
  96. /*
  97. * Append device path instance.
  98. *
  99. * This function implements the AppendDevicePathInstance service of the device
  100. * path utilities protocol.
  101. *
  102. * See the Unified Extensible Firmware Interface (UEFI) specification
  103. * for details.
  104. *
  105. * @device_path 1st device path
  106. * @device_path_instance 2nd device path
  107. * @return concatenated device path
  108. */
  109. static struct efi_device_path * EFIAPI append_device_path_instance(
  110. const struct efi_device_path *device_path,
  111. const struct efi_device_path *device_path_instance)
  112. {
  113. EFI_ENTRY("%pD, %pD", device_path, device_path_instance);
  114. return EFI_EXIT(efi_dp_append_instance(device_path,
  115. device_path_instance));
  116. }
  117. /*
  118. * Get next device path instance.
  119. *
  120. * This function implements the GetNextDevicePathInstance service of the device
  121. * path utilities protocol.
  122. *
  123. * See the Unified Extensible Firmware Interface (UEFI) specification
  124. * for details.
  125. *
  126. * @device_path_instance next device path instance
  127. * @device_path_instance_size size of the device path instance
  128. * @return concatenated device path
  129. */
  130. static struct efi_device_path * EFIAPI get_next_device_path_instance(
  131. struct efi_device_path **device_path_instance,
  132. efi_uintn_t *device_path_instance_size)
  133. {
  134. EFI_ENTRY("%pD, %p", device_path_instance, device_path_instance_size);
  135. return EFI_EXIT(efi_dp_get_next_instance(device_path_instance,
  136. device_path_instance_size));
  137. }
  138. /*
  139. * Check if a device path contains more than one instance.
  140. *
  141. * This function implements the AppendDeviceNode service of the device path
  142. * utilities protocol.
  143. *
  144. * See the Unified Extensible Firmware Interface (UEFI) specification
  145. * for details.
  146. *
  147. * @device_path device path
  148. * @device_node device node
  149. * @return concatenated device path
  150. */
  151. static bool EFIAPI is_device_path_multi_instance(
  152. const struct efi_device_path *device_path)
  153. {
  154. EFI_ENTRY("%pD", device_path);
  155. return EFI_EXIT(efi_dp_is_multi_instance(device_path));
  156. }
  157. /*
  158. * Create device node.
  159. *
  160. * This function implements the CreateDeviceNode service of the device path
  161. * utilities protocol.
  162. *
  163. * See the Unified Extensible Firmware Interface (UEFI) specification
  164. * for details.
  165. *
  166. * @node_type node type
  167. * @node_sub_type node sub type
  168. * @node_length node length
  169. * @return device path node
  170. */
  171. static struct efi_device_path * EFIAPI create_device_node(
  172. uint8_t node_type, uint8_t node_sub_type, uint16_t node_length)
  173. {
  174. EFI_ENTRY("%u, %u, %u", node_type, node_sub_type, node_length);
  175. return EFI_EXIT(efi_dp_create_device_node(node_type, node_sub_type,
  176. node_length));
  177. }
  178. const struct efi_device_path_utilities_protocol efi_device_path_utilities = {
  179. .get_device_path_size = get_device_path_size,
  180. .duplicate_device_path = duplicate_device_path,
  181. .append_device_path = append_device_path,
  182. .append_device_node = append_device_node,
  183. .append_device_path_instance = append_device_path_instance,
  184. .get_next_device_path_instance = get_next_device_path_instance,
  185. .is_device_path_multi_instance = is_device_path_multi_instance,
  186. .create_device_node = create_device_node,
  187. };