From nobody Sun May 19 14:32:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+102891+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+102891+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1681313436; cv=none; d=zohomail.com; s=zohoarc; b=HCnJXhldKaZEawg5szfgX35iIFsvrT2hjvxnxQpR/Ou4wQlCKwbaFhhyQJuwXD5gD3Mxc3bPnjcdOqAwhVfhLh86nvpmWrBwOgkXL/YRuS1BDOUW91i2YymDJAULpUc8a7kKjeJCfiZfCxJ3VycuJ+0N5MgNSQWslWVkX291arQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1681313436; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=mucp3CPoNGnUfqwD2B14nk5H15aMRGr9//w0PTjjgSQ=; b=jK7kYKhc3WSfz0KqrvZHvB2eTUdgGDIYZAXdoKYmQeSj+YVyqTx5L6cSqKqviXRJIYWfMyOnoVYOKKTl+yO655Ytg3mv98FvqTUOnU6dkX+by9D/kQ5y5NXXbYFdGeMsz+eV+WaNdx+qMm2X3QKJlIg9ma9MR+rqMNvUuCYauUo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+102891+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1681313435228202.99503917902325; Wed, 12 Apr 2023 08:30:35 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id HwUaYY1788612xtqh1qwtNcC; Wed, 12 Apr 2023 08:30:34 -0700 X-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 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" X-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" X-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: [edk2-devel] [PATCH v2] MdePkg: Support FDT library. Date: Wed, 12 Apr 2023 23:30:01 +0800 Message-Id: <20230412153001.877-1-benny.lin@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,benny.lin@intel.com X-Gm-Message-State: MnLWIJ9Q3O5ugwTKn2S3jFbPx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1681313434; bh=KRLkC6FX25WYmu4RX4qdNnlrIxAcgLYgEeUroRkfnlE=; h=Cc:Date:From:Reply-To:Subject:To; b=CoVLKbGqqyBXu0WVS7TXIMpBWRKeDd7uFeRkhubH7bf8/wEoGBGn3NGGi4gf8DoOyBL klduJHp/MOGJ6SF87pYr50OzpMg9SJtLpkfuXdpXRjgc6pZ8nYX6g3tFdEBNx00gQmKiV wX+f+btMgKTWz2axXE9FB6+Za62QC649XJ8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1681313436830100001 Content-Type: text/plain; charset="utf-8" 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 ( ); =20 /** - Add or modify a porperty in the given node. + Add or modify a property in the given node. =20 @param[in] Fdt The pointer to FDT blob. @param[in] NodeOffset The offset to the node offset which want to ad= d in. 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 ( } =20 /** - Add or modify a porperty in the given node. + Add or modify a property in the given node. =20 @param[in] Fdt The pointer to FDT blob. @param[in] NodeOffset The offset to the node offset which want to ad= d in. 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; // // Definitions for global constants used by libfdt library routines // -#ifndef INT_MAX -#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */ -#endif +#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */ #define INT32_MAX 0x7FFFFFFF /* Maximum (signed) int32 value */ #define UINT32_MAX 0xFFFFFFFF /* Maximum unsigned int32 value */ -#define MAX_STRING_SIZE 0x1000 +#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */ =20 // // Function prototypes of libfdt Library routines 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 @@ **/ =20 #include "LibFdtSupport.h" -#include =20 -/** - Returns the first occurrence of a Null-terminated ASCII character - in a Null-terminated ASCII string. +char * +strchr ( + const char *Str, + int Char + ) +{ + char *S; =20 - This function scans the contents of the ASCII string specified by s - and returns the first occurrence of c. If c is not found in s, - then NULL is returned. If the length of c is zero, then s is returned. + S =3D (char *)Str; =20 - @param s The pointer to a Null-terminated ASCII string. - @param c The pointer to a Null-terminated ASCII character= to search for. + for ( ; ; S++) { + if (*S =3D=3D Char) { + return S; + } =20 - @retval NULL If the c does not appear in s. - @retval others If there is a match return the first occurrence = of c. - If the length of c is zero,return s. + if (*S =3D=3D '\0') { + return NULL; + } + } +} =20 -**/ char * -strchr ( - const char *s, - int c +strrchr ( + const char *Str, + int Char ) { - char pattern[2]; + char *S, *last; =20 - pattern[0] =3D (CHAR8)c; - pattern[1] =3D 0; - return AsciiStrStr (s, pattern); -} + S =3D (char *)Str; + last =3D NULL; =20 -/** - Returns the last occurrence of a Null-terminated ASCII character - in a Null-terminated ASCII string. - - This function scans the contents of the ASCII string specified by s - and returns the last occurrence of c. If c is not found in s, - then NULL is returned. If the length of c is zero, then s is returned. + for ( ; ; S++) { + if (*S =3D=3D Char) { + last =3D S; + } =20 - @param s The pointer to a Null-terminated ASCII string. - @param c The pointer to a Null-terminated ASCII character= to search for. + if (*S =3D=3D '\0') { + return last; + } + } +} =20 - @retval NULL If the c does not appear in s. - @retval others If there is a match return the last occurrence o= f c. - If the length of c is zero,return s. +STATIC +int +__isspace ( + int ch + ) +{ + // basic ASCII ctype.h:isspace(). Not efficient + return ch =3D=3D '\r' || ch =3D=3D '\n' || ch =3D=3D ' ' || ch =3D=3D '\= t' || ch =3D=3D '\v' || ch =3D=3D '\f'; +} =20 -**/ -char * -strrchr ( - const char *s, - int c +unsigned long +strtoul ( + const char *Nptr, + char **EndPtr, + int Base ) { - char pattern[2]; - CONST CHAR8 *LastMatch; - CONST CHAR8 *StringTmp; - CONST CHAR8 *SearchString; - - pattern[0] =3D (CHAR8)c; - pattern[1] =3D 0; - SearchString =3D pattern; - - // - // Return NULL if both strings are less long than PcdMaximumAsciiStringL= ength - // - if ((AsciiStrSize (s) =3D=3D 0) || (AsciiStrSize (SearchString) =3D=3D 0= )) { - return NULL; + BOOLEAN Negate; + BOOLEAN Overflow; + unsigned long Val; + + Negate =3D FALSE; + Overflow =3D FALSE; + Val =3D 0; + + // Reject bad numeric bases + if ((Base < 0) || (Base =3D=3D 1) || (Base > 36)) { + return 0; } =20 - if (*SearchString =3D=3D '\0') { - return (CHAR8 *)s; + // Skip whitespace + while (__isspace (*Nptr)) { + Nptr++; } =20 - LastMatch =3D NULL; - do { - StringTmp =3D AsciiStrStr (s, SearchString); + // Check for + or - prefixes + if (*Nptr =3D=3D '-') { + Negate =3D TRUE; + Nptr++; + } else if (*Nptr =3D=3D '+') { + Nptr++; + } =20 - if (StringTmp =3D=3D NULL) { - break; + // Consume the start, autodetecting base if needed + if ((Nptr[0] =3D=3D '0') && ((Nptr[1] =3D=3D 'x') || (Nptr[1] =3D=3D 'X'= )) && ((Base =3D=3D 0) || (Base =3D=3D 16))) { + // Hex + Nptr +=3D 2; + Base =3D 16; + } 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))) { + // Binary (standard pending C23) + Nptr +=3D 2; + Base =3D 2; + } else if ((Nptr[0] =3D=3D '0') && ((Base =3D=3D 0) || (Base =3D=3D 8)))= { + // Octal + Nptr++; + Base =3D 8; + } else { + if (Base =3D=3D 0) { + // Assume decimal + Base =3D 10; } + } =20 - LastMatch =3D StringTmp; - s =3D StringTmp + 1; - } while (s !=3D NULL); + while (TRUE) { + int Digit; + char C; + unsigned long NewVal; =20 - return (CHAR8 *)LastMatch; -} + C =3D *Nptr; + Digit =3D -1; + + if ((C >=3D '0') && (C <=3D '9')) { + Digit =3D C - '0'; + } else if ((C >=3D 'a') && (C <=3D 'z')) { + Digit =3D C - 'a' + 10; + } else if ((C >=3D 'A') && (C <=3D 'Z')) { + Digit =3D C - 'A' + 10; + } =20 -/** - Convert a Null-terminated Ascii decimal or hexadecimal string to a value= of type UINTN. + if ((Digit =3D=3D -1) || (Digit >=3D Base)) { + // Note that this case also handles the \0 + if (EndPtr) { + *EndPtr =3D (char *)Nptr; + } =20 - This function outputs a value of type UINTN by interpreting the contents= of - the Ascii string specified by String as a decimal or hexadecimal number. + break; + } =20 - @param nptr Pointer to a Null-terminated Ascii stri= ng. - @param endptr Pointer to character that stops scan. - @param base Pointer to decimal or hexadecimal. + NewVal =3D Val * Base + Digit; =20 - @retval MAX_UINTN If nptr is NULL. - If endptr is NULL. - If base is not 10 or 16. - @retval others Value is translated from String. + if (NewVal < Val) { + // Overflow + Overflow =3D TRUE; + } =20 -**/ -unsigned long -strtoul ( - const char *nptr, - char **endptr, - int base - ) -{ - RETURN_STATUS Status; - UINTN ReturnValue; + Val =3D NewVal; =20 - if (base =3D=3D 10) { - Status =3D AsciiStrDecimalToUintnS (nptr, endptr, &ReturnValue); - } else if (base =3D=3D 16) { - Status =3D AsciiStrHexToUintnS (nptr, endptr, &ReturnValue); - } else { - Status =3D RETURN_INVALID_PARAMETER; + Nptr++; + } + + if (Negate) { + Val =3D -Val; } =20 - if (RETURN_ERROR (Status)) { - return MAX_UINTN; + if (Overflow) { + Val =3D ULONG_MAX; } =20 - return (unsigned long)ReturnValue; + // TODO: We're lacking errno here. + return Val; } --=20 2.39.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#102891): https://edk2.groups.io/g/devel/message/102891 Mute This Topic: https://groups.io/mt/98221692/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-