livetree.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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(const 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 = xstrdup(name);
  37. new->val = val;
  38. new->srcpos = srcpos_copy(srcpos);
  39. return new;
  40. }
  41. struct property *build_property_delete(const char *name)
  42. {
  43. struct property *new = xmalloc(sizeof(*new));
  44. memset(new, 0, sizeof(*new));
  45. new->name = xstrdup(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, const char *name)
  91. {
  92. assert(node->name == NULL);
  93. node->name = xstrdup(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. srcpos_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. free(name);
  199. add_child(dt, node);
  200. return dt;
  201. }
  202. struct node *chain_node(struct node *first, struct node *list)
  203. {
  204. assert(first->next_sibling == NULL);
  205. first->next_sibling = list;
  206. return first;
  207. }
  208. void add_property(struct node *node, struct property *prop)
  209. {
  210. struct property **p;
  211. prop->next = NULL;
  212. p = &node->proplist;
  213. while (*p)
  214. p = &((*p)->next);
  215. *p = prop;
  216. }
  217. void delete_property_by_name(struct node *node, char *name)
  218. {
  219. struct property *prop = node->proplist;
  220. while (prop) {
  221. if (streq(prop->name, name)) {
  222. delete_property(prop);
  223. return;
  224. }
  225. prop = prop->next;
  226. }
  227. }
  228. void delete_property(struct property *prop)
  229. {
  230. prop->deleted = 1;
  231. delete_labels(&prop->labels);
  232. }
  233. void add_child(struct node *parent, struct node *child)
  234. {
  235. struct node **p;
  236. child->next_sibling = NULL;
  237. child->parent = parent;
  238. p = &parent->children;
  239. while (*p)
  240. p = &((*p)->next_sibling);
  241. *p = child;
  242. }
  243. void delete_node_by_name(struct node *parent, char *name)
  244. {
  245. struct node *node = parent->children;
  246. while (node) {
  247. if (streq(node->name, name)) {
  248. delete_node(node);
  249. return;
  250. }
  251. node = node->next_sibling;
  252. }
  253. }
  254. void delete_node(struct node *node)
  255. {
  256. struct property *prop;
  257. struct node *child;
  258. node->deleted = 1;
  259. for_each_child(node, child)
  260. delete_node(child);
  261. for_each_property(node, prop)
  262. delete_property(prop);
  263. delete_labels(&node->labels);
  264. }
  265. void append_to_property(struct node *node,
  266. char *name, const void *data, int len,
  267. enum markertype type)
  268. {
  269. struct property *p;
  270. p = get_property(node, name);
  271. if (!p) {
  272. p = build_property(name, empty_data, NULL);
  273. add_property(node, p);
  274. }
  275. p->val = data_add_marker(p->val, type, name);
  276. p->val = data_append_data(p->val, data, len);
  277. }
  278. static int append_unique_str_to_property(struct node *node,
  279. char *name, const char *data, int len)
  280. {
  281. struct property *p;
  282. p = get_property(node, name);
  283. if (p) {
  284. const char *s;
  285. if (p->val.len && p->val.val[p->val.len - 1] != '\0')
  286. /* The current content doesn't look like a string */
  287. return -1;
  288. for (s = p->val.val; s < p->val.val + p->val.len; s = strchr(s, '\0') + 1) {
  289. if (strcmp(data, s) == 0)
  290. /* data already contained in node.name */
  291. return 0;
  292. }
  293. } else {
  294. p = build_property(name, empty_data, NULL);
  295. add_property(node, p);
  296. }
  297. p->val = data_add_marker(p->val, TYPE_STRING, name);
  298. p->val = data_append_data(p->val, data, len);
  299. return 0;
  300. }
  301. static int append_unique_u32_to_property(struct node *node, char *name, fdt32_t value)
  302. {
  303. struct property *p;
  304. p = get_property(node, name);
  305. if (p) {
  306. const fdt32_t *v, *val_end = (const fdt32_t *)p->val.val + p->val.len / 4;
  307. if (p->val.len % 4 != 0)
  308. /* The current content doesn't look like a u32 array */
  309. return -1;
  310. for (v = (const void *)p->val.val; v < val_end; v++) {
  311. if (*v == value)
  312. /* value already contained */
  313. return 0;
  314. }
  315. } else {
  316. p = build_property(name, empty_data, NULL);
  317. add_property(node, p);
  318. }
  319. p->val = data_add_marker(p->val, TYPE_UINT32, name);
  320. p->val = data_append_data(p->val, &value, 4);
  321. return 0;
  322. }
  323. struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
  324. {
  325. struct reserve_info *new = xmalloc(sizeof(*new));
  326. memset(new, 0, sizeof(*new));
  327. new->address = address;
  328. new->size = size;
  329. return new;
  330. }
  331. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  332. struct reserve_info *list)
  333. {
  334. assert(first->next == NULL);
  335. first->next = list;
  336. return first;
  337. }
  338. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  339. struct reserve_info *new)
  340. {
  341. struct reserve_info *last;
  342. new->next = NULL;
  343. if (! list)
  344. return new;
  345. for (last = list; last->next; last = last->next)
  346. ;
  347. last->next = new;
  348. return list;
  349. }
  350. struct dt_info *build_dt_info(unsigned int dtsflags,
  351. struct reserve_info *reservelist,
  352. struct node *tree, uint32_t boot_cpuid_phys)
  353. {
  354. struct dt_info *dti;
  355. dti = xmalloc(sizeof(*dti));
  356. dti->dtsflags = dtsflags;
  357. dti->reservelist = reservelist;
  358. dti->dt = tree;
  359. dti->boot_cpuid_phys = boot_cpuid_phys;
  360. return dti;
  361. }
  362. /*
  363. * Tree accessor functions
  364. */
  365. const char *get_unitname(struct node *node)
  366. {
  367. if (node->name[node->basenamelen] == '\0')
  368. return "";
  369. else
  370. return node->name + node->basenamelen + 1;
  371. }
  372. struct property *get_property(struct node *node, const char *propname)
  373. {
  374. struct property *prop;
  375. for_each_property(node, prop)
  376. if (streq(prop->name, propname))
  377. return prop;
  378. return NULL;
  379. }
  380. cell_t propval_cell(struct property *prop)
  381. {
  382. assert(prop->val.len == sizeof(cell_t));
  383. return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
  384. }
  385. cell_t propval_cell_n(struct property *prop, unsigned int n)
  386. {
  387. assert(prop->val.len / sizeof(cell_t) > n);
  388. return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
  389. }
  390. struct property *get_property_by_label(struct node *tree, const char *label,
  391. struct node **node)
  392. {
  393. struct property *prop;
  394. struct node *c;
  395. *node = tree;
  396. for_each_property(tree, prop) {
  397. struct label *l;
  398. for_each_label(prop->labels, l)
  399. if (streq(l->label, label))
  400. return prop;
  401. }
  402. for_each_child(tree, c) {
  403. prop = get_property_by_label(c, label, node);
  404. if (prop)
  405. return prop;
  406. }
  407. *node = NULL;
  408. return NULL;
  409. }
  410. struct marker *get_marker_label(struct node *tree, const char *label,
  411. struct node **node, struct property **prop)
  412. {
  413. struct marker *m;
  414. struct property *p;
  415. struct node *c;
  416. *node = tree;
  417. for_each_property(tree, p) {
  418. *prop = p;
  419. m = p->val.markers;
  420. for_each_marker_of_type(m, LABEL)
  421. if (streq(m->ref, label))
  422. return m;
  423. }
  424. for_each_child(tree, c) {
  425. m = get_marker_label(c, label, node, prop);
  426. if (m)
  427. return m;
  428. }
  429. *prop = NULL;
  430. *node = NULL;
  431. return NULL;
  432. }
  433. struct node *get_subnode(struct node *node, const char *nodename)
  434. {
  435. struct node *child;
  436. for_each_child(node, child)
  437. if (streq(child->name, nodename) && !child->deleted)
  438. return child;
  439. return NULL;
  440. }
  441. struct node *get_node_by_path(struct node *tree, const char *path)
  442. {
  443. const char *p;
  444. struct node *child;
  445. if (!path || ! (*path)) {
  446. if (tree->deleted)
  447. return NULL;
  448. return tree;
  449. }
  450. while (path[0] == '/')
  451. path++;
  452. p = strchr(path, '/');
  453. for_each_child(tree, child) {
  454. if (p && strprefixeq(path, (size_t)(p - path), child->name))
  455. return get_node_by_path(child, p+1);
  456. else if (!p && streq(path, child->name))
  457. return child;
  458. }
  459. return NULL;
  460. }
  461. struct node *get_node_by_label(struct node *tree, const char *label)
  462. {
  463. struct node *child, *node;
  464. struct label *l;
  465. assert(label && (strlen(label) > 0));
  466. for_each_label(tree->labels, l)
  467. if (streq(l->label, label))
  468. return tree;
  469. for_each_child(tree, child) {
  470. node = get_node_by_label(child, label);
  471. if (node)
  472. return node;
  473. }
  474. return NULL;
  475. }
  476. struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
  477. {
  478. struct node *child, *node;
  479. if (!phandle_is_valid(phandle)) {
  480. assert(generate_fixups);
  481. return NULL;
  482. }
  483. if (tree->phandle == phandle) {
  484. if (tree->deleted)
  485. return NULL;
  486. return tree;
  487. }
  488. for_each_child(tree, child) {
  489. node = get_node_by_phandle(child, phandle);
  490. if (node)
  491. return node;
  492. }
  493. return NULL;
  494. }
  495. struct node *get_node_by_ref(struct node *tree, const char *ref)
  496. {
  497. struct node *target = tree;
  498. const char *label = NULL, *path = NULL;
  499. if (streq(ref, "/"))
  500. return tree;
  501. if (ref[0] == '/')
  502. path = ref;
  503. else
  504. label = ref;
  505. if (label) {
  506. const char *slash = strchr(label, '/');
  507. char *buf = NULL;
  508. if (slash) {
  509. buf = xstrndup(label, slash - label);
  510. label = buf;
  511. path = slash + 1;
  512. }
  513. target = get_node_by_label(tree, label);
  514. free(buf);
  515. if (!target)
  516. return NULL;
  517. }
  518. if (path)
  519. target = get_node_by_path(target, path);
  520. return target;
  521. }
  522. static void add_phandle_property(struct node *node,
  523. const char *name, int format)
  524. {
  525. struct data d;
  526. if (!(phandle_format & format))
  527. return;
  528. if (get_property(node, name))
  529. return;
  530. d = data_add_marker(empty_data, TYPE_UINT32, NULL);
  531. d = data_append_cell(d, node->phandle);
  532. add_property(node, build_property(name, d, NULL));
  533. }
  534. cell_t get_node_phandle(struct node *root, struct node *node)
  535. {
  536. static cell_t phandle = 1; /* FIXME: ick, static local */
  537. if (phandle_is_valid(node->phandle))
  538. return node->phandle;
  539. while (get_node_by_phandle(root, phandle))
  540. phandle++;
  541. node->phandle = phandle;
  542. add_phandle_property(node, "linux,phandle", PHANDLE_LEGACY);
  543. add_phandle_property(node, "phandle", PHANDLE_EPAPR);
  544. /* If the node *does* have a phandle property, we must
  545. * be dealing with a self-referencing phandle, which will be
  546. * fixed up momentarily in the caller */
  547. return node->phandle;
  548. }
  549. uint32_t guess_boot_cpuid(struct node *tree)
  550. {
  551. struct node *cpus, *bootcpu;
  552. struct property *reg;
  553. cpus = get_node_by_path(tree, "/cpus");
  554. if (!cpus)
  555. return 0;
  556. bootcpu = cpus->children;
  557. if (!bootcpu)
  558. return 0;
  559. reg = get_property(bootcpu, "reg");
  560. if (!reg || (reg->val.len != sizeof(uint32_t)))
  561. return 0;
  562. /* FIXME: Sanity check node? */
  563. return propval_cell(reg);
  564. }
  565. static int cmp_reserve_info(const void *ax, const void *bx)
  566. {
  567. const struct reserve_info *a, *b;
  568. a = *((const struct reserve_info * const *)ax);
  569. b = *((const struct reserve_info * const *)bx);
  570. if (a->address < b->address)
  571. return -1;
  572. else if (a->address > b->address)
  573. return 1;
  574. else if (a->size < b->size)
  575. return -1;
  576. else if (a->size > b->size)
  577. return 1;
  578. else
  579. return 0;
  580. }
  581. static void sort_reserve_entries(struct dt_info *dti)
  582. {
  583. struct reserve_info *ri, **tbl;
  584. int n = 0, i = 0;
  585. for (ri = dti->reservelist;
  586. ri;
  587. ri = ri->next)
  588. n++;
  589. if (n == 0)
  590. return;
  591. tbl = xmalloc(n * sizeof(*tbl));
  592. for (ri = dti->reservelist;
  593. ri;
  594. ri = ri->next)
  595. tbl[i++] = ri;
  596. qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
  597. dti->reservelist = tbl[0];
  598. for (i = 0; i < (n-1); i++)
  599. tbl[i]->next = tbl[i+1];
  600. tbl[n-1]->next = NULL;
  601. free(tbl);
  602. }
  603. static int cmp_prop(const void *ax, const void *bx)
  604. {
  605. const struct property *a, *b;
  606. a = *((const struct property * const *)ax);
  607. b = *((const struct property * const *)bx);
  608. return strcmp(a->name, b->name);
  609. }
  610. static void sort_properties(struct node *node)
  611. {
  612. int n = 0, i = 0;
  613. struct property *prop, **tbl;
  614. for_each_property_withdel(node, prop)
  615. n++;
  616. if (n == 0)
  617. return;
  618. tbl = xmalloc(n * sizeof(*tbl));
  619. for_each_property_withdel(node, prop)
  620. tbl[i++] = prop;
  621. qsort(tbl, n, sizeof(*tbl), cmp_prop);
  622. node->proplist = tbl[0];
  623. for (i = 0; i < (n-1); i++)
  624. tbl[i]->next = tbl[i+1];
  625. tbl[n-1]->next = NULL;
  626. free(tbl);
  627. }
  628. static int cmp_subnode(const void *ax, const void *bx)
  629. {
  630. const struct node *a, *b;
  631. a = *((const struct node * const *)ax);
  632. b = *((const struct node * const *)bx);
  633. return strcmp(a->name, b->name);
  634. }
  635. static void sort_subnodes(struct node *node)
  636. {
  637. int n = 0, i = 0;
  638. struct node *subnode, **tbl;
  639. for_each_child_withdel(node, subnode)
  640. n++;
  641. if (n == 0)
  642. return;
  643. tbl = xmalloc(n * sizeof(*tbl));
  644. for_each_child_withdel(node, subnode)
  645. tbl[i++] = subnode;
  646. qsort(tbl, n, sizeof(*tbl), cmp_subnode);
  647. node->children = tbl[0];
  648. for (i = 0; i < (n-1); i++)
  649. tbl[i]->next_sibling = tbl[i+1];
  650. tbl[n-1]->next_sibling = NULL;
  651. free(tbl);
  652. }
  653. static void sort_node(struct node *node)
  654. {
  655. struct node *c;
  656. sort_properties(node);
  657. sort_subnodes(node);
  658. for_each_child_withdel(node, c)
  659. sort_node(c);
  660. }
  661. void sort_tree(struct dt_info *dti)
  662. {
  663. sort_reserve_entries(dti);
  664. sort_node(dti->dt);
  665. }
  666. /* utility helper to avoid code duplication */
  667. static struct node *build_and_name_child_node(struct node *parent, const char *name)
  668. {
  669. struct node *node;
  670. node = build_node(NULL, NULL, NULL);
  671. name_node(node, name);
  672. add_child(parent, node);
  673. return node;
  674. }
  675. static struct node *build_root_node(struct node *dt, const char *name)
  676. {
  677. struct node *an;
  678. an = get_subnode(dt, name);
  679. if (!an)
  680. an = build_and_name_child_node(dt, name);
  681. if (!an)
  682. die("Could not build root node /%s\n", name);
  683. return an;
  684. }
  685. static bool any_label_tree(struct dt_info *dti, struct node *node)
  686. {
  687. struct node *c;
  688. if (node->labels)
  689. return true;
  690. for_each_child(node, c)
  691. if (any_label_tree(dti, c))
  692. return true;
  693. return false;
  694. }
  695. static void generate_label_tree_internal(struct dt_info *dti,
  696. struct node *an, struct node *node,
  697. bool allocph)
  698. {
  699. struct node *dt = dti->dt;
  700. struct node *c;
  701. struct property *p;
  702. struct label *l;
  703. /* if there are labels */
  704. if (node->labels) {
  705. /* now add the label in the node */
  706. for_each_label(node->labels, l) {
  707. /* check whether the label already exists */
  708. p = get_property(an, l->label);
  709. if (p) {
  710. fprintf(stderr, "WARNING: label %s already"
  711. " exists in /%s", l->label,
  712. an->name);
  713. continue;
  714. }
  715. /* insert it */
  716. p = build_property(l->label,
  717. data_copy_escape_string(node->fullpath,
  718. strlen(node->fullpath)),
  719. NULL);
  720. add_property(an, p);
  721. }
  722. /* force allocation of a phandle for this node */
  723. if (allocph)
  724. (void)get_node_phandle(dt, node);
  725. }
  726. for_each_child(node, c)
  727. generate_label_tree_internal(dti, an, c, allocph);
  728. }
  729. static bool any_fixup_tree(struct dt_info *dti, struct node *node)
  730. {
  731. struct node *c;
  732. struct property *prop;
  733. struct marker *m;
  734. for_each_property(node, prop) {
  735. m = prop->val.markers;
  736. for_each_marker_of_type(m, REF_PHANDLE) {
  737. if (!get_node_by_ref(dti->dt, m->ref))
  738. return true;
  739. }
  740. }
  741. for_each_child(node, c) {
  742. if (any_fixup_tree(dti, c))
  743. return true;
  744. }
  745. return false;
  746. }
  747. static int add_fixup_entry(struct dt_info *dti, struct node *fn,
  748. struct node *node, struct property *prop,
  749. struct marker *m)
  750. {
  751. char *entry;
  752. int ret;
  753. /* m->ref can only be a REF_PHANDLE, but check anyway */
  754. assert(m->type == REF_PHANDLE);
  755. /* The format only permits fixups for references to label, not
  756. * references to path */
  757. if (strchr(m->ref, '/'))
  758. die("Can't generate fixup for reference to path &{%s}\n",
  759. m->ref);
  760. /* there shouldn't be any ':' in the arguments */
  761. if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
  762. die("arguments should not contain ':'\n");
  763. xasprintf(&entry, "%s:%s:%u",
  764. node->fullpath, prop->name, m->offset);
  765. ret = append_unique_str_to_property(fn, m->ref, entry, strlen(entry) + 1);
  766. free(entry);
  767. return ret;
  768. }
  769. static int generate_fixups_tree_internal(struct dt_info *dti,
  770. struct node *fn,
  771. struct node *node)
  772. {
  773. struct node *dt = dti->dt;
  774. struct node *c;
  775. struct property *prop;
  776. struct marker *m;
  777. struct node *refnode;
  778. int ret = 0;
  779. for_each_property(node, prop) {
  780. m = prop->val.markers;
  781. for_each_marker_of_type(m, REF_PHANDLE) {
  782. refnode = get_node_by_ref(dt, m->ref);
  783. if (!refnode)
  784. if (add_fixup_entry(dti, fn, node, prop, m))
  785. ret = -1;
  786. }
  787. }
  788. for_each_child(node, c)
  789. if (generate_fixups_tree_internal(dti, fn, c))
  790. ret = -1;
  791. return ret;
  792. }
  793. static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
  794. {
  795. struct node *c;
  796. struct property *prop;
  797. struct marker *m;
  798. for_each_property(node, prop) {
  799. m = prop->val.markers;
  800. for_each_marker_of_type(m, REF_PHANDLE) {
  801. if (get_node_by_ref(dti->dt, m->ref))
  802. return true;
  803. }
  804. }
  805. for_each_child(node, c) {
  806. if (any_local_fixup_tree(dti, c))
  807. return true;
  808. }
  809. return false;
  810. }
  811. static int add_local_fixup_entry(struct dt_info *dti,
  812. struct node *lfn, struct node *node,
  813. struct property *prop, struct marker *m,
  814. struct node *refnode)
  815. {
  816. struct node *wn, *nwn; /* local fixup node, walk node, new */
  817. fdt32_t value_32;
  818. char **compp;
  819. int i, depth;
  820. /* walk back retrieving depth */
  821. depth = 0;
  822. for (wn = node; wn; wn = wn->parent)
  823. depth++;
  824. /* allocate name array */
  825. compp = xmalloc(sizeof(*compp) * depth);
  826. /* store names in the array */
  827. for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
  828. compp[i] = wn->name;
  829. /* walk the path components creating nodes if they don't exist */
  830. for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
  831. /* if no node exists, create it */
  832. nwn = build_root_node(wn, compp[i]);
  833. }
  834. free(compp);
  835. value_32 = cpu_to_fdt32(m->offset);
  836. return append_unique_u32_to_property(wn, prop->name, value_32);
  837. }
  838. static int generate_local_fixups_tree_internal(struct dt_info *dti,
  839. struct node *lfn,
  840. struct node *node)
  841. {
  842. struct node *dt = dti->dt;
  843. struct node *c;
  844. struct property *prop;
  845. struct marker *m;
  846. struct node *refnode;
  847. int ret = 0;
  848. for_each_property(node, prop) {
  849. m = prop->val.markers;
  850. for_each_marker_of_type(m, REF_PHANDLE) {
  851. refnode = get_node_by_ref(dt, m->ref);
  852. if (refnode)
  853. if (add_local_fixup_entry(dti, lfn, node, prop, m, refnode))
  854. ret = -1;
  855. }
  856. }
  857. for_each_child(node, c)
  858. if (generate_local_fixups_tree_internal(dti, lfn, c))
  859. ret = -1;
  860. return ret;
  861. }
  862. void generate_labels_from_tree(struct dt_info *dti, const char *name)
  863. {
  864. struct node *an;
  865. struct property *p;
  866. an = get_subnode(dti->dt, name);
  867. if (!an)
  868. return;
  869. for_each_property(an, p) {
  870. struct node *labeled_node;
  871. labeled_node = get_node_by_path(dti->dt, p->val.val);
  872. if (labeled_node)
  873. add_label(&labeled_node->labels, p->name);
  874. else if (quiet < 1)
  875. fprintf(stderr, "Warning: Path %s referenced in property %s/%s missing",
  876. p->val.val, name, p->name);
  877. }
  878. }
  879. void generate_label_tree(struct dt_info *dti, const char *name, bool allocph)
  880. {
  881. if (!any_label_tree(dti, dti->dt))
  882. return;
  883. generate_label_tree_internal(dti, build_root_node(dti->dt, name),
  884. dti->dt, allocph);
  885. }
  886. void generate_fixups_tree(struct dt_info *dti, const char *name)
  887. {
  888. if (!any_fixup_tree(dti, dti->dt))
  889. return;
  890. if (generate_fixups_tree_internal(dti, build_root_node(dti->dt, name), dti->dt))
  891. fprintf(stderr,
  892. "Warning: Preexisting data in %s malformed, some content could not be added.\n",
  893. name);
  894. }
  895. void fixup_phandles(struct dt_info *dti, const char *name)
  896. {
  897. struct node *an;
  898. struct property *fp;
  899. an = get_subnode(dti->dt, name);
  900. if (!an)
  901. return;
  902. for_each_property(an, fp) {
  903. char *fnext = fp->val.val;
  904. char *fv;
  905. unsigned int fl;
  906. while ((fl = fp->val.len - (fnext - fp->val.val))) {
  907. char *propname, *soffset;
  908. struct node *n;
  909. struct property *p;
  910. long offset;
  911. fv = fnext;
  912. fnext = memchr(fv, 0, fl);
  913. if (!fnext) {
  914. if (quiet < 1)
  915. fprintf(stderr, "Warning: Malformed fixup entry for label %s\n",
  916. fp->name);
  917. break;
  918. }
  919. fnext += 1;
  920. propname = memchr(fv, ':', fnext - 1 - fv);
  921. if (!propname) {
  922. if (quiet < 1)
  923. fprintf(stderr, "Warning: Malformed fixup entry for label %s\n",
  924. fp->name);
  925. continue;
  926. }
  927. propname++;
  928. soffset = memchr(propname, ':', fnext - 1 - propname);
  929. if (!soffset) {
  930. if (quiet < 1)
  931. fprintf(stderr, "Warning: Malformed fixup entry for label %s\n",
  932. fp->name);
  933. continue;
  934. }
  935. soffset++;
  936. /*
  937. * temporarily modify the property to not have to create
  938. * a copy for the node path.
  939. */
  940. *(propname - 1) = '\0';
  941. n = get_node_by_path(dti->dt, fv);
  942. if (!n && quiet < 1)
  943. fprintf(stderr, "Warning: Label %s references non-existing node %s\n",
  944. fp->name, fv);
  945. *(propname - 1) = ':';
  946. if (!n)
  947. continue;
  948. /*
  949. * temporarily modify the property to not have to create
  950. * a copy for the property name.
  951. */
  952. *(soffset - 1) = '\0';
  953. p = get_property(n, propname);
  954. if (!p && quiet < 1)
  955. fprintf(stderr, "Warning: Label %s references non-existing property %s in node %s\n",
  956. fp->name, n->fullpath, propname);
  957. *(soffset - 1) = ':';
  958. if (!p)
  959. continue;
  960. offset = strtol(soffset, NULL, 0);
  961. if (offset < 0 || offset + 4 > p->val.len) {
  962. if (quiet < 1)
  963. fprintf(stderr,
  964. "Warning: Label %s contains invalid offset for property %s in node %s\n",
  965. fp->name, p->name, n->fullpath);
  966. continue;
  967. }
  968. property_add_marker(p, REF_PHANDLE, offset, fp->name);
  969. }
  970. }
  971. }
  972. void generate_local_fixups_tree(struct dt_info *dti, const char *name)
  973. {
  974. if (!any_local_fixup_tree(dti, dti->dt))
  975. return;
  976. if (generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name), dti->dt))
  977. fprintf(stderr,
  978. "Warning: Preexisting data in %s malformed, some content could not be added.\n",
  979. name);
  980. }
  981. static void local_fixup_phandles_node(struct dt_info *dti, struct node *lf, struct node *n)
  982. {
  983. struct property *lfp;
  984. struct node *lfsubnode;
  985. for_each_property(lf, lfp) {
  986. struct property *p = get_property(n, lfp->name);
  987. fdt32_t *offsets = (fdt32_t *)lfp->val.val;
  988. size_t i;
  989. if (!p) {
  990. if (quiet < 1)
  991. fprintf(stderr, "Warning: Property %s in %s referenced in __local_fixups__ missing\n",
  992. lfp->name, n->fullpath);
  993. continue;
  994. }
  995. /*
  996. * Each property in the __local_fixups__ tree is a concatenation
  997. * of offsets, so it must be a multiple of sizeof(fdt32_t).
  998. */
  999. if (lfp->val.len % sizeof(fdt32_t)) {
  1000. if (quiet < 1)
  1001. fprintf(stderr, "Warning: property %s in /__local_fixups__%s malformed\n",
  1002. lfp->name, n->fullpath);
  1003. continue;
  1004. }
  1005. for (i = 0; i < lfp->val.len / sizeof(fdt32_t); i++)
  1006. add_phandle_marker(dti, p, dtb_ld32(offsets + i));
  1007. }
  1008. for_each_child(lf, lfsubnode) {
  1009. struct node *subnode = get_subnode(n, lfsubnode->name);
  1010. if (!subnode) {
  1011. if (quiet < 1)
  1012. fprintf(stderr, "Warning: node %s/%s referenced in __local_fixups__ missing\n",
  1013. lfsubnode->name, n->fullpath);
  1014. continue;
  1015. }
  1016. local_fixup_phandles_node(dti, lfsubnode, subnode);
  1017. }
  1018. }
  1019. void local_fixup_phandles(struct dt_info *dti, const char *name)
  1020. {
  1021. struct node *an;
  1022. an = get_subnode(dti->dt, name);
  1023. if (!an)
  1024. return;
  1025. local_fixup_phandles_node(dti, an, dti->dt);
  1026. }