flatdevtree.c 21 KB

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