xref: /rk3399_rockchip-uboot/scripts/dtc/pylibfdt/libfdt.i_shipped (revision 815366520a3bc6d29a5aa94d316f2c3a396fd2df)
1*81536652SMasahiro Yamada/*
2*81536652SMasahiro Yamada * pylibfdt - Flat Device Tree manipulation in Python
3*81536652SMasahiro Yamada * Copyright (C) 2017 Google, Inc.
4*81536652SMasahiro Yamada * Written by Simon Glass <sjg@chromium.org>
5*81536652SMasahiro Yamada *
6*81536652SMasahiro Yamada * SPDX-License-Identifier:	GPL-2.0+ BSD-2-Clause
7*81536652SMasahiro Yamada */
8*81536652SMasahiro Yamada
9*81536652SMasahiro Yamada%module libfdt
10*81536652SMasahiro Yamada
11*81536652SMasahiro Yamada%include <stdint.i>
12*81536652SMasahiro Yamada
13*81536652SMasahiro Yamada%{
14*81536652SMasahiro Yamada#define SWIG_FILE_WITH_INIT
15*81536652SMasahiro Yamada#include "libfdt.h"
16*81536652SMasahiro Yamada%}
17*81536652SMasahiro Yamada
18*81536652SMasahiro Yamada%pythoncode %{
19*81536652SMasahiro Yamada
20*81536652SMasahiro Yamadaimport struct
21*81536652SMasahiro Yamada
22*81536652SMasahiro Yamada# Error codes, corresponding to FDT_ERR_... in libfdt.h
23*81536652SMasahiro Yamada(NOTFOUND,
24*81536652SMasahiro Yamada        EXISTS,
25*81536652SMasahiro Yamada        NOSPACE,
26*81536652SMasahiro Yamada        BADOFFSET,
27*81536652SMasahiro Yamada        BADPATH,
28*81536652SMasahiro Yamada        BADPHANDLE,
29*81536652SMasahiro Yamada        BADSTATE,
30*81536652SMasahiro Yamada        TRUNCATED,
31*81536652SMasahiro Yamada        BADMAGIC,
32*81536652SMasahiro Yamada        BADVERSION,
33*81536652SMasahiro Yamada        BADSTRUCTURE,
34*81536652SMasahiro Yamada        BADLAYOUT,
35*81536652SMasahiro Yamada        INTERNAL,
36*81536652SMasahiro Yamada        BADNCELLS,
37*81536652SMasahiro Yamada        BADVALUE,
38*81536652SMasahiro Yamada        BADOVERLAY,
39*81536652SMasahiro Yamada        NOPHANDLES) = QUIET_ALL = range(1, 18)
40*81536652SMasahiro Yamada# QUIET_ALL can be passed as the 'quiet' parameter to avoid exceptions
41*81536652SMasahiro Yamada# altogether. All # functions passed this value will return an error instead
42*81536652SMasahiro Yamada# of raising an exception.
43*81536652SMasahiro Yamada
44*81536652SMasahiro Yamada# Pass this as the 'quiet' parameter to return -ENOTFOUND on NOTFOUND errors,
45*81536652SMasahiro Yamada# instead of raising an exception.
46*81536652SMasahiro YamadaQUIET_NOTFOUND = (NOTFOUND,)
47*81536652SMasahiro Yamada
48*81536652SMasahiro Yamada
49*81536652SMasahiro Yamadaclass FdtException(Exception):
50*81536652SMasahiro Yamada    """An exception caused by an error such as one of the codes above"""
51*81536652SMasahiro Yamada    def __init__(self, err):
52*81536652SMasahiro Yamada        self.err = err
53*81536652SMasahiro Yamada
54*81536652SMasahiro Yamada    def __str__(self):
55*81536652SMasahiro Yamada        return 'pylibfdt error %d: %s' % (self.err, fdt_strerror(self.err))
56*81536652SMasahiro Yamada
57*81536652SMasahiro Yamadadef strerror(fdt_err):
58*81536652SMasahiro Yamada    """Get the string for an error number
59*81536652SMasahiro Yamada
60*81536652SMasahiro Yamada    Args:
61*81536652SMasahiro Yamada        fdt_err: Error number (-ve)
62*81536652SMasahiro Yamada
63*81536652SMasahiro Yamada    Returns:
64*81536652SMasahiro Yamada        String containing the associated error
65*81536652SMasahiro Yamada    """
66*81536652SMasahiro Yamada    return fdt_strerror(fdt_err)
67*81536652SMasahiro Yamada
68*81536652SMasahiro Yamadadef check_err(val, quiet=()):
69*81536652SMasahiro Yamada    """Raise an error if the return value is -ve
70*81536652SMasahiro Yamada
71*81536652SMasahiro Yamada    This is used to check for errors returned by libfdt C functions.
72*81536652SMasahiro Yamada
73*81536652SMasahiro Yamada    Args:
74*81536652SMasahiro Yamada        val: Return value from a libfdt function
75*81536652SMasahiro Yamada        quiet: Errors to ignore (empty to raise on all errors)
76*81536652SMasahiro Yamada
77*81536652SMasahiro Yamada    Returns:
78*81536652SMasahiro Yamada        val if val >= 0
79*81536652SMasahiro Yamada
80*81536652SMasahiro Yamada    Raises
81*81536652SMasahiro Yamada        FdtException if val < 0
82*81536652SMasahiro Yamada    """
83*81536652SMasahiro Yamada    if val < 0:
84*81536652SMasahiro Yamada        if -val not in quiet:
85*81536652SMasahiro Yamada            raise FdtException(val)
86*81536652SMasahiro Yamada    return val
87*81536652SMasahiro Yamada
88*81536652SMasahiro Yamadadef check_err_null(val, quiet=()):
89*81536652SMasahiro Yamada    """Raise an error if the return value is NULL
90*81536652SMasahiro Yamada
91*81536652SMasahiro Yamada    This is used to check for a NULL return value from certain libfdt C
92*81536652SMasahiro Yamada    functions
93*81536652SMasahiro Yamada
94*81536652SMasahiro Yamada    Args:
95*81536652SMasahiro Yamada        val: Return value from a libfdt function
96*81536652SMasahiro Yamada        quiet: Errors to ignore (empty to raise on all errors)
97*81536652SMasahiro Yamada
98*81536652SMasahiro Yamada    Returns:
99*81536652SMasahiro Yamada        val if val is a list, None if not
100*81536652SMasahiro Yamada
101*81536652SMasahiro Yamada    Raises
102*81536652SMasahiro Yamada        FdtException if val indicates an error was reported and the error
103*81536652SMasahiro Yamada        is not in @quiet.
104*81536652SMasahiro Yamada    """
105*81536652SMasahiro Yamada    # Normally a list is returned which contains the data and its length.
106*81536652SMasahiro Yamada    # If we get just an integer error code, it means the function failed.
107*81536652SMasahiro Yamada    if not isinstance(val, list):
108*81536652SMasahiro Yamada        if -val not in quiet:
109*81536652SMasahiro Yamada            raise FdtException(val)
110*81536652SMasahiro Yamada    return val
111*81536652SMasahiro Yamada
112*81536652SMasahiro Yamadaclass Fdt:
113*81536652SMasahiro Yamada    """Device tree class, supporting all operations
114*81536652SMasahiro Yamada
115*81536652SMasahiro Yamada    The Fdt object is created is created from a device tree binary file,
116*81536652SMasahiro Yamada    e.g. with something like:
117*81536652SMasahiro Yamada
118*81536652SMasahiro Yamada       fdt = Fdt(open("filename.dtb").read())
119*81536652SMasahiro Yamada
120*81536652SMasahiro Yamada    Operations can then be performed using the methods in this class. Each
121*81536652SMasahiro Yamada    method xxx(args...) corresponds to a libfdt function fdt_xxx(fdt, args...).
122*81536652SMasahiro Yamada
123*81536652SMasahiro Yamada    All methods raise an FdtException if an error occurs. To avoid this
124*81536652SMasahiro Yamada    behaviour a 'quiet' parameter is provided for some functions. This
125*81536652SMasahiro Yamada    defaults to empty, but you can pass a list of errors that you expect.
126*81536652SMasahiro Yamada    If one of these errors occurs, the function will return an error number
127*81536652SMasahiro Yamada    (e.g. -NOTFOUND).
128*81536652SMasahiro Yamada    """
129*81536652SMasahiro Yamada    def __init__(self, data):
130*81536652SMasahiro Yamada        self._fdt = bytearray(data)
131*81536652SMasahiro Yamada        check_err(fdt_check_header(self._fdt));
132*81536652SMasahiro Yamada
133*81536652SMasahiro Yamada    def subnode_offset(self, parentoffset, name, quiet=()):
134*81536652SMasahiro Yamada        """Get the offset of a named subnode
135*81536652SMasahiro Yamada
136*81536652SMasahiro Yamada        Args:
137*81536652SMasahiro Yamada            parentoffset: Offset of the parent node to check
138*81536652SMasahiro Yamada            name: Name of the required subnode, e.g. 'subnode@1'
139*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
140*81536652SMasahiro Yamada
141*81536652SMasahiro Yamada        Returns:
142*81536652SMasahiro Yamada            The node offset of the found node, if any
143*81536652SMasahiro Yamada
144*81536652SMasahiro Yamada        Raises
145*81536652SMasahiro Yamada            FdtException if there is no node with that name, or other error
146*81536652SMasahiro Yamada        """
147*81536652SMasahiro Yamada        return check_err(fdt_subnode_offset(self._fdt, parentoffset, name),
148*81536652SMasahiro Yamada                         quiet)
149*81536652SMasahiro Yamada
150*81536652SMasahiro Yamada    def path_offset(self, path, quiet=()):
151*81536652SMasahiro Yamada        """Get the offset for a given path
152*81536652SMasahiro Yamada
153*81536652SMasahiro Yamada        Args:
154*81536652SMasahiro Yamada            path: Path to the required node, e.g. '/node@3/subnode@1'
155*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
156*81536652SMasahiro Yamada
157*81536652SMasahiro Yamada        Returns:
158*81536652SMasahiro Yamada            Node offset
159*81536652SMasahiro Yamada
160*81536652SMasahiro Yamada        Raises
161*81536652SMasahiro Yamada            FdtException if the path is not valid or not found
162*81536652SMasahiro Yamada        """
163*81536652SMasahiro Yamada        return check_err(fdt_path_offset(self._fdt, path), quiet)
164*81536652SMasahiro Yamada
165*81536652SMasahiro Yamada    def first_property_offset(self, nodeoffset, quiet=()):
166*81536652SMasahiro Yamada        """Get the offset of the first property in a node offset
167*81536652SMasahiro Yamada
168*81536652SMasahiro Yamada        Args:
169*81536652SMasahiro Yamada            nodeoffset: Offset to the node to check
170*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
171*81536652SMasahiro Yamada
172*81536652SMasahiro Yamada        Returns:
173*81536652SMasahiro Yamada            Offset of the first property
174*81536652SMasahiro Yamada
175*81536652SMasahiro Yamada        Raises
176*81536652SMasahiro Yamada            FdtException if the associated node has no properties, or some
177*81536652SMasahiro Yamada                other error occurred
178*81536652SMasahiro Yamada        """
179*81536652SMasahiro Yamada        return check_err(fdt_first_property_offset(self._fdt, nodeoffset),
180*81536652SMasahiro Yamada                         quiet)
181*81536652SMasahiro Yamada
182*81536652SMasahiro Yamada    def next_property_offset(self, prop_offset, quiet=()):
183*81536652SMasahiro Yamada        """Get the next property in a node
184*81536652SMasahiro Yamada
185*81536652SMasahiro Yamada        Args:
186*81536652SMasahiro Yamada            prop_offset: Offset of the previous property
187*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
188*81536652SMasahiro Yamada
189*81536652SMasahiro Yamada        Returns:
190*81536652SMasahiro Yamada            Offset of the next property
191*81536652SMasahiro Yamada
192*81536652SMasahiro Yamada        Raises:
193*81536652SMasahiro Yamada            FdtException if the associated node has no more properties, or
194*81536652SMasahiro Yamada                some other error occurred
195*81536652SMasahiro Yamada        """
196*81536652SMasahiro Yamada        return check_err(fdt_next_property_offset(self._fdt, prop_offset),
197*81536652SMasahiro Yamada                         quiet)
198*81536652SMasahiro Yamada
199*81536652SMasahiro Yamada    def get_name(self, nodeoffset):
200*81536652SMasahiro Yamada        """Get the name of a node
201*81536652SMasahiro Yamada
202*81536652SMasahiro Yamada        Args:
203*81536652SMasahiro Yamada            nodeoffset: Offset of node to check
204*81536652SMasahiro Yamada
205*81536652SMasahiro Yamada        Returns:
206*81536652SMasahiro Yamada            Node name
207*81536652SMasahiro Yamada
208*81536652SMasahiro Yamada        Raises:
209*81536652SMasahiro Yamada            FdtException on error (e.g. nodeoffset is invalid)
210*81536652SMasahiro Yamada        """
211*81536652SMasahiro Yamada        return check_err_null(fdt_get_name(self._fdt, nodeoffset))[0]
212*81536652SMasahiro Yamada
213*81536652SMasahiro Yamada    def get_property_by_offset(self, prop_offset, quiet=()):
214*81536652SMasahiro Yamada        """Obtains a property that can be examined
215*81536652SMasahiro Yamada
216*81536652SMasahiro Yamada        Args:
217*81536652SMasahiro Yamada            prop_offset: Offset of property (e.g. from first_property_offset())
218*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
219*81536652SMasahiro Yamada
220*81536652SMasahiro Yamada        Returns:
221*81536652SMasahiro Yamada            Property object, or None if not found
222*81536652SMasahiro Yamada
223*81536652SMasahiro Yamada        Raises:
224*81536652SMasahiro Yamada            FdtException on error (e.g. invalid prop_offset or device
225*81536652SMasahiro Yamada            tree format)
226*81536652SMasahiro Yamada        """
227*81536652SMasahiro Yamada        pdata = check_err_null(
228*81536652SMasahiro Yamada                fdt_get_property_by_offset(self._fdt, prop_offset), quiet)
229*81536652SMasahiro Yamada        if isinstance(pdata, (int)):
230*81536652SMasahiro Yamada            return pdata
231*81536652SMasahiro Yamada        return Property(pdata[0], pdata[1])
232*81536652SMasahiro Yamada
233*81536652SMasahiro Yamada    def first_subnode(self, nodeoffset, quiet=()):
234*81536652SMasahiro Yamada        """Find the first subnode of a parent node
235*81536652SMasahiro Yamada
236*81536652SMasahiro Yamada        Args:
237*81536652SMasahiro Yamada            nodeoffset: Node offset of parent node
238*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
239*81536652SMasahiro Yamada
240*81536652SMasahiro Yamada        Returns:
241*81536652SMasahiro Yamada            The offset of the first subnode, if any
242*81536652SMasahiro Yamada
243*81536652SMasahiro Yamada        Raises:
244*81536652SMasahiro Yamada            FdtException if no subnode found or other error occurs
245*81536652SMasahiro Yamada        """
246*81536652SMasahiro Yamada        return check_err(fdt_first_subnode(self._fdt, nodeoffset), quiet)
247*81536652SMasahiro Yamada
248*81536652SMasahiro Yamada    def next_subnode(self, nodeoffset, quiet=()):
249*81536652SMasahiro Yamada        """Find the next subnode
250*81536652SMasahiro Yamada
251*81536652SMasahiro Yamada        Args:
252*81536652SMasahiro Yamada            nodeoffset: Node offset of previous subnode
253*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
254*81536652SMasahiro Yamada
255*81536652SMasahiro Yamada        Returns:
256*81536652SMasahiro Yamada            The offset of the next subnode, if any
257*81536652SMasahiro Yamada
258*81536652SMasahiro Yamada        Raises:
259*81536652SMasahiro Yamada            FdtException if no more subnode found or other error occurs
260*81536652SMasahiro Yamada        """
261*81536652SMasahiro Yamada        return check_err(fdt_next_subnode(self._fdt, nodeoffset), quiet)
262*81536652SMasahiro Yamada
263*81536652SMasahiro Yamada    def totalsize(self):
264*81536652SMasahiro Yamada        """Return the total size of the device tree
265*81536652SMasahiro Yamada
266*81536652SMasahiro Yamada        Returns:
267*81536652SMasahiro Yamada            Total tree size in bytes
268*81536652SMasahiro Yamada        """
269*81536652SMasahiro Yamada        return check_err(fdt_totalsize(self._fdt))
270*81536652SMasahiro Yamada
271*81536652SMasahiro Yamada    def off_dt_struct(self):
272*81536652SMasahiro Yamada        """Return the start of the device tree struct area
273*81536652SMasahiro Yamada
274*81536652SMasahiro Yamada        Returns:
275*81536652SMasahiro Yamada            Start offset of struct area
276*81536652SMasahiro Yamada        """
277*81536652SMasahiro Yamada        return check_err(fdt_off_dt_struct(self._fdt))
278*81536652SMasahiro Yamada
279*81536652SMasahiro Yamada    def pack(self, quiet=()):
280*81536652SMasahiro Yamada        """Pack the device tree to remove unused space
281*81536652SMasahiro Yamada
282*81536652SMasahiro Yamada        This adjusts the tree in place.
283*81536652SMasahiro Yamada
284*81536652SMasahiro Yamada        Args:
285*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
286*81536652SMasahiro Yamada
287*81536652SMasahiro Yamada        Raises:
288*81536652SMasahiro Yamada            FdtException if any error occurs
289*81536652SMasahiro Yamada        """
290*81536652SMasahiro Yamada        return check_err(fdt_pack(self._fdt), quiet)
291*81536652SMasahiro Yamada
292*81536652SMasahiro Yamada    def delprop(self, nodeoffset, prop_name):
293*81536652SMasahiro Yamada        """Delete a property from a node
294*81536652SMasahiro Yamada
295*81536652SMasahiro Yamada        Args:
296*81536652SMasahiro Yamada            nodeoffset: Node offset containing property to delete
297*81536652SMasahiro Yamada            prop_name: Name of property to delete
298*81536652SMasahiro Yamada
299*81536652SMasahiro Yamada        Raises:
300*81536652SMasahiro Yamada            FdtError if the property does not exist, or another error occurs
301*81536652SMasahiro Yamada        """
302*81536652SMasahiro Yamada        return check_err(fdt_delprop(self._fdt, nodeoffset, prop_name))
303*81536652SMasahiro Yamada
304*81536652SMasahiro Yamada    def getprop(self, nodeoffset, prop_name, quiet=()):
305*81536652SMasahiro Yamada        """Get a property from a node
306*81536652SMasahiro Yamada
307*81536652SMasahiro Yamada        Args:
308*81536652SMasahiro Yamada            nodeoffset: Node offset containing property to get
309*81536652SMasahiro Yamada            prop_name: Name of property to get
310*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
311*81536652SMasahiro Yamada
312*81536652SMasahiro Yamada        Returns:
313*81536652SMasahiro Yamada            Value of property as a bytearray, or -ve error number
314*81536652SMasahiro Yamada
315*81536652SMasahiro Yamada        Raises:
316*81536652SMasahiro Yamada            FdtError if any error occurs (e.g. the property is not found)
317*81536652SMasahiro Yamada        """
318*81536652SMasahiro Yamada        pdata = check_err_null(fdt_getprop(self._fdt, nodeoffset, prop_name),
319*81536652SMasahiro Yamada                               quiet)
320*81536652SMasahiro Yamada        if isinstance(pdata, (int)):
321*81536652SMasahiro Yamada            return pdata
322*81536652SMasahiro Yamada        return bytearray(pdata[0])
323*81536652SMasahiro Yamada
324*81536652SMasahiro Yamada    def get_phandle(self, nodeoffset):
325*81536652SMasahiro Yamada        """Get the phandle of a node
326*81536652SMasahiro Yamada
327*81536652SMasahiro Yamada        Args:
328*81536652SMasahiro Yamada            nodeoffset: Node offset to check
329*81536652SMasahiro Yamada
330*81536652SMasahiro Yamada        Returns:
331*81536652SMasahiro Yamada            phandle of node, or 0 if the node has no phandle or another error
332*81536652SMasahiro Yamada            occurs
333*81536652SMasahiro Yamada        """
334*81536652SMasahiro Yamada        return fdt_get_phandle(self._fdt, nodeoffset)
335*81536652SMasahiro Yamada
336*81536652SMasahiro Yamada    def parent_offset(self, nodeoffset, quiet=()):
337*81536652SMasahiro Yamada        """Get the offset of a node's parent
338*81536652SMasahiro Yamada
339*81536652SMasahiro Yamada        Args:
340*81536652SMasahiro Yamada            nodeoffset: Node offset to check
341*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
342*81536652SMasahiro Yamada
343*81536652SMasahiro Yamada        Returns:
344*81536652SMasahiro Yamada            The offset of the parent node, if any
345*81536652SMasahiro Yamada
346*81536652SMasahiro Yamada        Raises:
347*81536652SMasahiro Yamada            FdtException if no parent found or other error occurs
348*81536652SMasahiro Yamada        """
349*81536652SMasahiro Yamada        return check_err(fdt_parent_offset(self._fdt, nodeoffset), quiet)
350*81536652SMasahiro Yamada
351*81536652SMasahiro Yamada    def node_offset_by_phandle(self, phandle, quiet=()):
352*81536652SMasahiro Yamada        """Get the offset of a node with the given phandle
353*81536652SMasahiro Yamada
354*81536652SMasahiro Yamada        Args:
355*81536652SMasahiro Yamada            phandle: Phandle to search for
356*81536652SMasahiro Yamada            quiet: Errors to ignore (empty to raise on all errors)
357*81536652SMasahiro Yamada
358*81536652SMasahiro Yamada        Returns:
359*81536652SMasahiro Yamada            The offset of node with that phandle, if any
360*81536652SMasahiro Yamada
361*81536652SMasahiro Yamada        Raises:
362*81536652SMasahiro Yamada            FdtException if no node found or other error occurs
363*81536652SMasahiro Yamada        """
364*81536652SMasahiro Yamada        return check_err(fdt_node_offset_by_phandle(self._fdt, phandle), quiet)
365*81536652SMasahiro Yamada
366*81536652SMasahiro Yamadaclass Property:
367*81536652SMasahiro Yamada    """Holds a device tree property name and value.
368*81536652SMasahiro Yamada
369*81536652SMasahiro Yamada    This holds a copy of a property taken from the device tree. It does not
370*81536652SMasahiro Yamada    reference the device tree, so if anything changes in the device tree,
371*81536652SMasahiro Yamada    a Property object will remain valid.
372*81536652SMasahiro Yamada
373*81536652SMasahiro Yamada    Properties:
374*81536652SMasahiro Yamada        name: Property name
375*81536652SMasahiro Yamada        value: Proper value as a bytearray
376*81536652SMasahiro Yamada    """
377*81536652SMasahiro Yamada    def __init__(self, name, value):
378*81536652SMasahiro Yamada        self.name = name
379*81536652SMasahiro Yamada        self.value = value
380*81536652SMasahiro Yamada%}
381*81536652SMasahiro Yamada
382*81536652SMasahiro Yamada%rename(fdt_property) fdt_property_func;
383*81536652SMasahiro Yamada
384*81536652SMasahiro Yamadatypedef int fdt32_t;
385*81536652SMasahiro Yamada
386*81536652SMasahiro Yamada%include "libfdt/fdt.h"
387*81536652SMasahiro Yamada
388*81536652SMasahiro Yamada%include "typemaps.i"
389*81536652SMasahiro Yamada
390*81536652SMasahiro Yamada/* Most functions don't change the device tree, so use a const void * */
391*81536652SMasahiro Yamada%typemap(in) (const void *)(const void *fdt) {
392*81536652SMasahiro Yamada	if (!PyByteArray_Check($input)) {
393*81536652SMasahiro Yamada		SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname"
394*81536652SMasahiro Yamada			"', argument " "$argnum"" of type '" "$type""'");
395*81536652SMasahiro Yamada	}
396*81536652SMasahiro Yamada	$1 = (void *)PyByteArray_AsString($input);
397*81536652SMasahiro Yamada        fdt = $1;
398*81536652SMasahiro Yamada        fdt = fdt; /* avoid unused variable warning */
399*81536652SMasahiro Yamada}
400*81536652SMasahiro Yamada
401*81536652SMasahiro Yamada/* Some functions do change the device tree, so use void * */
402*81536652SMasahiro Yamada%typemap(in) (void *)(const void *fdt) {
403*81536652SMasahiro Yamada	if (!PyByteArray_Check($input)) {
404*81536652SMasahiro Yamada		SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname"
405*81536652SMasahiro Yamada			"', argument " "$argnum"" of type '" "$type""'");
406*81536652SMasahiro Yamada	}
407*81536652SMasahiro Yamada	$1 = PyByteArray_AsString($input);
408*81536652SMasahiro Yamada        fdt = $1;
409*81536652SMasahiro Yamada        fdt = fdt; /* avoid unused variable warning */
410*81536652SMasahiro Yamada}
411*81536652SMasahiro Yamada
412*81536652SMasahiro Yamada%typemap(out) (struct fdt_property *) {
413*81536652SMasahiro Yamada	PyObject *buff;
414*81536652SMasahiro Yamada
415*81536652SMasahiro Yamada	if ($1) {
416*81536652SMasahiro Yamada		resultobj = PyString_FromString(
417*81536652SMasahiro Yamada			fdt_string(fdt1, fdt32_to_cpu($1->nameoff)));
418*81536652SMasahiro Yamada		buff = PyByteArray_FromStringAndSize(
419*81536652SMasahiro Yamada			(const char *)($1 + 1), fdt32_to_cpu($1->len));
420*81536652SMasahiro Yamada		resultobj = SWIG_Python_AppendOutput(resultobj, buff);
421*81536652SMasahiro Yamada	}
422*81536652SMasahiro Yamada}
423*81536652SMasahiro Yamada
424*81536652SMasahiro Yamada%apply int *OUTPUT { int *lenp };
425*81536652SMasahiro Yamada
426*81536652SMasahiro Yamada/* typemap used for fdt_getprop() */
427*81536652SMasahiro Yamada%typemap(out) (const void *) {
428*81536652SMasahiro Yamada	if (!$1)
429*81536652SMasahiro Yamada		$result = Py_None;
430*81536652SMasahiro Yamada	else
431*81536652SMasahiro Yamada		$result = Py_BuildValue("s#", $1, *arg4);
432*81536652SMasahiro Yamada}
433*81536652SMasahiro Yamada
434*81536652SMasahiro Yamada/* We have both struct fdt_property and a function fdt_property() */
435*81536652SMasahiro Yamada%warnfilter(302) fdt_property;
436*81536652SMasahiro Yamada
437*81536652SMasahiro Yamada/* These are macros in the header so have to be redefined here */
438*81536652SMasahiro Yamadaint fdt_magic(const void *fdt);
439*81536652SMasahiro Yamadaint fdt_totalsize(const void *fdt);
440*81536652SMasahiro Yamadaint fdt_off_dt_struct(const void *fdt);
441*81536652SMasahiro Yamadaint fdt_off_dt_strings(const void *fdt);
442*81536652SMasahiro Yamadaint fdt_off_mem_rsvmap(const void *fdt);
443*81536652SMasahiro Yamadaint fdt_version(const void *fdt);
444*81536652SMasahiro Yamadaint fdt_last_comp_version(const void *fdt);
445*81536652SMasahiro Yamadaint fdt_boot_cpuid_phys(const void *fdt);
446*81536652SMasahiro Yamadaint fdt_size_dt_strings(const void *fdt);
447*81536652SMasahiro Yamadaint fdt_size_dt_struct(const void *fdt);
448*81536652SMasahiro Yamada
449*81536652SMasahiro Yamada%include <../libfdt/libfdt.h>
450