fdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Functions for working with the Flattened Device Tree data format
  3. *
  4. * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
  5. * benh@kernel.crashing.org
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/initrd.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
  20. #ifdef CONFIG_PPC
  21. #include <asm/machdep.h>
  22. #endif /* CONFIG_PPC */
  23. #include <asm/page.h>
  24. char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
  25. {
  26. return ((char *)blob) +
  27. be32_to_cpu(blob->off_dt_strings) + offset;
  28. }
  29. /**
  30. * of_fdt_get_property - Given a node in the given flat blob, return
  31. * the property ptr
  32. */
  33. void *of_fdt_get_property(struct boot_param_header *blob,
  34. unsigned long node, const char *name,
  35. unsigned long *size)
  36. {
  37. unsigned long p = node;
  38. do {
  39. u32 tag = be32_to_cpup((__be32 *)p);
  40. u32 sz, noff;
  41. const char *nstr;
  42. p += 4;
  43. if (tag == OF_DT_NOP)
  44. continue;
  45. if (tag != OF_DT_PROP)
  46. return NULL;
  47. sz = be32_to_cpup((__be32 *)p);
  48. noff = be32_to_cpup((__be32 *)(p + 4));
  49. p += 8;
  50. if (be32_to_cpu(blob->version) < 0x10)
  51. p = ALIGN(p, sz >= 8 ? 8 : 4);
  52. nstr = of_fdt_get_string(blob, noff);
  53. if (nstr == NULL) {
  54. pr_warning("Can't find property index name !\n");
  55. return NULL;
  56. }
  57. if (strcmp(name, nstr) == 0) {
  58. if (size)
  59. *size = sz;
  60. return (void *)p;
  61. }
  62. p += sz;
  63. p = ALIGN(p, 4);
  64. } while (1);
  65. }
  66. /**
  67. * of_fdt_is_compatible - Return true if given node from the given blob has
  68. * compat in its compatible list
  69. * @blob: A device tree blob
  70. * @node: node to test
  71. * @compat: compatible string to compare with compatible list.
  72. *
  73. * On match, returns a non-zero value with smaller values returned for more
  74. * specific compatible values.
  75. */
  76. int of_fdt_is_compatible(struct boot_param_header *blob,
  77. unsigned long node, const char *compat)
  78. {
  79. const char *cp;
  80. unsigned long cplen, l, score = 0;
  81. cp = of_fdt_get_property(blob, node, "compatible", &cplen);
  82. if (cp == NULL)
  83. return 0;
  84. while (cplen > 0) {
  85. score++;
  86. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  87. return score;
  88. l = strlen(cp) + 1;
  89. cp += l;
  90. cplen -= l;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * of_fdt_match - Return true if node matches a list of compatible values
  96. */
  97. int of_fdt_match(struct boot_param_header *blob, unsigned long node,
  98. const char *const *compat)
  99. {
  100. unsigned int tmp, score = 0;
  101. if (!compat)
  102. return 0;
  103. while (*compat) {
  104. tmp = of_fdt_is_compatible(blob, node, *compat);
  105. if (tmp && (score == 0 || (tmp < score)))
  106. score = tmp;
  107. compat++;
  108. }
  109. return score;
  110. }
  111. static void *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  112. unsigned long align)
  113. {
  114. void *res;
  115. *mem = ALIGN(*mem, align);
  116. res = (void *)*mem;
  117. *mem += size;
  118. return res;
  119. }
  120. /**
  121. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  122. * @blob: The parent device tree blob
  123. * @mem: Memory chunk to use for allocating device nodes and properties
  124. * @p: pointer to node in flat tree
  125. * @dad: Parent struct device_node
  126. * @allnextpp: pointer to ->allnext from last allocated device_node
  127. * @fpsize: Size of the node path up at the current depth.
  128. */
  129. static unsigned long unflatten_dt_node(struct boot_param_header *blob,
  130. unsigned long mem,
  131. unsigned long *p,
  132. struct device_node *dad,
  133. struct device_node ***allnextpp,
  134. unsigned long fpsize)
  135. {
  136. struct device_node *np;
  137. struct property *pp, **prev_pp = NULL;
  138. char *pathp;
  139. u32 tag;
  140. unsigned int l, allocl;
  141. int has_name = 0;
  142. int new_format = 0;
  143. tag = be32_to_cpup((__be32 *)(*p));
  144. if (tag != OF_DT_BEGIN_NODE) {
  145. pr_err("Weird tag at start of node: %x\n", tag);
  146. return mem;
  147. }
  148. *p += 4;
  149. pathp = (char *)*p;
  150. l = allocl = strlen(pathp) + 1;
  151. *p = ALIGN(*p + l, 4);
  152. /* version 0x10 has a more compact unit name here instead of the full
  153. * path. we accumulate the full path size using "fpsize", we'll rebuild
  154. * it later. We detect this because the first character of the name is
  155. * not '/'.
  156. */
  157. if ((*pathp) != '/') {
  158. new_format = 1;
  159. if (fpsize == 0) {
  160. /* root node: special case. fpsize accounts for path
  161. * plus terminating zero. root node only has '/', so
  162. * fpsize should be 2, but we want to avoid the first
  163. * level nodes to have two '/' so we use fpsize 1 here
  164. */
  165. fpsize = 1;
  166. allocl = 2;
  167. l = 1;
  168. *pathp = '\0';
  169. } else {
  170. /* account for '/' and path size minus terminal 0
  171. * already in 'l'
  172. */
  173. fpsize += l;
  174. allocl = fpsize;
  175. }
  176. }
  177. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  178. __alignof__(struct device_node));
  179. if (allnextpp) {
  180. memset(np, 0, sizeof(*np));
  181. np->full_name = ((char *)np) + sizeof(struct device_node);
  182. if (new_format) {
  183. char *fn = np->full_name;
  184. /* rebuild full path for new format */
  185. if (dad && dad->parent) {
  186. strcpy(fn, dad->full_name);
  187. #ifdef DEBUG
  188. if ((strlen(fn) + l + 1) != allocl) {
  189. pr_debug("%s: p: %d, l: %d, a: %d\n",
  190. pathp, (int)strlen(fn),
  191. l, allocl);
  192. }
  193. #endif
  194. fn += strlen(fn);
  195. }
  196. *(fn++) = '/';
  197. memcpy(fn, pathp, l);
  198. } else
  199. memcpy(np->full_name, pathp, l);
  200. prev_pp = &np->properties;
  201. **allnextpp = np;
  202. *allnextpp = &np->allnext;
  203. if (dad != NULL) {
  204. np->parent = dad;
  205. /* we temporarily use the next field as `last_child'*/
  206. if (dad->next == NULL)
  207. dad->child = np;
  208. else
  209. dad->next->sibling = np;
  210. dad->next = np;
  211. }
  212. kref_init(&np->kref);
  213. }
  214. /* process properties */
  215. while (1) {
  216. u32 sz, noff;
  217. char *pname;
  218. tag = be32_to_cpup((__be32 *)(*p));
  219. if (tag == OF_DT_NOP) {
  220. *p += 4;
  221. continue;
  222. }
  223. if (tag != OF_DT_PROP)
  224. break;
  225. *p += 4;
  226. sz = be32_to_cpup((__be32 *)(*p));
  227. noff = be32_to_cpup((__be32 *)((*p) + 4));
  228. *p += 8;
  229. if (be32_to_cpu(blob->version) < 0x10)
  230. *p = ALIGN(*p, sz >= 8 ? 8 : 4);
  231. pname = of_fdt_get_string(blob, noff);
  232. if (pname == NULL) {
  233. pr_info("Can't find property name in list !\n");
  234. break;
  235. }
  236. if (strcmp(pname, "name") == 0)
  237. has_name = 1;
  238. l = strlen(pname) + 1;
  239. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  240. __alignof__(struct property));
  241. if (allnextpp) {
  242. /* We accept flattened tree phandles either in
  243. * ePAPR-style "phandle" properties, or the
  244. * legacy "linux,phandle" properties. If both
  245. * appear and have different values, things
  246. * will get weird. Don't do that. */
  247. if ((strcmp(pname, "phandle") == 0) ||
  248. (strcmp(pname, "linux,phandle") == 0)) {
  249. if (np->phandle == 0)
  250. np->phandle = be32_to_cpup((__be32*)*p);
  251. }
  252. /* And we process the "ibm,phandle" property
  253. * used in pSeries dynamic device tree
  254. * stuff */
  255. if (strcmp(pname, "ibm,phandle") == 0)
  256. np->phandle = be32_to_cpup((__be32 *)*p);
  257. pp->name = pname;
  258. pp->length = sz;
  259. pp->value = (void *)*p;
  260. *prev_pp = pp;
  261. prev_pp = &pp->next;
  262. }
  263. *p = ALIGN((*p) + sz, 4);
  264. }
  265. /* with version 0x10 we may not have the name property, recreate
  266. * it here from the unit name if absent
  267. */
  268. if (!has_name) {
  269. char *p1 = pathp, *ps = pathp, *pa = NULL;
  270. int sz;
  271. while (*p1) {
  272. if ((*p1) == '@')
  273. pa = p1;
  274. if ((*p1) == '/')
  275. ps = p1 + 1;
  276. p1++;
  277. }
  278. if (pa < ps)
  279. pa = p1;
  280. sz = (pa - ps) + 1;
  281. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  282. __alignof__(struct property));
  283. if (allnextpp) {
  284. pp->name = "name";
  285. pp->length = sz;
  286. pp->value = pp + 1;
  287. *prev_pp = pp;
  288. prev_pp = &pp->next;
  289. memcpy(pp->value, ps, sz - 1);
  290. ((char *)pp->value)[sz - 1] = 0;
  291. pr_debug("fixed up name for %s -> %s\n", pathp,
  292. (char *)pp->value);
  293. }
  294. }
  295. if (allnextpp) {
  296. *prev_pp = NULL;
  297. np->name = of_get_property(np, "name", NULL);
  298. np->type = of_get_property(np, "device_type", NULL);
  299. if (!np->name)
  300. np->name = "<NULL>";
  301. if (!np->type)
  302. np->type = "<NULL>";
  303. }
  304. while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
  305. if (tag == OF_DT_NOP)
  306. *p += 4;
  307. else
  308. mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
  309. fpsize);
  310. tag = be32_to_cpup((__be32 *)(*p));
  311. }
  312. if (tag != OF_DT_END_NODE) {
  313. pr_err("Weird tag at end of node: %x\n", tag);
  314. return mem;
  315. }
  316. *p += 4;
  317. return mem;
  318. }
  319. /**
  320. * __unflatten_device_tree - create tree of device_nodes from flat blob
  321. *
  322. * unflattens a device-tree, creating the
  323. * tree of struct device_node. It also fills the "name" and "type"
  324. * pointers of the nodes so the normal device-tree walking functions
  325. * can be used.
  326. * @blob: The blob to expand
  327. * @mynodes: The device_node tree created by the call
  328. * @dt_alloc: An allocator that provides a virtual address to memory
  329. * for the resulting tree
  330. */
  331. static void __unflatten_device_tree(struct boot_param_header *blob,
  332. struct device_node **mynodes,
  333. void * (*dt_alloc)(u64 size, u64 align))
  334. {
  335. unsigned long start, mem, size;
  336. struct device_node **allnextp = mynodes;
  337. pr_debug(" -> unflatten_device_tree()\n");
  338. if (!blob) {
  339. pr_debug("No device tree pointer\n");
  340. return;
  341. }
  342. pr_debug("Unflattening device tree:\n");
  343. pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
  344. pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
  345. pr_debug("version: %08x\n", be32_to_cpu(blob->version));
  346. if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
  347. pr_err("Invalid device tree blob header\n");
  348. return;
  349. }
  350. /* First pass, scan for size */
  351. start = ((unsigned long)blob) +
  352. be32_to_cpu(blob->off_dt_struct);
  353. size = unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
  354. size = (size | 3) + 1;
  355. pr_debug(" size is %lx, allocating...\n", size);
  356. /* Allocate memory for the expanded device tree */
  357. mem = (unsigned long)
  358. dt_alloc(size + 4, __alignof__(struct device_node));
  359. memset((void *)mem, 0, size);
  360. ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
  361. pr_debug(" unflattening %lx...\n", mem);
  362. /* Second pass, do actual unflattening */
  363. start = ((unsigned long)blob) +
  364. be32_to_cpu(blob->off_dt_struct);
  365. unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
  366. if (be32_to_cpup((__be32 *)start) != OF_DT_END)
  367. pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
  368. if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
  369. pr_warning("End of tree marker overwritten: %08x\n",
  370. be32_to_cpu(((__be32 *)mem)[size / 4]));
  371. *allnextp = NULL;
  372. pr_debug(" <- unflatten_device_tree()\n");
  373. }
  374. static void *kernel_tree_alloc(u64 size, u64 align)
  375. {
  376. return kzalloc(size, GFP_KERNEL);
  377. }
  378. /**
  379. * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
  380. *
  381. * unflattens the device-tree passed by the firmware, creating the
  382. * tree of struct device_node. It also fills the "name" and "type"
  383. * pointers of the nodes so the normal device-tree walking functions
  384. * can be used.
  385. */
  386. void of_fdt_unflatten_tree(unsigned long *blob,
  387. struct device_node **mynodes)
  388. {
  389. struct boot_param_header *device_tree =
  390. (struct boot_param_header *)blob;
  391. __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
  392. }
  393. EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
  394. /* Everything below here references initial_boot_params directly. */
  395. int __initdata dt_root_addr_cells;
  396. int __initdata dt_root_size_cells;
  397. struct boot_param_header *initial_boot_params;
  398. #ifdef CONFIG_OF_EARLY_FLATTREE
  399. /**
  400. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  401. * @it: callback function
  402. * @data: context data pointer
  403. *
  404. * This function is used to scan the flattened device-tree, it is
  405. * used to extract the memory information at boot before we can
  406. * unflatten the tree
  407. */
  408. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  409. const char *uname, int depth,
  410. void *data),
  411. void *data)
  412. {
  413. unsigned long p = ((unsigned long)initial_boot_params) +
  414. be32_to_cpu(initial_boot_params->off_dt_struct);
  415. int rc = 0;
  416. int depth = -1;
  417. do {
  418. u32 tag = be32_to_cpup((__be32 *)p);
  419. char *pathp;
  420. p += 4;
  421. if (tag == OF_DT_END_NODE) {
  422. depth--;
  423. continue;
  424. }
  425. if (tag == OF_DT_NOP)
  426. continue;
  427. if (tag == OF_DT_END)
  428. break;
  429. if (tag == OF_DT_PROP) {
  430. u32 sz = be32_to_cpup((__be32 *)p);
  431. p += 8;
  432. if (be32_to_cpu(initial_boot_params->version) < 0x10)
  433. p = ALIGN(p, sz >= 8 ? 8 : 4);
  434. p += sz;
  435. p = ALIGN(p, 4);
  436. continue;
  437. }
  438. if (tag != OF_DT_BEGIN_NODE) {
  439. pr_err("Invalid tag %x in flat device tree!\n", tag);
  440. return -EINVAL;
  441. }
  442. depth++;
  443. pathp = (char *)p;
  444. p = ALIGN(p + strlen(pathp) + 1, 4);
  445. if ((*pathp) == '/') {
  446. char *lp, *np;
  447. for (lp = NULL, np = pathp; *np; np++)
  448. if ((*np) == '/')
  449. lp = np+1;
  450. if (lp != NULL)
  451. pathp = lp;
  452. }
  453. rc = it(p, pathp, depth, data);
  454. if (rc != 0)
  455. break;
  456. } while (1);
  457. return rc;
  458. }
  459. /**
  460. * of_get_flat_dt_root - find the root node in the flat blob
  461. */
  462. unsigned long __init of_get_flat_dt_root(void)
  463. {
  464. unsigned long p = ((unsigned long)initial_boot_params) +
  465. be32_to_cpu(initial_boot_params->off_dt_struct);
  466. while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
  467. p += 4;
  468. BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
  469. p += 4;
  470. return ALIGN(p + strlen((char *)p) + 1, 4);
  471. }
  472. /**
  473. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  474. *
  475. * This function can be used within scan_flattened_dt callback to get
  476. * access to properties
  477. */
  478. void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  479. unsigned long *size)
  480. {
  481. return of_fdt_get_property(initial_boot_params, node, name, size);
  482. }
  483. /**
  484. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  485. * @node: node to test
  486. * @compat: compatible string to compare with compatible list.
  487. */
  488. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  489. {
  490. return of_fdt_is_compatible(initial_boot_params, node, compat);
  491. }
  492. /**
  493. * of_flat_dt_match - Return true if node matches a list of compatible values
  494. */
  495. int __init of_flat_dt_match(unsigned long node, const char *const *compat)
  496. {
  497. return of_fdt_match(initial_boot_params, node, compat);
  498. }
  499. #ifdef CONFIG_BLK_DEV_INITRD
  500. /**
  501. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  502. * @node: reference to node containing initrd location ('chosen')
  503. */
  504. void __init early_init_dt_check_for_initrd(unsigned long node)
  505. {
  506. unsigned long start, end, len;
  507. __be32 *prop;
  508. pr_debug("Looking for initrd properties... ");
  509. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  510. if (!prop)
  511. return;
  512. start = of_read_ulong(prop, len/4);
  513. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  514. if (!prop)
  515. return;
  516. end = of_read_ulong(prop, len/4);
  517. early_init_dt_setup_initrd_arch(start, end);
  518. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", start, end);
  519. }
  520. #else
  521. inline void early_init_dt_check_for_initrd(unsigned long node)
  522. {
  523. }
  524. #endif /* CONFIG_BLK_DEV_INITRD */
  525. /**
  526. * early_init_dt_scan_root - fetch the top level address and size cells
  527. */
  528. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  529. int depth, void *data)
  530. {
  531. __be32 *prop;
  532. if (depth != 0)
  533. return 0;
  534. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  535. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  536. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  537. if (prop)
  538. dt_root_size_cells = be32_to_cpup(prop);
  539. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  540. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  541. if (prop)
  542. dt_root_addr_cells = be32_to_cpup(prop);
  543. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  544. /* break now */
  545. return 1;
  546. }
  547. u64 __init dt_mem_next_cell(int s, __be32 **cellp)
  548. {
  549. __be32 *p = *cellp;
  550. *cellp = p + s;
  551. return of_read_number(p, s);
  552. }
  553. /**
  554. * early_init_dt_scan_memory - Look for an parse memory nodes
  555. */
  556. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  557. int depth, void *data)
  558. {
  559. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  560. __be32 *reg, *endp;
  561. unsigned long l;
  562. /* We are scanning "memory" nodes only */
  563. if (type == NULL) {
  564. /*
  565. * The longtrail doesn't have a device_type on the
  566. * /memory node, so look for the node called /memory@0.
  567. */
  568. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  569. return 0;
  570. } else if (strcmp(type, "memory") != 0)
  571. return 0;
  572. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  573. if (reg == NULL)
  574. reg = of_get_flat_dt_prop(node, "reg", &l);
  575. if (reg == NULL)
  576. return 0;
  577. endp = reg + (l / sizeof(__be32));
  578. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  579. uname, l, reg[0], reg[1], reg[2], reg[3]);
  580. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  581. u64 base, size;
  582. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  583. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  584. if (size == 0)
  585. continue;
  586. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  587. (unsigned long long)size);
  588. early_init_dt_add_memory_arch(base, size);
  589. }
  590. return 0;
  591. }
  592. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  593. int depth, void *data)
  594. {
  595. unsigned long l;
  596. char *p;
  597. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  598. if (depth != 1 || !data ||
  599. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  600. return 0;
  601. early_init_dt_check_for_initrd(node);
  602. /* Retrieve command line */
  603. p = of_get_flat_dt_prop(node, "bootargs", &l);
  604. if (p != NULL && l > 0)
  605. strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
  606. /*
  607. * CONFIG_CMDLINE is meant to be a default in case nothing else
  608. * managed to set the command line, unless CONFIG_CMDLINE_FORCE
  609. * is set in which case we override whatever was found earlier.
  610. */
  611. #ifdef CONFIG_CMDLINE
  612. #ifndef CONFIG_CMDLINE_FORCE
  613. if (!((char *)data)[0])
  614. #endif
  615. strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  616. #endif /* CONFIG_CMDLINE */
  617. pr_debug("Command line is: %s\n", (char*)data);
  618. /* break now */
  619. return 1;
  620. }
  621. /**
  622. * unflatten_device_tree - create tree of device_nodes from flat blob
  623. *
  624. * unflattens the device-tree passed by the firmware, creating the
  625. * tree of struct device_node. It also fills the "name" and "type"
  626. * pointers of the nodes so the normal device-tree walking functions
  627. * can be used.
  628. */
  629. void __init unflatten_device_tree(void)
  630. {
  631. __unflatten_device_tree(initial_boot_params, &allnodes,
  632. early_init_dt_alloc_memory_arch);
  633. /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
  634. of_alias_scan(early_init_dt_alloc_memory_arch);
  635. }
  636. #endif /* CONFIG_OF_EARLY_FLATTREE */