Skip to content

Commit

Permalink
Merge pull request #1759 from riscv-software-src/dts-api
Browse files Browse the repository at this point in the history
Improve dts <-> dtb API
  • Loading branch information
aswaterman authored Aug 2, 2024
2 parents fdd2570 + b9ecc1d commit 2890ea7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
21 changes: 14 additions & 7 deletions riscv/dts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,10 @@ std::string make_dts(size_t insns_per_rtc_tick, size_t cpu_hz,
return s.str();
}

std::string dtc_compile(const std::string& dtc_input, const std::string& input_type, const std::string& output_type)
static std::string dtc_compile(const std::string& dtc_input, bool compile)
{
if (input_type == output_type)
std::cerr << "Must have differing {in,out}put types for running " DTC << std::endl;

if (!((input_type == "dts" && output_type == "dtb") || (input_type == "dtb" && output_type == "dts")))
std::cerr << "Invalid {in,out}put types for running " DTC ": Must convert from 'dts' to 'dtb' (or vice versa)" << std::endl;
const char* input_type = compile ? "dts" : "dtb";
const char* output_type = compile ? "dtb" : "dts";

int dtc_input_pipe[2];
pid_t dtc_input_pid;
Expand Down Expand Up @@ -147,7 +144,7 @@ std::string dtc_compile(const std::string& dtc_input, const std::string& input_t
close(dtc_input_pipe[1]);
close(dtc_output_pipe[0]);
close(dtc_output_pipe[1]);
execlp(DTC, DTC, "-O", output_type.c_str(), "-I", input_type.c_str(), (char *)0);
execlp(DTC, DTC, "-O", output_type, "-I", input_type, nullptr);
std::cerr << "Failed to run " DTC ": " << strerror(errno) << std::endl;
exit(1);
}
Expand Down Expand Up @@ -186,6 +183,16 @@ std::string dtc_compile(const std::string& dtc_input, const std::string& input_t
return dtc_output.str();
}

std::string dtb_to_dts(const std::string& dtc_input)
{
return dtc_compile(dtc_input, false);
}

std::string dts_to_dtb(const std::string& dtc_input)
{
return dtc_compile(dtc_input, true);
}

int fdt_get_node_addr_size(const void *fdt, int node, reg_t *addr,
unsigned long *size, const char *field)
{
Expand Down
3 changes: 2 additions & 1 deletion riscv/dts.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ std::string make_dts(size_t insns_per_rtc_tick, size_t cpu_hz,
std::vector<std::pair<reg_t, abstract_mem_t*>> mems,
std::string device_nodes);

std::string dtc_compile(const std::string& dtc_input, const std::string& input_type, const std::string& output_type);
std::string dts_to_dtb(const std::string& dtc_input);
std::string dtb_to_dts(const std::string& dtc_input);

int fdt_get_node_addr_size(const void *fdt, int node, reg_t *addr,
unsigned long *size, const char *field);
Expand Down
4 changes: 2 additions & 2 deletions riscv/sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
std::stringstream strstream;
strstream << fin.rdbuf();
dtb = strstream.str();
dts = dtc_compile(dtb, "dtb", "dts");
dts = dtb_to_dts(dtb);
} else {
std::pair<reg_t, reg_t> initrd_bounds = cfg->initrd_bounds;
std::string device_nodes;
Expand All @@ -143,7 +143,7 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
device_nodes.append(factory->generate_dts(this, sargs));
}
dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, cfg, mems, device_nodes);
dtb = dtc_compile(dts, "dts", "dtb");
dtb = dts_to_dtb(dts);
}

int fdt_code = fdt_check_header(dtb.c_str());
Expand Down

0 comments on commit 2890ea7

Please sign in to comment.