From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.120, mailfrom: zhijux.fan@intel.com) Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by groups.io with SMTP; Fri, 21 Jun 2019 02:47:42 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Jun 2019 02:47:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,400,1557212400"; d="scan'208";a="335774139" Received: from fmsmsx108.amr.corp.intel.com ([10.18.124.206]) by orsmga005.jf.intel.com with ESMTP; 21 Jun 2019 02:47:41 -0700 Received: from fmsmsx124.amr.corp.intel.com (10.18.125.39) by FMSMSX108.amr.corp.intel.com (10.18.124.206) with Microsoft SMTP Server (TLS) id 14.3.439.0; Fri, 21 Jun 2019 02:47:41 -0700 Received: from shsmsx153.ccr.corp.intel.com (10.239.6.53) by fmsmsx124.amr.corp.intel.com (10.18.125.39) with Microsoft SMTP Server (TLS) id 14.3.439.0; Fri, 21 Jun 2019 02:47:40 -0700 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.87]) by SHSMSX153.ccr.corp.intel.com ([169.254.12.76]) with mapi id 14.03.0439.000; Fri, 21 Jun 2019 17:47:39 +0800 From: "Fan, ZhijuX" To: "devel@edk2.groups.io" , "leif.lindholm@linaro.org" CC: "Gao, Liming" , "Feng, Bob C" , Ard Biesheuvel , "Kinney, Michael D" Subject: Re: [edk2-devel] [edk2-platform patch 1/2] Platform/Intel:Add GenBiosId into edk2-platforms/Platform/Intel/Tools Thread-Topic: [edk2-devel] [edk2-platform patch 1/2] Platform/Intel:Add GenBiosId into edk2-platforms/Platform/Intel/Tools Thread-Index: AdUn1MqHf8imMAQ4TZu3CECNkVVq/v//7/MA//9wLYA= Date: Fri, 21 Jun 2019 09:47:37 +0000 Message-ID: References: <20190621090050.px3t4a7cdldorpnf@bivouac.eciton.net> In-Reply-To: <20190621090050.px3t4a7cdldorpnf@bivouac.eciton.net> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-version: 11.0.600.7 dlp-reaction: no-action x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Return-Path: zhijux.fan@intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi: Thank you for your comments. The output 1 was designed as a standard error= . Function EdkLogger () sys.exit(1) "1" missing while writing the scr= ipt Any question, please let me know. Thanks. Best Regards Fan Zhiju > -----Original Message----- > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of > Leif Lindholm > Sent: Friday, June 21, 2019 5:01 PM > To: Fan, ZhijuX > Cc: devel@edk2.groups.io; Gao, Liming ; Feng, Bob = C > ; Ard Biesheuvel ; > Kinney, Michael D > Subject: Re: [edk2-devel] [edk2-platform patch 1/2] Platform/Intel:Add > GenBiosId into edk2-platforms/Platform/Intel/Tools >=20 > On Fri, Jun 21, 2019 at 01:58:17AM +0000, Fan, ZhijuX wrote: > > BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=3D1855 > > > > GenBiosId is a tool to generate the BIOS ID binary file which uses the > > data from the configuration file. > > This tool can be run in both Py2 and Py3. > > > > This patch is going to add GenBiosId to Platform/Intel/Tools > > > > Cc: Liming Gao > > Cc: Bob Feng > > Cc: Ard Biesheuvel > > Cc: Leif Lindholm > > Cc: Michael D Kinney > > Signed-off-by: Zhiju.Fan > > --- > > Platform/Intel/Tools/GenBiosId/BiosId.env | 27 +++++ > > Platform/Intel/Tools/GenBiosId/GenBiosId.py | 180 > > ++++++++++++++++++++++++++++ > > 2 files changed, 207 insertions(+) > > create mode 100644 Platform/Intel/Tools/GenBiosId/BiosId.env > > create mode 100644 Platform/Intel/Tools/GenBiosId/GenBiosId.py > > > > diff --git a/Platform/Intel/Tools/GenBiosId/BiosId.env > > b/Platform/Intel/Tools/GenBiosId/BiosId.env > > new file mode 100644 > > index 0000000000..e1e913da76 > > --- /dev/null > > +++ b/Platform/Intel/Tools/GenBiosId/BiosId.env > > @@ -0,0 +1,27 @@ > > +## @file > > +# This file is used to define the BIOS ID parameters of the build. > > +# This file is processed by GenBiosId. > > +# Here, it is just a template and can be customized by user. > > +# > > +# BIOS ID string format: > > +# > $(BOARD_ID)$(BOARD_REV).$(BOARD_EXT).$(VERSION_MAJOR).$(BUILD_T > YPE)$(VERSION_MINOR).YYMMDDHHMM > > +# All fields must have a fixed length. YYMMDDHHMM is UTC time. > > +# Example: "EMLATOR1.000.0001.D01.1906141517" > > +# > > +# If DATE is specified for YYMMDD and TIME is specified for HHMM > > +like below, # GenBiosId will use the value of DATE and TIME to fill > > +YYMMDDHHMM, # otherwise GenBiosId will fill YYMMDDHHMM with > current UTC time of the build machine. > > +# DATE =3D 190614 > > +# TIME =3D 1517 > > +# > > +# Copyright (c) 2019, Intel Corporation. All rights reserved.
# > > +SPDX-License-Identifier: BSD-2-Clause-Patent # ## [config] > > +BOARD_ID =3D KBLRVP3 > > +BOARD_REV =3D 1 > > +BOARD_EXT =3D 000 > > +BUILD_TYPE =3D D > > +VERSION_MAJOR =3D 0001 > > +VERSION_MINOR =3D 01 > > diff --git a/Platform/Intel/Tools/GenBiosId/GenBiosId.py > > b/Platform/Intel/Tools/GenBiosId/GenBiosId.py > > new file mode 100644 > > index 0000000000..20fb7592b4 > > --- /dev/null > > +++ b/Platform/Intel/Tools/GenBiosId/GenBiosId.py > > @@ -0,0 +1,180 @@ > > +## @file > > +# Trim files preprocessed by compiler # # Copyright (c) 2019, Intel > > +Corporation. All rights reserved.
# SPDX-License-Identifier: > > +BSD-2-Clause-Patent # > > + > > +## > > +# Import Modules > > +# > > +import os > > +import sys > > +import time > > +import logging > > +import struct > > +import datetime > > +import argparse > > +import platform > > + > > +try: > > + from configparser import ConfigParser > > +except: > > + from ConfigParser import ConfigParser > > + > > +# Config message > > +_BIOS_Signature =3D "$IBIOSI$" > > +_SectionKeyName =3D '__name__' > > +_SectionName =3D 'config' > > + > > +_ConfigItem =3D { > > + "BOARD_ID": {'Value': '', 'Length': 7}, > > + "BOARD_REV": {'Value': '', 'Length': 1}, > > + "BOARD_EXT": {'Value': '', 'Length': 3}, > > + "BUILD_TYPE": {'Value': '', 'Length': 1}, > > + "VERSION_MAJOR": {'Value': '0000', 'Length': 4}, > > + "VERSION_MINOR": {'Value': '00', 'Length': 2}, } > > + > > +# Version message > > +__prog__ =3D 'GenBiosld' > > +__description__ =3D 'Trim files preprocessed by compiler' > > +__copyright__ =3D 'Copyright (c) 2019, Intel Corporation. All rights > reserved.
' > > +__version__ =3D '%s Version %s' % (__prog__, '0.1 ') > > + > > +# ExtraData message > > +_Usage =3D "Usage: GenBiosId -i Configfile -o OutputFile [-ot > OutputTextFile]" > > +_ConfigSectionNotDefine =3D "Not support the config file format, need > config section" > > +_ErrorMessageTemplate =3D '\n\n%(tool)s...\n : > error: %(msg)s\n\t%(extra)s' > > +_ErrorLogger =3D logging.getLogger("tool_error") _ErrorFormatter =3D > > +logging.Formatter("%(message)s") _ConfigLenInvalid =3D "Config item %= s > > +length is invalid" > > +_ConfigItemInvalid =3D "Item %s is invalid" > > + > > +# Error message > > +INFO =3D 20 > > +ERRORCODE =3D 50 > > +OPTION_MISSING =3D 'Missing option' > > +FORMAT_INVALID =3D 'Invalid syntax/format' > > +FILE_NOT_FOUND =3D 'File/directory not found in workspace' > > +FORMAT_UNKNOWN_ERROR =3D 'Unknown error in syntax/format' > > +FORMAT_NOT_SUPPORTED =3D 'Not supported syntax/format' > > + > > + > > +def SetEdkLogger(): > > + _ErrorLogger.setLevel(INFO) > > + _ErrorCh =3D logging.StreamHandler(sys.stderr) > > + _ErrorCh.setFormatter(_ErrorFormatter) > > + _ErrorLogger.addHandler(_ErrorCh) > > + return _ErrorLogger > > + > > + > > +# Output the error message and exit the tool def EdkLogger(ToolName, > > +Message, ExtraData): > > + _ErrorLogger =3D SetEdkLogger() > > + TemplateDict =3D {"tool": ToolName, "msg": Message, "extra": Extr= aData} > > + LogText =3D _ErrorMessageTemplate % TemplateDict > > + _ErrorLogger.log(ERRORCODE, LogText) > > + sys.exit() >=20 > Should the above two functions be in a common module in edk2? >=20 > > + > > + > > +# Open the file in the correct way > > +def FileOpen(FileName, Mode, Buffer=3D-1): > > + def LongFilePath(FileName): > > + FileName =3D os.path.normpath(FileName) > > + if platform.system() =3D=3D 'Windows': > > + if FileName.startswith('\\\\?\\'): > > + return FileName > > + if FileName.startswith('\\\\'): > > + return '\\\\?\\UNC\\' + FileName[2:] > > + if os.path.isabs(FileName): > > + return '\\\\?\\' + FileName > > + return FileName > > + > > + return open(LongFilePath(FileName), Mode, Buffer) > > + > > + > > +# Parse command line options > > +def MyOptionParser(): > > + parser =3D argparse.ArgumentParser(prog=3D__prog__, > > + description=3D__description__ + = __copyright__ + _Usage, > > + conflict_handler=3D'resolve') > > + parser.add_argument('-v', '--version', action=3D'version', > version=3D__version__, > > + help=3D"show program's version number and exi= t") > > + parser.add_argument('-i', '--int', metavar=3D'FILENAME', dest=3D'= InputFile', > help=3D"Input Config file") > > + parser.add_argument('-o', '--out', metavar=3D'FILENAME', > dest=3D'OutputFile', help=3D"Output file") > > + parser.add_argument('-ot', '--text', metavar=3D'FILENAME', > > +dest=3D'OutputTextFile', help=3D"Output Text file") >=20 > '-ot' is long for a short argument. Could it just be '-t', which also ma= tches '-- > text' better? >=20 > > + Options =3D parser.parse_args() > > + return Options > > + > > + > > +# Check the Tool for missing variables def CheckOptions(Options): > > + if len(sys.argv) !=3D 5 and not (len(sys.argv) =3D=3D 7 and > Options.OutputTextFile): > > + EdkLogger("GenBiosId", OPTION_MISSING, ExtraData=3D_Usage) > > + elif not Options.InputFile or not Options.OutputFile: > > + EdkLogger("GenBiosId", OPTION_MISSING, ExtraData=3D_Usage) >=20 > Can we use argparse to validate the arguments instead - that's what it's= for? >=20 > > + InputFile =3D Options.InputFile > > + OutputFile =3D Options.OutputFile > > + OutputTextFile =3D Options.OutputTextFile > > + if not os.path.exists(InputFile): > > + EdkLogger("GenBiosId", FILE_NOT_FOUND, ExtraData=3D"Input fil= e > > + not found") >=20 > https://stackoverflow.com/questions/37471636/python-argument-parsing- > validation-best-practices > gives an example on how to make argparse verify that files exist. >=20 > > + return InputFile, OutputFile, OutputTextFile > > + > > + > > +# Parse the input file and extract the information def > > +ParserInputFile(InputFile): > > + cf =3D ConfigParser() > > + cf.optionxform =3D str > > + cf.read(InputFile) > > + if _SectionName not in cf._sections: > > + EdkLogger("GenBiosId", FORMAT_NOT_SUPPORTED, > ExtraData=3D_ConfigSectionNotDefine) > > + for Item in cf._sections[_SectionName]: > > + if Item =3D=3D _SectionKeyName: > > + continue > > + if Item not in _ConfigItem: > > + EdkLogger("GenBiosId", FORMAT_INVALID, > ExtraData=3D_ConfigItemInvalid % Item) > > + _ConfigItem[Item]['Value'] =3D cf._sections[_SectionName][Ite= m] > > + if len(_ConfigItem[Item]['Value']) !=3D _ConfigItem[Item]['Le= ngth']: > > + EdkLogger("GenBiosId", FORMAT_INVALID, > ExtraData=3D_ConfigLenInvalid % Item) > > + for Item in _ConfigItem: > > + if not _ConfigItem[Item]['Value']: > > + EdkLogger("GenBiosId", FORMAT_UNKNOWN_ERROR, > ExtraData=3D"Item %s is missing" % Item) > > + utcnow =3D datetime.datetime.utcnow() > > + TimeStamp =3D time.strftime("%y%m%d%H%M", utcnow.timetuple()) > > + > > + Id_Str =3D _ConfigItem['BOARD_ID']['Value'] + > _ConfigItem['BOARD_REV']['Value'] + '.' + _ConfigItem['BOARD_EXT'][ > > + 'Value'] + '.' + _ConfigItem['VERSION_MAJOR']['Value'] + \ > > + '.' + _ConfigItem["BUILD_TYPE"]['Value'] + > _ConfigItem['VERSION_MINOR']['Value'] + '.' + TimeStamp > > + return Id_Str > > + > > + > > +# Output information to a file > > +def PrintOutputFile(OutputFile, OutputTextFile, Id_Str): > > + with FileOpen(OutputFile, 'wb') as FdOut: > > + for i in _BIOS_Signature: > > + FdOut.write(struct.pack('B', ord(i))) > > + > > + for i in Id_Str: > > + FdOut.write(struct.pack('H', ord(i))) > > + > > + FdOut.write(struct.pack('H', 0x00)) > > + if OutputTextFile: > > + with FileOpen(OutputTextFile, 'w') as FdOut: > > + FdOut.write(Id_Str) > > + > > + > > +# Tool entrance method > > +def Main(): > > + Options =3D MyOptionParser() > > + InputFile, OutputFile, OutputTextFile =3D CheckOptions(Options) > > + Id_Str =3D ParserInputFile(InputFile) > > + PrintOutputFile(OutputFile, OutputTextFile, Id_Str) > > + return 0 > > + > > + > > +if __name__ =3D=3D '__main__': > > + r =3D Main() > > + ## 0-127 is a safe return range, and 1 is a standard default erro= r > > + if r < 0 or r > 127: r =3D 1 >=20 > What is the point of this dance? Main always returns 0 (which does not s= ound > ideal). >=20 > > + sys.exit(r) >=20 > Implications for asynchronous i/o? >=20 > / > Leif >=20 > > -- > > 2.14.1.windows.1 > > >=20 >=20 >=20 >=20