libfdt.swig 3.0 KB

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