fdt_region.c 19 KB

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