flatdevtree.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * Copyright Pantelis Antoniou 2006
  17. * Copyright (C) IBM Corporation 2006
  18. *
  19. * Authors: Pantelis Antoniou <pantelis@embeddedalley.com>
  20. * Hollis Blanchard <hollisb@us.ibm.com>
  21. * Mark A. Greer <mgreer@mvista.com>
  22. * Paul Mackerras <paulus@samba.org>
  23. */
  24. #include <string.h>
  25. #include <stddef.h>
  26. #include "flatdevtree.h"
  27. #include "flatdevtree_env.h"
  28. #define _ALIGN(x, al) (((x) + (al) - 1) & ~((al) - 1))
  29. static char *ft_root_node(struct ft_cxt *cxt)
  30. {
  31. return cxt->rgn[FT_STRUCT].start;
  32. }
  33. /* Routines for keeping node ptrs returned by ft_find_device current */
  34. /* First entry not used b/c it would return 0 and be taken as NULL/error */
  35. static void *ft_get_phandle(struct ft_cxt *cxt, char *node)
  36. {
  37. unsigned int i;
  38. if (!node)
  39. return NULL;
  40. for (i = 1; i < cxt->nodes_used; i++) /* already there? */
  41. if (cxt->node_tbl[i] == node)
  42. return (void *)i;
  43. if (cxt->nodes_used < cxt->node_max) {
  44. cxt->node_tbl[cxt->nodes_used] = node;
  45. return (void *)cxt->nodes_used++;
  46. }
  47. return NULL;
  48. }
  49. static char *ft_node_ph2node(struct ft_cxt *cxt, const void *phandle)
  50. {
  51. unsigned int i = (unsigned int)phandle;
  52. if (i < cxt->nodes_used)
  53. return cxt->node_tbl[i];
  54. return NULL;
  55. }
  56. static void ft_node_update_before(struct ft_cxt *cxt, char *addr, int shift)
  57. {
  58. unsigned int i;
  59. if (shift == 0)
  60. return;
  61. for (i = 1; i < cxt->nodes_used; i++)
  62. if (cxt->node_tbl[i] < addr)
  63. cxt->node_tbl[i] += shift;
  64. }
  65. static void ft_node_update_after(struct ft_cxt *cxt, char *addr, int shift)
  66. {
  67. unsigned int i;
  68. if (shift == 0)
  69. return;
  70. for (i = 1; i < cxt->nodes_used; i++)
  71. if (cxt->node_tbl[i] >= addr)
  72. cxt->node_tbl[i] += shift;
  73. }
  74. /* Struct used to return info from ft_next() */
  75. struct ft_atom {
  76. u32 tag;
  77. const char *name;
  78. void *data;
  79. u32 size;
  80. };
  81. /* Set ptrs to current one's info; return addr of next one */
  82. static char *ft_next(struct ft_cxt *cxt, char *p, struct ft_atom *ret)
  83. {
  84. u32 sz;
  85. if (p >= cxt->rgn[FT_STRUCT].start + cxt->rgn[FT_STRUCT].size)
  86. return NULL;
  87. ret->tag = be32_to_cpu(*(u32 *) p);
  88. p += 4;
  89. switch (ret->tag) { /* Tag */
  90. case OF_DT_BEGIN_NODE:
  91. ret->name = p;
  92. ret->data = (void *)(p - 4); /* start of node */
  93. p += _ALIGN(strlen(p) + 1, 4);
  94. break;
  95. case OF_DT_PROP:
  96. ret->size = sz = be32_to_cpu(*(u32 *) p);
  97. ret->name = cxt->str_anchor + be32_to_cpu(*(u32 *) (p + 4));
  98. ret->data = (void *)(p + 8);
  99. p += 8 + _ALIGN(sz, 4);
  100. break;
  101. case OF_DT_END_NODE:
  102. case OF_DT_NOP:
  103. break;
  104. case OF_DT_END:
  105. default:
  106. p = NULL;
  107. break;
  108. }
  109. return p;
  110. }
  111. #define HDR_SIZE _ALIGN(sizeof(struct boot_param_header), 8)
  112. #define EXPAND_INCR 1024 /* alloc this much extra when expanding */
  113. /* Copy the tree to a newly-allocated region and put things in order */
  114. static int ft_reorder(struct ft_cxt *cxt, int nextra)
  115. {
  116. unsigned long tot;
  117. enum ft_rgn_id r;
  118. char *p, *pend;
  119. int stroff;
  120. tot = HDR_SIZE + EXPAND_INCR;
  121. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r)
  122. tot += cxt->rgn[r].size;
  123. if (nextra > 0)
  124. tot += nextra;
  125. tot = _ALIGN(tot, 8);
  126. if (!cxt->realloc)
  127. return 0;
  128. p = cxt->realloc(NULL, tot);
  129. if (!p)
  130. return 0;
  131. memcpy(p, cxt->bph, sizeof(struct boot_param_header));
  132. /* offsets get fixed up later */
  133. cxt->bph = (struct boot_param_header *)p;
  134. cxt->max_size = tot;
  135. pend = p + tot;
  136. p += HDR_SIZE;
  137. memcpy(p, cxt->rgn[FT_RSVMAP].start, cxt->rgn[FT_RSVMAP].size);
  138. cxt->rgn[FT_RSVMAP].start = p;
  139. p += cxt->rgn[FT_RSVMAP].size;
  140. memcpy(p, cxt->rgn[FT_STRUCT].start, cxt->rgn[FT_STRUCT].size);
  141. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  142. p - cxt->rgn[FT_STRUCT].start);
  143. cxt->p += p - cxt->rgn[FT_STRUCT].start;
  144. cxt->rgn[FT_STRUCT].start = p;
  145. p = pend - cxt->rgn[FT_STRINGS].size;
  146. memcpy(p, cxt->rgn[FT_STRINGS].start, cxt->rgn[FT_STRINGS].size);
  147. stroff = cxt->str_anchor - cxt->rgn[FT_STRINGS].start;
  148. cxt->rgn[FT_STRINGS].start = p;
  149. cxt->str_anchor = p + stroff;
  150. cxt->isordered = 1;
  151. return 1;
  152. }
  153. static inline char *prev_end(struct ft_cxt *cxt, enum ft_rgn_id r)
  154. {
  155. if (r > FT_RSVMAP)
  156. return cxt->rgn[r - 1].start + cxt->rgn[r - 1].size;
  157. return (char *)cxt->bph + HDR_SIZE;
  158. }
  159. static inline char *next_start(struct ft_cxt *cxt, enum ft_rgn_id r)
  160. {
  161. if (r < FT_STRINGS)
  162. return cxt->rgn[r + 1].start;
  163. return (char *)cxt->bph + cxt->max_size;
  164. }
  165. /*
  166. * See if we can expand region rgn by nextra bytes by using up
  167. * free space after or before the region.
  168. */
  169. static int ft_shuffle(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
  170. int nextra)
  171. {
  172. char *p = *pp;
  173. char *rgn_start, *rgn_end;
  174. rgn_start = cxt->rgn[rgn].start;
  175. rgn_end = rgn_start + cxt->rgn[rgn].size;
  176. if (nextra <= 0 || rgn_end + nextra <= next_start(cxt, rgn)) {
  177. /* move following stuff */
  178. if (p < rgn_end) {
  179. if (nextra < 0)
  180. memmove(p, p - nextra, rgn_end - p + nextra);
  181. else
  182. memmove(p + nextra, p, rgn_end - p);
  183. if (rgn == FT_STRUCT)
  184. ft_node_update_after(cxt, p, nextra);
  185. }
  186. cxt->rgn[rgn].size += nextra;
  187. if (rgn == FT_STRINGS)
  188. /* assumes strings only added at beginning */
  189. cxt->str_anchor += nextra;
  190. return 1;
  191. }
  192. if (prev_end(cxt, rgn) <= rgn_start - nextra) {
  193. /* move preceding stuff */
  194. if (p > rgn_start) {
  195. memmove(rgn_start - nextra, rgn_start, p - rgn_start);
  196. if (rgn == FT_STRUCT)
  197. ft_node_update_before(cxt, p, -nextra);
  198. }
  199. *pp -= nextra;
  200. cxt->rgn[rgn].start -= nextra;
  201. cxt->rgn[rgn].size += nextra;
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. static int ft_make_space(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
  207. int nextra)
  208. {
  209. unsigned long size, ssize, tot;
  210. char *str, *next;
  211. enum ft_rgn_id r;
  212. if (!cxt->isordered) {
  213. unsigned long rgn_off = *pp - cxt->rgn[rgn].start;
  214. if (!ft_reorder(cxt, nextra))
  215. return 0;
  216. *pp = cxt->rgn[rgn].start + rgn_off;
  217. }
  218. if (ft_shuffle(cxt, pp, rgn, nextra))
  219. return 1;
  220. /* See if there is space after the strings section */
  221. ssize = cxt->rgn[FT_STRINGS].size;
  222. if (cxt->rgn[FT_STRINGS].start + ssize
  223. < (char *)cxt->bph + cxt->max_size) {
  224. /* move strings up as far as possible */
  225. str = (char *)cxt->bph + cxt->max_size - ssize;
  226. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  227. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  228. cxt->rgn[FT_STRINGS].start = str;
  229. /* enough space now? */
  230. if (rgn >= FT_STRUCT && ft_shuffle(cxt, pp, rgn, nextra))
  231. return 1;
  232. }
  233. /* how much total free space is there following this region? */
  234. tot = 0;
  235. for (r = rgn; r < FT_STRINGS; ++r) {
  236. char *r_end = cxt->rgn[r].start + cxt->rgn[r].size;
  237. tot += next_start(cxt, rgn) - r_end;
  238. }
  239. /* cast is to shut gcc up; we know nextra >= 0 */
  240. if (tot < (unsigned int)nextra) {
  241. /* have to reallocate */
  242. char *newp, *new_start;
  243. int shift;
  244. if (!cxt->realloc)
  245. return 0;
  246. size = _ALIGN(cxt->max_size + (nextra - tot) + EXPAND_INCR, 8);
  247. newp = cxt->realloc(cxt->bph, size);
  248. if (!newp)
  249. return 0;
  250. cxt->max_size = size;
  251. shift = newp - (char *)cxt->bph;
  252. if (shift) { /* realloc can return same addr */
  253. cxt->bph = (struct boot_param_header *)newp;
  254. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
  255. shift);
  256. for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
  257. new_start = cxt->rgn[r].start + shift;
  258. cxt->rgn[r].start = new_start;
  259. }
  260. *pp += shift;
  261. cxt->str_anchor += shift;
  262. }
  263. /* move strings up to the end */
  264. str = newp + size - ssize;
  265. cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
  266. memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
  267. cxt->rgn[FT_STRINGS].start = str;
  268. if (ft_shuffle(cxt, pp, rgn, nextra))
  269. return 1;
  270. }
  271. /* must be FT_RSVMAP and we need to move FT_STRUCT up */
  272. if (rgn == FT_RSVMAP) {
  273. next = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  274. + nextra;
  275. ssize = cxt->rgn[FT_STRUCT].size;
  276. if (next + ssize >= cxt->rgn[FT_STRINGS].start)
  277. return 0; /* "can't happen" */
  278. memmove(next, cxt->rgn[FT_STRUCT].start, ssize);
  279. ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start, nextra);
  280. cxt->rgn[FT_STRUCT].start = next;
  281. if (ft_shuffle(cxt, pp, rgn, nextra))
  282. return 1;
  283. }
  284. return 0; /* "can't happen" */
  285. }
  286. static void ft_put_word(struct ft_cxt *cxt, u32 v)
  287. {
  288. *(u32 *) cxt->p = cpu_to_be32(v);
  289. cxt->p += 4;
  290. }
  291. static void ft_put_bin(struct ft_cxt *cxt, const void *data, unsigned int sz)
  292. {
  293. unsigned long sza = _ALIGN(sz, 4);
  294. /* zero out the alignment gap if necessary */
  295. if (sz < sza)
  296. *(u32 *) (cxt->p + sza - 4) = 0;
  297. /* copy in the data */
  298. memcpy(cxt->p, data, sz);
  299. cxt->p += sza;
  300. }
  301. char *ft_begin_node(struct ft_cxt *cxt, const char *name)
  302. {
  303. unsigned long nlen = strlen(name) + 1;
  304. unsigned long len = 8 + _ALIGN(nlen, 4);
  305. char *ret;
  306. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
  307. return NULL;
  308. ret = cxt->p;
  309. ft_put_word(cxt, OF_DT_BEGIN_NODE);
  310. ft_put_bin(cxt, name, strlen(name) + 1);
  311. return ret;
  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 = map_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. cxt->p = cxt->rgn[FT_STRUCT].start;
  472. cxt->str_anchor = cxt->rgn[FT_STRINGS].start;
  473. return 0;
  474. }
  475. /* add a reserver physical area to the rsvmap */
  476. int ft_add_rsvmap(struct ft_cxt *cxt, u64 physaddr, u64 size)
  477. {
  478. char *p;
  479. struct ft_reserve *pres;
  480. p = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
  481. - sizeof(struct ft_reserve);
  482. if (!ft_make_space(cxt, &p, FT_RSVMAP, sizeof(struct ft_reserve)))
  483. return -1;
  484. pres = (struct ft_reserve *)p;
  485. pres->start = cpu_to_be64(physaddr);
  486. pres->len = cpu_to_be64(size);
  487. return 0;
  488. }
  489. void ft_begin_tree(struct ft_cxt *cxt)
  490. {
  491. cxt->p = ft_root_node(cxt);
  492. }
  493. void ft_end_tree(struct ft_cxt *cxt)
  494. {
  495. struct boot_param_header *bph = cxt->bph;
  496. char *p, *oldstr, *str, *endp;
  497. unsigned long ssize;
  498. int adj;
  499. if (!cxt->isordered)
  500. return; /* we haven't touched anything */
  501. /* adjust string offsets */
  502. oldstr = cxt->rgn[FT_STRINGS].start;
  503. adj = cxt->str_anchor - oldstr;
  504. if (adj)
  505. adjust_string_offsets(cxt, adj);
  506. /* make strings end on 8-byte boundary */
  507. ssize = cxt->rgn[FT_STRINGS].size;
  508. endp = (char *)_ALIGN((unsigned long)cxt->rgn[FT_STRUCT].start
  509. + cxt->rgn[FT_STRUCT].size + ssize, 8);
  510. str = endp - ssize;
  511. /* move strings down to end of structs */
  512. memmove(str, oldstr, ssize);
  513. cxt->str_anchor = str;
  514. cxt->rgn[FT_STRINGS].start = str;
  515. /* fill in header fields */
  516. p = (char *)bph;
  517. bph->totalsize = cpu_to_be32(endp - p);
  518. bph->off_mem_rsvmap = cpu_to_be32(cxt->rgn[FT_RSVMAP].start - p);
  519. bph->off_dt_struct = cpu_to_be32(cxt->rgn[FT_STRUCT].start - p);
  520. bph->off_dt_strings = cpu_to_be32(cxt->rgn[FT_STRINGS].start - p);
  521. bph->dt_strings_size = cpu_to_be32(ssize);
  522. }
  523. void *ft_find_device(struct ft_cxt *cxt, const void *top, const char *srch_path)
  524. {
  525. char *node;
  526. if (top) {
  527. node = ft_node_ph2node(cxt, top);
  528. if (node == NULL)
  529. return NULL;
  530. } else {
  531. node = ft_root_node(cxt);
  532. }
  533. node = ft_find_descendent(cxt, node, srch_path);
  534. return ft_get_phandle(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, void *node)
  595. {
  596. int d;
  597. struct ft_atom atom;
  598. char *p;
  599. for (d = 0; cxt->genealogy[d] != NULL; ++d)
  600. if (cxt->genealogy[d] == node)
  601. return d > 0 ? cxt->genealogy[d - 1] : NULL;
  602. /* have to do it the hard way... */
  603. p = ft_root_node(cxt);
  604. d = 0;
  605. while ((p = ft_next(cxt, p, &atom)) != NULL) {
  606. switch (atom.tag) {
  607. case OF_DT_BEGIN_NODE:
  608. cxt->genealogy[d] = atom.data;
  609. if (node == atom.data) {
  610. /* found it */
  611. cxt->genealogy[d + 1] = NULL;
  612. return d > 0 ? cxt->genealogy[d - 1] : NULL;
  613. }
  614. ++d;
  615. break;
  616. case OF_DT_END_NODE:
  617. --d;
  618. break;
  619. }
  620. }
  621. return NULL;
  622. }
  623. void *ft_get_parent(struct ft_cxt *cxt, const void *phandle)
  624. {
  625. void *node = ft_node_ph2node(cxt, phandle);
  626. if (node == NULL)
  627. return NULL;
  628. node = __ft_get_parent(cxt, node);
  629. return ft_get_phandle(cxt, node);
  630. }
  631. static const void *__ft_get_prop(struct ft_cxt *cxt, void *node,
  632. const char *propname, unsigned int *len)
  633. {
  634. struct ft_atom atom;
  635. int depth = 0;
  636. while ((node = ft_next(cxt, node, &atom)) != NULL) {
  637. switch (atom.tag) {
  638. case OF_DT_BEGIN_NODE:
  639. ++depth;
  640. break;
  641. case OF_DT_PROP:
  642. if (depth != 1 || strcmp(atom.name, propname))
  643. break;
  644. if (len)
  645. *len = atom.size;
  646. return atom.data;
  647. case OF_DT_END_NODE:
  648. if (--depth <= 0)
  649. return NULL;
  650. }
  651. }
  652. return NULL;
  653. }
  654. int ft_get_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  655. void *buf, const unsigned int buflen)
  656. {
  657. const void *data;
  658. unsigned int size;
  659. void *node = ft_node_ph2node(cxt, phandle);
  660. if (!node)
  661. return -1;
  662. data = __ft_get_prop(cxt, node, propname, &size);
  663. if (data) {
  664. unsigned int clipped_size = min(size, buflen);
  665. memcpy(buf, data, clipped_size);
  666. return size;
  667. }
  668. return -1;
  669. }
  670. void *__ft_find_node_by_prop_value(struct ft_cxt *cxt, void *prev,
  671. const char *propname, const char *propval,
  672. unsigned int proplen)
  673. {
  674. struct ft_atom atom;
  675. char *p = ft_root_node(cxt);
  676. char *next;
  677. int past_prev = prev ? 0 : 1;
  678. int depth = -1;
  679. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  680. const void *data;
  681. unsigned int size;
  682. switch (atom.tag) {
  683. case OF_DT_BEGIN_NODE:
  684. depth++;
  685. if (prev == p) {
  686. past_prev = 1;
  687. break;
  688. }
  689. if (!past_prev || depth < 1)
  690. break;
  691. data = __ft_get_prop(cxt, p, propname, &size);
  692. if (!data || size != proplen)
  693. break;
  694. if (memcmp(data, propval, size))
  695. break;
  696. return p;
  697. case OF_DT_END_NODE:
  698. if (depth-- == 0)
  699. return NULL;
  700. break;
  701. }
  702. p = next;
  703. }
  704. return NULL;
  705. }
  706. void *ft_find_node_by_prop_value(struct ft_cxt *cxt, const void *prev,
  707. const char *propname, const char *propval,
  708. int proplen)
  709. {
  710. void *node = NULL;
  711. if (prev) {
  712. node = ft_node_ph2node(cxt, prev);
  713. if (!node)
  714. return NULL;
  715. }
  716. node = __ft_find_node_by_prop_value(cxt, node, propname,
  717. propval, proplen);
  718. return ft_get_phandle(cxt, node);
  719. }
  720. int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
  721. const void *buf, const unsigned int buflen)
  722. {
  723. struct ft_atom atom;
  724. void *node;
  725. char *p, *next;
  726. int nextra;
  727. node = ft_node_ph2node(cxt, phandle);
  728. if (node == NULL)
  729. return -1;
  730. next = ft_next(cxt, node, &atom);
  731. if (atom.tag != OF_DT_BEGIN_NODE)
  732. /* phandle didn't point to a node */
  733. return -1;
  734. p = next;
  735. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  736. switch (atom.tag) {
  737. case OF_DT_BEGIN_NODE: /* properties must go before subnodes */
  738. case OF_DT_END_NODE:
  739. /* haven't found the property, insert here */
  740. cxt->p = p;
  741. return ft_prop(cxt, propname, buf, buflen);
  742. case OF_DT_PROP:
  743. if (strcmp(atom.name, propname))
  744. break;
  745. /* found an existing property, overwrite it */
  746. nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4);
  747. cxt->p = atom.data;
  748. if (nextra && !ft_make_space(cxt, &cxt->p, FT_STRUCT,
  749. nextra))
  750. return -1;
  751. *(u32 *) (cxt->p - 8) = cpu_to_be32(buflen);
  752. ft_put_bin(cxt, buf, buflen);
  753. return 0;
  754. }
  755. p = next;
  756. }
  757. return -1;
  758. }
  759. int ft_del_prop(struct ft_cxt *cxt, const void *phandle, const char *propname)
  760. {
  761. struct ft_atom atom;
  762. void *node;
  763. char *p, *next;
  764. int size;
  765. node = ft_node_ph2node(cxt, phandle);
  766. if (node == NULL)
  767. return -1;
  768. p = node;
  769. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  770. switch (atom.tag) {
  771. case OF_DT_BEGIN_NODE:
  772. case OF_DT_END_NODE:
  773. return -1;
  774. case OF_DT_PROP:
  775. if (strcmp(atom.name, propname))
  776. break;
  777. /* found the property, remove it */
  778. size = 12 + -_ALIGN(atom.size, 4);
  779. cxt->p = p;
  780. if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, -size))
  781. return -1;
  782. return 0;
  783. }
  784. p = next;
  785. }
  786. return -1;
  787. }
  788. void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *name)
  789. {
  790. struct ft_atom atom;
  791. char *p, *next, *ret;
  792. int depth = 0;
  793. if (parent) {
  794. p = ft_node_ph2node(cxt, parent);
  795. if (!p)
  796. return NULL;
  797. } else {
  798. p = ft_root_node(cxt);
  799. }
  800. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  801. switch (atom.tag) {
  802. case OF_DT_BEGIN_NODE:
  803. ++depth;
  804. if (depth == 1 && strcmp(atom.name, name) == 0)
  805. /* duplicate node name, return error */
  806. return NULL;
  807. break;
  808. case OF_DT_END_NODE:
  809. --depth;
  810. if (depth > 0)
  811. break;
  812. /* end of node, insert here */
  813. cxt->p = p;
  814. ret = ft_begin_node(cxt, name);
  815. ft_end_node(cxt);
  816. return ft_get_phandle(cxt, ret);
  817. }
  818. p = next;
  819. }
  820. return NULL;
  821. }
  822. /* Returns the start of the path within the provided buffer, or NULL on
  823. * error.
  824. */
  825. char *ft_get_path(struct ft_cxt *cxt, const void *phandle,
  826. char *buf, int len)
  827. {
  828. const char *path_comp[FT_MAX_DEPTH];
  829. struct ft_atom atom;
  830. char *p, *next, *pos;
  831. int depth = 0, i;
  832. void *node;
  833. node = ft_node_ph2node(cxt, phandle);
  834. if (node == NULL)
  835. return NULL;
  836. p = ft_root_node(cxt);
  837. while ((next = ft_next(cxt, p, &atom)) != NULL) {
  838. switch (atom.tag) {
  839. case OF_DT_BEGIN_NODE:
  840. path_comp[depth++] = atom.name;
  841. if (p == node)
  842. goto found;
  843. break;
  844. case OF_DT_END_NODE:
  845. if (--depth == 0)
  846. return NULL;
  847. }
  848. p = next;
  849. }
  850. found:
  851. pos = buf;
  852. for (i = 1; i < depth; i++) {
  853. int this_len;
  854. if (len <= 1)
  855. return NULL;
  856. *pos++ = '/';
  857. len--;
  858. strncpy(pos, path_comp[i], len);
  859. if (pos[len - 1] != 0)
  860. return NULL;
  861. this_len = strlen(pos);
  862. len -= this_len;
  863. pos += this_len;
  864. }
  865. return buf;
  866. }