blob: 44995f28a382e1d99904856cc43007d368981881 [file] [log] [blame]
Simon Glassb8c26552023-06-01 10:23:03 -06001#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0+
3
4"""
5Expo utility - used for testing of expo features
6
7Copyright 2023 Google LLC
8Written by Simon Glass <sjg@chromium.org>
9"""
10
11import argparse
12import collections
13import io
14import re
15import subprocess
16import sys
17
18#from u_boot_pylib import cros_subprocess
19from u_boot_pylib import tools
20
21# Parse:
22# SCENE1 = 7,
Simon Glass53a0a2f2024-10-14 16:31:57 -060023# or SCENE1 = EXPOID_BASE_ID,
Simon Glassb8c26552023-06-01 10:23:03 -060024# or SCENE2,
Simon Glass53a0a2f2024-10-14 16:31:57 -060025RE_ENUM = re.compile(r'(\S*)(\s*= ([0-9A-Z_]+))?,')
Simon Glassb8c26552023-06-01 10:23:03 -060026
27# Parse #define <name> "string"
28RE_DEF = re.compile(r'#define (\S*)\s*"(.*)"')
29
Simon Glass53a0a2f2024-10-14 16:31:57 -060030# Parse EXPOID_BASE_ID = 5,
31RE_BASE_ID = re.compile(r'\s*EXPOID_BASE_ID\s*= (\d+),')
32
33def calc_ids(fname, base_id):
Simon Glassb8c26552023-06-01 10:23:03 -060034 """Figure out the value of the enums in a C file
35
36 Args:
37 fname (str): Filename to parse
Simon Glass53a0a2f2024-10-14 16:31:57 -060038 base_id (int): Base ID (value of EXPOID_BASE_ID)
Simon Glassb8c26552023-06-01 10:23:03 -060039
40 Returns:
41 OrderedDict():
42 key (str): enum name
43 value (int or str):
44 Value of enum, if int
45 Value of #define, if string
46 """
47 vals = collections.OrderedDict()
48 with open(fname, 'r', encoding='utf-8') as inf:
49 in_enum = False
50 cur_id = 0
51 for line in inf.readlines():
52 line = line.strip()
53 if line == 'enum {':
54 in_enum = True
55 continue
56 if in_enum and line == '};':
57 in_enum = False
58
59 if in_enum:
60 if not line or line.startswith('/*'):
61 continue
62 m_enum = RE_ENUM.match(line)
Simon Glass53a0a2f2024-10-14 16:31:57 -060063 enum_name = m_enum.group(3)
64 if enum_name:
65 if enum_name == 'EXPOID_BASE_ID':
66 cur_id = base_id
67 else:
68 cur_id = int(enum_name)
Simon Glassb8c26552023-06-01 10:23:03 -060069 vals[m_enum.group(1)] = cur_id
70 cur_id += 1
71 else:
72 m_def = RE_DEF.match(line)
73 if m_def:
74 vals[m_def.group(1)] = tools.to_bytes(m_def.group(2))
75
76 return vals
77
Simon Glass53a0a2f2024-10-14 16:31:57 -060078
79def find_base_id():
80 fname = 'include/expo.h'
81 base_id = None
82 with open(fname, 'r', encoding='utf-8') as inf:
83 for line in inf.readlines():
84 m_base_id = RE_BASE_ID.match(line)
85 if m_base_id:
86 base_id = int(m_base_id.group(1))
87 if base_id is None:
88 raise ValueError('EXPOID_BASE_ID not found in expo.h')
89 #print(f'EXPOID_BASE_ID={base_id}')
90 return base_id
Simon Glassb8c26552023-06-01 10:23:03 -060091
92def run_expo(args):
93 """Run the expo program"""
Simon Glass53a0a2f2024-10-14 16:31:57 -060094 base_id = find_base_id()
Simon Glassb1263bc2023-08-14 16:40:28 -060095 fname = args.enum_fname or args.layout
Simon Glass53a0a2f2024-10-14 16:31:57 -060096 ids = calc_ids(fname, base_id)
Simon Glassb1263bc2023-08-14 16:40:28 -060097 if not ids:
98 print(f"Warning: No enum ID values found in file '{fname}'")
Simon Glassb8c26552023-06-01 10:23:03 -060099
100 indata = tools.read_file(args.layout)
101
102 outf = io.BytesIO()
103
104 for name, val in ids.items():
105 if isinstance(val, int):
106 outval = b'%d' % val
107 else:
108 outval = b'"%s"' % val
109 find_str = r'\b%s\b' % name
110 indata = re.sub(tools.to_bytes(find_str), outval, indata)
111
112 outf.write(indata)
113 data = outf.getvalue()
114
115 with open('/tmp/asc', 'wb') as outf:
116 outf.write(data)
Simon Glassb1263bc2023-08-14 16:40:28 -0600117 proc = subprocess.run('dtc', input=data, capture_output=True)
Simon Glassb8c26552023-06-01 10:23:03 -0600118 edtb = proc.stdout
119 if proc.stderr:
Simon Glassb1263bc2023-08-14 16:40:28 -0600120 print(f"Devicetree compiler error:\n{proc.stderr.decode('utf-8')}")
Simon Glassb8c26552023-06-01 10:23:03 -0600121 return 1
122 tools.write_file(args.outfile, edtb)
123 return 0
124
125
126def parse_args(argv):
127 """Parse the command-line arguments
128
129 Args:
130 argv (list of str): List of string arguments
131
132 Returns:
133 tuple: (options, args) with the command-line options and arugments.
134 options provides access to the options (e.g. option.debug)
135 args is a list of string arguments
136 """
137 parser = argparse.ArgumentParser()
Simon Glassb1263bc2023-08-14 16:40:28 -0600138 parser.add_argument('-D', '--debug', action='store_true',
139 help='Enable full debug traceback')
Simon Glassb8c26552023-06-01 10:23:03 -0600140 parser.add_argument('-e', '--enum-fname', type=str,
Simon Glassb1263bc2023-08-14 16:40:28 -0600141 help='.dts or C file containing enum declaration for expo items')
142 parser.add_argument('-l', '--layout', type=str, required=True,
143 help='Devicetree file source .dts for expo layout (and perhaps enums)')
144 parser.add_argument('-o', '--outfile', type=str, required=True,
Simon Glassb8c26552023-06-01 10:23:03 -0600145 help='Filename to write expo layout dtb')
146
147 return parser.parse_args(argv)
148
149def start_expo():
150 """Start the expo program"""
151 args = parse_args(sys.argv[1:])
152
Simon Glassb1263bc2023-08-14 16:40:28 -0600153 if not args.debug:
154 sys.tracebacklimit = 0
155
Simon Glassb8c26552023-06-01 10:23:03 -0600156 ret_code = run_expo(args)
157 sys.exit(ret_code)
158
159
160if __name__ == "__main__":
161 start_expo()