/*-1* Copyright (c) 2017 Netflix, Inc.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425/*26* Routines to format EFI_DEVICE_PATHs from the UEFI standard. Much of27* this file is taken from EDK2 and rototilled.28*/2930#include <efivar.h>31#include <limits.h>32#include <stdio.h>33#include <string.h>34#include <sys/endian.h>3536#include "efi-osdep.h"3738#include "uefi-dplib.h"3940/* XXX maybe I should include the entire DevicePathUtiltiies.c and ifdef out what we don't use */4142/*43* Taken from MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c44* hash a11928f3310518ab1c6fd34e8d0fdbb72de9602c 2017-Mar-0145*/4647/** @file48Device Path services. The thing to remember is device paths are built out of49nodes. The device path is terminated by an end node that is length50sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)51all over this file.5253The only place where multi-instance device paths are supported is in54environment varibles. Multi-instance device paths should never be placed55on a Handle.5657Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>58This program and the accompanying materials59are licensed and made available under the terms and conditions of the BSD License60which accompanies this distribution. The full text of the license may be found at61http://opensource.org/licenses/bsd-license.php.6263THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,64WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.6566**/6768//69// Template for an end-of-device path node.70//71static CONST EFI_DEVICE_PATH_PROTOCOL mUefiDevicePathLibEndDevicePath = {72END_DEVICE_PATH_TYPE,73END_ENTIRE_DEVICE_PATH_SUBTYPE,74{75END_DEVICE_PATH_LENGTH,76077}78};798081/**82Returns the size of a device path in bytes.8384This function returns the size, in bytes, of the device path data structure85specified by DevicePath including the end of device path node.86If DevicePath is NULL or invalid, then 0 is returned.8788@param DevicePath A pointer to a device path data structure.8990@retval 0 If DevicePath is NULL or invalid.91@retval Others The size of a device path in bytes.9293**/94UINTN95EFIAPI96GetDevicePathSize (97IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath98)99{100CONST EFI_DEVICE_PATH_PROTOCOL *Start;101102if (DevicePath == NULL) {103return 0;104}105106if (!IsDevicePathValid (DevicePath, 0)) {107return 0;108}109110//111// Search for the end of the device path structure112//113Start = DevicePath;114while (!IsDevicePathEnd (DevicePath)) {115DevicePath = NextDevicePathNode (DevicePath);116}117118//119// Compute the size and add back in the size of the end device path structure120//121return ((UINTN) DevicePath - (UINTN) Start) + DevicePathNodeLength (DevicePath);122}123124/**125Determine whether a given device path is valid.126If DevicePath is NULL, then ASSERT().127128@param DevicePath A pointer to a device path data structure.129@param MaxSize The maximum size of the device path data structure.130131@retval TRUE DevicePath is valid.132@retval FALSE The length of any node in the DevicePath is less133than sizeof (EFI_DEVICE_PATH_PROTOCOL).134@retval FALSE If MaxSize is not zero, the size of the DevicePath135exceeds MaxSize.136@retval FALSE If PcdMaximumDevicePathNodeCount is not zero, the node137count of the DevicePath exceeds PcdMaximumDevicePathNodeCount.138**/139BOOLEAN140EFIAPI141IsDevicePathValid (142IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,143IN UINTN MaxSize144)145{146UINTN Count;147UINTN Size;148UINTN NodeLength;149150ASSERT (DevicePath != NULL);151152if (MaxSize == 0) {153MaxSize = MAX_UINTN;154}155156//157// Validate the input size big enough to touch the first node.158//159if (MaxSize < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {160return FALSE;161}162163for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {164NodeLength = DevicePathNodeLength (DevicePath);165if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {166return FALSE;167}168169if (NodeLength > MAX_UINTN - Size) {170return FALSE;171}172Size += NodeLength;173174//175// Validate next node before touch it.176//177if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {178return FALSE;179}180181if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {182Count++;183if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {184return FALSE;185}186}187}188189//190// Only return TRUE when the End Device Path node is valid.191//192return (BOOLEAN) (DevicePathNodeLength (DevicePath) == END_DEVICE_PATH_LENGTH);193}194195/**196Returns the Type field of a device path node.197198Returns the Type field of the device path node specified by Node.199200If Node is NULL, then ASSERT().201202@param Node A pointer to a device path node data structure.203204@return The Type field of the device path node specified by Node.205206**/207UINT8208EFIAPI209DevicePathType (210IN CONST VOID *Node211)212{213ASSERT (Node != NULL);214return ((const EFI_DEVICE_PATH_PROTOCOL *)(Node))->Type;215}216217218/**219Returns the SubType field of a device path node.220221Returns the SubType field of the device path node specified by Node.222223If Node is NULL, then ASSERT().224225@param Node A pointer to a device path node data structure.226227@return The SubType field of the device path node specified by Node.228229**/230UINT8231EFIAPI232DevicePathSubType (233IN CONST VOID *Node234)235{236ASSERT (Node != NULL);237return ((const EFI_DEVICE_PATH_PROTOCOL *)(Node))->SubType;238}239240/**241Returns the 16-bit Length field of a device path node.242243Returns the 16-bit Length field of the device path node specified by Node.244Node is not required to be aligned on a 16-bit boundary, so it is recommended245that a function such as ReadUnaligned16() be used to extract the contents of246the Length field.247248If Node is NULL, then ASSERT().249250@param Node A pointer to a device path node data structure.251252@return The 16-bit Length field of the device path node specified by Node.253254**/255UINTN256EFIAPI257DevicePathNodeLength (258IN CONST VOID *Node259)260{261ASSERT (Node != NULL);262return ((const EFI_DEVICE_PATH_PROTOCOL *)Node)->Length[0] |263(((const EFI_DEVICE_PATH_PROTOCOL *)Node)->Length[1] << 8);264}265266/**267Returns a pointer to the next node in a device path.268269Returns a pointer to the device path node that follows the device path node270specified by Node.271272If Node is NULL, then ASSERT().273274@param Node A pointer to a device path node data structure.275276@return a pointer to the device path node that follows the device path node277specified by Node.278279**/280EFI_DEVICE_PATH_PROTOCOL *281EFIAPI282NextDevicePathNode (283IN CONST VOID *Node284)285{286ASSERT (Node != NULL);287return ((EFI_DEVICE_PATH_PROTOCOL *)(__DECONST(UINT8 *, Node) + DevicePathNodeLength(Node)));288}289290/**291Determines if a device path node is an end node of a device path.292This includes nodes that are the end of a device path instance and nodes that293are the end of an entire device path.294295Determines if the device path node specified by Node is an end node of a device path.296This includes nodes that are the end of a device path instance and nodes that are the297end of an entire device path. If Node represents an end node of a device path,298then TRUE is returned. Otherwise, FALSE is returned.299300If Node is NULL, then ASSERT().301302@param Node A pointer to a device path node data structure.303304@retval TRUE The device path node specified by Node is an end node of a305device path.306@retval FALSE The device path node specified by Node is not an end node of307a device path.308309**/310BOOLEAN311EFIAPI312IsDevicePathEndType (313IN CONST VOID *Node314)315{316ASSERT (Node != NULL);317return (BOOLEAN) (DevicePathType (Node) == END_DEVICE_PATH_TYPE);318}319320/**321Determines if a device path node is an end node of an entire device path.322323Determines if a device path node specified by Node is an end node of an entire324device path. If Node represents the end of an entire device path, then TRUE is325returned. Otherwise, FALSE is returned.326327If Node is NULL, then ASSERT().328329@param Node A pointer to a device path node data structure.330331@retval TRUE The device path node specified by Node is the end of an entire332device path.333@retval FALSE The device path node specified by Node is not the end of an334entire device path.335336**/337BOOLEAN338EFIAPI339IsDevicePathEnd (340IN CONST VOID *Node341)342{343ASSERT (Node != NULL);344return (BOOLEAN) (IsDevicePathEndType (Node) && DevicePathSubType(Node) == END_ENTIRE_DEVICE_PATH_SUBTYPE);345}346347/**348Fills in all the fields of a device path node that is the end of an entire device path.349350Fills in all the fields of a device path node specified by Node so Node represents351the end of an entire device path. The Type field of Node is set to352END_DEVICE_PATH_TYPE, the SubType field of Node is set to353END_ENTIRE_DEVICE_PATH_SUBTYPE, and the Length field of Node is set to354END_DEVICE_PATH_LENGTH. Node is not required to be aligned on a 16-bit boundary,355so it is recommended that a function such as WriteUnaligned16() be used to set356the contents of the Length field.357358If Node is NULL, then ASSERT().359360@param Node A pointer to a device path node data structure.361362**/363VOID364EFIAPI365SetDevicePathEndNode (366OUT VOID *Node367)368{369ASSERT (Node != NULL);370memcpy (Node, &mUefiDevicePathLibEndDevicePath, sizeof (mUefiDevicePathLibEndDevicePath));371}372373/**374Sets the length, in bytes, of a device path node.375376Sets the length of the device path node specified by Node to the value specified377by NodeLength. NodeLength is returned. Node is not required to be aligned on378a 16-bit boundary, so it is recommended that a function such as WriteUnaligned16()379be used to set the contents of the Length field.380381If Node is NULL, then ASSERT().382If NodeLength >= SIZE_64KB, then ASSERT().383If NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL), then ASSERT().384385@param Node A pointer to a device path node data structure.386@param Length The length, in bytes, of the device path node.387388@return Length389390**/391UINT16392EFIAPI393SetDevicePathNodeLength (394IN OUT VOID *Node,395IN UINTN Length396)397{398ASSERT (Node != NULL);399ASSERT ((Length >= sizeof (EFI_DEVICE_PATH_PROTOCOL)) && (Length < SIZE_64KB));400// return WriteUnaligned16 ((UINT16 *)&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));401le16enc(&((EFI_DEVICE_PATH_PROTOCOL *)(Node))->Length[0], (UINT16)(Length));402return Length;403}404405/**406Creates a device node.407408This function creates a new device node in a newly allocated buffer of size409NodeLength and initializes the device path node header with NodeType and NodeSubType.410The new device path node is returned.411If NodeLength is smaller than a device path header, then NULL is returned.412If there is not enough memory to allocate space for the new device path, then413NULL is returned.414The memory is allocated from EFI boot services memory. It is the responsibility415of the caller to free the memory allocated.416417@param NodeType The device node type for the new device node.418@param NodeSubType The device node sub-type for the new device node.419@param NodeLength The length of the new device node.420421@return The new device path.422423**/424EFI_DEVICE_PATH_PROTOCOL *425EFIAPI426CreateDeviceNode (427IN UINT8 NodeType,428IN UINT8 NodeSubType,429IN UINT16 NodeLength430)431{432EFI_DEVICE_PATH_PROTOCOL *DevicePath;433434if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {435//436// NodeLength is less than the size of the header.437//438return NULL;439}440441DevicePath = AllocateZeroPool (NodeLength);442if (DevicePath != NULL) {443DevicePath->Type = NodeType;444DevicePath->SubType = NodeSubType;445SetDevicePathNodeLength (DevicePath, NodeLength);446}447448return DevicePath;449}450451/**452Creates a new copy of an existing device path.453454This function allocates space for a new copy of the device path specified by DevicePath.455If DevicePath is NULL, then NULL is returned. If the memory is successfully456allocated, then the contents of DevicePath are copied to the newly allocated457buffer, and a pointer to that buffer is returned. Otherwise, NULL is returned.458The memory for the new device path is allocated from EFI boot services memory.459It is the responsibility of the caller to free the memory allocated.460461@param DevicePath A pointer to a device path data structure.462463@retval NULL DevicePath is NULL or invalid.464@retval Others A pointer to the duplicated device path.465466**/467EFI_DEVICE_PATH_PROTOCOL *468EFIAPI469DuplicateDevicePath (470IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath471)472{473UINTN Size;474475//476// Compute the size477//478Size = GetDevicePathSize (DevicePath);479if (Size == 0) {480return NULL;481}482483//484// Allocate space for duplicate device path485//486487return AllocateCopyPool (Size, DevicePath);488}489490/**491Creates a new device path by appending a second device path to a first device path.492493This function creates a new device path by appending a copy of SecondDevicePath494to a copy of FirstDevicePath in a newly allocated buffer. Only the end-of-device-path495device node from SecondDevicePath is retained. The newly created device path is496returned. If FirstDevicePath is NULL, then it is ignored, and a duplicate of497SecondDevicePath is returned. If SecondDevicePath is NULL, then it is ignored,498and a duplicate of FirstDevicePath is returned. If both FirstDevicePath and499SecondDevicePath are NULL, then a copy of an end-of-device-path is returned.500501If there is not enough memory for the newly allocated buffer, then NULL is returned.502The memory for the new device path is allocated from EFI boot services memory.503It is the responsibility of the caller to free the memory allocated.504505@param FirstDevicePath A pointer to a device path data structure.506@param SecondDevicePath A pointer to a device path data structure.507508@retval NULL If there is not enough memory for the newly allocated buffer.509@retval NULL If FirstDevicePath or SecondDevicePath is invalid.510@retval Others A pointer to the new device path if success.511Or a copy an end-of-device-path if both FirstDevicePath and SecondDevicePath are NULL.512513**/514EFI_DEVICE_PATH_PROTOCOL *515EFIAPI516AppendDevicePath (517IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath, OPTIONAL518IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL519)520{521UINTN Size;522UINTN Size1;523UINTN Size2;524EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;525EFI_DEVICE_PATH_PROTOCOL *DevicePath2;526527//528// If there's only 1 path, just duplicate it.529//530if (FirstDevicePath == NULL) {531return DuplicateDevicePath ((SecondDevicePath != NULL) ? SecondDevicePath : &mUefiDevicePathLibEndDevicePath);532}533534if (SecondDevicePath == NULL) {535return DuplicateDevicePath (FirstDevicePath);536}537538if (!IsDevicePathValid (FirstDevicePath, 0) || !IsDevicePathValid (SecondDevicePath, 0)) {539return NULL;540}541542//543// Allocate space for the combined device path. It only has one end node of544// length EFI_DEVICE_PATH_PROTOCOL.545//546Size1 = GetDevicePathSize (FirstDevicePath);547Size2 = GetDevicePathSize (SecondDevicePath);548Size = Size1 + Size2 - END_DEVICE_PATH_LENGTH;549550NewDevicePath = AllocatePool (Size);551552if (NewDevicePath != NULL) {553NewDevicePath = CopyMem (NewDevicePath, FirstDevicePath, Size1);554//555// Over write FirstDevicePath EndNode and do the copy556//557DevicePath2 = (EFI_DEVICE_PATH_PROTOCOL *) ((CHAR8 *) NewDevicePath +558(Size1 - END_DEVICE_PATH_LENGTH));559CopyMem (DevicePath2, SecondDevicePath, Size2);560}561562return NewDevicePath;563}564565/**566Creates a new path by appending the device node to the device path.567568This function creates a new device path by appending a copy of the device node569specified by DevicePathNode to a copy of the device path specified by DevicePath570in an allocated buffer. The end-of-device-path device node is moved after the571end of the appended device node.572If DevicePathNode is NULL then a copy of DevicePath is returned.573If DevicePath is NULL then a copy of DevicePathNode, followed by an end-of-device574path device node is returned.575If both DevicePathNode and DevicePath are NULL then a copy of an end-of-device-path576device node is returned.577If there is not enough memory to allocate space for the new device path, then578NULL is returned.579The memory is allocated from EFI boot services memory. It is the responsibility580of the caller to free the memory allocated.581582@param DevicePath A pointer to a device path data structure.583@param DevicePathNode A pointer to a single device path node.584585@retval NULL If there is not enough memory for the new device path.586@retval Others A pointer to the new device path if success.587A copy of DevicePathNode followed by an end-of-device-path node588if both FirstDevicePath and SecondDevicePath are NULL.589A copy of an end-of-device-path node if both FirstDevicePath590and SecondDevicePath are NULL.591592**/593EFI_DEVICE_PATH_PROTOCOL *594EFIAPI595AppendDevicePathNode (596IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, OPTIONAL597IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode OPTIONAL598)599{600EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;601EFI_DEVICE_PATH_PROTOCOL *NextNode;602EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;603UINTN NodeLength;604605if (DevicePathNode == NULL) {606return DuplicateDevicePath ((DevicePath != NULL) ? DevicePath : &mUefiDevicePathLibEndDevicePath);607}608//609// Build a Node that has a terminator on it610//611NodeLength = DevicePathNodeLength (DevicePathNode);612613TempDevicePath = AllocatePool (NodeLength + END_DEVICE_PATH_LENGTH);614if (TempDevicePath == NULL) {615return NULL;616}617TempDevicePath = CopyMem (TempDevicePath, DevicePathNode, NodeLength);618//619// Add and end device path node to convert Node to device path620//621NextNode = NextDevicePathNode (TempDevicePath);622SetDevicePathEndNode (NextNode);623//624// Append device paths625//626NewDevicePath = AppendDevicePath (DevicePath, TempDevicePath);627628FreePool (TempDevicePath);629630return NewDevicePath;631}632633634