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