14549e789STom Rini/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
215b97f5cSMasahiro Yamada/*
315b97f5cSMasahiro Yamada * pylibfdt - Flat Device Tree manipulation in Python
415b97f5cSMasahiro Yamada * Copyright (C) 2017 Google, Inc.
515b97f5cSMasahiro Yamada * Written by Simon Glass <sjg@chromium.org>
615b97f5cSMasahiro Yamada */
715b97f5cSMasahiro Yamada
815b97f5cSMasahiro Yamada%module libfdt
915b97f5cSMasahiro Yamada
1015b97f5cSMasahiro Yamada%include <stdint.i>
1115b97f5cSMasahiro Yamada
1215b97f5cSMasahiro Yamada%{
1315b97f5cSMasahiro Yamada#define SWIG_FILE_WITH_INIT
1415b97f5cSMasahiro Yamada#include "libfdt.h"
153def0cf2SSimon Glass
163def0cf2SSimon Glass/*
173def0cf2SSimon Glass * We rename this function here to avoid problems with swig, since we also have
183def0cf2SSimon Glass * a struct called fdt_property. That struct causes swig to create a class in
193def0cf2SSimon Glass * libfdt.py called fdt_property(), which confuses things.
203def0cf2SSimon Glass */
2150c59522SSimon Glassstatic int fdt_property_stub(void *fdt, const char *name, const char *val,
2250c59522SSimon Glass                             int len)
233def0cf2SSimon Glass{
243def0cf2SSimon Glass    return fdt_property(fdt, name, val, len);
253def0cf2SSimon Glass}
263def0cf2SSimon Glass
2715b97f5cSMasahiro Yamada%}
2815b97f5cSMasahiro Yamada
2915b97f5cSMasahiro Yamada%pythoncode %{
3015b97f5cSMasahiro Yamada
3115b97f5cSMasahiro Yamadaimport struct
3215b97f5cSMasahiro Yamada
3315b97f5cSMasahiro Yamada# Error codes, corresponding to FDT_ERR_... in libfdt.h
3415b97f5cSMasahiro Yamada(NOTFOUND,
3515b97f5cSMasahiro Yamada        EXISTS,
3615b97f5cSMasahiro Yamada        NOSPACE,
3715b97f5cSMasahiro Yamada        BADOFFSET,
3815b97f5cSMasahiro Yamada        BADPATH,
3915b97f5cSMasahiro Yamada        BADPHANDLE,
4015b97f5cSMasahiro Yamada        BADSTATE,
4115b97f5cSMasahiro Yamada        TRUNCATED,
4215b97f5cSMasahiro Yamada        BADMAGIC,
4315b97f5cSMasahiro Yamada        BADVERSION,
4415b97f5cSMasahiro Yamada        BADSTRUCTURE,
4515b97f5cSMasahiro Yamada        BADLAYOUT,
4615b97f5cSMasahiro Yamada        INTERNAL,
4715b97f5cSMasahiro Yamada        BADNCELLS,
4815b97f5cSMasahiro Yamada        BADVALUE,
4915b97f5cSMasahiro Yamada        BADOVERLAY,
5015b97f5cSMasahiro Yamada        NOPHANDLES) = QUIET_ALL = range(1, 18)
5115b97f5cSMasahiro Yamada# QUIET_ALL can be passed as the 'quiet' parameter to avoid exceptions
5215b97f5cSMasahiro Yamada# altogether. All # functions passed this value will return an error instead
5315b97f5cSMasahiro Yamada# of raising an exception.
5415b97f5cSMasahiro Yamada
5515b97f5cSMasahiro Yamada# Pass this as the 'quiet' parameter to return -ENOTFOUND on NOTFOUND errors,
5615b97f5cSMasahiro Yamada# instead of raising an exception.
5715b97f5cSMasahiro YamadaQUIET_NOTFOUND = (NOTFOUND,)
5850c59522SSimon GlassQUIET_NOSPACE = (NOSPACE,)
5915b97f5cSMasahiro Yamada
6015b97f5cSMasahiro Yamada
6115b97f5cSMasahiro Yamadaclass FdtException(Exception):
6215b97f5cSMasahiro Yamada    """An exception caused by an error such as one of the codes above"""
6315b97f5cSMasahiro Yamada    def __init__(self, err):
6415b97f5cSMasahiro Yamada        self.err = err
6515b97f5cSMasahiro Yamada
6615b97f5cSMasahiro Yamada    def __str__(self):
6715b97f5cSMasahiro Yamada        return 'pylibfdt error %d: %s' % (self.err, fdt_strerror(self.err))
6815b97f5cSMasahiro Yamada
6915b97f5cSMasahiro Yamadadef strerror(fdt_err):
7015b97f5cSMasahiro Yamada    """Get the string for an error number
7115b97f5cSMasahiro Yamada
7215b97f5cSMasahiro Yamada    Args:
7315b97f5cSMasahiro Yamada        fdt_err: Error number (-ve)
7415b97f5cSMasahiro Yamada
7515b97f5cSMasahiro Yamada    Returns:
7615b97f5cSMasahiro Yamada        String containing the associated error
7715b97f5cSMasahiro Yamada    """
7815b97f5cSMasahiro Yamada    return fdt_strerror(fdt_err)
7915b97f5cSMasahiro Yamada
8015b97f5cSMasahiro Yamadadef check_err(val, quiet=()):
8115b97f5cSMasahiro Yamada    """Raise an error if the return value is -ve
8215b97f5cSMasahiro Yamada
8315b97f5cSMasahiro Yamada    This is used to check for errors returned by libfdt C functions.
8415b97f5cSMasahiro Yamada
8515b97f5cSMasahiro Yamada    Args:
8615b97f5cSMasahiro Yamada        val: Return value from a libfdt function
8715b97f5cSMasahiro Yamada        quiet: Errors to ignore (empty to raise on all errors)
8815b97f5cSMasahiro Yamada
8915b97f5cSMasahiro Yamada    Returns:
9015b97f5cSMasahiro Yamada        val if val >= 0
9115b97f5cSMasahiro Yamada
9215b97f5cSMasahiro Yamada    Raises
9315b97f5cSMasahiro Yamada        FdtException if val < 0
9415b97f5cSMasahiro Yamada    """
9515b97f5cSMasahiro Yamada    if val < 0:
9615b97f5cSMasahiro Yamada        if -val not in quiet:
9715b97f5cSMasahiro Yamada            raise FdtException(val)
9815b97f5cSMasahiro Yamada    return val
9915b97f5cSMasahiro Yamada
10015b97f5cSMasahiro Yamadadef check_err_null(val, quiet=()):
10115b97f5cSMasahiro Yamada    """Raise an error if the return value is NULL
10215b97f5cSMasahiro Yamada
10315b97f5cSMasahiro Yamada    This is used to check for a NULL return value from certain libfdt C
10415b97f5cSMasahiro Yamada    functions
10515b97f5cSMasahiro Yamada
10615b97f5cSMasahiro Yamada    Args:
10715b97f5cSMasahiro Yamada        val: Return value from a libfdt function
10815b97f5cSMasahiro Yamada        quiet: Errors to ignore (empty to raise on all errors)
10915b97f5cSMasahiro Yamada
11015b97f5cSMasahiro Yamada    Returns:
11115b97f5cSMasahiro Yamada        val if val is a list, None if not
11215b97f5cSMasahiro Yamada
11315b97f5cSMasahiro Yamada    Raises
11415b97f5cSMasahiro Yamada        FdtException if val indicates an error was reported and the error
11515b97f5cSMasahiro Yamada        is not in @quiet.
11615b97f5cSMasahiro Yamada    """
11715b97f5cSMasahiro Yamada    # Normally a list is returned which contains the data and its length.
11815b97f5cSMasahiro Yamada    # If we get just an integer error code, it means the function failed.
11915b97f5cSMasahiro Yamada    if not isinstance(val, list):
12015b97f5cSMasahiro Yamada        if -val not in quiet:
12115b97f5cSMasahiro Yamada            raise FdtException(val)
12215b97f5cSMasahiro Yamada    return val
12315b97f5cSMasahiro Yamada
12450c59522SSimon Glassclass FdtRo(object):
12550c59522SSimon Glass    """Class for a read-only device-tree
1263def0cf2SSimon Glass
12750c59522SSimon Glass    This is a base class used by FdtRw (read-write access) and FdtSw
12850c59522SSimon Glass    (sequential-write access). It implements read-only access to the
12950c59522SSimon Glass    device tree.
13015b97f5cSMasahiro Yamada
13150c59522SSimon Glass    Here are the three classes and when you should use them:
13215b97f5cSMasahiro Yamada
13350c59522SSimon Glass        FdtRo - read-only access to an existing FDT
13450c59522SSimon Glass        FdtRw - read-write access to an existing FDT (most common case)
13550c59522SSimon Glass        FdtSw - for creating a new FDT, as well as allowing read-only access
13615b97f5cSMasahiro Yamada    """
13715b97f5cSMasahiro Yamada    def __init__(self, data):
13815b97f5cSMasahiro Yamada        self._fdt = bytearray(data)
13915b97f5cSMasahiro Yamada        check_err(fdt_check_header(self._fdt));
14015b97f5cSMasahiro Yamada
1413def0cf2SSimon Glass    def as_bytearray(self):
1423def0cf2SSimon Glass        """Get the device tree contents as a bytearray
1433def0cf2SSimon Glass
1443def0cf2SSimon Glass        This can be passed directly to libfdt functions that access a
1453def0cf2SSimon Glass        const void * for the device tree.
1463def0cf2SSimon Glass
1473def0cf2SSimon Glass        Returns:
1483def0cf2SSimon Glass            bytearray containing the device tree
1493def0cf2SSimon Glass        """
1503def0cf2SSimon Glass        return bytearray(self._fdt)
1513def0cf2SSimon Glass
1523def0cf2SSimon Glass    def next_node(self, nodeoffset, depth, quiet=()):
1533def0cf2SSimon Glass        """Find the next subnode
1543def0cf2SSimon Glass
1553def0cf2SSimon Glass        Args:
1563def0cf2SSimon Glass            nodeoffset: Node offset of previous node
15750c59522SSimon Glass            depth: The depth of the node at nodeoffset. This is used to
15850c59522SSimon Glass                calculate the depth of the returned node
1593def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
1603def0cf2SSimon Glass
1613def0cf2SSimon Glass        Returns:
16250c59522SSimon Glass            Typle:
16350c59522SSimon Glass                Offset of the next node, if any, else a -ve error
16450c59522SSimon Glass                Depth of the returned node, if any, else undefined
1653def0cf2SSimon Glass
1663def0cf2SSimon Glass        Raises:
1673def0cf2SSimon Glass            FdtException if no more nodes found or other error occurs
1683def0cf2SSimon Glass        """
1693def0cf2SSimon Glass        return check_err(fdt_next_node(self._fdt, nodeoffset, depth), quiet)
1703def0cf2SSimon Glass
1713def0cf2SSimon Glass    def first_subnode(self, nodeoffset, quiet=()):
1723def0cf2SSimon Glass        """Find the first subnode of a parent node
1733def0cf2SSimon Glass
1743def0cf2SSimon Glass        Args:
1753def0cf2SSimon Glass            nodeoffset: Node offset of parent node
1763def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
1773def0cf2SSimon Glass
1783def0cf2SSimon Glass        Returns:
1793def0cf2SSimon Glass            The offset of the first subnode, if any
1803def0cf2SSimon Glass
1813def0cf2SSimon Glass        Raises:
1823def0cf2SSimon Glass            FdtException if no subnodes found or other error occurs
1833def0cf2SSimon Glass        """
1843def0cf2SSimon Glass        return check_err(fdt_first_subnode(self._fdt, nodeoffset), quiet)
1853def0cf2SSimon Glass
1863def0cf2SSimon Glass    def next_subnode(self, nodeoffset, quiet=()):
1873def0cf2SSimon Glass        """Find the next subnode
1883def0cf2SSimon Glass
1893def0cf2SSimon Glass        Args:
1903def0cf2SSimon Glass            nodeoffset: Node offset of previous subnode
1913def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
1923def0cf2SSimon Glass
1933def0cf2SSimon Glass        Returns:
1943def0cf2SSimon Glass            The offset of the next subnode, if any
1953def0cf2SSimon Glass
1963def0cf2SSimon Glass        Raises:
1973def0cf2SSimon Glass            FdtException if no more subnodes found or other error occurs
1983def0cf2SSimon Glass        """
1993def0cf2SSimon Glass        return check_err(fdt_next_subnode(self._fdt, nodeoffset), quiet)
2003def0cf2SSimon Glass
2013def0cf2SSimon Glass    def magic(self):
2023def0cf2SSimon Glass        """Return the magic word from the header
2033def0cf2SSimon Glass
2043def0cf2SSimon Glass        Returns:
2053def0cf2SSimon Glass            Magic word
2063def0cf2SSimon Glass        """
20750c59522SSimon Glass        return fdt_magic(self._fdt)
2083def0cf2SSimon Glass
2093def0cf2SSimon Glass    def totalsize(self):
2103def0cf2SSimon Glass        """Return the total size of the device tree
2113def0cf2SSimon Glass
2123def0cf2SSimon Glass        Returns:
2133def0cf2SSimon Glass            Total tree size in bytes
2143def0cf2SSimon Glass        """
21550c59522SSimon Glass        return fdt_totalsize(self._fdt)
2163def0cf2SSimon Glass
2173def0cf2SSimon Glass    def off_dt_struct(self):
2183def0cf2SSimon Glass        """Return the start of the device-tree struct area
2193def0cf2SSimon Glass
2203def0cf2SSimon Glass        Returns:
2213def0cf2SSimon Glass            Start offset of struct area
2223def0cf2SSimon Glass        """
22350c59522SSimon Glass        return fdt_off_dt_struct(self._fdt)
2243def0cf2SSimon Glass
2253def0cf2SSimon Glass    def off_dt_strings(self):
2263def0cf2SSimon Glass        """Return the start of the device-tree string area
2273def0cf2SSimon Glass
2283def0cf2SSimon Glass        Returns:
2293def0cf2SSimon Glass            Start offset of string area
2303def0cf2SSimon Glass        """
23150c59522SSimon Glass        return fdt_off_dt_strings(self._fdt)
2323def0cf2SSimon Glass
2333def0cf2SSimon Glass    def off_mem_rsvmap(self):
2343def0cf2SSimon Glass        """Return the start of the memory reserve map
2353def0cf2SSimon Glass
2363def0cf2SSimon Glass        Returns:
2373def0cf2SSimon Glass            Start offset of memory reserve map
2383def0cf2SSimon Glass        """
23950c59522SSimon Glass        return fdt_off_mem_rsvmap(self._fdt)
2403def0cf2SSimon Glass
2413def0cf2SSimon Glass    def version(self):
2423def0cf2SSimon Glass        """Return the version of the device tree
2433def0cf2SSimon Glass
2443def0cf2SSimon Glass        Returns:
2453def0cf2SSimon Glass            Version number of the device tree
2463def0cf2SSimon Glass        """
24750c59522SSimon Glass        return fdt_version(self._fdt)
2483def0cf2SSimon Glass
2493def0cf2SSimon Glass    def last_comp_version(self):
2503def0cf2SSimon Glass        """Return the last compatible version of the device tree
2513def0cf2SSimon Glass
2523def0cf2SSimon Glass        Returns:
2533def0cf2SSimon Glass            Last compatible version number of the device tree
2543def0cf2SSimon Glass        """
25550c59522SSimon Glass        return fdt_last_comp_version(self._fdt)
2563def0cf2SSimon Glass
2573def0cf2SSimon Glass    def boot_cpuid_phys(self):
2583def0cf2SSimon Glass        """Return the physical boot CPU ID
2593def0cf2SSimon Glass
2603def0cf2SSimon Glass        Returns:
2613def0cf2SSimon Glass            Physical boot CPU ID
2623def0cf2SSimon Glass        """
26350c59522SSimon Glass        return fdt_boot_cpuid_phys(self._fdt)
2643def0cf2SSimon Glass
2653def0cf2SSimon Glass    def size_dt_strings(self):
2663def0cf2SSimon Glass        """Return the start of the device-tree string area
2673def0cf2SSimon Glass
2683def0cf2SSimon Glass        Returns:
2693def0cf2SSimon Glass            Start offset of string area
2703def0cf2SSimon Glass        """
27150c59522SSimon Glass        return fdt_size_dt_strings(self._fdt)
2723def0cf2SSimon Glass
2733def0cf2SSimon Glass    def size_dt_struct(self):
2743def0cf2SSimon Glass        """Return the start of the device-tree struct area
2753def0cf2SSimon Glass
2763def0cf2SSimon Glass        Returns:
2773def0cf2SSimon Glass            Start offset of struct area
2783def0cf2SSimon Glass        """
27950c59522SSimon Glass        return fdt_size_dt_struct(self._fdt)
2803def0cf2SSimon Glass
2813def0cf2SSimon Glass    def num_mem_rsv(self, quiet=()):
2823def0cf2SSimon Glass        """Return the number of memory reserve-map records
2833def0cf2SSimon Glass
2843def0cf2SSimon Glass        Returns:
2853def0cf2SSimon Glass            Number of memory reserve-map records
2863def0cf2SSimon Glass        """
2873def0cf2SSimon Glass        return check_err(fdt_num_mem_rsv(self._fdt), quiet)
2883def0cf2SSimon Glass
2893def0cf2SSimon Glass    def get_mem_rsv(self, index, quiet=()):
2903def0cf2SSimon Glass        """Return the indexed memory reserve-map record
2913def0cf2SSimon Glass
2923def0cf2SSimon Glass        Args:
2933def0cf2SSimon Glass            index: Record to return (0=first)
2943def0cf2SSimon Glass
2953def0cf2SSimon Glass        Returns:
2963def0cf2SSimon Glass            Number of memory reserve-map records
2973def0cf2SSimon Glass        """
2983def0cf2SSimon Glass        return check_err(fdt_get_mem_rsv(self._fdt, index), quiet)
2993def0cf2SSimon Glass
30015b97f5cSMasahiro Yamada    def subnode_offset(self, parentoffset, name, quiet=()):
30115b97f5cSMasahiro Yamada        """Get the offset of a named subnode
30215b97f5cSMasahiro Yamada
30315b97f5cSMasahiro Yamada        Args:
30415b97f5cSMasahiro Yamada            parentoffset: Offset of the parent node to check
30515b97f5cSMasahiro Yamada            name: Name of the required subnode, e.g. 'subnode@1'
30615b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
30715b97f5cSMasahiro Yamada
30815b97f5cSMasahiro Yamada        Returns:
30915b97f5cSMasahiro Yamada            The node offset of the found node, if any
31015b97f5cSMasahiro Yamada
31115b97f5cSMasahiro Yamada        Raises
31215b97f5cSMasahiro Yamada            FdtException if there is no node with that name, or other error
31315b97f5cSMasahiro Yamada        """
31415b97f5cSMasahiro Yamada        return check_err(fdt_subnode_offset(self._fdt, parentoffset, name),
31515b97f5cSMasahiro Yamada                         quiet)
31615b97f5cSMasahiro Yamada
31715b97f5cSMasahiro Yamada    def path_offset(self, path, quiet=()):
31815b97f5cSMasahiro Yamada        """Get the offset for a given path
31915b97f5cSMasahiro Yamada
32015b97f5cSMasahiro Yamada        Args:
32115b97f5cSMasahiro Yamada            path: Path to the required node, e.g. '/node@3/subnode@1'
32215b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
32315b97f5cSMasahiro Yamada
32415b97f5cSMasahiro Yamada        Returns:
32515b97f5cSMasahiro Yamada            Node offset
32615b97f5cSMasahiro Yamada
32715b97f5cSMasahiro Yamada        Raises
32815b97f5cSMasahiro Yamada            FdtException if the path is not valid or not found
32915b97f5cSMasahiro Yamada        """
33015b97f5cSMasahiro Yamada        return check_err(fdt_path_offset(self._fdt, path), quiet)
33115b97f5cSMasahiro Yamada
3323def0cf2SSimon Glass    def get_name(self, nodeoffset):
3333def0cf2SSimon Glass        """Get the name of a node
3343def0cf2SSimon Glass
3353def0cf2SSimon Glass        Args:
3363def0cf2SSimon Glass            nodeoffset: Offset of node to check
3373def0cf2SSimon Glass
3383def0cf2SSimon Glass        Returns:
3393def0cf2SSimon Glass            Node name
3403def0cf2SSimon Glass
3413def0cf2SSimon Glass        Raises:
3423def0cf2SSimon Glass            FdtException on error (e.g. nodeoffset is invalid)
3433def0cf2SSimon Glass        """
3443def0cf2SSimon Glass        return check_err_null(fdt_get_name(self._fdt, nodeoffset))[0]
3453def0cf2SSimon Glass
34615b97f5cSMasahiro Yamada    def first_property_offset(self, nodeoffset, quiet=()):
34715b97f5cSMasahiro Yamada        """Get the offset of the first property in a node offset
34815b97f5cSMasahiro Yamada
34915b97f5cSMasahiro Yamada        Args:
35015b97f5cSMasahiro Yamada            nodeoffset: Offset to the node to check
35115b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
35215b97f5cSMasahiro Yamada
35315b97f5cSMasahiro Yamada        Returns:
35415b97f5cSMasahiro Yamada            Offset of the first property
35515b97f5cSMasahiro Yamada
35615b97f5cSMasahiro Yamada        Raises
35715b97f5cSMasahiro Yamada            FdtException if the associated node has no properties, or some
35815b97f5cSMasahiro Yamada                other error occurred
35915b97f5cSMasahiro Yamada        """
36015b97f5cSMasahiro Yamada        return check_err(fdt_first_property_offset(self._fdt, nodeoffset),
36115b97f5cSMasahiro Yamada                         quiet)
36215b97f5cSMasahiro Yamada
36315b97f5cSMasahiro Yamada    def next_property_offset(self, prop_offset, quiet=()):
36415b97f5cSMasahiro Yamada        """Get the next property in a node
36515b97f5cSMasahiro Yamada
36615b97f5cSMasahiro Yamada        Args:
36715b97f5cSMasahiro Yamada            prop_offset: Offset of the previous property
36815b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
36915b97f5cSMasahiro Yamada
37015b97f5cSMasahiro Yamada        Returns:
37115b97f5cSMasahiro Yamada            Offset of the next property
37215b97f5cSMasahiro Yamada
37315b97f5cSMasahiro Yamada        Raises:
37415b97f5cSMasahiro Yamada            FdtException if the associated node has no more properties, or
37515b97f5cSMasahiro Yamada                some other error occurred
37615b97f5cSMasahiro Yamada        """
37715b97f5cSMasahiro Yamada        return check_err(fdt_next_property_offset(self._fdt, prop_offset),
37815b97f5cSMasahiro Yamada                         quiet)
37915b97f5cSMasahiro Yamada
38015b97f5cSMasahiro Yamada    def get_property_by_offset(self, prop_offset, quiet=()):
38115b97f5cSMasahiro Yamada        """Obtains a property that can be examined
38215b97f5cSMasahiro Yamada
38315b97f5cSMasahiro Yamada        Args:
38415b97f5cSMasahiro Yamada            prop_offset: Offset of property (e.g. from first_property_offset())
38515b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
38615b97f5cSMasahiro Yamada
38715b97f5cSMasahiro Yamada        Returns:
38815b97f5cSMasahiro Yamada            Property object, or None if not found
38915b97f5cSMasahiro Yamada
39015b97f5cSMasahiro Yamada        Raises:
39115b97f5cSMasahiro Yamada            FdtException on error (e.g. invalid prop_offset or device
39215b97f5cSMasahiro Yamada            tree format)
39315b97f5cSMasahiro Yamada        """
39415b97f5cSMasahiro Yamada        pdata = check_err_null(
39515b97f5cSMasahiro Yamada                fdt_get_property_by_offset(self._fdt, prop_offset), quiet)
39615b97f5cSMasahiro Yamada        if isinstance(pdata, (int)):
39715b97f5cSMasahiro Yamada            return pdata
39815b97f5cSMasahiro Yamada        return Property(pdata[0], pdata[1])
39915b97f5cSMasahiro Yamada
40015b97f5cSMasahiro Yamada    def getprop(self, nodeoffset, prop_name, quiet=()):
40115b97f5cSMasahiro Yamada        """Get a property from a node
40215b97f5cSMasahiro Yamada
40315b97f5cSMasahiro Yamada        Args:
40415b97f5cSMasahiro Yamada            nodeoffset: Node offset containing property to get
40515b97f5cSMasahiro Yamada            prop_name: Name of property to get
40615b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
40715b97f5cSMasahiro Yamada
40815b97f5cSMasahiro Yamada        Returns:
40950c59522SSimon Glass            Value of property as a Property object (which can be used as a
41050c59522SSimon Glass               bytearray/string), or -ve error number. On failure, returns an
41150c59522SSimon Glass               integer error
41215b97f5cSMasahiro Yamada
41315b97f5cSMasahiro Yamada        Raises:
41415b97f5cSMasahiro Yamada            FdtError if any error occurs (e.g. the property is not found)
41515b97f5cSMasahiro Yamada        """
41615b97f5cSMasahiro Yamada        pdata = check_err_null(fdt_getprop(self._fdt, nodeoffset, prop_name),
41715b97f5cSMasahiro Yamada                               quiet)
41815b97f5cSMasahiro Yamada        if isinstance(pdata, (int)):
41915b97f5cSMasahiro Yamada            return pdata
4203def0cf2SSimon Glass        return Property(prop_name, bytearray(pdata[0]))
42115b97f5cSMasahiro Yamada
42215b97f5cSMasahiro Yamada    def get_phandle(self, nodeoffset):
42315b97f5cSMasahiro Yamada        """Get the phandle of a node
42415b97f5cSMasahiro Yamada
42515b97f5cSMasahiro Yamada        Args:
42615b97f5cSMasahiro Yamada            nodeoffset: Node offset to check
42715b97f5cSMasahiro Yamada
42815b97f5cSMasahiro Yamada        Returns:
42915b97f5cSMasahiro Yamada            phandle of node, or 0 if the node has no phandle or another error
43015b97f5cSMasahiro Yamada            occurs
43115b97f5cSMasahiro Yamada        """
43215b97f5cSMasahiro Yamada        return fdt_get_phandle(self._fdt, nodeoffset)
43315b97f5cSMasahiro Yamada
43415b97f5cSMasahiro Yamada    def parent_offset(self, nodeoffset, quiet=()):
43515b97f5cSMasahiro Yamada        """Get the offset of a node's parent
43615b97f5cSMasahiro Yamada
43715b97f5cSMasahiro Yamada        Args:
43815b97f5cSMasahiro Yamada            nodeoffset: Node offset to check
43915b97f5cSMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
44015b97f5cSMasahiro Yamada
44115b97f5cSMasahiro Yamada        Returns:
44215b97f5cSMasahiro Yamada            The offset of the parent node, if any
44315b97f5cSMasahiro Yamada
44415b97f5cSMasahiro Yamada        Raises:
44515b97f5cSMasahiro Yamada            FdtException if no parent found or other error occurs
44615b97f5cSMasahiro Yamada        """
44715b97f5cSMasahiro Yamada        return check_err(fdt_parent_offset(self._fdt, nodeoffset), quiet)
44815b97f5cSMasahiro Yamada
44950c59522SSimon Glass    def node_offset_by_phandle(self, phandle, quiet=()):
45050c59522SSimon Glass        """Get the offset of a node with the given phandle
45150c59522SSimon Glass
45250c59522SSimon Glass        Args:
45350c59522SSimon Glass            phandle: Phandle to search for
45450c59522SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
45550c59522SSimon Glass
45650c59522SSimon Glass        Returns:
45750c59522SSimon Glass            The offset of node with that phandle, if any
45850c59522SSimon Glass
45950c59522SSimon Glass        Raises:
46050c59522SSimon Glass            FdtException if no node found or other error occurs
46150c59522SSimon Glass        """
46250c59522SSimon Glass        return check_err(fdt_node_offset_by_phandle(self._fdt, phandle), quiet)
46350c59522SSimon Glass
46450c59522SSimon Glass
46550c59522SSimon Glassclass Fdt(FdtRo):
46650c59522SSimon Glass    """Device tree class, supporting all operations
46750c59522SSimon Glass
46850c59522SSimon Glass    The Fdt object is created is created from a device tree binary file,
46950c59522SSimon Glass    e.g. with something like:
47050c59522SSimon Glass
47150c59522SSimon Glass       fdt = Fdt(open("filename.dtb").read())
47250c59522SSimon Glass
47350c59522SSimon Glass    Operations can then be performed using the methods in this class. Each
47450c59522SSimon Glass    method xxx(args...) corresponds to a libfdt function fdt_xxx(fdt, args...).
47550c59522SSimon Glass
47650c59522SSimon Glass    All methods raise an FdtException if an error occurs. To avoid this
47750c59522SSimon Glass    behaviour a 'quiet' parameter is provided for some functions. This
47850c59522SSimon Glass    defaults to empty, but you can pass a list of errors that you expect.
47950c59522SSimon Glass    If one of these errors occurs, the function will return an error number
48050c59522SSimon Glass    (e.g. -NOTFOUND).
48150c59522SSimon Glass    """
48250c59522SSimon Glass    def __init__(self, data):
48350c59522SSimon Glass        FdtRo.__init__(self, data)
48450c59522SSimon Glass
48550c59522SSimon Glass    @staticmethod
48650c59522SSimon Glass    def create_empty_tree(size, quiet=()):
48750c59522SSimon Glass        """Create an empty device tree ready for use
48850c59522SSimon Glass
48950c59522SSimon Glass        Args:
49050c59522SSimon Glass            size: Size of device tree in bytes
49150c59522SSimon Glass
49250c59522SSimon Glass        Returns:
49350c59522SSimon Glass            Fdt object containing the device tree
49450c59522SSimon Glass        """
49550c59522SSimon Glass        data = bytearray(size)
49650c59522SSimon Glass        err = check_err(fdt_create_empty_tree(data, size), quiet)
49750c59522SSimon Glass        if err:
49850c59522SSimon Glass            return err
49950c59522SSimon Glass        return Fdt(data)
50050c59522SSimon Glass
50150c59522SSimon Glass    def resize(self, size, quiet=()):
50250c59522SSimon Glass        """Move the device tree into a larger or smaller space
50350c59522SSimon Glass
50450c59522SSimon Glass        This creates a new device tree of size @size and moves the existing
50550c59522SSimon Glass        device tree contents over to that. It can be used to create more space
50650c59522SSimon Glass        in a device tree. Note that the Fdt object remains the same, but it
50750c59522SSimon Glass        now has a new bytearray holding the contents.
50850c59522SSimon Glass
50950c59522SSimon Glass        Args:
51050c59522SSimon Glass            size: Required new size of device tree in bytes
51150c59522SSimon Glass        """
51250c59522SSimon Glass        fdt = bytearray(size)
51350c59522SSimon Glass        err = check_err(fdt_open_into(self._fdt, fdt, size), quiet)
51450c59522SSimon Glass        if err:
51550c59522SSimon Glass            return err
51650c59522SSimon Glass        self._fdt = fdt
51750c59522SSimon Glass
51850c59522SSimon Glass    def pack(self, quiet=()):
51950c59522SSimon Glass        """Pack the device tree to remove unused space
52050c59522SSimon Glass
52150c59522SSimon Glass        This adjusts the tree in place.
52250c59522SSimon Glass
52350c59522SSimon Glass        Args:
52450c59522SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
52550c59522SSimon Glass
52650c59522SSimon Glass        Returns:
52750c59522SSimon Glass            Error code, or 0 if OK
52850c59522SSimon Glass
52950c59522SSimon Glass        Raises:
53050c59522SSimon Glass            FdtException if any error occurs
53150c59522SSimon Glass        """
53250c59522SSimon Glass        err = check_err(fdt_pack(self._fdt), quiet)
53350c59522SSimon Glass        if err:
53450c59522SSimon Glass            return err
53550c59522SSimon Glass        del self._fdt[self.totalsize():]
53650c59522SSimon Glass        return err
53750c59522SSimon Glass
5383def0cf2SSimon Glass    def set_name(self, nodeoffset, name, quiet=()):
5393def0cf2SSimon Glass        """Set the name of a node
5403def0cf2SSimon Glass
5413def0cf2SSimon Glass        Args:
5423def0cf2SSimon Glass            nodeoffset: Node offset of node to update
54350c59522SSimon Glass            name: New node name (string without \0)
5443def0cf2SSimon Glass
5453def0cf2SSimon Glass        Returns:
5463def0cf2SSimon Glass            Error code, or 0 if OK
5473def0cf2SSimon Glass
5483def0cf2SSimon Glass        Raises:
5493def0cf2SSimon Glass            FdtException if no parent found or other error occurs
5503def0cf2SSimon Glass        """
55150c59522SSimon Glass        if chr(0) in name:
55250c59522SSimon Glass            raise ValueError('Property contains embedded nul characters')
5533def0cf2SSimon Glass        return check_err(fdt_set_name(self._fdt, nodeoffset, name), quiet)
5543def0cf2SSimon Glass
5553def0cf2SSimon Glass    def setprop(self, nodeoffset, prop_name, val, quiet=()):
5563def0cf2SSimon Glass        """Set the value of a property
5573def0cf2SSimon Glass
5583def0cf2SSimon Glass        Args:
5593def0cf2SSimon Glass            nodeoffset: Node offset containing the property to create/update
5603def0cf2SSimon Glass            prop_name: Name of property
5613def0cf2SSimon Glass            val: Value to write (string or bytearray)
5623def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
5633def0cf2SSimon Glass
5643def0cf2SSimon Glass        Returns:
5653def0cf2SSimon Glass            Error code, or 0 if OK
5663def0cf2SSimon Glass
5673def0cf2SSimon Glass        Raises:
5683def0cf2SSimon Glass            FdtException if no parent found or other error occurs
5693def0cf2SSimon Glass        """
5703def0cf2SSimon Glass        return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name, val,
5713def0cf2SSimon Glass                                     len(val)), quiet)
5723def0cf2SSimon Glass
5733def0cf2SSimon Glass    def setprop_u32(self, nodeoffset, prop_name, val, quiet=()):
5743def0cf2SSimon Glass        """Set the value of a property
5753def0cf2SSimon Glass
5763def0cf2SSimon Glass        Args:
5773def0cf2SSimon Glass            nodeoffset: Node offset containing the property to create/update
5783def0cf2SSimon Glass            prop_name: Name of property
5793def0cf2SSimon Glass            val: Value to write (integer)
5803def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
5813def0cf2SSimon Glass
5823def0cf2SSimon Glass        Returns:
5833def0cf2SSimon Glass            Error code, or 0 if OK
5843def0cf2SSimon Glass
5853def0cf2SSimon Glass        Raises:
5863def0cf2SSimon Glass            FdtException if no parent found or other error occurs
5873def0cf2SSimon Glass        """
5883def0cf2SSimon Glass        return check_err(fdt_setprop_u32(self._fdt, nodeoffset, prop_name, val),
5893def0cf2SSimon Glass                         quiet)
5903def0cf2SSimon Glass
5913def0cf2SSimon Glass    def setprop_u64(self, nodeoffset, prop_name, val, quiet=()):
5923def0cf2SSimon Glass        """Set the value of a property
5933def0cf2SSimon Glass
5943def0cf2SSimon Glass        Args:
5953def0cf2SSimon Glass            nodeoffset: Node offset containing the property to create/update
5963def0cf2SSimon Glass            prop_name: Name of property
5973def0cf2SSimon Glass            val: Value to write (integer)
5983def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
5993def0cf2SSimon Glass
6003def0cf2SSimon Glass        Returns:
6013def0cf2SSimon Glass            Error code, or 0 if OK
6023def0cf2SSimon Glass
6033def0cf2SSimon Glass        Raises:
6043def0cf2SSimon Glass            FdtException if no parent found or other error occurs
6053def0cf2SSimon Glass        """
6063def0cf2SSimon Glass        return check_err(fdt_setprop_u64(self._fdt, nodeoffset, prop_name, val),
6073def0cf2SSimon Glass                         quiet)
6083def0cf2SSimon Glass
6093def0cf2SSimon Glass    def setprop_str(self, nodeoffset, prop_name, val, quiet=()):
6103def0cf2SSimon Glass        """Set the string value of a property
6113def0cf2SSimon Glass
6123def0cf2SSimon Glass        The property is set to the string, with a nul terminator added
6133def0cf2SSimon Glass
6143def0cf2SSimon Glass        Args:
6153def0cf2SSimon Glass            nodeoffset: Node offset containing the property to create/update
6163def0cf2SSimon Glass            prop_name: Name of property
61750c59522SSimon Glass            val: Value to write (string without nul terminator). Unicode is
61850c59522SSimon Glass                supposed by encoding to UTF-8
6193def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
6203def0cf2SSimon Glass
6213def0cf2SSimon Glass        Returns:
6223def0cf2SSimon Glass            Error code, or 0 if OK
6233def0cf2SSimon Glass
6243def0cf2SSimon Glass        Raises:
6253def0cf2SSimon Glass            FdtException if no parent found or other error occurs
6263def0cf2SSimon Glass        """
62750c59522SSimon Glass        val = val.encode('utf-8') + '\0'
6283def0cf2SSimon Glass        return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
6293def0cf2SSimon Glass                                     val, len(val)), quiet)
6303def0cf2SSimon Glass
631*34967c26SSimon Glass    def delprop(self, nodeoffset, prop_name, quiet=()):
6323def0cf2SSimon Glass        """Delete a property from a node
6333def0cf2SSimon Glass
6343def0cf2SSimon Glass        Args:
6353def0cf2SSimon Glass            nodeoffset: Node offset containing property to delete
6363def0cf2SSimon Glass            prop_name: Name of property to delete
637*34967c26SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
638*34967c26SSimon Glass
639*34967c26SSimon Glass        Returns:
640*34967c26SSimon Glass            Error code, or 0 if OK
6413def0cf2SSimon Glass
6423def0cf2SSimon Glass        Raises:
6433def0cf2SSimon Glass            FdtError if the property does not exist, or another error occurs
6443def0cf2SSimon Glass        """
645*34967c26SSimon Glass        return check_err(fdt_delprop(self._fdt, nodeoffset, prop_name), quiet)
6463def0cf2SSimon Glass
647*34967c26SSimon Glass    def add_subnode(self, parentoffset, name, quiet=()):
648*34967c26SSimon Glass        """Add a new subnode to a node
649*34967c26SSimon Glass
650*34967c26SSimon Glass        Args:
651*34967c26SSimon Glass            parentoffset: Parent offset to add the subnode to
652*34967c26SSimon Glass            name: Name of node to add
653*34967c26SSimon Glass
654*34967c26SSimon Glass        Returns:
655*34967c26SSimon Glass            offset of the node created, or negative error code on failure
656*34967c26SSimon Glass
657*34967c26SSimon Glass        Raises:
658*34967c26SSimon Glass            FdtError if there is not enough space, or another error occurs
659*34967c26SSimon Glass        """
660*34967c26SSimon Glass        return check_err(fdt_add_subnode(self._fdt, parentoffset, name), quiet)
661*34967c26SSimon Glass
662*34967c26SSimon Glass    def del_node(self, nodeoffset, quiet=()):
663c640ed0cSSimon Glass        """Delete a node
664c640ed0cSSimon Glass
665c640ed0cSSimon Glass        Args:
666*34967c26SSimon Glass            nodeoffset: Offset of node to delete
667*34967c26SSimon Glass
668*34967c26SSimon Glass        Returns:
669*34967c26SSimon Glass            Error code, or 0 if OK
670c640ed0cSSimon Glass
671c640ed0cSSimon Glass        Raises:
672*34967c26SSimon Glass            FdtError if an error occurs
673c640ed0cSSimon Glass        """
674*34967c26SSimon Glass        return check_err(fdt_del_node(self._fdt, nodeoffset), quiet)
675c640ed0cSSimon Glass
6763def0cf2SSimon Glass
6773def0cf2SSimon Glassclass Property(bytearray):
67815b97f5cSMasahiro Yamada    """Holds a device tree property name and value.
67915b97f5cSMasahiro Yamada
68015b97f5cSMasahiro Yamada    This holds a copy of a property taken from the device tree. It does not
68115b97f5cSMasahiro Yamada    reference the device tree, so if anything changes in the device tree,
68215b97f5cSMasahiro Yamada    a Property object will remain valid.
68315b97f5cSMasahiro Yamada
68415b97f5cSMasahiro Yamada    Properties:
68515b97f5cSMasahiro Yamada        name: Property name
6863def0cf2SSimon Glass        value: Property value as a bytearray
68715b97f5cSMasahiro Yamada    """
68815b97f5cSMasahiro Yamada    def __init__(self, name, value):
6893def0cf2SSimon Glass        bytearray.__init__(self, value)
69015b97f5cSMasahiro Yamada        self.name = name
6913def0cf2SSimon Glass
6923def0cf2SSimon Glass    def as_cell(self, fmt):
6933def0cf2SSimon Glass        return struct.unpack('>' + fmt, self)[0]
6943def0cf2SSimon Glass
6953def0cf2SSimon Glass    def as_uint32(self):
6963def0cf2SSimon Glass        return self.as_cell('L')
6973def0cf2SSimon Glass
6983def0cf2SSimon Glass    def as_int32(self):
6993def0cf2SSimon Glass        return self.as_cell('l')
7003def0cf2SSimon Glass
7013def0cf2SSimon Glass    def as_uint64(self):
7023def0cf2SSimon Glass        return self.as_cell('Q')
7033def0cf2SSimon Glass
7043def0cf2SSimon Glass    def as_int64(self):
7053def0cf2SSimon Glass        return self.as_cell('q')
7063def0cf2SSimon Glass
7073def0cf2SSimon Glass    def as_str(self):
70850c59522SSimon Glass        """Unicode is supported by decoding from UTF-8"""
70950c59522SSimon Glass        if self[-1] != 0:
71050c59522SSimon Glass            raise ValueError('Property lacks nul termination')
71150c59522SSimon Glass        if 0 in self[:-1]:
71250c59522SSimon Glass            raise ValueError('Property contains embedded nul characters')
71350c59522SSimon Glass        return self[:-1].decode('utf-8')
7143def0cf2SSimon Glass
7153def0cf2SSimon Glass
71650c59522SSimon Glassclass FdtSw(FdtRo):
7173def0cf2SSimon Glass    """Software interface to create a device tree from scratch
7183def0cf2SSimon Glass
7193def0cf2SSimon Glass    The methods in this class work by adding to an existing 'partial' device
7203def0cf2SSimon Glass    tree buffer of a fixed size created by instantiating this class. When the
72150c59522SSimon Glass    tree is complete, call as_fdt() to obtain a device tree ready to be used.
7223def0cf2SSimon Glass
7233def0cf2SSimon Glass    Similarly with nodes, a new node is started with begin_node() and finished
7243def0cf2SSimon Glass    with end_node().
7253def0cf2SSimon Glass
7263def0cf2SSimon Glass    The context manager functions can be used to make this a bit easier:
7273def0cf2SSimon Glass
7283def0cf2SSimon Glass    # First create the device tree with a node and property:
72950c59522SSimon Glass    sw = FdtSw()
73050c59522SSimon Glass    with sw.add_node('node'):
7313def0cf2SSimon Glass        sw.property_u32('reg', 2)
73250c59522SSimon Glass    fdt = sw.as_fdt()
7333def0cf2SSimon Glass
7343def0cf2SSimon Glass    # Now we can use it as a real device tree
7353def0cf2SSimon Glass    fdt.setprop_u32(0, 'reg', 3)
73650c59522SSimon Glass
73750c59522SSimon Glass    The size hint provides a starting size for the space to be used by the
73850c59522SSimon Glass    device tree. This will be increased automatically as needed as new items
73950c59522SSimon Glass    are added to the tree.
7403def0cf2SSimon Glass    """
74150c59522SSimon Glass    INC_SIZE = 1024  # Expand size by this much when out of space
74250c59522SSimon Glass
74350c59522SSimon Glass    def __init__(self, size_hint=None):
74450c59522SSimon Glass        """Create a new FdtSw object
74550c59522SSimon Glass
74650c59522SSimon Glass        Args:
74750c59522SSimon Glass            size_hint: A hint as to the initial size to use
74850c59522SSimon Glass
74950c59522SSimon Glass        Raises:
75050c59522SSimon Glass            ValueError if size_hint is negative
75150c59522SSimon Glass
75250c59522SSimon Glass        Returns:
75350c59522SSimon Glass            FdtSw object on success, else integer error code (if not raising)
75450c59522SSimon Glass        """
75550c59522SSimon Glass        if not size_hint:
75650c59522SSimon Glass            size_hint = self.INC_SIZE
75750c59522SSimon Glass        fdtsw = bytearray(size_hint)
75850c59522SSimon Glass        err = check_err(fdt_create(fdtsw, size_hint))
7593def0cf2SSimon Glass        if err:
7603def0cf2SSimon Glass            return err
76150c59522SSimon Glass        self._fdt = fdtsw
7623def0cf2SSimon Glass
76350c59522SSimon Glass    def as_fdt(self):
7643def0cf2SSimon Glass        """Convert a FdtSw into an Fdt so it can be accessed as normal
7653def0cf2SSimon Glass
76650c59522SSimon Glass        Creates a new Fdt object from the work-in-progress device tree. This
76750c59522SSimon Glass        does not call fdt_finish() on the current object, so it is possible to
76850c59522SSimon Glass        add more nodes/properties and call as_fdt() again to get an updated
76950c59522SSimon Glass        tree.
7703def0cf2SSimon Glass
7713def0cf2SSimon Glass        Returns:
7723def0cf2SSimon Glass            Fdt object allowing access to the newly created device tree
7733def0cf2SSimon Glass        """
77450c59522SSimon Glass        fdtsw = bytearray(self._fdt)
77550c59522SSimon Glass        check_err(fdt_finish(fdtsw))
77650c59522SSimon Glass        return Fdt(fdtsw)
7773def0cf2SSimon Glass
77850c59522SSimon Glass    def check_space(self, val):
77950c59522SSimon Glass        """Check if we need to add more space to the FDT
78050c59522SSimon Glass
78150c59522SSimon Glass        This should be called with the error code from an operation. If this is
78250c59522SSimon Glass        -NOSPACE then the FDT will be expanded to have more space, and True will
78350c59522SSimon Glass        be returned, indicating that the operation needs to be tried again.
78450c59522SSimon Glass
78550c59522SSimon Glass        Args:
78650c59522SSimon Glass            val: Return value from the operation that was attempted
78750c59522SSimon Glass
78850c59522SSimon Glass        Returns:
78950c59522SSimon Glass            True if the operation must be retried, else False
79050c59522SSimon Glass        """
79150c59522SSimon Glass        if check_err(val, QUIET_NOSPACE) < 0:
79250c59522SSimon Glass            self.resize(len(self._fdt) + self.INC_SIZE)
79350c59522SSimon Glass            return True
79450c59522SSimon Glass        return False
79550c59522SSimon Glass
79650c59522SSimon Glass    def resize(self, size):
7973def0cf2SSimon Glass        """Resize the buffer to accommodate a larger tree
7983def0cf2SSimon Glass
7993def0cf2SSimon Glass        Args:
8003def0cf2SSimon Glass            size: New size of tree
8013def0cf2SSimon Glass
8023def0cf2SSimon Glass        Raises:
80350c59522SSimon Glass            FdtException on any error
8043def0cf2SSimon Glass        """
8053def0cf2SSimon Glass        fdt = bytearray(size)
80650c59522SSimon Glass        err = check_err(fdt_resize(self._fdt, fdt, size))
80750c59522SSimon Glass        self._fdt = fdt
8083def0cf2SSimon Glass
80950c59522SSimon Glass    def add_reservemap_entry(self, addr, size):
8103def0cf2SSimon Glass        """Add a new memory reserve map entry
8113def0cf2SSimon Glass
8123def0cf2SSimon Glass        Once finished adding, you must call finish_reservemap().
8133def0cf2SSimon Glass
8143def0cf2SSimon Glass        Args:
8153def0cf2SSimon Glass            addr: 64-bit start address
8163def0cf2SSimon Glass            size: 64-bit size
8173def0cf2SSimon Glass
8183def0cf2SSimon Glass        Raises:
81950c59522SSimon Glass            FdtException on any error
8203def0cf2SSimon Glass        """
82150c59522SSimon Glass        while self.check_space(fdt_add_reservemap_entry(self._fdt, addr,
82250c59522SSimon Glass                                                        size)):
82350c59522SSimon Glass            pass
8243def0cf2SSimon Glass
82550c59522SSimon Glass    def finish_reservemap(self):
8263def0cf2SSimon Glass        """Indicate that there are no more reserve map entries to add
8273def0cf2SSimon Glass
8283def0cf2SSimon Glass        Raises:
82950c59522SSimon Glass            FdtException on any error
8303def0cf2SSimon Glass        """
83150c59522SSimon Glass        while self.check_space(fdt_finish_reservemap(self._fdt)):
83250c59522SSimon Glass            pass
8333def0cf2SSimon Glass
83450c59522SSimon Glass    def begin_node(self, name):
8353def0cf2SSimon Glass        """Begin a new node
8363def0cf2SSimon Glass
8373def0cf2SSimon Glass        Use this before adding properties to the node. Then call end_node() to
8383def0cf2SSimon Glass        finish it. You can also use the context manager as shown in the FdtSw
8393def0cf2SSimon Glass        class comment.
8403def0cf2SSimon Glass
8413def0cf2SSimon Glass        Args:
8423def0cf2SSimon Glass            name: Name of node to begin
8433def0cf2SSimon Glass
8443def0cf2SSimon Glass        Raises:
84550c59522SSimon Glass            FdtException on any error
8463def0cf2SSimon Glass        """
84750c59522SSimon Glass        while self.check_space(fdt_begin_node(self._fdt, name)):
84850c59522SSimon Glass            pass
8493def0cf2SSimon Glass
85050c59522SSimon Glass    def property_string(self, name, string):
8513def0cf2SSimon Glass        """Add a property with a string value
8523def0cf2SSimon Glass
8533def0cf2SSimon Glass        The string will be nul-terminated when written to the device tree
8543def0cf2SSimon Glass
8553def0cf2SSimon Glass        Args:
8563def0cf2SSimon Glass            name: Name of property to add
8573def0cf2SSimon Glass            string: String value of property
8583def0cf2SSimon Glass
8593def0cf2SSimon Glass        Raises:
86050c59522SSimon Glass            FdtException on any error
8613def0cf2SSimon Glass        """
86250c59522SSimon Glass        while self.check_space(fdt_property_string(self._fdt, name, string)):
86350c59522SSimon Glass            pass
8643def0cf2SSimon Glass
86550c59522SSimon Glass    def property_u32(self, name, val):
8663def0cf2SSimon Glass        """Add a property with a 32-bit value
8673def0cf2SSimon Glass
8683def0cf2SSimon Glass        Write a single-cell value to the device tree
8693def0cf2SSimon Glass
8703def0cf2SSimon Glass        Args:
8713def0cf2SSimon Glass            name: Name of property to add
8723def0cf2SSimon Glass            val: Value of property
8733def0cf2SSimon Glass
8743def0cf2SSimon Glass        Raises:
87550c59522SSimon Glass            FdtException on any error
8763def0cf2SSimon Glass        """
87750c59522SSimon Glass        while self.check_space(fdt_property_u32(self._fdt, name, val)):
87850c59522SSimon Glass            pass
8793def0cf2SSimon Glass
88050c59522SSimon Glass    def property_u64(self, name, val):
8813def0cf2SSimon Glass        """Add a property with a 64-bit value
8823def0cf2SSimon Glass
8833def0cf2SSimon Glass        Write a double-cell value to the device tree in big-endian format
8843def0cf2SSimon Glass
8853def0cf2SSimon Glass        Args:
8863def0cf2SSimon Glass            name: Name of property to add
8873def0cf2SSimon Glass            val: Value of property
8883def0cf2SSimon Glass
8893def0cf2SSimon Glass        Raises:
89050c59522SSimon Glass            FdtException on any error
8913def0cf2SSimon Glass        """
89250c59522SSimon Glass        while self.check_space(fdt_property_u64(self._fdt, name, val)):
89350c59522SSimon Glass            pass
8943def0cf2SSimon Glass
89550c59522SSimon Glass    def property_cell(self, name, val):
8963def0cf2SSimon Glass        """Add a property with a single-cell value
8973def0cf2SSimon Glass
8983def0cf2SSimon Glass        Write a single-cell value to the device tree
8993def0cf2SSimon Glass
9003def0cf2SSimon Glass        Args:
9013def0cf2SSimon Glass            name: Name of property to add
9023def0cf2SSimon Glass            val: Value of property
9033def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
9043def0cf2SSimon Glass
9053def0cf2SSimon Glass        Raises:
90650c59522SSimon Glass            FdtException on any error
9073def0cf2SSimon Glass        """
90850c59522SSimon Glass        while self.check_space(fdt_property_cell(self._fdt, name, val)):
90950c59522SSimon Glass            pass
9103def0cf2SSimon Glass
91150c59522SSimon Glass    def property(self, name, val):
9123def0cf2SSimon Glass        """Add a property
9133def0cf2SSimon Glass
9143def0cf2SSimon Glass        Write a new property with the given value to the device tree. The value
9153def0cf2SSimon Glass        is taken as is and is not nul-terminated
9163def0cf2SSimon Glass
9173def0cf2SSimon Glass        Args:
9183def0cf2SSimon Glass            name: Name of property to add
9193def0cf2SSimon Glass            val: Value of property
9203def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
9213def0cf2SSimon Glass
9223def0cf2SSimon Glass        Raises:
92350c59522SSimon Glass            FdtException on any error
9243def0cf2SSimon Glass        """
92550c59522SSimon Glass        while self.check_space(fdt_property_stub(self._fdt, name, val,
92650c59522SSimon Glass                                                 len(val))):
92750c59522SSimon Glass            pass
9283def0cf2SSimon Glass
92950c59522SSimon Glass    def end_node(self):
9303def0cf2SSimon Glass        """End a node
9313def0cf2SSimon Glass
9323def0cf2SSimon Glass        Use this after adding properties to a node to close it off. You can also
9333def0cf2SSimon Glass        use the context manager as shown in the FdtSw class comment.
9343def0cf2SSimon Glass
9353def0cf2SSimon Glass        Args:
9363def0cf2SSimon Glass            quiet: Errors to ignore (empty to raise on all errors)
9373def0cf2SSimon Glass
9383def0cf2SSimon Glass        Raises:
93950c59522SSimon Glass            FdtException on any error
9403def0cf2SSimon Glass        """
94150c59522SSimon Glass        while self.check_space(fdt_end_node(self._fdt)):
94250c59522SSimon Glass            pass
9433def0cf2SSimon Glass
94450c59522SSimon Glass    def add_node(self, name):
9453def0cf2SSimon Glass        """Create a new context for adding a node
9463def0cf2SSimon Glass
9473def0cf2SSimon Glass        When used in a 'with' clause this starts a new node and finishes it
9483def0cf2SSimon Glass        afterward.
9493def0cf2SSimon Glass
9503def0cf2SSimon Glass        Args:
9513def0cf2SSimon Glass            name: Name of node to add
9523def0cf2SSimon Glass        """
95350c59522SSimon Glass        return NodeAdder(self, name)
9543def0cf2SSimon Glass
9553def0cf2SSimon Glass
9563def0cf2SSimon Glassclass NodeAdder():
9573def0cf2SSimon Glass    """Class to provide a node context
9583def0cf2SSimon Glass
9593def0cf2SSimon Glass    This allows you to add nodes in a more natural way:
9603def0cf2SSimon Glass
96150c59522SSimon Glass        with fdtsw.add_node('name'):
9623def0cf2SSimon Glass            fdtsw.property_string('test', 'value')
9633def0cf2SSimon Glass
9643def0cf2SSimon Glass    The node is automatically completed with a call to end_node() when the
9653def0cf2SSimon Glass    context exits.
9663def0cf2SSimon Glass    """
96750c59522SSimon Glass    def __init__(self, fdtsw, name):
96850c59522SSimon Glass        self._fdt = fdtsw
9693def0cf2SSimon Glass        self._name = name
9703def0cf2SSimon Glass
9713def0cf2SSimon Glass    def __enter__(self):
97250c59522SSimon Glass        self._fdt.begin_node(self._name)
9733def0cf2SSimon Glass
9743def0cf2SSimon Glass    def __exit__(self, type, value, traceback):
97550c59522SSimon Glass        self._fdt.end_node()
97615b97f5cSMasahiro Yamada%}
97715b97f5cSMasahiro Yamada
97815b97f5cSMasahiro Yamada%rename(fdt_property) fdt_property_func;
97915b97f5cSMasahiro Yamada
98050c59522SSimon Glass/*
98150c59522SSimon Glass * fdt32_t is a big-endian 32-bit value defined to uint32_t in libfdt_env.h
98250c59522SSimon Glass * so use the same type here.
98350c59522SSimon Glass */
98450c59522SSimon Glasstypedef uint32_t fdt32_t;
98515b97f5cSMasahiro Yamada
98615b97f5cSMasahiro Yamada%include "libfdt/fdt.h"
98715b97f5cSMasahiro Yamada
98815b97f5cSMasahiro Yamada%include "typemaps.i"
98915b97f5cSMasahiro Yamada
99015b97f5cSMasahiro Yamada/* Most functions don't change the device tree, so use a const void * */
99115b97f5cSMasahiro Yamada%typemap(in) (const void *)(const void *fdt) {
99215b97f5cSMasahiro Yamada	if (!PyByteArray_Check($input)) {
99315b97f5cSMasahiro Yamada		SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname"
99415b97f5cSMasahiro Yamada			"', argument " "$argnum"" of type '" "$type""'");
99515b97f5cSMasahiro Yamada	}
99615b97f5cSMasahiro Yamada	$1 = (void *)PyByteArray_AsString($input);
99715b97f5cSMasahiro Yamada        fdt = $1;
99815b97f5cSMasahiro Yamada        fdt = fdt; /* avoid unused variable warning */
99915b97f5cSMasahiro Yamada}
100015b97f5cSMasahiro Yamada
100115b97f5cSMasahiro Yamada/* Some functions do change the device tree, so use void * */
100215b97f5cSMasahiro Yamada%typemap(in) (void *)(const void *fdt) {
100315b97f5cSMasahiro Yamada	if (!PyByteArray_Check($input)) {
100415b97f5cSMasahiro Yamada		SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname"
100515b97f5cSMasahiro Yamada			"', argument " "$argnum"" of type '" "$type""'");
100615b97f5cSMasahiro Yamada	}
100715b97f5cSMasahiro Yamada	$1 = PyByteArray_AsString($input);
100815b97f5cSMasahiro Yamada        fdt = $1;
100915b97f5cSMasahiro Yamada        fdt = fdt; /* avoid unused variable warning */
101015b97f5cSMasahiro Yamada}
101115b97f5cSMasahiro Yamada
10123def0cf2SSimon Glass/* typemap used for fdt_get_property_by_offset() */
101315b97f5cSMasahiro Yamada%typemap(out) (struct fdt_property *) {
101415b97f5cSMasahiro Yamada	PyObject *buff;
101515b97f5cSMasahiro Yamada
101615b97f5cSMasahiro Yamada	if ($1) {
101715b97f5cSMasahiro Yamada		resultobj = PyString_FromString(
101815b97f5cSMasahiro Yamada			fdt_string(fdt1, fdt32_to_cpu($1->nameoff)));
101915b97f5cSMasahiro Yamada		buff = PyByteArray_FromStringAndSize(
102015b97f5cSMasahiro Yamada			(const char *)($1 + 1), fdt32_to_cpu($1->len));
102115b97f5cSMasahiro Yamada		resultobj = SWIG_Python_AppendOutput(resultobj, buff);
102215b97f5cSMasahiro Yamada	}
102315b97f5cSMasahiro Yamada}
102415b97f5cSMasahiro Yamada
102515b97f5cSMasahiro Yamada%apply int *OUTPUT { int *lenp };
102615b97f5cSMasahiro Yamada
102715b97f5cSMasahiro Yamada/* typemap used for fdt_getprop() */
102815b97f5cSMasahiro Yamada%typemap(out) (const void *) {
102915b97f5cSMasahiro Yamada	if (!$1)
103015b97f5cSMasahiro Yamada		$result = Py_None;
103115b97f5cSMasahiro Yamada	else
103215b97f5cSMasahiro Yamada		$result = Py_BuildValue("s#", $1, *arg4);
103315b97f5cSMasahiro Yamada}
103415b97f5cSMasahiro Yamada
10353def0cf2SSimon Glass/* typemap used for fdt_setprop() */
10363def0cf2SSimon Glass%typemap(in) (const void *val) {
10373def0cf2SSimon Glass    $1 = PyString_AsString($input);   /* char *str */
10383def0cf2SSimon Glass}
10393def0cf2SSimon Glass
10403def0cf2SSimon Glass/* typemap used for fdt_add_reservemap_entry() */
10413def0cf2SSimon Glass%typemap(in) uint64_t {
10423def0cf2SSimon Glass   $1 = PyLong_AsUnsignedLong($input);
10433def0cf2SSimon Glass}
10443def0cf2SSimon Glass
10453def0cf2SSimon Glass/* typemaps used for fdt_next_node() */
10463def0cf2SSimon Glass%typemap(in, numinputs=1) int *depth (int depth) {
10473def0cf2SSimon Glass   depth = (int) PyInt_AsLong($input);
10483def0cf2SSimon Glass   $1 = &depth;
10493def0cf2SSimon Glass}
10503def0cf2SSimon Glass
10513def0cf2SSimon Glass%typemap(argout) int *depth {
10523def0cf2SSimon Glass        PyObject *val = Py_BuildValue("i", *arg$argnum);
10533def0cf2SSimon Glass        resultobj = SWIG_Python_AppendOutput(resultobj, val);
10543def0cf2SSimon Glass}
10553def0cf2SSimon Glass
10563def0cf2SSimon Glass%apply int *depth { int *depth };
10573def0cf2SSimon Glass
10583def0cf2SSimon Glass/* typemaps for fdt_get_mem_rsv */
10593def0cf2SSimon Glass%typemap(in, numinputs=0) uint64_t * (uint64_t temp) {
10603def0cf2SSimon Glass   $1 = &temp;
10613def0cf2SSimon Glass}
10623def0cf2SSimon Glass
10633def0cf2SSimon Glass%typemap(argout) uint64_t * {
10643def0cf2SSimon Glass        PyObject *val = PyLong_FromUnsignedLong(*arg$argnum);
10653def0cf2SSimon Glass        if (!result) {
10663def0cf2SSimon Glass           if (PyTuple_GET_SIZE(resultobj) == 0)
10673def0cf2SSimon Glass              resultobj = val;
10683def0cf2SSimon Glass           else
10693def0cf2SSimon Glass              resultobj = SWIG_Python_AppendOutput(resultobj, val);
10703def0cf2SSimon Glass        }
10713def0cf2SSimon Glass}
10723def0cf2SSimon Glass
107315b97f5cSMasahiro Yamada/* We have both struct fdt_property and a function fdt_property() */
107415b97f5cSMasahiro Yamada%warnfilter(302) fdt_property;
107515b97f5cSMasahiro Yamada
107615b97f5cSMasahiro Yamada/* These are macros in the header so have to be redefined here */
107750c59522SSimon Glassuint32_t fdt_magic(const void *fdt);
107850c59522SSimon Glassuint32_t fdt_totalsize(const void *fdt);
107950c59522SSimon Glassuint32_t fdt_off_dt_struct(const void *fdt);
108050c59522SSimon Glassuint32_t fdt_off_dt_strings(const void *fdt);
108150c59522SSimon Glassuint32_t fdt_off_mem_rsvmap(const void *fdt);
108250c59522SSimon Glassuint32_t fdt_version(const void *fdt);
108350c59522SSimon Glassuint32_t fdt_last_comp_version(const void *fdt);
108450c59522SSimon Glassuint32_t fdt_boot_cpuid_phys(const void *fdt);
108550c59522SSimon Glassuint32_t fdt_size_dt_strings(const void *fdt);
108650c59522SSimon Glassuint32_t fdt_size_dt_struct(const void *fdt);
108750c59522SSimon Glass
10883def0cf2SSimon Glassint fdt_property_string(void *fdt, const char *name, const char *val);
10893def0cf2SSimon Glassint fdt_property_cell(void *fdt, const char *name, uint32_t val);
10903def0cf2SSimon Glass
10913def0cf2SSimon Glass/*
10923def0cf2SSimon Glass * This function has a stub since the name fdt_property is used for both a
10933def0cf2SSimon Glass  * function and a struct, which confuses SWIG.
10943def0cf2SSimon Glass */
109550c59522SSimon Glassint fdt_property_stub(void *fdt, const char *name, const char *val, int len);
109615b97f5cSMasahiro Yamada
109715b97f5cSMasahiro Yamada%include <../libfdt/libfdt.h>
1098