2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 David Chisnall
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
47 void write(dtc::byte_buffer &buffer, int fd)
49 size_t size = buffer.size();
50 uint8_t *data = buffer.data();
53 ssize_t r = ::write(fd, data, size);
59 else if (errno != EAGAIN)
61 fprintf(stderr, "Writing to file failed\n");
73 void output_writer::write_data(byte_buffer b)
82 binary_writer::write_string(const string &name)
84 push_string(buffer, name);
90 binary_writer::write_data(uint8_t v)
96 binary_writer::write_data(uint32_t v)
98 while (buffer.size() % 4 != 0)
102 push_big_endian(buffer, v);
106 binary_writer::write_data(uint64_t v)
108 while (buffer.size() % 8 != 0)
112 push_big_endian(buffer, v);
116 binary_writer::write_to_file(int fd)
122 binary_writer::size()
124 return buffer.size();
128 asm_writer::write_line(const char *c)
133 buffer.push_back('\n');
139 asm_writer::write_byte(uint8_t b)
142 if (byte_count++ == 0)
144 buffer.push_back('\t');
146 write_string(".byte 0x");
147 snprintf(out, 3, "%.2hhx", b);
148 buffer.push_back(out[0]);
149 buffer.push_back(out[1]);
152 buffer.push_back('\n');
157 buffer.push_back(';');
158 buffer.push_back(' ');
163 asm_writer::write_label(const string &name)
165 write_line("\t.globl ");
166 push_string(buffer, name);
167 buffer.push_back('\n');
168 push_string(buffer, name);
169 buffer.push_back(':');
170 buffer.push_back('\n');
171 buffer.push_back('_');
172 push_string(buffer, name);
173 buffer.push_back(':');
174 buffer.push_back('\n');
179 asm_writer::write_comment(const string &name)
182 push_string(buffer, name);
183 write_string(" */\n");
187 asm_writer::write_string(const char *c)
191 buffer.push_back((uint8_t)*(c++));
197 asm_writer::write_string(const string &name)
199 write_line("\t.string \"");
200 push_string(buffer, name);
202 bytes_written += name.size() + 1;
206 asm_writer::write_data(uint8_t v)
213 asm_writer::write_data(uint32_t v)
215 if (bytes_written % 4 != 0)
217 write_line("\t.balign 4\n");
218 bytes_written += (4 - (bytes_written % 4));
220 write_byte((v >> 24) & 0xff);
221 write_byte((v >> 16) & 0xff);
222 write_byte((v >> 8) & 0xff);
223 write_byte((v >> 0) & 0xff);
228 asm_writer::write_data(uint64_t v)
230 if (bytes_written % 8 != 0)
232 write_line("\t.balign 8\n");
233 bytes_written += (8 - (bytes_written % 8));
235 write_byte((v >> 56) & 0xff);
236 write_byte((v >> 48) & 0xff);
237 write_byte((v >> 40) & 0xff);
238 write_byte((v >> 32) & 0xff);
239 write_byte((v >> 24) & 0xff);
240 write_byte((v >> 16) & 0xff);
241 write_byte((v >> 8) & 0xff);
242 write_byte((v >> 0) & 0xff);
247 asm_writer::write_to_file(int fd)
255 return bytes_written;
259 header::write(output_writer &out)
261 out.write_label("dt_blob_start");
262 out.write_label("dt_header");
263 out.write_comment("magic");
264 out.write_data(magic);
265 out.write_comment("totalsize");
266 out.write_data(totalsize);
267 out.write_comment("off_dt_struct");
268 out.write_data(off_dt_struct);
269 out.write_comment("off_dt_strings");
270 out.write_data(off_dt_strings);
271 out.write_comment("off_mem_rsvmap");
272 out.write_data(off_mem_rsvmap);
273 out.write_comment("version");
274 out.write_data(version);
275 out.write_comment("last_comp_version");
276 out.write_data(last_comp_version);
277 out.write_comment("boot_cpuid_phys");
278 out.write_data(boot_cpuid_phys);
279 out.write_comment("size_dt_strings");
280 out.write_data(size_dt_strings);
281 out.write_comment("size_dt_struct");
282 out.write_data(size_dt_struct);
286 header::read_dtb(input_buffer &input)
288 if (!input.consume_binary(magic))
290 fprintf(stderr, "Missing magic token in header.");
293 if (magic != 0xd00dfeed)
295 fprintf(stderr, "Bad magic token in header. Got %" PRIx32
296 " expected 0xd00dfeed\n", magic);
299 return input.consume_binary(totalsize) &&
300 input.consume_binary(off_dt_struct) &&
301 input.consume_binary(off_dt_strings) &&
302 input.consume_binary(off_mem_rsvmap) &&
303 input.consume_binary(version) &&
304 input.consume_binary(last_comp_version) &&
305 input.consume_binary(boot_cpuid_phys) &&
306 input.consume_binary(size_dt_strings) &&
307 input.consume_binary(size_dt_struct);
310 string_table::add_string(const string &str)
312 auto old = string_offsets.find(str);
313 if (old == string_offsets.end())
315 uint32_t start = size;
316 // Don't forget the trailing nul
317 size += str.size() + 1;
318 string_offsets.insert(std::make_pair(str, start));
319 strings.push_back(str);
329 string_table::write(dtb::output_writer &writer)
331 writer.write_comment("Strings table.");
332 writer.write_label("dt_strings_start");
333 for (auto &i : strings)
335 writer.write_string(i);
337 writer.write_label("dt_strings_end");