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