dtb.cc (b626f5a73a48f44a31a200291b141e1da408a2ff) | dtb.cc (bbe31b709a653884e18995a1c97cdafd7392999a) |
---|---|
1/*- 2 * Copyright (c) 2013 David Chisnall 3 * All rights reserved. 4 * 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. 8 * --- 22 unchanged lines hidden (view full) --- 31 */ 32 33#include "dtb.hh" 34#include <sys/types.h> 35#include <inttypes.h> 36#include <stdio.h> 37#include <unistd.h> 38 | 1/*- 2 * Copyright (c) 2013 David Chisnall 3 * All rights reserved. 4 * 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. 8 * --- 22 unchanged lines hidden (view full) --- 31 */ 32 33#include "dtb.hh" 34#include <sys/types.h> 35#include <inttypes.h> 36#include <stdio.h> 37#include <unistd.h> 38 |
39using std::string; |
|
39 40namespace dtc 41{ 42namespace dtb 43{ 44 45void output_writer::write_data(byte_buffer b) 46{ 47 for (auto i : b) 48 { 49 write_data(i); 50 } 51} 52 53void | 40 41namespace dtc 42{ 43namespace dtb 44{ 45 46void output_writer::write_data(byte_buffer b) 47{ 48 for (auto i : b) 49 { 50 write_data(i); 51 } 52} 53 54void |
54binary_writer::write_string(string name) | 55binary_writer::write_string(const string &name) |
55{ | 56{ |
56 name.push_to_buffer(buffer); | 57 push_string(buffer, name); |
57 // Trailing nul 58 buffer.push_back(0); 59} 60 61void 62binary_writer::write_data(uint8_t v) 63{ 64 buffer.push_back(v); --- 28 unchanged lines hidden (view full) --- 93 94uint32_t 95binary_writer::size() 96{ 97 return buffer.size(); 98} 99 100void | 58 // Trailing nul 59 buffer.push_back(0); 60} 61 62void 63binary_writer::write_data(uint8_t v) 64{ 65 buffer.push_back(v); --- 28 unchanged lines hidden (view full) --- 94 95uint32_t 96binary_writer::size() 97{ 98 return buffer.size(); 99} 100 101void |
101asm_writer::write_string(const char *c) 102{ 103 while (*c) 104 { 105 buffer.push_back((uint8_t)*(c++)); 106 } 107} 108 109void | |
110asm_writer::write_line(const char *c) 111{ 112 if (byte_count != 0) 113 { 114 byte_count = 0; 115 buffer.push_back('\n'); 116 } 117 write_string(c); --- 19 unchanged lines hidden (view full) --- 137 else 138 { 139 buffer.push_back(';'); 140 buffer.push_back(' '); 141 } 142} 143 144void | 102asm_writer::write_line(const char *c) 103{ 104 if (byte_count != 0) 105 { 106 byte_count = 0; 107 buffer.push_back('\n'); 108 } 109 write_string(c); --- 19 unchanged lines hidden (view full) --- 129 else 130 { 131 buffer.push_back(';'); 132 buffer.push_back(' '); 133 } 134} 135 136void |
145asm_writer::write_label(string name) | 137asm_writer::write_label(const string &name) |
146{ 147 write_line("\t.globl "); | 138{ 139 write_line("\t.globl "); |
148 name.push_to_buffer(buffer); | 140 push_string(buffer, name); |
149 buffer.push_back('\n'); | 141 buffer.push_back('\n'); |
150 name.push_to_buffer(buffer); | 142 push_string(buffer, name); |
151 buffer.push_back(':'); 152 buffer.push_back('\n'); 153 buffer.push_back('_'); | 143 buffer.push_back(':'); 144 buffer.push_back('\n'); 145 buffer.push_back('_'); |
154 name.push_to_buffer(buffer); | 146 push_string(buffer, name); |
155 buffer.push_back(':'); 156 buffer.push_back('\n'); 157 158} 159 160void | 147 buffer.push_back(':'); 148 buffer.push_back('\n'); 149 150} 151 152void |
161asm_writer::write_comment(string name) | 153asm_writer::write_comment(const string &name) |
162{ 163 write_line("\t/* "); | 154{ 155 write_line("\t/* "); |
164 name.push_to_buffer(buffer); | 156 push_string(buffer, name); |
165 write_string(" */\n"); 166} 167 168void | 157 write_string(" */\n"); 158} 159 160void |
169asm_writer::write_string(string name) | 161asm_writer::write_string(const char *c) |
170{ | 162{ |
163 while (*c) 164 { 165 buffer.push_back((uint8_t)*(c++)); 166 } 167} 168 169 170void 171asm_writer::write_string(const string &name) 172{ |
|
171 write_line("\t.string \""); | 173 write_line("\t.string \""); |
172 name.push_to_buffer(buffer); | 174 push_string(buffer, name); |
173 write_line("\"\n"); 174 bytes_written += name.size() + 1; 175} 176 177void 178asm_writer::write_data(uint8_t v) 179{ 180 write_byte(v); --- 45 unchanged lines hidden (view full) --- 226asm_writer::size() 227{ 228 return bytes_written; 229} 230 231void 232header::write(output_writer &out) 233{ | 175 write_line("\"\n"); 176 bytes_written += name.size() + 1; 177} 178 179void 180asm_writer::write_data(uint8_t v) 181{ 182 write_byte(v); --- 45 unchanged lines hidden (view full) --- 228asm_writer::size() 229{ 230 return bytes_written; 231} 232 233void 234header::write(output_writer &out) 235{ |
234 out.write_label(string("dt_blob_start")); 235 out.write_label(string("dt_header")); | 236 out.write_label("dt_blob_start"); 237 out.write_label("dt_header"); |
236 out.write_comment("magic"); 237 out.write_data(magic); 238 out.write_comment("totalsize"); 239 out.write_data(totalsize); 240 out.write_comment("off_dt_struct"); 241 out.write_data(off_dt_struct); 242 out.write_comment("off_dt_strings"); 243 out.write_data(off_dt_strings); --- 26 unchanged lines hidden (view full) --- 270 input.consume_binary(off_mem_rsvmap) && 271 input.consume_binary(version) && 272 input.consume_binary(last_comp_version) && 273 input.consume_binary(boot_cpuid_phys) && 274 input.consume_binary(size_dt_strings) && 275 input.consume_binary(size_dt_struct); 276} 277uint32_t | 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); --- 26 unchanged lines hidden (view full) --- 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); 278} 279uint32_t |
278string_table::add_string(string str) | 280string_table::add_string(const string &str) |
279{ 280 auto old = string_offsets.find(str); 281 if (old == string_offsets.end()) 282 { 283 uint32_t start = size; 284 // Don't forget the trailing nul 285 size += str.size() + 1; 286 string_offsets.insert(std::make_pair(str, start)); --- 4 unchanged lines hidden (view full) --- 291 { 292 return old->second; 293 } 294} 295 296void 297string_table::write(dtb::output_writer &writer) 298{ | 281{ 282 auto old = string_offsets.find(str); 283 if (old == string_offsets.end()) 284 { 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)); --- 4 unchanged lines hidden (view full) --- 293 { 294 return old->second; 295 } 296} 297 298void 299string_table::write(dtb::output_writer &writer) 300{ |
299 writer.write_comment(string("Strings table.")); 300 writer.write_label(string("dt_strings_start")); | 301 writer.write_comment("Strings table."); 302 writer.write_label("dt_strings_start"); |
301 for (auto &i : strings) 302 { 303 writer.write_string(i); 304 } | 303 for (auto &i : strings) 304 { 305 writer.write_string(i); 306 } |
305 writer.write_label(string("dt_strings_end")); | 307 writer.write_label("dt_strings_end"); |
306} 307 308} // namespace dtb 309 310} // namespace dtc 311 | 308} 309 310} // namespace dtb 311 312} // namespace dtc 313 |