libfdt.swig 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
  64. int fdt_path_offset(const void *fdt, const char *path);
  65. int fdt_first_property_offset(const void *fdt, int nodeoffset);
  66. int fdt_next_property_offset(const void *fdt, int offset);
  67. const char *fdt_strerror(int errval);
  68. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  69. int offset,
  70. int *OUTPUT);
  71. const char *fdt_get_name(const void *fdt, int nodeoffset, int *OUTPUT);
  72. const char *fdt_string(const void *fdt, int stroffset);
  73. int fdt_first_subnode(const void *fdt, int offset);
  74. int fdt_next_subnode(const void *fdt, int offset);