flatdevtree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * Copyright Pantelis Antoniou 2006
  17. * Copyright (C) IBM Corporation 2006
  18. *
  19. * Authors: Pantelis Antoniou <pantelis@embeddedalley.com>
  20. * Hollis Blanchard <hollisb@us.ibm.com>
  21. * Mark A. Greer <mgreer@mvista.com>
  22. * Paul Mackerras <paulus@samba.org>
  23. */
  24. #include <string.h>
  25. #include <stddef.h>
  26. #include "flatdevtree.h"
  27. #include "flatdevtree_env.h"
  28. #define _ALIGN(x, al) (((x) + (al) - 1) & ~((al) - 1))
  29. static char *ft_root_node(struct ft_cxt *cxt)
  30. {
  31. return cxt->rgn[FT_STRUCT].start;
  32. }
  33. /* Routines for keeping node ptrs returned by ft_find_device current */
  34. /* First entry not used b/c it would return 0 and be taken as NULL/error */
  35. static void *ft_get_phandle(struct ft_cxt *cxt, char *node)
  36. {
  37. unsigned int i;
  38. if (!node)
  39. return NULL;
  40. for (i = 1; i < cxt->nodes_used; i++) /* already there? */
  41. if (cxt->node_tbl[i] == node)
  42. return (void *)i;
  43. if (cxt->nodes_used < cxt->node_max) {
  44. cxt->node_tbl[cxt->nodes_used] = node;
  45. return (void *)cxt->nodes_used++;
  46. }
  47. return NULL;
  48. }
  49. static char *ft_node_ph2node(struct ft_cxt *cxt, const void *phandle)
  50. {
  51. unsigned int i = (unsigned int)phandle;
  52. if (i < cxt->nodes_used)
  53. return cxt->node_tbl[i];
  54. return NULL;
  55. }
  56. static void ft_node_update_before(struct ft_cxt *cxt, char *addr, int shift)
  57. {
  58. unsigned int i;
  59. if (shift == 0)
  60. return;
  61. for (i = 1; i < cxt->nodes_used; i++)
  62. if (cxt->node_tbl[i] < addr)
  63. cxt->node_tbl[i] += shift;
  64. }
  65. static void ft_node_update_after(struct ft_cxt *cxt, char *addr, int shift)
  66. {
  67. unsigned int i;
  68. if (shift == 0)
  69. return;
  70. for (i = 1; i < cxt->nodes_used; i++)
  71. if (cxt->node_tbl[i] >= addr)
  72. cxt->node_tbl[i] += shift;
  73. }
  74. /* Struct used to return info from ft_next() */
  75. struct ft_atom {
  76. u32 tag;
  77. const char *name;
  78. void *data;
  79. u32 size;
  80. };
  81. /* Set ptrs to current one's info; return addr of next one */
  82. static char *ft_next(struct ft_cxt *cxt, char *p, struct ft_atom *ret)
  83. {
  84. u32 sz;
  85. if (p >= cxt->rgn[FT_STRUCT].start + cxt->rgn[FT_STRUCT].size)
  86. return NULL;
  87. ret->tag = be32_to_cpu(*(u32 *) p);
  88. p += 4;
  89. switch (ret->tag) { /* Tag */
  90. case OF_DT_BEGIN_NODE:
  91. ret->name = p;
  92. ret->data = (void *)(p - 4); /* start of node */
  93. p += _ALIGN(strlen(p) + 1, 4);
  94. break;
  95. case OF_DT_PROP:
  96. ret->size = sz = be32_to_cpu(*(u32 *) p);
  97. ret->name = cxt->str_anchor + be32_to_cpu(*(u32 *) (p + 4));
  98. ret->data = (void *)(p + 8);
  99. p += 8 + _ALIGN(sz, 4);
  100. break;
  101. case OF_DT_END_NODE:
  102. case OF_DT_NOP:
  103. break;
  104. case OF_DT_END:
  105. default:
  106. p = NULL;
  107. break;
  108. }
  109. return p;
  110. }
  111. #define HDR_SIZE _ALIGN(sizeof(struct boot_param_header), 8)
  112. #define EXPAND_INCR 1024 /* alloc this much extra when expanding */
  113. /* Copy the tree to a newly-allocated region and put things in order */
  114. static int ft_reorder(struct ft_cxt *cxt, int nextra)
  115. {
  116. unsigned long tot;
  117. enum ft_rgn_id r;
  118. char *p, *pend;
  119. int stroff;
  120. tot = HDR_SIZE + EXPAND_INCR;
  121. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r)
  122. tot += cxt->rgn[r].size;
  123. if (nextra > 0)
  124. tot += nextra;
  125. tot = _ALIGN(tot, 8);
  126. if (!cxt->realloc)
  127. return 0;
  128. p = cxt->realloc(NULL, tot);
  129. if (!p)
  130. return 0;
  131. memcpy(p, cxt->bph, sizeof(struct boot_param_header));
  132. /* offsets get fixed up later */
  133. cxt->bph = (struct boot_param_header *)p;
  134. cxt->max_size = tot;
  135. pend = p + tot;
  136. p += HDR_SIZE;
  137. memcpy(p, cxt->rgn[FT_RSVMAP].start, cxt->rgn[FT_RSVMAP].size);
  138. cxt->rgn[FT_RSVMAP].start = p;
  139. p += cxt->rgn[FT_RSVMAP].size;
  140. memcpy(p, cxt->rgn[FT_STRUCT].start, cxt->rgn[FT_STRUCT].size);
  141. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  142. p - cxt->rgn[FT_STRUCT].start);
  143. cxt->p += p - cxt->rgn[FT_STRUCT].start;
  144. cxt->rgn[FT_STRUCT].start = p;
  145. p = pend - cxt->rgn[FT_STRINGS].size;
  146. memcpy(p, cxt->rgn[FT_STRINGS].start, cxt->rgn[FT_STRINGS].size);
  147. stroff = cxt->str_anchor - cxt->rgn[FT_STRINGS].start;
  148. cxt->rgn[FT_STRINGS].start = p;
  149. cxt->str_anchor = p + stroff;
  150. cxt->isordered = 1;
  151. return 1;
  152. }
  153. static inline char *prev_end(struct ft_cxt *cxt, enum ft_rgn_id r)
  154. {
  155. if (r > FT_RSVMAP)
  156. return cxt->rgn[r - 1].start + cxt->rgn[r - 1].size;
  157. return (char *)cxt->bph + HDR_SIZE;
  158. }
  159. static inline char *next_start(struct ft_cxt *cxt, enum ft_rgn_id r)
  160. {
  161. if (r < FT_STRINGS)
  162. return cxt->rgn[r + 1].start;
  163. return (char *)cxt->bph + cxt->max_size;
  164. }
  165. /*
  166. * See if we can expand region rgn by nextra bytes by using up
  167. * free space after or before the region.
  168. */
  169. static int ft_shuffle(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
  170. int nextra)
  171. {
  172. char *p = *pp;
  173. char *rgn_start, *rgn_end;
  174. rgn_start = cxt->rgn[rgn].start;
  175. rgn_end = rgn_start + cxt->rgn[rgn].size;
  176. if (nextra <= 0 || rgn_end + nextra <= next_start(cxt, rgn)) {
  177. /* move following stuff */
  178. if (p < rgn_end) {
  179. if (nextra < 0)
  180. memmove(p, p - nextra, rgn_end - p + nextra);
  181. else
  182. memmove(p + nextra, p, rgn_end - p);
  183. if (rgn == FT_STRUCT)
  184. ft_node_update_after(cxt, p, nextra);
  185. }
  186. cxt->rgn[rgn].size += nextra;
  187. if (rgn == FT_STRINGS)
  188. /* assumes strings only added at beginning */
  189. cxt->str_anchor += nextra;
  190. return 1;
  191. }
  192. if (prev_end(cxt, rgn) <= rgn_start - nextra) {
  193. /* move preceding stuff */
  194. if (p > rgn_start) {
  195. memmove(rgn_start - nextra, rgn_start, p - rgn_start);
  196. if (rgn == FT_STRUCT)
  197. ft_node_update_before(cxt, p, -nextra);
  198. }
  199. *pp -= nextra;
  200. cxt->rgn[rgn].start -= nextra;
  201. cxt->rgn[rgn].size += nextra;
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. static int ft_make_space(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
  207. int nextra)
  208. {
  209. unsigned long size, ssize, tot;
  210. char *str, *next;
  211. enum ft_rgn_id r;
  212. if (!cxt->isordered) {
  213. unsigned long rgn_off = *pp - cxt->rgn[rgn].start;
  214. if (!ft_reorder(cxt, nextra))
  215. return 0;
  216. *pp = cxt->rgn[rgn].start + rgn_off;
  217. }
  218. if (ft_shuffle(cxt, pp, rgn, nextra))
  219. return 1;
  220. /* See if there is space after the strings section */
  221. ssize = cxt->rgn[FT_STRINGS].size;
  222. if (cxt->rgn[FT_STRINGS].start + ssize
  223. < (char *)cxt->bph + cxt->max_size) {
  224. /* move strings up as far as possible */
  225. str = (char *)cxt->bph + cxt->max_size - ssize;
  226. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  227. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  228. cxt->rgn[FT_STRINGS].start = str;
  229. /* enough space now? */
  230. if (rgn >= FT_STRUCT && ft_shuffle(cxt, pp, rgn, nextra))
  231. return 1;
  232. }
  233. /* how much total free space is there following this region? */
  234. tot = 0;
  235. for (r = rgn; r < FT_STRINGS; ++r) {
  236. char *r_end = cxt->rgn[r].start + cxt->rgn[r].size;
  237. tot += next_start(cxt, rgn) - r_end;
  238. }
  239. /* cast is to shut gcc up; we know nextra >= 0 */
  240. if (tot < (unsigned int)nextra) {
  241. /* have to reallocate */
  242. char *newp, *new_start;
  243. int shift;
  244. if (!cxt->realloc)
  245. return 0;
  246. size = _ALIGN(cxt->max_size + (nextra - tot) + EXPAND_INCR, 8);
  247. newp = cxt->realloc(cxt->bph, size);
  248. if (!newp)
  249. return 0;
  250. cxt->max_size = size;
  251. shift = newp - (char *)cxt->bph;
  252. if (shift) { /* realloc can return same addr */
  253. cxt->bph = (struct boot_param_header *)newp;
  254. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  255. shift);
  256. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
  257. new_start = cxt->rgn[r].start + shift;
  258. cxt->rgn[r].start = new_start;
  259. }
  260. *pp += shift;
  261. cxt->str_anchor += shift;
  262. }
  263. /* move strings up to the end */
  264. str = newp + size - ssize;
  265. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  266. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  267. cxt->rgn[FT_STRINGS].start = str;
  268. if (ft_shuffle(cxt, pp, rgn, nextra))
  269. return 1;
  270. }
  271. /* must be FT_RSVMAP and we need to move FT_STRUCT up */
  272. if (rgn == FT_RSVMAP) {
  273. next = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  274. + nextra;
  275. ssize = cxt->rgn[FT_STRUCT].size;
  276. if (next + ssize >= cxt->rgn[FT_STRINGS].start)
  277. return 0; /* "can't happen" */
  278. memmove(next, cxt->rgn[FT_STRUCT].start, ssize);
  279. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start, nextra);
  280. cxt->rgn[FT_STRUCT].start = next;
  281. if (ft_shuffle(cxt, pp, rgn, nextra))
  282. return 1;
  283. }
  284. return 0; /* "can't happen" */
  285. }
  286. static void ft_put_word(struct ft_cxt *cxt, u32 v)
  287. {
  288. *(u32 *) cxt->p = cpu_to_be32(v);
  289. cxt->p += 4;
  290. }
  291. static void ft_put_bin(struct ft_cxt *cxt, const void *data, unsigned int sz)
  292. {
  293. unsigned long sza = _ALIGN(sz, 4);
  294. /* zero out the alignment gap if necessary */
  295. if (sz < sza)
  296. *(u32 *) (cxt->p + sza - 4) = 0;
  297. /* copy in the data */
  298. memcpy(cxt->p, data, sz);
  299. cxt->p += sza;
  300. }
  301. int ft_begin_node(struct ft_cxt *cxt, const char *name)
  302. {
  303. unsigned long nlen = strlen(name) + 1;
  304. unsigned long len = 8 + _ALIGN(nlen, 4);
  305. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  306. return -1;
  307. ft_put_word(cxt, OF_DT_BEGIN_NODE);
  308. ft_put_bin(cxt, name, strlen(name) + 1);
  309. return 0;
  310. }
  311. void ft_end_node(struct ft_cxt *cxt)
  312. {
  313. ft_put_word(cxt, OF_DT_END_NODE);
  314. }
  315. void ft_nop(struct ft_cxt *cxt)
  316. {
  317. if (ft_make_space(cxt, &cxt->p, FT_STRUCT, 4))
  318. ft_put_word(cxt, OF_DT_NOP);
  319. }
  320. #define NO_STRING 0x7fffffff
  321. static int lookup_string(struct ft_cxt *cxt, const char *name)
  322. {
  323. char *p, *end;
  324. p = cxt->rgn[FT_STRINGS].start;
  325. end = p + cxt->rgn[FT_STRINGS].size;
  326. while (p < end) {
  327. if (strcmp(p, (char *)name) == 0)
  328. return p - cxt->str_anchor;
  329. p += strlen(p) + 1;
  330. }
  331. return NO_STRING;
  332. }
  333. /* lookup string and insert if not found */
  334. static int map_string(struct ft_cxt *cxt, const char *name)
  335. {
  336. int off;
  337. char *p;
  338. off = lookup_string(cxt, name);
  339. if (off != NO_STRING)
  340. return off;
  341. p = cxt->rgn[FT_STRINGS].start;
  342. if (!ft_make_space(cxt, &p, FT_STRINGS, strlen(name) + 1))
  343. return NO_STRING;
  344. strcpy(p, name);
  345. return p - cxt->str_anchor;
  346. }
  347. int ft_prop(struct ft_cxt *cxt, const char *name, const void *data,
  348. unsigned int sz)
  349. {
  350. int off, len;
  351. off = map_string(cxt, name);
  352. if (off == NO_STRING)
  353. return -1;
  354. len = 12 + _ALIGN(sz, 4);
  355. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  356. return -1;
  357. ft_put_word(cxt, OF_DT_PROP);
  358. ft_put_word(cxt, sz);
  359. ft_put_word(cxt, off);
  360. ft_put_bin(cxt, data, sz);
  361. return 0;
  362. }
  363. int ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
  364. {
  365. return ft_prop(cxt, name, str, strlen(str) + 1);
  366. }
  367. int ft_prop_int(struct ft_cxt *cxt, const char *name, unsigned int val)
  368. {
  369. u32 v = cpu_to_be32((u32) val);
  370. return ft_prop(cxt, name, &v, 4);
  371. }
  372. /* Calculate the size of the reserved map */
  373. static unsigned long rsvmap_size(struct ft_cxt *cxt)
  374. {
  375. struct ft_reserve *res;
  376. res = (struct ft_reserve *)cxt->rgn[FT_RSVMAP].start;
  377. while (res->start || res->len)
  378. ++res;
  379. return (char *)(res + 1) - cxt->rgn[FT_RSVMAP].start;
  380. }
  381. /* Calculate the size of the struct region by stepping through it */
  382. static unsigned long struct_size(struct ft_cxt *cxt)
  383. {
  384. char *p = cxt->rgn[FT_STRUCT].start;
  385. char *next;
  386. struct ft_atom atom;
  387. /* make check in ft_next happy */
  388. if (cxt->rgn[FT_STRUCT].size == 0)
  389. cxt->rgn[FT_STRUCT].size = 0xfffffffful - (unsigned long)p;
  390. while ((next = ft_next(cxt, p, &atom)) != NULL)
  391. p = next;
  392. return p + 4 - cxt->rgn[FT_STRUCT].start;
  393. }
  394. /* add `adj' on to all string offset values in the struct area */
  395. static void adjust_string_offsets(struct ft_cxt *cxt, int adj)
  396. {
  397. char *p = cxt->rgn[FT_STRUCT].start;
  398. char *next;
  399. struct ft_atom atom;
  400. int off;
  401. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  402. if (atom.tag == OF_DT_PROP) {
  403. off = be32_to_cpu(*(u32 *) (p + 8));
  404. *(u32 *) (p + 8) = cpu_to_be32(off + adj);
  405. }
  406. p = next;
  407. }
  408. }
  409. /* start construction of the flat OF tree from scratch */
  410. void ft_begin(struct ft_cxt *cxt, void *blob, unsigned int max_size,
  411. void *(*realloc_fn) (void *, unsigned long))
  412. {
  413. struct boot_param_header *bph = blob;
  414. char *p;
  415. struct ft_reserve *pres;
  416. /* clear the cxt */
  417. memset(cxt, 0, sizeof(*cxt));
  418. cxt->bph = bph;
  419. cxt->max_size = max_size;
  420. cxt->realloc = realloc_fn;
  421. cxt->isordered = 1;
  422. /* zero everything in the header area */
  423. memset(bph, 0, sizeof(*bph));
  424. bph->magic = cpu_to_be32(OF_DT_HEADER);
  425. bph->version = cpu_to_be32(0x10);
  426. bph->last_comp_version = cpu_to_be32(0x10);
  427. /* start pointers */
  428. cxt->rgn[FT_RSVMAP].start = p = blob + HDR_SIZE;
  429. cxt->rgn[FT_RSVMAP].size = sizeof(struct ft_reserve);
  430. pres = (struct ft_reserve *)p;
  431. cxt->rgn[FT_STRUCT].start = p += sizeof(struct ft_reserve);
  432. cxt->rgn[FT_STRUCT].size = 4;
  433. cxt->rgn[FT_STRINGS].start = blob + max_size;
  434. cxt->rgn[FT_STRINGS].size = 0;
  435. /* init rsvmap and struct */
  436. pres->start = 0;
  437. pres->len = 0;
  438. *(u32 *) p = cpu_to_be32(OF_DT_END);
  439. cxt->str_anchor = blob;
  440. }
  441. /* open up an existing blob to be examined or modified */
  442. int ft_open(struct ft_cxt *cxt, void *blob, unsigned int max_size,
  443. unsigned int max_find_device,
  444. void *(*realloc_fn) (void *, unsigned long))
  445. {
  446. struct boot_param_header *bph = blob;
  447. /* can't cope with version < 16 */
  448. if (be32_to_cpu(bph->version) < 16)
  449. return -1;
  450. /* clear the cxt */
  451. memset(cxt, 0, sizeof(*cxt));
  452. /* alloc node_tbl to track node ptrs returned by ft_find_device */
  453. ++max_find_device;
  454. cxt->node_tbl = realloc_fn(NULL, max_find_device * sizeof(char *));
  455. if (!cxt->node_tbl)
  456. return -1;
  457. memset(cxt->node_tbl, 0, max_find_device * sizeof(char *));
  458. cxt->node_max = max_find_device;
  459. cxt->nodes_used = 1; /* don't use idx 0 b/c looks like NULL */
  460. cxt->bph = bph;
  461. cxt->max_size = max_size;
  462. cxt->realloc = realloc_fn;
  463. cxt->rgn[FT_RSVMAP].start = blob + be32_to_cpu(bph->off_mem_rsvmap);
  464. cxt->rgn[FT_RSVMAP].size = rsvmap_size(cxt);
  465. cxt->rgn[FT_STRUCT].start = blob + be32_to_cpu(bph->off_dt_struct);
  466. cxt->rgn[FT_STRUCT].size = struct_size(cxt);
  467. cxt->rgn[FT_STRINGS].start = blob + be32_to_cpu(bph->off_dt_strings);
  468. cxt->rgn[FT_STRINGS].size = be32_to_cpu(bph->dt_strings_size);
  469. cxt->p = cxt->rgn[FT_STRUCT].start;
  470. cxt->str_anchor = cxt->rgn[FT_STRINGS].start;
  471. return 0;
  472. }
  473. /* add a reserver physical area to the rsvmap */
  474. int ft_add_rsvmap(struct ft_cxt *cxt, u64 physaddr, u64 size)
  475. {
  476. char *p;
  477. struct ft_reserve *pres;
  478. p = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  479. - sizeof(struct ft_reserve);
  480. if (!ft_make_space(cxt, &p, FT_RSVMAP, sizeof(struct ft_reserve)))
  481. return -1;
  482. pres = (struct ft_reserve *)p;
  483. pres->start = cpu_to_be64(physaddr);
  484. pres->len = cpu_to_be64(size);
  485. return 0;
  486. }
  487. void ft_begin_tree(struct ft_cxt *cxt)
  488. {
  489. cxt->p = ft_root_node(cxt);
  490. }
  491. void ft_end_tree(struct ft_cxt *cxt)
  492. {
  493. struct boot_param_header *bph = cxt->bph;
  494. char *p, *oldstr, *str, *endp;
  495. unsigned long ssize;
  496. int adj;
  497. if (!cxt->isordered)
  498. return; /* we haven't touched anything */
  499. /* adjust string offsets */
  500. oldstr = cxt->rgn[FT_STRINGS].start;
  501. adj = cxt->str_anchor - oldstr;
  502. if (adj)
  503. adjust_string_offsets(cxt, adj);
  504. /* make strings end on 8-byte boundary */
  505. ssize = cxt->rgn[FT_STRINGS].size;
  506. endp = (char *)_ALIGN((unsigned long)cxt->rgn[FT_STRUCT].start
  507. + cxt->rgn[FT_STRUCT].size + ssize, 8);
  508. str = endp - ssize;
  509. /* move strings down to end of structs */
  510. memmove(str, oldstr, ssize);
  511. cxt->str_anchor = str;
  512. cxt->rgn[FT_STRINGS].start = str;
  513. /* fill in header fields */
  514. p = (char *)bph;
  515. bph->totalsize = cpu_to_be32(endp - p);
  516. bph->off_mem_rsvmap = cpu_to_be32(cxt->rgn[FT_RSVMAP].start - p);
  517. bph->off_dt_struct = cpu_to_be32(cxt->rgn[FT_STRUCT].start - p);
  518. bph->off_dt_strings = cpu_to_be32(cxt->rgn[FT_STRINGS].start - p);
  519. bph->dt_strings_size = cpu_to_be32(ssize);
  520. }
  521. void *ft_find_device(struct ft_cxt *cxt, const char *srch_path)
  522. {
  523. char *node;
  524. /* require absolute path */
  525. if (srch_path[0] != '/')
  526. return NULL;
  527. node = ft_find_descendent(cxt, ft_root_node(cxt), srch_path);
  528. return ft_get_phandle(cxt, node);
  529. }
  530. void *ft_find_device_rel(struct ft_cxt *cxt, const void *top,
  531. const char *srch_path)
  532. {
  533. char *node;
  534. node = ft_node_ph2node(cxt, top);
  535. if (node == NULL)
  536. return NULL;
  537. node = ft_find_descendent(cxt, node, srch_path);
  538. return ft_get_phandle(cxt, node);
  539. }
  540. void *ft_find_descendent(struct ft_cxt *cxt, void *top, const char *srch_path)
  541. {
  542. struct ft_atom atom;
  543. char *p;
  544. const char *cp, *q;
  545. int cl;
  546. int depth = -1;
  547. int dmatch = 0;
  548. const char *path_comp[FT_MAX_DEPTH];
  549. cp = srch_path;
  550. cl = 0;
  551. p = top;
  552. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  553. switch (atom.tag) {
  554. case OF_DT_BEGIN_NODE:
  555. ++depth;
  556. if (depth != dmatch)
  557. break;
  558. cxt->genealogy[depth] = atom.data;
  559. cxt->genealogy[depth + 1] = NULL;
  560. if (depth && !(strncmp(atom.name, cp, cl) == 0
  561. && (atom.name[cl] == '/'
  562. || atom.name[cl] == '\0'
  563. || atom.name[cl] == '@')))
  564. break;
  565. path_comp[dmatch] = cp;
  566. /* it matches so far, advance to next path component */
  567. cp += cl;
  568. /* skip slashes */
  569. while (*cp == '/')
  570. ++cp;
  571. /* we're done if this is the end of the string */
  572. if (*cp == 0)
  573. return atom.data;
  574. /* look for end of this component */
  575. q = strchr(cp, '/');
  576. if (q)
  577. cl = q - cp;
  578. else
  579. cl = strlen(cp);
  580. ++dmatch;
  581. break;
  582. case OF_DT_END_NODE:
  583. if (depth == 0)
  584. return NULL;
  585. if (dmatch > depth) {
  586. --dmatch;
  587. cl = cp - path_comp[dmatch] - 1;
  588. cp = path_comp[dmatch];
  589. while (cl > 0 && cp[cl - 1] == '/')
  590. --cl;
  591. }
  592. --depth;
  593. break;
  594. }
  595. }
  596. return NULL;
  597. }
  598. void *__ft_get_parent(struct ft_cxt *cxt, void *node)
  599. {
  600. int d;
  601. struct ft_atom atom;
  602. char *p;
  603. for (d = 0; cxt->genealogy[d] != NULL; ++d)
  604. if (cxt->genealogy[d] == node)
  605. return d > 0 ? cxt->genealogy[d - 1] : NULL;
  606. /* have to do it the hard way... */
  607. p = ft_root_node(cxt);
  608. d = 0;
  609. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  610. switch (atom.tag) {
  611. case OF_DT_BEGIN_NODE:
  612. cxt->genealogy[d] = atom.data;
  613. if (node == atom.data) {
  614. /* found it */
  615. cxt->genealogy[d + 1] = NULL;
  616. return d > 0 ? cxt->genealogy[d - 1] : NULL;
  617. }
  618. ++d;
  619. break;
  620. case OF_DT_END_NODE:
  621. --d;
  622. break;
  623. }
  624. }
  625. return NULL;
  626. }
  627. void *ft_get_parent(struct ft_cxt *cxt, const void *phandle)
  628. {
  629. void *node = ft_node_ph2node(cxt, phandle);
  630. if (node == NULL)
  631. return NULL;
  632. node = __ft_get_parent(cxt, node);
  633. return ft_get_phandle(cxt, node);
  634. }
  635. static const void *__ft_get_prop(struct ft_cxt *cxt, void *node,
  636. const char *propname, unsigned int *len)
  637. {
  638. struct ft_atom atom;
  639. int depth = 0;
  640. while ((node = ft_next(cxt, node, &atom)) != NULL) {
  641. switch (atom.tag) {
  642. case OF_DT_BEGIN_NODE:
  643. ++depth;
  644. break;
  645. case OF_DT_PROP:
  646. if (depth != 1 || strcmp(atom.name, propname))
  647. break;
  648. if (len)
  649. *len = atom.size;
  650. return atom.data;
  651. case OF_DT_END_NODE:
  652. if (--depth <= 0)
  653. return NULL;
  654. }
  655. }
  656. return NULL;
  657. }
  658. int ft_get_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  659. void *buf, const unsigned int buflen)
  660. {
  661. const void *data;
  662. unsigned int size;
  663. void *node = ft_node_ph2node(cxt, phandle);
  664. if (!node)
  665. return -1;
  666. data = __ft_get_prop(cxt, node, propname, &size);
  667. if (data) {
  668. unsigned int clipped_size = min(size, buflen);
  669. memcpy(buf, data, clipped_size);
  670. return size;
  671. }
  672. return -1;
  673. }
  674. void *__ft_find_node_by_prop_value(struct ft_cxt *cxt, void *prev,
  675. const char *propname, const char *propval,
  676. unsigned int proplen)
  677. {
  678. struct ft_atom atom;
  679. char *p = ft_root_node(cxt);
  680. char *next;
  681. int past_prev = prev ? 0 : 1;
  682. int depth = -1;
  683. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  684. const void *data;
  685. unsigned int size;
  686. switch (atom.tag) {
  687. case OF_DT_BEGIN_NODE:
  688. depth++;
  689. if (prev == p) {
  690. past_prev = 1;
  691. break;
  692. }
  693. if (!past_prev || depth < 1)
  694. break;
  695. data = __ft_get_prop(cxt, p, propname, &size);
  696. if (!data || size != proplen)
  697. break;
  698. if (memcmp(data, propval, size))
  699. break;
  700. return p;
  701. case OF_DT_END_NODE:
  702. if (depth-- == 0)
  703. return NULL;
  704. break;
  705. }
  706. p = next;
  707. }
  708. return NULL;
  709. }
  710. void *ft_find_node_by_prop_value(struct ft_cxt *cxt, const void *prev,
  711. const char *propname, const char *propval,
  712. int proplen)
  713. {
  714. void *node = NULL;
  715. if (prev) {
  716. node = ft_node_ph2node(cxt, prev);
  717. if (!node)
  718. return NULL;
  719. }
  720. node = __ft_find_node_by_prop_value(cxt, node, propname,
  721. propval, proplen);
  722. return ft_get_phandle(cxt, node);
  723. }
  724. int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  725. const void *buf, const unsigned int buflen)
  726. {
  727. struct ft_atom atom;
  728. void *node;
  729. char *p, *next;
  730. int nextra;
  731. node = ft_node_ph2node(cxt, phandle);
  732. if (node == NULL)
  733. return -1;
  734. next = ft_next(cxt, node, &atom);
  735. if (atom.tag != OF_DT_BEGIN_NODE)
  736. /* phandle didn't point to a node */
  737. return -1;
  738. p = next;
  739. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  740. switch (atom.tag) {
  741. case OF_DT_BEGIN_NODE: /* properties must go before subnodes */
  742. case OF_DT_END_NODE:
  743. /* haven't found the property, insert here */
  744. cxt->p = p;
  745. return ft_prop(cxt, propname, buf, buflen);
  746. case OF_DT_PROP:
  747. if (strcmp(atom.name, propname))
  748. break;
  749. /* found an existing property, overwrite it */
  750. nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4);
  751. cxt->p = atom.data;
  752. if (nextra && !ft_make_space(cxt, &cxt->p, FT_STRUCT,
  753. nextra))
  754. return -1;
  755. *(u32 *) (cxt->p - 8) = cpu_to_be32(buflen);
  756. ft_put_bin(cxt, buf, buflen);
  757. return 0;
  758. }
  759. p = next;
  760. }
  761. return -1;
  762. }
  763. int ft_del_prop(struct ft_cxt *cxt, const void *phandle, const char *propname)
  764. {
  765. struct ft_atom atom;
  766. void *node;
  767. char *p, *next;
  768. int size;
  769. node = ft_node_ph2node(cxt, phandle);
  770. if (node == NULL)
  771. return -1;
  772. p = node;
  773. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  774. switch (atom.tag) {
  775. case OF_DT_BEGIN_NODE:
  776. case OF_DT_END_NODE:
  777. return -1;
  778. case OF_DT_PROP:
  779. if (strcmp(atom.name, propname))
  780. break;
  781. /* found the property, remove it */
  782. size = 12 + -_ALIGN(atom.size, 4);
  783. cxt->p = p;
  784. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, -size))
  785. return -1;
  786. return 0;
  787. }
  788. p = next;
  789. }
  790. return -1;
  791. }
  792. void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *name)
  793. {
  794. struct ft_atom atom;
  795. char *p, *next;
  796. int depth = 0;
  797. if (parent) {
  798. p = ft_node_ph2node(cxt, parent);
  799. if (!p)
  800. return NULL;
  801. } else {
  802. p = ft_root_node(cxt);
  803. }
  804. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  805. switch (atom.tag) {
  806. case OF_DT_BEGIN_NODE:
  807. ++depth;
  808. if (depth == 1 && strcmp(atom.name, name) == 0)
  809. /* duplicate node name, return error */
  810. return NULL;
  811. break;
  812. case OF_DT_END_NODE:
  813. --depth;
  814. if (depth > 0)
  815. break;
  816. /* end of node, insert here */
  817. cxt->p = p;
  818. ft_begin_node(cxt, name);
  819. ft_end_node(cxt);
  820. return p;
  821. }
  822. p = next;
  823. }
  824. return NULL;
  825. }