flatdevtree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. for (i = 1; i < cxt->nodes_used; i++) /* already there? */
  39. if (cxt->node_tbl[i] == node)
  40. return (void *)i;
  41. if (cxt->nodes_used < cxt->node_max) {
  42. cxt->node_tbl[cxt->nodes_used] = node;
  43. return (void *)cxt->nodes_used++;
  44. }
  45. return NULL;
  46. }
  47. static char *ft_node_ph2node(struct ft_cxt *cxt, const void *phandle)
  48. {
  49. unsigned int i = (unsigned int)phandle;
  50. if (i < cxt->nodes_used)
  51. return cxt->node_tbl[i];
  52. return NULL;
  53. }
  54. static void ft_node_update_before(struct ft_cxt *cxt, char *addr, int shift)
  55. {
  56. unsigned int i;
  57. if (shift == 0)
  58. return;
  59. for (i = 1; i < cxt->nodes_used; i++)
  60. if (cxt->node_tbl[i] < addr)
  61. cxt->node_tbl[i] += shift;
  62. }
  63. static void ft_node_update_after(struct ft_cxt *cxt, char *addr, int shift)
  64. {
  65. unsigned int i;
  66. if (shift == 0)
  67. return;
  68. for (i = 1; i < cxt->nodes_used; i++)
  69. if (cxt->node_tbl[i] >= addr)
  70. cxt->node_tbl[i] += shift;
  71. }
  72. /* Struct used to return info from ft_next() */
  73. struct ft_atom {
  74. u32 tag;
  75. const char *name;
  76. void *data;
  77. u32 size;
  78. };
  79. /* Set ptrs to current one's info; return addr of next one */
  80. static char *ft_next(struct ft_cxt *cxt, char *p, struct ft_atom *ret)
  81. {
  82. u32 sz;
  83. if (p >= cxt->rgn[FT_STRUCT].start + cxt->rgn[FT_STRUCT].size)
  84. return NULL;
  85. ret->tag = be32_to_cpu(*(u32 *) p);
  86. p += 4;
  87. switch (ret->tag) { /* Tag */
  88. case OF_DT_BEGIN_NODE:
  89. ret->name = p;
  90. ret->data = (void *)(p - 4); /* start of node */
  91. p += _ALIGN(strlen(p) + 1, 4);
  92. break;
  93. case OF_DT_PROP:
  94. ret->size = sz = be32_to_cpu(*(u32 *) p);
  95. ret->name = cxt->str_anchor + be32_to_cpu(*(u32 *) (p + 4));
  96. ret->data = (void *)(p + 8);
  97. p += 8 + _ALIGN(sz, 4);
  98. break;
  99. case OF_DT_END_NODE:
  100. case OF_DT_NOP:
  101. break;
  102. case OF_DT_END:
  103. default:
  104. p = NULL;
  105. break;
  106. }
  107. return p;
  108. }
  109. #define HDR_SIZE _ALIGN(sizeof(struct boot_param_header), 8)
  110. #define EXPAND_INCR 1024 /* alloc this much extra when expanding */
  111. /* See if the regions are in the standard order and non-overlapping */
  112. static int ft_ordered(struct ft_cxt *cxt)
  113. {
  114. char *p = (char *)cxt->bph + HDR_SIZE;
  115. enum ft_rgn_id r;
  116. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
  117. if (p > cxt->rgn[r].start)
  118. return 0;
  119. p = cxt->rgn[r].start + cxt->rgn[r].size;
  120. }
  121. return p <= (char *)cxt->bph + cxt->max_size;
  122. }
  123. /* Copy the tree to a newly-allocated region and put things in order */
  124. static int ft_reorder(struct ft_cxt *cxt, int nextra)
  125. {
  126. unsigned long tot;
  127. enum ft_rgn_id r;
  128. char *p, *pend;
  129. int stroff;
  130. tot = HDR_SIZE + EXPAND_INCR;
  131. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r)
  132. tot += cxt->rgn[r].size;
  133. if (nextra > 0)
  134. tot += nextra;
  135. tot = _ALIGN(tot, 8);
  136. if (!cxt->realloc)
  137. return 0;
  138. p = cxt->realloc(NULL, tot);
  139. if (!p)
  140. return 0;
  141. memcpy(p, cxt->bph, sizeof(struct boot_param_header));
  142. /* offsets get fixed up later */
  143. cxt->bph = (struct boot_param_header *)p;
  144. cxt->max_size = tot;
  145. pend = p + tot;
  146. p += HDR_SIZE;
  147. memcpy(p, cxt->rgn[FT_RSVMAP].start, cxt->rgn[FT_RSVMAP].size);
  148. cxt->rgn[FT_RSVMAP].start = p;
  149. p += cxt->rgn[FT_RSVMAP].size;
  150. memcpy(p, cxt->rgn[FT_STRUCT].start, cxt->rgn[FT_STRUCT].size);
  151. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  152. p - cxt->rgn[FT_STRUCT].start);
  153. cxt->p += p - cxt->rgn[FT_STRUCT].start;
  154. cxt->rgn[FT_STRUCT].start = p;
  155. p = pend - cxt->rgn[FT_STRINGS].size;
  156. memcpy(p, cxt->rgn[FT_STRINGS].start, cxt->rgn[FT_STRINGS].size);
  157. stroff = cxt->str_anchor - cxt->rgn[FT_STRINGS].start;
  158. cxt->rgn[FT_STRINGS].start = p;
  159. cxt->str_anchor = p + stroff;
  160. cxt->isordered = 1;
  161. return 1;
  162. }
  163. static inline char *prev_end(struct ft_cxt *cxt, enum ft_rgn_id r)
  164. {
  165. if (r > FT_RSVMAP)
  166. return cxt->rgn[r - 1].start + cxt->rgn[r - 1].size;
  167. return (char *)cxt->bph + HDR_SIZE;
  168. }
  169. static inline char *next_start(struct ft_cxt *cxt, enum ft_rgn_id r)
  170. {
  171. if (r < FT_STRINGS)
  172. return cxt->rgn[r + 1].start;
  173. return (char *)cxt->bph + cxt->max_size;
  174. }
  175. /*
  176. * See if we can expand region rgn by nextra bytes by using up
  177. * free space after or before the region.
  178. */
  179. static int ft_shuffle(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
  180. int nextra)
  181. {
  182. char *p = *pp;
  183. char *rgn_start, *rgn_end;
  184. rgn_start = cxt->rgn[rgn].start;
  185. rgn_end = rgn_start + cxt->rgn[rgn].size;
  186. if (nextra <= 0 || rgn_end + nextra <= next_start(cxt, rgn)) {
  187. /* move following stuff */
  188. if (p < rgn_end) {
  189. if (nextra < 0)
  190. memmove(p, p - nextra, rgn_end - p + nextra);
  191. else
  192. memmove(p + nextra, p, rgn_end - p);
  193. if (rgn == FT_STRUCT)
  194. ft_node_update_after(cxt, p, nextra);
  195. }
  196. cxt->rgn[rgn].size += nextra;
  197. if (rgn == FT_STRINGS)
  198. /* assumes strings only added at beginning */
  199. cxt->str_anchor += nextra;
  200. return 1;
  201. }
  202. if (prev_end(cxt, rgn) <= rgn_start - nextra) {
  203. /* move preceding stuff */
  204. if (p > rgn_start) {
  205. memmove(rgn_start - nextra, rgn_start, p - rgn_start);
  206. if (rgn == FT_STRUCT)
  207. ft_node_update_before(cxt, p, -nextra);
  208. }
  209. *p -= nextra;
  210. cxt->rgn[rgn].start -= nextra;
  211. cxt->rgn[rgn].size += nextra;
  212. return 1;
  213. }
  214. return 0;
  215. }
  216. static int ft_make_space(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
  217. int nextra)
  218. {
  219. unsigned long size, ssize, tot;
  220. char *str, *next;
  221. enum ft_rgn_id r;
  222. if (!cxt->isordered && !ft_reorder(cxt, nextra))
  223. return 0;
  224. if (ft_shuffle(cxt, pp, rgn, nextra))
  225. return 1;
  226. /* See if there is space after the strings section */
  227. ssize = cxt->rgn[FT_STRINGS].size;
  228. if (cxt->rgn[FT_STRINGS].start + ssize
  229. < (char *)cxt->bph + cxt->max_size) {
  230. /* move strings up as far as possible */
  231. str = (char *)cxt->bph + cxt->max_size - ssize;
  232. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  233. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  234. cxt->rgn[FT_STRINGS].start = str;
  235. /* enough space now? */
  236. if (rgn >= FT_STRUCT && ft_shuffle(cxt, pp, rgn, nextra))
  237. return 1;
  238. }
  239. /* how much total free space is there following this region? */
  240. tot = 0;
  241. for (r = rgn; r < FT_STRINGS; ++r) {
  242. char *r_end = cxt->rgn[r].start + cxt->rgn[r].size;
  243. tot += next_start(cxt, rgn) - r_end;
  244. }
  245. /* cast is to shut gcc up; we know nextra >= 0 */
  246. if (tot < (unsigned int)nextra) {
  247. /* have to reallocate */
  248. char *newp, *new_start;
  249. int shift;
  250. if (!cxt->realloc)
  251. return 0;
  252. size = _ALIGN(cxt->max_size + (nextra - tot) + EXPAND_INCR, 8);
  253. newp = cxt->realloc(cxt->bph, size);
  254. if (!newp)
  255. return 0;
  256. cxt->max_size = size;
  257. shift = newp - (char *)cxt->bph;
  258. if (shift) { /* realloc can return same addr */
  259. cxt->bph = (struct boot_param_header *)newp;
  260. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  261. shift);
  262. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
  263. new_start = cxt->rgn[r].start + shift;
  264. cxt->rgn[r].start = new_start;
  265. }
  266. *pp += shift;
  267. cxt->str_anchor += shift;
  268. }
  269. /* move strings up to the end */
  270. str = newp + size - ssize;
  271. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  272. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  273. cxt->rgn[FT_STRINGS].start = str;
  274. if (ft_shuffle(cxt, pp, rgn, nextra))
  275. return 1;
  276. }
  277. /* must be FT_RSVMAP and we need to move FT_STRUCT up */
  278. if (rgn == FT_RSVMAP) {
  279. next = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  280. + nextra;
  281. ssize = cxt->rgn[FT_STRUCT].size;
  282. if (next + ssize >= cxt->rgn[FT_STRINGS].start)
  283. return 0; /* "can't happen" */
  284. memmove(next, cxt->rgn[FT_STRUCT].start, ssize);
  285. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start, nextra);
  286. cxt->rgn[FT_STRUCT].start = next;
  287. if (ft_shuffle(cxt, pp, rgn, nextra))
  288. return 1;
  289. }
  290. return 0; /* "can't happen" */
  291. }
  292. static void ft_put_word(struct ft_cxt *cxt, u32 v)
  293. {
  294. *(u32 *) cxt->p = cpu_to_be32(v);
  295. cxt->p += 4;
  296. }
  297. static void ft_put_bin(struct ft_cxt *cxt, const void *data, unsigned int sz)
  298. {
  299. unsigned long sza = _ALIGN(sz, 4);
  300. /* zero out the alignment gap if necessary */
  301. if (sz < sza)
  302. *(u32 *) (cxt->p + sza - 4) = 0;
  303. /* copy in the data */
  304. memcpy(cxt->p, data, sz);
  305. cxt->p += sza;
  306. }
  307. int ft_begin_node(struct ft_cxt *cxt, const char *name)
  308. {
  309. unsigned long nlen = strlen(name) + 1;
  310. unsigned long len = 8 + _ALIGN(nlen, 4);
  311. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  312. return -1;
  313. ft_put_word(cxt, OF_DT_BEGIN_NODE);
  314. ft_put_bin(cxt, name, strlen(name) + 1);
  315. return 0;
  316. }
  317. void ft_end_node(struct ft_cxt *cxt)
  318. {
  319. ft_put_word(cxt, OF_DT_END_NODE);
  320. }
  321. void ft_nop(struct ft_cxt *cxt)
  322. {
  323. if (ft_make_space(cxt, &cxt->p, FT_STRUCT, 4))
  324. ft_put_word(cxt, OF_DT_NOP);
  325. }
  326. #define NO_STRING 0x7fffffff
  327. static int lookup_string(struct ft_cxt *cxt, const char *name)
  328. {
  329. char *p, *end;
  330. p = cxt->rgn[FT_STRINGS].start;
  331. end = p + cxt->rgn[FT_STRINGS].size;
  332. while (p < end) {
  333. if (strcmp(p, (char *)name) == 0)
  334. return p - cxt->str_anchor;
  335. p += strlen(p) + 1;
  336. }
  337. return NO_STRING;
  338. }
  339. /* lookup string and insert if not found */
  340. static int map_string(struct ft_cxt *cxt, const char *name)
  341. {
  342. int off;
  343. char *p;
  344. off = lookup_string(cxt, name);
  345. if (off != NO_STRING)
  346. return off;
  347. p = cxt->rgn[FT_STRINGS].start;
  348. if (!ft_make_space(cxt, &p, FT_STRINGS, strlen(name) + 1))
  349. return NO_STRING;
  350. strcpy(p, name);
  351. return p - cxt->str_anchor;
  352. }
  353. int ft_prop(struct ft_cxt *cxt, const char *name, const void *data,
  354. unsigned int sz)
  355. {
  356. int off, len;
  357. off = lookup_string(cxt, name);
  358. if (off == NO_STRING)
  359. return -1;
  360. len = 12 + _ALIGN(sz, 4);
  361. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  362. return -1;
  363. ft_put_word(cxt, OF_DT_PROP);
  364. ft_put_word(cxt, sz);
  365. ft_put_word(cxt, off);
  366. ft_put_bin(cxt, data, sz);
  367. return 0;
  368. }
  369. int ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
  370. {
  371. return ft_prop(cxt, name, str, strlen(str) + 1);
  372. }
  373. int ft_prop_int(struct ft_cxt *cxt, const char *name, unsigned int val)
  374. {
  375. u32 v = cpu_to_be32((u32) val);
  376. return ft_prop(cxt, name, &v, 4);
  377. }
  378. /* Calculate the size of the reserved map */
  379. static unsigned long rsvmap_size(struct ft_cxt *cxt)
  380. {
  381. struct ft_reserve *res;
  382. res = (struct ft_reserve *)cxt->rgn[FT_RSVMAP].start;
  383. while (res->start || res->len)
  384. ++res;
  385. return (char *)(res + 1) - cxt->rgn[FT_RSVMAP].start;
  386. }
  387. /* Calculate the size of the struct region by stepping through it */
  388. static unsigned long struct_size(struct ft_cxt *cxt)
  389. {
  390. char *p = cxt->rgn[FT_STRUCT].start;
  391. char *next;
  392. struct ft_atom atom;
  393. /* make check in ft_next happy */
  394. if (cxt->rgn[FT_STRUCT].size == 0)
  395. cxt->rgn[FT_STRUCT].size = 0xfffffffful - (unsigned long)p;
  396. while ((next = ft_next(cxt, p, &atom)) != NULL)
  397. p = next;
  398. return p + 4 - cxt->rgn[FT_STRUCT].start;
  399. }
  400. /* add `adj' on to all string offset values in the struct area */
  401. static void adjust_string_offsets(struct ft_cxt *cxt, int adj)
  402. {
  403. char *p = cxt->rgn[FT_STRUCT].start;
  404. char *next;
  405. struct ft_atom atom;
  406. int off;
  407. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  408. if (atom.tag == OF_DT_PROP) {
  409. off = be32_to_cpu(*(u32 *) (p + 8));
  410. *(u32 *) (p + 8) = cpu_to_be32(off + adj);
  411. }
  412. p = next;
  413. }
  414. }
  415. /* start construction of the flat OF tree from scratch */
  416. void ft_begin(struct ft_cxt *cxt, void *blob, unsigned int max_size,
  417. void *(*realloc_fn) (void *, unsigned long))
  418. {
  419. struct boot_param_header *bph = blob;
  420. char *p;
  421. struct ft_reserve *pres;
  422. /* clear the cxt */
  423. memset(cxt, 0, sizeof(*cxt));
  424. cxt->bph = bph;
  425. cxt->max_size = max_size;
  426. cxt->realloc = realloc_fn;
  427. cxt->isordered = 1;
  428. /* zero everything in the header area */
  429. memset(bph, 0, sizeof(*bph));
  430. bph->magic = cpu_to_be32(OF_DT_HEADER);
  431. bph->version = cpu_to_be32(0x10);
  432. bph->last_comp_version = cpu_to_be32(0x10);
  433. /* start pointers */
  434. cxt->rgn[FT_RSVMAP].start = p = blob + HDR_SIZE;
  435. cxt->rgn[FT_RSVMAP].size = sizeof(struct ft_reserve);
  436. pres = (struct ft_reserve *)p;
  437. cxt->rgn[FT_STRUCT].start = p += sizeof(struct ft_reserve);
  438. cxt->rgn[FT_STRUCT].size = 4;
  439. cxt->rgn[FT_STRINGS].start = blob + max_size;
  440. cxt->rgn[FT_STRINGS].size = 0;
  441. /* init rsvmap and struct */
  442. pres->start = 0;
  443. pres->len = 0;
  444. *(u32 *) p = cpu_to_be32(OF_DT_END);
  445. cxt->str_anchor = blob;
  446. }
  447. /* open up an existing blob to be examined or modified */
  448. int ft_open(struct ft_cxt *cxt, void *blob, unsigned int max_size,
  449. unsigned int max_find_device,
  450. void *(*realloc_fn) (void *, unsigned long))
  451. {
  452. struct boot_param_header *bph = blob;
  453. /* can't cope with version < 16 */
  454. if (be32_to_cpu(bph->version) < 16)
  455. return -1;
  456. /* clear the cxt */
  457. memset(cxt, 0, sizeof(*cxt));
  458. /* alloc node_tbl to track node ptrs returned by ft_find_device */
  459. ++max_find_device;
  460. cxt->node_tbl = realloc_fn(NULL, max_find_device * sizeof(char *));
  461. if (!cxt->node_tbl)
  462. return -1;
  463. memset(cxt->node_tbl, 0, max_find_device * sizeof(char *));
  464. cxt->node_max = max_find_device;
  465. cxt->nodes_used = 1; /* don't use idx 0 b/c looks like NULL */
  466. cxt->bph = bph;
  467. cxt->max_size = max_size;
  468. cxt->realloc = realloc_fn;
  469. cxt->rgn[FT_RSVMAP].start = blob + be32_to_cpu(bph->off_mem_rsvmap);
  470. cxt->rgn[FT_RSVMAP].size = rsvmap_size(cxt);
  471. cxt->rgn[FT_STRUCT].start = blob + be32_to_cpu(bph->off_dt_struct);
  472. cxt->rgn[FT_STRUCT].size = struct_size(cxt);
  473. cxt->rgn[FT_STRINGS].start = blob + be32_to_cpu(bph->off_dt_strings);
  474. cxt->rgn[FT_STRINGS].size = be32_to_cpu(bph->dt_strings_size);
  475. /* Leave as '0' to force first ft_make_space call to do a ft_reorder
  476. * and move dt to an area allocated by realloc.
  477. cxt->isordered = ft_ordered(cxt);
  478. */
  479. cxt->p = cxt->rgn[FT_STRUCT].start;
  480. cxt->str_anchor = cxt->rgn[FT_STRINGS].start;
  481. return 0;
  482. }
  483. /* add a reserver physical area to the rsvmap */
  484. int ft_add_rsvmap(struct ft_cxt *cxt, u64 physaddr, u64 size)
  485. {
  486. char *p;
  487. struct ft_reserve *pres;
  488. p = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  489. - sizeof(struct ft_reserve);
  490. if (!ft_make_space(cxt, &p, FT_RSVMAP, sizeof(struct ft_reserve)))
  491. return -1;
  492. pres = (struct ft_reserve *)p;
  493. pres->start = cpu_to_be64(physaddr);
  494. pres->len = cpu_to_be64(size);
  495. return 0;
  496. }
  497. void ft_begin_tree(struct ft_cxt *cxt)
  498. {
  499. cxt->p = ft_root_node(cxt);
  500. }
  501. void ft_end_tree(struct ft_cxt *cxt)
  502. {
  503. struct boot_param_header *bph = cxt->bph;
  504. char *p, *oldstr, *str, *endp;
  505. unsigned long ssize;
  506. int adj;
  507. if (!cxt->isordered)
  508. return; /* we haven't touched anything */
  509. /* adjust string offsets */
  510. oldstr = cxt->rgn[FT_STRINGS].start;
  511. adj = cxt->str_anchor - oldstr;
  512. if (adj)
  513. adjust_string_offsets(cxt, adj);
  514. /* make strings end on 8-byte boundary */
  515. ssize = cxt->rgn[FT_STRINGS].size;
  516. endp = (char *)_ALIGN((unsigned long)cxt->rgn[FT_STRUCT].start
  517. + cxt->rgn[FT_STRUCT].size + ssize, 8);
  518. str = endp - ssize;
  519. /* move strings down to end of structs */
  520. memmove(str, oldstr, ssize);
  521. cxt->str_anchor = str;
  522. cxt->rgn[FT_STRINGS].start = str;
  523. /* fill in header fields */
  524. p = (char *)bph;
  525. bph->totalsize = cpu_to_be32(endp - p);
  526. bph->off_mem_rsvmap = cpu_to_be32(cxt->rgn[FT_RSVMAP].start - p);
  527. bph->off_dt_struct = cpu_to_be32(cxt->rgn[FT_STRUCT].start - p);
  528. bph->off_dt_strings = cpu_to_be32(cxt->rgn[FT_STRINGS].start - p);
  529. bph->dt_strings_size = cpu_to_be32(ssize);
  530. }
  531. void *ft_find_device(struct ft_cxt *cxt, const char *srch_path)
  532. {
  533. char *node;
  534. /* require absolute path */
  535. if (srch_path[0] != '/')
  536. return NULL;
  537. node = ft_find_descendent(cxt, ft_root_node(cxt), 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, const void *phandle)
  599. {
  600. void *node;
  601. int d;
  602. struct ft_atom atom;
  603. char *p;
  604. node = ft_node_ph2node(cxt, phandle);
  605. if (node == NULL)
  606. return NULL;
  607. for (d = 0; cxt->genealogy[d] != NULL; ++d)
  608. if (cxt->genealogy[d] == node)
  609. return cxt->genealogy[d > 0 ? d - 1 : 0];
  610. /* have to do it the hard way... */
  611. p = ft_root_node(cxt);
  612. d = 0;
  613. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  614. switch (atom.tag) {
  615. case OF_DT_BEGIN_NODE:
  616. cxt->genealogy[d] = atom.data;
  617. if (node == atom.data) {
  618. /* found it */
  619. cxt->genealogy[d + 1] = NULL;
  620. return d > 0 ? cxt->genealogy[d - 1] : node;
  621. }
  622. ++d;
  623. break;
  624. case OF_DT_END_NODE:
  625. --d;
  626. break;
  627. }
  628. }
  629. return NULL;
  630. }
  631. int ft_get_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  632. void *buf, const unsigned int buflen)
  633. {
  634. struct ft_atom atom;
  635. void *node;
  636. char *p;
  637. int depth;
  638. unsigned int size;
  639. node = ft_node_ph2node(cxt, phandle);
  640. if (node == NULL)
  641. return -1;
  642. depth = 0;
  643. p = (char *)node;
  644. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  645. switch (atom.tag) {
  646. case OF_DT_BEGIN_NODE:
  647. ++depth;
  648. break;
  649. case OF_DT_PROP:
  650. if ((depth != 1) || strcmp(atom.name, propname))
  651. break;
  652. size = min(atom.size, buflen);
  653. memcpy(buf, atom.data, size);
  654. return atom.size;
  655. case OF_DT_END_NODE:
  656. if (--depth <= 0)
  657. return -1;
  658. }
  659. }
  660. return -1;
  661. }
  662. int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  663. const void *buf, const unsigned int buflen)
  664. {
  665. struct ft_atom atom;
  666. void *node;
  667. char *p, *next;
  668. int nextra, depth;
  669. node = ft_node_ph2node(cxt, phandle);
  670. if (node == NULL)
  671. return -1;
  672. depth = 0;
  673. p = node;
  674. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  675. switch (atom.tag) {
  676. case OF_DT_BEGIN_NODE:
  677. ++depth;
  678. break;
  679. case OF_DT_END_NODE:
  680. if (--depth > 0)
  681. break;
  682. /* haven't found the property, insert here */
  683. cxt->p = p;
  684. return ft_prop(cxt, propname, buf, buflen);
  685. case OF_DT_PROP:
  686. if ((depth != 1) || strcmp(atom.name, propname))
  687. break;
  688. /* found an existing property, overwrite it */
  689. nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4);
  690. cxt->p = atom.data;
  691. if (nextra && !ft_make_space(cxt, &cxt->p, FT_STRUCT,
  692. nextra))
  693. return -1;
  694. *(u32 *) (cxt->p - 8) = cpu_to_be32(buflen);
  695. ft_put_bin(cxt, buf, buflen);
  696. return 0;
  697. }
  698. p = next;
  699. }
  700. return -1;
  701. }
  702. int ft_del_prop(struct ft_cxt *cxt, const void *phandle, const char *propname)
  703. {
  704. struct ft_atom atom;
  705. void *node;
  706. char *p, *next;
  707. int size;
  708. node = ft_node_ph2node(cxt, phandle);
  709. if (node == NULL)
  710. return -1;
  711. p = node;
  712. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  713. switch (atom.tag) {
  714. case OF_DT_BEGIN_NODE:
  715. case OF_DT_END_NODE:
  716. return -1;
  717. case OF_DT_PROP:
  718. if (strcmp(atom.name, propname))
  719. break;
  720. /* found the property, remove it */
  721. size = 12 + -_ALIGN(atom.size, 4);
  722. cxt->p = p;
  723. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, -size))
  724. return -1;
  725. return 0;
  726. }
  727. p = next;
  728. }
  729. return -1;
  730. }
  731. void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *path)
  732. {
  733. struct ft_atom atom;
  734. char *p, *next;
  735. int depth = 0;
  736. p = ft_root_node(cxt);
  737. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  738. switch (atom.tag) {
  739. case OF_DT_BEGIN_NODE:
  740. ++depth;
  741. if (depth == 1 && strcmp(atom.name, path) == 0)
  742. /* duplicate node path, return error */
  743. return NULL;
  744. break;
  745. case OF_DT_END_NODE:
  746. --depth;
  747. if (depth > 0)
  748. break;
  749. /* end of node, insert here */
  750. cxt->p = p;
  751. ft_begin_node(cxt, path);
  752. ft_end_node(cxt);
  753. return p;
  754. }
  755. p = next;
  756. }
  757. return NULL;
  758. }