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