From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web11.31053.1680195146314081910 for ; Thu, 30 Mar 2023 09:52:27 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=BMwsYw8T; spf=pass (domain: intel.com, ip: 192.55.52.151, mailfrom: benny.lin@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680195147; x=1711731147; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xPFLEhlHDiBTGTf5V9gDWmH2NI4d1k2J6tktFNEROt0=; b=BMwsYw8TK3fHFzIrdntoHcRp6NM9Iz6AwTp9CjvFtl8vxG5xKDtWyENH boBSx2MBrprtmdPtAe2oE7JrNEXE5+TvMGb7fa18MO+xqh9UmyK32BJkD NJjg9vecvFnM0LyxdTo4vwbnt9jJkfuv0vr5g9PUVVC/MAdc+98T9xVMP eRcNeyEQ2aPlDZmEHaq6YadFD647cMDYZT6lM28ASe0CZ33xdT3set8tw TS30GIJ7Pi5+WX1EtL4NGesc8l7kA3YrNXUan9G5HJKIqj0COAvngJDIf 8s2ZoJ5ASNzaSlQvf0c39VazZfRhzcK9ahvrVVfNbl8pi3rHbaRTu/Fi8 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10665"; a="321614944" X-IronPort-AV: E=Sophos;i="5.98,305,1673942400"; d="scan'208";a="321614944" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2023 09:52:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10665"; a="795734899" X-IronPort-AV: E=Sophos;i="5.98,305,1673942400"; d="scan'208";a="795734899" Received: from bennyli1-mobl2.gar.corp.intel.com ([10.252.191.174]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2023 09:52:25 -0700 From: "Benny Lin" To: devel@edk2.groups.io Cc: Benny Lin , Michael D Kinney , Liming Gao , Zhiguang Liu Subject: [PATCH 1/2] MdePkg: Support FDT library. Date: Fri, 31 Mar 2023 00:52:04 +0800 Message-Id: X-Mailer: git-send-email 2.39.1.windows.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4392 Add FDT support in EDK2 by submodule 3rd party libfdt (https://github.com/devicetree-org/pylibfdt/tree/main/libfdt) Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Benny Lin --- .gitmodules | 3 + MdePkg/Include/Library/FdtLib.h | 300 ++++++++++++++++++++++ MdePkg/Library/BaseFdtLib/BaseFdtLib.inf | 62 +++++ MdePkg/Library/BaseFdtLib/BaseFdtLib.uni | 14 + MdePkg/Library/BaseFdtLib/FdtLib.c | 284 ++++++++++++++++++++ MdePkg/Library/BaseFdtLib/LibFdtSupport.h | 102 ++++++++ MdePkg/Library/BaseFdtLib/LibFdtWrapper.c | 138 ++++++++++ MdePkg/Library/BaseFdtLib/libfdt | 1 + MdePkg/Library/BaseFdtLib/limits.h | 10 + MdePkg/Library/BaseFdtLib/stdbool.h | 10 + MdePkg/Library/BaseFdtLib/stddef.h | 10 + MdePkg/Library/BaseFdtLib/stdint.h | 10 + MdePkg/Library/BaseFdtLib/stdlib.h | 10 + MdePkg/Library/BaseFdtLib/string.h | 10 + MdePkg/MdePkg.ci.yaml | 17 +- MdePkg/MdePkg.dec | 4 + MdePkg/MdePkg.dsc | 2 + ReadMe.rst | 1 + 18 files changed, 986 insertions(+), 2 deletions(-) create mode 100644 MdePkg/Include/Library/FdtLib.h create mode 100644 MdePkg/Library/BaseFdtLib/BaseFdtLib.inf create mode 100644 MdePkg/Library/BaseFdtLib/BaseFdtLib.uni create mode 100644 MdePkg/Library/BaseFdtLib/FdtLib.c create mode 100644 MdePkg/Library/BaseFdtLib/LibFdtSupport.h create mode 100644 MdePkg/Library/BaseFdtLib/LibFdtWrapper.c create mode 160000 MdePkg/Library/BaseFdtLib/libfdt create mode 100644 MdePkg/Library/BaseFdtLib/limits.h create mode 100644 MdePkg/Library/BaseFdtLib/stdbool.h create mode 100644 MdePkg/Library/BaseFdtLib/stddef.h create mode 100644 MdePkg/Library/BaseFdtLib/stdint.h create mode 100644 MdePkg/Library/BaseFdtLib/stdlib.h create mode 100644 MdePkg/Library/BaseFdtLib/string.h diff --git a/.gitmodules b/.gitmodules index 8011a88d9d..5da342e90c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -23,3 +23,6 @@ [submodule "UnitTestFrameworkPkg/Library/GoogleTestLib/googletest"] path =3D UnitTestFrameworkPkg/Library/GoogleTestLib/googletest url =3D https://github.com/google/googletest.git +[submodule "MdePkg/Library/BaseFdtLib/libfdt"] + path =3D MdePkg/Library/BaseFdtLib/libfdt + url =3D https://github.com/devicetree-org/pylibfdt.git diff --git a/MdePkg/Include/Library/FdtLib.h b/MdePkg/Include/Library/FdtLi= b.h new file mode 100644 index 0000000000..bcb097b77e --- /dev/null +++ b/MdePkg/Include/Library/FdtLib.h @@ -0,0 +1,300 @@ +/** @file=0D + Flattened Device Tree Library.=0D +=0D + Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef FDT_LIB_H_=0D +#define FDT_LIB_H_=0D +=0D +///=0D +/// Flattened Device Tree definition=0D +///=0D +typedef struct {=0D + UINT32 Magic; /* magic word FDT_MAGIC */=0D + UINT32 TotalSize; /* total size of DT block */=0D + UINT32 OffsetDtStruct; /* offset to structure */=0D + UINT32 OffsetDtStrings; /* offset to strings */=0D + UINT32 OffsetMemRsvmap; /* offset to memory reserve map */=0D + UINT32 Version; /* format version */=0D + UINT32 LastCompVersion; /* last compatible version */=0D +=0D + /* version 2 fields below */=0D + UINT32 BootCpuidPhys; /* Which physical CPU id we're=0D + booting on */=0D + /* version 3 fields below */=0D + UINT32 SizeDtStrings; /* size of the strings block */=0D +=0D + /* version 17 fields below */=0D + UINT32 SizeDtStruct; /* size of the structure block */=0D +} FDT_HEADER;=0D +=0D +typedef struct {=0D + UINT64 Address;=0D + UINT64 Size;=0D +} FDT_RESERVE_ENTRY;=0D +=0D +typedef struct {=0D + UINT32 Tag;=0D + CHAR8 Name[];=0D +} FDT_NODE_HEADER;=0D +=0D +typedef struct {=0D + UINT32 Tag;=0D + UINT32 Length;=0D + UINT32 NameOffset;=0D + CHAR8 Data[];=0D +} FDT_PROPERTY;=0D +=0D +#define FDT_GET_HEADER(Fdt, Field) FDT32_TO_CPU(((CONST FDT_HEADER *)(Fdt= ))->Field)=0D +=0D +#define FDT_MAGIC(Fdt) (FDT_GET_HEADER(Fdt, Magic))=0D +#define FDT_TOTAL_SIZE(Fdt) (FDT_GET_HEADER(Fdt, TotalSize))=0D +#define FDT_OFFSET_DT_STRUCT(Fdt) (FDT_GET_HEADER(Fdt, OffsetDtStruct))= =0D +#define FDT_OFFSET_DT_STRINGS(Fdt) (FDT_GET_HEADER(Fdt, OffsetDtStrings))= =0D +#define FDT_OFFSET_MEM_RSVMAP(Fdt) (FDT_GET_HEADER(Fdt, OffsetMemRsvmap))= =0D +#define FDT_VERSION(Fdt) (FDT_GET_HEADER(Fdt, Version))=0D +#define FDT_LAST_COMP_VERSION(Fdt) (FDT_GET_HEADER(Fdt, LastCompVersion))= =0D +#define FDT_BOOT_CPUID_PHYS(Fdt) (FDT_GET_HEADER(Fdt, BootCpuidPhys))=0D +#define FDT_SIZE_DT_STRINGS(Fdt) (FDT_GET_HEADER(Fdt, SizeDtStrings))=0D +#define FDT_SIZE_DT_STRUCT(Fdt) (FDT_GET_HEADER(Fdt, SizeDtStruct))=0D +=0D +/**=0D + Create a empty Flattened Device Tree.=0D +=0D + @param[in] Buffer The pointer to allocate a pool for FDT blob.=0D + @param[in] BufferSize The BufferSize to the pool size.=0D +=0D + @return Zero for successfully, otherwise failed.=0D +=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +FdtCreateEmptyTree (=0D + IN VOID *Buffer,=0D + IN UINTN BufferSize=0D + );=0D +=0D +/**=0D + Returns a offset of next node from the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous node.=0D + @param[in] Depth The depth to the level of tree hierarchy.=0D +=0D + @return The offset to next node offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNextNode (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset,=0D + IN INT32 *Depth=0D + );=0D +=0D +/**=0D + Returns a offset of first node under the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous node.=0D +=0D + @return The offset to next node offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtFirstSubnode (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset=0D + );=0D +=0D +/**=0D + Returns a offset of next node from the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous node.=0D +=0D + @return The offset to next node offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNextSubnode (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset=0D + );=0D +=0D +/**=0D + Returns a offset of first node which includes the given name.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] ParentOffset The offset to the node which start find under= .=0D + @param[in] Name The name to search the node with the name.=0D + @param[in] NameLength The length of the name to check only.=0D +=0D + @return The offset to node offset with given node name.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtSubnodeOffsetNameLen (=0D + IN CONST VOID *Fdt,=0D + IN INT32 ParentOffset,=0D + IN CONST CHAR8 *Name,=0D + IN INT32 NameLength=0D + );=0D +=0D +/**=0D + Returns a offset of first node which includes the given property name an= d value.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] StartOffset The offset to the starting node to find.=0D + @param[in] PropertyName The property name to search the node includin= g the named property.=0D + @param[in] PropertyValue The property value to check the same property= value.=0D + @param[in] PropertyLength The length of the value in PropertValue.=0D +=0D + @return The offset to node offset with given property.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNodeOffsetByPropValue (=0D + IN CONST VOID *Fdt,=0D + IN INT32 StartOffset,=0D + IN CONST CHAR8 *PropertyName,=0D + IN CONST VOID *PropertyValue,=0D + IN INT32 PropertyLength=0D + );=0D +=0D +/**=0D + Returns a property with the given name from the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] NodeOffset The offset to the given node.=0D + @param[in] Name The name to the property which need be searche= d=0D + @param[in] Length The length to the size of the property found.= =0D +=0D + @return The property to the structure of the found property.=0D +=0D +**/=0D +CONST FDT_PROPERTY *=0D +EFIAPI=0D +FdtGetProperty (=0D + IN CONST VOID *Fdt,=0D + IN INT32 NodeOffset,=0D + IN CONST CHAR8 *Name,=0D + IN INT32 *Length=0D + );=0D +=0D +/**=0D + Returns a offset of first property in the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to the node which need be searched.= =0D +=0D + @return The offset to first property offset in the given node.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtFirstPropertyOffset (=0D + IN CONST VOID *Fdt,=0D + IN INT32 NodeOffset=0D + );=0D +=0D +/**=0D + Returns a offset of next property from the given property.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous property.=0D +=0D + @return The offset to next property offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNextPropertyOffset (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset=0D + );=0D +=0D +/**=0D + Returns a property from the given offset of the property.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to the given offset of the property= .=0D + @param[in] Length The length to the size of the property found.= =0D +=0D + @return The property to the structure of the given property offset.=0D +=0D +**/=0D +CONST FDT_PROPERTY *=0D +EFIAPI=0D +FdtGetPropertyByOffset (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset,=0D + IN INT32 *Length=0D + );=0D +=0D +/**=0D + Returns a string by the given string offset.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] StrOffset The offset to the location in the strings bloc= k of FDT.=0D + @param[in] Length The length to the size of string which need be= retrieved.=0D +=0D + @return The string to the given string offset.=0D +=0D +**/=0D +CONST CHAR8 *=0D +EFIAPI=0D +FdtGetString (=0D + IN CONST VOID *Fdt,=0D + IN INT32 StrOffset,=0D + IN INT32 *Length OPTIONAL=0D + );=0D +=0D +/**=0D + Add a new node to the FDT.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] ParentOffset The offset to the node offset which want to ad= d in.=0D + @param[in] Name The name to name the node.=0D +=0D + @return The offset to the new node.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtAddSubnode (=0D + IN VOID *Fdt,=0D + IN INT32 ParentOffset,=0D + IN CONST CHAR8 *Name=0D + );=0D +=0D +/**=0D + Add or modify a porperty in the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] NodeOffset The offset to the node offset which want to ad= d in.=0D + @param[in] Name The name to name the property.=0D + @param[in] Value The value to the property value.=0D + @param[in] Length The length to the size of the property.=0D +=0D + @return Zero for successfully, otherwise failed.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtSetProp (=0D + IN VOID *Fdt,=0D + IN INT32 NodeOffset,=0D + IN CONST CHAR8 *Name,=0D + IN CONST VOID *Value,=0D + IN UINT32 Length=0D + );=0D +=0D +#endif /* FDT_LIB_H_ */=0D diff --git a/MdePkg/Library/BaseFdtLib/BaseFdtLib.inf b/MdePkg/Library/Base= FdtLib/BaseFdtLib.inf new file mode 100644 index 0000000000..730e568ff6 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/BaseFdtLib.inf @@ -0,0 +1,62 @@ +## @file=0D +# Flattened Device Tree Library.=0D +#=0D +# Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x0001001B=0D + BASE_NAME =3D BaseFdtLib=0D + MODULE_UNI_FILE =3D BaseFdtLib.uni=0D + FILE_GUID =3D C64DCB01-B037-4FF6-9CF3-E8CEC206DE04= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D FdtLib=0D +=0D + DEFINE FDT_LIB_PATH =3D libfdt/libfdt=0D +=0D +#=0D +# VALID_ARCHITECTURES =3D IA32 X64=0D +#=0D +=0D +[Sources]=0D + FdtLib.c=0D + LibFdtWrapper.c=0D + # header Wrapper files=0D + limits.h=0D + stdbool.h=0D + stddef.h=0D + stdint.h=0D + stdlib.h=0D + string.h=0D +=0D + $(FDT_LIB_PATH)/fdt.c=0D + $(FDT_LIB_PATH)/fdt.h=0D + $(FDT_LIB_PATH)/fdt_addresses.c=0D + $(FDT_LIB_PATH)/fdt_check.c=0D + $(FDT_LIB_PATH)/fdt_empty_tree.c=0D + $(FDT_LIB_PATH)/fdt_overlay.c=0D + $(FDT_LIB_PATH)/fdt_ro.c=0D + $(FDT_LIB_PATH)/fdt_rw.c=0D + $(FDT_LIB_PATH)/fdt_strerror.c=0D + $(FDT_LIB_PATH)/fdt_sw.c=0D + $(FDT_LIB_PATH)/fdt_wip.c=0D + $(FDT_LIB_PATH)/libfdt.h=0D + $(FDT_LIB_PATH)/libfdt_env.h=0D + $(FDT_LIB_PATH)/libfdt_internal.h=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + BaseMemoryLib=0D +=0D +[BuildOptions]=0D + MSFT:*_*_IA32_CC_FLAGS =3D /wd4146 /wd4245=0D + MSFT:*_*_X64_CC_FLAGS =3D /wd4146 /wd4244 /wd4245 /wd4267=0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/BaseFdtLib.uni b/MdePkg/Library/Base= FdtLib/BaseFdtLib.uni new file mode 100644 index 0000000000..3f7e45ea6f --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/BaseFdtLib.uni @@ -0,0 +1,14 @@ +// /** @file=0D +// Flattened Device Tree Library.=0D +//=0D +// Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +//=0D +// SPDX-License-Identifier: BSD-2-Clause-Patent=0D +//=0D +// **/=0D +=0D +=0D +#string STR_MODULE_ABSTRACT #language en-US "Instance of FDT L= ibrary"=0D +=0D +#string STR_MODULE_DESCRIPTION #language en-US "This module provi= des FDT Library implementation."=0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/FdtLib.c b/MdePkg/Library/BaseFdtLib= /FdtLib.c new file mode 100644 index 0000000000..ba9a284e58 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/FdtLib.c @@ -0,0 +1,284 @@ +/** @file=0D + Flattened Device Tree Library.=0D +=0D + Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D +/**=0D + Create a empty Flattened Device Tree.=0D +=0D + @param[in] Buffer The pointer to allocate a pool for FDT blob.=0D + @param[in] BufferSize The BufferSize to the pool size.=0D +=0D + @return Zero for successfully, otherwise failed.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtCreateEmptyTree (=0D + IN VOID *Buffer,=0D + IN UINT32 BufferSize=0D + )=0D +{=0D + return fdt_create_empty_tree (Buffer, (int)BufferSize);=0D +}=0D +=0D +/**=0D + Returns a offset of next node from the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous node.=0D + @param[in] Depth The depth to the level of tree hierarchy.=0D +=0D + @return The offset to next node offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNextNode (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset,=0D + IN INT32 *Depth=0D + )=0D +{=0D + return fdt_next_node (Fdt, Offset, Depth);=0D +}=0D +=0D +/**=0D + Returns a offset of first node under the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous node.=0D +=0D + @return The offset to next node offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtFirstSubnode (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset=0D + )=0D +{=0D + return fdt_first_subnode (Fdt, Offset);=0D +}=0D +=0D +/**=0D + Returns a offset of next node from the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous node.=0D +=0D + @return The offset to next node offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNextSubnode (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset=0D + )=0D +{=0D + return fdt_next_subnode (Fdt, Offset);=0D +}=0D +=0D +/**=0D + Returns a offset of first node which includes the given name.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] ParentOffset The offset to the node which start find under= .=0D + @param[in] Name The name to search the node with the name.=0D + @param[in] NameLength The length of the name to check only.=0D +=0D + @return The offset to node offset with given node name.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtSubnodeOffsetNameLen (=0D + IN CONST VOID *Fdt,=0D + IN INT32 ParentOffset,=0D + IN CONST CHAR8 *Name,=0D + IN INT32 NameLength=0D + )=0D +{=0D + return fdt_subnode_offset_namelen (Fdt, ParentOffset, Name, NameLength);= =0D +}=0D +=0D +/**=0D + Returns a offset of first node which includes the given property name an= d value.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] StartOffset The offset to the starting node to find.=0D + @param[in] PropertyName The property name to search the node includin= g the named property.=0D + @param[in] PropertyValue The property value to check the same property= value.=0D + @param[in] PropertyLength The length of the value in PropertValue.=0D +=0D + @return The offset to node offset with given property.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNodeOffsetByPropValue (=0D + IN CONST VOID *Fdt,=0D + IN INT32 StartOffset,=0D + IN CONST CHAR8 *PropertyName,=0D + IN CONST VOID *PropertyValue,=0D + IN INT32 PropertyLength=0D + )=0D +{=0D + return fdt_node_offset_by_prop_value (Fdt, StartOffset, PropertyName, Pr= opertyValue, PropertyLength);=0D +}=0D +=0D +/**=0D + Returns a property with the given name from the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] NodeOffset The offset to the given node.=0D + @param[in] Name The name to the property which need be searche= d=0D + @param[in] Length The length to the size of the property found.= =0D +=0D + @return The property to the structure of the found property.=0D +=0D +**/=0D +CONST struct fdt_property *=0D +EFIAPI=0D +FdtGetProperty (=0D + IN CONST VOID *Fdt,=0D + IN INT32 NodeOffset,=0D + IN CONST CHAR8 *Name,=0D + IN INT32 *Length=0D + )=0D +{=0D + return fdt_get_property (Fdt, NodeOffset, Name, Length);=0D +}=0D +=0D +/**=0D + Returns a offset of first property in the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] NodeOffset The offset to the node which need be searched.= =0D +=0D + @return The offset to first property offset in the given node.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtFirstPropertyOffset (=0D + IN CONST VOID *Fdt,=0D + IN INT32 NodeOffset=0D + )=0D +{=0D + return fdt_first_property_offset (Fdt, NodeOffset);=0D +}=0D +=0D +/**=0D + Returns a offset of next property from the given property.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to previous property.=0D +=0D + @return The offset to next property offset.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtNextPropertyOffset (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset=0D + )=0D +{=0D + return fdt_next_property_offset (Fdt, Offset);=0D +}=0D +=0D +/**=0D + Returns a property from the given offset of the property.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] Offset The offset to the given offset of the property= .=0D + @param[in] Length The length to the size of the property found.= =0D +=0D + @return The property to the structure of the given property offset.=0D +=0D +**/=0D +CONST struct fdt_property *=0D +EFIAPI=0D +FdtGetPropertyByOffset (=0D + IN CONST VOID *Fdt,=0D + IN INT32 Offset,=0D + IN INT32 *Length=0D + )=0D +{=0D + return fdt_get_property_by_offset (Fdt, Offset, Length);=0D +}=0D +=0D +/**=0D + Returns a string by the given string offset.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] StrOffset The offset to the location in the strings bloc= k of FDT.=0D + @param[in] Length The length to the size of string which need be= retrieved.=0D +=0D + @return The string to the given string offset.=0D +=0D +**/=0D +CONST CHAR8 *=0D +EFIAPI=0D +FdtGetString (=0D + IN CONST VOID *Fdt,=0D + IN INT32 StrOffset,=0D + IN INT32 *Length OPTIONAL=0D + )=0D +{=0D + return fdt_get_string (Fdt, StrOffset, Length);=0D +}=0D +=0D +/**=0D + Add a new node to the FDT.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] ParentOffset The offset to the node offset which want to ad= d in.=0D + @param[in] Name The name to name the node.=0D +=0D + @return The offset to the new node.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtAddSubnode (=0D + IN VOID *Fdt,=0D + IN INT32 ParentOffset,=0D + IN CONST CHAR8 *Name=0D + )=0D +{=0D + return fdt_add_subnode (Fdt, ParentOffset, Name);=0D +}=0D +=0D +/**=0D + Add or modify a porperty in the given node.=0D +=0D + @param[in] Fdt The pointer to FDT blob.=0D + @param[in] NodeOffset The offset to the node offset which want to ad= d in.=0D + @param[in] Name The name to name the property.=0D + @param[in] Value The value to the property value.=0D + @param[in] Length The length to the size of the property.=0D +=0D + @return Zero for successfully, otherwise failed.=0D +=0D +**/=0D +INT32=0D +EFIAPI=0D +FdtSetProp (=0D + IN VOID *Fdt,=0D + IN INT32 NodeOffset,=0D + IN CONST CHAR8 *Name,=0D + IN CONST VOID *Value,=0D + IN UINT32 Length=0D + )=0D +{=0D + return fdt_setprop (Fdt, NodeOffset, Name, Value, (int)Length);=0D +}=0D diff --git a/MdePkg/Library/BaseFdtLib/LibFdtSupport.h b/MdePkg/Library/Bas= eFdtLib/LibFdtSupport.h new file mode 100644 index 0000000000..58b0bb403e --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/LibFdtSupport.h @@ -0,0 +1,102 @@ +/** @file=0D + Root include file of C runtime library to support building the third-par= ty=0D + libfdt library.=0D +=0D + Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef FDT_LIB_SUPPORT_H_=0D +#define FDT_LIB_SUPPORT_H_=0D +=0D +#include =0D +#include =0D +#include =0D +=0D +typedef UINT8 uint8_t;=0D +typedef UINT16 uint16_t;=0D +typedef INT32 int32_t;=0D +typedef UINT32 uint32_t;=0D +typedef UINT64 uint64_t;=0D +typedef UINTN uintptr_t;=0D +typedef UINTN size_t;=0D +typedef BOOLEAN bool;=0D +=0D +#define true (1 =3D=3D 1)=0D +#define false (1 =3D=3D 0)=0D +=0D +//=0D +// Definitions for global constants used by libfdt library routines=0D +//=0D +#ifndef INT_MAX=0D +#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */= =0D +#endif=0D +#define INT32_MAX 0x7FFFFFFF /* Maximum (signed) int32 value *= /=0D +#define UINT32_MAX 0xFFFFFFFF /* Maximum unsigned int32 value *= /=0D +#define MAX_STRING_SIZE 0x1000=0D +=0D +//=0D +// Function prototypes of libfdt Library routines=0D +//=0D +void *=0D +memset (=0D + void *,=0D + int,=0D + size_t=0D + );=0D +=0D +int=0D +memcmp (=0D + const void *,=0D + const void *,=0D + size_t=0D + );=0D +=0D +int=0D +strcmp (=0D + const char *,=0D + const char *=0D + );=0D +=0D +char *=0D +strchr (=0D + const char *,=0D + int=0D + );=0D +=0D +char *=0D +strrchr (=0D + const char *,=0D + int=0D + );=0D +=0D +unsigned long=0D +strtoul (=0D + const char *,=0D + char **,=0D + int=0D + );=0D +=0D +char *=0D +strcpy (=0D + char *strDest,=0D + const char *strSource=0D + );=0D +=0D +//=0D +// Macros that directly map functions to BaseLib, BaseMemoryLib, and Debug= Lib functions=0D +//=0D +#define memcpy(dest, source, count) CopyMem(dest,source, (UINTN)(c= ount))=0D +#define memset(dest, ch, count) SetMem(dest, (UINTN)(count),(U= INT8)(ch))=0D +#define memchr(buf, ch, count) ScanMem8(buf, (UINTN)(count),(= UINT8)ch)=0D +#define memcmp(buf1, buf2, count) (int)(CompareMem(buf1, buf2, (= UINTN)(count)))=0D +#define memmove(dest, source, count) CopyMem(dest, source, (UINTN)(= count))=0D +#define strlen(str) (size_t)(AsciiStrLen(str))=0D +#define strnlen(str, count) (size_t)(AsciiStrnLenS(str, co= unt))=0D +#define strncpy(strDest, strSource, count) AsciiStrnCpyS(strDest, MAX_STR= ING_SIZE, strSource, (UINTN)count)=0D +#define strcat(strDest, strSource) AsciiStrCatS(strDest, MAX_STRI= NG_SIZE, strSource)=0D +#define strcmp(string1, string2, count) (int)(AsciiStrCmp(string1, str= ing2))=0D +#define strncmp(string1, string2, count) (int)(AsciiStrnCmp(string1, st= ring2, (UINTN)(count)))=0D +=0D +#endif /* FDT_LIB_SUPPORT_H_ */=0D diff --git a/MdePkg/Library/BaseFdtLib/LibFdtWrapper.c b/MdePkg/Library/Bas= eFdtLib/LibFdtWrapper.c new file mode 100644 index 0000000000..3f1cc69dc6 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/LibFdtWrapper.c @@ -0,0 +1,138 @@ +/** @file=0D + Root include file of C runtime library to support building the third-par= ty=0D + libfdt library.=0D +=0D + Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "LibFdtSupport.h"=0D +#include =0D +=0D +/**=0D + Returns the first occurrence of a Null-terminated ASCII character=0D + in a Null-terminated ASCII string.=0D +=0D + This function scans the contents of the ASCII string specified by s=0D + and returns the first occurrence of c. If c is not found in s,=0D + then NULL is returned. If the length of c is zero, then s is returned.=0D +=0D + @param s The pointer to a Null-terminated ASCII string.=0D + @param c The pointer to a Null-terminated ASCII character= to search for.=0D +=0D + @retval NULL If the c does not appear in s.=0D + @retval others If there is a match return the first occurrence = of c.=0D + If the length of c is zero,return s.=0D +=0D +**/=0D +char *=0D +strchr (=0D + const char *s,=0D + int c=0D + )=0D +{=0D + char pattern[2];=0D +=0D + pattern[0] =3D (CHAR8)c;=0D + pattern[1] =3D 0;=0D + return AsciiStrStr (s, pattern);=0D +}=0D +=0D +/**=0D + Returns the last occurrence of a Null-terminated ASCII character=0D + in a Null-terminated ASCII string.=0D +=0D + This function scans the contents of the ASCII string specified by s=0D + and returns the last occurrence of c. If c is not found in s,=0D + then NULL is returned. If the length of c is zero, then s is returned.=0D +=0D + @param s The pointer to a Null-terminated ASCII string.=0D + @param c The pointer to a Null-terminated ASCII character= to search for.=0D +=0D + @retval NULL If the c does not appear in s.=0D + @retval others If there is a match return the last occurrence o= f c.=0D + If the length of c is zero,return s.=0D +=0D +**/=0D +char *=0D +strrchr (=0D + const char *s,=0D + int c=0D + )=0D +{=0D + char pattern[2];=0D + CONST CHAR8 *LastMatch;=0D + CONST CHAR8 *StringTmp;=0D + CONST CHAR8 *SearchString;=0D +=0D + pattern[0] =3D (CHAR8)c;=0D + pattern[1] =3D 0;=0D + SearchString =3D pattern;=0D +=0D + //=0D + // Return NULL if both strings are less long than PcdMaximumAsciiStringL= ength=0D + //=0D + if ((AsciiStrSize (s) =3D=3D 0) || (AsciiStrSize (SearchString) =3D=3D 0= )) {=0D + return NULL;=0D + }=0D +=0D + if (*SearchString =3D=3D '\0') {=0D + return (CHAR8 *)s;=0D + }=0D +=0D + LastMatch =3D NULL;=0D + do {=0D + StringTmp =3D AsciiStrStr (s, SearchString);=0D +=0D + if (StringTmp =3D=3D NULL) {=0D + break;=0D + }=0D +=0D + LastMatch =3D StringTmp;=0D + s =3D StringTmp + 1;=0D + } while (s !=3D NULL);=0D +=0D + return (CHAR8 *)LastMatch;=0D +}=0D +=0D +/**=0D + Convert a Null-terminated Ascii decimal or hexadecimal string to a value= of type UINTN.=0D +=0D + This function outputs a value of type UINTN by interpreting the contents= of=0D + the Ascii string specified by String as a decimal or hexadecimal number.= =0D +=0D + @param nptr Pointer to a Null-terminated Ascii stri= ng.=0D + @param endptr Pointer to character that stops scan.=0D + @param base Pointer to decimal or hexadecimal.=0D +=0D + @retval MAX_UINTN If nptr is NULL.=0D + If endptr is NULL.=0D + If base is not 10 or 16.=0D + @retval others Value is translated from String.=0D +=0D +**/=0D +unsigned long=0D +strtoul (=0D + const char *nptr,=0D + char **endptr,=0D + int base=0D + )=0D +{=0D + RETURN_STATUS Status;=0D + UINTN ReturnValue;=0D +=0D + if (base =3D=3D 10) {=0D + Status =3D AsciiStrDecimalToUintnS (nptr, endptr, &ReturnValue);=0D + } else if (base =3D=3D 16) {=0D + Status =3D AsciiStrHexToUintnS (nptr, endptr, &ReturnValue);=0D + } else {=0D + Status =3D RETURN_INVALID_PARAMETER;=0D + }=0D +=0D + if (RETURN_ERROR (Status)) {=0D + return MAX_UINTN;=0D + }=0D +=0D + return (unsigned long)ReturnValue;=0D +}=0D diff --git a/MdePkg/Library/BaseFdtLib/libfdt b/MdePkg/Library/BaseFdtLib/l= ibfdt new file mode 160000 index 0000000000..cfff805481 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/libfdt @@ -0,0 +1 @@ +Subproject commit cfff805481bdea27f900c32698171286542b8d3c diff --git a/MdePkg/Library/BaseFdtLib/limits.h b/MdePkg/Library/BaseFdtLib= /limits.h new file mode 100644 index 0000000000..f6cf8d5702 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/limits.h @@ -0,0 +1,10 @@ +/** @file=0D + Include file to support building the third-party libfdt library.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/stdbool.h b/MdePkg/Library/BaseFdtLi= b/stdbool.h new file mode 100644 index 0000000000..f6cf8d5702 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/stdbool.h @@ -0,0 +1,10 @@ +/** @file=0D + Include file to support building the third-party libfdt library.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/stddef.h b/MdePkg/Library/BaseFdtLib= /stddef.h new file mode 100644 index 0000000000..f6cf8d5702 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/stddef.h @@ -0,0 +1,10 @@ +/** @file=0D + Include file to support building the third-party libfdt library.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/stdint.h b/MdePkg/Library/BaseFdtLib= /stdint.h new file mode 100644 index 0000000000..f6cf8d5702 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/stdint.h @@ -0,0 +1,10 @@ +/** @file=0D + Include file to support building the third-party libfdt library.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/stdlib.h b/MdePkg/Library/BaseFdtLib= /stdlib.h new file mode 100644 index 0000000000..f6cf8d5702 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/stdlib.h @@ -0,0 +1,10 @@ +/** @file=0D + Include file to support building the third-party libfdt library.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D diff --git a/MdePkg/Library/BaseFdtLib/string.h b/MdePkg/Library/BaseFdtLib= /string.h new file mode 100644 index 0000000000..f6cf8d5702 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/string.h @@ -0,0 +1,10 @@ +/** @file=0D + Include file to support building the third-party libfdt library.=0D +=0D +Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D diff --git a/MdePkg/MdePkg.ci.yaml b/MdePkg/MdePkg.ci.yaml index 6ba85ebe61..f039daccdb 100644 --- a/MdePkg/MdePkg.ci.yaml +++ b/MdePkg/MdePkg.ci.yaml @@ -2,7 +2,7 @@ # CI configuration for MdePkg=0D #=0D # Copyright (c) Microsoft Corporation=0D -# Copyright (c) 2020, Intel Corporation. All rights reserved.
=0D +# Copyright (c) 2023, Intel Corporation. All rights reserved.
=0D # Copyright (c) 2021, Arm Limited. All rights reserved.
=0D # Copyright (c) 2023, Loongson Technology Corporation Limited. All rights = reserved.
=0D # SPDX-License-Identifier: BSD-2-Clause-Patent=0D @@ -67,7 +67,8 @@ "Include/Library/PcdLib.h",=0D "Include/Library/SafeIntLib.h",=0D "Include/Protocol/DebugSupport.h",=0D - "Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.c"=0D + "Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.c",=0D + "Library/BaseFdtLib"=0D ]=0D },=0D ## options defined ci/Plugin/CompilerPlugin=0D @@ -164,5 +165,17 @@ "ExtendWords": [], # words to extend to the dictionary f= or this package=0D "IgnoreStandardPaths": [], # Standard Plugin defined paths that = should be ignore=0D "AdditionalIncludePaths": [] # Additional paths to spell check (wi= ldcards supported)=0D + },=0D + # options defined in .pytool/Plugin/UncrustifyCheck=0D + "UncrustifyCheck": {=0D + "IgnoreFiles": [=0D + "Library/BaseFdtLib/libfdt",=0D + "Library/BaseFdtLib/limits.h",=0D + "Library/BaseFdtLib/stdbool.h",=0D + "Library/BaseFdtLib/stddef.h",=0D + "Library/BaseFdtLib/stdint.h",=0D + "Library/BaseFdtLib/stdlib.h",=0D + "Library/BaseFdtLib/string.h"=0D + ]=0D }=0D }=0D diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index 5cf04bc0cb..3ce7f28b6e 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -285,6 +285,10 @@ #=0D ArmTrngLib|Include/Library/ArmTrngLib.h=0D =0D + ## @libraryclass Provides APIs for third-party library libfdt.=0D + #=0D + FdtLib|Include/Library/FdtLib.h=0D +=0D [LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]=0D ## @libraryclass Provides services to generate random number.=0D #=0D diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 0ac7618b46..b262068b67 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -136,6 +136,8 @@ MdePkg/Library/CcProbeLibNull/CcProbeLibNull.inf=0D MdePkg/Library/SmmCpuRendezvousLibNull/SmmCpuRendezvousLibNull.inf=0D =0D + MdePkg/Library/BaseFdtLib/BaseFdtLib.inf=0D +=0D [Components.IA32, Components.X64, Components.ARM, Components.AARCH64]=0D #=0D # Add UEFI Target Based Unit Tests=0D diff --git a/ReadMe.rst b/ReadMe.rst index 497d963559..00514c84ff 100644 --- a/ReadMe.rst +++ b/ReadMe.rst @@ -95,6 +95,7 @@ that are covered by additional licenses. - `UnitTestFrameworkPkg/Library/CmockaLib/cmocka `__= =0D - `UnitTestFrameworkPkg/Library/GoogleTestLib/googletest `__=0D - `RedfishPkg/Library/JsonLib/jansson `__=0D +- `MdePkg/Library/BaseFdtLib/libfdt `__=0D =0D The EDK II Project is composed of packages. The maintainers for each packa= ge=0D are listed in `Maintainers.txt `__.=0D --=20 2.39.1.windows.1