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 75070C433EF for ; Fri, 1 Apr 2022 14:16:18 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AB27E842D2; Fri, 1 Apr 2022 16:16:00 +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="XIKCgLAo"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 457B3842C0; Fri, 1 Apr 2022 16:13:34 +0200 (CEST) Received: from fllv0016.ext.ti.com (fllv0016.ext.ti.com [198.47.19.142]) (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 4BF38842BB for ; Fri, 1 Apr 2022 16:13:23 +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 fllv0035.itg.ti.com ([10.64.41.0]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id 231ED84w080018; Fri, 1 Apr 2022 09:13:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1648822388; bh=KOCULdVsESUQb7Jyx/iRNv72KA6sNOXIPSU1TDkrnfM=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=XIKCgLAoAOyzYLlAktZ+TfVaDRDV2rQaiReydT3VGXUz9TgKmElAVXA+GHbtNzn19 OLX5TmbXKNKavXyizLWf+i1uyidSq3oR8HqiXZXbNSvjcRO0lka5PJez1SzKZKBskg diPlrg7gZodrm5p05qaypcAHGBI6KYJ0P4zVPU/A= Received: from DLEE105.ent.ti.com (dlee105.ent.ti.com [157.170.170.35]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 231ED7Pp015037 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 1 Apr 2022 09:13:07 -0500 Received: from DLEE102.ent.ti.com (157.170.170.32) by DLEE105.ent.ti.com (157.170.170.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.14; Fri, 1 Apr 2022 09:13:07 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) by DLEE102.ent.ti.com (157.170.170.32) 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; Fri, 1 Apr 2022 09:13:07 -0500 Received: from ula0497641.india.ti.com (ileax41-snat.itg.ti.com [10.172.224.153]) by lelv0327.itg.ti.com (8.15.2/8.15.2) with ESMTP id 231ECtKI128991; Fri, 1 Apr 2022 09:13:01 -0500 From: Neha Malcom Francis To: , , CC: , , , , , , , , , , , , , Subject: [uU PATCH 1/8] tools: config: yaml: Add board config class to generate config binaries Date: Fri, 1 Apr 2022 19:42:47 +0530 Message-ID: <20220401141254.27412-2-n-francis@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220401141254.27412-1-n-francis@ti.com> References: <20220401141254.27412-1-n-francis@ti.com> MIME-Version: 1.0 Content-Type: text/plain X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-Mailman-Approved-At: Fri, 01 Apr 2022 16:15:56 +0200 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 | 116 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 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..7635596906 --- /dev/null +++ b/tools/tibcfg_gen.py @@ -0,0 +1,116 @@ +# 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 +# + +from jsonschema import validate + +import yaml +import os +import sys + + +class TIBoardConfig: + file_yaml = {} + schema_yaml = {} + data_rules = {} + + def __init__(self): + pass + + def Load(self, file, schema, data_rules=""): + 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 + + def CheckValidity(self): + try: + validate(self.file_yaml, self.schema_yaml) + return True + except Exception as e: + print(e) + return False + + def __ConvertToByteChunk(self, val, data_type): + br = [] + size = 0 + if(data_type == "#/definitions/u8"): + size = 1 + elif(data_type == "#/definitions/u16"): + size = 2 + elif(data_type == "#/definitions/u32"): + size = 4 + else: + return -1 + if(type(val) == int): + while(val != 0): + br = br + [(val & 0xFF)] + val = val >> 8 + while(len(br) < size): + br = br + [0] + return br + + def __CompileYaml(self, schema_yaml, file_yaml): + br = [] + for key in file_yaml.keys(): + if not 'type' in schema_yaml['properties'][key]: + br = br + \ + self.__ConvertToByteChunk( + file_yaml[key], schema_yaml['properties'][key]["$ref"]) + elif schema_yaml['properties'][key]['type'] == 'object': + br = br + \ + self.__CompileYaml( + schema_yaml['properties'][key], file_yaml[key]) + elif schema_yaml['properties'][key]['type'] == 'array': + for item in file_yaml[key]: + if not isinstance(item, dict): + br = br + \ + self.__ConvertToByteChunk( + item, schema_yaml['properties'][key]['items']["$ref"]) + else: + br = br + \ + self.__CompileYaml( + schema_yaml['properties'][key]['items'], item) + return br + + def GenerateBinaries(self, out_path=""): + if not os.path.isdir(out_path): + os.mkdir(out_path) + if(self.CheckValidity()): + for key in self.file_yaml.keys(): + br = [] + br = self.__CompileYaml( + self.schema_yaml['properties'][key], self.file_yaml[key]) + with open(out_path + "/" + key + ".bin", 'wb') as cfg: + cfg.write(bytearray(br)) + else: + raise ValueError("Config YAML Validation failed!") + + def DeleteBinaries(self, out_path=""): + if os.path.isdir(out_path): + for key in self.file_yaml.keys(): + if os.path.isfile(out_path + "/" + key + ".bin"): + os.remove(out_path + "/" + key + ".bin") + + +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 + Pass the arguments along with the filename in the Makefile. + """ + tibcfg = TIBoardConfig() + config_yaml = sys.argv[1] + schema_yaml = sys.argv[2] + try: + tibcfg.Load(config_yaml, schema_yaml) + except: + raise ValueError("Could not find config files!") + tibcfg.GenerateBinaries(os.environ['O']) + + +cfgBinaryGen() -- 2.17.1