Simon Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 1 | /* File: libfdt.i */ |
| 2 | %module libfdt |
| 3 | |
| 4 | %{ |
| 5 | #define SWIG_FILE_WITH_INIT |
| 6 | #include "libfdt.h" |
| 7 | %} |
| 8 | |
| 9 | %pythoncode %{ |
| 10 | def Raise(errnum): |
| 11 | raise ValueError('Error %s' % fdt_strerror(errnum)) |
| 12 | |
| 13 | def Name(fdt, offset): |
| 14 | name, len = fdt_get_name(fdt, offset) |
| 15 | return name |
| 16 | |
| 17 | def String(fdt, offset): |
| 18 | offset = fdt32_to_cpu(offset) |
| 19 | name = fdt_string(fdt, offset) |
| 20 | return name |
| 21 | |
| 22 | def swap32(x): |
| 23 | return (((x << 24) & 0xFF000000) | |
| 24 | ((x << 8) & 0x00FF0000) | |
| 25 | ((x >> 8) & 0x0000FF00) | |
| 26 | ((x >> 24) & 0x000000FF)) |
| 27 | |
| 28 | def fdt32_to_cpu(x): |
| 29 | return swap32(x) |
| 30 | |
| 31 | def 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 | |
| 41 | typedef int fdt32_t; |
| 42 | |
| 43 | struct 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 Glass | a930ecc | 2016-07-25 18:59:13 -0600 | [diff] [blame] | 78 | %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 Glass | a2a6f04 | 2016-07-04 11:58:11 -0600 | [diff] [blame] | 86 | const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen); |
| 87 | int fdt_path_offset(const void *fdt, const char *path); |
| 88 | int fdt_first_property_offset(const void *fdt, int nodeoffset); |
| 89 | int fdt_next_property_offset(const void *fdt, int offset); |
| 90 | const char *fdt_strerror(int errval); |
| 91 | const struct fdt_property *fdt_get_property_by_offset(const void *fdt, |
| 92 | int offset, |
| 93 | int *OUTPUT); |
| 94 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *OUTPUT); |
| 95 | const char *fdt_string(const void *fdt, int stroffset); |
| 96 | int fdt_first_subnode(const void *fdt, int offset); |
| 97 | int fdt_next_subnode(const void *fdt, int offset); |
Simon Glass | c719e42 | 2016-07-25 18:59:14 -0600 | [diff] [blame] | 98 | |
| 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 | |
| 107 | int fdt_delprop(void *fdt, int nodeoffset, const char *name); |
| 108 | |
| 109 | const char *fdt_strerror(int errval); |
Simon Glass | 32d9827 | 2016-07-25 18:59:15 -0600 | [diff] [blame] | 110 | int fdt_pack(void *fdt); |
Simon Glass | 54cec6d | 2016-07-25 18:59:16 -0600 | [diff] [blame^] | 111 | |
| 112 | int fdt_totalsize(const void *fdt); |
| 113 | int fdt_off_dt_struct(const void *fdt); |