libfdt-wrapper.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * This file does the necessary interface mapping between the bootwrapper
  3. * device tree operations and the interface provided by shared source
  4. * files flatdevicetree.[ch].
  5. *
  6. * Copyright 2007 David Gibson, IBM Corporation.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. */
  23. #include <stddef.h>
  24. #include <stdio.h>
  25. #include <page.h>
  26. #include <libfdt.h>
  27. #include "ops.h"
  28. #define DEBUG 0
  29. #define BAD_ERROR(err) (((err) < 0) \
  30. && ((err) != -FDT_ERR_NOTFOUND) \
  31. && ((err) != -FDT_ERR_EXISTS))
  32. #define check_err(err) \
  33. ({ \
  34. if (BAD_ERROR(err) || ((err < 0) && DEBUG)) \
  35. printf("%s():%d %s\n\r", __func__, __LINE__, \
  36. fdt_strerror(err)); \
  37. if (BAD_ERROR(err)) \
  38. exit(); \
  39. (err < 0) ? -1 : 0; \
  40. })
  41. #define offset_devp(off) \
  42. ({ \
  43. unsigned long _offset = (off); \
  44. check_err(_offset) ? NULL : (void *)(_offset+1); \
  45. })
  46. #define devp_offset_find(devp) (((unsigned long)(devp))-1)
  47. #define devp_offset(devp) (devp ? ((unsigned long)(devp))-1 : 0)
  48. static void *fdt;
  49. static void *buf; /* = NULL */
  50. #define EXPAND_GRANULARITY 1024
  51. static void expand_buf(int minexpand)
  52. {
  53. int size = fdt_totalsize(fdt);
  54. int rc;
  55. size = _ALIGN(size + minexpand, EXPAND_GRANULARITY);
  56. buf = platform_ops.realloc(buf, size);
  57. if (!buf)
  58. fatal("Couldn't find %d bytes to expand device tree\n\r", size);
  59. rc = fdt_open_into(fdt, buf, size);
  60. if (rc != 0)
  61. fatal("Couldn't expand fdt into new buffer: %s\n\r",
  62. fdt_strerror(rc));
  63. fdt = buf;
  64. }
  65. static void *fdt_wrapper_finddevice(const char *path)
  66. {
  67. return offset_devp(fdt_path_offset(fdt, path));
  68. }
  69. static int fdt_wrapper_getprop(const void *devp, const char *name,
  70. void *buf, const int buflen)
  71. {
  72. const void *p;
  73. int len;
  74. p = fdt_getprop(fdt, devp_offset(devp), name, &len);
  75. if (!p)
  76. return check_err(len);
  77. memcpy(buf, p, min(len, buflen));
  78. return len;
  79. }
  80. static int fdt_wrapper_setprop(const void *devp, const char *name,
  81. const void *buf, const int len)
  82. {
  83. int rc;
  84. rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len);
  85. if (rc == -FDT_ERR_NOSPACE) {
  86. expand_buf(len + 16);
  87. rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len);
  88. }
  89. return check_err(rc);
  90. }
  91. static int fdt_wrapper_del_node(const void *devp)
  92. {
  93. return fdt_del_node(fdt, devp_offset(devp));
  94. }
  95. static void *fdt_wrapper_get_parent(const void *devp)
  96. {
  97. return offset_devp(fdt_parent_offset(fdt, devp_offset(devp)));
  98. }
  99. static void *fdt_wrapper_create_node(const void *devp, const char *name)
  100. {
  101. int offset;
  102. offset = fdt_add_subnode(fdt, devp_offset(devp), name);
  103. if (offset == -FDT_ERR_NOSPACE) {
  104. expand_buf(strlen(name) + 16);
  105. offset = fdt_add_subnode(fdt, devp_offset(devp), name);
  106. }
  107. return offset_devp(offset);
  108. }
  109. static void *fdt_wrapper_find_node_by_prop_value(const void *prev,
  110. const char *name,
  111. const char *val,
  112. int len)
  113. {
  114. int offset = fdt_node_offset_by_prop_value(fdt, devp_offset_find(prev),
  115. name, val, len);
  116. return offset_devp(offset);
  117. }
  118. static void *fdt_wrapper_find_node_by_compatible(const void *prev,
  119. const char *val)
  120. {
  121. int offset = fdt_node_offset_by_compatible(fdt, devp_offset_find(prev),
  122. val);
  123. return offset_devp(offset);
  124. }
  125. static char *fdt_wrapper_get_path(const void *devp, char *buf, int len)
  126. {
  127. int rc;
  128. rc = fdt_get_path(fdt, devp_offset(devp), buf, len);
  129. if (check_err(rc))
  130. return NULL;
  131. return buf;
  132. }
  133. static unsigned long fdt_wrapper_finalize(void)
  134. {
  135. int rc;
  136. rc = fdt_pack(fdt);
  137. if (rc != 0)
  138. fatal("Couldn't pack flat tree: %s\n\r",
  139. fdt_strerror(rc));
  140. return (unsigned long)fdt;
  141. }
  142. void fdt_init(void *blob)
  143. {
  144. int err;
  145. int bufsize;
  146. dt_ops.finddevice = fdt_wrapper_finddevice;
  147. dt_ops.getprop = fdt_wrapper_getprop;
  148. dt_ops.setprop = fdt_wrapper_setprop;
  149. dt_ops.get_parent = fdt_wrapper_get_parent;
  150. dt_ops.create_node = fdt_wrapper_create_node;
  151. dt_ops.find_node_by_prop_value = fdt_wrapper_find_node_by_prop_value;
  152. dt_ops.find_node_by_compatible = fdt_wrapper_find_node_by_compatible;
  153. dt_ops.del_node = fdt_wrapper_del_node;
  154. dt_ops.get_path = fdt_wrapper_get_path;
  155. dt_ops.finalize = fdt_wrapper_finalize;
  156. /* Make sure the dt blob is the right version and so forth */
  157. fdt = blob;
  158. bufsize = fdt_totalsize(fdt) + EXPAND_GRANULARITY;
  159. buf = malloc(bufsize);
  160. if(!buf)
  161. fatal("malloc failed. can't relocate the device tree\n\r");
  162. err = fdt_open_into(fdt, buf, bufsize);
  163. if (err != 0)
  164. fatal("fdt_init(): %s\n\r", fdt_strerror(err));
  165. fdt = buf;
  166. }