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