UefiDevicePathLib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /** @file
  2. Device Path services. The thing to remember is device paths are built out of
  3. nodes. The device path is terminated by an end node that is length
  4. sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
  5. all over this file.
  6. The only place where multi-instance device paths are supported is in
  7. environment variables. Multi-instance device paths should never be placed
  8. on a Handle.
  9. Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
  10. SPDX-License-Identifier: BSD-2-Clause-Patent
  11. **/
  12. #include "UefiDevicePathLib.h"
  13. /**
  14. Returns the size of a device path in bytes.
  15. This function returns the size, in bytes, of the device path data structure
  16. specified by DevicePath including the end of device path node.
  17. If DevicePath is NULL or invalid, then 0 is returned.
  18. @param DevicePath A pointer to a device path data structure.
  19. @retval 0 If DevicePath is NULL or invalid.
  20. @retval Others The size of a device path in bytes.
  21. **/
  22. UINTN
  23. GetDevicePathSize (
  24. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  25. )
  26. {
  27. return UefiDevicePathLibGetDevicePathSize (DevicePath);
  28. }
  29. /**
  30. Creates a new copy of an existing device path.
  31. This function allocates space for a new copy of the device path specified by DevicePath.
  32. If DevicePath is NULL, then NULL is returned. If the memory is successfully
  33. allocated, then the contents of DevicePath are copied to the newly allocated
  34. buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
  35. The memory for the new device path is allocated from EFI boot services memory.
  36. It is the responsibility of the caller to free the memory allocated.
  37. @param DevicePath A pointer to a device path data structure.
  38. @retval NULL DevicePath is NULL or invalid.
  39. @retval Others A pointer to the duplicated device path.
  40. **/
  41. EFI_DEVICE_PATH_PROTOCOL *
  42. DuplicateDevicePath (
  43. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  44. )
  45. {
  46. return UefiDevicePathLibDuplicateDevicePath (DevicePath);
  47. }
  48. /**
  49. Creates a new device path by appending a second device path to a first device path.
  50. This function creates a new device path by appending a copy of SecondDevicePath
  51. to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
  52. device node from SecondDevicePath is retained. The newly created device path is
  53. returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
  54. SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
  55. and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
  56. SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
  57. If there is not enough memory for the newly allocated buffer, then NULL is returned.
  58. The memory for the new device path is allocated from EFI boot services memory.
  59. It is the responsibility of the caller to free the memory allocated.
  60. @param FirstDevicePath A pointer to a device path data structure.
  61. @param SecondDevicePath A pointer to a device path data structure.
  62. @retval NULL If there is not enough memory for the newly allocated buffer.
  63. @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
  64. @retval Others A pointer to the new device path if success.
  65. Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
  66. **/
  67. EFI_DEVICE_PATH_PROTOCOL *
  68. AppendDevicePath (
  69. CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL
  70. CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL
  71. )
  72. {
  73. return UefiDevicePathLibAppendDevicePath (FirstDevicePath, SecondDevicePath);
  74. }
  75. /**
  76. Creates a new path by appending the device node to the device path.
  77. This function creates a new device path by appending a copy of the device node
  78. specified by DevicePathNode to a copy of the device path specified by DevicePath
  79. in an allocated buffer. The end-of-device-path device node is moved after the
  80. end of the appended device node.
  81. If DevicePathNode is NULL then a copy of DevicePath is returned.
  82. If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
  83. path device node is returned.
  84. If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
  85. device node is returned.
  86. If there is not enough memory to allocate space for the new device path, then
  87. NULL is returned.
  88. The memory is allocated from EFI boot services memory. It is the responsibility
  89. of the caller to free the memory allocated.
  90. @param DevicePath A pointer to a device path data structure.
  91. @param DevicePathNode A pointer to a single device path node.
  92. @retval NULL If there is not enough memory for the new device path.
  93. @retval Others A pointer to the new device path if success.
  94. A copy of DevicePathNode followed by an end-of-device-path node
  95. if both FirstDevicePath and SecondDevicePath are NULL.
  96. A copy of an end-of-device-path node if both FirstDevicePath
  97. and SecondDevicePath are NULL.
  98. **/
  99. EFI_DEVICE_PATH_PROTOCOL *
  100. AppendDevicePathNode (
  101. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
  102. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
  103. )
  104. {
  105. return UefiDevicePathLibAppendDevicePathNode (DevicePath, DevicePathNode);
  106. }
  107. /**
  108. Creates a new device path by appending the specified device path instance to the specified device
  109. path.
  110. This function creates a new device path by appending a copy of the device path
  111. instance specified by DevicePathInstance to a copy of the device path specified
  112. by DevicePath in a allocated buffer.
  113. The end-of-device-path device node is moved after the end of the appended device
  114. path instance and a new end-of-device-path-instance node is inserted between.
  115. If DevicePath is NULL, then a copy if DevicePathInstance is returned.
  116. If DevicePathInstance is NULL, then NULL is returned.
  117. If DevicePath or DevicePathInstance is invalid, then NULL is returned.
  118. If there is not enough memory to allocate space for the new device path, then
  119. NULL is returned.
  120. The memory is allocated from EFI boot services memory. It is the responsibility
  121. of the caller to free the memory allocated.
  122. @param DevicePath A pointer to a device path data structure.
  123. @param DevicePathInstance A pointer to a device path instance.
  124. @return A pointer to the new device path.
  125. **/
  126. EFI_DEVICE_PATH_PROTOCOL *
  127. AppendDevicePathInstance (
  128. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
  129. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL
  130. )
  131. {
  132. return UefiDevicePathLibAppendDevicePathInstance (DevicePath, DevicePathInstance);
  133. }
  134. /**
  135. Creates a copy of the current device path instance and returns a pointer to the next device path
  136. instance.
  137. This function creates a copy of the current device path instance. It also updates
  138. DevicePath to point to the next device path instance in the device path (or NULL
  139. if no more) and updates Size to hold the size of the device path instance copy.
  140. If DevicePath is NULL, then NULL is returned.
  141. If DevicePath points to a invalid device path, then NULL is returned.
  142. If there is not enough memory to allocate space for the new device path, then
  143. NULL is returned.
  144. The memory is allocated from EFI boot services memory. It is the responsibility
  145. of the caller to free the memory allocated.
  146. If Size is NULL, then ASSERT().
  147. @param DevicePath On input, this holds the pointer to the current
  148. device path instance. On output, this holds
  149. the pointer to the next device path instance
  150. or NULL if there are no more device path
  151. instances in the device path pointer to a
  152. device path data structure.
  153. @param Size On output, this holds the size of the device
  154. path instance, in bytes or zero, if DevicePath
  155. is NULL.
  156. @return A pointer to the current device path instance.
  157. **/
  158. EFI_DEVICE_PATH_PROTOCOL *
  159. GetNextDevicePathInstance (
  160. EFI_DEVICE_PATH_PROTOCOL **DevicePath,
  161. UINTN *Size
  162. )
  163. {
  164. return UefiDevicePathLibGetNextDevicePathInstance (DevicePath, Size);
  165. }
  166. /**
  167. Creates a device node.
  168. This function creates a new device node in a newly allocated buffer of size
  169. NodeLength and initializes the device path node header with NodeType and NodeSubType.
  170. The new device path node is returned.
  171. If NodeLength is smaller than a device path header, then NULL is returned.
  172. If there is not enough memory to allocate space for the new device path, then
  173. NULL is returned.
  174. The memory is allocated from EFI boot services memory. It is the responsibility
  175. of the caller to free the memory allocated.
  176. @param NodeType The device node type for the new device node.
  177. @param NodeSubType The device node sub-type for the new device node.
  178. @param NodeLength The length of the new device node.
  179. @return The new device path.
  180. **/
  181. EFI_DEVICE_PATH_PROTOCOL *
  182. CreateDeviceNode (
  183. UINT8 NodeType,
  184. UINT8 NodeSubType,
  185. UINT16 NodeLength
  186. )
  187. {
  188. return UefiDevicePathLibCreateDeviceNode (NodeType, NodeSubType, NodeLength);
  189. }
  190. /**
  191. Determines if a device path is single or multi-instance.
  192. This function returns TRUE if the device path specified by DevicePath is
  193. multi-instance.
  194. Otherwise, FALSE is returned.
  195. If DevicePath is NULL or invalid, then FALSE is returned.
  196. @param DevicePath A pointer to a device path data structure.
  197. @retval TRUE DevicePath is multi-instance.
  198. @retval FALSE DevicePath is not multi-instance, or DevicePath
  199. is NULL or invalid.
  200. **/
  201. BOOLEAN
  202. IsDevicePathMultiInstance (
  203. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  204. )
  205. {
  206. return UefiDevicePathLibIsDevicePathMultiInstance (DevicePath);
  207. }
  208. /**
  209. Convert text to the binary representation of a device node.
  210. @param TextDeviceNode TextDeviceNode points to the text representation of a device
  211. node. Conversion starts with the first character and continues
  212. until the first non-device node character.
  213. @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was
  214. insufficient memory or text unsupported.
  215. **/
  216. EFI_DEVICE_PATH_PROTOCOL *
  217. ConvertTextToDeviceNode (
  218. CONST CHAR16 *TextDeviceNode
  219. )
  220. {
  221. return UefiDevicePathLibConvertTextToDeviceNode (TextDeviceNode);
  222. }
  223. /**
  224. Convert text to the binary representation of a device path.
  225. @param TextDevicePath TextDevicePath points to the text representation of a device
  226. path. Conversion starts with the first character and continues
  227. until the first non-device node character.
  228. @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or
  229. there was insufficient memory.
  230. **/
  231. EFI_DEVICE_PATH_PROTOCOL *
  232. ConvertTextToDevicePath (
  233. CONST CHAR16 *TextDevicePath
  234. )
  235. {
  236. return UefiDevicePathLibConvertTextToDevicePath (TextDevicePath);
  237. }