UefiDevicePathLib.c 12 KB

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