fdthelper.py revision 12469
112469Sglenn.bergmans@arm.com# Copyright (c) 2016 ARM Limited
212469Sglenn.bergmans@arm.com# All rights reserved.
312469Sglenn.bergmans@arm.com#
412469Sglenn.bergmans@arm.com# The license below extends only to copyright in the software and shall
512469Sglenn.bergmans@arm.com# not be construed as granting a license to any other intellectual
612469Sglenn.bergmans@arm.com# property including but not limited to intellectual property relating
712469Sglenn.bergmans@arm.com# to a hardware implementation of the functionality of the software
812469Sglenn.bergmans@arm.com# licensed hereunder.  You may use the software subject to the license
912469Sglenn.bergmans@arm.com# terms below provided that you ensure that this notice is replicated
1012469Sglenn.bergmans@arm.com# unmodified and in its entirety in all distributions of the software,
1112469Sglenn.bergmans@arm.com# modified or unmodified, in source code or in binary form.
1212469Sglenn.bergmans@arm.com#
1312469Sglenn.bergmans@arm.com# Redistribution and use in source and binary forms, with or without
1412469Sglenn.bergmans@arm.com# modification, are permitted provided that the following conditions are
1512469Sglenn.bergmans@arm.com# met: redistributions of source code must retain the above copyright
1612469Sglenn.bergmans@arm.com# notice, this list of conditions and the following disclaimer;
1712469Sglenn.bergmans@arm.com# redistributions in binary form must reproduce the above copyright
1812469Sglenn.bergmans@arm.com# notice, this list of conditions and the following disclaimer in the
1912469Sglenn.bergmans@arm.com# documentation and/or other materials provided with the distribution;
2012469Sglenn.bergmans@arm.com# neither the name of the copyright holders nor the names of its
2112469Sglenn.bergmans@arm.com# contributors may be used to endorse or promote products derived from
2212469Sglenn.bergmans@arm.com# this software without specific prior written permission.
2312469Sglenn.bergmans@arm.com#
2412469Sglenn.bergmans@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2512469Sglenn.bergmans@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2612469Sglenn.bergmans@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2712469Sglenn.bergmans@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2812469Sglenn.bergmans@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2912469Sglenn.bergmans@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3012469Sglenn.bergmans@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3112469Sglenn.bergmans@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3212469Sglenn.bergmans@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3312469Sglenn.bergmans@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3412469Sglenn.bergmans@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3512469Sglenn.bergmans@arm.com#
3612469Sglenn.bergmans@arm.com# Author: Glenn Bergmans
3712469Sglenn.bergmans@arm.com
3812469Sglenn.bergmans@arm.comfrom m5.ext.pyfdt import pyfdt
3912469Sglenn.bergmans@arm.comimport re
4012469Sglenn.bergmans@arm.comimport os
4112469Sglenn.bergmans@arm.comfrom m5.SimObject import SimObject
4212469Sglenn.bergmans@arm.com
4312469Sglenn.bergmans@arm.comclass FdtProperty(pyfdt.FdtProperty):
4412469Sglenn.bergmans@arm.com    """Create a property without values."""
4512469Sglenn.bergmans@arm.com    pass
4612469Sglenn.bergmans@arm.com
4712469Sglenn.bergmans@arm.comclass FdtPropertyWords(pyfdt.FdtPropertyWords):
4812469Sglenn.bergmans@arm.com    """Create a property with word (32-bit unsigned) values."""
4912469Sglenn.bergmans@arm.com    def __init__(self, name, words):
5012469Sglenn.bergmans@arm.com        if type(words) != list:
5112469Sglenn.bergmans@arm.com            words = [words]
5212469Sglenn.bergmans@arm.com        # Make sure all values are ints (use automatic base detection if the
5312469Sglenn.bergmans@arm.com        # type is str)
5412469Sglenn.bergmans@arm.com        words = [long(w, base=0) if type(w) == str else long(w) for w in words]
5512469Sglenn.bergmans@arm.com        super(FdtPropertyWords, self).__init__(name, words)
5612469Sglenn.bergmans@arm.com
5712469Sglenn.bergmans@arm.comclass FdtPropertyStrings(pyfdt.FdtPropertyStrings):
5812469Sglenn.bergmans@arm.com    """Create a property with string values."""
5912469Sglenn.bergmans@arm.com
6012469Sglenn.bergmans@arm.com    def __init__(self, name, strings):
6112469Sglenn.bergmans@arm.com        if type(strings) == str:
6212469Sglenn.bergmans@arm.com            strings = [strings]
6312469Sglenn.bergmans@arm.com        strings = [str(string) for string in strings] # Make all values strings
6412469Sglenn.bergmans@arm.com        super(FdtPropertyStrings, self).__init__(name, strings)
6512469Sglenn.bergmans@arm.com
6612469Sglenn.bergmans@arm.comclass FdtPropertyBytes(pyfdt.FdtPropertyBytes):
6712469Sglenn.bergmans@arm.com    """Create a property with integer (8-bit signed) values."""
6812469Sglenn.bergmans@arm.com
6912469Sglenn.bergmans@arm.com    def __init__(self, name, values):
7012469Sglenn.bergmans@arm.com        if type(values) != list:
7112469Sglenn.bergmans@arm.com            values = [values]
7212469Sglenn.bergmans@arm.com        # Make sure all values are ints (use automatic base detection if the
7312469Sglenn.bergmans@arm.com        # type is str)
7412469Sglenn.bergmans@arm.com        values = [int(v, base=0)
7512469Sglenn.bergmans@arm.com                   if isinstance(v, str) else int(v) for v in values]
7612469Sglenn.bergmans@arm.com        super(FdtPropertyBytes, self).__init__(name, values)
7712469Sglenn.bergmans@arm.com
7812469Sglenn.bergmans@arm.comclass FdtState(object):
7912469Sglenn.bergmans@arm.com    """Class for maintaining state while recursively generating a flattened
8012469Sglenn.bergmans@arm.com    device tree. The state tracks address, size and CPU address cell sizes, and
8112469Sglenn.bergmans@arm.com    maintains a dictionary of allocated phandles."""
8212469Sglenn.bergmans@arm.com
8312469Sglenn.bergmans@arm.com    phandle_counter = 0
8412469Sglenn.bergmans@arm.com    phandles = dict()
8512469Sglenn.bergmans@arm.com
8612469Sglenn.bergmans@arm.com    def __init__(self, addr_cells, size_cells, cpu_cells):
8712469Sglenn.bergmans@arm.com        """Instantiate values of this state. The state can only be initialized
8812469Sglenn.bergmans@arm.com        once."""
8912469Sglenn.bergmans@arm.com
9012469Sglenn.bergmans@arm.com        self.addr_cells = addr_cells
9112469Sglenn.bergmans@arm.com        self.size_cells = size_cells
9212469Sglenn.bergmans@arm.com        self.cpu_cells = cpu_cells
9312469Sglenn.bergmans@arm.com
9412469Sglenn.bergmans@arm.com    def phandle(self, obj):
9512469Sglenn.bergmans@arm.com        """Return a unique phandle number for a key. The key can be a SimObject
9612469Sglenn.bergmans@arm.com        or any value that is castable to a string. If the phandle doesn't exist
9712469Sglenn.bergmans@arm.com        a new one is created, otherwise the existing one is returned."""
9812469Sglenn.bergmans@arm.com
9912469Sglenn.bergmans@arm.com        if isinstance(obj, SimObject):
10012469Sglenn.bergmans@arm.com            key = str(id(obj))
10112469Sglenn.bergmans@arm.com        else:
10212469Sglenn.bergmans@arm.com            try:
10312469Sglenn.bergmans@arm.com                key = str(obj)
10412469Sglenn.bergmans@arm.com            except ValueError:
10512469Sglenn.bergmans@arm.com                raise ValueError('Phandle keys must be castable to str')
10612469Sglenn.bergmans@arm.com
10712469Sglenn.bergmans@arm.com        if not key in FdtState.phandles:
10812469Sglenn.bergmans@arm.com            FdtState.phandle_counter += 1
10912469Sglenn.bergmans@arm.com
11012469Sglenn.bergmans@arm.com        return FdtState.phandles.setdefault(key, FdtState.phandle_counter)
11112469Sglenn.bergmans@arm.com
11212469Sglenn.bergmans@arm.com    def resetPhandles(self):
11312469Sglenn.bergmans@arm.com        FdtState.phandle_counter = 0
11412469Sglenn.bergmans@arm.com        FdtState.phandles = dict()
11512469Sglenn.bergmans@arm.com
11612469Sglenn.bergmans@arm.com    def int_to_cells(self, value, cells):
11712469Sglenn.bergmans@arm.com        """Helper function for: generates a list of 32 bit cells from an int,
11812469Sglenn.bergmans@arm.com        used to split up addresses in appropriate 32 bit chunks."""
11912469Sglenn.bergmans@arm.com        value = long(value)
12012469Sglenn.bergmans@arm.com
12112469Sglenn.bergmans@arm.com        if (value >> (32 * cells)) != 0:
12212469Sglenn.bergmans@arm.com            fatal("Value %d doesn't fit in %d cells" % (value, cells))
12312469Sglenn.bergmans@arm.com
12412469Sglenn.bergmans@arm.com        return [(value >> 32*(x-1)) & 0xFFFFFFFF for x in range(cells, 0, -1)]
12512469Sglenn.bergmans@arm.com
12612469Sglenn.bergmans@arm.com    def addrCells(self, addr):
12712469Sglenn.bergmans@arm.com        """Format an integer type according to the address_cells value of this
12812469Sglenn.bergmans@arm.com        state."""
12912469Sglenn.bergmans@arm.com        return self.int_to_cells(addr, self.addr_cells)
13012469Sglenn.bergmans@arm.com
13112469Sglenn.bergmans@arm.com    def CPUAddrCells(self, addr):
13212469Sglenn.bergmans@arm.com        """Format an integer type according to the cpu_cells value of this
13312469Sglenn.bergmans@arm.com        state."""
13412469Sglenn.bergmans@arm.com        return self.int_to_cells(addr, self.cpu_cells)
13512469Sglenn.bergmans@arm.com
13612469Sglenn.bergmans@arm.com    def sizeCells(self, size):
13712469Sglenn.bergmans@arm.com        """Format an integer type according to the size_cells value of this
13812469Sglenn.bergmans@arm.com        state."""
13912469Sglenn.bergmans@arm.com        return self.int_to_cells(size, self.size_cells)
14012469Sglenn.bergmans@arm.com
14112469Sglenn.bergmans@arm.com    def addrCellsProperty(self):
14212469Sglenn.bergmans@arm.com        """Return an #address-cells property with the value of this state."""
14312469Sglenn.bergmans@arm.com        return FdtPropertyWords("#address-cells", self.addr_cells)
14412469Sglenn.bergmans@arm.com
14512469Sglenn.bergmans@arm.com    def sizeCellsProperty(self):
14612469Sglenn.bergmans@arm.com        """Return an #size-cells property with the value of this state."""
14712469Sglenn.bergmans@arm.com        return FdtPropertyWords("#size-cells", self.size_cells)
14812469Sglenn.bergmans@arm.com
14912469Sglenn.bergmans@arm.com    def CPUCellsProperty(self):
15012469Sglenn.bergmans@arm.com        """Return an #address-cells property for cpu nodes with the value
15112469Sglenn.bergmans@arm.com        of this state."""
15212469Sglenn.bergmans@arm.com        return FdtPropertyWords("#address-cells", self.cpu_cells)
15312469Sglenn.bergmans@arm.com
15412469Sglenn.bergmans@arm.comclass FdtNop(pyfdt.FdtNop):
15512469Sglenn.bergmans@arm.com    """Create an empty node."""
15612469Sglenn.bergmans@arm.com    pass
15712469Sglenn.bergmans@arm.com
15812469Sglenn.bergmans@arm.comclass FdtNode(pyfdt.FdtNode):
15912469Sglenn.bergmans@arm.com    def __init__(self, name, obj=None):
16012469Sglenn.bergmans@arm.com        """Create a new node and immediately set the phandle property, if obj
16112469Sglenn.bergmans@arm.com        is supplied"""
16212469Sglenn.bergmans@arm.com        super(FdtNode, self).__init__(name)
16312469Sglenn.bergmans@arm.com        if obj != None:
16412469Sglenn.bergmans@arm.com            self.appendPhandle(obj)
16512469Sglenn.bergmans@arm.com
16612469Sglenn.bergmans@arm.com    def append(self, subnodes):
16712469Sglenn.bergmans@arm.com        """Change the behavior of the normal append to override if a node with
16812469Sglenn.bergmans@arm.com        the same name already exists or merge if the name exists and is a node
16912469Sglenn.bergmans@arm.com        type. Can also take a list of subnodes, that each get appended."""
17012469Sglenn.bergmans@arm.com        if not hasattr(subnodes, '__iter__'):
17112469Sglenn.bergmans@arm.com            subnodes = [subnodes]
17212469Sglenn.bergmans@arm.com
17312469Sglenn.bergmans@arm.com        for subnode in subnodes:
17412469Sglenn.bergmans@arm.com            try:
17512469Sglenn.bergmans@arm.com                if not issubclass(type(subnode), pyfdt.FdtNop):
17612469Sglenn.bergmans@arm.com                    index = self.index(subnode.name)
17712469Sglenn.bergmans@arm.com                    item = self.pop(index)
17812469Sglenn.bergmans@arm.com                else:
17912469Sglenn.bergmans@arm.com                    item = None
18012469Sglenn.bergmans@arm.com            except ValueError:
18112469Sglenn.bergmans@arm.com                item = None
18212469Sglenn.bergmans@arm.com
18312469Sglenn.bergmans@arm.com            if isinstance(item,  pyfdt.FdtNode) and \
18412469Sglenn.bergmans@arm.com               isinstance(subnode,  pyfdt.FdtNode):
18512469Sglenn.bergmans@arm.com                item.merge(subnode)
18612469Sglenn.bergmans@arm.com                subnode = item
18712469Sglenn.bergmans@arm.com
18812469Sglenn.bergmans@arm.com            super(FdtNode, self).append(subnode)
18912469Sglenn.bergmans@arm.com
19012469Sglenn.bergmans@arm.com    def appendList(self, subnode_list):
19112469Sglenn.bergmans@arm.com        """Append all properties/nodes in the iterable."""
19212469Sglenn.bergmans@arm.com        for subnode in subnode_list:
19312469Sglenn.bergmans@arm.com            self.append(subnode)
19412469Sglenn.bergmans@arm.com
19512469Sglenn.bergmans@arm.com    def appendCompatible(self, compatible):
19612469Sglenn.bergmans@arm.com        """Append a compatible property with the supplied compatibility
19712469Sglenn.bergmans@arm.com        strings."""
19812469Sglenn.bergmans@arm.com        if isinstance(compatible, str):
19912469Sglenn.bergmans@arm.com            compatible = [compatible]
20012469Sglenn.bergmans@arm.com        self.append(FdtPropertyStrings('compatible', compatible))
20112469Sglenn.bergmans@arm.com
20212469Sglenn.bergmans@arm.com    def appendPhandle(self, obj):
20312469Sglenn.bergmans@arm.com        """Append a phandle property to this node with the phandle of the
20412469Sglenn.bergmans@arm.com        supplied object."""
20512469Sglenn.bergmans@arm.com        # Create a bogus state because we only need the Phandle dictionary
20612469Sglenn.bergmans@arm.com        state = FdtState(addr_cells=1, size_cells=1, cpu_cells=1)
20712469Sglenn.bergmans@arm.com
20812469Sglenn.bergmans@arm.com        phandle = state.phandle(obj)
20912469Sglenn.bergmans@arm.com        self.append(FdtPropertyWords("phandle", [phandle]))
21012469Sglenn.bergmans@arm.com
21112469Sglenn.bergmans@arm.comclass Fdt(pyfdt.Fdt):
21212469Sglenn.bergmans@arm.com    def sortNodes(self, node):
21312469Sglenn.bergmans@arm.com        """Move all properties to the beginning and subnodes to the end
21412469Sglenn.bergmans@arm.com        while maintaining the order of the subnodes. DTB files require the
21512469Sglenn.bergmans@arm.com        properties to go before the nodes, but the PyFdt doesn't account for
21612469Sglenn.bergmans@arm.com        defining nodes and properties in a random order."""
21712469Sglenn.bergmans@arm.com        properties = FdtNode(node.name)
21812469Sglenn.bergmans@arm.com        subnodes = FdtNode(node.name)
21912469Sglenn.bergmans@arm.com
22012469Sglenn.bergmans@arm.com        while len(node):
22112469Sglenn.bergmans@arm.com            subnode = node.pop(0)
22212469Sglenn.bergmans@arm.com            if issubclass(type(subnode), pyfdt.FdtNode):
22312469Sglenn.bergmans@arm.com                subnode = self.sortNodes(subnode)
22412469Sglenn.bergmans@arm.com                subnodes.append(subnode)
22512469Sglenn.bergmans@arm.com            else:
22612469Sglenn.bergmans@arm.com                properties.append(subnode)
22712469Sglenn.bergmans@arm.com
22812469Sglenn.bergmans@arm.com        properties.merge(subnodes)
22912469Sglenn.bergmans@arm.com
23012469Sglenn.bergmans@arm.com        return properties
23112469Sglenn.bergmans@arm.com
23212469Sglenn.bergmans@arm.com    def add_rootnode(self, rootnode, prenops=None, postnops=None):
23312469Sglenn.bergmans@arm.com        """First sort the device tree, so that properties are before nodes."""
23412469Sglenn.bergmans@arm.com        rootnode = self.sortNodes(rootnode)
23512469Sglenn.bergmans@arm.com        super(Fdt, self).add_rootnode(rootnode, prenops, postnops)
23612469Sglenn.bergmans@arm.com
23712469Sglenn.bergmans@arm.com    def writeDtbFile(self, filename):
23812469Sglenn.bergmans@arm.com        """Convert the device tree to DTB and write to a file."""
23912469Sglenn.bergmans@arm.com        filename = os.path.realpath(filename)
24012469Sglenn.bergmans@arm.com        try:
24112469Sglenn.bergmans@arm.com            with open(filename, 'wb') as f:
24212469Sglenn.bergmans@arm.com                f.write(self.to_dtb())
24312469Sglenn.bergmans@arm.com            return filename
24412469Sglenn.bergmans@arm.com        except IOError:
24512469Sglenn.bergmans@arm.com            raise RuntimeError("Failed to open DTB output file")
24612469Sglenn.bergmans@arm.com
24712469Sglenn.bergmans@arm.com    def writeDtsFile(self, filename):
24812469Sglenn.bergmans@arm.com        """Convert the device tree to DTS and write to a file."""
24912469Sglenn.bergmans@arm.com        filename = os.path.realpath(filename)
25012469Sglenn.bergmans@arm.com        try:
25112469Sglenn.bergmans@arm.com            with open(filename, 'w') as f:
25212469Sglenn.bergmans@arm.com                f.write(self.to_dts())
25312469Sglenn.bergmans@arm.com            return filename
25412469Sglenn.bergmans@arm.com        except IOError:
25512469Sglenn.bergmans@arm.com            raise RuntimeError("Failed to open DTS output file")
256