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:
return '%#x' % fdt_util.fdt32_to_cpu(value)
elif ftype == fdt.Type.BYTE:
ch = value[0]
return '%#x' % (ord(ch) if isinstance(ch, str) else ch)
char = value[0]
return '%#x' % (ord(char) if isinstance(char, str) else char)
elif ftype == fdt.Type.STRING:
# Handle evil ACPI backslashes by adding another backslash before them.
# 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
"""
def __init__(self, dtb_fname, include_disabled, warning_disabled,
drivers_additional=[]):
drivers_additional=None):
self._fdt = None
self._dtb_fname = dtb_fname
self._valid_nodes = None
@ -173,7 +173,7 @@ class DtbPlatdata(object):
self._lines = []
self._drivers = []
self._driver_aliases = {}
self._drivers_additional = drivers_additional
self._drivers_additional = drivers_additional or []
def get_normalized_compat_name(self, node):
"""Get a node's normalized compat name
@ -312,20 +312,20 @@ class DtbPlatdata(object):
return PhandleInfo(max_args, args)
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
This procedure will populate self._drivers and self._driver_aliases
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:
buff = fd.read()
buff = inf.read()
except UnicodeDecodeError:
# 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
# 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
# U_BOOT_DRIVER_ALIAS(alias, driver_name)
driver_aliases = re.findall('U_BOOT_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)',
buff)
driver_aliases = re.findall(
'U_BOOT_DRIVER_ALIAS\(\s*(\w+)\s*,\s*(\w+)\s*\)',
buff)
for alias in driver_aliases: # pragma: no cover
if len(alias) != 2:
@ -354,19 +355,19 @@ class DtbPlatdata(object):
basedir = sys.argv[0].replace('tools/dtoc/dtoc', '')
if basedir == '':
basedir = './'
for (dirpath, dirnames, filenames) in os.walk(basedir):
for fn in filenames:
if not fn.endswith('.c'):
for (dirpath, _, filenames) in os.walk(basedir):
for fname in filenames:
if not fname.endswith('.c'):
continue
self.scan_driver(dirpath + '/' + fn)
self.scan_driver(dirpath + '/' + fname)
for fn in self._drivers_additional:
if not isinstance(fn, str) or len(fn) == 0:
for fname in self._drivers_additional:
if not isinstance(fname, str) or len(fname) == 0:
continue
if fn[0] == '/':
self.scan_driver(fn)
if fname[0] == '/':
self.scan_driver(fname)
else:
self.scan_driver(basedir + '/' + fn)
self.scan_driver(basedir + '/' + fname)
def scan_dtb(self):
"""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
"""
parent = node.parent
na, ns = 2, 2
num_addr, num_size = 2, 2
if parent:
na_prop = parent.props.get('#address-cells')
ns_prop = parent.props.get('#size-cells')
if na_prop:
na = fdt_util.fdt32_to_cpu(na_prop.value)
if ns_prop:
ns = fdt_util.fdt32_to_cpu(ns_prop.value)
return na, ns
addr_prop = parent.props.get('#address-cells')
size_prop = parent.props.get('#size-cells')
if addr_prop:
num_addr = fdt_util.fdt32_to_cpu(addr_prop.value)
if size_prop:
num_size = fdt_util.fdt32_to_cpu(size_prop.value)
return num_addr, num_size
def scan_reg_sizes(self):
"""Scan for 64-bit 'reg' properties and update the values
@ -443,8 +444,8 @@ class DtbPlatdata(object):
reg = node.props.get('reg')
if not reg:
continue
na, ns = self.get_num_cells(node)
total = na + ns
num_addr, num_size = self.get_num_cells(node)
total = num_addr + num_size
if reg.type != fdt.Type.INT:
raise ValueError("Node '%s' reg property is not an int" %
@ -453,10 +454,10 @@ class DtbPlatdata(object):
raise ValueError(
"Node '%s' reg property has %d cells "
'which is not a multiple of na + ns = %d + %d)' %
(node.name, len(reg.value), na, ns))
reg.na = na
reg.ns = ns
if na != 1 or ns != 1:
(node.name, len(reg.value), num_addr, num_size))
reg.num_addr = num_addr
reg.num_size = num_size
if num_addr != 1 or num_size != 1:
reg.type = fdt.Type.INT64
i = 0
new_value = []
@ -464,10 +465,10 @@ class DtbPlatdata(object):
if not isinstance(val, list):
val = [val]
while i < len(val):
addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.na)
i += na
size = fdt_util.fdt_cells_to_cpu(val[i:], reg.ns)
i += ns
addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_addr)
i += num_addr
size = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_size)
i += num_size
new_value += [addr, size]
reg.value = new_value
@ -513,14 +514,12 @@ class DtbPlatdata(object):
else:
structs[node_name] = fields
upto = 0
for node in self._valid_nodes:
node_name, _ = self.get_normalized_compat_name(node)
struct = structs[node_name]
for name, prop in node.props.items():
if name not in PROP_IGNORE_LIST and name[0] != '#':
prop.Widen(struct[name])
upto += 1
return structs
@ -613,12 +612,10 @@ class DtbPlatdata(object):
if info:
# Process the list as pairs of (phandle, id)
pos = 0
item = 0
for args in info.args:
phandle_cell = prop.value[pos]
phandle = fdt_util.fdt32_to_cpu(phandle_cell)
target_node = self._fdt.phandle_to_node[phandle]
name = conv_name_to_c(target_node.name)
arg_values = []
for i in range(args):
arg_values.append(
@ -626,7 +623,6 @@ class DtbPlatdata(object):
pos += 1 + args
vals.append('\t{%d, {%s}}' % (target_node.idx,
', '.join(arg_values)))
item += 1
for val in vals:
self.buf('\n\t\t%s,' % val)
else:
@ -714,7 +710,7 @@ class DtbPlatdata(object):
self.out(''.join(self.get_buf()))
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
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
include_disabled (bool): True to include disabled nodes
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:
ValueError: if args has no command, or an unknown command
"""
if not args:
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_dtb()
plat.scan_tree()