flatdevtree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. *pp -= 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) {
  225. unsigned long rgn_off = *pp - cxt->rgn[rgn].start;
  226. if (!ft_reorder(cxt, nextra))
  227. return 0;
  228. *pp = cxt->rgn[rgn].start + rgn_off;
  229. }
  230. if (ft_shuffle(cxt, pp, rgn, nextra))
  231. return 1;
  232. /* See if there is space after the strings section */
  233. ssize = cxt->rgn[FT_STRINGS].size;
  234. if (cxt->rgn[FT_STRINGS].start + ssize
  235. < (char *)cxt->bph + cxt->max_size) {
  236. /* move strings up as far as possible */
  237. str = (char *)cxt->bph + cxt->max_size - ssize;
  238. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  239. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  240. cxt->rgn[FT_STRINGS].start = str;
  241. /* enough space now? */
  242. if (rgn >= FT_STRUCT && ft_shuffle(cxt, pp, rgn, nextra))
  243. return 1;
  244. }
  245. /* how much total free space is there following this region? */
  246. tot = 0;
  247. for (r = rgn; r < FT_STRINGS; ++r) {
  248. char *r_end = cxt->rgn[r].start + cxt->rgn[r].size;
  249. tot += next_start(cxt, rgn) - r_end;
  250. }
  251. /* cast is to shut gcc up; we know nextra >= 0 */
  252. if (tot < (unsigned int)nextra) {
  253. /* have to reallocate */
  254. char *newp, *new_start;
  255. int shift;
  256. if (!cxt->realloc)
  257. return 0;
  258. size = _ALIGN(cxt->max_size + (nextra - tot) + EXPAND_INCR, 8);
  259. newp = cxt->realloc(cxt->bph, size);
  260. if (!newp)
  261. return 0;
  262. cxt->max_size = size;
  263. shift = newp - (char *)cxt->bph;
  264. if (shift) { /* realloc can return same addr */
  265. cxt->bph = (struct boot_param_header *)newp;
  266. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  267. shift);
  268. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
  269. new_start = cxt->rgn[r].start + shift;
  270. cxt->rgn[r].start = new_start;
  271. }
  272. *pp += shift;
  273. cxt->str_anchor += shift;
  274. }
  275. /* move strings up to the end */
  276. str = newp + size - ssize;
  277. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  278. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  279. cxt->rgn[FT_STRINGS].start = str;
  280. if (ft_shuffle(cxt, pp, rgn, nextra))
  281. return 1;
  282. }
  283. /* must be FT_RSVMAP and we need to move FT_STRUCT up */
  284. if (rgn == FT_RSVMAP) {
  285. next = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  286. + nextra;
  287. ssize = cxt->rgn[FT_STRUCT].size;
  288. if (next + ssize >= cxt->rgn[FT_STRINGS].start)
  289. return 0; /* "can't happen" */
  290. memmove(next, cxt->rgn[FT_STRUCT].start, ssize);
  291. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start, nextra);
  292. cxt->rgn[FT_STRUCT].start = next;
  293. if (ft_shuffle(cxt, pp, rgn, nextra))
  294. return 1;
  295. }
  296. return 0; /* "can't happen" */
  297. }
  298. static void ft_put_word(struct ft_cxt *cxt, u32 v)
  299. {
  300. *(u32 *) cxt->p = cpu_to_be32(v);
  301. cxt->p += 4;
  302. }
  303. static void ft_put_bin(struct ft_cxt *cxt, const void *data, unsigned int sz)
  304. {
  305. unsigned long sza = _ALIGN(sz, 4);
  306. /* zero out the alignment gap if necessary */
  307. if (sz < sza)
  308. *(u32 *) (cxt->p + sza - 4) = 0;
  309. /* copy in the data */
  310. memcpy(cxt->p, data, sz);
  311. cxt->p += sza;
  312. }
  313. int ft_begin_node(struct ft_cxt *cxt, const char *name)
  314. {
  315. unsigned long nlen = strlen(name) + 1;
  316. unsigned long len = 8 + _ALIGN(nlen, 4);
  317. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  318. return -1;
  319. ft_put_word(cxt, OF_DT_BEGIN_NODE);
  320. ft_put_bin(cxt, name, strlen(name) + 1);
  321. return 0;
  322. }
  323. void ft_end_node(struct ft_cxt *cxt)
  324. {
  325. ft_put_word(cxt, OF_DT_END_NODE);
  326. }
  327. void ft_nop(struct ft_cxt *cxt)
  328. {
  329. if (ft_make_space(cxt, &cxt->p, FT_STRUCT, 4))
  330. ft_put_word(cxt, OF_DT_NOP);
  331. }
  332. #define NO_STRING 0x7fffffff
  333. static int lookup_string(struct ft_cxt *cxt, const char *name)
  334. {
  335. char *p, *end;
  336. p = cxt->rgn[FT_STRINGS].start;
  337. end = p + cxt->rgn[FT_STRINGS].size;
  338. while (p < end) {
  339. if (strcmp(p, (char *)name) == 0)
  340. return p - cxt->str_anchor;
  341. p += strlen(p) + 1;
  342. }
  343. return NO_STRING;
  344. }
  345. /* lookup string and insert if not found */
  346. static int map_string(struct ft_cxt *cxt, const char *name)
  347. {
  348. int off;
  349. char *p;
  350. off = lookup_string(cxt, name);
  351. if (off != NO_STRING)
  352. return off;
  353. p = cxt->rgn[FT_STRINGS].start;
  354. if (!ft_make_space(cxt, &p, FT_STRINGS, strlen(name) + 1))
  355. return NO_STRING;
  356. strcpy(p, name);
  357. return p - cxt->str_anchor;
  358. }
  359. int ft_prop(struct ft_cxt *cxt, const char *name, const void *data,
  360. unsigned int sz)
  361. {
  362. int off, len;
  363. off = map_string(cxt, name);
  364. if (off == NO_STRING)
  365. return -1;
  366. len = 12 + _ALIGN(sz, 4);
  367. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  368. return -1;
  369. ft_put_word(cxt, OF_DT_PROP);
  370. ft_put_word(cxt, sz);
  371. ft_put_word(cxt, off);
  372. ft_put_bin(cxt, data, sz);
  373. return 0;
  374. }
  375. int ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
  376. {
  377. return ft_prop(cxt, name, str, strlen(str) + 1);
  378. }
  379. int ft_prop_int(struct ft_cxt *cxt, const char *name, unsigned int val)
  380. {
  381. u32 v = cpu_to_be32((u32) val);
  382. return ft_prop(cxt, name, &v, 4);
  383. }
  384. /* Calculate the size of the reserved map */
  385. static unsigned long rsvmap_size(struct ft_cxt *cxt)
  386. {
  387. struct ft_reserve *res;
  388. res = (struct ft_reserve *)cxt->rgn[FT_RSVMAP].start;
  389. while (res->start || res->len)
  390. ++res;
  391. return (char *)(res + 1) - cxt->rgn[FT_RSVMAP].start;
  392. }
  393. /* Calculate the size of the struct region by stepping through it */
  394. static unsigned long struct_size(struct ft_cxt *cxt)
  395. {
  396. char *p = cxt->rgn[FT_STRUCT].start;
  397. char *next;
  398. struct ft_atom atom;
  399. /* make check in ft_next happy */
  400. if (cxt->rgn[FT_STRUCT].size == 0)
  401. cxt->rgn[FT_STRUCT].size = 0xfffffffful - (unsigned long)p;
  402. while ((next = ft_next(cxt, p, &atom)) != NULL)
  403. p = next;
  404. return p + 4 - cxt->rgn[FT_STRUCT].start;
  405. }
  406. /* add `adj' on to all string offset values in the struct area */
  407. static void adjust_string_offsets(struct ft_cxt *cxt, int adj)
  408. {
  409. char *p = cxt->rgn[FT_STRUCT].start;
  410. char *next;
  411. struct ft_atom atom;
  412. int off;
  413. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  414. if (atom.tag == OF_DT_PROP) {
  415. off = be32_to_cpu(*(u32 *) (p + 8));
  416. *(u32 *) (p + 8) = cpu_to_be32(off + adj);
  417. }
  418. p = next;
  419. }
  420. }
  421. /* start construction of the flat OF tree from scratch */
  422. void ft_begin(struct ft_cxt *cxt, void *blob, unsigned int max_size,
  423. void *(*realloc_fn) (void *, unsigned long))
  424. {
  425. struct boot_param_header *bph = blob;
  426. char *p;
  427. struct ft_reserve *pres;
  428. /* clear the cxt */
  429. memset(cxt, 0, sizeof(*cxt));
  430. cxt->bph = bph;
  431. cxt->max_size = max_size;
  432. cxt->realloc = realloc_fn;
  433. cxt->isordered = 1;
  434. /* zero everything in the header area */
  435. memset(bph, 0, sizeof(*bph));
  436. bph->magic = cpu_to_be32(OF_DT_HEADER);
  437. bph->version = cpu_to_be32(0x10);
  438. bph->last_comp_version = cpu_to_be32(0x10);
  439. /* start pointers */
  440. cxt->rgn[FT_RSVMAP].start = p = blob + HDR_SIZE;
  441. cxt->rgn[FT_RSVMAP].size = sizeof(struct ft_reserve);
  442. pres = (struct ft_reserve *)p;
  443. cxt->rgn[FT_STRUCT].start = p += sizeof(struct ft_reserve);
  444. cxt->rgn[FT_STRUCT].size = 4;
  445. cxt->rgn[FT_STRINGS].start = blob + max_size;
  446. cxt->rgn[FT_STRINGS].size = 0;
  447. /* init rsvmap and struct */
  448. pres->start = 0;
  449. pres->len = 0;
  450. *(u32 *) p = cpu_to_be32(OF_DT_END);
  451. cxt->str_anchor = blob;
  452. }
  453. /* open up an existing blob to be examined or modified */
  454. int ft_open(struct ft_cxt *cxt, void *blob, unsigned int max_size,
  455. unsigned int max_find_device,
  456. void *(*realloc_fn) (void *, unsigned long))
  457. {
  458. struct boot_param_header *bph = blob;
  459. /* can't cope with version < 16 */
  460. if (be32_to_cpu(bph->version) < 16)
  461. return -1;
  462. /* clear the cxt */
  463. memset(cxt, 0, sizeof(*cxt));
  464. /* alloc node_tbl to track node ptrs returned by ft_find_device */
  465. ++max_find_device;
  466. cxt->node_tbl = realloc_fn(NULL, max_find_device * sizeof(char *));
  467. if (!cxt->node_tbl)
  468. return -1;
  469. memset(cxt->node_tbl, 0, max_find_device * sizeof(char *));
  470. cxt->node_max = max_find_device;
  471. cxt->nodes_used = 1; /* don't use idx 0 b/c looks like NULL */
  472. cxt->bph = bph;
  473. cxt->max_size = max_size;
  474. cxt->realloc = realloc_fn;
  475. cxt->rgn[FT_RSVMAP].start = blob + be32_to_cpu(bph->off_mem_rsvmap);
  476. cxt->rgn[FT_RSVMAP].size = rsvmap_size(cxt);
  477. cxt->rgn[FT_STRUCT].start = blob + be32_to_cpu(bph->off_dt_struct);
  478. cxt->rgn[FT_STRUCT].size = struct_size(cxt);
  479. cxt->rgn[FT_STRINGS].start = blob + be32_to_cpu(bph->off_dt_strings);
  480. cxt->rgn[FT_STRINGS].size = be32_to_cpu(bph->dt_strings_size);
  481. /* Leave as '0' to force first ft_make_space call to do a ft_reorder
  482. * and move dt to an area allocated by realloc.
  483. cxt->isordered = ft_ordered(cxt);
  484. */
  485. cxt->p = cxt->rgn[FT_STRUCT].start;
  486. cxt->str_anchor = cxt->rgn[FT_STRINGS].start;
  487. return 0;
  488. }
  489. /* add a reserver physical area to the rsvmap */
  490. int ft_add_rsvmap(struct ft_cxt *cxt, u64 physaddr, u64 size)
  491. {
  492. char *p;
  493. struct ft_reserve *pres;
  494. p = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  495. - sizeof(struct ft_reserve);
  496. if (!ft_make_space(cxt, &p, FT_RSVMAP, sizeof(struct ft_reserve)))
  497. return -1;
  498. pres = (struct ft_reserve *)p;
  499. pres->start = cpu_to_be64(physaddr);
  500. pres->len = cpu_to_be64(size);
  501. return 0;
  502. }
  503. void ft_begin_tree(struct ft_cxt *cxt)
  504. {
  505. cxt->p = ft_root_node(cxt);
  506. }
  507. void ft_end_tree(struct ft_cxt *cxt)
  508. {
  509. struct boot_param_header *bph = cxt->bph;
  510. char *p, *oldstr, *str, *endp;
  511. unsigned long ssize;
  512. int adj;
  513. if (!cxt->isordered)
  514. return; /* we haven't touched anything */
  515. /* adjust string offsets */
  516. oldstr = cxt->rgn[FT_STRINGS].start;
  517. adj = cxt->str_anchor - oldstr;
  518. if (adj)
  519. adjust_string_offsets(cxt, adj);
  520. /* make strings end on 8-byte boundary */
  521. ssize = cxt->rgn[FT_STRINGS].size;
  522. endp = (char *)_ALIGN((unsigned long)cxt->rgn[FT_STRUCT].start
  523. + cxt->rgn[FT_STRUCT].size + ssize, 8);
  524. str = endp - ssize;
  525. /* move strings down to end of structs */
  526. memmove(str, oldstr, ssize);
  527. cxt->str_anchor = str;
  528. cxt->rgn[FT_STRINGS].start = str;
  529. /* fill in header fields */
  530. p = (char *)bph;
  531. bph->totalsize = cpu_to_be32(endp - p);
  532. bph->off_mem_rsvmap = cpu_to_be32(cxt->rgn[FT_RSVMAP].start - p);
  533. bph->off_dt_struct = cpu_to_be32(cxt->rgn[FT_STRUCT].start - p);
  534. bph->off_dt_strings = cpu_to_be32(cxt->rgn[FT_STRINGS].start - p);
  535. bph->dt_strings_size = cpu_to_be32(ssize);
  536. }
  537. void *ft_find_device(struct ft_cxt *cxt, const char *srch_path)
  538. {
  539. char *node;
  540. /* require absolute path */
  541. if (srch_path[0] != '/')
  542. return NULL;
  543. node = ft_find_descendent(cxt, ft_root_node(cxt), srch_path);
  544. return ft_get_phandle(cxt, node);
  545. }
  546. void *ft_find_device_rel(struct ft_cxt *cxt, const void *top,
  547. const char *srch_path)
  548. {
  549. char *node;
  550. node = ft_node_ph2node(cxt, top);
  551. if (node == NULL)
  552. return NULL;
  553. node = ft_find_descendent(cxt, node, srch_path);
  554. return ft_get_phandle(cxt, node);
  555. }
  556. void *ft_find_descendent(struct ft_cxt *cxt, void *top, const char *srch_path)
  557. {
  558. struct ft_atom atom;
  559. char *p;
  560. const char *cp, *q;
  561. int cl;
  562. int depth = -1;
  563. int dmatch = 0;
  564. const char *path_comp[FT_MAX_DEPTH];
  565. cp = srch_path;
  566. cl = 0;
  567. p = top;
  568. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  569. switch (atom.tag) {
  570. case OF_DT_BEGIN_NODE:
  571. ++depth;
  572. if (depth != dmatch)
  573. break;
  574. cxt->genealogy[depth] = atom.data;
  575. cxt->genealogy[depth + 1] = NULL;
  576. if (depth && !(strncmp(atom.name, cp, cl) == 0
  577. && (atom.name[cl] == '/'
  578. || atom.name[cl] == '\0'
  579. || atom.name[cl] == '@')))
  580. break;
  581. path_comp[dmatch] = cp;
  582. /* it matches so far, advance to next path component */
  583. cp += cl;
  584. /* skip slashes */
  585. while (*cp == '/')
  586. ++cp;
  587. /* we're done if this is the end of the string */
  588. if (*cp == 0)
  589. return atom.data;
  590. /* look for end of this component */
  591. q = strchr(cp, '/');
  592. if (q)
  593. cl = q - cp;
  594. else
  595. cl = strlen(cp);
  596. ++dmatch;
  597. break;
  598. case OF_DT_END_NODE:
  599. if (depth == 0)
  600. return NULL;
  601. if (dmatch > depth) {
  602. --dmatch;
  603. cl = cp - path_comp[dmatch] - 1;
  604. cp = path_comp[dmatch];
  605. while (cl > 0 && cp[cl - 1] == '/')
  606. --cl;
  607. }
  608. --depth;
  609. break;
  610. }
  611. }
  612. return NULL;
  613. }
  614. void *__ft_get_parent(struct ft_cxt *cxt, void *node)
  615. {
  616. int d;
  617. struct ft_atom atom;
  618. char *p;
  619. for (d = 0; cxt->genealogy[d] != NULL; ++d)
  620. if (cxt->genealogy[d] == node)
  621. return d > 0 ? cxt->genealogy[d - 1] : NULL;
  622. /* have to do it the hard way... */
  623. p = ft_root_node(cxt);
  624. d = 0;
  625. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  626. switch (atom.tag) {
  627. case OF_DT_BEGIN_NODE:
  628. cxt->genealogy[d] = atom.data;
  629. if (node == atom.data) {
  630. /* found it */
  631. cxt->genealogy[d + 1] = NULL;
  632. return d > 0 ? cxt->genealogy[d - 1] : NULL;
  633. }
  634. ++d;
  635. break;
  636. case OF_DT_END_NODE:
  637. --d;
  638. break;
  639. }
  640. }
  641. return NULL;
  642. }
  643. void *ft_get_parent(struct ft_cxt *cxt, const void *phandle)
  644. {
  645. void *node = ft_node_ph2node(cxt, phandle);
  646. if (node == NULL)
  647. return NULL;
  648. node = __ft_get_parent(cxt, node);
  649. return ft_get_phandle(cxt, node);
  650. }
  651. static const void *__ft_get_prop(struct ft_cxt *cxt, void *node,
  652. const char *propname, unsigned int *len)
  653. {
  654. struct ft_atom atom;
  655. int depth = 0;
  656. while ((node = ft_next(cxt, node, &atom)) != NULL) {
  657. switch (atom.tag) {
  658. case OF_DT_BEGIN_NODE:
  659. ++depth;
  660. break;
  661. case OF_DT_PROP:
  662. if (depth != 1 || strcmp(atom.name, propname))
  663. break;
  664. if (len)
  665. *len = atom.size;
  666. return atom.data;
  667. case OF_DT_END_NODE:
  668. if (--depth <= 0)
  669. return NULL;
  670. }
  671. }
  672. return NULL;
  673. }
  674. int ft_get_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  675. void *buf, const unsigned int buflen)
  676. {
  677. const void *data;
  678. unsigned int size;
  679. void *node = ft_node_ph2node(cxt, phandle);
  680. if (!node)
  681. return -1;
  682. data = __ft_get_prop(cxt, node, propname, &size);
  683. if (data) {
  684. unsigned int clipped_size = min(size, buflen);
  685. memcpy(buf, data, clipped_size);
  686. return size;
  687. }
  688. return -1;
  689. }
  690. void *__ft_find_node_by_prop_value(struct ft_cxt *cxt, void *prev,
  691. const char *propname, const char *propval,
  692. unsigned int proplen)
  693. {
  694. struct ft_atom atom;
  695. char *p = ft_root_node(cxt);
  696. char *next;
  697. int past_prev = prev ? 0 : 1;
  698. int depth = -1;
  699. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  700. const void *data;
  701. unsigned int size;
  702. switch (atom.tag) {
  703. case OF_DT_BEGIN_NODE:
  704. depth++;
  705. if (prev == p) {
  706. past_prev = 1;
  707. break;
  708. }
  709. if (!past_prev || depth < 1)
  710. break;
  711. data = __ft_get_prop(cxt, p, propname, &size);
  712. if (!data || size != proplen)
  713. break;
  714. if (memcmp(data, propval, size))
  715. break;
  716. return p;
  717. case OF_DT_END_NODE:
  718. if (depth-- == 0)
  719. return NULL;
  720. break;
  721. }
  722. p = next;
  723. }
  724. return NULL;
  725. }
  726. void *ft_find_node_by_prop_value(struct ft_cxt *cxt, const void *prev,
  727. const char *propname, const char *propval,
  728. int proplen)
  729. {
  730. void *node = NULL;
  731. if (prev) {
  732. node = ft_node_ph2node(cxt, prev);
  733. if (!node)
  734. return NULL;
  735. }
  736. node = __ft_find_node_by_prop_value(cxt, node, propname,
  737. propval, proplen);
  738. return ft_get_phandle(cxt, node);
  739. }
  740. int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  741. const void *buf, const unsigned int buflen)
  742. {
  743. struct ft_atom atom;
  744. void *node;
  745. char *p, *next;
  746. int nextra, depth;
  747. node = ft_node_ph2node(cxt, phandle);
  748. if (node == NULL)
  749. return -1;
  750. depth = 0;
  751. p = node;
  752. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  753. switch (atom.tag) {
  754. case OF_DT_BEGIN_NODE:
  755. ++depth;
  756. break;
  757. case OF_DT_END_NODE:
  758. if (--depth > 0)
  759. break;
  760. /* haven't found the property, insert here */
  761. cxt->p = p;
  762. return ft_prop(cxt, propname, buf, buflen);
  763. case OF_DT_PROP:
  764. if ((depth != 1) || strcmp(atom.name, propname))
  765. break;
  766. /* found an existing property, overwrite it */
  767. nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4);
  768. cxt->p = atom.data;
  769. if (nextra && !ft_make_space(cxt, &cxt->p, FT_STRUCT,
  770. nextra))
  771. return -1;
  772. *(u32 *) (cxt->p - 8) = cpu_to_be32(buflen);
  773. ft_put_bin(cxt, buf, buflen);
  774. return 0;
  775. }
  776. p = next;
  777. }
  778. return -1;
  779. }
  780. int ft_del_prop(struct ft_cxt *cxt, const void *phandle, const char *propname)
  781. {
  782. struct ft_atom atom;
  783. void *node;
  784. char *p, *next;
  785. int size;
  786. node = ft_node_ph2node(cxt, phandle);
  787. if (node == NULL)
  788. return -1;
  789. p = node;
  790. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  791. switch (atom.tag) {
  792. case OF_DT_BEGIN_NODE:
  793. case OF_DT_END_NODE:
  794. return -1;
  795. case OF_DT_PROP:
  796. if (strcmp(atom.name, propname))
  797. break;
  798. /* found the property, remove it */
  799. size = 12 + -_ALIGN(atom.size, 4);
  800. cxt->p = p;
  801. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, -size))
  802. return -1;
  803. return 0;
  804. }
  805. p = next;
  806. }
  807. return -1;
  808. }
  809. void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *name)
  810. {
  811. struct ft_atom atom;
  812. char *p, *next;
  813. int depth = 0;
  814. if (parent) {
  815. p = ft_node_ph2node(cxt, parent);
  816. if (!p)
  817. return NULL;
  818. } else {
  819. p = ft_root_node(cxt);
  820. }
  821. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  822. switch (atom.tag) {
  823. case OF_DT_BEGIN_NODE:
  824. ++depth;
  825. if (depth == 1 && strcmp(atom.name, name) == 0)
  826. /* duplicate node name, return error */
  827. return NULL;
  828. break;
  829. case OF_DT_END_NODE:
  830. --depth;
  831. if (depth > 0)
  832. break;
  833. /* end of node, insert here */
  834. cxt->p = p;
  835. ft_begin_node(cxt, name);
  836. ft_end_node(cxt);
  837. return p;
  838. }
  839. p = next;
  840. }
  841. return NULL;
  842. }