aboutsummaryrefslogtreecommitdiff
path: root/tools/binman/etype/ti_board_config.py
blob: 2c3bb8f7b56ad40ee5ec474771403e37ddf54a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2022-2023 Texas Instruments Incorporated - https://www.ti.com/
# Written by Neha Malcom Francis <n-francis@ti.com>
#
# Entry-type module for generating schema validated TI board
# configuration binary
#

import os
import struct
import yaml
import yamllint

from collections import OrderedDict
from jsonschema import validate
from shutil import copyfileobj

from binman.entry import Entry
from binman.etype.section import Entry_section
from dtoc import fdt_util
from u_boot_pylib import tools
from yamllint import config

BOARDCFG = 0xB
BOARDCFG_SEC = 0xD
BOARDCFG_PM = 0xE
BOARDCFG_RM = 0xC
BOARDCFG_NUM_ELEMS = 4

class Entry_ti_board_config(Entry_section):
    """An entry containing a TI schema validated board config binary

    This etype supports generation of two kinds of board configuration
    binaries: singular board config binary as well as combined board config
    binary.

    Properties / Entry arguments:
        - config-file: File containing board configuration data in YAML
        - schema-file: File containing board configuration YAML schema against
          which the config file is validated

    Output files:
        - board config binary: File containing board configuration binary

    These above parameters are used only when the generated binary is
    intended to be a single board configuration binary. Example::

        my-ti-board-config {
            ti-board-config {
                config = "board-config.yaml";
                schema = "schema.yaml";
            };
        };

    To generate a combined board configuration binary, we pack the
    needed individual binaries into a ti-board-config binary. In this case,
    the available supported subnode names are board-cfg, pm-cfg, sec-cfg and
    rm-cfg. The final binary is prepended with a header containing details about
    the included board config binaries. Example::

        my-combined-ti-board-config {
            ti-board-config {
                board-cfg {
                    config = "board-cfg.yaml";
                    schema = "schema.yaml";
                };
                sec-cfg {
                    config = "sec-cfg.yaml";
                    schema = "schema.yaml";
                };
            }
        }
    """
    def __init__(self, section, etype, node):
        super().__init__(section, etype, node)
        self._config = None
        self._schema = None
        self._entries = OrderedDict()
        self._num_elems = BOARDCFG_NUM_ELEMS
        self._fmt = '<HHHBB'
        self._index = 0
        self._binary_offset = 0
        self._sw_rev = 1
        self._devgrp = 0

    def ReadNode(self):
        super().ReadNode()
        self._config = fdt_util.GetString(self._node, 'config')
        self._schema = fdt_util.GetString(self._node, 'schema')
        # Depending on whether config file is present in node, we determine
        # whether it is a combined board config binary or not
        if self._config is None:
            self.ReadEntries()
        else:
            self._config_file = tools.get_input_filename(self._config)
            self._schema_file = tools.get_input_filename(self._schema)

    def ReadEntries(self):
        """Read the subnodes to find out what should go in this image
        """
        for node in self._node.subnodes:
            if 'type' not in node.props:
                entry = Entry.Create(self, node, 'ti-board-config')
                entry.ReadNode()
                cfg_data = entry.BuildSectionData(True)
                entry._cfg_data = cfg_data
                self._entries[entry.name] = entry
        self._num_elems = len(self._node.subnodes)

    def _convert_to_byte_chunk(self, val, data_type):
        """Convert value into byte array

        Args:
            val: value to convert into byte array
            data_type: data type used in schema, supported data types are u8,
                u16 and u32

        Returns:
            array of bytes representing value
        """
        size = 0
        if (data_type == '#/definitions/u8'):
            size = 1
        elif (data_type == '#/definitions/u16'):
            size = 2
        else:
            size = 4
        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

        Args:
            schema_yaml: file containing YAML schema
            file_yaml: file containing config to compile

        Returns:
            array of bytes repesenting YAML file against YAML schema
        """
        br = bytearray()
        for key, node in file_yaml.items():
            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):
        """Generate config binary artifacts from the loaded YAML configuration file

        Returns:
            byte array containing config binary artifacts
            or None if generation fails
        """
        cfg_binary = bytearray()
        for key, node in self.file_yaml.items():
            node_schema = self.schema_yaml['properties'][key]
            br = self._compile_yaml(node_schema, node)
            cfg_binary += br
        return cfg_binary

    def _add_boardcfg(self, bcfgtype, bcfgdata):
        """Add board config to combined board config binary

        Args:
            bcfgtype (int): board config type
            bcfgdata (byte array): board config data
        """
        size = len(bcfgdata)
        desc = struct.pack(self._fmt, bcfgtype,
                            self._binary_offset, size, self._devgrp, 0)
        with open(self.descfile, 'ab+') as desc_fh:
            desc_fh.write(desc)
        with open(self.bcfgfile, 'ab+') as bcfg_fh:
            bcfg_fh.write(bcfgdata)
        self._binary_offset += size
        self._index += 1

    def _finalize(self):
        """Generate final combined board config binary

        Returns:
            byte array containing combined board config data
            or None if unable to generate
        """
        with open(self.descfile, 'rb') as desc_fh:
            with open(self.bcfgfile, 'rb') as bcfg_fh:
                with open(self.fh_file, 'ab+') as fh:
                    copyfileobj(desc_fh, fh)
                    copyfileobj(bcfg_fh, fh)
        data = tools.read_file(self.fh_file)
        return data

    def BuildSectionData(self, required):
        if self._config is None:
            self._binary_offset = 0
            uniq = self.GetUniqueName()
            self.fh_file = tools.get_output_filename('fh.%s' % uniq)
            self.descfile = tools.get_output_filename('desc.%s' % uniq)
            self.bcfgfile = tools.get_output_filename('bcfg.%s' % uniq)

            # when binman runs again make sure we start clean
            if os.path.exists(self.fh_file):
                os.remove(self.fh_file)
            if os.path.exists(self.descfile):
                os.remove(self.descfile)
            if os.path.exists(self.bcfgfile):
                os.remove(self.bcfgfile)

            with open(self.fh_file, 'wb') as f:
                t_bytes = f.write(struct.pack(
                    '<BB', self._num_elems, self._sw_rev))
            self._binary_offset += t_bytes
            self._binary_offset += self._num_elems * struct.calcsize(self._fmt)

            if 'board-cfg' in self._entries:
                self._add_boardcfg(BOARDCFG, self._entries['board-cfg']._cfg_data)

            if 'sec-cfg' in self._entries:
                self._add_boardcfg(BOARDCFG_SEC, self._entries['sec-cfg']._cfg_data)

            if 'pm-cfg' in self._entries:
                self._add_boardcfg(BOARDCFG_PM, self._entries['pm-cfg']._cfg_data)

            if 'rm-cfg' in self._entries:
                self._add_boardcfg(BOARDCFG_RM, self._entries['rm-cfg']._cfg_data)

            data = self._finalize()
            return data

        else:
            with open(self._config_file, 'r') as f:
                self.file_yaml = yaml.safe_load(f)
            with open(self._schema_file, 'r') as sch:
                self.schema_yaml = yaml.safe_load(sch)

            yaml_config = config.YamlLintConfig("extends: default")
            for p in yamllint.linter.run(open(self._config_file, "r"), yaml_config):
                self.Raise(f"Yamllint error: {p.line}: {p.rule}")
            try:
                validate(self.file_yaml, self.schema_yaml)
            except Exception as e:
                self.Raise(f"Schema validation error: {e}")

            data = self._generate_binaries()
            return data

    def SetImagePos(self, image_pos):
        Entry.SetImagePos(self, image_pos)

    def CheckEntries(self):
        Entry.CheckEntries(self)