blob: 14f583dfbeea629fd59a3a15a57b9bc49ece923f [file] [log] [blame]
Simon Glassa2a6f042016-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
78const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
79int fdt_path_offset(const void *fdt, const char *path);
80int fdt_first_property_offset(const void *fdt, int nodeoffset);
81int fdt_next_property_offset(const void *fdt, int offset);
82const char *fdt_strerror(int errval);
83const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
84 int offset,
85 int *OUTPUT);
86const char *fdt_get_name(const void *fdt, int nodeoffset, int *OUTPUT);
87const char *fdt_string(const void *fdt, int stroffset);
88int fdt_first_subnode(const void *fdt, int offset);
89int fdt_next_subnode(const void *fdt, int offset);