livetree.c 20 KB

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