livetree.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 *omit_node_if_unused(struct node *node)
  106. {
  107. node->omit_if_unused = 1;
  108. return node;
  109. }
  110. struct node *reference_node(struct node *node)
  111. {
  112. node->is_referenced = 1;
  113. return node;
  114. }
  115. struct node *merge_nodes(struct node *old_node, struct node *new_node)
  116. {
  117. struct property *new_prop, *old_prop;
  118. struct node *new_child, *old_child;
  119. struct label *l;
  120. old_node->deleted = 0;
  121. /* Add new node labels to old node */
  122. for_each_label_withdel(new_node->labels, l)
  123. add_label(&old_node->labels, l->label);
  124. /* Move properties from the new node to the old node. If there
  125. * is a collision, replace the old value with the new */
  126. while (new_node->proplist) {
  127. /* Pop the property off the list */
  128. new_prop = new_node->proplist;
  129. new_node->proplist = new_prop->next;
  130. new_prop->next = NULL;
  131. if (new_prop->deleted) {
  132. delete_property_by_name(old_node, new_prop->name);
  133. free(new_prop);
  134. continue;
  135. }
  136. /* Look for a collision, set new value if there is */
  137. for_each_property_withdel(old_node, old_prop) {
  138. if (streq(old_prop->name, new_prop->name)) {
  139. /* Add new labels to old property */
  140. for_each_label_withdel(new_prop->labels, l)
  141. add_label(&old_prop->labels, l->label);
  142. old_prop->val = new_prop->val;
  143. old_prop->deleted = 0;
  144. free(new_prop);
  145. new_prop = NULL;
  146. break;
  147. }
  148. }
  149. /* if no collision occurred, add property to the old node. */
  150. if (new_prop)
  151. add_property(old_node, new_prop);
  152. }
  153. /* Move the override child nodes into the primary node. If
  154. * there is a collision, then merge the nodes. */
  155. while (new_node->children) {
  156. /* Pop the child node off the list */
  157. new_child = new_node->children;
  158. new_node->children = new_child->next_sibling;
  159. new_child->parent = NULL;
  160. new_child->next_sibling = NULL;
  161. if (new_child->deleted) {
  162. delete_node_by_name(old_node, new_child->name);
  163. free(new_child);
  164. continue;
  165. }
  166. /* Search for a collision. Merge if there is */
  167. for_each_child_withdel(old_node, old_child) {
  168. if (streq(old_child->name, new_child->name)) {
  169. merge_nodes(old_child, new_child);
  170. new_child = NULL;
  171. break;
  172. }
  173. }
  174. /* if no collision occurred, add child to the old node. */
  175. if (new_child)
  176. add_child(old_node, new_child);
  177. }
  178. /* The new node contents are now merged into the old node. Free
  179. * the new node. */
  180. free(new_node);
  181. return old_node;
  182. }
  183. struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
  184. {
  185. static unsigned int next_orphan_fragment = 0;
  186. struct node *node;
  187. struct property *p;
  188. struct data d = empty_data;
  189. char *name;
  190. if (ref[0] == '/') {
  191. d = data_append_data(d, ref, strlen(ref) + 1);
  192. p = build_property("target-path", d);
  193. } else {
  194. d = data_add_marker(d, REF_PHANDLE, ref);
  195. d = data_append_integer(d, 0xffffffff, 32);
  196. p = build_property("target", d);
  197. }
  198. xasprintf(&name, "fragment@%u",
  199. next_orphan_fragment++);
  200. name_node(new_node, "__overlay__");
  201. node = build_node(p, new_node);
  202. name_node(node, name);
  203. add_child(dt, node);
  204. return dt;
  205. }
  206. struct node *chain_node(struct node *first, struct node *list)
  207. {
  208. assert(first->next_sibling == NULL);
  209. first->next_sibling = list;
  210. return first;
  211. }
  212. void add_property(struct node *node, struct property *prop)
  213. {
  214. struct property **p;
  215. prop->next = NULL;
  216. p = &node->proplist;
  217. while (*p)
  218. p = &((*p)->next);
  219. *p = prop;
  220. }
  221. void delete_property_by_name(struct node *node, char *name)
  222. {
  223. struct property *prop = node->proplist;
  224. while (prop) {
  225. if (streq(prop->name, name)) {
  226. delete_property(prop);
  227. return;
  228. }
  229. prop = prop->next;
  230. }
  231. }
  232. void delete_property(struct property *prop)
  233. {
  234. prop->deleted = 1;
  235. delete_labels(&prop->labels);
  236. }
  237. void add_child(struct node *parent, struct node *child)
  238. {
  239. struct node **p;
  240. child->next_sibling = NULL;
  241. child->parent = parent;
  242. p = &parent->children;
  243. while (*p)
  244. p = &((*p)->next_sibling);
  245. *p = child;
  246. }
  247. void delete_node_by_name(struct node *parent, char *name)
  248. {
  249. struct node *node = parent->children;
  250. while (node) {
  251. if (streq(node->name, name)) {
  252. delete_node(node);
  253. return;
  254. }
  255. node = node->next_sibling;
  256. }
  257. }
  258. void delete_node(struct node *node)
  259. {
  260. struct property *prop;
  261. struct node *child;
  262. node->deleted = 1;
  263. for_each_child(node, child)
  264. delete_node(child);
  265. for_each_property(node, prop)
  266. delete_property(prop);
  267. delete_labels(&node->labels);
  268. }
  269. void append_to_property(struct node *node,
  270. char *name, const void *data, int len)
  271. {
  272. struct data d;
  273. struct property *p;
  274. p = get_property(node, name);
  275. if (p) {
  276. d = data_append_data(p->val, data, len);
  277. p->val = d;
  278. } else {
  279. d = data_append_data(empty_data, data, len);
  280. p = build_property(name, d);
  281. add_property(node, p);
  282. }
  283. }
  284. struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
  285. {
  286. struct reserve_info *new = xmalloc(sizeof(*new));
  287. memset(new, 0, sizeof(*new));
  288. new->address = address;
  289. new->size = size;
  290. return new;
  291. }
  292. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  293. struct reserve_info *list)
  294. {
  295. assert(first->next == NULL);
  296. first->next = list;
  297. return first;
  298. }
  299. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  300. struct reserve_info *new)
  301. {
  302. struct reserve_info *last;
  303. new->next = NULL;
  304. if (! list)
  305. return new;
  306. for (last = list; last->next; last = last->next)
  307. ;
  308. last->next = new;
  309. return list;
  310. }
  311. struct dt_info *build_dt_info(unsigned int dtsflags,
  312. struct reserve_info *reservelist,
  313. struct node *tree, uint32_t boot_cpuid_phys)
  314. {
  315. struct dt_info *dti;
  316. dti = xmalloc(sizeof(*dti));
  317. dti->dtsflags = dtsflags;
  318. dti->reservelist = reservelist;
  319. dti->dt = tree;
  320. dti->boot_cpuid_phys = boot_cpuid_phys;
  321. return dti;
  322. }
  323. /*
  324. * Tree accessor functions
  325. */
  326. const char *get_unitname(struct node *node)
  327. {
  328. if (node->name[node->basenamelen] == '\0')
  329. return "";
  330. else
  331. return node->name + node->basenamelen + 1;
  332. }
  333. struct property *get_property(struct node *node, const char *propname)
  334. {
  335. struct property *prop;
  336. for_each_property(node, prop)
  337. if (streq(prop->name, propname))
  338. return prop;
  339. return NULL;
  340. }
  341. cell_t propval_cell(struct property *prop)
  342. {
  343. assert(prop->val.len == sizeof(cell_t));
  344. return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
  345. }
  346. cell_t propval_cell_n(struct property *prop, int n)
  347. {
  348. assert(prop->val.len / sizeof(cell_t) >= n);
  349. return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
  350. }
  351. struct property *get_property_by_label(struct node *tree, const char *label,
  352. struct node **node)
  353. {
  354. struct property *prop;
  355. struct node *c;
  356. *node = tree;
  357. for_each_property(tree, prop) {
  358. struct label *l;
  359. for_each_label(prop->labels, l)
  360. if (streq(l->label, label))
  361. return prop;
  362. }
  363. for_each_child(tree, c) {
  364. prop = get_property_by_label(c, label, node);
  365. if (prop)
  366. return prop;
  367. }
  368. *node = NULL;
  369. return NULL;
  370. }
  371. struct marker *get_marker_label(struct node *tree, const char *label,
  372. struct node **node, struct property **prop)
  373. {
  374. struct marker *m;
  375. struct property *p;
  376. struct node *c;
  377. *node = tree;
  378. for_each_property(tree, p) {
  379. *prop = p;
  380. m = p->val.markers;
  381. for_each_marker_of_type(m, LABEL)
  382. if (streq(m->ref, label))
  383. return m;
  384. }
  385. for_each_child(tree, c) {
  386. m = get_marker_label(c, label, node, prop);
  387. if (m)
  388. return m;
  389. }
  390. *prop = NULL;
  391. *node = NULL;
  392. return NULL;
  393. }
  394. struct node *get_subnode(struct node *node, const char *nodename)
  395. {
  396. struct node *child;
  397. for_each_child(node, child)
  398. if (streq(child->name, nodename))
  399. return child;
  400. return NULL;
  401. }
  402. struct node *get_node_by_path(struct node *tree, const char *path)
  403. {
  404. const char *p;
  405. struct node *child;
  406. if (!path || ! (*path)) {
  407. if (tree->deleted)
  408. return NULL;
  409. return tree;
  410. }
  411. while (path[0] == '/')
  412. path++;
  413. p = strchr(path, '/');
  414. for_each_child(tree, child) {
  415. if (p && (strlen(child->name) == p-path) &&
  416. strprefixeq(path, p - path, child->name))
  417. return get_node_by_path(child, p+1);
  418. else if (!p && streq(path, child->name))
  419. return child;
  420. }
  421. return NULL;
  422. }
  423. struct node *get_node_by_label(struct node *tree, const char *label)
  424. {
  425. struct node *child, *node;
  426. struct label *l;
  427. assert(label && (strlen(label) > 0));
  428. for_each_label(tree->labels, l)
  429. if (streq(l->label, label))
  430. return tree;
  431. for_each_child(tree, child) {
  432. node = get_node_by_label(child, label);
  433. if (node)
  434. return node;
  435. }
  436. return NULL;
  437. }
  438. struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
  439. {
  440. struct node *child, *node;
  441. if ((phandle == 0) || (phandle == -1)) {
  442. assert(generate_fixups);
  443. return NULL;
  444. }
  445. if (tree->phandle == phandle) {
  446. if (tree->deleted)
  447. return NULL;
  448. return tree;
  449. }
  450. for_each_child(tree, child) {
  451. node = get_node_by_phandle(child, phandle);
  452. if (node)
  453. return node;
  454. }
  455. return NULL;
  456. }
  457. struct node *get_node_by_ref(struct node *tree, const char *ref)
  458. {
  459. if (streq(ref, "/"))
  460. return tree;
  461. else if (ref[0] == '/')
  462. return get_node_by_path(tree, ref);
  463. else
  464. return get_node_by_label(tree, ref);
  465. }
  466. cell_t get_node_phandle(struct node *root, struct node *node)
  467. {
  468. static cell_t phandle = 1; /* FIXME: ick, static local */
  469. if ((node->phandle != 0) && (node->phandle != -1))
  470. return node->phandle;
  471. while (get_node_by_phandle(root, phandle))
  472. phandle++;
  473. node->phandle = phandle;
  474. if (!get_property(node, "linux,phandle")
  475. && (phandle_format & PHANDLE_LEGACY))
  476. add_property(node,
  477. build_property("linux,phandle",
  478. data_append_cell(empty_data, phandle)));
  479. if (!get_property(node, "phandle")
  480. && (phandle_format & PHANDLE_EPAPR))
  481. add_property(node,
  482. build_property("phandle",
  483. data_append_cell(empty_data, phandle)));
  484. /* If the node *does* have a phandle property, we must
  485. * be dealing with a self-referencing phandle, which will be
  486. * fixed up momentarily in the caller */
  487. return node->phandle;
  488. }
  489. uint32_t guess_boot_cpuid(struct node *tree)
  490. {
  491. struct node *cpus, *bootcpu;
  492. struct property *reg;
  493. cpus = get_node_by_path(tree, "/cpus");
  494. if (!cpus)
  495. return 0;
  496. bootcpu = cpus->children;
  497. if (!bootcpu)
  498. return 0;
  499. reg = get_property(bootcpu, "reg");
  500. if (!reg || (reg->val.len != sizeof(uint32_t)))
  501. return 0;
  502. /* FIXME: Sanity check node? */
  503. return propval_cell(reg);
  504. }
  505. static int cmp_reserve_info(const void *ax, const void *bx)
  506. {
  507. const struct reserve_info *a, *b;
  508. a = *((const struct reserve_info * const *)ax);
  509. b = *((const struct reserve_info * const *)bx);
  510. if (a->address < b->address)
  511. return -1;
  512. else if (a->address > b->address)
  513. return 1;
  514. else if (a->size < b->size)
  515. return -1;
  516. else if (a->size > b->size)
  517. return 1;
  518. else
  519. return 0;
  520. }
  521. static void sort_reserve_entries(struct dt_info *dti)
  522. {
  523. struct reserve_info *ri, **tbl;
  524. int n = 0, i = 0;
  525. for (ri = dti->reservelist;
  526. ri;
  527. ri = ri->next)
  528. n++;
  529. if (n == 0)
  530. return;
  531. tbl = xmalloc(n * sizeof(*tbl));
  532. for (ri = dti->reservelist;
  533. ri;
  534. ri = ri->next)
  535. tbl[i++] = ri;
  536. qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
  537. dti->reservelist = tbl[0];
  538. for (i = 0; i < (n-1); i++)
  539. tbl[i]->next = tbl[i+1];
  540. tbl[n-1]->next = NULL;
  541. free(tbl);
  542. }
  543. static int cmp_prop(const void *ax, const void *bx)
  544. {
  545. const struct property *a, *b;
  546. a = *((const struct property * const *)ax);
  547. b = *((const struct property * const *)bx);
  548. return strcmp(a->name, b->name);
  549. }
  550. static void sort_properties(struct node *node)
  551. {
  552. int n = 0, i = 0;
  553. struct property *prop, **tbl;
  554. for_each_property_withdel(node, prop)
  555. n++;
  556. if (n == 0)
  557. return;
  558. tbl = xmalloc(n * sizeof(*tbl));
  559. for_each_property_withdel(node, prop)
  560. tbl[i++] = prop;
  561. qsort(tbl, n, sizeof(*tbl), cmp_prop);
  562. node->proplist = tbl[0];
  563. for (i = 0; i < (n-1); i++)
  564. tbl[i]->next = tbl[i+1];
  565. tbl[n-1]->next = NULL;
  566. free(tbl);
  567. }
  568. static int cmp_subnode(const void *ax, const void *bx)
  569. {
  570. const struct node *a, *b;
  571. a = *((const struct node * const *)ax);
  572. b = *((const struct node * const *)bx);
  573. return strcmp(a->name, b->name);
  574. }
  575. static void sort_subnodes(struct node *node)
  576. {
  577. int n = 0, i = 0;
  578. struct node *subnode, **tbl;
  579. for_each_child_withdel(node, subnode)
  580. n++;
  581. if (n == 0)
  582. return;
  583. tbl = xmalloc(n * sizeof(*tbl));
  584. for_each_child_withdel(node, subnode)
  585. tbl[i++] = subnode;
  586. qsort(tbl, n, sizeof(*tbl), cmp_subnode);
  587. node->children = tbl[0];
  588. for (i = 0; i < (n-1); i++)
  589. tbl[i]->next_sibling = tbl[i+1];
  590. tbl[n-1]->next_sibling = NULL;
  591. free(tbl);
  592. }
  593. static void sort_node(struct node *node)
  594. {
  595. struct node *c;
  596. sort_properties(node);
  597. sort_subnodes(node);
  598. for_each_child_withdel(node, c)
  599. sort_node(c);
  600. }
  601. void sort_tree(struct dt_info *dti)
  602. {
  603. sort_reserve_entries(dti);
  604. sort_node(dti->dt);
  605. }
  606. /* utility helper to avoid code duplication */
  607. static struct node *build_and_name_child_node(struct node *parent, char *name)
  608. {
  609. struct node *node;
  610. node = build_node(NULL, NULL);
  611. name_node(node, xstrdup(name));
  612. add_child(parent, node);
  613. return node;
  614. }
  615. static struct node *build_root_node(struct node *dt, char *name)
  616. {
  617. struct node *an;
  618. an = get_subnode(dt, name);
  619. if (!an)
  620. an = build_and_name_child_node(dt, name);
  621. if (!an)
  622. die("Could not build root node /%s\n", name);
  623. return an;
  624. }
  625. static bool any_label_tree(struct dt_info *dti, struct node *node)
  626. {
  627. struct node *c;
  628. if (node->labels)
  629. return true;
  630. for_each_child(node, c)
  631. if (any_label_tree(dti, c))
  632. return true;
  633. return false;
  634. }
  635. static void generate_label_tree_internal(struct dt_info *dti,
  636. struct node *an, struct node *node,
  637. bool allocph)
  638. {
  639. struct node *dt = dti->dt;
  640. struct node *c;
  641. struct property *p;
  642. struct label *l;
  643. /* if there are labels */
  644. if (node->labels) {
  645. /* now add the label in the node */
  646. for_each_label(node->labels, l) {
  647. /* check whether the label already exists */
  648. p = get_property(an, l->label);
  649. if (p) {
  650. fprintf(stderr, "WARNING: label %s already"
  651. " exists in /%s", l->label,
  652. an->name);
  653. continue;
  654. }
  655. /* insert it */
  656. p = build_property(l->label,
  657. data_copy_mem(node->fullpath,
  658. strlen(node->fullpath) + 1));
  659. add_property(an, p);
  660. }
  661. /* force allocation of a phandle for this node */
  662. if (allocph)
  663. (void)get_node_phandle(dt, node);
  664. }
  665. for_each_child(node, c)
  666. generate_label_tree_internal(dti, an, c, allocph);
  667. }
  668. static bool any_fixup_tree(struct dt_info *dti, struct node *node)
  669. {
  670. struct node *c;
  671. struct property *prop;
  672. struct marker *m;
  673. for_each_property(node, prop) {
  674. m = prop->val.markers;
  675. for_each_marker_of_type(m, REF_PHANDLE) {
  676. if (!get_node_by_ref(dti->dt, m->ref))
  677. return true;
  678. }
  679. }
  680. for_each_child(node, c) {
  681. if (any_fixup_tree(dti, c))
  682. return true;
  683. }
  684. return false;
  685. }
  686. static void add_fixup_entry(struct dt_info *dti, struct node *fn,
  687. struct node *node, struct property *prop,
  688. struct marker *m)
  689. {
  690. char *entry;
  691. /* m->ref can only be a REF_PHANDLE, but check anyway */
  692. assert(m->type == REF_PHANDLE);
  693. /* there shouldn't be any ':' in the arguments */
  694. if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
  695. die("arguments should not contain ':'\n");
  696. xasprintf(&entry, "%s:%s:%u",
  697. node->fullpath, prop->name, m->offset);
  698. append_to_property(fn, m->ref, entry, strlen(entry) + 1);
  699. free(entry);
  700. }
  701. static void generate_fixups_tree_internal(struct dt_info *dti,
  702. struct node *fn,
  703. struct node *node)
  704. {
  705. struct node *dt = dti->dt;
  706. struct node *c;
  707. struct property *prop;
  708. struct marker *m;
  709. struct node *refnode;
  710. for_each_property(node, prop) {
  711. m = prop->val.markers;
  712. for_each_marker_of_type(m, REF_PHANDLE) {
  713. refnode = get_node_by_ref(dt, m->ref);
  714. if (!refnode)
  715. add_fixup_entry(dti, fn, node, prop, m);
  716. }
  717. }
  718. for_each_child(node, c)
  719. generate_fixups_tree_internal(dti, fn, c);
  720. }
  721. static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
  722. {
  723. struct node *c;
  724. struct property *prop;
  725. struct marker *m;
  726. for_each_property(node, prop) {
  727. m = prop->val.markers;
  728. for_each_marker_of_type(m, REF_PHANDLE) {
  729. if (get_node_by_ref(dti->dt, m->ref))
  730. return true;
  731. }
  732. }
  733. for_each_child(node, c) {
  734. if (any_local_fixup_tree(dti, c))
  735. return true;
  736. }
  737. return false;
  738. }
  739. static void add_local_fixup_entry(struct dt_info *dti,
  740. struct node *lfn, struct node *node,
  741. struct property *prop, struct marker *m,
  742. struct node *refnode)
  743. {
  744. struct node *wn, *nwn; /* local fixup node, walk node, new */
  745. fdt32_t value_32;
  746. char **compp;
  747. int i, depth;
  748. /* walk back retreiving depth */
  749. depth = 0;
  750. for (wn = node; wn; wn = wn->parent)
  751. depth++;
  752. /* allocate name array */
  753. compp = xmalloc(sizeof(*compp) * depth);
  754. /* store names in the array */
  755. for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
  756. compp[i] = wn->name;
  757. /* walk the path components creating nodes if they don't exist */
  758. for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
  759. /* if no node exists, create it */
  760. nwn = get_subnode(wn, compp[i]);
  761. if (!nwn)
  762. nwn = build_and_name_child_node(wn, compp[i]);
  763. }
  764. free(compp);
  765. value_32 = cpu_to_fdt32(m->offset);
  766. append_to_property(wn, prop->name, &value_32, sizeof(value_32));
  767. }
  768. static void generate_local_fixups_tree_internal(struct dt_info *dti,
  769. struct node *lfn,
  770. struct node *node)
  771. {
  772. struct node *dt = dti->dt;
  773. struct node *c;
  774. struct property *prop;
  775. struct marker *m;
  776. struct node *refnode;
  777. for_each_property(node, prop) {
  778. m = prop->val.markers;
  779. for_each_marker_of_type(m, REF_PHANDLE) {
  780. refnode = get_node_by_ref(dt, m->ref);
  781. if (refnode)
  782. add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
  783. }
  784. }
  785. for_each_child(node, c)
  786. generate_local_fixups_tree_internal(dti, lfn, c);
  787. }
  788. void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
  789. {
  790. if (!any_label_tree(dti, dti->dt))
  791. return;
  792. generate_label_tree_internal(dti, build_root_node(dti->dt, name),
  793. dti->dt, allocph);
  794. }
  795. void generate_fixups_tree(struct dt_info *dti, char *name)
  796. {
  797. if (!any_fixup_tree(dti, dti->dt))
  798. return;
  799. generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
  800. dti->dt);
  801. }
  802. void generate_local_fixups_tree(struct dt_info *dti, char *name)
  803. {
  804. if (!any_local_fixup_tree(dti, dti->dt))
  805. return;
  806. generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),
  807. dti->dt);
  808. }