blob: b24c72b1a22ba5d87224e6e2b5e922757d948d8b [file] [log] [blame]
Simon Glass76bce102016-07-04 11:58:11 -06001/* File: libfdt.i */
2%module libfdt
3
4%{
5#define SWIG_FILE_WITH_INIT
6#include "libfdt.h"
7%}
8
9%pythoncode %{
10def Raise(errnum):
11 raise ValueError('Error %s' % fdt_strerror(errnum))
12
13def Name(fdt, offset):
14 name, len = fdt_get_name(fdt, offset)
15 return name
16
17def String(fdt, offset):
18 offset = fdt32_to_cpu(offset)
19 name = fdt_string(fdt, offset)
20 return name
21
22def swap32(x):
23 return (((x << 24) & 0xFF000000) |
24 ((x << 8) & 0x00FF0000) |
25 ((x >> 8) & 0x0000FF00) |
26 ((x >> 24) & 0x000000FF))
27
28def fdt32_to_cpu(x):
29 return swap32(x)
30
31def Data(prop):
32 set_prop(prop)
33 return get_prop_data()
34%}
35
36%include "typemaps.i"
37%include "cstring.i"
38
39%typemap(in) void* = char*;
40
41typedef int fdt32_t;
42
43struct fdt_property {
44 fdt32_t tag;
45 fdt32_t len;
46 fdt32_t nameoff;
47 char data[0];
48};
49
50/*
51 * This is a work-around since I'm not sure of a better way to copy out the
52 * contents of a string. This is used in dtoc/GetProps(). The intent is to
53 * pass in a pointer to a property and access the data field at the end of
54 * it. Ideally the Data() function above would be able to do this directly,
55 * but I'm not sure how to do that.
56 */
57#pragma SWIG nowarn=454
58%inline %{
59 static struct fdt_property *cur_prop;
60
61 void set_prop(struct fdt_property *prop) {
62 cur_prop = prop;
63 }
64%}
65
66%cstring_output_allocate_size(char **s, int *sz, free(*$1));
67%inline %{
68 void get_prop_data(char **s, int *sz) {
69 *sz = fdt32_to_cpu(cur_prop->len);
70 *s = (char *)malloc(*sz);
71 if (!*s)
72 *sz = 0;
73 else
74 memcpy(*s, cur_prop + 1, *sz);
75 }
76%}
77
Simon Glass01708042016-07-25 18:59:13 -060078%typemap(in) (const void *) {
79 if (!PyByteArray_Check($input)) {
80 SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname" "', argument "
81 "$argnum"" of type '" "$type""'");
82 }
83 $1 = (void *) PyByteArray_AsString($input);
84}
85
Simon Glass76bce102016-07-04 11:58:11 -060086const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
87int fdt_path_offset(const void *fdt, const char *path);
88int fdt_first_property_offset(const void *fdt, int nodeoffset);
89int fdt_next_property_offset(const void *fdt, int offset);
90const char *fdt_strerror(int errval);
91const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
92 int offset,
93 int *OUTPUT);
94const char *fdt_get_name(const void *fdt, int nodeoffset, int *OUTPUT);
95const char *fdt_string(const void *fdt, int stroffset);
96int fdt_first_subnode(const void *fdt, int offset);
97int fdt_next_subnode(const void *fdt, int offset);
Simon Glass2a70d892016-07-25 18:59:14 -060098
99%typemap(in) (void *) {
100 if (!PyByteArray_Check($input)) {
101 SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname" "', argument "
102 "$argnum"" of type '" "$type""'");
103 }
104 $1 = PyByteArray_AsString($input);
105}
106
107int fdt_delprop(void *fdt, int nodeoffset, const char *name);
108
109const char *fdt_strerror(int errval);
Simon Glassda5f7492016-07-25 18:59:15 -0600110int fdt_pack(void *fdt);
Simon Glassbabdbde2016-07-25 18:59:16 -0600111
112int fdt_totalsize(const void *fdt);
113int fdt_off_dt_struct(const void *fdt);