@@ -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
184189int fdt_get_node_addr_size (const void *fdt, int node, reg_t *addr,
0 commit comments