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.46579.1681313434165433039 for ; Wed, 12 Apr 2023 08:30:34 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=fw8jMqt6; 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=1681313434; x=1712849434; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=cs3EAUr4L+vS+opRkzO/1XD9Uu1EzuejdjAmt/fcqac=; b=fw8jMqt6amYD+J813VCLq+5Jv4KpU2i7UmS/Aj8W2U+TQeyH/XgmTuVS UatmQ/95F1F1tH4ueO9YsuvekFihvNt0JTVUtGkP5ok+uWL5S7WFh571s VLgg6QdMXkY3mVg0D25Uq0VbBbUCKX+47ulbhLr4eVheQ8qq8pMz19eSl 5+sFCJAg/8MDWXlw5XEQz2/Tc6qFCOQpERZhaemS+F1kemyCwN/kuEeGn nXrygs3I7kCEIjELOGEF4sSVByPv+FiX0I6TsF2j++8yGMvPZHMuRb9LL mnttS7jABD8wrMzQuFnh7KI2tzgkQyTrV/QNaFd6P2xr2DiENPyYwToTO A==; X-IronPort-AV: E=McAfee;i="6600,9927,10678"; a="324291621" X-IronPort-AV: E=Sophos;i="5.98,339,1673942400"; d="scan'208";a="324291621" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 08:30:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10678"; a="719413295" X-IronPort-AV: E=Sophos;i="5.98,339,1673942400"; d="scan'208";a="719413295" Received: from bennyli1-mobl2.gar.corp.intel.com ([10.252.185.57]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 08:30:31 -0700 From: "Benny Lin" To: devel@edk2.groups.io Cc: Benny Lin , Michael D Kinney , Liming Gao , Zhiguang Liu , Chasel Chiu , James Lu , Gua Guo , Pedro Falcato Subject: [PATCH v2] MdePkg: Support FDT library. Date: Wed, 12 Apr 2023 23:30:01 +0800 Message-Id: <20230412153001.877-1-benny.lin@intel.com> X-Mailer: git-send-email 2.39.1.windows.1 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 Cc: Chasel Chiu Cc: James Lu Cc: Gua Guo Cc: Pedro Falcato Signed-off-by: Benny Lin --- MdePkg/Include/Library/FdtLib.h | 2 +- MdePkg/Library/BaseFdtLib/FdtLib.c | 2 +- MdePkg/Library/BaseFdtLib/LibFdtSupport.h | 6 +- MdePkg/Library/BaseFdtLib/LibFdtWrapper.c | 215 +++++++++++--------- 4 files changed, 125 insertions(+), 100 deletions(-) diff --git a/MdePkg/Include/Library/FdtLib.h b/MdePkg/Include/Library/FdtLi= b.h index bcb097b77e..d1e67c773f 100644 --- a/MdePkg/Include/Library/FdtLib.h +++ b/MdePkg/Include/Library/FdtLib.h @@ -276,7 +276,7 @@ FdtAddSubnode ( );=0D =0D /**=0D - Add or modify a porperty in the given node.=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 diff --git a/MdePkg/Library/BaseFdtLib/FdtLib.c b/MdePkg/Library/BaseFdtLib= /FdtLib.c index ba9a284e58..877c832c50 100644 --- a/MdePkg/Library/BaseFdtLib/FdtLib.c +++ b/MdePkg/Library/BaseFdtLib/FdtLib.c @@ -259,7 +259,7 @@ FdtAddSubnode ( }=0D =0D /**=0D - Add or modify a porperty in the given node.=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 diff --git a/MdePkg/Library/BaseFdtLib/LibFdtSupport.h b/MdePkg/Library/Bas= eFdtLib/LibFdtSupport.h index 58b0bb403e..92d7bf0946 100644 --- a/MdePkg/Library/BaseFdtLib/LibFdtSupport.h +++ b/MdePkg/Library/BaseFdtLib/LibFdtSupport.h @@ -29,12 +29,10 @@ typedef BOOLEAN bool; //=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 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 MAX_STRING_SIZE 0x1000=0D +#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */= =0D =0D //=0D // Function prototypes of libfdt Library routines=0D diff --git a/MdePkg/Library/BaseFdtLib/LibFdtWrapper.c b/MdePkg/Library/Bas= eFdtLib/LibFdtWrapper.c index 3f1cc69dc6..50b533a2b0 100644 --- a/MdePkg/Library/BaseFdtLib/LibFdtWrapper.c +++ b/MdePkg/Library/BaseFdtLib/LibFdtWrapper.c @@ -8,131 +8,158 @@ **/=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 +char *=0D +strchr (=0D + const char *Str,=0D + int Char=0D + )=0D +{=0D + char *S;=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 + S =3D (char *)Str;=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 + for ( ; ; S++) {=0D + if (*S =3D=3D Char) {=0D + return S;=0D + }=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 + if (*S =3D=3D '\0') {=0D + return NULL;=0D + }=0D + }=0D +}=0D =0D -**/=0D char *=0D -strchr (=0D - const char *s,=0D - int c=0D +strrchr (=0D + const char *Str,=0D + int Char=0D )=0D {=0D - char pattern[2];=0D + char *S, *last;=0D =0D - pattern[0] =3D (CHAR8)c;=0D - pattern[1] =3D 0;=0D - return AsciiStrStr (s, pattern);=0D -}=0D + S =3D (char *)Str;=0D + last =3D NULL;=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 + for ( ; ; S++) {=0D + if (*S =3D=3D Char) {=0D + last =3D S;=0D + }=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 + if (*S =3D=3D '\0') {=0D + return last;=0D + }=0D + }=0D +}=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 +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 -**/=0D -char *=0D -strrchr (=0D - const char *s,=0D - int c=0D +unsigned long=0D +strtoul (=0D + const char *Nptr,=0D + char **EndPtr,=0D + int Base=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 + 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 - if (*SearchString =3D=3D '\0') {=0D - return (CHAR8 *)s;=0D + // Skip whitespace=0D + while (__isspace (*Nptr)) {=0D + Nptr++;=0D }=0D =0D - LastMatch =3D NULL;=0D - do {=0D - StringTmp =3D AsciiStrStr (s, SearchString);=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 - if (StringTmp =3D=3D NULL) {=0D - break;=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 - LastMatch =3D StringTmp;=0D - s =3D StringTmp + 1;=0D - } while (s !=3D NULL);=0D + while (TRUE) {=0D + int Digit;=0D + char C;=0D + unsigned long NewVal;=0D =0D - return (CHAR8 *)LastMatch;=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 -/**=0D - Convert a Null-terminated Ascii decimal or hexadecimal string to a value= of type UINTN.=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 - 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 + break;=0D + }=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 + NewVal =3D Val * Base + Digit;=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 + if (NewVal < Val) {=0D + // Overflow=0D + Overflow =3D TRUE;=0D + }=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 + Val =3D NewVal;=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 + Nptr++;=0D + }=0D +=0D + if (Negate) {=0D + Val =3D -Val;=0D }=0D =0D - if (RETURN_ERROR (Status)) {=0D - return MAX_UINTN;=0D + if (Overflow) {=0D + Val =3D ULONG_MAX;=0D }=0D =0D - return (unsigned long)ReturnValue;=0D + // TODO: We're lacking errno here.=0D + return Val;=0D }=0D --=20 2.39.1.windows.1