From: Alper Nebi Yasak <alpernebiyasak@gmail.com>
To: Neha Malcom Francis <n-francis@ti.com>
Cc: u-boot@lists.denx.de, afd@ti.com, trini@konsulko.com,
rogerq@kernel.org, a-govindraju@ti.com, vigneshr@ti.com
Subject: Re: [PATCH RFC v3 02/11] ti: tools: config: Add board config class to generate config binaries
Date: Fri, 1 Jul 2022 22:07:20 +0300 [thread overview]
Message-ID: <23350c36-3dc2-da12-c790-d9bd0cbb1818@gmail.com> (raw)
In-Reply-To: <20220615064804.29553-3-n-francis@ti.com>
On 15/06/2022 09:47, Neha Malcom Francis wrote:
> 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 <t-sahu@ti.com>
> [n-francis@ti.com: prepared patch for upstreaming]
> Signed-off-by: Neha Malcom Francis <n-francis@ti.com>
> ---
> 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)
Don't catch the exception here, so that we never have a non-validated
TIBoardConfig object. Instead, catch it in cfgBinaryGen() below to
report the validation error.
> +
> + 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
Parentheses are unnecessary for these as well.
> + 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")
I'd prefer argparse, with both long/short forms of arguments and proper
help messages.
> + 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()
This needs to be
if __name__ == "__main__":
cfgBinaryGen()
or it will be run when anything uses 'import tibcfg_gen' in Python.
next prev parent reply other threads:[~2022-07-01 19:09 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-15 6:47 [PATCH RFC v3 00/11] Integration of tiboot3.bin, sysfw.itb and Neha Malcom Francis
2022-06-15 6:47 ` [PATCH RFC v3 01/11] j721e_evm: schema: yaml: Add general schema and J721E board config files Neha Malcom Francis
2022-06-15 6:47 ` [PATCH RFC v3 02/11] ti: tools: config: Add board config class to generate config binaries Neha Malcom Francis
2022-07-01 19:07 ` Alper Nebi Yasak [this message]
2022-06-15 6:47 ` [PATCH RFC v3 03/11] ti: etype: sysfw: Add entry type for sysfw Neha Malcom Francis
2022-06-15 15:37 ` Andrew Davis
2022-06-16 11:23 ` Neha Malcom Francis
2022-07-01 19:07 ` Alper Nebi Yasak
2022-06-15 6:47 ` [PATCH RFC v3 04/11] ti: etype: dm: Add entry type for TI DM Neha Malcom Francis
2022-07-01 19:07 ` Alper Nebi Yasak
2022-06-15 6:47 ` [PATCH RFC v3 05/11] ti: etype: x509: Add etype for x509 certificate for K3 devices Neha Malcom Francis
2022-07-01 19:07 ` Alper Nebi Yasak
2022-06-15 6:47 ` [PATCH RFC v3 06/11] ti: sysfw: Add support for packaging sysfw.itb Neha Malcom Francis
2022-06-15 6:48 ` [PATCH RFC v3 07/11] ti: j721e: Exclude makefile tiboot3.bin target for J721E Neha Malcom Francis
2022-06-15 6:48 ` [PATCH RFC v3 08/11] ti: j721e: Exclude makefile tispl.bin " Neha Malcom Francis
2022-06-15 13:44 ` Roger Quadros
2022-06-16 11:09 ` Neha Malcom Francis
2022-06-15 6:48 ` [PATCH RFC v3 09/11] ti: dtsi: j721e: Use binman to package sysfw.itb and tiboot3.bin Neha Malcom Francis
2022-07-01 19:07 ` Alper Nebi Yasak
2022-06-15 6:48 ` [PATCH RFC v3 10/11] ti: dtsi: j721e: Use binman to package tispl.bin Neha Malcom Francis
2022-06-15 14:25 ` Roger Quadros
2022-06-15 14:29 ` Roger Quadros
2022-07-01 19:08 ` Alper Nebi Yasak
2022-06-15 6:48 ` [PATCH RFC v3 11/11] ci: world_build: test: Add requirements.txt Neha Malcom Francis
2022-07-01 19:09 ` Alper Nebi Yasak
2022-07-01 19:07 ` [PATCH RFC v3 00/11] Integration of tiboot3.bin, sysfw.itb and Alper Nebi Yasak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=23350c36-3dc2-da12-c790-d9bd0cbb1818@gmail.com \
--to=alpernebiyasak@gmail.com \
--cc=a-govindraju@ti.com \
--cc=afd@ti.com \
--cc=n-francis@ti.com \
--cc=rogerq@kernel.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox