1. 打印出整个设备树,只需要一个参数,那就是设备树的root结点,打印出整个设备树,有利于调试内核或调试uboot传参等
2. 添加打印整个设备的代码在什么位置
linux/driver/of/fdt.c ----------->__unflatten_device_tree() //函数中第一行添加即可
3,添加代码如下:
dump_blob(blob)
4.以下代码为dump_blob(void *blob) 代码实现
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
#define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
static void print_data(const char *data, int len)
{
int i;
const char *p = data;
/* no data, don't print */
if (len == 0)
return;
if (util_is_printable_string(data, len)) {
printk(" = \"%s\"", (const char *)data);
} else if ((len % 4) == 0) {
printk(" = <");
for (i = 0; i < len; i += 4)
printk("0x%08x%s", fdt32_to_cpu(GET_CELL(p)),
i < (len - 4) ? " " : "");
printk(">");
} else {
printk(" = [");
for (i = 0; i < len; i++)
printk("%02x%s", *p++, i < len - 1 ? " " : "");
printk("]");
}
}
static void dump_blob(void *blob)
{
struct fdt_header *bph = blob;
uint32_t off_mem_rsvmap = fdt32_to_cpu(bph->off_mem_rsvmap);
uint32_t off_dt = fdt32_to_cpu(bph->off_dt_struct);
uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings);
struct fdt_reserve_entry *p_rsvmap =
(struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap);
const char *p_struct = (const char *)blob + off_dt;
const char *p_strings = (const char *)blob + off_str;
uint32_t version = fdt32_to_cpu(bph->version);
uint32_t totalsize = fdt32_to_cpu(bph->totalsize);
uint32_t tag;
const char *p, *s, *t;
int depth, sz, shift;
int i;
uint64_t addr, size;
depth = 0;
shift = 4;
printk("/dts-v1/;\n");
printk("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic));
printk("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize);
printk("// off_dt_struct:\t0x%x\n", off_dt);
printk("// off_dt_strings:\t0x%x\n", off_str);
printk("// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
printk("// version:\t\t%d\n", version);
printk("// last_comp_version:\t%d\n",
fdt32_to_cpu(bph->last_comp_version));
if (version >= 2)
printk("// boot_cpuid_phys:\t0x%x\n",
fdt32_to_cpu(bph->boot_cpuid_phys));
if (version >= 3)
printk("// size_dt_strings:\t0x%x\n",
fdt32_to_cpu(bph->size_dt_strings));
if (version >= 17)
printk("// size_dt_struct:\t0x%x\n",
fdt32_to_cpu(bph->size_dt_struct));
printk("\n");
for (i = 0; ; i++) {
addr = fdt64_to_cpu(p_rsvmap[i].address);
size = fdt64_to_cpu(p_rsvmap[i].size);
if (addr == 0 && size == 0)
break;
printk("/memreserve/ %llx %llx;\n",
(unsigned long long)addr, (unsigned long long)size);
}
p = p_struct;
while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
/* printk("tag: 0x%08x (%d)\n", tag, p - p_struct); */
if (tag == FDT_BEGIN_NODE) {
s = p;
p = PALIGN(p + strlen(s) + 1, 4);
if (*s == '\0')
s = "/";
printk("%*s%s {\n", depth * shift, "", s);
depth++;
continue;
}
if (tag == FDT_END_NODE) {
depth--;
printk("%*s};\n", depth * shift, "");
continue;
}
if (tag == FDT_NOP) {
printk("%*s// [NOP]\n", depth * shift, "");
continue;
}
if (tag != FDT_PROP) {
printk("%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
break;
}
sz = fdt32_to_cpu(GET_CELL(p));
s = p_strings + fdt32_to_cpu(GET_CELL(p));
if (version < 16 && sz >= 8)
p = PALIGN(p, 8);
t = p;
p = PALIGN(p + sz, 4);
printk("%*s%s", depth * shift, "", s);
print_data(t, sz);
printk(";\n");
}
}