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