fdt_region.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2013 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
  6. */
  7. #include "libfdt_env.h"
  8. #ifndef USE_HOSTCC
  9. #include <fdt.h>
  10. #include <libfdt.h>
  11. #else
  12. #include "fdt_host.h"
  13. #endif
  14. #include "libfdt_internal.h"
  15. /**
  16. * fdt_add_region() - Add a new region to our list
  17. *
  18. * The region is added if there is space, but in any case we increment the
  19. * count. If permitted, and the new region overlaps the last one, we merge
  20. * them.
  21. *
  22. * @info: State information
  23. * @offset: Start offset of region
  24. * @size: Size of region
  25. */
  26. static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
  27. {
  28. struct fdt_region *reg;
  29. reg = info->region ? &info->region[info->count - 1] : NULL;
  30. if (info->can_merge && info->count &&
  31. info->count <= info->max_regions &&
  32. reg && offset <= reg->offset + reg->size) {
  33. reg->size = offset + size - reg->offset;
  34. } else if (info->count++ < info->max_regions) {
  35. if (reg) {
  36. reg++;
  37. reg->offset = offset;
  38. reg->size = size;
  39. }
  40. } else {
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. static int region_list_contains_offset(struct fdt_region_state *info,
  46. const void *fdt, int target)
  47. {
  48. struct fdt_region *reg;
  49. int num;
  50. target += fdt_off_dt_struct(fdt);
  51. for (reg = info->region, num = 0; num < info->count; reg++, num++) {
  52. if (target >= reg->offset && target < reg->offset + reg->size)
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
  58. int max_regions, struct fdt_region_state *info)
  59. {
  60. int base = fdt_off_dt_struct(fdt);
  61. int node, node_end, offset;
  62. int did_alias_header;
  63. node = fdt_subnode_offset(fdt, 0, "aliases");
  64. if (node < 0)
  65. return -FDT_ERR_NOTFOUND;
  66. /* The aliases node must come before the others */
  67. node_end = fdt_next_subnode(fdt, node);
  68. if (node_end <= 0)
  69. return -FDT_ERR_BADLAYOUT;
  70. node_end -= sizeof(fdt32_t);
  71. did_alias_header = 0;
  72. info->region = region;
  73. info->count = count;
  74. info->can_merge = 0;
  75. info->max_regions = max_regions;
  76. for (offset = fdt_first_property_offset(fdt, node);
  77. offset >= 0;
  78. offset = fdt_next_property_offset(fdt, offset)) {
  79. const struct fdt_property *prop;
  80. const char *name;
  81. int target, next;
  82. prop = fdt_get_property_by_offset(fdt, offset, NULL);
  83. name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  84. target = fdt_path_offset(fdt, name);
  85. if (!region_list_contains_offset(info, fdt, target))
  86. continue;
  87. next = fdt_next_property_offset(fdt, offset);
  88. if (next < 0)
  89. next = node_end;
  90. if (!did_alias_header) {
  91. fdt_add_region(info, base + node, 12);
  92. did_alias_header = 1;
  93. }
  94. fdt_add_region(info, base + offset, next - offset);
  95. }
  96. /* Add the 'end' tag */
  97. if (did_alias_header)
  98. fdt_add_region(info, base + node_end, sizeof(fdt32_t));
  99. return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
  100. }
  101. /**
  102. * fdt_include_supernodes() - Include supernodes required by this node
  103. *
  104. * When we decided to include a node or property which is not at the top
  105. * level, this function forces the inclusion of higher level nodes. For
  106. * example, given this tree:
  107. *
  108. * / {
  109. * testing {
  110. * }
  111. * }
  112. *
  113. * If we decide to include testing then we need the root node to have a valid
  114. * tree. This function adds those regions.
  115. *
  116. * @info: State information
  117. * @depth: Current stack depth
  118. */
  119. static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
  120. {
  121. int base = fdt_off_dt_struct(info->fdt);
  122. int start, stop_at;
  123. int i;
  124. /*
  125. * Work down the stack looking for supernodes that we didn't include.
  126. * The algortihm here is actually pretty simple, since we know that
  127. * no previous subnode had to include these nodes, or if it did, we
  128. * marked them as included (on the stack) already.
  129. */
  130. for (i = 0; i <= depth; i++) {
  131. if (!info->stack[i].included) {
  132. start = info->stack[i].offset;
  133. /* Add the FDT_BEGIN_NODE tag of this supernode */
  134. fdt_next_tag(info->fdt, start, &stop_at);
  135. if (fdt_add_region(info, base + start, stop_at - start))
  136. return -1;
  137. /* Remember that this supernode is now included */
  138. info->stack[i].included = 1;
  139. info->can_merge = 1;
  140. }
  141. /* Force (later) generation of the FDT_END_NODE tag */
  142. if (!info->stack[i].want)
  143. info->stack[i].want = WANT_NODES_ONLY;
  144. }
  145. return 0;
  146. }
  147. enum {
  148. FDT_DONE_NOTHING,
  149. FDT_DONE_MEM_RSVMAP,
  150. FDT_DONE_STRUCT,
  151. FDT_DONE_END,
  152. FDT_DONE_STRINGS,
  153. FDT_DONE_ALL,
  154. };
  155. int fdt_first_region(const void *fdt,
  156. int (*h_include)(void *priv, const void *fdt, int offset,
  157. int type, const char *data, int size),
  158. void *priv, struct fdt_region *region,
  159. char *path, int path_len, int flags,
  160. struct fdt_region_state *info)
  161. {
  162. struct fdt_region_ptrs *p = &info->ptrs;
  163. /* Set up our state */
  164. info->fdt = fdt;
  165. info->can_merge = 1;
  166. info->max_regions = 1;
  167. info->start = -1;
  168. p->want = WANT_NOTHING;
  169. p->end = path;
  170. *p->end = '\0';
  171. p->nextoffset = 0;
  172. p->depth = -1;
  173. p->done = FDT_DONE_NOTHING;
  174. return fdt_next_region(fdt, h_include, priv, region,
  175. path, path_len, flags, info);
  176. }
  177. /*
  178. * Theory of operation
  179. *
  180. *
  181. *
  182. Note: in this description 'included' means that a node (or other part of
  183. the tree) should be included in the region list, i.e. it will have a region
  184. which covers its part of the tree.
  185. This function maintains some state from the last time it is called. It
  186. checks the next part of the tree that it is supposed to look at
  187. (p.nextoffset) to see if that should be included or not. When it finds
  188. something to include, it sets info->start to its offset. This marks the
  189. start of the region we want to include.
  190. Once info->start is set to the start (i.e. not -1), we continue scanning
  191. until we find something that we don't want included. This will be the end
  192. of a region. At this point we can close off the region and add it to the
  193. list. So we do so, and reset info->start to -1.
  194. One complication here is that we want to merge regions. So when we come to
  195. add another region later, we may in fact merge it with the previous one if
  196. one ends where the other starts.
  197. The function fdt_add_region() will return -1 if it fails to add the region,
  198. because we already have a region ready to be returned, and the new one
  199. cannot be merged in with it. In this case, we must return the region we
  200. found, and wait for another call to this function. When it comes, we will
  201. repeat the processing of the tag and again try to add a region. This time it
  202. will succeed.
  203. The current state of the pointers (stack, offset, etc.) is maintained in
  204. a ptrs member. At the start of every loop iteration we make a copy of it.
  205. The copy is then updated as the tag is processed. Only if we get to the end
  206. of the loop iteration (and successfully call fdt_add_region() if we need
  207. to) can we commit the changes we have made to these pointers. For example,
  208. if we see an FDT_END_NODE tag we will decrement the depth value. But if we
  209. need to add a region for this tag (let's say because the previous tag is
  210. included and this FDT_END_NODE tag is not included) then we will only commit
  211. the result if we were able to add the region. That allows us to retry again
  212. next time.
  213. We keep track of a variable called 'want' which tells us what we want to
  214. include when there is no specific information provided by the h_include
  215. function for a particular property. This basically handles the inclusion of
  216. properties which are pulled in by virtue of the node they are in. So if you
  217. include a node, its properties are also included. In this case 'want' will
  218. be WANT_NODES_AND_PROPS. The FDT_REG_DIRECT_SUBNODES feature also makes use
  219. of 'want'. While we are inside the subnode, 'want' will be set to
  220. WANT_NODES_ONLY, so that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE
  221. tags will be included, and properties will be skipped. If WANT_NOTHING is
  222. selected, then we will just rely on what the h_include() function tells us.
  223. Using 'want' we work out 'include', which tells us whether this current tag
  224. should be included or not. As you can imagine, if the value of 'include'
  225. changes, that means we are on a boundary between nodes to include and nodes
  226. to exclude. At this point we either close off a previous region and add it
  227. to the list, or mark the start of a new region.
  228. Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the string
  229. list. Each of these dealt with as a whole (i.e. we create a region for each
  230. if it is to be included). For mem_rsvmap we don't allow it to merge with
  231. the first struct region. For the stringlist we don't allow it to merge with
  232. the last struct region (which contains at minimum the FDT_END tag).
  233. */
  234. int fdt_next_region(const void *fdt,
  235. int (*h_include)(void *priv, const void *fdt, int offset,
  236. int type, const char *data, int size),
  237. void *priv, struct fdt_region *region,
  238. char *path, int path_len, int flags,
  239. struct fdt_region_state *info)
  240. {
  241. int base = fdt_off_dt_struct(fdt);
  242. int last_node = 0;
  243. const char *str;
  244. info->region = region;
  245. info->count = 0;
  246. if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
  247. (flags & FDT_REG_ADD_MEM_RSVMAP)) {
  248. /* Add the memory reserve map into its own region */
  249. if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
  250. fdt_off_dt_struct(fdt) -
  251. fdt_off_mem_rsvmap(fdt)))
  252. return 0;
  253. info->can_merge = 0; /* Don't allow merging with this */
  254. info->ptrs.done = FDT_DONE_MEM_RSVMAP;
  255. }
  256. /*
  257. * Work through the tags one by one, deciding whether each needs to
  258. * be included or not. We set the variable 'include' to indicate our
  259. * decision. 'want' is used to track what we want to include - it
  260. * allows us to pick up all the properties (and/or subnode tags) of
  261. * a node.
  262. */
  263. while (info->ptrs.done < FDT_DONE_STRUCT) {
  264. const struct fdt_property *prop;
  265. struct fdt_region_ptrs p;
  266. const char *name;
  267. int include = 0;
  268. int stop_at = 0;
  269. uint32_t tag;
  270. int offset;
  271. int val;
  272. int len;
  273. /*
  274. * Make a copy of our pointers. If we make it to the end of
  275. * this block then we will commit them back to info->ptrs.
  276. * Otherwise we can try again from the same starting state
  277. * next time we are called.
  278. */
  279. p = info->ptrs;
  280. /*
  281. * Find the tag, and the offset of the next one. If we need to
  282. * stop including tags, then by default we stop *after*
  283. * including the current tag
  284. */
  285. offset = p.nextoffset;
  286. tag = fdt_next_tag(fdt, offset, &p.nextoffset);
  287. stop_at = p.nextoffset;
  288. switch (tag) {
  289. case FDT_PROP:
  290. stop_at = offset;
  291. prop = fdt_get_property_by_offset(fdt, offset, NULL);
  292. str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  293. val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
  294. strlen(str) + 1);
  295. if (val == -1) {
  296. include = p.want >= WANT_NODES_AND_PROPS;
  297. } else {
  298. include = val;
  299. /*
  300. * Make sure we include the } for this block.
  301. * It might be more correct to have this done
  302. * by the call to fdt_include_supernodes() in
  303. * the case where it adds the node we are
  304. * currently in, but this is equivalent.
  305. */
  306. if ((flags & FDT_REG_SUPERNODES) && val &&
  307. !p.want)
  308. p.want = WANT_NODES_ONLY;
  309. }
  310. /* Value grepping is not yet supported */
  311. break;
  312. case FDT_NOP:
  313. include = p.want >= WANT_NODES_AND_PROPS;
  314. stop_at = offset;
  315. break;
  316. case FDT_BEGIN_NODE:
  317. last_node = offset;
  318. p.depth++;
  319. if (p.depth == FDT_MAX_DEPTH)
  320. return -FDT_ERR_TOODEEP;
  321. name = fdt_get_name(fdt, offset, &len);
  322. if (p.end - path + 2 + len >= path_len)
  323. return -FDT_ERR_NOSPACE;
  324. /* Build the full path of this node */
  325. if (p.end != path + 1)
  326. *p.end++ = '/';
  327. strcpy(p.end, name);
  328. p.end += len;
  329. info->stack[p.depth].want = p.want;
  330. info->stack[p.depth].offset = offset;
  331. /*
  332. * If we are not intending to include this node unless
  333. * it matches, make sure we stop *before* its tag.
  334. */
  335. if (p.want == WANT_NODES_ONLY ||
  336. !(flags & (FDT_REG_DIRECT_SUBNODES |
  337. FDT_REG_ALL_SUBNODES))) {
  338. stop_at = offset;
  339. p.want = WANT_NOTHING;
  340. }
  341. val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
  342. p.end - path + 1);
  343. /* Include this if requested */
  344. if (val) {
  345. p.want = (flags & FDT_REG_ALL_SUBNODES) ?
  346. WANT_ALL_NODES_AND_PROPS :
  347. WANT_NODES_AND_PROPS;
  348. }
  349. /* If not requested, decay our 'p.want' value */
  350. else if (p.want) {
  351. if (p.want != WANT_ALL_NODES_AND_PROPS)
  352. p.want--;
  353. /* Not including this tag, so stop now */
  354. } else {
  355. stop_at = offset;
  356. }
  357. /*
  358. * Decide whether to include this tag, and update our
  359. * stack with the state for this node
  360. */
  361. include = p.want;
  362. info->stack[p.depth].included = include;
  363. break;
  364. case FDT_END_NODE:
  365. include = p.want;
  366. if (p.depth < 0)
  367. return -FDT_ERR_BADSTRUCTURE;
  368. /*
  369. * If we don't want this node, stop right away, unless
  370. * we are including subnodes
  371. */
  372. if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
  373. stop_at = offset;
  374. p.want = info->stack[p.depth].want;
  375. p.depth--;
  376. while (p.end > path && *--p.end != '/')
  377. ;
  378. *p.end = '\0';
  379. break;
  380. case FDT_END:
  381. /* We always include the end tag */
  382. include = 1;
  383. p.done = FDT_DONE_STRUCT;
  384. break;
  385. }
  386. /* If this tag is to be included, mark it as region start */
  387. if (include && info->start == -1) {
  388. /* Include any supernodes required by this one */
  389. if (flags & FDT_REG_SUPERNODES) {
  390. if (fdt_include_supernodes(info, p.depth))
  391. return 0;
  392. }
  393. info->start = offset;
  394. }
  395. /*
  396. * If this tag is not to be included, finish up the current
  397. * region.
  398. */
  399. if (!include && info->start != -1) {
  400. if (fdt_add_region(info, base + info->start,
  401. stop_at - info->start))
  402. return 0;
  403. info->start = -1;
  404. info->can_merge = 1;
  405. }
  406. /* If we have made it this far, we can commit our pointers */
  407. info->ptrs = p;
  408. }
  409. /* Add a region for the END tag and a separate one for string table */
  410. if (info->ptrs.done < FDT_DONE_END) {
  411. if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
  412. return -FDT_ERR_BADSTRUCTURE;
  413. if (fdt_add_region(info, base + info->start,
  414. info->ptrs.nextoffset - info->start))
  415. return 0;
  416. info->ptrs.done++;
  417. }
  418. if (info->ptrs.done < FDT_DONE_STRINGS) {
  419. if (flags & FDT_REG_ADD_STRING_TAB) {
  420. info->can_merge = 0;
  421. if (fdt_off_dt_strings(fdt) <
  422. base + info->ptrs.nextoffset)
  423. return -FDT_ERR_BADLAYOUT;
  424. if (fdt_add_region(info, fdt_off_dt_strings(fdt),
  425. fdt_size_dt_strings(fdt)))
  426. return 0;
  427. }
  428. info->ptrs.done++;
  429. }
  430. return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
  431. }