fdt_wip.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "libfdt_env.h"
  20. #include <fdt.h>
  21. #include <libfdt.h>
  22. #include "libfdt_internal.h"
  23. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  24. const void *val, int len)
  25. {
  26. void *propval;
  27. int proplen;
  28. propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
  29. if (! propval)
  30. return proplen;
  31. if (proplen != len)
  32. return -FDT_ERR_NOSPACE;
  33. memcpy(propval, val, len);
  34. return 0;
  35. }
  36. static void nop_region(void *start, int len)
  37. {
  38. uint32_t *p;
  39. for (p = start; (void *)p < (start + len); p++)
  40. *p = cpu_to_fdt32(FDT_NOP);
  41. }
  42. int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
  43. {
  44. struct fdt_property *prop;
  45. int len;
  46. prop = fdt_get_property(fdt, nodeoffset, name, &len);
  47. if (! prop)
  48. return len;
  49. nop_region(prop, len + sizeof(*prop));
  50. return 0;
  51. }
  52. int _fdt_node_end_offset(void *fdt, int nodeoffset)
  53. {
  54. int level = 0;
  55. uint32_t tag;
  56. int offset, nextoffset;
  57. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  58. if (tag != FDT_BEGIN_NODE)
  59. return -FDT_ERR_BADOFFSET;
  60. do {
  61. offset = nextoffset;
  62. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  63. switch (tag) {
  64. case FDT_END:
  65. return offset;
  66. case FDT_BEGIN_NODE:
  67. level++;
  68. break;
  69. case FDT_END_NODE:
  70. level--;
  71. break;
  72. case FDT_PROP:
  73. case FDT_NOP:
  74. break;
  75. default:
  76. return -FDT_ERR_BADSTRUCTURE;
  77. }
  78. } while (level >= 0);
  79. return nextoffset;
  80. }
  81. int fdt_nop_node(void *fdt, int nodeoffset)
  82. {
  83. int endoffset;
  84. endoffset = _fdt_node_end_offset(fdt, nodeoffset);
  85. if (endoffset < 0)
  86. return endoffset;
  87. nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
  88. return 0;
  89. }
  90. /*
  91. * Replace a reserve map entry in the nth slot.
  92. */
  93. int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size)
  94. {
  95. struct fdt_reserve_entry *re;
  96. int used;
  97. int total;
  98. int err;
  99. err = fdt_num_reservemap(fdt, &used, &total);
  100. if (err != 0)
  101. return err;
  102. if (n >= total)
  103. return -FDT_ERR_NOSPACE;
  104. re = (struct fdt_reserve_entry *)
  105. (fdt + fdt_off_mem_rsvmap(fdt) +
  106. (n * sizeof(struct fdt_reserve_entry)));
  107. re->address = cpu_to_fdt64(addr);
  108. re->size = cpu_to_fdt64(size);
  109. return 0;
  110. }