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.229.1681324583433149482 for ; Wed, 12 Apr 2023 11:36:23 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=CLlhLATZ; 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=1681324583; x=1712860583; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dsLxoIMHB43MEZ+/zO7qiphCo3h2l5MYCLV4qeh+JVs=; b=CLlhLATZqG0Sb6h5CALrKEVgYJSzI2lAkH5w9yB15qLR2LjqHBEohNN0 dkl4jQW5IYtYuf0cJMF5/FMERrPCUmicPh+SC3E9P2knrgBFrurf73APC 7Us2ATrqlR+yocH0ziDx0TeL/QQgt/d+4kBQ9oyzLI2yuRHkSLqoRkMde TGnkJEt5olytThbybA7g0pgbg3Xsm67yAB78yoArXjjpw1Nk1kD+wIQVT HVRAmVGXJ0iTm/WAbu3OW+2wpCv/4gqMTg5VtMXEl37bZz0Oi0cE42aou +KgzwaTOQ4rOF5Z+fb9k56OQqd/S/DkhKOJd/QapJ1NlDrWiRofGVGJ7T Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10678"; a="324348875" X-IronPort-AV: E=Sophos;i="5.98,339,1673942400"; d="scan'208";a="324348875" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 11:36:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10678"; a="1018834634" X-IronPort-AV: E=Sophos;i="5.98,339,1673942400"; d="scan'208";a="1018834634" Received: from bennyli1-mobl2.gar.corp.intel.com ([10.252.185.57]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 11:36:21 -0700 From: "Benny Lin" To: devel@edk2.groups.io Cc: Benny Lin , Michael D Kinney , Liming Gao , Zhiguang Liu Subject: [PATCH v2 2/3] MdePkg: Support FDT library. Date: Thu, 13 Apr 2023 02:36:05 +0800 Message-Id: <20230412183606.887-3-benny.lin@intel.com> X-Mailer: git-send-email 2.39.1.windows.1 In-Reply-To: <20230412183606.887-1-benny.lin@intel.com> References: <20230412183606.887-1-benny.lin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Benny Lin 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) 1. Correct the typo. 2. Remove no use definitions from LibFdtSupport.h. 3. Refer to LibcLib implementation by Pedro. Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Benny Lin --- 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 | 100 +++++++ MdePkg/Library/BaseFdtLib/LibFdtWrapper.c | 165 +++++++++++ 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 | 1 + 15 files changed, 1005 insertions(+), 2 deletions(-) diff --git a/MdePkg/Include/Library/FdtLib.h b/MdePkg/Include/Library/FdtLi= b.h new file mode 100644 index 0000000000..d1e67c773f --- /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 property 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..877c832c50 --- /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 property 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..92d7bf0946 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/LibFdtSupport.h @@ -0,0 +1,100 @@ +/** @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 +#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */= =0D +#define INT32_MAX 0x7FFFFFFF /* Maximum (signed) int32 value *= /=0D +#define UINT32_MAX 0xFFFFFFFF /* Maximum unsigned int32 value *= /=0D +#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */= =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..50b533a2b0 --- /dev/null +++ b/MdePkg/Library/BaseFdtLib/LibFdtWrapper.c @@ -0,0 +1,165 @@ +/** @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 +=0D +char *=0D +strchr (=0D + const char *Str,=0D + int Char=0D + )=0D +{=0D + char *S;=0D +=0D + S =3D (char *)Str;=0D +=0D + for ( ; ; S++) {=0D + if (*S =3D=3D Char) {=0D + return S;=0D + }=0D +=0D + if (*S =3D=3D '\0') {=0D + return NULL;=0D + }=0D + }=0D +}=0D +=0D +char *=0D +strrchr (=0D + const char *Str,=0D + int Char=0D + )=0D +{=0D + char *S, *last;=0D +=0D + S =3D (char *)Str;=0D + last =3D NULL;=0D +=0D + for ( ; ; S++) {=0D + if (*S =3D=3D Char) {=0D + last =3D S;=0D + }=0D +=0D + if (*S =3D=3D '\0') {=0D + return last;=0D + }=0D + }=0D +}=0D +=0D +STATIC=0D +int=0D +__isspace (=0D + int ch=0D + )=0D +{=0D + // basic ASCII ctype.h:isspace(). Not efficient=0D + return ch =3D=3D '\r' || ch =3D=3D '\n' || ch =3D=3D ' ' || ch =3D=3D '\= t' || ch =3D=3D '\v' || ch =3D=3D '\f';=0D +}=0D +=0D +unsigned long=0D +strtoul (=0D + const char *Nptr,=0D + char **EndPtr,=0D + int Base=0D + )=0D +{=0D + BOOLEAN Negate;=0D + BOOLEAN Overflow;=0D + unsigned long Val;=0D +=0D + Negate =3D FALSE;=0D + Overflow =3D FALSE;=0D + Val =3D 0;=0D +=0D + // Reject bad numeric bases=0D + if ((Base < 0) || (Base =3D=3D 1) || (Base > 36)) {=0D + return 0;=0D + }=0D +=0D + // Skip whitespace=0D + while (__isspace (*Nptr)) {=0D + Nptr++;=0D + }=0D +=0D + // Check for + or - prefixes=0D + if (*Nptr =3D=3D '-') {=0D + Negate =3D TRUE;=0D + Nptr++;=0D + } else if (*Nptr =3D=3D '+') {=0D + Nptr++;=0D + }=0D +=0D + // Consume the start, autodetecting base if needed=0D + if ((Nptr[0] =3D=3D '0') && ((Nptr[1] =3D=3D 'x') || (Nptr[1] =3D=3D 'X'= )) && ((Base =3D=3D 0) || (Base =3D=3D 16))) {=0D + // Hex=0D + Nptr +=3D 2;=0D + Base =3D 16;=0D + } else if ((Nptr[0] =3D=3D '0') && ((Nptr[1] =3D=3D 'b') || (Nptr[1] =3D= =3D 'B')) && ((Base =3D=3D 0) || (Base =3D=3D 2))) {=0D + // Binary (standard pending C23)=0D + Nptr +=3D 2;=0D + Base =3D 2;=0D + } else if ((Nptr[0] =3D=3D '0') && ((Base =3D=3D 0) || (Base =3D=3D 8)))= {=0D + // Octal=0D + Nptr++;=0D + Base =3D 8;=0D + } else {=0D + if (Base =3D=3D 0) {=0D + // Assume decimal=0D + Base =3D 10;=0D + }=0D + }=0D +=0D + while (TRUE) {=0D + int Digit;=0D + char C;=0D + unsigned long NewVal;=0D +=0D + C =3D *Nptr;=0D + Digit =3D -1;=0D +=0D + if ((C >=3D '0') && (C <=3D '9')) {=0D + Digit =3D C - '0';=0D + } else if ((C >=3D 'a') && (C <=3D 'z')) {=0D + Digit =3D C - 'a' + 10;=0D + } else if ((C >=3D 'A') && (C <=3D 'Z')) {=0D + Digit =3D C - 'A' + 10;=0D + }=0D +=0D + if ((Digit =3D=3D -1) || (Digit >=3D Base)) {=0D + // Note that this case also handles the \0=0D + if (EndPtr) {=0D + *EndPtr =3D (char *)Nptr;=0D + }=0D +=0D + break;=0D + }=0D +=0D + NewVal =3D Val * Base + Digit;=0D +=0D + if (NewVal < Val) {=0D + // Overflow=0D + Overflow =3D TRUE;=0D + }=0D +=0D + Val =3D NewVal;=0D +=0D + Nptr++;=0D + }=0D +=0D + if (Negate) {=0D + Val =3D -Val;=0D + }=0D +=0D + if (Overflow) {=0D + Val =3D ULONG_MAX;=0D + }=0D +=0D + // TODO: We're lacking errno here.=0D + return Val;=0D +}=0D 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 e70182bf7f..9804a7a6bd 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -289,6 +289,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 152c02991a..b38c863812 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -137,6 +137,7 @@ MdePkg/Library/SmmCpuRendezvousLibNull/SmmCpuRendezvousLibNull.inf=0D =0D MdePkg/Library/JedecJep106Lib/JedecJep106Lib.inf=0D + MdePkg/Library/BaseFdtLib/BaseFdtLib.inf=0D =0D [Components.IA32, Components.X64, Components.ARM, Components.AARCH64]=0D #=0D --=20 2.39.1.windows.1