dtoc: Tidy up more Python style in dtb_platdata

Update this file to reduce the number of pylint warnings. Also add a few
missing comments while we are here.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2020-12-03 16:55:16 -07:00
parent 8ee05b5f90
commit 78128d52df

View File

@ -114,8 +114,8 @@ def get_value(ftype, value):
if ftype == fdt.Type.INT: if ftype == fdt.Type.INT:
return '%#x' % fdt_util.fdt32_to_cpu(value) return '%#x' % fdt_util.fdt32_to_cpu(value)
elif ftype == fdt.Type.BYTE: elif ftype == fdt.Type.BYTE:
ch = value[0] char = value[0]
return '%#x' % (ord(ch) if isinstance(ch, str) else ch) return '%#x' % (ord(char) if isinstance(char, str) else char)
elif ftype == fdt.Type.STRING: elif ftype == fdt.Type.STRING:
# Handle evil ACPI backslashes by adding another backslash before them. # Handle evil ACPI backslashes by adding another backslash before them.
# So "\\_SB.GPO0" in the device tree effectively stays like that in C # So "\\_SB.GPO0" in the device tree effectively stays like that in C
@ -163,7 +163,7 @@ class DtbPlatdata(object):
_drivers_additional: List of additional drivers to use during scanning _drivers_additional: List of additional drivers to use during scanning
""" """
def __init__(self, dtb_fname, include_disabled, warning_disabled, def __init__(self, dtb_fname, include_disabled, warning_disabled,
drivers_additional=[]): drivers_additional=None):
self._fdt = None self._fdt = None
self._dtb_fname = dtb_fname self._dtb_fname = dtb_fname
self._valid_nodes = None self._valid_nodes = None
@ -173,7 +173,7 @@ class DtbPlatdata(object):
self._lines = [] self._lines = []
self._drivers = [] self._drivers = []
self._driver_aliases = {} self._driver_aliases = {}
self._drivers_additional = drivers_additional self._drivers_additional = drivers_additional or []
def get_normalized_compat_name(self, node): def get_normalized_compat_name(self, node):
"""Get a node's normalized compat name """Get a node's normalized compat name
@ -312,20 +312,20 @@ class DtbPlatdata(object):
return PhandleInfo(max_args, args) return PhandleInfo(max_args, args)
return None return None
def scan_driver(self, fn): def scan_driver(self, fname):
"""Scan a driver file to build a list of driver names and aliases """Scan a driver file to build a list of driver names and aliases
This procedure will populate self._drivers and self._driver_aliases This procedure will populate self._drivers and self._driver_aliases
Args Args
fn: Driver filename to scan fname: Driver filename to scan
""" """
with open(fn, encoding='utf-8') as fd: with open(fname, encoding='utf-8') as inf:
try: try:
buff = fd.read() buff = inf.read()
except UnicodeDecodeError: except UnicodeDecodeError:
# This seems to happen on older Python versions # This seems to happen on older Python versions
print("Skipping file '%s' due to unicode error" % fn) print("Skipping file '%s' due to unicode error" % fname)
return return
# The following re will search for driver names declared as # The following re will search for driver names declared as
@ -337,8 +337,9 @@ class DtbPlatdata(object):
# The following re will search for driver aliases declared as # The following re will search for driver aliases declared as
# U_BOOT_DRIVER_ALIAS(alias, driver_name) # U_BOOT_DRIVER_ALIAS(alias, driver_name)
driver_aliases = re.findall('U_BOOT_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)', driver_aliases = re.findall(
buff) 'U_BOOT_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)',
buff)
for alias in driver_aliases: # pragma: no cover for alias in driver_aliases: # pragma: no cover
if len(alias) != 2: if len(alias) != 2:
@ -354,19 +355,19 @@ class DtbPlatdata(object):
basedir = sys.argv[0].replace('tools/dtoc/dtoc', '') basedir = sys.argv[0].replace('tools/dtoc/dtoc', '')
if basedir == '': if basedir == '':
basedir = './' basedir = './'
for (dirpath, dirnames, filenames) in os.walk(basedir): for (dirpath, _, filenames) in os.walk(basedir):
for fn in filenames: for fname in filenames:
if not fn.endswith('.c'): if not fname.endswith('.c'):
continue continue
self.scan_driver(dirpath + '/' + fn) self.scan_driver(dirpath + '/' + fname)
for fn in self._drivers_additional: for fname in self._drivers_additional:
if not isinstance(fn, str) or len(fn) == 0: if not isinstance(fname, str) or len(fname) == 0:
continue continue
if fn[0] == '/': if fname[0] == '/':
self.scan_driver(fn) self.scan_driver(fname)
else: else:
self.scan_driver(basedir + '/' + fn) self.scan_driver(basedir + '/' + fname)
def scan_dtb(self): def scan_dtb(self):
"""Scan the device tree to obtain a tree of nodes and properties """Scan the device tree to obtain a tree of nodes and properties
@ -422,15 +423,15 @@ class DtbPlatdata(object):
Number of size cells for this node Number of size cells for this node
""" """
parent = node.parent parent = node.parent
na, ns = 2, 2 num_addr, num_size = 2, 2
if parent: if parent:
na_prop = parent.props.get('#address-cells') addr_prop = parent.props.get('#address-cells')
ns_prop = parent.props.get('#size-cells') size_prop = parent.props.get('#size-cells')
if na_prop: if addr_prop:
na = fdt_util.fdt32_to_cpu(na_prop.value) num_addr = fdt_util.fdt32_to_cpu(addr_prop.value)
if ns_prop: if size_prop:
ns = fdt_util.fdt32_to_cpu(ns_prop.value) num_size = fdt_util.fdt32_to_cpu(size_prop.value)
return na, ns return num_addr, num_size
def scan_reg_sizes(self): def scan_reg_sizes(self):
"""Scan for 64-bit 'reg' properties and update the values """Scan for 64-bit 'reg' properties and update the values
@ -443,8 +444,8 @@ class DtbPlatdata(object):
reg = node.props.get('reg') reg = node.props.get('reg')
if not reg: if not reg:
continue continue
na, ns = self.get_num_cells(node) num_addr, num_size = self.get_num_cells(node)
total = na + ns total = num_addr + num_size
if reg.type != fdt.Type.INT: if reg.type != fdt.Type.INT:
raise ValueError("Node '%s' reg property is not an int" % raise ValueError("Node '%s' reg property is not an int" %
@ -453,10 +454,10 @@ class DtbPlatdata(object):
raise ValueError( raise ValueError(
"Node '%s' reg property has %d cells " "Node '%s' reg property has %d cells "
'which is not a multiple of na + ns = %d + %d)' % 'which is not a multiple of na + ns = %d + %d)' %
(node.name, len(reg.value), na, ns)) (node.name, len(reg.value), num_addr, num_size))
reg.na = na reg.num_addr = num_addr
reg.ns = ns reg.num_size = num_size
if na != 1 or ns != 1: if num_addr != 1 or num_size != 1:
reg.type = fdt.Type.INT64 reg.type = fdt.Type.INT64
i = 0 i = 0
new_value = [] new_value = []
@ -464,10 +465,10 @@ class DtbPlatdata(object):
if not isinstance(val, list): if not isinstance(val, list):
val = [val] val = [val]
while i < len(val): while i < len(val):
addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.na) addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_addr)
i += na i += num_addr
size = fdt_util.fdt_cells_to_cpu(val[i:], reg.ns) size = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_size)
i += ns i += num_size
new_value += [addr, size] new_value += [addr, size]
reg.value = new_value reg.value = new_value
@ -513,14 +514,12 @@ class DtbPlatdata(object):
else: else:
structs[node_name] = fields structs[node_name] = fields
upto = 0
for node in self._valid_nodes: for node in self._valid_nodes:
node_name, _ = self.get_normalized_compat_name(node) node_name, _ = self.get_normalized_compat_name(node)
struct = structs[node_name] struct = structs[node_name]
for name, prop in node.props.items(): for name, prop in node.props.items():
if name not in PROP_IGNORE_LIST and name[0] != '#': if name not in PROP_IGNORE_LIST and name[0] != '#':
prop.Widen(struct[name]) prop.Widen(struct[name])
upto += 1
return structs return structs
@ -613,12 +612,10 @@ class DtbPlatdata(object):
if info: if info:
# Process the list as pairs of (phandle, id) # Process the list as pairs of (phandle, id)
pos = 0 pos = 0
item = 0
for args in info.args: for args in info.args:
phandle_cell = prop.value[pos] phandle_cell = prop.value[pos]
phandle = fdt_util.fdt32_to_cpu(phandle_cell) phandle = fdt_util.fdt32_to_cpu(phandle_cell)
target_node = self._fdt.phandle_to_node[phandle] target_node = self._fdt.phandle_to_node[phandle]
name = conv_name_to_c(target_node.name)
arg_values = [] arg_values = []
for i in range(args): for i in range(args):
arg_values.append( arg_values.append(
@ -626,7 +623,6 @@ class DtbPlatdata(object):
pos += 1 + args pos += 1 + args
vals.append('\t{%d, {%s}}' % (target_node.idx, vals.append('\t{%d, {%s}}' % (target_node.idx,
', '.join(arg_values))) ', '.join(arg_values)))
item += 1
for val in vals: for val in vals:
self.buf('\n\t\t%s,' % val) self.buf('\n\t\t%s,' % val)
else: else:
@ -714,7 +710,7 @@ class DtbPlatdata(object):
self.out(''.join(self.get_buf())) self.out(''.join(self.get_buf()))
def run_steps(args, dtb_file, include_disabled, output, warning_disabled=False, def run_steps(args, dtb_file, include_disabled, output, warning_disabled=False,
drivers_additional=[]): drivers_additional=None):
"""Run all the steps of the dtoc tool """Run all the steps of the dtoc tool
Args: Args:
@ -722,13 +718,18 @@ def run_steps(args, dtb_file, include_disabled, output, warning_disabled=False,
dtb_file (str): Filename of dtb file to process dtb_file (str): Filename of dtb file to process
include_disabled (bool): True to include disabled nodes include_disabled (bool): True to include disabled nodes
output (str): Name of output file output (str): Name of output file
warning_disabled (bool): True to avoid showing warnings about missing
drivers
_drivers_additional (list): List of additional drivers to use during
scanning
Raises: Raises:
ValueError: if args has no command, or an unknown command ValueError: if args has no command, or an unknown command
""" """
if not args: if not args:
raise ValueError('Please specify a command: struct, platdata') raise ValueError('Please specify a command: struct, platdata')
plat = DtbPlatdata(dtb_file, include_disabled, warning_disabled, drivers_additional) plat = DtbPlatdata(dtb_file, include_disabled, warning_disabled,
drivers_additional)
plat.scan_drivers() plat.scan_drivers()
plat.scan_dtb() plat.scan_dtb()
plat.scan_tree() plat.scan_tree()