fdt_region.c 15 KB

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