2 * Copyright (c) 2017 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Routines to format EFI_DEVICE_PATHs from the UEFI standard. Much of
29 * this file is taken from EDK2 and rototilled.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
39 #include <sys/endian.h>
41 #include "efi-osdep.h"
43 #include "uefi-dplib.h"
45 /* XXX maybe I should include the entire DevicePathUtiltiies.c and ifdef out what we don't use */
48 * Taken from MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
49 * hash a11928f3310518ab1c6fd34e8d0fdbb72de9602c 2017-Mar-01
53 Device Path services. The thing to remember is device paths are built out of
54 nodes. The device path is terminated by an end node that is length
55 sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
58 The only place where multi-instance device paths are supported is in
59 environment varibles. Multi-instance device paths should never be placed
62 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
63 This program and the accompanying materials
64 are licensed and made available under the terms and conditions of the BSD License
65 which accompanies this distribution. The full text of the license may be found at
66 http://opensource.org/licenses/bsd-license.php.
68 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
69 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
74 // Template for an end-of-device path node.
76 static CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {
78 END_ENTIRE_DEVICE_PATH_SUBTYPE,
80 END_DEVICE_PATH_LENGTH,
87 Returns the size of a device path in bytes.
89 This function returns the size, in bytes, of the device path data structure
90 specified by DevicePath including the end of device path node.
91 If DevicePath is NULL or invalid, then 0 is returned.
93 @param DevicePath A pointer to a device path data structure.
95 @retval 0 If DevicePath is NULL or invalid.
96 @retval Others The size of a device path in bytes.
102 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
105 CONST EFI_DEVICE_PATH_PROTOCOL *Start;
107 if (DevicePath == NULL) {
111 if (!IsDevicePathValid (DevicePath, 0)) {
116 // Search for the end of the device path structure
119 while (!IsDevicePathEnd (DevicePath)) {
120 DevicePath = NextDevicePathNode (DevicePath);
124 // Compute the size and add back in the size of the end device path structure
126 return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);
130 Determine whether a given device path is valid.
131 If DevicePath is NULL, then ASSERT().
133 @param DevicePath A pointer to a device path data structure.
134 @param MaxSize The maximum size of the device path data structure.
136 @retval TRUE DevicePath is valid.
137 @retval FALSE The length of any node in the DevicePath is less
138 than sizeof (EFI_DEVICE_PATH_PROTOCOL).
139 @retval FALSE If MaxSize is not zero, the size of the DevicePath
141 @retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node
142 count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.
147 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
155 ASSERT (DevicePath != NULL);
162 // Validate the input size big enough to touch the first node.
164 if (MaxSize < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
168 for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {
169 NodeLength = DevicePathNodeLength (DevicePath);
170 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
174 if (NodeLength > MAX_UINTN - Size) {
180 // Validate next node before touch it.
182 if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {
186 if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {
188 if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {
195 // Only return TRUE when the End Device Path node is valid.
197 return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);
201 Returns the Type field of a device path node.
203 Returns the Type field of the device path node specified by Node.
205 If Node is NULL, then ASSERT().
207 @param Node A pointer to a device path node data structure.
209 @return The Type field of the device path node specified by Node.
218 ASSERT (Node != NULL);
219 return ((const EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;
224 Returns the SubType field of a device path node.
226 Returns the SubType field of the device path node specified by Node.
228 If Node is NULL, then ASSERT().
230 @param Node A pointer to a device path node data structure.
232 @return The SubType field of the device path node specified by Node.
241 ASSERT (Node != NULL);
242 return ((const EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;
246 Returns the 16-bit Length field of a device path node.
248 Returns the 16-bit Length field of the device path node specified by Node.
249 Node is not required to be aligned on a 16-bit boundary, so it is recommended
250 that a function such as ReadUnaligned16() be used to extract the contents of
253 If Node is NULL, then ASSERT().
255 @param Node A pointer to a device path node data structure.
257 @return The 16-bit Length field of the device path node specified by Node.
262 DevicePathNodeLength (
266 ASSERT (Node != NULL);
267 return ((const EFI_DEVICE_PATH_PROTOCOL *)Node)->Length[0] |
268 (((const EFI_DEVICE_PATH_PROTOCOL *)Node)->Length[1] << 8);
272 Returns a pointer to the next node in a device path.
274 Returns a pointer to the device path node that follows the device path node
277 If Node is NULL, then ASSERT().
279 @param Node A pointer to a device path node data structure.
281 @return a pointer to the device path node that follows the device path node
285 EFI_DEVICE_PATH_PROTOCOL *
291 ASSERT (Node != NULL);
292 return ((EFI_DEVICE_PATH_PROTOCOL *)(__DECONST(UINT8 *, Node) + DevicePathNodeLength(Node)));
296 Determines if a device path node is an end node of a device path.
297 This includes nodes that are the end of a device path instance and nodes that
298 are the end of an entire device path.
300 Determines if the device path node specified by Node is an end node of a device path.
301 This includes nodes that are the end of a device path instance and nodes that are the
302 end of an entire device path. If Node represents an end node of a device path,
303 then TRUE is returned. Otherwise, FALSE is returned.
305 If Node is NULL, then ASSERT().
307 @param Node A pointer to a device path node data structure.
309 @retval TRUE The device path node specified by Node is an end node of a
311 @retval FALSE The device path node specified by Node is not an end node of
317 IsDevicePathEndType (
321 ASSERT (Node != NULL);
322 return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);
326 Determines if a device path node is an end node of an entire device path.
328 Determines if a device path node specified by Node is an end node of an entire
329 device path. If Node represents the end of an entire device path, then TRUE is
330 returned. Otherwise, FALSE is returned.
332 If Node is NULL, then ASSERT().
334 @param Node A pointer to a device path node data structure.
336 @retval TRUE The device path node specified by Node is the end of an entire
338 @retval FALSE The device path node specified by Node is not the end of an
348 ASSERT (Node != NULL);
349 return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);
353 Fills in all the fields of a device path node that is the end of an entire device path.
355 Fills in all the fields of a device path node specified by Node so Node represents
356 the end of an entire device path. The Type field of Node is set to
357 END_DEVICE_PATH_TYPE, the SubType field of Node is set to
358 END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to
359 END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,
360 so it is recommended that a function such as WriteUnaligned16() be used to set
361 the contents of the Length field.
363 If Node is NULL, then ASSERT().
365 @param Node A pointer to a device path node data structure.
370 SetDevicePathEndNode (
374 ASSERT (Node != NULL);
375 memcpy (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));
379 Sets the length, in bytes, of a device path node.
381 Sets the length of the device path node specified by Node to the value specified
382 by NodeLength. NodeLength is returned. Node is not required to be aligned on
383 a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()
384 be used to set the contents of the Length field.
386 If Node is NULL, then ASSERT().
387 If NodeLength >= SIZE_64KB, then ASSERT().
388 If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().
390 @param Node A pointer to a device path node data structure.
391 @param Length The length, in bytes, of the device path node.
398 SetDevicePathNodeLength (
403 ASSERT (Node != NULL);
404 ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));
405 // return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));
406 le16enc(&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));
411 Creates a device node.
413 This function creates a new device node in a newly allocated buffer of size
414 NodeLength and initializes the device path node header with NodeType and NodeSubType.
415 The new device path node is returned.
416 If NodeLength is smaller than a device path header, then NULL is returned.
417 If there is not enough memory to allocate space for the new device path, then
419 The memory is allocated from EFI boot services memory. It is the responsibility
420 of the caller to free the memory allocated.
422 @param NodeType The device node type for the new device node.
423 @param NodeSubType The device node sub-type for the new device node.
424 @param NodeLength The length of the new device node.
426 @return The new device path.
429 EFI_DEVICE_PATH_PROTOCOL *
433 IN UINT8 NodeSubType,
437 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
439 if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
441 // NodeLength is less than the size of the header.
446 DevicePath = AllocateZeroPool (NodeLength);
447 if (DevicePath != NULL) {
448 DevicePath->Type = NodeType;
449 DevicePath->SubType = NodeSubType;
450 SetDevicePathNodeLength (DevicePath, NodeLength);
457 Creates a new copy of an existing device path.
459 This function allocates space for a new copy of the device path specified by DevicePath.
460 If DevicePath is NULL, then NULL is returned. If the memory is successfully
461 allocated, then the contents of DevicePath are copied to the newly allocated
462 buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.
463 The memory for the new device path is allocated from EFI boot services memory.
464 It is the responsibility of the caller to free the memory allocated.
466 @param DevicePath A pointer to a device path data structure.
468 @retval NULL DevicePath is NULL or invalid.
469 @retval Others A pointer to the duplicated device path.
472 EFI_DEVICE_PATH_PROTOCOL *
474 DuplicateDevicePath (
475 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
483 Size = GetDevicePathSize (DevicePath);
489 // Allocate space for duplicate device path
492 return AllocateCopyPool (Size, DevicePath);
496 Creates a new device path by appending a second device path to a first device path.
498 This function creates a new device path by appending a copy of SecondDevicePath
499 to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path
500 device node from SecondDevicePath is retained. The newly created device path is
501 returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of
502 SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,
503 and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and
504 SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.
506 If there is not enough memory for the newly allocated buffer, then NULL is returned.
507 The memory for the new device path is allocated from EFI boot services memory.
508 It is the responsibility of the caller to free the memory allocated.
510 @param FirstDevicePath A pointer to a device path data structure.
511 @param SecondDevicePath A pointer to a device path data structure.
513 @retval NULL If there is not enough memory for the newly allocated buffer.
514 @retval NULL If FirstDevicePath or SecondDevicePath is invalid.
515 @retval Others A pointer to the new device path if success.
516 Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.
519 EFI_DEVICE_PATH_PROTOCOL *
522 IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL
523 IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL
529 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
530 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;
533 // If there's only 1 path, just duplicate it.
535 if (FirstDevicePath == NULL) {
536 return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mUefiDevicePathLibEndDevicePath);
539 if (SecondDevicePath == NULL) {
540 return DuplicateDevicePath (FirstDevicePath);
543 if (!IsDevicePathValid (FirstDevicePath, 0) || !IsDevicePathValid (SecondDevicePath, 0)) {
548 // Allocate space for the combined device path. It only has one end node of
549 // length EFI_DEVICE_PATH_PROTOCOL.
551 Size1 = GetDevicePathSize (FirstDevicePath);
552 Size2 = GetDevicePathSize (SecondDevicePath);
553 Size = Size1 + Size2 - END_DEVICE_PATH_LENGTH;
555 NewDevicePath = AllocatePool (Size);
557 if (NewDevicePath != NULL) {
558 NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);
560 // Over write FirstDevicePath EndNode and do the copy
562 DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +
563 (Size1 - END_DEVICE_PATH_LENGTH));
564 CopyMem (DevicePath2, SecondDevicePath, Size2);
567 return NewDevicePath;
571 Creates a new path by appending the device node to the device path.
573 This function creates a new device path by appending a copy of the device node
574 specified by DevicePathNode to a copy of the device path specified by DevicePath
575 in an allocated buffer. The end-of-device-path device node is moved after the
576 end of the appended device node.
577 If DevicePathNode is NULL then a copy of DevicePath is returned.
578 If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device
579 path device node is returned.
580 If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path
581 device node is returned.
582 If there is not enough memory to allocate space for the new device path, then
584 The memory is allocated from EFI boot services memory. It is the responsibility
585 of the caller to free the memory allocated.
587 @param DevicePath A pointer to a device path data structure.
588 @param DevicePathNode A pointer to a single device path node.
590 @retval NULL If there is not enough memory for the new device path.
591 @retval Others A pointer to the new device path if success.
592 A copy of DevicePathNode followed by an end-of-device-path node
593 if both FirstDevicePath and SecondDevicePath are NULL.
594 A copy of an end-of-device-path node if both FirstDevicePath
595 and SecondDevicePath are NULL.
598 EFI_DEVICE_PATH_PROTOCOL *
600 AppendDevicePathNode (
601 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL
602 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL
605 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
606 EFI_DEVICE_PATH_PROTOCOL *NextNode;
607 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
610 if (DevicePathNode == NULL) {
611 return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mUefiDevicePathLibEndDevicePath);
614 // Build a Node that has a terminator on it
616 NodeLength = DevicePathNodeLength (DevicePathNode);
618 TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);
619 if (TempDevicePath == NULL) {
622 TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);
624 // Add and end device path node to convert Node to device path
626 NextNode = NextDevicePathNode (TempDevicePath);
627 SetDevicePathEndNode (NextNode);
629 // Append device paths
631 NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);
633 FreePool (TempDevicePath);
635 return NewDevicePath;