livetree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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->re.address = address;
  256. new->re.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(*((cell_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 && strneq(path, child->name, p-path))
  378. return get_node_by_path(child, p+1);
  379. else if (!p && streq(path, child->name))
  380. return child;
  381. }
  382. return NULL;
  383. }
  384. struct node *get_node_by_label(struct node *tree, const char *label)
  385. {
  386. struct node *child, *node;
  387. struct label *l;
  388. assert(label && (strlen(label) > 0));
  389. for_each_label(tree->labels, l)
  390. if (streq(l->label, label))
  391. return tree;
  392. for_each_child(tree, child) {
  393. node = get_node_by_label(child, label);
  394. if (node)
  395. return node;
  396. }
  397. return NULL;
  398. }
  399. struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
  400. {
  401. struct node *child, *node;
  402. assert((phandle != 0) && (phandle != -1));
  403. if (tree->phandle == phandle) {
  404. if (tree->deleted)
  405. return NULL;
  406. return tree;
  407. }
  408. for_each_child(tree, child) {
  409. node = get_node_by_phandle(child, phandle);
  410. if (node)
  411. return node;
  412. }
  413. return NULL;
  414. }
  415. struct node *get_node_by_ref(struct node *tree, const char *ref)
  416. {
  417. if (streq(ref, "/"))
  418. return tree;
  419. else if (ref[0] == '/')
  420. return get_node_by_path(tree, ref);
  421. else
  422. return get_node_by_label(tree, ref);
  423. }
  424. cell_t get_node_phandle(struct node *root, struct node *node)
  425. {
  426. static cell_t phandle = 1; /* FIXME: ick, static local */
  427. if ((node->phandle != 0) && (node->phandle != -1))
  428. return node->phandle;
  429. while (get_node_by_phandle(root, phandle))
  430. phandle++;
  431. node->phandle = phandle;
  432. if (!get_property(node, "linux,phandle")
  433. && (phandle_format & PHANDLE_LEGACY))
  434. add_property(node,
  435. build_property("linux,phandle",
  436. data_append_cell(empty_data, phandle)));
  437. if (!get_property(node, "phandle")
  438. && (phandle_format & PHANDLE_EPAPR))
  439. add_property(node,
  440. build_property("phandle",
  441. data_append_cell(empty_data, phandle)));
  442. /* If the node *does* have a phandle property, we must
  443. * be dealing with a self-referencing phandle, which will be
  444. * fixed up momentarily in the caller */
  445. return node->phandle;
  446. }
  447. uint32_t guess_boot_cpuid(struct node *tree)
  448. {
  449. struct node *cpus, *bootcpu;
  450. struct property *reg;
  451. cpus = get_node_by_path(tree, "/cpus");
  452. if (!cpus)
  453. return 0;
  454. bootcpu = cpus->children;
  455. if (!bootcpu)
  456. return 0;
  457. reg = get_property(bootcpu, "reg");
  458. if (!reg || (reg->val.len != sizeof(uint32_t)))
  459. return 0;
  460. /* FIXME: Sanity check node? */
  461. return propval_cell(reg);
  462. }
  463. static int cmp_reserve_info(const void *ax, const void *bx)
  464. {
  465. const struct reserve_info *a, *b;
  466. a = *((const struct reserve_info * const *)ax);
  467. b = *((const struct reserve_info * const *)bx);
  468. if (a->re.address < b->re.address)
  469. return -1;
  470. else if (a->re.address > b->re.address)
  471. return 1;
  472. else if (a->re.size < b->re.size)
  473. return -1;
  474. else if (a->re.size > b->re.size)
  475. return 1;
  476. else
  477. return 0;
  478. }
  479. static void sort_reserve_entries(struct dt_info *dti)
  480. {
  481. struct reserve_info *ri, **tbl;
  482. int n = 0, i = 0;
  483. for (ri = dti->reservelist;
  484. ri;
  485. ri = ri->next)
  486. n++;
  487. if (n == 0)
  488. return;
  489. tbl = xmalloc(n * sizeof(*tbl));
  490. for (ri = dti->reservelist;
  491. ri;
  492. ri = ri->next)
  493. tbl[i++] = ri;
  494. qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
  495. dti->reservelist = tbl[0];
  496. for (i = 0; i < (n-1); i++)
  497. tbl[i]->next = tbl[i+1];
  498. tbl[n-1]->next = NULL;
  499. free(tbl);
  500. }
  501. static int cmp_prop(const void *ax, const void *bx)
  502. {
  503. const struct property *a, *b;
  504. a = *((const struct property * const *)ax);
  505. b = *((const struct property * const *)bx);
  506. return strcmp(a->name, b->name);
  507. }
  508. static void sort_properties(struct node *node)
  509. {
  510. int n = 0, i = 0;
  511. struct property *prop, **tbl;
  512. for_each_property_withdel(node, prop)
  513. n++;
  514. if (n == 0)
  515. return;
  516. tbl = xmalloc(n * sizeof(*tbl));
  517. for_each_property_withdel(node, prop)
  518. tbl[i++] = prop;
  519. qsort(tbl, n, sizeof(*tbl), cmp_prop);
  520. node->proplist = tbl[0];
  521. for (i = 0; i < (n-1); i++)
  522. tbl[i]->next = tbl[i+1];
  523. tbl[n-1]->next = NULL;
  524. free(tbl);
  525. }
  526. static int cmp_subnode(const void *ax, const void *bx)
  527. {
  528. const struct node *a, *b;
  529. a = *((const struct node * const *)ax);
  530. b = *((const struct node * const *)bx);
  531. return strcmp(a->name, b->name);
  532. }
  533. static void sort_subnodes(struct node *node)
  534. {
  535. int n = 0, i = 0;
  536. struct node *subnode, **tbl;
  537. for_each_child_withdel(node, subnode)
  538. n++;
  539. if (n == 0)
  540. return;
  541. tbl = xmalloc(n * sizeof(*tbl));
  542. for_each_child_withdel(node, subnode)
  543. tbl[i++] = subnode;
  544. qsort(tbl, n, sizeof(*tbl), cmp_subnode);
  545. node->children = tbl[0];
  546. for (i = 0; i < (n-1); i++)
  547. tbl[i]->next_sibling = tbl[i+1];
  548. tbl[n-1]->next_sibling = NULL;
  549. free(tbl);
  550. }
  551. static void sort_node(struct node *node)
  552. {
  553. struct node *c;
  554. sort_properties(node);
  555. sort_subnodes(node);
  556. for_each_child_withdel(node, c)
  557. sort_node(c);
  558. }
  559. void sort_tree(struct dt_info *dti)
  560. {
  561. sort_reserve_entries(dti);
  562. sort_node(dti->dt);
  563. }
  564. /* utility helper to avoid code duplication */
  565. static struct node *build_and_name_child_node(struct node *parent, char *name)
  566. {
  567. struct node *node;
  568. node = build_node(NULL, NULL);
  569. name_node(node, xstrdup(name));
  570. add_child(parent, node);
  571. return node;
  572. }
  573. static struct node *build_root_node(struct node *dt, char *name)
  574. {
  575. struct node *an;
  576. an = get_subnode(dt, name);
  577. if (!an)
  578. an = build_and_name_child_node(dt, name);
  579. if (!an)
  580. die("Could not build root node /%s\n", name);
  581. return an;
  582. }
  583. static bool any_label_tree(struct dt_info *dti, struct node *node)
  584. {
  585. struct node *c;
  586. if (node->labels)
  587. return true;
  588. for_each_child(node, c)
  589. if (any_label_tree(dti, c))
  590. return true;
  591. return false;
  592. }
  593. static void generate_label_tree_internal(struct dt_info *dti,
  594. struct node *an, struct node *node,
  595. bool allocph)
  596. {
  597. struct node *dt = dti->dt;
  598. struct node *c;
  599. struct property *p;
  600. struct label *l;
  601. /* if there are labels */
  602. if (node->labels) {
  603. /* now add the label in the node */
  604. for_each_label(node->labels, l) {
  605. /* check whether the label already exists */
  606. p = get_property(an, l->label);
  607. if (p) {
  608. fprintf(stderr, "WARNING: label %s already"
  609. " exists in /%s", l->label,
  610. an->name);
  611. continue;
  612. }
  613. /* insert it */
  614. p = build_property(l->label,
  615. data_copy_mem(node->fullpath,
  616. strlen(node->fullpath) + 1));
  617. add_property(an, p);
  618. }
  619. /* force allocation of a phandle for this node */
  620. if (allocph)
  621. (void)get_node_phandle(dt, node);
  622. }
  623. for_each_child(node, c)
  624. generate_label_tree_internal(dti, an, c, allocph);
  625. }
  626. static bool any_fixup_tree(struct dt_info *dti, struct node *node)
  627. {
  628. struct node *c;
  629. struct property *prop;
  630. struct marker *m;
  631. for_each_property(node, prop) {
  632. m = prop->val.markers;
  633. for_each_marker_of_type(m, REF_PHANDLE) {
  634. if (!get_node_by_ref(dti->dt, m->ref))
  635. return true;
  636. }
  637. }
  638. for_each_child(node, c) {
  639. if (any_fixup_tree(dti, c))
  640. return true;
  641. }
  642. return false;
  643. }
  644. static void add_fixup_entry(struct dt_info *dti, struct node *fn,
  645. struct node *node, struct property *prop,
  646. struct marker *m)
  647. {
  648. char *entry;
  649. /* m->ref can only be a REF_PHANDLE, but check anyway */
  650. assert(m->type == REF_PHANDLE);
  651. /* there shouldn't be any ':' in the arguments */
  652. if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
  653. die("arguments should not contain ':'\n");
  654. xasprintf(&entry, "%s:%s:%u",
  655. node->fullpath, prop->name, m->offset);
  656. append_to_property(fn, m->ref, entry, strlen(entry) + 1);
  657. free(entry);
  658. }
  659. static void generate_fixups_tree_internal(struct dt_info *dti,
  660. struct node *fn,
  661. struct node *node)
  662. {
  663. struct node *dt = dti->dt;
  664. struct node *c;
  665. struct property *prop;
  666. struct marker *m;
  667. struct node *refnode;
  668. for_each_property(node, prop) {
  669. m = prop->val.markers;
  670. for_each_marker_of_type(m, REF_PHANDLE) {
  671. refnode = get_node_by_ref(dt, m->ref);
  672. if (!refnode)
  673. add_fixup_entry(dti, fn, node, prop, m);
  674. }
  675. }
  676. for_each_child(node, c)
  677. generate_fixups_tree_internal(dti, fn, c);
  678. }
  679. static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
  680. {
  681. struct node *c;
  682. struct property *prop;
  683. struct marker *m;
  684. for_each_property(node, prop) {
  685. m = prop->val.markers;
  686. for_each_marker_of_type(m, REF_PHANDLE) {
  687. if (get_node_by_ref(dti->dt, m->ref))
  688. return true;
  689. }
  690. }
  691. for_each_child(node, c) {
  692. if (any_local_fixup_tree(dti, c))
  693. return true;
  694. }
  695. return false;
  696. }
  697. static void add_local_fixup_entry(struct dt_info *dti,
  698. struct node *lfn, struct node *node,
  699. struct property *prop, struct marker *m,
  700. struct node *refnode)
  701. {
  702. struct node *wn, *nwn; /* local fixup node, walk node, new */
  703. uint32_t value_32;
  704. char **compp;
  705. int i, depth;
  706. /* walk back retreiving depth */
  707. depth = 0;
  708. for (wn = node; wn; wn = wn->parent)
  709. depth++;
  710. /* allocate name array */
  711. compp = xmalloc(sizeof(*compp) * depth);
  712. /* store names in the array */
  713. for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
  714. compp[i] = wn->name;
  715. /* walk the path components creating nodes if they don't exist */
  716. for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
  717. /* if no node exists, create it */
  718. nwn = get_subnode(wn, compp[i]);
  719. if (!nwn)
  720. nwn = build_and_name_child_node(wn, compp[i]);
  721. }
  722. free(compp);
  723. value_32 = cpu_to_fdt32(m->offset);
  724. append_to_property(wn, prop->name, &value_32, sizeof(value_32));
  725. }
  726. static void generate_local_fixups_tree_internal(struct dt_info *dti,
  727. struct node *lfn,
  728. struct node *node)
  729. {
  730. struct node *dt = dti->dt;
  731. struct node *c;
  732. struct property *prop;
  733. struct marker *m;
  734. struct node *refnode;
  735. for_each_property(node, prop) {
  736. m = prop->val.markers;
  737. for_each_marker_of_type(m, REF_PHANDLE) {
  738. refnode = get_node_by_ref(dt, m->ref);
  739. if (refnode)
  740. add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
  741. }
  742. }
  743. for_each_child(node, c)
  744. generate_local_fixups_tree_internal(dti, lfn, c);
  745. }
  746. void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
  747. {
  748. if (!any_label_tree(dti, dti->dt))
  749. return;
  750. generate_label_tree_internal(dti, build_root_node(dti->dt, name),
  751. dti->dt, allocph);
  752. }
  753. void generate_fixups_tree(struct dt_info *dti, char *name)
  754. {
  755. if (!any_fixup_tree(dti, dti->dt))
  756. return;
  757. generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
  758. dti->dt);
  759. }
  760. void generate_local_fixups_tree(struct dt_info *dti, char *name)
  761. {
  762. if (!any_local_fixup_tree(dti, dti->dt))
  763. return;
  764. generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),
  765. dti->dt);
  766. }