2 # SPDX-License-Identifier: GPL-2.0+
5 Expo utility - used for testing of expo features
7 Copyright 2023 Google LLC
8 Written by Simon Glass <sjg@chromium.org>
18 #from u_boot_pylib import cros_subprocess
19 from u_boot_pylib import tools
24 RE_ENUM = re.compile(r'(\S*)(\s*= (\d))?,')
26 # Parse #define <name> "string"
27 RE_DEF = re.compile(r'#define (\S*)\s*"(.*)"')
30 """Figure out the value of the enums in a C file
33 fname (str): Filename to parse
40 Value of #define, if string
42 vals = collections.OrderedDict()
43 with open(fname, 'r', encoding='utf-8') as inf:
46 for line in inf.readlines():
51 if in_enum and line == '};':
55 if not line or line.startswith('/*'):
57 m_enum = RE_ENUM.match(line)
59 cur_id = int(m_enum.group(3))
60 vals[m_enum.group(1)] = cur_id
63 m_def = RE_DEF.match(line)
65 vals[m_def.group(1)] = tools.to_bytes(m_def.group(2))
71 """Run the expo program"""
72 ids = calc_ids(args.enum_fname)
74 indata = tools.read_file(args.layout)
78 for name, val in ids.items():
79 if isinstance(val, int):
82 outval = b'"%s"' % val
83 find_str = r'\b%s\b' % name
84 indata = re.sub(tools.to_bytes(find_str), outval, indata)
87 data = outf.getvalue()
89 with open('/tmp/asc', 'wb') as outf:
91 proc = subprocess.run('dtc', input=data, capture_output=True, check=True)
96 tools.write_file(args.outfile, edtb)
100 def parse_args(argv):
101 """Parse the command-line arguments
104 argv (list of str): List of string arguments
107 tuple: (options, args) with the command-line options and arugments.
108 options provides access to the options (e.g. option.debug)
109 args is a list of string arguments
111 parser = argparse.ArgumentParser()
112 parser.add_argument('-e', '--enum-fname', type=str,
113 help='C file containing enum declaration for expo items')
114 parser.add_argument('-l', '--layout', type=str,
115 help='Devicetree file source .dts for expo layout')
116 parser.add_argument('-o', '--outfile', type=str,
117 help='Filename to write expo layout dtb')
119 return parser.parse_args(argv)
122 """Start the expo program"""
123 args = parse_args(sys.argv[1:])
125 ret_code = run_expo(args)
129 if __name__ == "__main__":