Skip to content

Commit 2890ea7

Browse files
authored
Merge pull request #1759 from riscv-software-src/dts-api
Improve dts <-> dtb API
2 parents fdd2570 + b9ecc1d commit 2890ea7

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

riscv/dts.cc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,10 @@ std::string make_dts(size_t insns_per_rtc_tick, size_t cpu_hz,
9999
return s.str();
100100
}
101101

102-
std::string dtc_compile(const std::string& dtc_input, const std::string& input_type, const std::string& output_type)
102+
static std::string dtc_compile(const std::string& dtc_input, bool compile)
103103
{
104-
if (input_type == output_type)
105-
std::cerr << "Must have differing {in,out}put types for running " DTC << std::endl;
106-
107-
if (!((input_type == "dts" && output_type == "dtb") || (input_type == "dtb" && output_type == "dts")))
108-
std::cerr << "Invalid {in,out}put types for running " DTC ": Must convert from 'dts' to 'dtb' (or vice versa)" << std::endl;
104+
const char* input_type = compile ? "dts" : "dtb";
105+
const char* output_type = compile ? "dtb" : "dts";
109106

110107
int dtc_input_pipe[2];
111108
pid_t dtc_input_pid;
@@ -147,7 +144,7 @@ std::string dtc_compile(const std::string& dtc_input, const std::string& input_t
147144
close(dtc_input_pipe[1]);
148145
close(dtc_output_pipe[0]);
149146
close(dtc_output_pipe[1]);
150-
execlp(DTC, DTC, "-O", output_type.c_str(), "-I", input_type.c_str(), (char *)0);
147+
execlp(DTC, DTC, "-O", output_type, "-I", input_type, nullptr);
151148
std::cerr << "Failed to run " DTC ": " << strerror(errno) << std::endl;
152149
exit(1);
153150
}
@@ -186,6 +183,16 @@ std::string dtc_compile(const std::string& dtc_input, const std::string& input_t
186183
return dtc_output.str();
187184
}
188185

186+
std::string dtb_to_dts(const std::string& dtc_input)
187+
{
188+
return dtc_compile(dtc_input, false);
189+
}
190+
191+
std::string dts_to_dtb(const std::string& dtc_input)
192+
{
193+
return dtc_compile(dtc_input, true);
194+
}
195+
189196
int fdt_get_node_addr_size(const void *fdt, int node, reg_t *addr,
190197
unsigned long *size, const char *field)
191198
{

riscv/dts.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ std::string make_dts(size_t insns_per_rtc_tick, size_t cpu_hz,
1111
std::vector<std::pair<reg_t, abstract_mem_t*>> mems,
1212
std::string device_nodes);
1313

14-
std::string dtc_compile(const std::string& dtc_input, const std::string& input_type, const std::string& output_type);
14+
std::string dts_to_dtb(const std::string& dtc_input);
15+
std::string dtb_to_dts(const std::string& dtc_input);
1516

1617
int fdt_get_node_addr_size(const void *fdt, int node, reg_t *addr,
1718
unsigned long *size, const char *field);

riscv/sim.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
133133
std::stringstream strstream;
134134
strstream << fin.rdbuf();
135135
dtb = strstream.str();
136-
dts = dtc_compile(dtb, "dtb", "dts");
136+
dts = dtb_to_dts(dtb);
137137
} else {
138138
std::pair<reg_t, reg_t> initrd_bounds = cfg->initrd_bounds;
139139
std::string device_nodes;
@@ -143,7 +143,7 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
143143
device_nodes.append(factory->generate_dts(this, sargs));
144144
}
145145
dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, cfg, mems, device_nodes);
146-
dtb = dtc_compile(dts, "dts", "dtb");
146+
dtb = dts_to_dtb(dts);
147147
}
148148

149149
int fdt_code = fdt_check_header(dtb.c_str());

0 commit comments

Comments
 (0)