2 # SPDX-License-Identifier: GPL-2.0+
5 Expo utility - used for testing of expo features
7 Copyright 2023 Google LLC
18 #from u_boot_pylib import cros_subprocess
19 from u_boot_pylib import tools
23 # or SCENE1 = EXPOID_BASE_ID,
25 RE_ENUM = re.compile(r'(\S*)(\s*= ([0-9A-Z_]+))?,')
27 # Parse #define <name> "string"
28 RE_DEF = re.compile(r'#define (\S*)\s*"(.*)"')
30 # Parse EXPOID_BASE_ID = 5,
31 RE_BASE_ID = re.compile(r'\s*EXPOID_BASE_ID\s*= (\d+),')
33 def calc_ids(fname, base_id):
34 """Figure out the value of the enums in a C file
37 fname (str): Filename to parse
38 base_id (int): Base ID (value of EXPOID_BASE_ID)
45 Value of #define, if string
47 vals = collections.OrderedDict()
48 with open(fname, 'r', encoding='utf-8') as inf:
51 for line in inf.readlines():
56 if in_enum and line == '};':
60 if not line or line.startswith('/*'):
62 m_enum = RE_ENUM.match(line)
63 enum_name = m_enum.group(3)
65 if enum_name == 'EXPOID_BASE_ID':
68 cur_id = int(enum_name)
69 vals[m_enum.group(1)] = cur_id
72 m_def = RE_DEF.match(line)
74 vals[m_def.group(1)] = tools.to_bytes(m_def.group(2))
80 fname = 'include/expo.h'
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)
86 base_id = int(m_base_id.group(1))
88 raise ValueError('EXPOID_BASE_ID not found in expo.h')
89 #print(f'EXPOID_BASE_ID={base_id}')
93 """Run the expo program"""
94 base_id = find_base_id()
95 fname = args.enum_fname or args.layout
96 ids = calc_ids(fname, base_id)
98 print(f"Warning: No enum ID values found in file '{fname}'")
100 indata = tools.read_file(args.layout)
104 for name, val in ids.items():
105 if isinstance(val, int):
108 outval = b'"%s"' % val
109 find_str = r'\b%s\b' % name
110 indata = re.sub(tools.to_bytes(find_str), outval, indata)
113 data = outf.getvalue()
115 with open('/tmp/asc', 'wb') as outf:
117 proc = subprocess.run('dtc', input=data, capture_output=True)
120 print(f"Devicetree compiler error:\n{proc.stderr.decode('utf-8')}")
122 tools.write_file(args.outfile, edtb)
126 def parse_args(argv):
127 """Parse the command-line arguments
130 argv (list of str): List of string arguments
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
137 parser = argparse.ArgumentParser()
138 parser.add_argument('-D', '--debug', action='store_true',
139 help='Enable full debug traceback')
140 parser.add_argument('-e', '--enum-fname', type=str,
141 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,
145 help='Filename to write expo layout dtb')
147 return parser.parse_args(argv)
150 """Start the expo program"""
151 args = parse_args(sys.argv[1:])
154 sys.tracebacklimit = 0
156 ret_code = run_expo(args)
160 if __name__ == "__main__":