livetree.c 20 KB

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