Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 542CCC05027 for ; Fri, 20 Jan 2023 10:19:41 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E718E85688; Fri, 20 Jan 2023 11:19:32 +0100 (CET) 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="uBDLhww7"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2F7C58533F; Fri, 20 Jan 2023 11:19:23 +0100 (CET) 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 6BCFC8543D for ; Fri, 20 Jan 2023 11:19:18 +0100 (CET) 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 fllv0035.itg.ti.com ([10.64.41.0]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id 30KAJBCY115266; Fri, 20 Jan 2023 04:19:11 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1674209951; bh=li8EJUyfJ4pMCSGxdk+/XxPu7PIsSZIvSSGmeNtW/tg=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=uBDLhww7FlVs0KLxO1I0TywsZ9vUZp73YZRysfNo+IjuYGXIk2vW5PcGB4VXLr3QP ivutpIl/peJN6YrN6TU75ykXgTXJNU0Ghp2vaxW37Fpi95QxCfha3Zj6+LV+faX9I2 aOwZnzqECGibYcwCTZBdrf0qtxzgUk9GFGWg6QjU= Received: from DLEE112.ent.ti.com (dlee112.ent.ti.com [157.170.170.23]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 30KAJBXx007934 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 20 Jan 2023 04:19:11 -0600 Received: from DLEE104.ent.ti.com (157.170.170.34) by DLEE112.ent.ti.com (157.170.170.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.16; Fri, 20 Jan 2023 04:19:10 -0600 Received: from lelv0326.itg.ti.com (10.180.67.84) by DLEE104.ent.ti.com (157.170.170.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.16 via Frontend Transport; Fri, 20 Jan 2023 04:19:10 -0600 Received: from ula0497641.dhcp.ti.com (ileaxei01-snat2.itg.ti.com [10.180.69.6]) by lelv0326.itg.ti.com (8.15.2/8.15.2) with ESMTP id 30KAJ493026221; Fri, 20 Jan 2023 04:19:08 -0600 From: Neha Malcom Francis To: , , , , , CC: , , , Subject: [PATCH 01/21] ti: tools: config: Add board config class to generate config binaries Date: Fri, 20 Jan 2023 15:48:43 +0530 Message-ID: <20230120101903.179959-2-n-francis@ti.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230120101903.179959-1-n-francis@ti.com> References: <20230120101903.179959-1-n-francis@ti.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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.6 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 | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 tools/tibcfg_gen.py diff --git a/test/py/requirements.txt b/test/py/requirements.txt index fae8b59caf..e4951cce21 100644 --- a/test/py/requirements.txt +++ b/test/py/requirements.txt @@ -5,6 +5,7 @@ extras==1.0.0 filelock==3.0.12 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..79a94efbb0 --- /dev/null +++ b/tools/tibcfg_gen.py @@ -0,0 +1,117 @@ +# 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") + output_dir = None + 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) + if output_dir is None: + output_dir = os.getcwd() + try: + tibcfg = TIBoardConfig(config_yaml, schema_yaml) + tibcfg.generate_binaries(output_dir) + except: + raise ValueError("Could not find config files!") + + +cfgBinaryGen() -- 2.34.1