内核--打印设备树

内核–打印设备树

1.修改的文件
需要操作的文件:driver/of/fdt.c中的__unflatten_device_tree,
在该函数的第一行添加函数:dump_blob(blob)
2.由于原本dump_blob函数定义在其他地方,并且包含了许多内核不能识别的数据类型或打印方式,现修改成内核能够识别的方式,并在driver/of/fdt.c中重新定义此函数。
在driver/of/fdt.c需要添加的内容如下

#include "of_private.h"
#include "../scripts/dtc/libfdt/fdt.h"
#include "../scripts/dtc/libfdt/libfdt_env.h"
#include <linux/types.h>
#include <linux/efi.h>
#include <linux/ctype.h>
#define PALIGN(p, a)	((void *)(ALIGN((unsigned long)(p), (a))))
#define GET_CELL(p)	(p += 4, *((const uint32_t *)(p-4)))
#define PALIGN(p, a)	((void *)(ALIGN((unsigned long)(p), (a))))

bool util_is_printable_string(const void *data, int len)
{
	const char *s = data;
	const char *ss, *se;

	/* zero length is not */
	if (len == 0)
		return 0;

	/* must terminate with zero */
	if (s[len - 1] != '\0')
		return 0;

	se = s + len;

	while (s < se) {
		ss = s;
		while (s < se && *s && isprint((unsigned char)*s))
			s++;

		/* not zero, or not done yet */
		if (*s != '\0' || s == ss)
			return 0;

		s++;
	}

	return 1;
}
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;

	pr_info("/dts-v1/;\n");
	pr_info("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic));
	pr_info("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize);
	pr_info("// off_dt_struct:\t0x%x\n", off_dt);
	pr_info("// off_dt_strings:\t0x%x\n", off_str);
	pr_info("// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
	pr_info("// version:\t\t%d\n", version);
	pr_info("// last_comp_version:\t%d\n",
	       fdt32_to_cpu(bph->last_comp_version));
	if (version >= 2)
		pr_info("// boot_cpuid_phys:\t0x%x\n",
		       fdt32_to_cpu(bph->boot_cpuid_phys));

	if (version >= 3)
		pr_info("// size_dt_strings:\t0x%x\n",
		       fdt32_to_cpu(bph->size_dt_strings));
	if (version >= 17)
		pr_info("// size_dt_struct:\t0x%x\n",
		       fdt32_to_cpu(bph->size_dt_struct));
	pr_info("\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;

		pr_info("/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) {

		/* printf("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 = "/";

			pr_info("%*s%s {\n", depth * shift, "", s);

			depth++;
			continue;
		}

		if (tag == FDT_END_NODE) {
			depth--;

			pr_info("%*s};\n", depth * shift, "");
			continue;
			}

		if (tag == FDT_NOP) {
			pr_info("%*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);

		pr_info("%*s%s", depth * shift, "", s);
		print_data(t, sz);
		pr_info(";\n");
	}
}

3.至此在加载内核后,就可在板卡启动的打印中看到设备树被打印了出来。
其中print_data是打印出了设备树中每一项的实际值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值