u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
From: Neha Malcom Francis <n-francis@ti.com>
To: <u-boot@lists.denx.de>
Cc: <n-francis@ti.com>
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	[thread overview]
Message-ID: <20220506043759.8193-3-n-francis@ti.com> (raw)
In-Reply-To: <20220506043759.8193-1-n-francis@ti.com>

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)
+
+    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()
-- 
2.17.1


  parent reply	other threads:[~2022-05-06  4:38 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-06  4:37 [PATCH RFC v2 00/11] Integration of sysfw, tispl and tiboot3 Neha Malcom Francis
2022-05-06  4:37 ` [PATCH RFC v2 01/11] j721e_evm: schema: yaml: Add general schema and J721E board config files Neha Malcom Francis
2022-05-06  4:37 ` Neha Malcom Francis [this message]
2022-05-06  4:37 ` [PATCH RFC v2 03/11] ti: etype: sysfw: Add entry type for sysfw Neha Malcom Francis
2022-05-31  8:44   ` Roger Quadros
2022-06-01  5:58     ` Neha Malcom Francis
2022-06-01  7:29       ` Neha Malcom Francis
2022-06-01  9:26       ` Roger Quadros
2022-05-06  4:37 ` [PATCH RFC v2 04/11] ti: etype: dm: Add entry type for TI DM Neha Malcom Francis
2022-05-06  4:37 ` [PATCH RFC v2 05/11] ti: etype: x509: Add etype for x509 certificate for K3 devices Neha Malcom Francis
2022-05-31  9:20   ` Roger Quadros
2022-06-01  6:02     ` Neha Malcom Francis
2022-06-01  9:24       ` Roger Quadros
2022-06-01  9:48         ` Neha Malcom Francis
2022-06-01 10:48           ` Roger Quadros
2022-06-02  4:09             ` Neha Malcom Francis
2022-05-06  4:37 ` [PATCH RFC v2 06/11] ti: sysfw: Add support for packaging sysfw.itb Neha Malcom Francis
2022-05-06  4:37 ` [PATCH RFC v2 07/11] ti: tiboot3.bin: Remove tiboot3.bin target from makefile Neha Malcom Francis
2022-05-31 10:51   ` Roger Quadros
2022-05-06  4:37 ` [PATCH RFC v2 08/11] ti: tispl.bin: Removed script that packages tispl.bin Neha Malcom Francis
2022-05-31 10:53   ` Roger Quadros
2022-05-06  4:37 ` [PATCH RFC v2 09/11] ti: x509: Remove shell script used for signing Neha Malcom Francis
2022-05-31 10:54   ` Roger Quadros
2022-05-06  4:37 ` [PATCH RFC v2 10/11] ti: dtsi: j721e: Use binman to package sysfw.itb and tiboot3.bin Neha Malcom Francis
2022-05-06  4:37 ` [PATCH RFC v2 11/11] ti: dtsi: j721e: Use binman to package tispl.bin Neha Malcom Francis
2022-05-31 11:02   ` Roger Quadros
2022-06-01  6:08     ` Neha Malcom Francis
2022-06-01  9:23       ` Roger Quadros
2022-06-01 10:42         ` Neha Malcom Francis
2022-06-01 10:55           ` Roger Quadros
2022-06-01 12:47             ` Andrew Davis
2022-06-03  8:49               ` Roger Quadros
2022-05-10 20:05 ` [PATCH RFC v2 00/11] Integration of sysfw, tispl and tiboot3 Tom Rini
2022-05-11 18:56   ` Alper Nebi Yasak
2022-05-31  8:21 ` Roger Quadros
2022-06-01  5:54   ` Neha Malcom Francis

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=20220506043759.8193-3-n-francis@ti.com \
    --to=n-francis@ti.com \
    --cc=u-boot@lists.denx.de \
    /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;
as well as URLs for NNTP newsgroup(s).