]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.c
Import some Pei and Dxe related instances for MdePkg.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLib / UefiDevicePathLib.c
CommitLineData
e386b444 1/** @file\r
2 Device Path services. The thing to remember is device paths are built out of\r
3 nodes. The device path is terminated by an end node that is length\r
4 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)\r
5 all over this file.\r
6\r
7 The only place where multi-instance device paths are supported is in\r
8 environment varibles. Multi-instance device paths should never be placed\r
9 on a Handle.\r
10\r
11 Copyright (c) 2006, Intel Corporation \r
12 All rights reserved. This program and the accompanying materials \r
13 are licensed and made available under the terms and conditions of the BSD License \r
14 which accompanies this distribution. The full text of the license may be found at \r
15 http://opensource.org/licenses/bsd-license.php \r
16\r
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
19\r
20 Module Name: UefiDevicePathLib.c\r
21\r
22**/\r
23\r
24//\r
25// Include common header file for this module.\r
26//\r
27#include "CommonHeader.h"\r
28\r
29/**\r
30 Returns the size of a device path in bytes.\r
31\r
32 This function returns the size, in bytes, of the device path data structure specified by\r
33 DevicePath including the end of device path node. If DevicePath is NULL, then 0 is returned.\r
34\r
35 @param DevicePath A pointer to a device path data structure.\r
36\r
37 @return The size of a device path in bytes.\r
38\r
39**/\r
40UINTN\r
41EFIAPI\r
42GetDevicePathSize (\r
43 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
44 )\r
45{\r
46 CONST EFI_DEVICE_PATH_PROTOCOL *Start;\r
47\r
48 if (DevicePath == NULL) {\r
49 return 0;\r
50 }\r
51\r
52 //\r
53 // Search for the end of the device path structure\r
54 //\r
55 Start = DevicePath;\r
56 while (!EfiIsDevicePathEnd (DevicePath)) {\r
57 DevicePath = EfiNextDevicePathNode (DevicePath);\r
58 }\r
59\r
60 //\r
61 // Compute the size and add back in the size of the end device path structure\r
62 //\r
63 return ((UINTN) DevicePath - (UINTN) Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
64}\r
65\r
66/**\r
67 Creates a new device path by appending a second device path to a first device path.\r
68\r
69 This function allocates space for a new copy of the device path specified by DevicePath. If\r
70 DevicePath is NULL, then NULL is returned. If the memory is successfully allocated, then the\r
71 contents of DevicePath are copied to the newly allocated buffer, and a pointer to that buffer\r
72 is returned. Otherwise, NULL is returned. \r
73 \r
74 @param DevicePath A pointer to a device path data structure.\r
75\r
76 @return A pointer to the duplicated device path.\r
77\r
78**/\r
79EFI_DEVICE_PATH_PROTOCOL *\r
80EFIAPI\r
81DuplicateDevicePath (\r
82 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
83 )\r
84{\r
85 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
86 UINTN Size;\r
87\r
88 //\r
89 // Compute the size\r
90 //\r
91 Size = GetDevicePathSize (DevicePath);\r
92 if (Size == 0) {\r
93 return NULL;\r
94 }\r
95\r
96 //\r
97 // Allocate space for duplicate device path\r
98 //\r
99 NewDevicePath = AllocateCopyPool (Size, DevicePath);\r
100\r
101 return NewDevicePath;\r
102}\r
103\r
104/**\r
105 Creates a new device path by appending a second device path to a first device path.\r
106\r
107 This function creates a new device path by appending a copy of SecondDevicePath to a copy of\r
108 FirstDevicePath in a newly allocated buffer. Only the end-of-device-path device node from\r
109 SecondDevicePath is retained. The newly created device path is returned. \r
110 If FirstDevicePath is NULL, then it is ignored, and a duplicate of SecondDevicePath is returned. \r
111 If SecondDevicePath is NULL, then it is ignored, and a duplicate of FirstDevicePath is returned. \r
112 If both FirstDevicePath and SecondDevicePath are NULL, then NULL is returned. \r
113 If there is not enough memory for the newly allocated buffer, then NULL is returned.\r
114 The memory for the new device path is allocated from EFI boot services memory. It is the\r
115 responsibility of the caller to free the memory allocated.\r
116\r
117 @param FirstDevicePath A pointer to a device path data structure.\r
118 @param SecondDevicePath A pointer to a device path data structure.\r
119\r
120 @return A pointer to the new device path.\r
121\r
122**/\r
123EFI_DEVICE_PATH_PROTOCOL *\r
124EFIAPI\r
125AppendDevicePath (\r
126 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL\r
127 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL\r
128 )\r
129{\r
130 UINTN Size;\r
131 UINTN Size1;\r
132 UINTN Size2;\r
133 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
134 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r
135\r
136 //\r
137 // If there's only 1 path, just duplicate it.\r
138 //\r
139 if (FirstDevicePath == NULL) {\r
140 return DuplicateDevicePath (SecondDevicePath);\r
141 }\r
142\r
143 if (SecondDevicePath == NULL) {\r
144 return DuplicateDevicePath (FirstDevicePath);\r
145 }\r
146\r
147 //\r
148 // Allocate space for the combined device path. It only has one end node of\r
149 // length EFI_DEVICE_PATH_PROTOCOL.\r
150 //\r
151 Size1 = GetDevicePathSize (FirstDevicePath);\r
152 Size2 = GetDevicePathSize (SecondDevicePath);\r
153 Size = Size1 + Size2 - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
154\r
155 NewDevicePath = AllocatePool (Size);\r
156\r
157 if (NewDevicePath != NULL) {\r
158 NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);\r
159 //\r
160 // Over write FirstDevicePath EndNode and do the copy\r
161 //\r
162 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +\r
163 (Size1 - sizeof (EFI_DEVICE_PATH_PROTOCOL)));\r
164 CopyMem (DevicePath2, SecondDevicePath, Size2);\r
165 }\r
166\r
167 return NewDevicePath;\r
168}\r
169\r
170/**\r
171 Creates a new path by appending the device node to the device path.\r
172\r
173 This function creates a new device path by appending a copy of the device node specified by\r
174 DevicePathNode to a copy of the device path specified by DevicePath in an allocated buffer.\r
175 The end-of-device-path device node is moved after the end of the appended device node.\r
176 If DevicePath is NULL, then NULL is returned.\r
177 If DevicePathNode is NULL, then NULL is returned.\r
178 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
179 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
180 free the memory allocated.\r
181\r
182 @param DevicePath A pointer to a device path data structure.\r
183 @param DevicePathNode A pointer to a single device path node.\r
184\r
185 @return A pointer to the new device path.\r
186\r
187**/\r
188EFI_DEVICE_PATH_PROTOCOL *\r
189EFIAPI\r
190AppendDevicePathNode (\r
191 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
192 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL\r
193 )\r
194{\r
195 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
196 EFI_DEVICE_PATH_PROTOCOL *NextNode;\r
197 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
198 UINTN NodeLength;\r
199\r
200 if (DevicePath == NULL || DevicePathNode == NULL) {\r
201 return NULL;\r
202 }\r
203 //\r
204 // Build a Node that has a terminator on it\r
205 //\r
206 NodeLength = DevicePathNodeLength (DevicePathNode);\r
207\r
208 TempDevicePath = AllocatePool (NodeLength + sizeof (EFI_DEVICE_PATH_PROTOCOL));\r
209 if (TempDevicePath == NULL) {\r
210 return NULL;\r
211 }\r
212 TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);\r
213 //\r
214 // Add and end device path node to convert Node to device path\r
215 //\r
216 NextNode = NextDevicePathNode (TempDevicePath);\r
217 SetDevicePathEndNode (NextNode);\r
218 //\r
219 // Append device paths\r
220 //\r
221 NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);\r
222\r
223 FreePool (TempDevicePath);\r
224\r
225 return NewDevicePath;\r
226}\r
227\r
228/**\r
229 Creates a new device path by appending the specified device path instance to the specified device\r
230 path.\r
231 \r
232 This function creates a new device path by appending a copy of the device path instance specified\r
233 by DevicePathInstance to a copy of the device path secified by DevicePath in a allocated buffer.\r
234 The end-of-device-path device node is moved after the end of the appended device path instance\r
235 and a new end-of-device-path-instance node is inserted between. \r
236 If DevicePath is NULL, then a copy if DevicePathInstance is returned.\r
237 If DevicePathInstance is NULL, then NULL is returned.\r
238 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
239 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
240 free the memory allocated.\r
241 \r
242 @param DevicePath A pointer to a device path data structure.\r
243 @param DevicePathInstance A pointer to a device path instance.\r
244\r
245 @return A pointer to the new device path.\r
246\r
247**/\r
248EFI_DEVICE_PATH_PROTOCOL *\r
249EFIAPI\r
250AppendDevicePathInstance (\r
251 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL\r
252 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance OPTIONAL\r
253 )\r
254{\r
255 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;\r
256 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
257 UINTN SrcSize;\r
258 UINTN InstanceSize;\r
259\r
260 if (DevicePath == NULL) {\r
261 return DuplicateDevicePath (DevicePathInstance);\r
262 }\r
263\r
264 if (DevicePathInstance == NULL) {\r
265 return NULL;\r
266 }\r
267\r
268 SrcSize = GetDevicePathSize (DevicePath);\r
269 InstanceSize = GetDevicePathSize (DevicePathInstance);\r
270\r
271 NewDevicePath = AllocatePool (SrcSize + InstanceSize);\r
272 if (NewDevicePath != NULL) {\r
273 \r
274 TempDevicePath = CopyMem (NewDevicePath, DevicePath, SrcSize);;\r
275 \r
276 while (!IsDevicePathEnd (TempDevicePath)) {\r
277 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
278 }\r
279 \r
280 TempDevicePath->SubType = END_INSTANCE_DEVICE_PATH_SUBTYPE;\r
281 TempDevicePath = NextDevicePathNode (TempDevicePath);\r
282 CopyMem (TempDevicePath, DevicePathInstance, InstanceSize);\r
283 }\r
284\r
285 return NewDevicePath;\r
286}\r
287\r
288/**\r
289 Creates a copy of the current device path instance and returns a pointer to the next device path\r
290 instance.\r
291\r
292 This function creates a copy of the current device path instance. It also updates DevicePath to\r
293 point to the next device path instance in the device path (or NULL if no more) and updates Size\r
294 to hold the size of the device path instance copy.\r
295 If DevicePath is NULL, then NULL is returned.\r
296 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
297 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
298 free the memory allocated.\r
299 If Size is NULL, then ASSERT().\r
300 \r
301 @param DevicePath On input, this holds the pointer to the current device path\r
302 instance. On output, this holds the pointer to the next device\r
303 path instance or NULL if there are no more device path\r
304 instances in the device path pointer to a device path data\r
305 structure.\r
306 @param Size On output, this holds the size of the device path instance, in\r
307 bytes or zero, if DevicePath is NULL.\r
308\r
309 @return A pointer to the current device path instance.\r
310\r
311**/\r
312EFI_DEVICE_PATH_PROTOCOL *\r
313EFIAPI\r
314GetNextDevicePathInstance (\r
315 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,\r
316 OUT UINTN *Size\r
317 )\r
318{\r
319 EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
320 EFI_DEVICE_PATH_PROTOCOL *ReturnValue;\r
321 UINT8 Temp;\r
322\r
323 ASSERT (Size != NULL);\r
324\r
325 if (DevicePath == NULL || *DevicePath == NULL) {\r
326 *Size = 0;\r
327 return NULL;\r
328 }\r
329\r
330 //\r
331 // Find the end of the device path instance\r
332 //\r
333 DevPath = *DevicePath;\r
334 while (!IsDevicePathEndType (DevPath)) {\r
335 DevPath = NextDevicePathNode (DevPath);\r
336 }\r
337\r
338 //\r
339 // Compute the size of the device path instance\r
340 //\r
341 *Size = ((UINTN) DevPath - (UINTN) (*DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
342 \r
343 //\r
344 // Make a copy and return the device path instance\r
345 //\r
346 Temp = DevPath->SubType;\r
347 DevPath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;\r
348 ReturnValue = DuplicateDevicePath (*DevicePath);\r
349 DevPath->SubType = Temp;\r
350\r
351 //\r
352 // If DevPath is the end of an entire device path, then another instance\r
353 // does not follow, so *DevicePath is set to NULL.\r
354 //\r
355 if (DevicePathSubType (DevPath) == END_ENTIRE_DEVICE_PATH_SUBTYPE) {\r
356 *DevicePath = NULL;\r
357 } else {\r
358 *DevicePath = NextDevicePathNode (DevPath);\r
359 }\r
360\r
361 return ReturnValue;\r
362}\r
363\r
364/**\r
365 Creates a copy of the current device path instance and returns a pointer to the next device path\r
366 instance.\r
367\r
368 This function creates a new device node in a newly allocated buffer of size NodeLength and\r
369 initializes the device path node header with NodeType and NodeSubType. The new device path node\r
370 is returned.\r
371 If NodeLength is smaller than a device path header, then NULL is returned. \r
372 If there is not enough memory to allocate space for the new device path, then NULL is returned. \r
373 The memory is allocated from EFI boot services memory. It is the responsibility of the caller to\r
374 free the memory allocated.\r
375\r
376 @param NodeType The device node type for the new device node.\r
377 @param NodeSubType The device node sub-type for the new device node.\r
378 @param NodeLength The length of the new device node.\r
379\r
380 @return The new device path.\r
381\r
382**/\r
383EFI_DEVICE_PATH_PROTOCOL *\r
384EFIAPI\r
385CreateDeviceNode (\r
386 IN UINT8 NodeType,\r
387 IN UINT8 NodeSubType,\r
388 IN UINT16 NodeLength\r
389 )\r
390{\r
391 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
392\r
393 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
394 //\r
395 // NodeLength is less than the size of the header.\r
396 //\r
397 return NULL;\r
398 }\r
399 \r
400 DevicePath = AllocatePool (NodeLength);\r
401 if (DevicePath != NULL) {\r
402 DevicePath->Type = NodeType;\r
403 DevicePath->SubType = NodeSubType;\r
404 SetDevicePathNodeLength (DevicePath, NodeLength);\r
405 }\r
406\r
407 return DevicePath;\r
408}\r
409\r
410/**\r
411 Determines if a device path is single or multi-instance.\r
412\r
413 This function returns TRUE if the device path specified by DevicePath is multi-instance.\r
414 Otherwise, FALSE is returned. If DevicePath is NULL, then FALSE is returned.\r
415\r
416 @param DevicePath A pointer to a device path data structure.\r
417\r
418 @retval TRUE DevicePath is multi-instance.\r
419 @retval FALSE DevicePath is not multi-instance or DevicePath is NULL.\r
420\r
421**/\r
422BOOLEAN\r
423EFIAPI\r
424IsDevicePathMultiInstance (\r
425 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
426 )\r
427{\r
428 CONST EFI_DEVICE_PATH_PROTOCOL *Node;\r
429\r
430 if (DevicePath == NULL) {\r
431 return FALSE;\r
432 }\r
433\r
434 Node = DevicePath;\r
435 while (!EfiIsDevicePathEnd (Node)) {\r
436 if (EfiIsDevicePathEndInstance (Node)) {\r
437 return TRUE;\r
438 }\r
439\r
440 Node = EfiNextDevicePathNode (Node);\r
441 }\r
442\r
443 return FALSE;\r
444}\r
445\r
446\r
447/**\r
448 Retrieves the device path protocol from a handle.\r
449\r
450 This function returns the device path protocol from the handle specified by Handle. If Handle is\r
451 NULL or Handle does not contain a device path protocol, then NULL is returned.\r
452 \r
453 @param Handle The handle from which to retrieve the device path protocol.\r
454\r
455 @return The device path protocol from the handle specified by Handle.\r
456\r
457**/\r
458EFI_DEVICE_PATH_PROTOCOL *\r
459EFIAPI\r
460DevicePathFromHandle (\r
461 IN EFI_HANDLE Handle\r
462 )\r
463{\r
464 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
465 EFI_STATUS Status;\r
466\r
467 Status = gBS->HandleProtocol (\r
468 Handle,\r
469 &gEfiDevicePathProtocolGuid,\r
470 (VOID *) &DevicePath\r
471 );\r
472 if (EFI_ERROR (Status)) {\r
473 DevicePath = NULL;\r
474 }\r
475 return DevicePath;\r
476}\r
477\r
478/**\r
479 Allocates a device path for a file and appends it to an existing device path.\r
480\r
481 If Device is a valid device handle that contains a device path protocol, then a device path for\r
482 the file specified by FileName is allocated and appended to the device path associated with the\r
483 handle Device. The allocated device path is returned. If Device is NULL or Device is a handle\r
484 that does not support the device path protocol, then a device path containing a single device\r
485 path node for the file specified by FileName is allocated and returned.\r
486 If FileName is NULL, then ASSERT().\r
487\r
488 @param Device A pointer to a device handle. This parameter is optional and\r
489 may be NULL.\r
490 @param FileName A pointer to a Null-terminated Unicode string.\r
491\r
492 @return The allocated device path.\r
493\r
494**/\r
495EFI_DEVICE_PATH_PROTOCOL *\r
496EFIAPI\r
497FileDevicePath (\r
498 IN EFI_HANDLE Device, OPTIONAL\r
499 IN CONST CHAR16 *FileName\r
500 )\r
501{\r
502 UINTN Size;\r
503 FILEPATH_DEVICE_PATH *FilePath;\r
504 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
505 EFI_DEVICE_PATH_PROTOCOL *FileDevicePath;\r
506\r
507 DevicePath = NULL;\r
508\r
509 Size = StrSize (FileName);\r
510 FileDevicePath = AllocatePool (Size + SIZE_OF_FILEPATH_DEVICE_PATH + EFI_END_DEVICE_PATH_LENGTH);\r
511 if (FileDevicePath != NULL) {\r
512 FilePath = (FILEPATH_DEVICE_PATH *) FileDevicePath;\r
513 FilePath->Header.Type = MEDIA_DEVICE_PATH;\r
514 FilePath->Header.SubType = MEDIA_FILEPATH_DP;\r
515 CopyMem (&FilePath->PathName, FileName, Size);\r
516 SetDevicePathNodeLength (&FilePath->Header, Size + SIZE_OF_FILEPATH_DEVICE_PATH);\r
517 SetDevicePathEndNode (NextDevicePathNode (&FilePath->Header));\r
518\r
519 if (Device != NULL) {\r
520 DevicePath = DevicePathFromHandle (Device);\r
521 }\r
522\r
523 DevicePath = AppendDevicePath (DevicePath, FileDevicePath);\r
524 FreePool (FileDevicePath);\r
525 }\r
526\r
527 return DevicePath;\r
528}\r
529\r