2 * Copyright (c) 2013 David Chisnall
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
46 void output_writer::write_data(byte_buffer b)
55 binary_writer::write_string(const string &name)
57 push_string(buffer, name);
63 binary_writer::write_data(uint8_t v)
69 binary_writer::write_data(uint32_t v)
71 while (buffer.size() % 4 != 0)
75 push_big_endian(buffer, v);
79 binary_writer::write_data(uint64_t v)
81 while (buffer.size() % 8 != 0)
85 push_big_endian(buffer, v);
89 binary_writer::write_to_file(int fd)
91 // FIXME: Check return
92 write(fd, buffer.data(), buffer.size());
102 asm_writer::write_line(const char *c)
107 buffer.push_back('\n');
113 asm_writer::write_byte(uint8_t b)
116 if (byte_count++ == 0)
118 buffer.push_back('\t');
120 write_string(".byte 0x");
121 snprintf(out, 3, "%.2hhx", b);
122 buffer.push_back(out[0]);
123 buffer.push_back(out[1]);
126 buffer.push_back('\n');
131 buffer.push_back(';');
132 buffer.push_back(' ');
137 asm_writer::write_label(const string &name)
139 write_line("\t.globl ");
140 push_string(buffer, name);
141 buffer.push_back('\n');
142 push_string(buffer, name);
143 buffer.push_back(':');
144 buffer.push_back('\n');
145 buffer.push_back('_');
146 push_string(buffer, name);
147 buffer.push_back(':');
148 buffer.push_back('\n');
153 asm_writer::write_comment(const string &name)
156 push_string(buffer, name);
157 write_string(" */\n");
161 asm_writer::write_string(const char *c)
165 buffer.push_back((uint8_t)*(c++));
171 asm_writer::write_string(const string &name)
173 write_line("\t.string \"");
174 push_string(buffer, name);
176 bytes_written += name.size() + 1;
180 asm_writer::write_data(uint8_t v)
187 asm_writer::write_data(uint32_t v)
189 if (bytes_written % 4 != 0)
191 write_line("\t.balign 4\n");
192 bytes_written += (4 - (bytes_written % 4));
194 write_byte((v >> 24) & 0xff);
195 write_byte((v >> 16) & 0xff);
196 write_byte((v >> 8) & 0xff);
197 write_byte((v >> 0) & 0xff);
202 asm_writer::write_data(uint64_t v)
204 if (bytes_written % 8 != 0)
206 write_line("\t.balign 8\n");
207 bytes_written += (8 - (bytes_written % 8));
209 write_byte((v >> 56) & 0xff);
210 write_byte((v >> 48) & 0xff);
211 write_byte((v >> 40) & 0xff);
212 write_byte((v >> 32) & 0xff);
213 write_byte((v >> 24) & 0xff);
214 write_byte((v >> 16) & 0xff);
215 write_byte((v >> 8) & 0xff);
216 write_byte((v >> 0) & 0xff);
221 asm_writer::write_to_file(int fd)
223 // FIXME: Check return
224 write(fd, buffer.data(), buffer.size());
230 return bytes_written;
234 header::write(output_writer &out)
236 out.write_label("dt_blob_start");
237 out.write_label("dt_header");
238 out.write_comment("magic");
239 out.write_data(magic);
240 out.write_comment("totalsize");
241 out.write_data(totalsize);
242 out.write_comment("off_dt_struct");
243 out.write_data(off_dt_struct);
244 out.write_comment("off_dt_strings");
245 out.write_data(off_dt_strings);
246 out.write_comment("off_mem_rsvmap");
247 out.write_data(off_mem_rsvmap);
248 out.write_comment("version");
249 out.write_data(version);
250 out.write_comment("last_comp_version");
251 out.write_data(last_comp_version);
252 out.write_comment("boot_cpuid_phys");
253 out.write_data(boot_cpuid_phys);
254 out.write_comment("size_dt_strings");
255 out.write_data(size_dt_strings);
256 out.write_comment("size_dt_struct");
257 out.write_data(size_dt_struct);
261 header::read_dtb(input_buffer &input)
263 if (!(input.consume_binary(magic) && magic == 0xd00dfeed))
265 fprintf(stderr, "Missing magic token in header. Got %" PRIx32
266 " expected 0xd00dfeed\n", magic);
269 return input.consume_binary(totalsize) &&
270 input.consume_binary(off_dt_struct) &&
271 input.consume_binary(off_dt_strings) &&
272 input.consume_binary(off_mem_rsvmap) &&
273 input.consume_binary(version) &&
274 input.consume_binary(last_comp_version) &&
275 input.consume_binary(boot_cpuid_phys) &&
276 input.consume_binary(size_dt_strings) &&
277 input.consume_binary(size_dt_struct);
280 string_table::add_string(const string &str)
282 auto old = string_offsets.find(str);
283 if (old == string_offsets.end())
285 uint32_t start = size;
286 // Don't forget the trailing nul
287 size += str.size() + 1;
288 string_offsets.insert(std::make_pair(str, start));
289 strings.push_back(str);
299 string_table::write(dtb::output_writer &writer)
301 writer.write_comment("Strings table.");
302 writer.write_label("dt_strings_start");
303 for (auto &i : strings)
305 writer.write_string(i);
307 writer.write_label("dt_strings_end");