MOS Source Code
Loading...
Searching...
No Matches
dt_dump.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "libfdt++.hpp"
4#include "mos/syslog/printk.h"
5
6#include <mos_string.h>
7
8#define INDENT " "
9
10static void print_property_value(const dt_property &prop, size_t indent_len = 0)
11{
12 const char *string = prop.get_string();
13 const int len = prop.len();
14
15 // a printable string shouldn't start with '\0' and should end with '\0'
16 bool printable = (string[0] != '\0') && (string[len - 1] == '\0');
17
18 if (printable)
19 {
20 for (int i = 0; i < len; ++i)
21 {
22 const char c = string[i];
23 if (!(c >= 32 && c <= 126) && (c != '\0'))
24 {
25 printable = false;
26 break;
27 }
28 }
29 }
30
31 if (printable)
32 {
33 pr_cont("\"");
34 for (int i = 0; i < len - 1; ++i)
35 {
36 char c = string[i];
37 if (c == '\0')
38 c = '|';
39 pr_cont("%c", c);
40 }
41 pr_cont("\"");
42 }
43 else if (prop.len() == 4)
44 pr_cont("0x%x", prop.get_u32());
45 else if (prop.len() == 8)
46 pr_cont("0x%llx", prop.get_u64());
47 else
48 {
49 if (strcmp(prop.get_name(), "reg") == 0)
50 {
51 const dt_reg regs = prop;
52 if (!regs.verify_validity())
53 {
54 pr_cont("<invalid reg>:");
55 goto do_dump;
56 }
57
58 for (const auto &[base, size] : regs)
59 pr_cont("(" PTR_VLFMT ", %lu)", base, size);
60 }
61 else
62 {
63 do_dump:
64 const char *data = prop.get_string();
65 const size_t len = prop.len();
66
67 for (size_t i = 0; i < len; ++i)
68 {
69 if (i % 16 == 0)
70 {
71 // print string
72 for (size_t j = i - 16; j <= i; j++)
73 {
74 const char c = data[j];
75 pr_cont("%c", c >= 32 && c <= 126 ? c : '.');
76 }
77
78 if (i != 0)
79 {
80 pr_cont("\n");
81 for (size_t j = 0; j < indent_len; ++j)
82 pr_cont(" ");
83 }
84 }
85
86 pr_cont("%02x ", data[i]);
87 }
88
89 if (len % 16 != 0)
90 {
91 size_t spaces = (16 - (len % 16)) * 3;
92
93 if (len < 16)
94 spaces = 3;
95
96 pr_cont("%*c", (int) spaces, ' ');
97 for (size_t i = len - (len % 16); i < len; i++)
98 {
99 const char c = data[i];
100 pr_cont("%c", c >= 32 && c <= 126 ? c : '.');
101 }
102 }
103 }
104 }
105
106 pr_cont("\n");
107}
108
109void dump_fdt_node(const dt_node &node, int depth = 0)
110{
111 if (depth == 0)
112 pr_info();
113
114 for (int i = 0; i < depth; ++i)
116
117 const auto name = node.get_name();
118 if (strlen(name) == 0)
119 {
120 MOS_ASSERT(depth == 0); // root node should have an empty name
121 pr_cont("/ {\n");
122 }
123 else
124 pr_cont("%s {\n", name);
125
126 // Print properties
127 for (const auto prop : node.properties())
128 {
129 for (int i = 0; i < depth + 1; ++i)
131
132 pr_cont("%s", prop.get_name());
133 if (prop.len())
134 {
135 pr_cont(" = ");
136
137 const size_t idenet_len = strlen(prop.get_name()) + 3 + (depth + 1) * strlen(INDENT);
138 print_property_value(prop, idenet_len);
139 }
140 else
141 {
142 pr_cont("\n");
143 }
144 }
145
146 // Print subnodes
147 for (const auto child : node)
148 dump_fdt_node(child, depth + 1);
149
150 for (int i = 0; i < depth; ++i)
152 pr_cont("}\n");
153}
#define MOS_ASSERT(cond)
Definition assert.h:14
const char * get_name() const
Definition libfdt++.cpp:55
node_property_list properties() const
Definition libfdt++.hpp:146
u64 get_u64() const
Definition libfdt++.cpp:112
const char * get_string() const
Definition libfdt++.cpp:117
int len() const
Definition libfdt++.hpp:28
const char * get_name() const
Definition libfdt++.hpp:30
u32 get_u32() const
Definition libfdt++.cpp:107
Class to iterate over reg property cells.
Definition libfdt++.hpp:48
bool verify_validity() const
Definition libfdt++.cpp:25
static void print_property_value(const dt_property &prop, size_t indent_len=0)
Definition dt_dump.cpp:10
#define INDENT
Definition dt_dump.cpp:8
void dump_fdt_node(const dt_node &node, int depth=0)
Definition dt_dump.cpp:109
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
#define pr_info(fmt,...)
Definition printk.h:35
#define pr_cont(fmt,...)
Definition printk.h:41
size_t size
Definition slab.c:30
const char * name
Definition slab.c:31
#define PTR_VLFMT
Definition types.h:34