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