From patchwork Fri May 6 04:37:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neha Malcom Francis X-Patchwork-Id: 1627373 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=ti.com header.i=@ti.com header.a=rsa-sha256 header.s=ti-com-17Q1 header.b=fgzsGdS5; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kvd9f1YqDz9sG4 for ; Fri, 6 May 2022 14:38:30 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7F00E8408F; Fri, 6 May 2022 06:38:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=ti.com header.i=@ti.com header.b="fgzsGdS5"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DD207840E7; Fri, 6 May 2022 06:38:14 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from lelv0142.ext.ti.com (lelv0142.ext.ti.com [198.47.23.249]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 27A43806B7 for ; Fri, 6 May 2022 06:38:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=n-francis@ti.com Received: from lelv0265.itg.ti.com ([10.180.67.224]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id 2464c6IF036123 for ; Thu, 5 May 2022 23:38:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1651811886; bh=ZGlwYu5Z9zKRlnYAmS4NM+2qbWw0VHaQcR4f8H3QD+I=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=fgzsGdS5kQDg+y7iDScpGS5DdkBA2ad4X1cYUbGcuT6t7s/oNOo40q2JX2uYM13k4 NSEUlOyMoZ5Y9N64qYlIi+O4wrCekRhTK0vzR2CIhLRcBkYu13eLyqhck2m1YruOfj 1QU5rYtyTeiRPVIuTacGY+HZomFNzKmwLzd9qpMs= Received: from DFLE114.ent.ti.com (dfle114.ent.ti.com [10.64.6.35]) by lelv0265.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 2464c6Uv029023 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 5 May 2022 23:38:06 -0500 Received: from DFLE110.ent.ti.com (10.64.6.31) by DFLE114.ent.ti.com (10.64.6.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.14; Thu, 5 May 2022 23:38:06 -0500 Received: from lelv0326.itg.ti.com (10.180.67.84) by DFLE110.ent.ti.com (10.64.6.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.14 via Frontend Transport; Thu, 5 May 2022 23:38:06 -0500 Received: from ula0497641.india.ti.com (ileax41-snat.itg.ti.com [10.172.224.153]) by lelv0326.itg.ti.com (8.15.2/8.15.2) with ESMTP id 2464bxmL032170; Thu, 5 May 2022 23:38:05 -0500 From: Neha Malcom Francis To: CC: Subject: [PATCH RFC v2 02/11] ti: tools: config: Add board config class to generate config binaries Date: Fri, 6 May 2022 10:07:50 +0530 Message-ID: <20220506043759.8193-3-n-francis@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220506043759.8193-1-n-francis@ti.com> References: <20220506043759.8193-1-n-francis@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean For validating config files and generating binary config artifacts, here board specific config class is added. Add function cfgBinaryGen() in tibcfg_gen.py. It uses TIBoardConfig class to load given schema and config files in YAML, validate them and generate binaries. Signed-off-by: Tarun Sahu [n-francis@ti.com: prepared patch for upstreaming] Signed-off-by: Neha Malcom Francis --- test/py/requirements.txt | 1 + tools/tibcfg_gen.py | 114 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tools/tibcfg_gen.py diff --git a/test/py/requirements.txt b/test/py/requirements.txt index 33c5c0bbc4..a91ba64563 100644 --- a/test/py/requirements.txt +++ b/test/py/requirements.txt @@ -4,6 +4,7 @@ coverage==4.5.4 extras==1.0.0 fixtures==3.0.0 importlib-metadata==0.23 +jsonschema==4.0.0 linecache2==1.0.0 more-itertools==7.2.0 packaging==19.2 diff --git a/tools/tibcfg_gen.py b/tools/tibcfg_gen.py new file mode 100644 index 0000000000..e5fa2690c8 --- /dev/null +++ b/tools/tibcfg_gen.py @@ -0,0 +1,114 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright (C) 2022 Texas Instruments Incorporated - https://www.ti.com/ +# +# TI Board Configuration Class for Schema Validation and Binary Generation +# + +import os +import getopt +import sys + +import yaml + +from jsonschema import validate + + +class TIBoardConfig: + + """ Texas Instruments Board Configuration File""" + + def __init__(self, file, schema, data_rules=""): + """Load a YAML configuration file and YAML schema + + Validation of the config file against the schema is also done.""" + with open(file, 'r') as f: + self.file_yaml = yaml.safe_load(f) + with open(schema, 'r') as sch: + self.schema_yaml = yaml.safe_load(sch) + self.data_rules = data_rules + try: + validate(self.file_yaml, self.schema_yaml) + except Exception as e: + print(e) + + def _convert_to_byte_chunk(self, val, data_type): + """Convert value into byte array""" + size = 0 + if(data_type == "#/definitions/u8"): + size = 1 + elif(data_type == "#/definitions/u16"): + size = 2 + elif(data_type == "#/definitions/u32"): + size = 4 + else: + raise Exception("Data type not present in definitions") + if type(val) == int: + br = val.to_bytes(size, byteorder="little") + return br + + def _compile_yaml(self, schema_yaml, file_yaml): + """Convert YAML file into byte array based on YAML schema""" + br = bytearray() + for key in file_yaml.keys(): + node = file_yaml[key] + node_schema = schema_yaml['properties'][key] + node_type = node_schema.get('type') + if not 'type' in node_schema: + br += self._convert_to_byte_chunk(node, + node_schema.get('$ref')) + elif node_type == 'object': + br += self._compile_yaml(node_schema, node) + elif node_type == 'array': + for item in node: + if not isinstance(item, dict): + br += self._convert_to_byte_chunk( + item, schema_yaml['properties'][key]['items']["$ref"]) + else: + br += self._compile_yaml(node_schema.get('items'), item) + return br + + def generate_binaries(self, out_path=""): + """Generate config binary artifacts from the loaded YAML configuration file""" + if not os.path.isdir(out_path): + os.mkdir(out_path) + for key in self.file_yaml.keys(): + node = self.file_yaml[key] + node_schema = self.schema_yaml['properties'][key] + br = self._compile_yaml(node_schema, node) + path = os.path.join(out_path, key + ".bin") + with open(path, 'wb') as cfg: + cfg.write(br) + + def delete_binaries(self, out_path=""): + """Delete generated binaries""" + if os.path.isdir(out_path): + for key in self.file_yaml.keys(): + path = os.path.join(out_path, key + ".bin") + if os.path.isfile(path): + os.remove(path) + + +def cfgBinaryGen(): + """Generate config binaries from YAML config file and YAML schema + Arguments: + - config_yaml: board config file in YAML + - schema_yaml: schema file in YAML to validate config_yaml against + - output_dir: output directory where generated binaries can be populated + Pass the arguments along with the filename in the Makefile. + """ + opts, args = getopt.getopt(sys.argv[1:], "c:s:o") + for opt, val in opts: + if opt == "-c": + config_yaml = val + elif opt == "-s": + schema_yaml = val + elif opt == "-o": + output_dir = os.path.abspath(val) + try: + tibcfg = TIBoardConfig(config_yaml, schema_yaml) + tibcfg.generate_binaries(output_dir) + except: + raise ValueError("Could not find config files!") + + +cfgBinaryGen()