2 * Functions for working with the Flattened Device Tree data format
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
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.
12 #include <linux/crc32.h>
13 #include <linux/kernel.h>
14 #include <linux/initrd.h>
15 #include <linux/memblock.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_reserved_mem.h>
19 #include <linux/sizes.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/libfdt.h>
24 #include <linux/debugfs.h>
25 #include <linux/serial_core.h>
26 #include <linux/sysfs.h>
28 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
32 * of_fdt_limit_memory - limit the number of regions in the /memory node
33 * @limit: maximum entries
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.
39 void of_fdt_limit_memory(int limit)
44 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
45 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
46 const uint32_t *addr_prop;
47 const uint32_t *size_prop;
51 root_offset = fdt_path_offset(initial_boot_params, "/");
55 addr_prop = fdt_getprop(initial_boot_params, root_offset,
56 "#address-cells", NULL);
58 nr_address_cells = fdt32_to_cpu(*addr_prop);
60 size_prop = fdt_getprop(initial_boot_params, root_offset,
63 nr_size_cells = fdt32_to_cpu(*size_prop);
65 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
67 memory = fdt_path_offset(initial_boot_params, "/memory");
69 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
70 if (len > limit*cell_size) {
71 len = limit*cell_size;
72 pr_debug("Limiting number of entries to %d\n", limit);
73 fdt_setprop(initial_boot_params, memory, "reg", val,
80 * of_fdt_is_compatible - Return true if given node from the given blob has
81 * compat in its compatible list
82 * @blob: A device tree blob
84 * @compat: compatible string to compare with compatible list.
86 * On match, returns a non-zero value with smaller values returned for more
87 * specific compatible values.
89 int of_fdt_is_compatible(const void *blob,
90 unsigned long node, const char *compat)
94 unsigned long l, score = 0;
96 cp = fdt_getprop(blob, node, "compatible", &cplen);
101 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
112 * of_fdt_match - Return true if node matches a list of compatible values
114 int of_fdt_match(const void *blob, unsigned long node,
115 const char *const *compat)
117 unsigned int tmp, score = 0;
123 tmp = of_fdt_is_compatible(blob, node, *compat);
124 if (tmp && (score == 0 || (tmp < score)))
132 static void *unflatten_dt_alloc(void **mem, unsigned long size,
137 *mem = PTR_ALIGN(*mem, align);
145 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
146 * @blob: The parent device tree blob
147 * @mem: Memory chunk to use for allocating device nodes and properties
148 * @p: pointer to node in flat tree
149 * @dad: Parent struct device_node
150 * @fpsize: Size of the node path up at the current depth.
152 static void * unflatten_dt_node(void *blob,
155 struct device_node *dad,
156 struct device_node **nodepp,
157 unsigned long fpsize,
161 struct device_node *np;
162 struct property *pp, **prev_pp = NULL;
164 unsigned int l, allocl;
165 static int depth = 0;
171 pathp = fdt_get_name(blob, *poffset, &l);
177 /* version 0x10 has a more compact unit name here instead of the full
178 * path. we accumulate the full path size using "fpsize", we'll rebuild
179 * it later. We detect this because the first character of the name is
182 if ((*pathp) != '/') {
185 /* root node: special case. fpsize accounts for path
186 * plus terminating zero. root node only has '/', so
187 * fpsize should be 2, but we want to avoid the first
188 * level nodes to have two '/' so we use fpsize 1 here
195 /* account for '/' and path size minus terminal 0
203 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
204 __alignof__(struct device_node));
208 np->full_name = fn = ((char *)np) + sizeof(*np);
210 /* rebuild full path for new format */
211 if (dad && dad->parent) {
212 strcpy(fn, dad->full_name);
214 if ((strlen(fn) + l + 1) != allocl) {
215 pr_debug("%s: p: %d, l: %d, a: %d\n",
216 pathp, (int)strlen(fn),
224 memcpy(fn, pathp, l);
226 prev_pp = &np->properties;
229 np->sibling = dad->child;
233 /* process properties */
234 for (offset = fdt_first_property_offset(blob, *poffset);
236 (offset = fdt_next_property_offset(blob, offset))) {
240 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
241 offset = -FDT_ERR_INTERNAL;
246 pr_info("Can't find property name in list !\n");
249 if (strcmp(pname, "name") == 0)
251 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
252 __alignof__(struct property));
254 /* We accept flattened tree phandles either in
255 * ePAPR-style "phandle" properties, or the
256 * legacy "linux,phandle" properties. If both
257 * appear and have different values, things
258 * will get weird. Don't do that. */
259 if ((strcmp(pname, "phandle") == 0) ||
260 (strcmp(pname, "linux,phandle") == 0)) {
261 if (np->phandle == 0)
262 np->phandle = be32_to_cpup(p);
264 /* And we process the "ibm,phandle" property
265 * used in pSeries dynamic device tree
267 if (strcmp(pname, "ibm,phandle") == 0)
268 np->phandle = be32_to_cpup(p);
269 pp->name = (char *)pname;
271 pp->value = (__be32 *)p;
276 /* with version 0x10 we may not have the name property, recreate
277 * it here from the unit name if absent
280 const char *p1 = pathp, *ps = pathp, *pa = NULL;
293 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
294 __alignof__(struct property));
301 memcpy(pp->value, ps, sz - 1);
302 ((char *)pp->value)[sz - 1] = 0;
303 pr_debug("fixed up name for %s -> %s\n", pathp,
309 np->name = of_get_property(np, "name", NULL);
310 np->type = of_get_property(np, "device_type", NULL);
319 *poffset = fdt_next_node(blob, *poffset, &depth);
322 while (*poffset > 0 && depth > old_depth)
323 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
326 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
327 pr_err("unflatten: error %d processing FDT\n", *poffset);
330 * Reverse the child list. Some drivers assumes node order matches .dts
333 if (!dryrun && np->child) {
334 struct device_node *child = np->child;
337 struct device_node *next = child->sibling;
338 child->sibling = np->child;
351 * __unflatten_device_tree - create tree of device_nodes from flat blob
353 * unflattens a device-tree, creating the
354 * tree of struct device_node. It also fills the "name" and "type"
355 * pointers of the nodes so the normal device-tree walking functions
357 * @blob: The blob to expand
358 * @mynodes: The device_node tree created by the call
359 * @dt_alloc: An allocator that provides a virtual address to memory
360 * for the resulting tree
362 static void __unflatten_device_tree(void *blob,
363 struct device_node **mynodes,
364 void * (*dt_alloc)(u64 size, u64 align))
370 pr_debug(" -> unflatten_device_tree()\n");
373 pr_debug("No device tree pointer\n");
377 pr_debug("Unflattening device tree:\n");
378 pr_debug("magic: %08x\n", fdt_magic(blob));
379 pr_debug("size: %08x\n", fdt_totalsize(blob));
380 pr_debug("version: %08x\n", fdt_version(blob));
382 if (fdt_check_header(blob)) {
383 pr_err("Invalid device tree blob header\n");
387 /* First pass, scan for size */
389 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
390 size = ALIGN(size, 4);
392 pr_debug(" size is %lx, allocating...\n", size);
394 /* Allocate memory for the expanded device tree */
395 mem = dt_alloc(size + 4, __alignof__(struct device_node));
396 memset(mem, 0, size);
398 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
400 pr_debug(" unflattening %p...\n", mem);
402 /* Second pass, do actual unflattening */
404 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
405 if (be32_to_cpup(mem + size) != 0xdeadbeef)
406 pr_warning("End of tree marker overwritten: %08x\n",
407 be32_to_cpup(mem + size));
409 pr_debug(" <- unflatten_device_tree()\n");
412 static void *kernel_tree_alloc(u64 size, u64 align)
414 return kzalloc(size, GFP_KERNEL);
418 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
420 * unflattens the device-tree passed by the firmware, creating the
421 * tree of struct device_node. It also fills the "name" and "type"
422 * pointers of the nodes so the normal device-tree walking functions
425 void of_fdt_unflatten_tree(unsigned long *blob,
426 struct device_node **mynodes)
428 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
430 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
432 /* Everything below here references initial_boot_params directly. */
433 int __initdata dt_root_addr_cells;
434 int __initdata dt_root_size_cells;
436 void *initial_boot_params;
438 #ifdef CONFIG_OF_EARLY_FLATTREE
440 static u32 of_fdt_crc32;
443 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
445 static int __init __reserved_mem_reserve_reg(unsigned long node,
448 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
449 phys_addr_t base, size;
452 int nomap, first = 1;
454 prop = of_get_flat_dt_prop(node, "reg", &len);
458 if (len && len % t_len != 0) {
459 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
464 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
466 while (len >= t_len) {
467 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
468 size = dt_mem_next_cell(dt_root_size_cells, &prop);
471 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
472 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
473 uname, &base, (unsigned long)size / SZ_1M);
475 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
476 uname, &base, (unsigned long)size / SZ_1M);
480 fdt_reserved_mem_save_node(node, uname, base, size);
488 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
489 * in /reserved-memory matches the values supported by the current implementation,
490 * also check if ranges property has been provided
492 static int __init __reserved_mem_check_root(unsigned long node)
496 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
497 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
500 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
501 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
504 prop = of_get_flat_dt_prop(node, "ranges", NULL);
511 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
513 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
514 int depth, void *data)
520 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
521 if (__reserved_mem_check_root(node) != 0) {
522 pr_err("Reserved memory: unsupported node format, ignoring\n");
532 } else if (found && depth < 2) {
533 /* scanning of /reserved-memory has been finished */
537 status = of_get_flat_dt_prop(node, "status", NULL);
538 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
541 err = __reserved_mem_reserve_reg(node, uname);
542 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
543 fdt_reserved_mem_save_node(node, uname, 0, 0);
550 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
552 * This function grabs memory from early allocator for device exclusive use
553 * defined in device tree structures. It should be called by arch specific code
554 * once the early allocator (i.e. memblock) has been fully activated.
556 void __init early_init_fdt_scan_reserved_mem(void)
561 if (!initial_boot_params)
564 /* Reserve the dtb region */
565 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
566 fdt_totalsize(initial_boot_params),
569 /* Process header /memreserve/ fields */
571 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
574 early_init_dt_reserve_memory_arch(base, size, 0);
577 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
578 fdt_init_reserved_mem();
582 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
583 * @it: callback function
584 * @data: context data pointer
586 * This function is used to scan the flattened device-tree, it is
587 * used to extract the memory information at boot before we can
590 int __init of_scan_flat_dt(int (*it)(unsigned long node,
591 const char *uname, int depth,
595 const void *blob = initial_boot_params;
597 int offset, rc = 0, depth = -1;
599 for (offset = fdt_next_node(blob, -1, &depth);
600 offset >= 0 && depth >= 0 && !rc;
601 offset = fdt_next_node(blob, offset, &depth)) {
603 pathp = fdt_get_name(blob, offset, NULL);
605 pathp = kbasename(pathp);
606 rc = it(offset, pathp, depth, data);
612 * of_get_flat_dt_root - find the root node in the flat blob
614 unsigned long __init of_get_flat_dt_root(void)
620 * of_get_flat_dt_size - Return the total size of the FDT
622 int __init of_get_flat_dt_size(void)
624 return fdt_totalsize(initial_boot_params);
628 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
630 * This function can be used within scan_flattened_dt callback to get
631 * access to properties
633 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
636 return fdt_getprop(initial_boot_params, node, name, size);
640 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
641 * @node: node to test
642 * @compat: compatible string to compare with compatible list.
644 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
646 return of_fdt_is_compatible(initial_boot_params, node, compat);
650 * of_flat_dt_match - Return true if node matches a list of compatible values
652 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
654 return of_fdt_match(initial_boot_params, node, compat);
657 struct fdt_scan_status {
662 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
666 const char * __init of_flat_dt_get_machine_name(void)
669 unsigned long dt_root = of_get_flat_dt_root();
671 name = of_get_flat_dt_prop(dt_root, "model", NULL);
673 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
678 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
680 * @default_match: A machine specific ptr to return in case of no match.
681 * @get_next_compat: callback function to return next compatible match table.
683 * Iterate through machine match tables to find the best match for the machine
684 * compatible string in the FDT.
686 const void * __init of_flat_dt_match_machine(const void *default_match,
687 const void * (*get_next_compat)(const char * const**))
689 const void *data = NULL;
690 const void *best_data = default_match;
691 const char *const *compat;
692 unsigned long dt_root;
693 unsigned int best_score = ~1, score = 0;
695 dt_root = of_get_flat_dt_root();
696 while ((data = get_next_compat(&compat))) {
697 score = of_flat_dt_match(dt_root, compat);
698 if (score > 0 && score < best_score) {
707 pr_err("\n unrecognized device tree list:\n[ ");
709 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
712 printk("'%s' ", prop);
713 size -= strlen(prop) + 1;
714 prop += strlen(prop) + 1;
721 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
726 #ifdef CONFIG_BLK_DEV_INITRD
728 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
729 * @node: reference to node containing initrd location ('chosen')
731 static void __init early_init_dt_check_for_initrd(unsigned long node)
737 pr_debug("Looking for initrd properties... ");
739 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
742 start = of_read_number(prop, len/4);
744 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
747 end = of_read_number(prop, len/4);
749 initrd_start = (unsigned long)__va(start);
750 initrd_end = (unsigned long)__va(end);
751 initrd_below_start_ok = 1;
753 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
754 (unsigned long long)start, (unsigned long long)end);
757 static inline void early_init_dt_check_for_initrd(unsigned long node)
760 #endif /* CONFIG_BLK_DEV_INITRD */
762 #ifdef CONFIG_SERIAL_EARLYCON
763 extern struct of_device_id __earlycon_of_table[];
765 static int __init early_init_dt_scan_chosen_serial(void)
770 const struct of_device_id *match = __earlycon_of_table;
771 const void *fdt = initial_boot_params;
773 offset = fdt_path_offset(fdt, "/chosen");
775 offset = fdt_path_offset(fdt, "/chosen@0");
779 p = fdt_getprop(fdt, offset, "stdout-path", &l);
781 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
785 /* Get the node specified by stdout-path */
786 offset = fdt_path_offset(fdt, p);
790 while (match->compatible[0]) {
792 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
797 addr = fdt_translate_address(fdt, offset);
801 of_setup_earlycon(addr, match->data);
807 static int __init setup_of_earlycon(char *buf)
812 return early_init_dt_scan_chosen_serial();
814 early_param("earlycon", setup_of_earlycon);
818 * early_init_dt_scan_root - fetch the top level address and size cells
820 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
821 int depth, void *data)
828 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
829 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
831 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
833 dt_root_size_cells = be32_to_cpup(prop);
834 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
836 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
838 dt_root_addr_cells = be32_to_cpup(prop);
839 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
845 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
847 const __be32 *p = *cellp;
850 return of_read_number(p, s);
854 * early_init_dt_scan_memory - Look for an parse memory nodes
856 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
857 int depth, void *data)
859 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
860 const __be32 *reg, *endp;
863 /* We are scanning "memory" nodes only */
866 * The longtrail doesn't have a device_type on the
867 * /memory node, so look for the node called /memory@0.
869 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
871 } else if (strcmp(type, "memory") != 0)
874 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
876 reg = of_get_flat_dt_prop(node, "reg", &l);
880 endp = reg + (l / sizeof(__be32));
882 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
883 uname, l, reg[0], reg[1], reg[2], reg[3]);
885 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
888 base = dt_mem_next_cell(dt_root_addr_cells, ®);
889 size = dt_mem_next_cell(dt_root_size_cells, ®);
893 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
894 (unsigned long long)size);
896 early_init_dt_add_memory_arch(base, size);
902 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
903 int depth, void *data)
908 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
910 if (depth != 1 || !data ||
911 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
914 early_init_dt_check_for_initrd(node);
916 /* Retrieve command line */
917 p = of_get_flat_dt_prop(node, "bootargs", &l);
918 if (p != NULL && l > 0)
919 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
922 * CONFIG_CMDLINE is meant to be a default in case nothing else
923 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
924 * is set in which case we override whatever was found earlier.
926 #ifdef CONFIG_CMDLINE
927 #ifndef CONFIG_CMDLINE_FORCE
928 if (!((char *)data)[0])
930 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
931 #endif /* CONFIG_CMDLINE */
933 pr_debug("Command line is: %s\n", (char*)data);
939 #ifdef CONFIG_HAVE_MEMBLOCK
940 #define MAX_PHYS_ADDR ((phys_addr_t)~0)
942 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
944 const u64 phys_offset = __pa(PAGE_OFFSET);
946 if (!PAGE_ALIGNED(base)) {
947 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
948 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
952 size -= PAGE_SIZE - (base & ~PAGE_MASK);
953 base = PAGE_ALIGN(base);
957 if (base > MAX_PHYS_ADDR) {
958 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
963 if (base + size - 1 > MAX_PHYS_ADDR) {
964 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
965 ((u64)MAX_PHYS_ADDR) + 1, base + size);
966 size = MAX_PHYS_ADDR - base + 1;
969 if (base + size < phys_offset) {
970 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
974 if (base < phys_offset) {
975 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
977 size -= phys_offset - base;
980 memblock_add(base, size);
983 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
984 phys_addr_t size, bool nomap)
987 return memblock_remove(base, size);
988 return memblock_reserve(base, size);
992 * called from unflatten_device_tree() to bootstrap devicetree itself
993 * Architectures can override this definition if memblock isn't used
995 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
997 return __va(memblock_alloc(size, align));
1000 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1001 phys_addr_t size, bool nomap)
1003 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
1004 &base, &size, nomap ? " (nomap)" : "");
1009 bool __init early_init_dt_verify(void *params)
1014 /* check device tree validity */
1015 if (fdt_check_header(params))
1018 /* Setup flat device-tree pointer */
1019 initial_boot_params = params;
1020 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1021 fdt_totalsize(initial_boot_params));
1026 void __init early_init_dt_scan_nodes(void)
1028 /* Retrieve various information from the /chosen node */
1029 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1031 /* Initialize {size,address}-cells info */
1032 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1034 /* Setup memory, calling early_init_dt_add_memory_arch */
1035 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1038 bool __init early_init_dt_scan(void *params)
1042 status = early_init_dt_verify(params);
1046 early_init_dt_scan_nodes();
1051 * unflatten_device_tree - create tree of device_nodes from flat blob
1053 * unflattens the device-tree passed by the firmware, creating the
1054 * tree of struct device_node. It also fills the "name" and "type"
1055 * pointers of the nodes so the normal device-tree walking functions
1058 void __init unflatten_device_tree(void)
1060 __unflatten_device_tree(initial_boot_params, &of_root,
1061 early_init_dt_alloc_memory_arch);
1063 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1064 of_alias_scan(early_init_dt_alloc_memory_arch);
1068 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1070 * Copies and unflattens the device-tree passed by the firmware, creating the
1071 * tree of struct device_node. It also fills the "name" and "type"
1072 * pointers of the nodes so the normal device-tree walking functions
1073 * can be used. This should only be used when the FDT memory has not been
1074 * reserved such is the case when the FDT is built-in to the kernel init
1075 * section. If the FDT memory is reserved already then unflatten_device_tree
1076 * should be used instead.
1078 void __init unflatten_and_copy_device_tree(void)
1083 if (!initial_boot_params) {
1084 pr_warn("No valid device tree found, continuing without\n");
1088 size = fdt_totalsize(initial_boot_params);
1089 dt = early_init_dt_alloc_memory_arch(size,
1090 roundup_pow_of_two(FDT_V17_SIZE));
1093 memcpy(dt, initial_boot_params, size);
1094 initial_boot_params = dt;
1096 unflatten_device_tree();
1100 static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1101 struct bin_attribute *bin_attr,
1102 char *buf, loff_t off, size_t count)
1104 memcpy(buf, initial_boot_params + off, count);
1108 static int __init of_fdt_raw_init(void)
1110 static struct bin_attribute of_fdt_raw_attr =
1111 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1113 if (!initial_boot_params)
1116 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1117 fdt_totalsize(initial_boot_params))) {
1118 pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
1121 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1122 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1124 late_initcall(of_fdt_raw_init);
1127 #endif /* CONFIG_OF_EARLY_FLATTREE */