flatdevtree.c 21 KB

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