fdt_region.c 18 KB

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