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 
7   The only place where multi-instance device paths are supported is in
8   environment varibles. Multi-instance device paths should never be placed
9   on a Handle.
10 
11   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
12   SPDX-License-Identifier: BSD-2-Clause-Patent
13 
14 **/
15 
16 
17 #include "UefiDevicePathLib.h"
18 
19 /**
20   Returns the size of a device path in bytes.
21 
22   This function returns the size, in bytes, of the device path data structure
23   specified by DevicePath including the end of device path node.
24   If DevicePath is NULL or invalid, then 0 is returned.
25 
26   @param  DevicePath  A pointer to a device path data structure.
27 
28   @retval 0           If DevicePath is NULL or invalid.
29   @retval Others      The size of a device path in bytes.
30 
31 **/
32 UINTN
33 EFIAPI
GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath)34 GetDevicePathSize (
35   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
36   )
37 {
38   return UefiDevicePathLibGetDevicePathSize (DevicePath);
39 }
40 
41 /**
42   Creates a new copy of an existing device path.
43 
44   This function allocates space for a new copy of the device path specified by DevicePath.
45   If DevicePath is NULL, then NULL is returned.  If the memory is successfully
46   allocated, then the contents of DevicePath are copied to the newly allocated
47   buffer, and a pointer to that buffer is returned.  Otherwise, NULL is returned.
48   The memory for the new device path is allocated from EFI boot services memory.
49   It is the responsibility of the caller to free the memory allocated.
50 
51   @param  DevicePath    A pointer to a device path data structure.
52 
53   @retval NULL          DevicePath is NULL or invalid.
54   @retval Others        A pointer to the duplicated device path.
55 
56 **/
57 EFI_DEVICE_PATH_PROTOCOL *
58 EFIAPI
DuplicateDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath)59 DuplicateDevicePath (
60   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
61   )
62 {
63   return UefiDevicePathLibDuplicateDevicePath (DevicePath);
64 }
65 
66 /**
67   Creates a new device path by appending a second device path to a first device path.
68 
69   This function creates a new device path by appending a copy of SecondDevicePath
70   to a copy of FirstDevicePath in a newly allocated buffer.  Only the end-of-device-path
71   device node from SecondDevicePath is retained. The newly created device path is
72   returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
73   SecondDevicePath is returned.  If SecondDevicePath is NULL, then it is ignored,
74   and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
75   SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
76 
77   If there is not enough memory for the newly allocated buffer, then NULL is returned.
78   The memory for the new device path is allocated from EFI boot services memory.
79   It is the responsibility of the caller to free the memory allocated.
80 
81   @param  FirstDevicePath            A pointer to a device path data structure.
82   @param  SecondDevicePath           A pointer to a device path data structure.
83 
84   @retval NULL      If there is not enough memory for the newly allocated buffer.
85   @retval NULL      If FirstDevicePath or SecondDevicePath is invalid.
86   @retval Others    A pointer to the new device path if success.
87                     Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
88 
89 **/
90 EFI_DEVICE_PATH_PROTOCOL *
91 EFIAPI
AppendDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL * FirstDevicePath,OPTIONAL IN CONST EFI_DEVICE_PATH_PROTOCOL * SecondDevicePath OPTIONAL)92 AppendDevicePath (
93   IN CONST EFI_DEVICE_PATH_PROTOCOL  *FirstDevicePath,  OPTIONAL
94   IN CONST EFI_DEVICE_PATH_PROTOCOL  *SecondDevicePath  OPTIONAL
95   )
96 {
97   return UefiDevicePathLibAppendDevicePath (FirstDevicePath, SecondDevicePath);
98 }
99 
100 /**
101   Creates a new path by appending the device node to the device path.
102 
103   This function creates a new device path by appending a copy of the device node
104   specified by DevicePathNode to a copy of the device path specified by DevicePath
105   in an allocated buffer. The end-of-device-path device node is moved after the
106   end of the appended device node.
107   If DevicePathNode is NULL then a copy of DevicePath is returned.
108   If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
109   path device node is returned.
110   If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
111   device node is returned.
112   If there is not enough memory to allocate space for the new device path, then
113   NULL is returned.
114   The memory is allocated from EFI boot services memory. It is the responsibility
115   of the caller to free the memory allocated.
116 
117   @param  DevicePath                 A pointer to a device path data structure.
118   @param  DevicePathNode             A pointer to a single device path node.
119 
120   @retval NULL      If there is not enough memory for the new device path.
121   @retval Others    A pointer to the new device path if success.
122                     A copy of DevicePathNode followed by an end-of-device-path node
123                     if both FirstDevicePath and SecondDevicePath are NULL.
124                     A copy of an end-of-device-path node if both FirstDevicePath
125                     and SecondDevicePath are NULL.
126 
127 **/
128 EFI_DEVICE_PATH_PROTOCOL *
129 EFIAPI
AppendDevicePathNode(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,OPTIONAL IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePathNode OPTIONAL)130 AppendDevicePathNode (
131   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath,     OPTIONAL
132   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePathNode  OPTIONAL
133   )
134 {
135   return UefiDevicePathLibAppendDevicePathNode (DevicePath, DevicePathNode);
136 }
137 
138 /**
139   Creates a new device path by appending the specified device path instance to the specified device
140   path.
141 
142   This function creates a new device path by appending a copy of the device path
143   instance specified by DevicePathInstance to a copy of the device path specified
144   by DevicePath in a allocated buffer.
145   The end-of-device-path device node is moved after the end of the appended device
146   path instance and a new end-of-device-path-instance node is inserted between.
147   If DevicePath is NULL, then a copy if DevicePathInstance is returned.
148   If DevicePathInstance is NULL, then NULL is returned.
149   If DevicePath or DevicePathInstance is invalid, then NULL is returned.
150   If there is not enough memory to allocate space for the new device path, then
151   NULL is returned.
152   The memory is allocated from EFI boot services memory. It is the responsibility
153   of the caller to free the memory allocated.
154 
155   @param  DevicePath                 A pointer to a device path data structure.
156   @param  DevicePathInstance         A pointer to a device path instance.
157 
158   @return A pointer to the new device path.
159 
160 **/
161 EFI_DEVICE_PATH_PROTOCOL *
162 EFIAPI
AppendDevicePathInstance(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,OPTIONAL IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePathInstance OPTIONAL)163 AppendDevicePathInstance (
164   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath,        OPTIONAL
165   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePathInstance OPTIONAL
166   )
167 {
168   return UefiDevicePathLibAppendDevicePathInstance (DevicePath, DevicePathInstance);
169 }
170 
171 /**
172   Creates a copy of the current device path instance and returns a pointer to the next device path
173   instance.
174 
175   This function creates a copy of the current device path instance. It also updates
176   DevicePath to point to the next device path instance in the device path (or NULL
177   if no more) and updates Size to hold the size of the device path instance copy.
178   If DevicePath is NULL, then NULL is returned.
179   If DevicePath points to a invalid device path, then NULL is returned.
180   If there is not enough memory to allocate space for the new device path, then
181   NULL is returned.
182   The memory is allocated from EFI boot services memory. It is the responsibility
183   of the caller to free the memory allocated.
184   If Size is NULL, then ASSERT().
185 
186   @param  DevicePath                 On input, this holds the pointer to the current
187                                      device path instance. On output, this holds
188                                      the pointer to the next device path instance
189                                      or NULL if there are no more device path
190                                      instances in the device path pointer to a
191                                      device path data structure.
192   @param  Size                       On output, this holds the size of the device
193                                      path instance, in bytes or zero, if DevicePath
194                                      is NULL.
195 
196   @return A pointer to the current device path instance.
197 
198 **/
199 EFI_DEVICE_PATH_PROTOCOL *
200 EFIAPI
GetNextDevicePathInstance(IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,OUT UINTN * Size)201 GetNextDevicePathInstance (
202   IN OUT EFI_DEVICE_PATH_PROTOCOL    **DevicePath,
203   OUT UINTN                          *Size
204   )
205 {
206   return UefiDevicePathLibGetNextDevicePathInstance (DevicePath, Size);
207 }
208 
209 /**
210   Creates a device node.
211 
212   This function creates a new device node in a newly allocated buffer of size
213   NodeLength and initializes the device path node header with NodeType and NodeSubType.
214   The new device path node is returned.
215   If NodeLength is smaller than a device path header, then NULL is returned.
216   If there is not enough memory to allocate space for the new device path, then
217   NULL is returned.
218   The memory is allocated from EFI boot services memory. It is the responsibility
219   of the caller to free the memory allocated.
220 
221   @param  NodeType                   The device node type for the new device node.
222   @param  NodeSubType                The device node sub-type for the new device node.
223   @param  NodeLength                 The length of the new device node.
224 
225   @return The new device path.
226 
227 **/
228 EFI_DEVICE_PATH_PROTOCOL *
229 EFIAPI
CreateDeviceNode(IN UINT8 NodeType,IN UINT8 NodeSubType,IN UINT16 NodeLength)230 CreateDeviceNode (
231   IN UINT8                           NodeType,
232   IN UINT8                           NodeSubType,
233   IN UINT16                          NodeLength
234   )
235 {
236   return UefiDevicePathLibCreateDeviceNode (NodeType, NodeSubType, NodeLength);
237 }
238 
239 /**
240   Determines if a device path is single or multi-instance.
241 
242   This function returns TRUE if the device path specified by DevicePath is
243   multi-instance.
244   Otherwise, FALSE is returned.
245   If DevicePath is NULL or invalid, then FALSE is returned.
246 
247   @param  DevicePath                 A pointer to a device path data structure.
248 
249   @retval  TRUE                      DevicePath is multi-instance.
250   @retval  FALSE                     DevicePath is not multi-instance, or DevicePath
251                                      is NULL or invalid.
252 
253 **/
254 BOOLEAN
255 EFIAPI
IsDevicePathMultiInstance(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath)256 IsDevicePathMultiInstance (
257   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
258   )
259 {
260   return UefiDevicePathLibIsDevicePathMultiInstance (DevicePath);
261 }
262 
263 /**
264   Converts a device node to its string representation.
265 
266   @param DeviceNode        A Pointer to the device node to be converted.
267   @param DisplayOnly       If DisplayOnly is TRUE, then the shorter text representation
268                            of the display node is used, where applicable. If DisplayOnly
269                            is FALSE, then the longer text representation of the display node
270                            is used.
271   @param AllowShortcuts    If AllowShortcuts is TRUE, then the shortcut forms of text
272                            representation for a device node can be used, where applicable.
273 
274   @return A pointer to the allocated text representation of the device node or NULL if DeviceNode
275           is NULL or there was insufficient memory.
276 
277 **/
278 CHAR16 *
279 EFIAPI
ConvertDeviceNodeToText(IN CONST EFI_DEVICE_PATH_PROTOCOL * DeviceNode,IN BOOLEAN DisplayOnly,IN BOOLEAN AllowShortcuts)280 ConvertDeviceNodeToText (
281   IN CONST EFI_DEVICE_PATH_PROTOCOL  *DeviceNode,
282   IN BOOLEAN                         DisplayOnly,
283   IN BOOLEAN                         AllowShortcuts
284   )
285 {
286   return UefiDevicePathLibConvertDeviceNodeToText (DeviceNode, DisplayOnly, AllowShortcuts);
287 }
288 
289 /**
290   Converts a device path to its text representation.
291 
292   @param DevicePath      A Pointer to the device to be converted.
293   @param DisplayOnly     If DisplayOnly is TRUE, then the shorter text representation
294                          of the display node is used, where applicable. If DisplayOnly
295                          is FALSE, then the longer text representation of the display node
296                          is used.
297   @param AllowShortcuts  If AllowShortcuts is TRUE, then the shortcut forms of text
298                          representation for a device node can be used, where applicable.
299 
300   @return A pointer to the allocated text representation of the device path or
301           NULL if DeviceNode is NULL or there was insufficient memory.
302 
303 **/
304 CHAR16 *
305 EFIAPI
ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL * DevicePath,IN BOOLEAN DisplayOnly,IN BOOLEAN AllowShortcuts)306 ConvertDevicePathToText (
307   IN CONST EFI_DEVICE_PATH_PROTOCOL   *DevicePath,
308   IN BOOLEAN                          DisplayOnly,
309   IN BOOLEAN                          AllowShortcuts
310   )
311 {
312   return UefiDevicePathLibConvertDevicePathToText (DevicePath, DisplayOnly, AllowShortcuts);
313 }
314 
315 /**
316   Convert text to the binary representation of a device node.
317 
318   @param TextDeviceNode  TextDeviceNode points to the text representation of a device
319                          node. Conversion starts with the first character and continues
320                          until the first non-device node character.
321 
322   @return A pointer to the EFI device node or NULL if TextDeviceNode is NULL or there was
323           insufficient memory or text unsupported.
324 
325 **/
326 EFI_DEVICE_PATH_PROTOCOL *
327 EFIAPI
ConvertTextToDeviceNode(IN CONST CHAR16 * TextDeviceNode)328 ConvertTextToDeviceNode (
329   IN CONST CHAR16 *TextDeviceNode
330   )
331 {
332   return UefiDevicePathLibConvertTextToDeviceNode (TextDeviceNode);
333 }
334 
335 /**
336   Convert text to the binary representation of a device path.
337 
338 
339   @param TextDevicePath  TextDevicePath points to the text representation of a device
340                          path. Conversion starts with the first character and continues
341                          until the first non-device node character.
342 
343   @return A pointer to the allocated device path or NULL if TextDeviceNode is NULL or
344           there was insufficient memory.
345 
346 **/
347 EFI_DEVICE_PATH_PROTOCOL *
348 EFIAPI
ConvertTextToDevicePath(IN CONST CHAR16 * TextDevicePath)349 ConvertTextToDevicePath (
350   IN CONST CHAR16 *TextDevicePath
351   )
352 {
353   return UefiDevicePathLibConvertTextToDevicePath (TextDevicePath);
354 }
355