data.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  4. */
  5. #include "dtc.h"
  6. void data_free(struct data d)
  7. {
  8. struct marker *m, *nm;
  9. m = d.markers;
  10. while (m) {
  11. nm = m->next;
  12. free(m->ref);
  13. free(m);
  14. m = nm;
  15. }
  16. if (d.val)
  17. free(d.val);
  18. }
  19. struct data data_grow_for(struct data d, unsigned int xlen)
  20. {
  21. struct data nd;
  22. unsigned int newsize;
  23. if (xlen == 0)
  24. return d;
  25. nd = d;
  26. newsize = xlen;
  27. while ((d.len + xlen) > newsize)
  28. newsize *= 2;
  29. nd.val = xrealloc(d.val, newsize);
  30. return nd;
  31. }
  32. struct data data_copy_mem(const char *mem, int len)
  33. {
  34. struct data d;
  35. d = data_grow_for(empty_data, len);
  36. d.len = len;
  37. memcpy(d.val, mem, len);
  38. return d;
  39. }
  40. struct data data_copy_escape_string(const char *s, int len)
  41. {
  42. int i = 0;
  43. struct data d;
  44. char *q;
  45. d = data_add_marker(empty_data, TYPE_STRING, NULL);
  46. d = data_grow_for(d, len + 1);
  47. q = d.val;
  48. while (i < len) {
  49. char c = s[i++];
  50. if (c == '\\')
  51. c = get_escape_char(s, &i);
  52. q[d.len++] = c;
  53. }
  54. q[d.len++] = '\0';
  55. return d;
  56. }
  57. struct data data_copy_file(FILE *f, size_t maxlen)
  58. {
  59. struct data d = empty_data;
  60. d = data_add_marker(d, TYPE_NONE, NULL);
  61. while (!feof(f) && (d.len < maxlen)) {
  62. size_t chunksize, ret;
  63. if (maxlen == (size_t)-1)
  64. chunksize = 4096;
  65. else
  66. chunksize = maxlen - d.len;
  67. d = data_grow_for(d, chunksize);
  68. ret = fread(d.val + d.len, 1, chunksize, f);
  69. if (ferror(f))
  70. die("Error reading file into data: %s", strerror(errno));
  71. if (d.len + ret < d.len)
  72. die("Overflow reading file into data\n");
  73. d.len += ret;
  74. }
  75. return d;
  76. }
  77. struct data data_append_data(struct data d, const void *p, int len)
  78. {
  79. d = data_grow_for(d, len);
  80. memcpy(d.val + d.len, p, len);
  81. d.len += len;
  82. return d;
  83. }
  84. struct data data_insert_at_marker(struct data d, struct marker *m,
  85. const void *p, int len)
  86. {
  87. d = data_grow_for(d, len);
  88. memmove(d.val + m->offset + len, d.val + m->offset, d.len - m->offset);
  89. memcpy(d.val + m->offset, p, len);
  90. d.len += len;
  91. /* Adjust all markers after the one we're inserting at */
  92. m = m->next;
  93. for_each_marker(m)
  94. m->offset += len;
  95. return d;
  96. }
  97. static struct data data_append_markers(struct data d, struct marker *m)
  98. {
  99. struct marker **mp = &d.markers;
  100. /* Find the end of the markerlist */
  101. while (*mp)
  102. mp = &((*mp)->next);
  103. *mp = m;
  104. return d;
  105. }
  106. struct data data_merge(struct data d1, struct data d2)
  107. {
  108. struct data d;
  109. struct marker *m2 = d2.markers;
  110. d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
  111. /* Adjust for the length of d1 */
  112. for_each_marker(m2)
  113. m2->offset += d1.len;
  114. d2.markers = NULL; /* So data_free() doesn't clobber them */
  115. data_free(d2);
  116. return d;
  117. }
  118. struct data data_append_integer(struct data d, uint64_t value, int bits)
  119. {
  120. uint8_t value_8;
  121. fdt16_t value_16;
  122. fdt32_t value_32;
  123. fdt64_t value_64;
  124. switch (bits) {
  125. case 8:
  126. value_8 = value;
  127. return data_append_data(d, &value_8, 1);
  128. case 16:
  129. value_16 = cpu_to_fdt16(value);
  130. return data_append_data(d, &value_16, 2);
  131. case 32:
  132. value_32 = cpu_to_fdt32(value);
  133. return data_append_data(d, &value_32, 4);
  134. case 64:
  135. value_64 = cpu_to_fdt64(value);
  136. return data_append_data(d, &value_64, 8);
  137. default:
  138. die("Invalid literal size (%d)\n", bits);
  139. }
  140. }
  141. struct data data_append_re(struct data d, uint64_t address, uint64_t size)
  142. {
  143. struct fdt_reserve_entry re;
  144. re.address = cpu_to_fdt64(address);
  145. re.size = cpu_to_fdt64(size);
  146. return data_append_data(d, &re, sizeof(re));
  147. }
  148. struct data data_append_cell(struct data d, cell_t word)
  149. {
  150. return data_append_integer(d, word, sizeof(word) * 8);
  151. }
  152. struct data data_append_addr(struct data d, uint64_t addr)
  153. {
  154. return data_append_integer(d, addr, sizeof(addr) * 8);
  155. }
  156. struct data data_append_byte(struct data d, uint8_t byte)
  157. {
  158. return data_append_data(d, &byte, 1);
  159. }
  160. struct data data_append_zeroes(struct data d, int len)
  161. {
  162. d = data_grow_for(d, len);
  163. memset(d.val + d.len, 0, len);
  164. d.len += len;
  165. return d;
  166. }
  167. struct data data_append_align(struct data d, int align)
  168. {
  169. int newlen = ALIGN(d.len, align);
  170. return data_append_zeroes(d, newlen - d.len);
  171. }
  172. struct data data_add_marker(struct data d, enum markertype type, char *ref)
  173. {
  174. struct marker *m;
  175. m = xmalloc(sizeof(*m));
  176. m->offset = d.len;
  177. m->type = type;
  178. m->ref = ref;
  179. m->next = NULL;
  180. return data_append_markers(d, m);
  181. }
  182. bool data_is_one_string(struct data d)
  183. {
  184. int i;
  185. int len = d.len;
  186. if (len == 0)
  187. return false;
  188. for (i = 0; i < len-1; i++)
  189. if (d.val[i] == '\0')
  190. return false;
  191. if (d.val[len-1] != '\0')
  192. return false;
  193. return true;
  194. }