fdt.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. #define pr_fmt(fmt) "OF: fdt: " fmt
  12. #include <linux/crc32.h>
  13. #include <linux/kernel.h>
  14. #include <linux/initrd.h>
  15. #include <linux/memblock.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/of_fdt.h>
  19. #include <linux/of_reserved_mem.h>
  20. #include <linux/sizes.h>
  21. #include <linux/string.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/libfdt.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/sysfs.h>
  28. #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
  29. #include <asm/page.h>
  30. #include "of_private.h"
  31. /*
  32. * of_fdt_limit_memory - limit the number of regions in the /memory node
  33. * @limit: maximum entries
  34. *
  35. * Adjust the flattened device tree to have at most 'limit' number of
  36. * memory entries in the /memory node. This function may be called
  37. * any time after initial_boot_param is set.
  38. */
  39. void of_fdt_limit_memory(int limit)
  40. {
  41. int memory;
  42. int len;
  43. const void *val;
  44. int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  45. int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  46. const __be32 *addr_prop;
  47. const __be32 *size_prop;
  48. int root_offset;
  49. int cell_size;
  50. root_offset = fdt_path_offset(initial_boot_params, "/");
  51. if (root_offset < 0)
  52. return;
  53. addr_prop = fdt_getprop(initial_boot_params, root_offset,
  54. "#address-cells", NULL);
  55. if (addr_prop)
  56. nr_address_cells = fdt32_to_cpu(*addr_prop);
  57. size_prop = fdt_getprop(initial_boot_params, root_offset,
  58. "#size-cells", NULL);
  59. if (size_prop)
  60. nr_size_cells = fdt32_to_cpu(*size_prop);
  61. cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
  62. memory = fdt_path_offset(initial_boot_params, "/memory");
  63. if (memory > 0) {
  64. val = fdt_getprop(initial_boot_params, memory, "reg", &len);
  65. if (len > limit*cell_size) {
  66. len = limit*cell_size;
  67. pr_debug("Limiting number of entries to %d\n", limit);
  68. fdt_setprop(initial_boot_params, memory, "reg", val,
  69. len);
  70. }
  71. }
  72. }
  73. /**
  74. * of_fdt_is_compatible - Return true if given node from the given blob has
  75. * compat in its compatible list
  76. * @blob: A device tree blob
  77. * @node: node to test
  78. * @compat: compatible string to compare with compatible list.
  79. *
  80. * On match, returns a non-zero value with smaller values returned for more
  81. * specific compatible values.
  82. */
  83. static int of_fdt_is_compatible(const void *blob,
  84. unsigned long node, const char *compat)
  85. {
  86. const char *cp;
  87. int cplen;
  88. unsigned long l, score = 0;
  89. cp = fdt_getprop(blob, node, "compatible", &cplen);
  90. if (cp == NULL)
  91. return 0;
  92. while (cplen > 0) {
  93. score++;
  94. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  95. return score;
  96. l = strlen(cp) + 1;
  97. cp += l;
  98. cplen -= l;
  99. }
  100. return 0;
  101. }
  102. /**
  103. * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
  104. * @blob: A device tree blob
  105. * @node: node to test
  106. *
  107. * Returns true if the node has a "big-endian" property, or if the kernel
  108. * was compiled for BE *and* the node has a "native-endian" property.
  109. * Returns false otherwise.
  110. */
  111. bool of_fdt_is_big_endian(const void *blob, unsigned long node)
  112. {
  113. if (fdt_getprop(blob, node, "big-endian", NULL))
  114. return true;
  115. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
  116. fdt_getprop(blob, node, "native-endian", NULL))
  117. return true;
  118. return false;
  119. }
  120. /**
  121. * of_fdt_match - Return true if node matches a list of compatible values
  122. */
  123. int of_fdt_match(const void *blob, unsigned long node,
  124. const char *const *compat)
  125. {
  126. unsigned int tmp, score = 0;
  127. if (!compat)
  128. return 0;
  129. while (*compat) {
  130. tmp = of_fdt_is_compatible(blob, node, *compat);
  131. if (tmp && (score == 0 || (tmp < score)))
  132. score = tmp;
  133. compat++;
  134. }
  135. return score;
  136. }
  137. static void *unflatten_dt_alloc(void **mem, unsigned long size,
  138. unsigned long align)
  139. {
  140. void *res;
  141. *mem = PTR_ALIGN(*mem, align);
  142. res = *mem;
  143. *mem += size;
  144. return res;
  145. }
  146. static void populate_properties(const void *blob,
  147. int offset,
  148. void **mem,
  149. struct device_node *np,
  150. const char *nodename,
  151. bool dryrun)
  152. {
  153. struct property *pp, **pprev = NULL;
  154. int cur;
  155. bool has_name = false;
  156. pprev = &np->properties;
  157. for (cur = fdt_first_property_offset(blob, offset);
  158. cur >= 0;
  159. cur = fdt_next_property_offset(blob, cur)) {
  160. const __be32 *val;
  161. const char *pname;
  162. u32 sz;
  163. val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
  164. if (!val) {
  165. pr_warn("Cannot locate property at 0x%x\n", cur);
  166. continue;
  167. }
  168. if (!pname) {
  169. pr_warn("Cannot find property name at 0x%x\n", cur);
  170. continue;
  171. }
  172. if (!strcmp(pname, "name"))
  173. has_name = true;
  174. pp = unflatten_dt_alloc(mem, sizeof(struct property),
  175. __alignof__(struct property));
  176. if (dryrun)
  177. continue;
  178. /* We accept flattened tree phandles either in
  179. * ePAPR-style "phandle" properties, or the
  180. * legacy "linux,phandle" properties. If both
  181. * appear and have different values, things
  182. * will get weird. Don't do that.
  183. */
  184. if (!strcmp(pname, "phandle") ||
  185. !strcmp(pname, "linux,phandle")) {
  186. if (!np->phandle)
  187. np->phandle = be32_to_cpup(val);
  188. }
  189. /* And we process the "ibm,phandle" property
  190. * used in pSeries dynamic device tree
  191. * stuff
  192. */
  193. if (!strcmp(pname, "ibm,phandle"))
  194. np->phandle = be32_to_cpup(val);
  195. pp->name = (char *)pname;
  196. pp->length = sz;
  197. pp->value = (__be32 *)val;
  198. *pprev = pp;
  199. pprev = &pp->next;
  200. }
  201. /* With version 0x10 we may not have the name property,
  202. * recreate it here from the unit name if absent
  203. */
  204. if (!has_name) {
  205. const char *p = nodename, *ps = p, *pa = NULL;
  206. int len;
  207. while (*p) {
  208. if ((*p) == '@')
  209. pa = p;
  210. else if ((*p) == '/')
  211. ps = p + 1;
  212. p++;
  213. }
  214. if (pa < ps)
  215. pa = p;
  216. len = (pa - ps) + 1;
  217. pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
  218. __alignof__(struct property));
  219. if (!dryrun) {
  220. pp->name = "name";
  221. pp->length = len;
  222. pp->value = pp + 1;
  223. *pprev = pp;
  224. pprev = &pp->next;
  225. memcpy(pp->value, ps, len - 1);
  226. ((char *)pp->value)[len - 1] = 0;
  227. pr_debug("fixed up name for %s -> %s\n",
  228. nodename, (char *)pp->value);
  229. }
  230. }
  231. if (!dryrun)
  232. *pprev = NULL;
  233. }
  234. static unsigned int populate_node(const void *blob,
  235. int offset,
  236. void **mem,
  237. struct device_node *dad,
  238. unsigned int fpsize,
  239. struct device_node **pnp,
  240. bool dryrun)
  241. {
  242. struct device_node *np;
  243. const char *pathp;
  244. unsigned int l, allocl;
  245. int new_format = 0;
  246. pathp = fdt_get_name(blob, offset, &l);
  247. if (!pathp) {
  248. *pnp = NULL;
  249. return 0;
  250. }
  251. allocl = ++l;
  252. /* version 0x10 has a more compact unit name here instead of the full
  253. * path. we accumulate the full path size using "fpsize", we'll rebuild
  254. * it later. We detect this because the first character of the name is
  255. * not '/'.
  256. */
  257. if ((*pathp) != '/') {
  258. new_format = 1;
  259. if (fpsize == 0) {
  260. /* root node: special case. fpsize accounts for path
  261. * plus terminating zero. root node only has '/', so
  262. * fpsize should be 2, but we want to avoid the first
  263. * level nodes to have two '/' so we use fpsize 1 here
  264. */
  265. fpsize = 1;
  266. allocl = 2;
  267. l = 1;
  268. pathp = "";
  269. } else {
  270. /* account for '/' and path size minus terminal 0
  271. * already in 'l'
  272. */
  273. fpsize += l;
  274. allocl = fpsize;
  275. }
  276. }
  277. np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
  278. __alignof__(struct device_node));
  279. if (!dryrun) {
  280. char *fn;
  281. of_node_init(np);
  282. np->full_name = fn = ((char *)np) + sizeof(*np);
  283. if (new_format) {
  284. /* rebuild full path for new format */
  285. if (dad && dad->parent) {
  286. strcpy(fn, dad->full_name);
  287. #ifdef DEBUG
  288. if ((strlen(fn) + l + 1) != allocl) {
  289. pr_debug("%s: p: %d, l: %d, a: %d\n",
  290. pathp, (int)strlen(fn),
  291. l, allocl);
  292. }
  293. #endif
  294. fn += strlen(fn);
  295. }
  296. *(fn++) = '/';
  297. }
  298. memcpy(fn, pathp, l);
  299. if (dad != NULL) {
  300. np->parent = dad;
  301. np->sibling = dad->child;
  302. dad->child = np;
  303. }
  304. }
  305. populate_properties(blob, offset, mem, np, pathp, dryrun);
  306. if (!dryrun) {
  307. np->name = of_get_property(np, "name", NULL);
  308. np->type = of_get_property(np, "device_type", NULL);
  309. if (!np->name)
  310. np->name = "<NULL>";
  311. if (!np->type)
  312. np->type = "<NULL>";
  313. }
  314. *pnp = np;
  315. return fpsize;
  316. }
  317. static void reverse_nodes(struct device_node *parent)
  318. {
  319. struct device_node *child, *next;
  320. /* In-depth first */
  321. child = parent->child;
  322. while (child) {
  323. reverse_nodes(child);
  324. child = child->sibling;
  325. }
  326. /* Reverse the nodes in the child list */
  327. child = parent->child;
  328. parent->child = NULL;
  329. while (child) {
  330. next = child->sibling;
  331. child->sibling = parent->child;
  332. parent->child = child;
  333. child = next;
  334. }
  335. }
  336. /**
  337. * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
  338. * @blob: The parent device tree blob
  339. * @mem: Memory chunk to use for allocating device nodes and properties
  340. * @dad: Parent struct device_node
  341. * @nodepp: The device_node tree created by the call
  342. *
  343. * It returns the size of unflattened device tree or error code
  344. */
  345. static int unflatten_dt_nodes(const void *blob,
  346. void *mem,
  347. struct device_node *dad,
  348. struct device_node **nodepp)
  349. {
  350. struct device_node *root;
  351. int offset = 0, depth = 0, initial_depth = 0;
  352. #define FDT_MAX_DEPTH 64
  353. unsigned int fpsizes[FDT_MAX_DEPTH];
  354. struct device_node *nps[FDT_MAX_DEPTH];
  355. void *base = mem;
  356. bool dryrun = !base;
  357. if (nodepp)
  358. *nodepp = NULL;
  359. /*
  360. * We're unflattening device sub-tree if @dad is valid. There are
  361. * possibly multiple nodes in the first level of depth. We need
  362. * set @depth to 1 to make fdt_next_node() happy as it bails
  363. * immediately when negative @depth is found. Otherwise, the device
  364. * nodes except the first one won't be unflattened successfully.
  365. */
  366. if (dad)
  367. depth = initial_depth = 1;
  368. root = dad;
  369. fpsizes[depth] = dad ? strlen(of_node_full_name(dad)) : 0;
  370. nps[depth] = dad;
  371. for (offset = 0;
  372. offset >= 0 && depth >= initial_depth;
  373. offset = fdt_next_node(blob, offset, &depth)) {
  374. if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
  375. continue;
  376. fpsizes[depth+1] = populate_node(blob, offset, &mem,
  377. nps[depth],
  378. fpsizes[depth],
  379. &nps[depth+1], dryrun);
  380. if (!fpsizes[depth+1])
  381. return mem - base;
  382. if (!dryrun && nodepp && !*nodepp)
  383. *nodepp = nps[depth+1];
  384. if (!dryrun && !root)
  385. root = nps[depth+1];
  386. }
  387. if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
  388. pr_err("Error %d processing FDT\n", offset);
  389. return -EINVAL;
  390. }
  391. /*
  392. * Reverse the child list. Some drivers assumes node order matches .dts
  393. * node order
  394. */
  395. if (!dryrun)
  396. reverse_nodes(root);
  397. return mem - base;
  398. }
  399. /**
  400. * __unflatten_device_tree - create tree of device_nodes from flat blob
  401. *
  402. * unflattens a device-tree, creating the
  403. * tree of struct device_node. It also fills the "name" and "type"
  404. * pointers of the nodes so the normal device-tree walking functions
  405. * can be used.
  406. * @blob: The blob to expand
  407. * @dad: Parent device node
  408. * @mynodes: The device_node tree created by the call
  409. * @dt_alloc: An allocator that provides a virtual address to memory
  410. * for the resulting tree
  411. *
  412. * Returns NULL on failure or the memory chunk containing the unflattened
  413. * device tree on success.
  414. */
  415. void *__unflatten_device_tree(const void *blob,
  416. struct device_node *dad,
  417. struct device_node **mynodes,
  418. void *(*dt_alloc)(u64 size, u64 align),
  419. bool detached)
  420. {
  421. int size;
  422. void *mem;
  423. pr_debug(" -> unflatten_device_tree()\n");
  424. if (!blob) {
  425. pr_debug("No device tree pointer\n");
  426. return NULL;
  427. }
  428. pr_debug("Unflattening device tree:\n");
  429. pr_debug("magic: %08x\n", fdt_magic(blob));
  430. pr_debug("size: %08x\n", fdt_totalsize(blob));
  431. pr_debug("version: %08x\n", fdt_version(blob));
  432. if (fdt_check_header(blob)) {
  433. pr_err("Invalid device tree blob header\n");
  434. return NULL;
  435. }
  436. /* First pass, scan for size */
  437. size = unflatten_dt_nodes(blob, NULL, dad, NULL);
  438. if (size < 0)
  439. return NULL;
  440. size = ALIGN(size, 4);
  441. pr_debug(" size is %d, allocating...\n", size);
  442. /* Allocate memory for the expanded device tree */
  443. mem = dt_alloc(size + 4, __alignof__(struct device_node));
  444. if (!mem)
  445. return NULL;
  446. memset(mem, 0, size);
  447. *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
  448. pr_debug(" unflattening %p...\n", mem);
  449. /* Second pass, do actual unflattening */
  450. unflatten_dt_nodes(blob, mem, dad, mynodes);
  451. if (be32_to_cpup(mem + size) != 0xdeadbeef)
  452. pr_warning("End of tree marker overwritten: %08x\n",
  453. be32_to_cpup(mem + size));
  454. if (detached && mynodes) {
  455. of_node_set_flag(*mynodes, OF_DETACHED);
  456. pr_debug("unflattened tree is detached\n");
  457. }
  458. pr_debug(" <- unflatten_device_tree()\n");
  459. return mem;
  460. }
  461. static void *kernel_tree_alloc(u64 size, u64 align)
  462. {
  463. return kzalloc(size, GFP_KERNEL);
  464. }
  465. static DEFINE_MUTEX(of_fdt_unflatten_mutex);
  466. /**
  467. * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
  468. * @blob: Flat device tree blob
  469. * @dad: Parent device node
  470. * @mynodes: The device tree created by the call
  471. *
  472. * unflattens the device-tree passed by the firmware, creating the
  473. * tree of struct device_node. It also fills the "name" and "type"
  474. * pointers of the nodes so the normal device-tree walking functions
  475. * can be used.
  476. *
  477. * Returns NULL on failure or the memory chunk containing the unflattened
  478. * device tree on success.
  479. */
  480. void *of_fdt_unflatten_tree(const unsigned long *blob,
  481. struct device_node *dad,
  482. struct device_node **mynodes)
  483. {
  484. void *mem;
  485. mutex_lock(&of_fdt_unflatten_mutex);
  486. mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
  487. true);
  488. mutex_unlock(&of_fdt_unflatten_mutex);
  489. return mem;
  490. }
  491. EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
  492. /* Everything below here references initial_boot_params directly. */
  493. int __initdata dt_root_addr_cells;
  494. int __initdata dt_root_size_cells;
  495. void *initial_boot_params;
  496. #ifdef CONFIG_OF_EARLY_FLATTREE
  497. static u32 of_fdt_crc32;
  498. /**
  499. * res_mem_reserve_reg() - reserve all memory described in 'reg' property
  500. */
  501. static int __init __reserved_mem_reserve_reg(unsigned long node,
  502. const char *uname)
  503. {
  504. int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
  505. phys_addr_t base, size;
  506. int len;
  507. const __be32 *prop;
  508. int nomap, first = 1;
  509. prop = of_get_flat_dt_prop(node, "reg", &len);
  510. if (!prop)
  511. return -ENOENT;
  512. if (len && len % t_len != 0) {
  513. pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
  514. uname);
  515. return -EINVAL;
  516. }
  517. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  518. while (len >= t_len) {
  519. base = dt_mem_next_cell(dt_root_addr_cells, &prop);
  520. size = dt_mem_next_cell(dt_root_size_cells, &prop);
  521. if (size &&
  522. early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
  523. pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
  524. uname, &base, (unsigned long)(size / SZ_1M));
  525. else
  526. pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
  527. uname, &base, (unsigned long)(size / SZ_1M));
  528. len -= t_len;
  529. if (first) {
  530. fdt_reserved_mem_save_node(node, uname, base, size);
  531. first = 0;
  532. }
  533. }
  534. return 0;
  535. }
  536. /**
  537. * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
  538. * in /reserved-memory matches the values supported by the current implementation,
  539. * also check if ranges property has been provided
  540. */
  541. static int __init __reserved_mem_check_root(unsigned long node)
  542. {
  543. const __be32 *prop;
  544. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  545. if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
  546. return -EINVAL;
  547. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  548. if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
  549. return -EINVAL;
  550. prop = of_get_flat_dt_prop(node, "ranges", NULL);
  551. if (!prop)
  552. return -EINVAL;
  553. return 0;
  554. }
  555. /**
  556. * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
  557. */
  558. static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
  559. int depth, void *data)
  560. {
  561. static int found;
  562. const char *status;
  563. int err;
  564. if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
  565. if (__reserved_mem_check_root(node) != 0) {
  566. pr_err("Reserved memory: unsupported node format, ignoring\n");
  567. /* break scan */
  568. return 1;
  569. }
  570. found = 1;
  571. /* scan next node */
  572. return 0;
  573. } else if (!found) {
  574. /* scan next node */
  575. return 0;
  576. } else if (found && depth < 2) {
  577. /* scanning of /reserved-memory has been finished */
  578. return 1;
  579. }
  580. status = of_get_flat_dt_prop(node, "status", NULL);
  581. if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
  582. return 0;
  583. err = __reserved_mem_reserve_reg(node, uname);
  584. if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
  585. fdt_reserved_mem_save_node(node, uname, 0, 0);
  586. /* scan next node */
  587. return 0;
  588. }
  589. /**
  590. * early_init_fdt_scan_reserved_mem() - create reserved memory regions
  591. *
  592. * This function grabs memory from early allocator for device exclusive use
  593. * defined in device tree structures. It should be called by arch specific code
  594. * once the early allocator (i.e. memblock) has been fully activated.
  595. */
  596. void __init early_init_fdt_scan_reserved_mem(void)
  597. {
  598. int n;
  599. u64 base, size;
  600. if (!initial_boot_params)
  601. return;
  602. /* Process header /memreserve/ fields */
  603. for (n = 0; ; n++) {
  604. fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
  605. if (!size)
  606. break;
  607. early_init_dt_reserve_memory_arch(base, size, 0);
  608. }
  609. of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
  610. fdt_init_reserved_mem();
  611. }
  612. /**
  613. * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
  614. */
  615. void __init early_init_fdt_reserve_self(void)
  616. {
  617. if (!initial_boot_params)
  618. return;
  619. /* Reserve the dtb region */
  620. early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
  621. fdt_totalsize(initial_boot_params),
  622. 0);
  623. }
  624. /**
  625. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  626. * @it: callback function
  627. * @data: context data pointer
  628. *
  629. * This function is used to scan the flattened device-tree, it is
  630. * used to extract the memory information at boot before we can
  631. * unflatten the tree
  632. */
  633. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  634. const char *uname, int depth,
  635. void *data),
  636. void *data)
  637. {
  638. const void *blob = initial_boot_params;
  639. const char *pathp;
  640. int offset, rc = 0, depth = -1;
  641. if (!blob)
  642. return 0;
  643. for (offset = fdt_next_node(blob, -1, &depth);
  644. offset >= 0 && depth >= 0 && !rc;
  645. offset = fdt_next_node(blob, offset, &depth)) {
  646. pathp = fdt_get_name(blob, offset, NULL);
  647. if (*pathp == '/')
  648. pathp = kbasename(pathp);
  649. rc = it(offset, pathp, depth, data);
  650. }
  651. return rc;
  652. }
  653. /**
  654. * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
  655. * @it: callback function
  656. * @data: context data pointer
  657. *
  658. * This function is used to scan sub-nodes of a node.
  659. */
  660. int __init of_scan_flat_dt_subnodes(unsigned long parent,
  661. int (*it)(unsigned long node,
  662. const char *uname,
  663. void *data),
  664. void *data)
  665. {
  666. const void *blob = initial_boot_params;
  667. int node;
  668. fdt_for_each_subnode(node, blob, parent) {
  669. const char *pathp;
  670. int rc;
  671. pathp = fdt_get_name(blob, node, NULL);
  672. if (*pathp == '/')
  673. pathp = kbasename(pathp);
  674. rc = it(node, pathp, data);
  675. if (rc)
  676. return rc;
  677. }
  678. return 0;
  679. }
  680. /**
  681. * of_get_flat_dt_subnode_by_name - get the subnode by given name
  682. *
  683. * @node: the parent node
  684. * @uname: the name of subnode
  685. * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
  686. */
  687. int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
  688. {
  689. return fdt_subnode_offset(initial_boot_params, node, uname);
  690. }
  691. /**
  692. * of_get_flat_dt_root - find the root node in the flat blob
  693. */
  694. unsigned long __init of_get_flat_dt_root(void)
  695. {
  696. return 0;
  697. }
  698. /**
  699. * of_get_flat_dt_size - Return the total size of the FDT
  700. */
  701. int __init of_get_flat_dt_size(void)
  702. {
  703. return fdt_totalsize(initial_boot_params);
  704. }
  705. /**
  706. * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
  707. *
  708. * This function can be used within scan_flattened_dt callback to get
  709. * access to properties
  710. */
  711. const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  712. int *size)
  713. {
  714. return fdt_getprop(initial_boot_params, node, name, size);
  715. }
  716. /**
  717. * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
  718. * @node: node to test
  719. * @compat: compatible string to compare with compatible list.
  720. */
  721. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  722. {
  723. return of_fdt_is_compatible(initial_boot_params, node, compat);
  724. }
  725. /**
  726. * of_flat_dt_match - Return true if node matches a list of compatible values
  727. */
  728. int __init of_flat_dt_match(unsigned long node, const char *const *compat)
  729. {
  730. return of_fdt_match(initial_boot_params, node, compat);
  731. }
  732. /**
  733. * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
  734. */
  735. uint32_t __init of_get_flat_dt_phandle(unsigned long node)
  736. {
  737. return fdt_get_phandle(initial_boot_params, node);
  738. }
  739. struct fdt_scan_status {
  740. const char *name;
  741. int namelen;
  742. int depth;
  743. int found;
  744. int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
  745. void *data;
  746. };
  747. const char * __init of_flat_dt_get_machine_name(void)
  748. {
  749. const char *name;
  750. unsigned long dt_root = of_get_flat_dt_root();
  751. name = of_get_flat_dt_prop(dt_root, "model", NULL);
  752. if (!name)
  753. name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  754. return name;
  755. }
  756. /**
  757. * of_flat_dt_match_machine - Iterate match tables to find matching machine.
  758. *
  759. * @default_match: A machine specific ptr to return in case of no match.
  760. * @get_next_compat: callback function to return next compatible match table.
  761. *
  762. * Iterate through machine match tables to find the best match for the machine
  763. * compatible string in the FDT.
  764. */
  765. const void * __init of_flat_dt_match_machine(const void *default_match,
  766. const void * (*get_next_compat)(const char * const**))
  767. {
  768. const void *data = NULL;
  769. const void *best_data = default_match;
  770. const char *const *compat;
  771. unsigned long dt_root;
  772. unsigned int best_score = ~1, score = 0;
  773. dt_root = of_get_flat_dt_root();
  774. while ((data = get_next_compat(&compat))) {
  775. score = of_flat_dt_match(dt_root, compat);
  776. if (score > 0 && score < best_score) {
  777. best_data = data;
  778. best_score = score;
  779. }
  780. }
  781. if (!best_data) {
  782. const char *prop;
  783. int size;
  784. pr_err("\n unrecognized device tree list:\n[ ");
  785. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  786. if (prop) {
  787. while (size > 0) {
  788. printk("'%s' ", prop);
  789. size -= strlen(prop) + 1;
  790. prop += strlen(prop) + 1;
  791. }
  792. }
  793. printk("]\n\n");
  794. return NULL;
  795. }
  796. pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
  797. return best_data;
  798. }
  799. #ifdef CONFIG_BLK_DEV_INITRD
  800. #ifndef __early_init_dt_declare_initrd
  801. static void __early_init_dt_declare_initrd(unsigned long start,
  802. unsigned long end)
  803. {
  804. initrd_start = (unsigned long)__va(start);
  805. initrd_end = (unsigned long)__va(end);
  806. initrd_below_start_ok = 1;
  807. }
  808. #endif
  809. /**
  810. * early_init_dt_check_for_initrd - Decode initrd location from flat tree
  811. * @node: reference to node containing initrd location ('chosen')
  812. */
  813. static void __init early_init_dt_check_for_initrd(unsigned long node)
  814. {
  815. u64 start, end;
  816. int len;
  817. const __be32 *prop;
  818. pr_debug("Looking for initrd properties... ");
  819. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
  820. if (!prop)
  821. return;
  822. start = of_read_number(prop, len/4);
  823. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
  824. if (!prop)
  825. return;
  826. end = of_read_number(prop, len/4);
  827. __early_init_dt_declare_initrd(start, end);
  828. pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
  829. (unsigned long long)start, (unsigned long long)end);
  830. }
  831. #else
  832. static inline void early_init_dt_check_for_initrd(unsigned long node)
  833. {
  834. }
  835. #endif /* CONFIG_BLK_DEV_INITRD */
  836. #ifdef CONFIG_SERIAL_EARLYCON
  837. int __init early_init_dt_scan_chosen_stdout(void)
  838. {
  839. int offset;
  840. const char *p, *q, *options = NULL;
  841. int l;
  842. const struct earlycon_id **p_match;
  843. const void *fdt = initial_boot_params;
  844. offset = fdt_path_offset(fdt, "/chosen");
  845. if (offset < 0)
  846. offset = fdt_path_offset(fdt, "/chosen@0");
  847. if (offset < 0)
  848. return -ENOENT;
  849. p = fdt_getprop(fdt, offset, "stdout-path", &l);
  850. if (!p)
  851. p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
  852. if (!p || !l)
  853. return -ENOENT;
  854. q = strchrnul(p, ':');
  855. if (*q != '\0')
  856. options = q + 1;
  857. l = q - p;
  858. /* Get the node specified by stdout-path */
  859. offset = fdt_path_offset_namelen(fdt, p, l);
  860. if (offset < 0) {
  861. pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
  862. return 0;
  863. }
  864. for (p_match = __earlycon_table; p_match < __earlycon_table_end;
  865. p_match++) {
  866. const struct earlycon_id *match = *p_match;
  867. if (!match->compatible[0])
  868. continue;
  869. if (fdt_node_check_compatible(fdt, offset, match->compatible))
  870. continue;
  871. of_setup_earlycon(match, offset, options);
  872. return 0;
  873. }
  874. return -ENODEV;
  875. }
  876. #endif
  877. /**
  878. * early_init_dt_scan_root - fetch the top level address and size cells
  879. */
  880. int __init early_init_dt_scan_root(unsigned long node, const char *uname,
  881. int depth, void *data)
  882. {
  883. const __be32 *prop;
  884. if (depth != 0)
  885. return 0;
  886. dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  887. dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  888. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  889. if (prop)
  890. dt_root_size_cells = be32_to_cpup(prop);
  891. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  892. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  893. if (prop)
  894. dt_root_addr_cells = be32_to_cpup(prop);
  895. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  896. /* break now */
  897. return 1;
  898. }
  899. u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
  900. {
  901. const __be32 *p = *cellp;
  902. *cellp = p + s;
  903. return of_read_number(p, s);
  904. }
  905. /**
  906. * early_init_dt_scan_memory - Look for and parse memory nodes
  907. */
  908. int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
  909. int depth, void *data)
  910. {
  911. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  912. const __be32 *reg, *endp;
  913. int l;
  914. bool hotpluggable;
  915. /* We are scanning "memory" nodes only */
  916. if (type == NULL) {
  917. /*
  918. * The longtrail doesn't have a device_type on the
  919. * /memory node, so look for the node called /memory@0.
  920. */
  921. if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
  922. return 0;
  923. } else if (strcmp(type, "memory") != 0)
  924. return 0;
  925. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  926. if (reg == NULL)
  927. reg = of_get_flat_dt_prop(node, "reg", &l);
  928. if (reg == NULL)
  929. return 0;
  930. endp = reg + (l / sizeof(__be32));
  931. hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
  932. pr_debug("memory scan node %s, reg size %d,\n", uname, l);
  933. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  934. u64 base, size;
  935. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  936. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  937. if (size == 0)
  938. continue;
  939. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  940. (unsigned long long)size);
  941. early_init_dt_add_memory_arch(base, size);
  942. if (!hotpluggable)
  943. continue;
  944. if (early_init_dt_mark_hotplug_memory_arch(base, size))
  945. pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
  946. base, base + size);
  947. }
  948. return 0;
  949. }
  950. /*
  951. * Convert configs to something easy to use in C code
  952. */
  953. #if defined(CONFIG_CMDLINE_FORCE)
  954. static const int overwrite_incoming_cmdline = 1;
  955. static const int read_dt_cmdline;
  956. static const int concat_cmdline;
  957. #elif defined(CONFIG_CMDLINE_EXTEND)
  958. static const int overwrite_incoming_cmdline;
  959. static const int read_dt_cmdline = 1;
  960. static const int concat_cmdline = 1;
  961. #else /* CMDLINE_FROM_BOOTLOADER */
  962. static const int overwrite_incoming_cmdline;
  963. static const int read_dt_cmdline = 1;
  964. static const int concat_cmdline;
  965. #endif
  966. #ifdef CONFIG_CMDLINE
  967. static const char *config_cmdline = CONFIG_CMDLINE;
  968. #else
  969. static const char *config_cmdline = "";
  970. #endif
  971. int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
  972. int depth, void *data)
  973. {
  974. int l = 0;
  975. const char *p = NULL;
  976. char *cmdline = data;
  977. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  978. if (depth != 1 || !cmdline ||
  979. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  980. return 0;
  981. early_init_dt_check_for_initrd(node);
  982. /* Put CONFIG_CMDLINE in if forced or if data had nothing in it to start */
  983. if (overwrite_incoming_cmdline || !cmdline[0])
  984. strlcpy(cmdline, config_cmdline, COMMAND_LINE_SIZE);
  985. /* Retrieve command line unless forcing */
  986. if (read_dt_cmdline)
  987. p = of_get_flat_dt_prop(node, "bootargs", &l);
  988. if (p != NULL && l > 0) {
  989. if (concat_cmdline) {
  990. int cmdline_len;
  991. int copy_len;
  992. strlcat(cmdline, " ", COMMAND_LINE_SIZE);
  993. cmdline_len = strlen(cmdline);
  994. copy_len = COMMAND_LINE_SIZE - cmdline_len - 1;
  995. copy_len = min((int)l, copy_len);
  996. strncpy(cmdline + cmdline_len, p, copy_len);
  997. cmdline[cmdline_len + copy_len] = '\0';
  998. } else {
  999. strlcpy(cmdline, p, min((int)l, COMMAND_LINE_SIZE));
  1000. }
  1001. }
  1002. pr_debug("Command line is: %s\n", (char*)data);
  1003. /* break now */
  1004. return 1;
  1005. }
  1006. #ifdef CONFIG_HAVE_MEMBLOCK
  1007. #ifndef MIN_MEMBLOCK_ADDR
  1008. #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
  1009. #endif
  1010. #ifndef MAX_MEMBLOCK_ADDR
  1011. #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
  1012. #endif
  1013. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  1014. {
  1015. const u64 phys_offset = MIN_MEMBLOCK_ADDR;
  1016. if (!PAGE_ALIGNED(base)) {
  1017. if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
  1018. pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
  1019. base, base + size);
  1020. return;
  1021. }
  1022. size -= PAGE_SIZE - (base & ~PAGE_MASK);
  1023. base = PAGE_ALIGN(base);
  1024. }
  1025. size &= PAGE_MASK;
  1026. if (base > MAX_MEMBLOCK_ADDR) {
  1027. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  1028. base, base + size);
  1029. return;
  1030. }
  1031. if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
  1032. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  1033. ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
  1034. size = MAX_MEMBLOCK_ADDR - base + 1;
  1035. }
  1036. if (base + size < phys_offset) {
  1037. pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
  1038. base, base + size);
  1039. return;
  1040. }
  1041. if (base < phys_offset) {
  1042. pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
  1043. base, phys_offset);
  1044. size -= phys_offset - base;
  1045. base = phys_offset;
  1046. }
  1047. memblock_add(base, size);
  1048. }
  1049. int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
  1050. {
  1051. return memblock_mark_hotplug(base, size);
  1052. }
  1053. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  1054. phys_addr_t size, bool nomap)
  1055. {
  1056. if (nomap)
  1057. return memblock_remove(base, size);
  1058. return memblock_reserve(base, size);
  1059. }
  1060. /*
  1061. * called from unflatten_device_tree() to bootstrap devicetree itself
  1062. * Architectures can override this definition if memblock isn't used
  1063. */
  1064. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  1065. {
  1066. return __va(memblock_alloc(size, align));
  1067. }
  1068. #else
  1069. void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
  1070. {
  1071. WARN_ON(1);
  1072. }
  1073. int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
  1074. {
  1075. return -ENOSYS;
  1076. }
  1077. int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
  1078. phys_addr_t size, bool nomap)
  1079. {
  1080. pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
  1081. &base, &size, nomap ? " (nomap)" : "");
  1082. return -ENOSYS;
  1083. }
  1084. void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
  1085. {
  1086. WARN_ON(1);
  1087. return NULL;
  1088. }
  1089. #endif
  1090. bool __init early_init_dt_verify(void *params)
  1091. {
  1092. if (!params)
  1093. return false;
  1094. /* check device tree validity */
  1095. if (fdt_check_header(params))
  1096. return false;
  1097. /* Setup flat device-tree pointer */
  1098. initial_boot_params = params;
  1099. of_fdt_crc32 = crc32_be(~0, initial_boot_params,
  1100. fdt_totalsize(initial_boot_params));
  1101. return true;
  1102. }
  1103. void __init early_init_dt_scan_nodes(void)
  1104. {
  1105. /* Retrieve various information from the /chosen node */
  1106. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  1107. /* Initialize {size,address}-cells info */
  1108. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1109. /* Setup memory, calling early_init_dt_add_memory_arch */
  1110. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1111. }
  1112. bool __init early_init_dt_scan(void *params)
  1113. {
  1114. bool status;
  1115. status = early_init_dt_verify(params);
  1116. if (!status)
  1117. return false;
  1118. early_init_dt_scan_nodes();
  1119. return true;
  1120. }
  1121. /**
  1122. * unflatten_device_tree - create tree of device_nodes from flat blob
  1123. *
  1124. * unflattens the device-tree passed by the firmware, creating the
  1125. * tree of struct device_node. It also fills the "name" and "type"
  1126. * pointers of the nodes so the normal device-tree walking functions
  1127. * can be used.
  1128. */
  1129. void __init unflatten_device_tree(void)
  1130. {
  1131. __unflatten_device_tree(initial_boot_params, NULL, &of_root,
  1132. early_init_dt_alloc_memory_arch, false);
  1133. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  1134. of_alias_scan(early_init_dt_alloc_memory_arch);
  1135. unittest_unflatten_overlay_base();
  1136. }
  1137. /**
  1138. * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
  1139. *
  1140. * Copies and unflattens the device-tree passed by the firmware, creating the
  1141. * tree of struct device_node. It also fills the "name" and "type"
  1142. * pointers of the nodes so the normal device-tree walking functions
  1143. * can be used. This should only be used when the FDT memory has not been
  1144. * reserved such is the case when the FDT is built-in to the kernel init
  1145. * section. If the FDT memory is reserved already then unflatten_device_tree
  1146. * should be used instead.
  1147. */
  1148. void __init unflatten_and_copy_device_tree(void)
  1149. {
  1150. int size;
  1151. void *dt;
  1152. if (!initial_boot_params) {
  1153. pr_warn("No valid device tree found, continuing without\n");
  1154. return;
  1155. }
  1156. size = fdt_totalsize(initial_boot_params);
  1157. dt = early_init_dt_alloc_memory_arch(size,
  1158. roundup_pow_of_two(FDT_V17_SIZE));
  1159. if (dt) {
  1160. memcpy(dt, initial_boot_params, size);
  1161. initial_boot_params = dt;
  1162. }
  1163. unflatten_device_tree();
  1164. }
  1165. #ifdef CONFIG_SYSFS
  1166. static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
  1167. struct bin_attribute *bin_attr,
  1168. char *buf, loff_t off, size_t count)
  1169. {
  1170. memcpy(buf, initial_boot_params + off, count);
  1171. return count;
  1172. }
  1173. static int __init of_fdt_raw_init(void)
  1174. {
  1175. static struct bin_attribute of_fdt_raw_attr =
  1176. __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
  1177. if (!initial_boot_params)
  1178. return 0;
  1179. if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
  1180. fdt_totalsize(initial_boot_params))) {
  1181. pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
  1182. return 0;
  1183. }
  1184. of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
  1185. return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
  1186. }
  1187. late_initcall(of_fdt_raw_init);
  1188. #endif
  1189. #endif /* CONFIG_OF_EARLY_FLATTREE */