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