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