]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/DevicePathLib.h
Code have been checked with spec
[mirror_edk2.git] / MdePkg / Include / Library / DevicePathLib.h
1 /** @file
2 Provides library functions to construct and parse UEFI Device Paths.
3
4 This library provides defines, macros, and functions to help create and parse
5 EFI_DEVICE_PATH_PROTOCOL structures. The macros that help create and parse device
6 path nodes make use of the ReadUnaligned16() and WriteUnaligned16() functions from
7 the Base Library, so this library class has an implied dependency on the Base Library.
8
9 Copyright (c) 2006 - 2008, Intel Corporation<BR>
10 All rights reserved. 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
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #ifndef __DEVICE_PATH_LIB_H__
21 #define __DEVICE_PATH_LIB_H__
22
23 #include <Library/BaseLib.h>
24
25 #define END_DEVICE_PATH_LENGTH (sizeof (EFI_DEVICE_PATH_PROTOCOL))
26
27 /**
28 Macro that returns the Type field of a device path node.
29
30 Returns the Type field of the device path node specified by Node.
31
32 @param Node A pointer to a device path node data structure.
33
34 @return The Type field of the device path node specified by Node.
35
36 **/
37 #define DevicePathType(Node) (((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type)
38
39 /**
40 Macro that returns the SubType field of a device path node.
41
42 Returns the SubType field of the device path node specified by Node.
43
44 @param Node A pointer to a device path node data structure.
45
46 @return The SubType field of the device path node specified by Node.
47
48 **/
49 #define DevicePathSubType(Node) (((EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType)
50
51 /**
52 Macro that returns the 16-bit Length field of a device path node.
53
54 Returns the 16-bit Length field of the device path node specified by Node.
55
56 @param Node A pointer to a device path node data structure.
57
58 @return The 16-bit Length field of the device path node specified by Node.
59
60 **/
61 #define DevicePathNodeLength(Node) ReadUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0])
62
63 /**
64 Macro that returns a pointer to the next node in a device path.
65
66 Returns a pointer to the device path node that follows the device path node specified by Node.
67
68 @param Node A pointer to a device path node data structure.
69
70 @return a pointer to the device path node that follows the device path node specified by Node.
71
72 **/
73 #define NextDevicePathNode(Node) ((EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)(Node) + DevicePathNodeLength(Node)))
74
75 /**
76 Macro that determines if a device path node is an end node of a device path.
77 This includes nodes that are the end of a device path instance and nodes that are the end of an entire device path.
78
79 Determines if the device path node specified by Node is an end node of a device path.
80 This includes nodes that are the end of a device path instance and nodes that are the
81 end of an entire device path. If Node represents an end node of a device path,
82 then TRUE is returned. Otherwise, FALSE is returned.
83
84 @param Node A pointer to a device path node data structure.
85
86 @retval TRUE The device path node specified by Node is an end node of a device path.
87 @retval FALSE The device path node specified by Node is not an end node of a device path.
88
89 **/
90 #define IsDevicePathEndType(Node) ((DevicePathType (Node) & 0x7f) == END_DEVICE_PATH_TYPE)
91
92 /**
93 Macro that determines if a device path node is an end node of an entire device path.
94
95 Determines if a device path node specified by Node is an end node of an entire device path.
96 If Node represents the end of an entire device path, then TRUE is returned. Otherwise, FALSE is returned.
97
98 @param Node A pointer to a device path node data structure.
99
100 @retval TRUE The device path node specified by Node is the end of an entire device path.
101 @retval FALSE The device path node specified by Node is not the end of an entire device path.
102
103 **/
104 #define IsDevicePathEnd(Node) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE)
105
106 /**
107 Macro that determines if a device path node is an end node of a device path instance.
108
109 Determines if a device path node specified by Node is an end node of a device path instance.
110 If Node represents the end of a device path instance, then TRUE is returned. Otherwise, FALSE is returned.
111
112 @param Node A pointer to a device path node data structure.
113
114 @retval TRUE The device path node specified by Node is the end of a device path instance.
115 @retval FALSE The device path node specified by Node is not the end of a device path instance.
116
117 **/
118 #define IsDevicePathEndInstance(Node) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_INSTANCE_DEVICE_PATH_SUBTYPE)
119
120 /**
121 Macro that sets the length, in bytes, of a device path node.
122
123 Sets the length of the device path node specified by Node to the value specified by Length. Length is returned.
124
125 @param Node A pointer to a device path node data structure.
126 @param Length The length, in bytes, of the device path node.
127
128 @return Length
129
130 **/
131 #define SetDevicePathNodeLength(Node,NodeLength) WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(NodeLength))
132
133 /**
134 Macro that fills in all the fields of a device path node that is the end of an entire device path.
135
136 Fills in all the fields of a device path node specified by Node so Node represents the end of an entire device path.
137
138 @param Node A pointer to a device path node data structure.
139
140 **/
141 #define SetDevicePathEndNode(Node) { \
142 DevicePathType (Node) = END_DEVICE_PATH_TYPE; \
143 DevicePathSubType (Node) = END_ENTIRE_DEVICE_PATH_SUBTYPE; \
144 SetDevicePathNodeLength ((Node), END_DEVICE_PATH_LENGTH); \
145 }
146
147 /**
148 Returns the size of a device path in bytes.
149
150 This function returns the size, in bytes, of the device path data structure specified by
151 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.
152
153 @param DevicePath A pointer to a device path data structure.
154
155 @retval 0 If DevicePath is NULL.
156 @retval Others The size of a device path in bytes.
157
158 **/
159 UINTN
160 EFIAPI
161 GetDevicePathSize (
162 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
163 );
164
165 /**
166 Creates a new device path by appending a second device path to a first device path.
167
168 This function allocates space for a new copy of the device path specified by DevicePath. If
169 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the
170 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer
171 is returned. Otherwise, NULL is returned.
172 The memory for the new device path is allocated from EFI boot services memory.
173 It is the responsibility of the caller to free the memory allocated.
174
175 @param DevicePath A pointer to a device path data structure.
176
177 @retval NULL If DevicePath is NULL.
178 @retval Others A pointer to the duplicated device path.
179
180 **/
181 EFI_DEVICE_PATH_PROTOCOL *
182 EFIAPI
183 DuplicateDevicePath (
184 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
185 );
186
187 /**
188 Creates a new device path by appending a second device path to a first device path.
189
190 This function creates a new device path by appending a copy of SecondDevicePath to a copy of
191 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from
192 SecondDevicePath is retained. The newly created device path is returned.
193 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned.
194 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned.
195 If both FirstDevicePath and SecondDevicePath are NULL, then a copy of an end-of-device-path is
196 returned.
197 If there is not enough memory for the newly allocated buffer, then NULL is returned.
198 The memory for the new device path is allocated from EFI boot services memory. It is the
199 responsibility of the caller to free the memory allocated.
200
201 @param FirstDevicePath A pointer to a device path data structure.
202 @param SecondDevicePath A pointer to a device path data structure.
203
204 @retval NULL If there is not enough memory for the newly allocated buffer.
205 @retval Others A pointer to the new device path if success.
206 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
207
208 **/
209 EFI_DEVICE_PATH_PROTOCOL *
210 EFIAPI
211 AppendDevicePath (
212 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL
213 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL
214 );
215
216 /**
217 Creates a new path by appending the device node to the device path.
218
219 This function creates a new device path by appending a copy of the device node specified by
220 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.
221 The end-of-device-path device node is moved after the end of the appended device node.
222 If DevicePathNode is NULL then a copy of DevicePath is returned.
223 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device path device
224 node is returned.
225 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path device node
226 is returned.
227 If there is not enough memory to allocate space for the new device path, then NULL is returned.
228 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
229 free the memory allocated.
230
231 @param DevicePath A pointer to a device path data structure.
232 @param DevicePathNode A pointer to a single device path node.
233
234 @retval NULL If there is not enough memory for the new device path.
235 @retval Others A pointer to the new device path if success.
236 A copy of DevicePathNode followed by an end-of-device-path node
237 if both FirstDevicePath and SecondDevicePath are NULL.
238 A copy of an end-of-device-path node if both FirstDevicePath and SecondDevicePath are NULL.
239
240 **/
241 EFI_DEVICE_PATH_PROTOCOL *
242 EFIAPI
243 AppendDevicePathNode (
244 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
245 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
246 );
247
248 /**
249 Creates a new device path by appending the specified device path instance to the specified device
250 path.
251
252 This function creates a new device path by appending a copy of the device path instance specified
253 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.
254 The end-of-device-path device node is moved after the end of the appended device path instance
255 and a new end-of-device-path-instance node is inserted between.
256 If DevicePath is NULL, then a copy if DevicePathInstance is returned.
257 If DevicePathInstance is NULL, then NULL is returned.
258 If there is not enough memory to allocate space for the new device path, then NULL is returned.
259 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
260 free the memory allocated.
261
262 @param DevicePath A pointer to a device path data structure.
263 @param DevicePathInstance A pointer to a device path instance.
264
265 @return A pointer to the new device path.
266
267 **/
268 EFI_DEVICE_PATH_PROTOCOL *
269 EFIAPI
270 AppendDevicePathInstance (
271 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
272 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL
273 );
274
275 /**
276 Creates a copy of the current device path instance and returns a pointer to the next device path
277 instance.
278
279 This function creates a copy of the current device path instance. It also updates DevicePath to
280 point to the next device path instance in the device path (or NULL if no more) and updates Size
281 to hold the size of the device path instance copy.
282 If DevicePath is NULL, then NULL is returned.
283 If there is not enough memory to allocate space for the new device path, then NULL is returned.
284 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
285 free the memory allocated.
286 If Size is NULL, then ASSERT().
287
288 @param DevicePath On input, this holds the pointer to the current device path
289 instance. On output, this holds the pointer to the next device
290 path instance or NULL if there are no more device path
291 instances in the device path pointer to a device path data
292 structure.
293 @param Size On output, this holds the size of the device path instance, in
294 bytes or zero, if DevicePath is NULL.
295
296 @return A pointer to the current device path instance.
297
298 **/
299 EFI_DEVICE_PATH_PROTOCOL *
300 EFIAPI
301 GetNextDevicePathInstance (
302 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
303 OUT UINTN *Size
304 );
305
306 /**
307 Creates a device node.
308
309 This function creates a new device node in a newly allocated buffer of size NodeLength and
310 initializes the device path node header with NodeType and NodeSubType. The new device path node
311 is returned.
312 If NodeLength is smaller than a device path header, then NULL is returned.
313 If there is not enough memory to allocate space for the new device path, then NULL is returned.
314 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to
315 free the memory allocated.
316
317 @param NodeType The device node type for the new device node.
318 @param NodeSubType The device node sub-type for the new device node.
319 @param NodeLength The length of the new device node.
320
321 @return The new device path.
322
323 **/
324 EFI_DEVICE_PATH_PROTOCOL *
325 EFIAPI
326 CreateDeviceNode (
327 IN UINT8 NodeType,
328 IN UINT8 NodeSubType,
329 IN UINT16 NodeLength
330 );
331
332 /**
333 Determines if a device path is single or multi-instance.
334
335 This function returns TRUE if the device path specified by DevicePath is multi-instance.
336 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.
337
338 @param DevicePath A pointer to a device path data structure.
339
340 @retval TRUE DevicePath is multi-instance.
341 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.
342
343 **/
344 BOOLEAN
345 EFIAPI
346 IsDevicePathMultiInstance (
347 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
348 );
349
350 /**
351 Retrieves the device path protocol from a handle.
352
353 This function returns the device path protocol from the handle specified by Handle. If Handle is
354 NULL or Handle does not contain a device path protocol, then NULL is returned.
355
356 @param Handle The handle from which to retrieve the device path protocol.
357
358 @return The device path protocol from the handle specified by Handle.
359
360 **/
361 EFI_DEVICE_PATH_PROTOCOL *
362 EFIAPI
363 DevicePathFromHandle (
364 IN EFI_HANDLE Handle
365 );
366
367 /**
368 Allocates a device path for a file and appends it to an existing device path.
369
370 If Device is a valid device handle that contains a device path protocol, then a device path for
371 the file specified by FileName is allocated and appended to the device path associated with the
372 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle
373 that does not support the device path protocol, then a device path containing a single device
374 path node for the file specified by FileName is allocated and returned.
375 The memory for the new device path is allocated from EFI boot services memory. It is the responsibility
376 of the caller to free the memory allocated.
377
378 If FileName is NULL, then ASSERT().
379 If FileName is not aligned on a 16-bit boundary, then ASSERT().
380
381 @param Device A pointer to a device handle. This parameter is optional and
382 may be NULL.
383 @param FileName A pointer to a Null-terminated Unicode string.
384
385 @return The allocated device path.
386
387 **/
388 EFI_DEVICE_PATH_PROTOCOL *
389 EFIAPI
390 FileDevicePath (
391 IN EFI_HANDLE Device, OPTIONAL
392 IN CONST CHAR16 *FileName
393 );
394
395 #endif