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