fdt_wip.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 2.1 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "config.h"
  20. #if CONFIG_OF_LIBFDT
  21. #include "libfdt_env.h"
  22. #include <fdt.h>
  23. #include <libfdt.h>
  24. #include "libfdt_internal.h"
  25. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  26. const void *val, int len)
  27. {
  28. void *propval;
  29. int proplen;
  30. propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
  31. if (! propval)
  32. return proplen;
  33. if (proplen != len)
  34. return -FDT_ERR_NOSPACE;
  35. memcpy(propval, val, len);
  36. return 0;
  37. }
  38. static void nop_region(void *start, int len)
  39. {
  40. uint32_t *p;
  41. for (p = start; (void *)p < (start + len); p++)
  42. *p = cpu_to_fdt32(FDT_NOP);
  43. }
  44. int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
  45. {
  46. struct fdt_property *prop;
  47. int len;
  48. prop = fdt_get_property(fdt, nodeoffset, name, &len);
  49. if (! prop)
  50. return len;
  51. nop_region(prop, len + sizeof(*prop));
  52. return 0;
  53. }
  54. int _fdt_node_end_offset(void *fdt, int nodeoffset)
  55. {
  56. int level = 0;
  57. uint32_t tag;
  58. int offset, nextoffset;
  59. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  60. if (tag != FDT_BEGIN_NODE)
  61. return -FDT_ERR_BADOFFSET;
  62. do {
  63. offset = nextoffset;
  64. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  65. switch (tag) {
  66. case FDT_END:
  67. return offset;
  68. case FDT_BEGIN_NODE:
  69. level++;
  70. break;
  71. case FDT_END_NODE:
  72. level--;
  73. break;
  74. case FDT_PROP:
  75. case FDT_NOP:
  76. break;
  77. default:
  78. return -FDT_ERR_BADSTRUCTURE;
  79. }
  80. } while (level >= 0);
  81. return nextoffset;
  82. }
  83. int fdt_nop_node(void *fdt, int nodeoffset)
  84. {
  85. int endoffset;
  86. endoffset = _fdt_node_end_offset(fdt, nodeoffset);
  87. if (endoffset < 0)
  88. return endoffset;
  89. nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
  90. return 0;
  91. }
  92. /*
  93. * Replace a reserve map entry in the nth slot.
  94. */
  95. int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size)
  96. {
  97. struct fdt_reserve_entry *re;
  98. int used;
  99. int total;
  100. int err;
  101. err = fdt_num_reservemap(fdt, &used, &total);
  102. if (err != 0)
  103. return err;
  104. if (n >= total)
  105. return -FDT_ERR_NOSPACE;
  106. re = (struct fdt_reserve_entry *)
  107. (fdt + fdt_off_mem_rsvmap(fdt) +
  108. (n * sizeof(struct fdt_reserve_entry)));
  109. re->address = cpu_to_fdt64(addr);
  110. re->size = cpu_to_fdt64(size);
  111. return 0;
  112. }
  113. #endif /* CONFIG_OF_LIBFDT */