livetree.c 20 KB

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