commit
a2ed3f452d
@ -0,0 +1,228 @@ |
|||||||
|
#!/usr/bin/python |
||||||
|
# |
||||||
|
# Copyright (C) 2016 Google, Inc |
||||||
|
# Written by Simon Glass <sjg@chromium.org> |
||||||
|
# |
||||||
|
# SPDX-License-Identifier: GPL-2.0+ |
||||||
|
# |
||||||
|
|
||||||
|
import struct |
||||||
|
import sys |
||||||
|
|
||||||
|
import fdt |
||||||
|
from fdt import Fdt, NodeBase, PropBase |
||||||
|
import fdt_util |
||||||
|
import libfdt |
||||||
|
|
||||||
|
# This deals with a device tree, presenting it as a list of Node and Prop |
||||||
|
# objects, representing nodes and properties, respectively. |
||||||
|
# |
||||||
|
# This implementation uses a libfdt Python library to access the device tree, |
||||||
|
# so it is fairly efficient. |
||||||
|
|
||||||
|
def CheckErr(errnum, msg): |
||||||
|
if errnum: |
||||||
|
raise ValueError('Error %d: %s: %s' % |
||||||
|
(errnum, libfdt.fdt_strerror(errnum), msg)) |
||||||
|
|
||||||
|
class Prop(PropBase): |
||||||
|
"""A device tree property |
||||||
|
|
||||||
|
Properties: |
||||||
|
name: Property name (as per the device tree) |
||||||
|
value: Property value as a string of bytes, or a list of strings of |
||||||
|
bytes |
||||||
|
type: Value type |
||||||
|
""" |
||||||
|
def __init__(self, node, offset, name, bytes): |
||||||
|
PropBase.__init__(self, node, offset, name) |
||||||
|
self.bytes = bytes |
||||||
|
if not bytes: |
||||||
|
self.type = fdt.TYPE_BOOL |
||||||
|
self.value = True |
||||||
|
return |
||||||
|
self.type, self.value = self.BytesToValue(bytes) |
||||||
|
|
||||||
|
def GetOffset(self): |
||||||
|
"""Get the offset of a property |
||||||
|
|
||||||
|
Returns: |
||||||
|
The offset of the property (struct fdt_property) within the file |
||||||
|
""" |
||||||
|
return self._node._fdt.GetStructOffset(self._offset) |
||||||
|
|
||||||
|
class Node(NodeBase): |
||||||
|
"""A device tree node |
||||||
|
|
||||||
|
Properties: |
||||||
|
offset: Integer offset in the device tree |
||||||
|
name: Device tree node tname |
||||||
|
path: Full path to node, along with the node name itself |
||||||
|
_fdt: Device tree object |
||||||
|
subnodes: A list of subnodes for this node, each a Node object |
||||||
|
props: A dict of properties for this node, each a Prop object. |
||||||
|
Keyed by property name |
||||||
|
""" |
||||||
|
def __init__(self, fdt, offset, name, path): |
||||||
|
NodeBase.__init__(self, fdt, offset, name, path) |
||||||
|
|
||||||
|
def Offset(self): |
||||||
|
"""Returns the offset of a node, after checking the cache |
||||||
|
|
||||||
|
This should be used instead of self._offset directly, to ensure that |
||||||
|
the cache does not contain invalid offsets. |
||||||
|
""" |
||||||
|
self._fdt.CheckCache() |
||||||
|
return self._offset |
||||||
|
|
||||||
|
def Scan(self): |
||||||
|
"""Scan a node's properties and subnodes |
||||||
|
|
||||||
|
This fills in the props and subnodes properties, recursively |
||||||
|
searching into subnodes so that the entire tree is built. |
||||||
|
""" |
||||||
|
self.props = self._fdt.GetProps(self, self.path) |
||||||
|
|
||||||
|
offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self.Offset()) |
||||||
|
while offset >= 0: |
||||||
|
sep = '' if self.path[-1] == '/' else '/' |
||||||
|
name = libfdt.Name(self._fdt.GetFdt(), offset) |
||||||
|
path = self.path + sep + name |
||||||
|
node = Node(self._fdt, offset, name, path) |
||||||
|
self.subnodes.append(node) |
||||||
|
|
||||||
|
node.Scan() |
||||||
|
offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset) |
||||||
|
|
||||||
|
def Refresh(self, my_offset): |
||||||
|
"""Fix up the _offset for each node, recursively |
||||||
|
|
||||||
|
Note: This does not take account of property offsets - these will not |
||||||
|
be updated. |
||||||
|
""" |
||||||
|
if self._offset != my_offset: |
||||||
|
#print '%s: %d -> %d\n' % (self.path, self._offset, my_offset) |
||||||
|
self._offset = my_offset |
||||||
|
offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset) |
||||||
|
for subnode in self.subnodes: |
||||||
|
subnode.Refresh(offset) |
||||||
|
offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset) |
||||||
|
|
||||||
|
def DeleteProp(self, prop_name): |
||||||
|
"""Delete a property of a node |
||||||
|
|
||||||
|
The property is deleted and the offset cache is invalidated. |
||||||
|
|
||||||
|
Args: |
||||||
|
prop_name: Name of the property to delete |
||||||
|
Raises: |
||||||
|
ValueError if the property does not exist |
||||||
|
""" |
||||||
|
CheckErr(libfdt.fdt_delprop(self._fdt.GetFdt(), self.Offset(), prop_name), |
||||||
|
"Node '%s': delete property: '%s'" % (self.path, prop_name)) |
||||||
|
del self.props[prop_name] |
||||||
|
self._fdt.Invalidate() |
||||||
|
|
||||||
|
class FdtNormal(Fdt): |
||||||
|
"""Provides simple access to a flat device tree blob using libfdt. |
||||||
|
|
||||||
|
Properties: |
||||||
|
_fdt: Device tree contents (bytearray) |
||||||
|
_cached_offsets: True if all the nodes have a valid _offset property, |
||||||
|
False if something has changed to invalidate the offsets |
||||||
|
""" |
||||||
|
def __init__(self, fname): |
||||||
|
Fdt.__init__(self, fname) |
||||||
|
self._cached_offsets = False |
||||||
|
if self._fname: |
||||||
|
self._fname = fdt_util.EnsureCompiled(self._fname) |
||||||
|
|
||||||
|
with open(self._fname) as fd: |
||||||
|
self._fdt = bytearray(fd.read()) |
||||||
|
|
||||||
|
def GetFdt(self): |
||||||
|
"""Get the contents of the FDT |
||||||
|
|
||||||
|
Returns: |
||||||
|
The FDT contents as a string of bytes |
||||||
|
""" |
||||||
|
return self._fdt |
||||||
|
|
||||||
|
def Flush(self): |
||||||
|
"""Flush device tree changes back to the file""" |
||||||
|
with open(self._fname, 'wb') as fd: |
||||||
|
fd.write(self._fdt) |
||||||
|
|
||||||
|
def Pack(self): |
||||||
|
"""Pack the device tree down to its minimum size""" |
||||||
|
CheckErr(libfdt.fdt_pack(self._fdt), 'pack') |
||||||
|
fdt_len = libfdt.fdt_totalsize(self._fdt) |
||||||
|
del self._fdt[fdt_len:] |
||||||
|
|
||||||
|
def GetProps(self, node, path): |
||||||
|
"""Get all properties from a node. |
||||||
|
|
||||||
|
Args: |
||||||
|
node: Full path to node name to look in. |
||||||
|
|
||||||
|
Returns: |
||||||
|
A dictionary containing all the properties, indexed by node name. |
||||||
|
The entries are Prop objects. |
||||||
|
|
||||||
|
Raises: |
||||||
|
ValueError: if the node does not exist. |
||||||
|
""" |
||||||
|
offset = libfdt.fdt_path_offset(self._fdt, path) |
||||||
|
if offset < 0: |
||||||
|
libfdt.Raise(offset) |
||||||
|
props_dict = {} |
||||||
|
poffset = libfdt.fdt_first_property_offset(self._fdt, offset) |
||||||
|
while poffset >= 0: |
||||||
|
dprop, plen = libfdt.fdt_get_property_by_offset(self._fdt, poffset) |
||||||
|
prop = Prop(node, poffset, libfdt.String(self._fdt, dprop.nameoff), |
||||||
|
libfdt.Data(dprop)) |
||||||
|
props_dict[prop.name] = prop |
||||||
|
|
||||||
|
poffset = libfdt.fdt_next_property_offset(self._fdt, poffset) |
||||||
|
return props_dict |
||||||
|
|
||||||
|
def Invalidate(self): |
||||||
|
"""Mark our offset cache as invalid""" |
||||||
|
self._cached_offsets = False |
||||||
|
|
||||||
|
def CheckCache(self): |
||||||
|
"""Refresh the offset cache if needed""" |
||||||
|
if self._cached_offsets: |
||||||
|
return |
||||||
|
self.Refresh() |
||||||
|
self._cached_offsets = True |
||||||
|
|
||||||
|
def Refresh(self): |
||||||
|
"""Refresh the offset cache""" |
||||||
|
self._root.Refresh(0) |
||||||
|
|
||||||
|
def GetStructOffset(self, offset): |
||||||
|
"""Get the file offset of a given struct offset |
||||||
|
|
||||||
|
Args: |
||||||
|
offset: Offset within the 'struct' region of the device tree |
||||||
|
Returns: |
||||||
|
Position of @offset within the device tree binary |
||||||
|
""" |
||||||
|
return libfdt.fdt_off_dt_struct(self._fdt) + offset |
||||||
|
|
||||||
|
@classmethod |
||||||
|
def Node(self, fdt, offset, name, path): |
||||||
|
"""Create a new node |
||||||
|
|
||||||
|
This is used by Fdt.Scan() to create a new node using the correct |
||||||
|
class. |
||||||
|
|
||||||
|
Args: |
||||||
|
fdt: Fdt object |
||||||
|
offset: Offset of node |
||||||
|
name: Node name |
||||||
|
path: Full path to node |
||||||
|
""" |
||||||
|
node = Node(fdt, offset, name, path) |
||||||
|
return node |
@ -0,0 +1,26 @@ |
|||||||
|
#!/usr/bin/python |
||||||
|
# |
||||||
|
# Copyright (C) 2016 Google, Inc |
||||||
|
# Written by Simon Glass <sjg@chromium.org> |
||||||
|
# |
||||||
|
# SPDX-License-Identifier: GPL-2.0+ |
||||||
|
# |
||||||
|
|
||||||
|
# Bring in either the normal fdt library (which relies on libfdt) or the |
||||||
|
# fallback one (which uses fdtget and is slower). Both provide the same |
||||||
|
# interface for this file to use. |
||||||
|
try: |
||||||
|
import fdt_normal |
||||||
|
have_libfdt = True |
||||||
|
except ImportError: |
||||||
|
have_libfdt = False |
||||||
|
import fdt_fallback |
||||||
|
|
||||||
|
def FdtScan(fname): |
||||||
|
"""Returns a new Fdt object from the implementation we are using""" |
||||||
|
if have_libfdt: |
||||||
|
dtb = fdt_normal.FdtNormal(fname) |
||||||
|
else: |
||||||
|
dtb = fdt_fallback.FdtFallback(fname) |
||||||
|
dtb.Scan() |
||||||
|
return dtb |
@ -0,0 +1,120 @@ |
|||||||
|
# |
||||||
|
# Copyright (c) 2016 Google, Inc |
||||||
|
# |
||||||
|
# SPDX-License-Identifier: GPL-2.0+ |
||||||
|
# |
||||||
|
|
||||||
|
import os |
||||||
|
import shutil |
||||||
|
import tempfile |
||||||
|
|
||||||
|
import tout |
||||||
|
|
||||||
|
outdir = None |
||||||
|
indirs = None |
||||||
|
preserve_outdir = False |
||||||
|
|
||||||
|
def PrepareOutputDir(dirname, preserve=False): |
||||||
|
"""Select an output directory, ensuring it exists. |
||||||
|
|
||||||
|
This either creates a temporary directory or checks that the one supplied |
||||||
|
by the user is valid. For a temporary directory, it makes a note to |
||||||
|
remove it later if required. |
||||||
|
|
||||||
|
Args: |
||||||
|
dirname: a string, name of the output directory to use to store |
||||||
|
intermediate and output files. If is None - create a temporary |
||||||
|
directory. |
||||||
|
preserve: a Boolean. If outdir above is None and preserve is False, the |
||||||
|
created temporary directory will be destroyed on exit. |
||||||
|
|
||||||
|
Raises: |
||||||
|
OSError: If it cannot create the output directory. |
||||||
|
""" |
||||||
|
global outdir, preserve_outdir |
||||||
|
|
||||||
|
preserve_outdir = dirname or preserve |
||||||
|
if dirname: |
||||||
|
outdir = dirname |
||||||
|
if not os.path.isdir(outdir): |
||||||
|
try: |
||||||
|
os.makedirs(outdir) |
||||||
|
except OSError as err: |
||||||
|
raise CmdError("Cannot make output directory '%s': '%s'" % |
||||||
|
(outdir, err.strerror)) |
||||||
|
tout.Debug("Using output directory '%s'" % outdir) |
||||||
|
else: |
||||||
|
outdir = tempfile.mkdtemp(prefix='binman.') |
||||||
|
tout.Debug("Using temporary directory '%s'" % outdir) |
||||||
|
|
||||||
|
def _RemoveOutputDir(): |
||||||
|
global outdir |
||||||
|
|
||||||
|
shutil.rmtree(outdir) |
||||||
|
tout.Debug("Deleted temporary directory '%s'" % outdir) |
||||||
|
outdir = None |
||||||
|
|
||||||
|
def FinaliseOutputDir(): |
||||||
|
global outdir, preserve_outdir |
||||||
|
|
||||||
|
"""Tidy up: delete output directory if temporary and not preserved.""" |
||||||
|
if outdir and not preserve_outdir: |
||||||
|
_RemoveOutputDir() |
||||||
|
|
||||||
|
def GetOutputFilename(fname): |
||||||
|
"""Return a filename within the output directory. |
||||||
|
|
||||||
|
Args: |
||||||
|
fname: Filename to use for new file |
||||||
|
|
||||||
|
Returns: |
||||||
|
The full path of the filename, within the output directory |
||||||
|
""" |
||||||
|
return os.path.join(outdir, fname) |
||||||
|
|
||||||
|
def _FinaliseForTest(): |
||||||
|
"""Remove the output directory (for use by tests)""" |
||||||
|
global outdir |
||||||
|
|
||||||
|
if outdir: |
||||||
|
_RemoveOutputDir() |
||||||
|
|
||||||
|
def SetInputDirs(dirname): |
||||||
|
"""Add a list of input directories, where input files are kept. |
||||||
|
|
||||||
|
Args: |
||||||
|
dirname: a list of paths to input directories to use for obtaining |
||||||
|
files needed by binman to place in the image. |
||||||
|
""" |
||||||
|
global indir |
||||||
|
|
||||||
|
indir = dirname |
||||||
|
tout.Debug("Using input directories %s" % indir) |
||||||
|
|
||||||
|
def GetInputFilename(fname): |
||||||
|
"""Return a filename for use as input. |
||||||
|
|
||||||
|
Args: |
||||||
|
fname: Filename to use for new file |
||||||
|
|
||||||
|
Returns: |
||||||
|
The full path of the filename, within the input directory |
||||||
|
""" |
||||||
|
if not indir: |
||||||
|
return fname |
||||||
|
for dirname in indir: |
||||||
|
pathname = os.path.join(dirname, fname) |
||||||
|
if os.path.exists(pathname): |
||||||
|
return pathname |
||||||
|
|
||||||
|
raise ValueError("Filename '%s' not found in input path (%s)" % |
||||||
|
(fname, ','.join(indir))) |
||||||
|
|
||||||
|
def Align(pos, align): |
||||||
|
if align: |
||||||
|
mask = align - 1 |
||||||
|
pos = (pos + mask) & ~mask |
||||||
|
return pos |
||||||
|
|
||||||
|
def NotPowerOfTwo(num): |
||||||
|
return num and (num & (num - 1)) |
@ -0,0 +1,166 @@ |
|||||||
|
# Copyright (c) 2016 Google, Inc |
||||||
|
# |
||||||
|
# SPDX-License-Identifier: GPL-2.0+ |
||||||
|
# |
||||||
|
# Terminal output logging. |
||||||
|
# |
||||||
|
|
||||||
|
import sys |
||||||
|
|
||||||
|
import terminal |
||||||
|
|
||||||
|
# Output verbosity levels that we support |
||||||
|
ERROR = 0 |
||||||
|
WARNING = 1 |
||||||
|
NOTICE = 2 |
||||||
|
INFO = 3 |
||||||
|
DEBUG = 4 |
||||||
|
|
||||||
|
""" |
||||||
|
This class handles output of progress and other useful information |
||||||
|
to the user. It provides for simple verbosity level control and can |
||||||
|
output nothing but errors at verbosity zero. |
||||||
|
|
||||||
|
The idea is that modules set up an Output object early in their years and pass |
||||||
|
it around to other modules that need it. This keeps the output under control |
||||||
|
of a single class. |
||||||
|
|
||||||
|
Public properties: |
||||||
|
verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug |
||||||
|
""" |
||||||
|
def __enter__(): |
||||||
|
return |
||||||
|
|
||||||
|
def __exit__(unused1, unused2, unused3): |
||||||
|
"""Clean up and remove any progress message.""" |
||||||
|
ClearProgress() |
||||||
|
return False |
||||||
|
|
||||||
|
def UserIsPresent(): |
||||||
|
"""This returns True if it is likely that a user is present. |
||||||
|
|
||||||
|
Sometimes we want to prompt the user, but if no one is there then this |
||||||
|
is a waste of time, and may lock a script which should otherwise fail. |
||||||
|
|
||||||
|
Returns: |
||||||
|
True if it thinks the user is there, and False otherwise |
||||||
|
""" |
||||||
|
return stdout_is_tty and verbose > 0 |
||||||
|
|
||||||
|
def ClearProgress(): |
||||||
|
"""Clear any active progress message on the terminal.""" |
||||||
|
if verbose > 0 and stdout_is_tty: |
||||||
|
_stdout.write('\r%s\r' % (" " * len (_progress))) |
||||||
|
_stdout.flush() |
||||||
|
|
||||||
|
def Progress(msg, warning=False, trailer='...'): |
||||||
|
"""Display progress information. |
||||||
|
|
||||||
|
Args: |
||||||
|
msg: Message to display. |
||||||
|
warning: True if this is a warning.""" |
||||||
|
ClearProgress() |
||||||
|
if verbose > 0: |
||||||
|
_progress = msg + trailer |
||||||
|
if stdout_is_tty: |
||||||
|
col = _color.YELLOW if warning else _color.GREEN |
||||||
|
_stdout.write('\r' + _color.Color(col, _progress)) |
||||||
|
_stdout.flush() |
||||||
|
else: |
||||||
|
_stdout.write(_progress + '\n') |
||||||
|
|
||||||
|
def _Output(level, msg, color=None): |
||||||
|
"""Output a message to the terminal. |
||||||
|
|
||||||
|
Args: |
||||||
|
level: Verbosity level for this message. It will only be displayed if |
||||||
|
this as high as the currently selected level. |
||||||
|
msg; Message to display. |
||||||
|
error: True if this is an error message, else False. |
||||||
|
""" |
||||||
|
if verbose >= level: |
||||||
|
ClearProgress() |
||||||
|
if color: |
||||||
|
msg = _color.Color(color, msg) |
||||||
|
_stdout.write(msg + '\n') |
||||||
|
|
||||||
|
def DoOutput(level, msg): |
||||||
|
"""Output a message to the terminal. |
||||||
|
|
||||||
|
Args: |
||||||
|
level: Verbosity level for this message. It will only be displayed if |
||||||
|
this as high as the currently selected level. |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(level, msg) |
||||||
|
|
||||||
|
def Error(msg): |
||||||
|
"""Display an error message |
||||||
|
|
||||||
|
Args: |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(0, msg, _color.RED) |
||||||
|
|
||||||
|
def Warning(msg): |
||||||
|
"""Display a warning message |
||||||
|
|
||||||
|
Args: |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(1, msg, _color.YELLOW) |
||||||
|
|
||||||
|
def Notice(msg): |
||||||
|
"""Display an important infomation message |
||||||
|
|
||||||
|
Args: |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(2, msg) |
||||||
|
|
||||||
|
def Info(msg): |
||||||
|
"""Display an infomation message |
||||||
|
|
||||||
|
Args: |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(3, msg) |
||||||
|
|
||||||
|
def Debug(msg): |
||||||
|
"""Display a debug message |
||||||
|
|
||||||
|
Args: |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(4, msg) |
||||||
|
|
||||||
|
def UserOutput(msg): |
||||||
|
"""Display a message regardless of the current output level. |
||||||
|
|
||||||
|
This is used when the output was specifically requested by the user. |
||||||
|
Args: |
||||||
|
msg; Message to display. |
||||||
|
""" |
||||||
|
_Output(0, msg) |
||||||
|
|
||||||
|
def Init(_verbose=WARNING, stdout=sys.stdout): |
||||||
|
"""Initialize a new output object. |
||||||
|
|
||||||
|
Args: |
||||||
|
verbose: Verbosity level (0-4). |
||||||
|
stdout: File to use for stdout. |
||||||
|
""" |
||||||
|
global verbose, _progress, _color, _stdout, stdout_is_tty |
||||||
|
|
||||||
|
verbose = _verbose |
||||||
|
_progress = '' # Our last progress message |
||||||
|
_color = terminal.Color() |
||||||
|
_stdout = stdout |
||||||
|
|
||||||
|
# TODO(sjg): Move this into Chromite libraries when we have them |
||||||
|
stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
||||||
|
|
||||||
|
def Uninit(): |
||||||
|
ClearProgress() |
||||||
|
|
||||||
|
Init() |
Loading…
Reference in new issue