fdt_region.c 18 KB

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