Skip to content

Commit 398101b

Browse files
committed
Generalize DTC compilation to support both DTS/B
1 parent f11bd7b commit 398101b

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

riscv/dts.cc

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

102-
std::string dts_compile(const std::string& dts)
102+
std::string dtc_compile(const std::string& dtc_input, const std::string& input_type, const std::string& output_type)
103103
{
104-
// Convert the DTS to DTB
105-
int dts_pipe[2];
106-
pid_t dts_pid;
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;
109+
110+
int dtc_input_pipe[2];
111+
pid_t dtc_input_pid;
107112

108113
fflush(NULL); // flush stdout/stderr before forking
109-
if (pipe(dts_pipe) != 0 || (dts_pid = fork()) < 0) {
110-
std::cerr << "Failed to fork dts child: " << strerror(errno) << std::endl;
114+
if (pipe(dtc_input_pipe) != 0 || (dtc_input_pid = fork()) < 0) {
115+
std::cerr << "Failed to fork dtc_input child: " << strerror(errno) << std::endl;
111116
exit(1);
112117
}
113118

114-
// Child process to output dts
115-
if (dts_pid == 0) {
116-
close(dts_pipe[0]);
117-
int step, len = dts.length();
118-
const char *buf = dts.c_str();
119+
// Child process to output dtc_input
120+
if (dtc_input_pid == 0) {
121+
close(dtc_input_pipe[0]);
122+
int step, len = dtc_input.length();
123+
const char *buf = dtc_input.c_str();
119124
for (int done = 0; done < len; done += step) {
120-
step = write(dts_pipe[1], buf+done, len-done);
125+
step = write(dtc_input_pipe[1], buf+done, len-done);
121126
if (step == -1) {
122-
std::cerr << "Failed to write dts: " << strerror(errno) << std::endl;
127+
std::cerr << "Failed to write dtc_input: " << strerror(errno) << std::endl;
123128
exit(1);
124129
}
125130
}
126-
close(dts_pipe[1]);
131+
close(dtc_input_pipe[1]);
127132
exit(0);
128133
}
129134

130-
pid_t dtb_pid;
131-
int dtb_pipe[2];
132-
if (pipe(dtb_pipe) != 0 || (dtb_pid = fork()) < 0) {
133-
std::cerr << "Failed to fork dtb child: " << strerror(errno) << std::endl;
135+
pid_t dtc_output_pid;
136+
int dtc_output_pipe[2];
137+
if (pipe(dtc_output_pipe) != 0 || (dtc_output_pid = fork()) < 0) {
138+
std::cerr << "Failed to fork dtc_output child: " << strerror(errno) << std::endl;
134139
exit(1);
135140
}
136141

137-
// Child process to output dtb
138-
if (dtb_pid == 0) {
139-
dup2(dts_pipe[0], 0);
140-
dup2(dtb_pipe[1], 1);
141-
close(dts_pipe[0]);
142-
close(dts_pipe[1]);
143-
close(dtb_pipe[0]);
144-
close(dtb_pipe[1]);
145-
execlp(DTC, DTC, "-O", "dtb", (char *)0);
142+
// Child process to output dtc_output
143+
if (dtc_output_pid == 0) {
144+
dup2(dtc_input_pipe[0], 0);
145+
dup2(dtc_output_pipe[1], 1);
146+
close(dtc_input_pipe[0]);
147+
close(dtc_input_pipe[1]);
148+
close(dtc_output_pipe[0]);
149+
close(dtc_output_pipe[1]);
150+
execlp(DTC, DTC, "-O", output_type.c_str(), "-I", input_type.c_str(), (char *)0);
146151
std::cerr << "Failed to run " DTC ": " << strerror(errno) << std::endl;
147152
exit(1);
148153
}
149154

150-
close(dts_pipe[1]);
151-
close(dts_pipe[0]);
152-
close(dtb_pipe[1]);
155+
close(dtc_input_pipe[1]);
156+
close(dtc_input_pipe[0]);
157+
close(dtc_output_pipe[1]);
153158

154-
// Read-out dtb
155-
std::stringstream dtb;
159+
// Read-out dtc_output
160+
std::stringstream dtc_output;
156161

157162
int got;
158163
char buf[4096];
159-
while ((got = read(dtb_pipe[0], buf, sizeof(buf))) > 0) {
160-
dtb.write(buf, got);
164+
while ((got = read(dtc_output_pipe[0], buf, sizeof(buf))) > 0) {
165+
dtc_output.write(buf, got);
161166
}
162167
if (got == -1) {
163-
std::cerr << "Failed to read dtb: " << strerror(errno) << std::endl;
168+
std::cerr << "Failed to read dtc_output: " << strerror(errno) << std::endl;
164169
exit(1);
165170
}
166-
close(dtb_pipe[0]);
171+
close(dtc_output_pipe[0]);
167172

168173
// Reap children
169174
int status;
170-
waitpid(dts_pid, &status, 0);
175+
waitpid(dtc_input_pid, &status, 0);
171176
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
172-
std::cerr << "Child dts process failed" << std::endl;
177+
std::cerr << "Child dtc_input process failed" << std::endl;
173178
exit(1);
174179
}
175-
waitpid(dtb_pid, &status, 0);
180+
waitpid(dtc_output_pid, &status, 0);
176181
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
177-
std::cerr << "Child dtb process failed" << std::endl;
182+
std::cerr << "Child dtc_output process failed" << std::endl;
178183
exit(1);
179184
}
180185

181-
return dtb.str();
186+
return dtc_output.str();
182187
}
183188

184189
int fdt_get_node_addr_size(const void *fdt, int node, reg_t *addr,

riscv/dts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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 dts_compile(const std::string& dts);
14+
std::string dtc_compile(const std::string& dtc_input, const std::string& input_type, const std::string& output_type);
1515

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

riscv/sim.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +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");
136137
} else {
137138
std::pair<reg_t, reg_t> initrd_bounds = cfg->initrd_bounds;
138139
std::string device_nodes;
@@ -142,7 +143,7 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
142143
device_nodes.append(factory->generate_dts(this, sargs));
143144
}
144145
dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, cfg, mems, device_nodes);
145-
dtb = dts_compile(dts);
146+
dtb = dtc_compile(dts, "dts", "dtb");
146147
}
147148

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

0 commit comments

Comments
 (0)