livetree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. */
  20. #include "dtc.h"
  21. /*
  22. * Tree building functions
  23. */
  24. void add_label(struct label **labels, char *label)
  25. {
  26. struct label *new;
  27. /* Make sure the label isn't already there */
  28. for_each_label_withdel(*labels, new)
  29. if (streq(new->label, label)) {
  30. new->deleted = 0;
  31. return;
  32. }
  33. new = xmalloc(sizeof(*new));
  34. memset(new, 0, sizeof(*new));
  35. new->label = label;
  36. new->next = *labels;
  37. *labels = new;
  38. }
  39. void delete_labels(struct label **labels)
  40. {
  41. struct label *label;
  42. for_each_label(*labels, label)
  43. label->deleted = 1;
  44. }
  45. struct property *build_property(char *name, struct data val)
  46. {
  47. struct property *new = xmalloc(sizeof(*new));
  48. memset(new, 0, sizeof(*new));
  49. new->name = name;
  50. new->val = val;
  51. return new;
  52. }
  53. struct property *build_property_delete(char *name)
  54. {
  55. struct property *new = xmalloc(sizeof(*new));
  56. memset(new, 0, sizeof(*new));
  57. new->name = name;
  58. new->deleted = 1;
  59. return new;
  60. }
  61. struct property *chain_property(struct property *first, struct property *list)
  62. {
  63. assert(first->next == NULL);
  64. first->next = list;
  65. return first;
  66. }
  67. struct property *reverse_properties(struct property *first)
  68. {
  69. struct property *p = first;
  70. struct property *head = NULL;
  71. struct property *next;
  72. while (p) {
  73. next = p->next;
  74. p->next = head;
  75. head = p;
  76. p = next;
  77. }
  78. return head;
  79. }
  80. struct node *build_node(struct property *proplist, struct node *children)
  81. {
  82. struct node *new = xmalloc(sizeof(*new));
  83. struct node *child;
  84. memset(new, 0, sizeof(*new));
  85. new->proplist = reverse_properties(proplist);
  86. new->children = children;
  87. for_each_child(new, child) {
  88. child->parent = new;
  89. }
  90. return new;
  91. }
  92. struct node *build_node_delete(void)
  93. {
  94. struct node *new = xmalloc(sizeof(*new));
  95. memset(new, 0, sizeof(*new));
  96. new->deleted = 1;
  97. return new;
  98. }
  99. struct node *name_node(struct node *node, char *name)
  100. {
  101. assert(node->name == NULL);
  102. node->name = name;
  103. return node;
  104. }
  105. struct node *merge_nodes(struct node *old_node, struct node *new_node)
  106. {
  107. struct property *new_prop, *old_prop;
  108. struct node *new_child, *old_child;
  109. struct label *l;
  110. old_node->deleted = 0;
  111. /* Add new node labels to old node */
  112. for_each_label_withdel(new_node->labels, l)
  113. add_label(&old_node->labels, l->label);
  114. /* Move properties from the new node to the old node. If there
  115. * is a collision, replace the old value with the new */
  116. while (new_node->proplist) {
  117. /* Pop the property off the list */
  118. new_prop = new_node->proplist;
  119. new_node->proplist = new_prop->next;
  120. new_prop->next = NULL;
  121. if (new_prop->deleted) {
  122. delete_property_by_name(old_node, new_prop->name);
  123. free(new_prop);
  124. continue;
  125. }
  126. /* Look for a collision, set new value if there is */
  127. for_each_property_withdel(old_node, old_prop) {
  128. if (streq(old_prop->name, new_prop->name)) {
  129. /* Add new labels to old property */
  130. for_each_label_withdel(new_prop->labels, l)
  131. add_label(&old_prop->labels, l->label);
  132. old_prop->val = new_prop->val;
  133. old_prop->deleted = 0;
  134. free(new_prop);
  135. new_prop = NULL;
  136. break;
  137. }
  138. }
  139. /* if no collision occurred, add property to the old node. */
  140. if (new_prop)
  141. add_property(old_node, new_prop);
  142. }
  143. /* Move the override child nodes into the primary node. If
  144. * there is a collision, then merge the nodes. */
  145. while (new_node->children) {
  146. /* Pop the child node off the list */
  147. new_child = new_node->children;
  148. new_node->children = new_child->next_sibling;
  149. new_child->parent = NULL;
  150. new_child->next_sibling = NULL;
  151. if (new_child->deleted) {
  152. delete_node_by_name(old_node, new_child->name);
  153. free(new_child);
  154. continue;
  155. }
  156. /* Search for a collision. Merge if there is */
  157. for_each_child_withdel(old_node, old_child) {
  158. if (streq(old_child->name, new_child->name)) {
  159. merge_nodes(old_child, new_child);
  160. new_child = NULL;
  161. break;
  162. }
  163. }
  164. /* if no collision occurred, add child to the old node. */
  165. if (new_child)
  166. add_child(old_node, new_child);
  167. }
  168. /* The new node contents are now merged into the old node. Free
  169. * the new node. */
  170. free(new_node);
  171. return old_node;
  172. }
  173. struct node *chain_node(struct node *first, struct node *list)
  174. {
  175. assert(first->next_sibling == NULL);
  176. first->next_sibling = list;
  177. return first;
  178. }
  179. void add_property(struct node *node, struct property *prop)
  180. {
  181. struct property **p;
  182. prop->next = NULL;
  183. p = &node->proplist;
  184. while (*p)
  185. p = &((*p)->next);
  186. *p = prop;
  187. }
  188. void delete_property_by_name(struct node *node, char *name)
  189. {
  190. struct property *prop = node->proplist;
  191. while (prop) {
  192. if (streq(prop->name, name)) {
  193. delete_property(prop);
  194. return;
  195. }
  196. prop = prop->next;
  197. }
  198. }
  199. void delete_property(struct property *prop)
  200. {
  201. prop->deleted = 1;
  202. delete_labels(&prop->labels);
  203. }
  204. void add_child(struct node *parent, struct node *child)
  205. {
  206. struct node **p;
  207. child->next_sibling = NULL;
  208. child->parent = parent;
  209. p = &parent->children;
  210. while (*p)
  211. p = &((*p)->next_sibling);
  212. *p = child;
  213. }
  214. void delete_node_by_name(struct node *parent, char *name)
  215. {
  216. struct node *node = parent->children;
  217. while (node) {
  218. if (streq(node->name, name)) {
  219. delete_node(node);
  220. return;
  221. }
  222. node = node->next_sibling;
  223. }
  224. }
  225. void delete_node(struct node *node)
  226. {
  227. struct property *prop;
  228. struct node *child;
  229. node->deleted = 1;
  230. for_each_child(node, child)
  231. delete_node(child);
  232. for_each_property(node, prop)
  233. delete_property(prop);
  234. delete_labels(&node->labels);
  235. }
  236. void append_to_property(struct node *node,
  237. char *name, const void *data, int len)
  238. {
  239. struct data d;
  240. struct property *p;
  241. p = get_property(node, name);
  242. if (p) {
  243. d = data_append_data(p->val, data, len);
  244. p->val = d;
  245. } else {
  246. d = data_append_data(empty_data, data, len);
  247. p = build_property(name, d);
  248. add_property(node, p);
  249. }
  250. }
  251. struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
  252. {
  253. struct reserve_info *new = xmalloc(sizeof(*new));
  254. memset(new, 0, sizeof(*new));
  255. new->address = address;
  256. new->size = size;
  257. return new;
  258. }
  259. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  260. struct reserve_info *list)
  261. {
  262. assert(first->next == NULL);
  263. first->next = list;
  264. return first;
  265. }
  266. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  267. struct reserve_info *new)
  268. {
  269. struct reserve_info *last;
  270. new->next = NULL;
  271. if (! list)
  272. return new;
  273. for (last = list; last->next; last = last->next)
  274. ;
  275. last->next = new;
  276. return list;
  277. }
  278. struct dt_info *build_dt_info(unsigned int dtsflags,
  279. struct reserve_info *reservelist,
  280. struct node *tree, uint32_t boot_cpuid_phys)
  281. {
  282. struct dt_info *dti;
  283. dti = xmalloc(sizeof(*dti));
  284. dti->dtsflags = dtsflags;
  285. dti->reservelist = reservelist;
  286. dti->dt = tree;
  287. dti->boot_cpuid_phys = boot_cpuid_phys;
  288. return dti;
  289. }
  290. /*
  291. * Tree accessor functions
  292. */
  293. const char *get_unitname(struct node *node)
  294. {
  295. if (node->name[node->basenamelen] == '\0')
  296. return "";
  297. else
  298. return node->name + node->basenamelen + 1;
  299. }
  300. struct property *get_property(struct node *node, const char *propname)
  301. {
  302. struct property *prop;
  303. for_each_property(node, prop)
  304. if (streq(prop->name, propname))
  305. return prop;
  306. return NULL;
  307. }
  308. cell_t propval_cell(struct property *prop)
  309. {
  310. assert(prop->val.len == sizeof(cell_t));
  311. return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
  312. }
  313. struct property *get_property_by_label(struct node *tree, const char *label,
  314. struct node **node)
  315. {
  316. struct property *prop;
  317. struct node *c;
  318. *node = tree;
  319. for_each_property(tree, prop) {
  320. struct label *l;
  321. for_each_label(prop->labels, l)
  322. if (streq(l->label, label))
  323. return prop;
  324. }
  325. for_each_child(tree, c) {
  326. prop = get_property_by_label(c, label, node);
  327. if (prop)
  328. return prop;
  329. }
  330. *node = NULL;
  331. return NULL;
  332. }
  333. struct marker *get_marker_label(struct node *tree, const char *label,
  334. struct node **node, struct property **prop)
  335. {
  336. struct marker *m;
  337. struct property *p;
  338. struct node *c;
  339. *node = tree;
  340. for_each_property(tree, p) {
  341. *prop = p;
  342. m = p->val.markers;
  343. for_each_marker_of_type(m, LABEL)
  344. if (streq(m->ref, label))
  345. return m;
  346. }
  347. for_each_child(tree, c) {
  348. m = get_marker_label(c, label, node, prop);
  349. if (m)
  350. return m;
  351. }
  352. *prop = NULL;
  353. *node = NULL;
  354. return NULL;
  355. }
  356. struct node *get_subnode(struct node *node, const char *nodename)
  357. {
  358. struct node *child;
  359. for_each_child(node, child)
  360. if (streq(child->name, nodename))
  361. return child;
  362. return NULL;
  363. }
  364. struct node *get_node_by_path(struct node *tree, const char *path)
  365. {
  366. const char *p;
  367. struct node *child;
  368. if (!path || ! (*path)) {
  369. if (tree->deleted)
  370. return NULL;
  371. return tree;
  372. }
  373. while (path[0] == '/')
  374. path++;
  375. p = strchr(path, '/');
  376. for_each_child(tree, child) {
  377. if (p && (strlen(child->name) == p-path) &&
  378. strneq(path, child->name, p-path))
  379. return get_node_by_path(child, p+1);
  380. else if (!p && streq(path, child->name))
  381. return child;
  382. }
  383. return NULL;
  384. }
  385. struct node *get_node_by_label(struct node *tree, const char *label)
  386. {
  387. struct node *child, *node;
  388. struct label *l;
  389. assert(label && (strlen(label) > 0));
  390. for_each_label(tree->labels, l)
  391. if (streq(l->label, label))
  392. return tree;
  393. for_each_child(tree, child) {
  394. node = get_node_by_label(child, label);
  395. if (node)
  396. return node;
  397. }
  398. return NULL;
  399. }
  400. struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
  401. {
  402. struct node *child, *node;
  403. assert((phandle != 0) && (phandle != -1));
  404. if (tree->phandle == phandle) {
  405. if (tree->deleted)
  406. return NULL;
  407. return tree;
  408. }
  409. for_each_child(tree, child) {
  410. node = get_node_by_phandle(child, phandle);
  411. if (node)
  412. return node;
  413. }
  414. return NULL;
  415. }
  416. struct node *get_node_by_ref(struct node *tree, const char *ref)
  417. {
  418. if (streq(ref, "/"))
  419. return tree;
  420. else if (ref[0] == '/')
  421. return get_node_by_path(tree, ref);
  422. else
  423. return get_node_by_label(tree, ref);
  424. }
  425. cell_t get_node_phandle(struct node *root, struct node *node)
  426. {
  427. static cell_t phandle = 1; /* FIXME: ick, static local */
  428. if ((node->phandle != 0) && (node->phandle != -1))
  429. return node->phandle;
  430. while (get_node_by_phandle(root, phandle))
  431. phandle++;
  432. node->phandle = phandle;
  433. if (!get_property(node, "linux,phandle")
  434. && (phandle_format & PHANDLE_LEGACY))
  435. add_property(node,
  436. build_property("linux,phandle",
  437. data_append_cell(empty_data, phandle)));
  438. if (!get_property(node, "phandle")
  439. && (phandle_format & PHANDLE_EPAPR))
  440. add_property(node,
  441. build_property("phandle",
  442. data_append_cell(empty_data, phandle)));
  443. /* If the node *does* have a phandle property, we must
  444. * be dealing with a self-referencing phandle, which will be
  445. * fixed up momentarily in the caller */
  446. return node->phandle;
  447. }
  448. uint32_t guess_boot_cpuid(struct node *tree)
  449. {
  450. struct node *cpus, *bootcpu;
  451. struct property *reg;
  452. cpus = get_node_by_path(tree, "/cpus");
  453. if (!cpus)
  454. return 0;
  455. bootcpu = cpus->children;
  456. if (!bootcpu)
  457. return 0;
  458. reg = get_property(bootcpu, "reg");
  459. if (!reg || (reg->val.len != sizeof(uint32_t)))
  460. return 0;
  461. /* FIXME: Sanity check node? */
  462. return propval_cell(reg);
  463. }
  464. static int cmp_reserve_info(const void *ax, const void *bx)
  465. {
  466. const struct reserve_info *a, *b;
  467. a = *((const struct reserve_info * const *)ax);
  468. b = *((const struct reserve_info * const *)bx);
  469. if (a->address < b->address)
  470. return -1;
  471. else if (a->address > b->address)
  472. return 1;
  473. else if (a->size < b->size)
  474. return -1;
  475. else if (a->size > b->size)
  476. return 1;
  477. else
  478. return 0;
  479. }
  480. static void sort_reserve_entries(struct dt_info *dti)
  481. {
  482. struct reserve_info *ri, **tbl;
  483. int n = 0, i = 0;
  484. for (ri = dti->reservelist;
  485. ri;
  486. ri = ri->next)
  487. n++;
  488. if (n == 0)
  489. return;
  490. tbl = xmalloc(n * sizeof(*tbl));
  491. for (ri = dti->reservelist;
  492. ri;
  493. ri = ri->next)
  494. tbl[i++] = ri;
  495. qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
  496. dti->reservelist = tbl[0];
  497. for (i = 0; i < (n-1); i++)
  498. tbl[i]->next = tbl[i+1];
  499. tbl[n-1]->next = NULL;
  500. free(tbl);
  501. }
  502. static int cmp_prop(const void *ax, const void *bx)
  503. {
  504. const struct property *a, *b;
  505. a = *((const struct property * const *)ax);
  506. b = *((const struct property * const *)bx);
  507. return strcmp(a->name, b->name);
  508. }
  509. static void sort_properties(struct node *node)
  510. {
  511. int n = 0, i = 0;
  512. struct property *prop, **tbl;
  513. for_each_property_withdel(node, prop)
  514. n++;
  515. if (n == 0)
  516. return;
  517. tbl = xmalloc(n * sizeof(*tbl));
  518. for_each_property_withdel(node, prop)
  519. tbl[i++] = prop;
  520. qsort(tbl, n, sizeof(*tbl), cmp_prop);
  521. node->proplist = tbl[0];
  522. for (i = 0; i < (n-1); i++)
  523. tbl[i]->next = tbl[i+1];
  524. tbl[n-1]->next = NULL;
  525. free(tbl);
  526. }
  527. static int cmp_subnode(const void *ax, const void *bx)
  528. {
  529. const struct node *a, *b;
  530. a = *((const struct node * const *)ax);
  531. b = *((const struct node * const *)bx);
  532. return strcmp(a->name, b->name);
  533. }
  534. static void sort_subnodes(struct node *node)
  535. {
  536. int n = 0, i = 0;
  537. struct node *subnode, **tbl;
  538. for_each_child_withdel(node, subnode)
  539. n++;
  540. if (n == 0)
  541. return;
  542. tbl = xmalloc(n * sizeof(*tbl));
  543. for_each_child_withdel(node, subnode)
  544. tbl[i++] = subnode;
  545. qsort(tbl, n, sizeof(*tbl), cmp_subnode);
  546. node->children = tbl[0];
  547. for (i = 0; i < (n-1); i++)
  548. tbl[i]->next_sibling = tbl[i+1];
  549. tbl[n-1]->next_sibling = NULL;
  550. free(tbl);
  551. }
  552. static void sort_node(struct node *node)
  553. {
  554. struct node *c;
  555. sort_properties(node);
  556. sort_subnodes(node);
  557. for_each_child_withdel(node, c)
  558. sort_node(c);
  559. }
  560. void sort_tree(struct dt_info *dti)
  561. {
  562. sort_reserve_entries(dti);
  563. sort_node(dti->dt);
  564. }
  565. /* utility helper to avoid code duplication */
  566. static struct node *build_and_name_child_node(struct node *parent, char *name)
  567. {
  568. struct node *node;
  569. node = build_node(NULL, NULL);
  570. name_node(node, xstrdup(name));
  571. add_child(parent, node);
  572. return node;
  573. }
  574. static struct node *build_root_node(struct node *dt, char *name)
  575. {
  576. struct node *an;
  577. an = get_subnode(dt, name);
  578. if (!an)
  579. an = build_and_name_child_node(dt, name);
  580. if (!an)
  581. die("Could not build root node /%s\n", name);
  582. return an;
  583. }
  584. static bool any_label_tree(struct dt_info *dti, struct node *node)
  585. {
  586. struct node *c;
  587. if (node->labels)
  588. return true;
  589. for_each_child(node, c)
  590. if (any_label_tree(dti, c))
  591. return true;
  592. return false;
  593. }
  594. static void generate_label_tree_internal(struct dt_info *dti,
  595. struct node *an, struct node *node,
  596. bool allocph)
  597. {
  598. struct node *dt = dti->dt;
  599. struct node *c;
  600. struct property *p;
  601. struct label *l;
  602. /* if there are labels */
  603. if (node->labels) {
  604. /* now add the label in the node */
  605. for_each_label(node->labels, l) {
  606. /* check whether the label already exists */
  607. p = get_property(an, l->label);
  608. if (p) {
  609. fprintf(stderr, "WARNING: label %s already"
  610. " exists in /%s", l->label,
  611. an->name);
  612. continue;
  613. }
  614. /* insert it */
  615. p = build_property(l->label,
  616. data_copy_mem(node->fullpath,
  617. strlen(node->fullpath) + 1));
  618. add_property(an, p);
  619. }
  620. /* force allocation of a phandle for this node */
  621. if (allocph)
  622. (void)get_node_phandle(dt, node);
  623. }
  624. for_each_child(node, c)
  625. generate_label_tree_internal(dti, an, c, allocph);
  626. }
  627. static bool any_fixup_tree(struct dt_info *dti, struct node *node)
  628. {
  629. struct node *c;
  630. struct property *prop;
  631. struct marker *m;
  632. for_each_property(node, prop) {
  633. m = prop->val.markers;
  634. for_each_marker_of_type(m, REF_PHANDLE) {
  635. if (!get_node_by_ref(dti->dt, m->ref))
  636. return true;
  637. }
  638. }
  639. for_each_child(node, c) {
  640. if (any_fixup_tree(dti, c))
  641. return true;
  642. }
  643. return false;
  644. }
  645. static void add_fixup_entry(struct dt_info *dti, struct node *fn,
  646. struct node *node, struct property *prop,
  647. struct marker *m)
  648. {
  649. char *entry;
  650. /* m->ref can only be a REF_PHANDLE, but check anyway */
  651. assert(m->type == REF_PHANDLE);
  652. /* there shouldn't be any ':' in the arguments */
  653. if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
  654. die("arguments should not contain ':'\n");
  655. xasprintf(&entry, "%s:%s:%u",
  656. node->fullpath, prop->name, m->offset);
  657. append_to_property(fn, m->ref, entry, strlen(entry) + 1);
  658. free(entry);
  659. }
  660. static void generate_fixups_tree_internal(struct dt_info *dti,
  661. struct node *fn,
  662. struct node *node)
  663. {
  664. struct node *dt = dti->dt;
  665. struct node *c;
  666. struct property *prop;
  667. struct marker *m;
  668. struct node *refnode;
  669. for_each_property(node, prop) {
  670. m = prop->val.markers;
  671. for_each_marker_of_type(m, REF_PHANDLE) {
  672. refnode = get_node_by_ref(dt, m->ref);
  673. if (!refnode)
  674. add_fixup_entry(dti, fn, node, prop, m);
  675. }
  676. }
  677. for_each_child(node, c)
  678. generate_fixups_tree_internal(dti, fn, c);
  679. }
  680. static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
  681. {
  682. struct node *c;
  683. struct property *prop;
  684. struct marker *m;
  685. for_each_property(node, prop) {
  686. m = prop->val.markers;
  687. for_each_marker_of_type(m, REF_PHANDLE) {
  688. if (get_node_by_ref(dti->dt, m->ref))
  689. return true;
  690. }
  691. }
  692. for_each_child(node, c) {
  693. if (any_local_fixup_tree(dti, c))
  694. return true;
  695. }
  696. return false;
  697. }
  698. static void add_local_fixup_entry(struct dt_info *dti,
  699. struct node *lfn, struct node *node,
  700. struct property *prop, struct marker *m,
  701. struct node *refnode)
  702. {
  703. struct node *wn, *nwn; /* local fixup node, walk node, new */
  704. fdt32_t value_32;
  705. char **compp;
  706. int i, depth;
  707. /* walk back retreiving depth */
  708. depth = 0;
  709. for (wn = node; wn; wn = wn->parent)
  710. depth++;
  711. /* allocate name array */
  712. compp = xmalloc(sizeof(*compp) * depth);
  713. /* store names in the array */
  714. for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
  715. compp[i] = wn->name;
  716. /* walk the path components creating nodes if they don't exist */
  717. for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
  718. /* if no node exists, create it */
  719. nwn = get_subnode(wn, compp[i]);
  720. if (!nwn)
  721. nwn = build_and_name_child_node(wn, compp[i]);
  722. }
  723. free(compp);
  724. value_32 = cpu_to_fdt32(m->offset);
  725. append_to_property(wn, prop->name, &value_32, sizeof(value_32));
  726. }
  727. static void generate_local_fixups_tree_internal(struct dt_info *dti,
  728. struct node *lfn,
  729. struct node *node)
  730. {
  731. struct node *dt = dti->dt;
  732. struct node *c;
  733. struct property *prop;
  734. struct marker *m;
  735. struct node *refnode;
  736. for_each_property(node, prop) {
  737. m = prop->val.markers;
  738. for_each_marker_of_type(m, REF_PHANDLE) {
  739. refnode = get_node_by_ref(dt, m->ref);
  740. if (refnode)
  741. add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
  742. }
  743. }
  744. for_each_child(node, c)
  745. generate_local_fixups_tree_internal(dti, lfn, c);
  746. }
  747. void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
  748. {
  749. if (!any_label_tree(dti, dti->dt))
  750. return;
  751. generate_label_tree_internal(dti, build_root_node(dti->dt, name),
  752. dti->dt, allocph);
  753. }
  754. void generate_fixups_tree(struct dt_info *dti, char *name)
  755. {
  756. if (!any_fixup_tree(dti, dti->dt))
  757. return;
  758. generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
  759. dti->dt);
  760. }
  761. void generate_local_fixups_tree(struct dt_info *dti, char *name)
  762. {
  763. if (!any_local_fixup_tree(dti, dti->dt))
  764. return;
  765. generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),
  766. dti->dt);
  767. }