]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
Checked in part of MDE library instances following PI and UEFI. It includes:
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
CommitLineData
e386b444 1/** @file\r
2 UEFI Device Path Library.\r
3\r
4 Copyright (c) 2006, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 Module Name: UefiDevicePathLib.c\r
14\r
15**/\r
16\r
17//\r
c7d265a9 18// The package level header files this module uses\r
e386b444 19//\r
c7d265a9 20#include <Uefi.h>\r
21//\r
22// The protocols, PPI and GUID defintions for this module\r
23//\r
24#include <Protocol/DevicePathUtilities.h>\r
25#include <Protocol/DevicePath.h>\r
26//\r
27// The Library classes this module consumes\r
28//\r
29#include <Library/DevicePathLib.h>\r
30#include <Library/DebugLib.h>\r
31#include <Library/BaseLib.h>\r
32#include <Library/MemoryAllocationLib.h>\r
33#include <Library/BaseMemoryLib.h>\r
34#include <Library/UefiBootServicesTableLib.h>\r
e386b444 35\r
36STATIC EFI_DEVICE_PATH_UTILITIES_PROTOCOL *mDevicePathUtilities = NULL;\r
37\r
38/**\r
39 The constructor function caches the pointer to DevicePathUtilites protocol.\r
40 \r
41 The constructor function locates DevicePathUtilities protocol from protocol database.\r
42 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS. \r
43\r
44 @param ImageHandle The firmware allocated handle for the EFI image.\r
45 @param SystemTable A pointer to the EFI System Table.\r
46 \r
47 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
48\r
49**/\r
50EFI_STATUS\r
51EFIAPI\r
52DevicePathLibConstructor (\r
53 IN EFI_HANDLE ImageHandle,\r
54 IN EFI_SYSTEM_TABLE *SystemTable\r
55 )\r
56{\r
57 EFI_STATUS Status;\r
58\r
59 Status = gBS->LocateProtocol (\r
60 &gEfiDevicePathUtilitiesProtocolGuid,\r
61 NULL,\r
62 (VOID**) &mDevicePathUtilities\r
63 );\r
64 ASSERT_EFI_ERROR (Status);\r
65 ASSERT (mDevicePathUtilities != NULL);\r
66\r
67 return Status;\r
68}\r
69\r
70/**\r
71 Returns the size of a device path in bytes.\r
72\r
73 This function returns the size, in bytes, of the device path data structure specified by\r
74 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.\r
75\r
76 @param DevicePath A pointer to a device path data structure.\r
77\r
78 @return The size of a device path in bytes.\r
79\r
80**/\r
81UINTN\r
82EFIAPI\r
83GetDevicePathSize (\r
84 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
85 )\r
86{\r
87 return mDevicePathUtilities->GetDevicePathSize (DevicePath);\r
88}\r
89\r
90/**\r
91 Creates a new device path by appending a second device path to a first device path.\r
92\r
93 This function allocates space for a new copy of the device path specified by DevicePath. If\r
94 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the\r
95 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
96 is returned. Otherwise, NULL is returned. \r
97 \r
98 @param DevicePath A pointer to a device path data structure.\r
99\r
100 @return A pointer to the duplicated device path.\r
101\r
102**/\r
103EFI_DEVICE_PATH_PROTOCOL *\r
104EFIAPI\r
105DuplicateDevicePath (\r
106 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
107 )\r
108{\r
109 return mDevicePathUtilities->DuplicateDevicePath (DevicePath);\r
110}\r
111\r
112/**\r
113 Creates a new device path by appending a second device path to a first device path.\r
114\r
115 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
116 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
117 SecondDevicePath is retained. The newly created device path is returned. \r
118 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
119 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
120 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned. \r
121 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
122 The memory for the new device path is allocated from EFI boot services memory. It is the\r
123 responsibility of the caller to free the memory allocated.\r
124\r
125 @param FirstDevicePath A pointer to a device path data structure.\r
126 @param SecondDevicePath A pointer to a device path data structure.\r
127\r
128 @return A pointer to the new device path.\r
129\r
130**/\r
131EFI_DEVICE_PATH_PROTOCOL *\r
132EFIAPI\r
133AppendDevicePath (\r
134 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
135 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
136 )\r
137{\r
138 return mDevicePathUtilities->AppendDevicePath (FirstDevicePath, SecondDevicePath);\r
139}\r
140\r
141/**\r
142 Creates a new path by appending the device node to the device path.\r
143\r
144 This function creates a new device path by appending a copy of the device node specified by\r
145 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.\r
146 The end-of-device-path device node is moved after the end of the appended device node.\r
147 If DevicePath is NULL, then NULL is returned.\r
148 If DevicePathNode is NULL, then NULL is returned.\r
149 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
150 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
151 free the memory allocated.\r
152\r
153 @param DevicePath A pointer to a device path data structure.\r
154 @param DevicePathNode A pointer to a single device path node.\r
155\r
156 @return A pointer to the new device path.\r
157\r
158**/\r
159EFI_DEVICE_PATH_PROTOCOL *\r
160EFIAPI\r
161AppendDevicePathNode (\r
162 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
163 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
164 )\r
165{\r
166 return mDevicePathUtilities->AppendDeviceNode (DevicePath, DevicePathNode);\r
167}\r
168\r
169/**\r
170 Creates a new device path by appending the specified device path instance to the specified device\r
171 path.\r
172 \r
173 This function creates a new device path by appending a copy of the device path instance specified\r
174 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.\r
175 The end-of-device-path device node is moved after the end of the appended device path instance\r
176 and a new end-of-device-path-instance node is inserted between. \r
177 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
178 If DevicePathInstance is NULL, then NULL is returned.\r
179 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
180 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
181 free the memory allocated.\r
182 \r
183 @param DevicePath A pointer to a device path data structure.\r
184 @param DevicePathInstance A pointer to a device path instance.\r
185\r
186 @return A pointer to the new device path.\r
187\r
188**/\r
189EFI_DEVICE_PATH_PROTOCOL *\r
190EFIAPI\r
191AppendDevicePathInstance (\r
192 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
193 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
194 )\r
195{\r
196 return mDevicePathUtilities->AppendDevicePathInstance (DevicePath, DevicePathInstance);\r
197}\r
198\r
199/**\r
200 Creates a copy of the current device path instance and returns a pointer to the next device path\r
201 instance.\r
202\r
203 This function creates a copy of the current device path instance. It also updates DevicePath to\r
204 point to the next device path instance in the device path (or NULL if no more) and updates Size\r
205 to hold the size of the device path instance copy.\r
206 If DevicePath is NULL, then NULL is returned.\r
207 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
208 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
209 free the memory allocated.\r
210 If Size is NULL, then ASSERT().\r
211 \r
212 @param DevicePath On input, this holds the pointer to the current device path\r
213 instance. On output, this holds the pointer to the next device\r
214 path instance or NULL if there are no more device path\r
215 instances in the device path pointer to a device path data\r
216 structure.\r
217 @param Size On output, this holds the size of the device path instance, in\r
218 bytes or zero, if DevicePath is NULL.\r
219\r
220 @return A pointer to the current device path instance.\r
221\r
222**/\r
223EFI_DEVICE_PATH_PROTOCOL *\r
224EFIAPI\r
225GetNextDevicePathInstance (\r
226 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
227 OUT UINTN *Size\r
228 )\r
229{\r
230 ASSERT (Size != NULL);\r
231 return mDevicePathUtilities->GetNextDevicePathInstance (DevicePath, Size);\r
232}\r
233\r
234/**\r
235 Creates a copy of the current device path instance and returns a pointer to the next device path\r
236 instance.\r
237\r
238 This function creates a new device node in a newly allocated buffer of size NodeLength and\r
239 initializes the device path node header with NodeType and NodeSubType. The new device path node\r
240 is returned.\r
241 If NodeLength is smaller than a device path header, then NULL is returned. \r
242 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
243 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
244 free the memory allocated.\r
245\r
246 @param NodeType The device node type for the new device node.\r
247 @param NodeSubType The device node sub-type for the new device node.\r
248 @param NodeLength The length of the new device node.\r
249\r
250 @return The new device path.\r
251\r
252**/\r
253EFI_DEVICE_PATH_PROTOCOL *\r
254EFIAPI\r
255CreateDeviceNode (\r
256 IN UINT8 NodeType,\r
257 IN UINT8 NodeSubType,\r
258 IN UINT16 NodeLength\r
259 )\r
260{\r
261 return mDevicePathUtilities->CreateDeviceNode (NodeType, NodeSubType, NodeLength);\r
262}\r
263\r
264/**\r
265 Determines if a device path is single or multi-instance.\r
266\r
267 This function returns TRUE if the device path specified by DevicePath is multi-instance.\r
268 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.\r
269\r
270 @param DevicePath A pointer to a device path data structure.\r
271\r
272 @retval TRUE DevicePath is multi-instance.\r
273 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.\r
274\r
275**/\r
276BOOLEAN\r
277EFIAPI\r
278IsDevicePathMultiInstance (\r
279 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
280 )\r
281{\r
282 return mDevicePathUtilities->IsDevicePathMultiInstance (DevicePath);\r
283}\r
284\r
285/**\r
286 Retrieves the device path protocol from a handle.\r
287\r
288 This function returns the device path protocol from the handle specified by Handle. If Handle is\r
289 NULL or Handle does not contain a device path protocol, then NULL is returned.\r
290 \r
291 @param Handle The handle from which to retrieve the device path protocol.\r
292\r
293 @return The device path protocol from the handle specified by Handle.\r
294\r
295**/\r
296EFI_DEVICE_PATH_PROTOCOL *\r
297EFIAPI\r
298DevicePathFromHandle (\r
299 IN EFI_HANDLE Handle\r
300 )\r
301{\r
302 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
303 EFI_STATUS Status;\r
304\r
305 Status = gBS->HandleProtocol (\r
306 Handle,\r
307 &gEfiDevicePathProtocolGuid,\r
308 (VOID *) &DevicePath\r
309 );\r
310 if (EFI_ERROR (Status)) {\r
311 DevicePath = NULL;\r
312 }\r
313 return DevicePath;\r
314}\r
315\r
316/**\r
317 Allocates a device path for a file and appends it to an existing device path.\r
318\r
319 If Device is a valid device handle that contains a device path protocol, then a device path for\r
320 the file specified by FileName is allocated and appended to the device path associated with the\r
321 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r
322 that does not support the device path protocol, then a device path containing a single device\r
323 path node for the file specified by FileName is allocated and returned.\r
324 If FileName is NULL, then ASSERT().\r
325\r
326 @param Device A pointer to a device handle. This parameter is optional and\r
327 may be NULL.\r
328 @param FileName A pointer to a Null-terminated Unicode string.\r
329\r
330 @return The allocated device path.\r
331\r
332**/\r
333EFI_DEVICE_PATH_PROTOCOL *\r
334EFIAPI\r
335FileDevicePath (\r
336 IN EFI_HANDLE Device, OPTIONAL\r
337 IN CONST CHAR16 *FileName\r
338 )\r
339{\r
340 UINTN Size;\r
341 FILEPATH_DEVICE_PATH *FilePath;\r
342 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
343 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
344\r
345 DevicePath = NULL;\r
346\r
347 Size = StrSize (FileName);\r
348 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + EFI_END_DEVICE_PATH_LENGTH);\r
349 if (FileDevicePath != NULL) {\r
350 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
351 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
352 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
353 CopyMem (&FilePath->PathName, FileName, Size);\r
354 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
355 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
356\r
357 if (Device != NULL) {\r
358 DevicePath = DevicePathFromHandle (Device);\r
359 }\r
360\r
361 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
362 FreePool (FileDevicePath);\r
363 }\r
364\r
365 return DevicePath;\r
366}\r
367\r