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
35 #define __STDC_LIMIT_MACROS 1
50 #include <sys/types.h>
63 property_value::get_as_uint32()
65 if (byte_data.size() != 4)
70 v &= byte_data[0] << 24;
71 v &= byte_data[1] << 16;
72 v &= byte_data[2] << 8;
73 v &= byte_data[3] << 0;
78 property_value::push_to_buffer(byte_buffer &buffer)
80 if (!byte_data.empty())
82 buffer.insert(buffer.end(), byte_data.begin(), byte_data.end());
86 push_string(buffer, string_data, true);
93 property_value::write_dts(FILE *file)
99 assert(0 && "Invalid type");
102 case CROSS_REFERENCE:
103 write_as_string(file);
106 write_as_cells(file);
109 if (byte_data.size() % 4 == 0)
111 write_as_cells(file);
114 write_as_bytes(file);
120 property_value::resolve_type()
126 if (byte_data.empty())
131 if (byte_data.back() == 0)
133 bool is_all_printable = true;
136 bool lastWasNull = false;
137 for (auto i : byte_data)
140 is_all_printable &= (i == '\0') || isprint(i);
143 // If there are two nulls in a row, then we're probably binary.
156 if (!is_all_printable)
161 if ((is_all_printable && (bytes > nuls)) || bytes == 0)
175 property_value::size()
177 if (!byte_data.empty())
179 return byte_data.size();
181 return string_data.size() + 1;
185 property_value::write_as_string(FILE *file)
188 if (byte_data.empty())
190 fputs(string_data.c_str(), file);
194 bool hasNull = (byte_data.back() == '\0');
195 // Remove trailing null bytes from the string before printing as dts.
198 byte_data.pop_back();
200 for (auto i : byte_data)
202 // FIXME Escape tabs, newlines, and so on.
205 fputs("\", \"", file);
212 byte_data.push_back('\0');
219 property_value::write_as_cells(FILE *file)
222 assert((byte_data.size() % 4) == 0);
223 for (auto i=byte_data.begin(), e=byte_data.end(); i!=e ; ++i)
233 fprintf(file, "0x%" PRIx32, v);
243 property_value::write_as_bytes(FILE *file)
246 for (auto i=byte_data.begin(), e=byte_data.end(); i!=e ; i++)
248 fprintf(file, "%02hhx", *i);
258 property::parse_string(text_input_buffer &input)
261 assert(*input == '"');
263 std::vector<char> bytes;
264 bool isEscaped = false;
265 while (char c = *input)
267 if (c == '"' && !isEscaped)
272 isEscaped = (c == '\\');
276 v.string_data = string(bytes.begin(), bytes.end());
281 property::parse_cells(text_input_buffer &input, int cell_size)
283 assert(*input == '<');
287 while (!input.consume('>'))
290 // If this is a phandle then we need to get the name of the
292 if (input.consume('&'))
296 input.parse_error("reference only permitted in 32-bit arrays");
302 if (!input.consume('{'))
304 referenced = input.parse_node_name();
308 referenced = input.parse_to('}');
311 if (referenced.empty())
313 input.parse_error("Expected node name");
318 // If we already have some bytes, make the phandle a
319 // separate component.
320 if (!v.byte_data.empty())
323 v = property_value();
325 v.string_data = referenced;
326 v.type = property_value::PHANDLE;
328 v = property_value();
332 //FIXME: We should support labels in the middle
333 //of these, but we don't.
334 unsigned long long val;
335 if (!input.consume_integer_expression(val))
337 input.parse_error("Expected numbers in array of cells");
344 v.byte_data.push_back(val);
347 push_big_endian(v.byte_data, (uint16_t)val);
350 push_big_endian(v.byte_data, (uint32_t)val);
353 push_big_endian(v.byte_data, (uint64_t)val);
356 assert(0 && "Invalid cell size!");
361 // Don't store an empty string value here.
362 if (v.byte_data.size() > 0)
369 property::parse_bytes(text_input_buffer &input)
371 assert(*input == '[');
375 while (!input.consume(']'))
378 //FIXME: We should support
379 //labels in the middle of
380 //these, but we don't.
382 if (!input.consume_hex_byte(val))
384 input.parse_error("Expected hex bytes in array of bytes");
388 v.byte_data.push_back(val);
396 property::parse_reference(text_input_buffer &input)
398 assert(*input == '&');
402 v.string_data = input.parse_node_name();
403 if (v.string_data.empty())
405 input.parse_error("Expected node name");
409 v.type = property_value::CROSS_REFERENCE;
413 property::property(input_buffer &structs, input_buffer &strings)
415 uint32_t name_offset;
417 valid = structs.consume_binary(length) &&
418 structs.consume_binary(name_offset);
421 fprintf(stderr, "Failed to read property\n");
425 input_buffer name_buffer = strings.buffer_from_offset(name_offset);
426 if (name_buffer.finished())
428 fprintf(stderr, "Property name offset %" PRIu32
429 " is past the end of the strings table\n",
434 key = name_buffer.parse_to(0);
436 // If we're empty, do not push anything as value.
443 for (uint32_t i=0 ; i<length ; i++)
445 if (!(valid = structs.consume_binary(byte)))
447 fprintf(stderr, "Failed to read property value\n");
450 v.byte_data.push_back(byte);
455 void property::parse_define(text_input_buffer &input, define_map *defines)
460 input.parse_error("No predefined properties to match name\n");
464 string name = input.parse_property_name();
465 define_map::iterator found;
466 if ((name == string()) ||
467 ((found = defines->find(name)) == defines->end()))
469 input.parse_error("Undefined property name\n");
473 values.push_back((*found).second->values[0]);
476 property::property(text_input_buffer &input,
479 bool semicolonTerminated,
480 define_map *defines) : key(k), labels(l), valid(true)
488 parse_define(input, defines);
495 input.parse_error("Invalid property value.");
500 if (input.consume("/incbin/(\""))
502 auto loc = input.location();
503 std::string filename = input.parse_to('"');
504 if (!(valid = input.consume('"')))
506 loc.report_error("Syntax error, expected '\"' to terminate /incbin/(");
510 if (!(valid = input.read_binary_file(filename, v.byte_data)))
512 input.parse_error("Cannot open binary include file");
515 if (!(valid &= input.consume(')')))
517 input.parse_error("Syntax error, expected ')' to terminate /incbin/(");
523 unsigned long long bits = 0;
524 valid = input.consume("/bits/");
526 valid &= input.consume_integer(bits);
531 input.parse_error("Invalid size for elements");
538 input.parse_error("/bits/ directive is only valid on arrays");
542 parse_cells(input, bits);
549 parse_cells(input, 32);
555 parse_reference(input);
563 } while (input.consume(','));
564 if (semicolonTerminated && !input.consume(';'))
566 input.parse_error("Expected ; at end of property");
572 property::parse_dtb(input_buffer &structs, input_buffer &strings)
574 property_ptr p(new property(structs, strings));
583 property::parse(text_input_buffer &input, string &&key, string_set &&label,
584 bool semicolonTerminated, define_map *defines)
586 property_ptr p(new property(input,
599 property::write(dtb::output_writer &writer, dtb::string_table &strings)
601 writer.write_token(dtb::FDT_PROP);
602 byte_buffer value_buffer;
603 for (value_iterator i=begin(), e=end() ; i!=e ; ++i)
605 i->push_to_buffer(value_buffer);
607 writer.write_data((uint32_t)value_buffer.size());
608 writer.write_comment(key);
609 writer.write_data(strings.add_string(key));
610 writer.write_data(value_buffer);
614 property_value::try_to_merge(property_value &other)
620 __builtin_unreachable();
627 case CROSS_REFERENCE:
631 if (other.type == PHANDLE || other.type == BINARY)
634 byte_data.insert(byte_data.end(), other.byte_data.begin(),
635 other.byte_data.end());
643 property::write_dts(FILE *file, int indent)
645 for (int i=0 ; i<indent ; i++)
650 for (auto &l : labels)
652 fputs(l.c_str(), file);
658 fputs(key.c_str(), file);
662 std::vector<property_value> *vals = &values;
663 std::vector<property_value> v;
664 // If we've got multiple values then try to merge them all together.
665 if (values.size() > 1)
668 v.push_back(values.front());
669 for (auto i=(++begin()), e=end() ; i!=e ; ++i)
671 if (!v.back().try_to_merge(*i))
678 for (auto i=vals->begin(), e=vals->end() ; i!=e ; ++i)
692 property::offset_of_value(property_value &val)
695 for (auto &v : values)
707 node::parse_name(text_input_buffer &input, bool &is_property, const char *error)
716 return input.parse_property_name();
718 string n = input.parse_node_or_property_name(is_property);
723 input.parse_error(error);
731 node::visit(std::function<void(node&)> fn)
734 for (auto &&c : children)
740 node::node(input_buffer &structs, input_buffer &strings) : valid(true)
742 std::vector<char> bytes;
743 while (structs[0] != '\0' && structs[0] != '@')
745 bytes.push_back(structs[0]);
748 name = string(bytes.begin(), bytes.end());
750 if (structs[0] == '@')
753 while (structs[0] != '\0')
755 bytes.push_back(structs[0]);
758 unit_address = string(bytes.begin(), bytes.end());
762 while (structs.consume_binary(token))
767 fprintf(stderr, "Unexpected token 0x%" PRIx32
768 " while parsing node.\n", token);
771 // Child node, parse it.
772 case dtb::FDT_BEGIN_NODE:
774 node_ptr child = node::parse_dtb(structs, strings);
780 children.push_back(std::move(child));
783 // End of this node, no errors.
784 case dtb::FDT_END_NODE:
786 // Property, parse it.
789 property_ptr prop = property::parse_dtb(structs, strings);
795 props.push_back(prop);
799 // End of structs table. Should appear after
800 // the end of the last node.
802 fprintf(stderr, "Unexpected FDT_END token while parsing node.\n");
805 // NOPs are padding. Ignore them.
810 fprintf(stderr, "Failed to read token from structs table while parsing node.\n");
816 node::node(const string &n,
817 const std::vector<property_ptr> &p)
820 props.insert(props.begin(), p.begin(), p.end());
823 node_ptr node::create_special_node(const string &name,
824 const std::vector<property_ptr> &props)
826 node_ptr n(new node(name, props));
830 node::node(text_input_buffer &input,
832 std::unordered_set<string> &&l,
835 : labels(l), name(n), unit_address(a), valid(true)
837 if (!input.consume('{'))
839 input.parse_error("Expected { to start new device tree node.\n");
842 while (valid && !input.consume('}'))
844 // flag set if we find any characters that are only in
845 // the property name character set, not the node
846 bool is_property = false;
847 string child_name, child_address;
848 std::unordered_set<string> child_labels;
849 auto parse_delete = [&](const char *expected, bool at)
851 if (child_name == string())
853 input.parse_error(expected);
858 if (at && input.consume('@'))
861 child_name += parse_name(input, is_property, "Expected unit address");
863 if (!input.consume(';'))
865 input.parse_error("Expected semicolon");
871 if (input.consume("/delete-node/"))
874 child_name = input.parse_node_name();
875 parse_delete("Expected node name", true);
878 deleted_children.insert(child_name);
882 if (input.consume("/delete-property/"))
885 child_name = input.parse_property_name();
886 parse_delete("Expected property name", false);
889 deleted_props.insert(child_name);
893 child_name = parse_name(input, is_property,
894 "Expected property or node name");
895 while (input.consume(':'))
897 // Node labels can contain any characters? The
898 // spec doesn't say, so we guess so...
900 child_labels.insert(std::move(child_name));
901 child_name = parse_name(input, is_property, "Expected property or node name");
903 if (input.consume('@'))
905 child_address = parse_name(input, is_property, "Expected unit address");
912 // If we're parsing a property, then we must actually do that.
913 if (input.consume('='))
915 property_ptr p = property::parse(input, std::move(child_name),
916 std::move(child_labels), true, defines);
926 else if (!is_property && *input == ('{'))
928 node_ptr child = node::parse(input, std::move(child_name),
929 std::move(child_labels), std::move(child_address), defines);
932 children.push_back(std::move(child));
939 else if (input.consume(';'))
941 props.push_back(property_ptr(new property(std::move(child_name), std::move(child_labels))));
945 input.parse_error("Error parsing property. Expected property value");
955 node::cmp_properties(property_ptr &p1, property_ptr &p2)
957 return p1->get_key() < p2->get_key();
961 node::cmp_children(node_ptr &c1, node_ptr &c2)
963 if (c1->name == c2->name)
965 return c1->unit_address < c2->unit_address;
967 return c1->name < c2->name;
973 std::sort(property_begin(), property_end(), cmp_properties);
974 std::sort(child_begin(), child_end(), cmp_children);
975 for (auto &c : child_nodes())
982 node::parse(text_input_buffer &input,
988 node_ptr n(new node(input,
1001 node::parse_dtb(input_buffer &structs, input_buffer &strings)
1003 node_ptr n(new node(structs, strings));
1012 node::get_property(const string &key)
1014 for (auto &i : props)
1016 if (i->get_key() == key)
1025 node::merge_node(node_ptr &other)
1027 for (auto &l : other->labels)
1031 // Note: this is an O(n*m) operation. It might be sensible to
1032 // optimise this if we find that there are nodes with very
1033 // large numbers of properties, but for typical usage the
1034 // entire vector will fit (easily) into cache, so iterating
1035 // over it repeatedly isn't that expensive.
1036 for (auto &p : other->properties())
1039 for (auto &mp : properties())
1041 if (mp->get_key() == p->get_key())
1053 for (auto &c : other->children)
1056 for (auto &i : children)
1058 if (i->name == c->name && i->unit_address == c->unit_address)
1067 children.push_back(std::move(c));
1070 children.erase(std::remove_if(children.begin(), children.end(),
1071 [&](const node_ptr &p) {
1072 string full_name = p->name;
1073 if (p->unit_address != string())
1076 full_name += p->unit_address;
1078 if (other->deleted_children.count(full_name) > 0)
1080 other->deleted_children.erase(full_name);
1084 }), children.end());
1085 props.erase(std::remove_if(props.begin(), props.end(),
1086 [&](const property_ptr &p) {
1087 if (other->deleted_props.count(p->get_key()) > 0)
1089 other->deleted_props.erase(p->get_key());
1097 node::write(dtb::output_writer &writer, dtb::string_table &strings)
1099 writer.write_token(dtb::FDT_BEGIN_NODE);
1100 byte_buffer name_buffer;
1101 push_string(name_buffer, name);
1102 if (unit_address != string())
1104 name_buffer.push_back('@');
1105 push_string(name_buffer, unit_address);
1107 writer.write_comment(name);
1108 writer.write_data(name_buffer);
1109 writer.write_data((uint8_t)0);
1110 for (auto p : properties())
1112 p->write(writer, strings);
1114 for (auto &c : child_nodes())
1116 c->write(writer, strings);
1118 writer.write_token(dtb::FDT_END_NODE);
1122 node::write_dts(FILE *file, int indent)
1124 for (int i=0 ; i<indent ; i++)
1129 for (auto &label : labels)
1131 fprintf(file, "%s: ", label.c_str());
1134 if (name != string())
1136 fputs(name.c_str(), file);
1138 if (unit_address != string())
1141 fputs(unit_address.c_str(), file);
1143 fputs(" {\n\n", file);
1144 for (auto p : properties())
1146 p->write_dts(file, indent+1);
1148 for (auto &c : child_nodes())
1150 c->write_dts(file, indent+1);
1152 for (int i=0 ; i<indent ; i++)
1156 fputs("};\n", file);
1160 device_tree::collect_names_recursive(node_ptr &n, node_path &path)
1162 path.push_back(std::make_pair(n->name, n->unit_address));
1163 for (const string &name : n->labels)
1165 if (name != string())
1167 auto iter = node_names.find(name);
1168 if (iter == node_names.end())
1170 node_names.insert(std::make_pair(name, n.get()));
1171 node_paths.insert(std::make_pair(name, path));
1175 node_names.erase(iter);
1176 auto i = node_paths.find(name);
1177 if (i != node_paths.end())
1179 node_paths.erase(name);
1181 fprintf(stderr, "Label not unique: %s. References to this label will not be resolved.\n", name.c_str());
1185 for (auto &c : n->child_nodes())
1187 collect_names_recursive(c, path);
1189 // Now we collect the phandles and properties that reference
1191 for (auto &p : n->properties())
1197 fixups.push_back({path, p, v});
1199 if (v.is_cross_reference())
1201 cross_references.push_back(&v);
1204 if ((p->get_key() == "phandle") ||
1205 (p->get_key() == "linux,phandle"))
1207 if (p->begin()->byte_data.size() != 4)
1209 fprintf(stderr, "Invalid phandle value for node %s. Should be a 4-byte value.\n", n->name.c_str());
1214 uint32_t phandle = p->begin()->get_as_uint32();
1215 used_phandles.insert(std::make_pair(phandle, n.get()));
1223 device_tree::collect_names()
1228 cross_references.clear();
1230 collect_names_recursive(root, p);
1234 device_tree::assign_phandle(node *n, uint32_t &phandle)
1236 // If there is an existing phandle, use it
1237 property_ptr p = n->get_property("phandle");
1240 p = n->get_property("linux,phandle");
1244 // Otherwise insert a new phandle node
1246 while (used_phandles.find(phandle) != used_phandles.end())
1248 // Note that we only don't need to
1249 // store this phandle in the set,
1250 // because we are monotonically
1251 // increasing the value of phandle and
1252 // so will only ever revisit this value
1253 // if we have used 2^32 phandles, at
1254 // which point our blob won't fit in
1255 // any 32-bit system and we've done
1256 // something badly wrong elsewhere
1260 push_big_endian(v.byte_data, phandle++);
1261 if (phandle_node_name == BOTH || phandle_node_name == LINUX)
1263 p.reset(new property("linux,phandle"));
1267 if (phandle_node_name == BOTH || phandle_node_name == EPAPR)
1269 p.reset(new property("phandle"));
1279 device_tree::assign_phandles(node_ptr &n, uint32_t &next)
1281 if (!n->labels.empty())
1283 assign_phandle(n.get(), next);
1286 for (auto &c : n->child_nodes())
1288 assign_phandles(c, next);
1293 device_tree::resolve_cross_references(uint32_t &phandle)
1295 for (auto *pv : cross_references)
1297 node_path path = node_paths[pv->string_data];
1298 auto p = path.begin();
1299 auto pe = path.end();
1302 // Skip the first name in the path. It's always "", and implicitly /
1303 for (++p ; p!=pe ; ++p)
1305 pv->byte_data.push_back('/');
1306 push_string(pv->byte_data, p->first);
1307 if (!(p->second.empty()))
1309 pv->byte_data.push_back('@');
1310 push_string(pv->byte_data, p->second);
1313 pv->byte_data.push_back(0);
1316 std::unordered_map<property_value*, fixup&> phandle_set;
1317 for (auto &i : fixups)
1319 phandle_set.insert({&i.val, i});
1321 std::vector<std::reference_wrapper<fixup>> sorted_phandles;
1322 root->visit([&](node &n) {
1323 for (auto &p : n.properties())
1327 auto i = phandle_set.find(&v);
1328 if (i != phandle_set.end())
1330 sorted_phandles.push_back(i->second);
1335 assert(sorted_phandles.size() == fixups.size());
1337 for (auto &i : sorted_phandles)
1339 string target_name = i.get().val.string_data;
1340 node *target = nullptr;
1342 // If the node name is a path, then look it up by following the path,
1343 // otherwise jump directly to the named node.
1344 if (target_name[0] == '/')
1347 target = root.get();
1348 std::istringstream ss(target_name);
1349 string path_element;
1350 // Read the leading /
1351 std::getline(ss, path_element, '/');
1352 // Iterate over path elements
1356 std::getline(ss, path_element, '/');
1357 std::istringstream nss(path_element);
1358 string node_name, node_address;
1359 std::getline(nss, node_name, '@');
1360 std::getline(nss, node_address, '@');
1361 node *next = nullptr;
1362 for (auto &c : target->child_nodes())
1364 if (c->name == node_name)
1366 if (c->unit_address == node_address)
1373 possible = path + c->name;
1374 if (c->unit_address != string())
1377 possible += c->unit_address;
1383 if (node_address != string())
1386 path += node_address;
1389 if (target == nullptr)
1397 target = node_names[target_name];
1399 if (target == nullptr)
1403 unresolved_fixups.push_back(i);
1408 fprintf(stderr, "Failed to find node with label: %s\n", target_name.c_str());
1409 if (possible != string())
1411 fprintf(stderr, "Possible intended match: %s\n", possible.c_str());
1417 // If there is an existing phandle, use it
1418 property_ptr p = assign_phandle(target, phandle);
1419 p->begin()->push_to_buffer(i.get().val.byte_data);
1420 assert(i.get().val.byte_data.size() == 4);
1426 device_tree::parse_file(text_input_buffer &input,
1427 std::vector<node_ptr> &roots,
1432 if (input.consume("/dts-v1/;"))
1437 if (input.consume("/plugin/;"))
1444 input.parse_error("Expected /dts-v1/; version string");
1446 // Read any memory reservations
1447 while (input.consume("/memreserve/"))
1449 unsigned long long start, len;
1451 // Read the start and length.
1452 if (!(input.consume_integer_expression(start) &&
1453 (input.next_token(),
1454 input.consume_integer_expression(len))))
1456 input.parse_error("Expected size on /memreserve/ node.");
1460 reservations.push_back(reservation(start, len));
1463 while (valid && !input.finished())
1466 if (input.consume('/'))
1469 n = node::parse(input, string(), string_set(), string(), &defines);
1471 else if (input.consume('&'))
1474 string name = input.parse_node_name();
1476 n = node::parse(input, std::move(name), string_set(), string(), &defines);
1480 input.parse_error("Failed to find root node /.");
1484 roots.push_back(std::move(n));
1494 template<class writer> void
1495 device_tree::write(int fd)
1497 dtb::string_table st;
1500 writer reservation_writer;
1501 writer struct_writer;
1502 writer strings_writer;
1504 // Build the reservation table
1505 reservation_writer.write_comment(string("Memory reservations"));
1506 reservation_writer.write_label(string("dt_reserve_map"));
1507 for (auto &i : reservations)
1509 reservation_writer.write_comment(string("Reservation start"));
1510 reservation_writer.write_data(i.first);
1511 reservation_writer.write_comment(string("Reservation length"));
1512 reservation_writer.write_data(i.first);
1514 // Write n spare reserve map entries, plus the trailing 0.
1515 for (uint32_t i=0 ; i<=spare_reserve_map_entries ; i++)
1517 reservation_writer.write_data((uint64_t)0);
1518 reservation_writer.write_data((uint64_t)0);
1522 struct_writer.write_comment(string("Device tree"));
1523 struct_writer.write_label(string("dt_struct_start"));
1524 root->write(struct_writer, st);
1525 struct_writer.write_token(dtb::FDT_END);
1526 struct_writer.write_label(string("dt_struct_end"));
1528 st.write(strings_writer);
1529 // Find the strings size before we stick padding on the end.
1530 // Note: We should possibly use a new writer for the padding.
1531 head.size_dt_strings = strings_writer.size();
1533 // Stick the padding in the strings writer, but after the
1534 // marker indicating that it's the end.
1535 // Note: We probably should add a padding call to the writer so
1536 // that the asm back end can write padding directives instead
1537 // of a load of 0 bytes.
1538 for (uint32_t i=0 ; i<blob_padding ; i++)
1540 strings_writer.write_data((uint8_t)0);
1542 head.totalsize = sizeof(head) + strings_writer.size() +
1543 struct_writer.size() + reservation_writer.size();
1544 while (head.totalsize < minimum_blob_size)
1547 strings_writer.write_data((uint8_t)0);
1549 head.off_dt_struct = sizeof(head) + reservation_writer.size();;
1550 head.off_dt_strings = head.off_dt_struct + struct_writer.size();
1551 head.off_mem_rsvmap = sizeof(head);
1552 head.boot_cpuid_phys = boot_cpu;
1553 head.size_dt_struct = struct_writer.size();
1554 head.write(head_writer);
1556 head_writer.write_to_file(fd);
1557 reservation_writer.write_to_file(fd);
1558 struct_writer.write_to_file(fd);
1559 strings_writer.write_label(string("dt_blob_end"));
1560 strings_writer.write_to_file(fd);
1564 device_tree::referenced_node(property_value &v)
1568 return node_names[v.string_data];
1572 return used_phandles[v.get_as_uint32()];
1578 device_tree::write_binary(int fd)
1580 write<dtb::binary_writer>(fd);
1584 device_tree::write_asm(int fd)
1586 write<dtb::asm_writer>(fd);
1590 device_tree::write_dts(int fd)
1592 FILE *file = fdopen(fd, "w");
1593 fputs("/dts-v1/;\n\n", file);
1595 if (!reservations.empty())
1597 const char msg[] = "/memreserve/";
1598 fwrite(msg, sizeof(msg), 1, file);
1599 for (auto &i : reservations)
1601 fprintf(file, " %" PRIx64 " %" PRIx64, i.first, i.second);
1603 fputs(";\n\n", file);
1607 root->write_dts(file, 0);
1612 device_tree::parse_dtb(const string &fn, FILE *)
1614 auto in = input_buffer::buffer_for_file(fn);
1620 input_buffer &input = *in;
1622 valid = h.read_dtb(input);
1623 boot_cpu = h.boot_cpuid_phys;
1624 if (h.last_comp_version > 17)
1626 fprintf(stderr, "Don't know how to read this version of the device tree blob");
1633 input_buffer reservation_map =
1634 input.buffer_from_offset(h.off_mem_rsvmap, 0);
1635 uint64_t start, length;
1638 if (!(reservation_map.consume_binary(start) &&
1639 reservation_map.consume_binary(length)))
1641 fprintf(stderr, "Failed to read memory reservation table\n");
1645 } while (!((start == 0) && (length == 0)));
1646 input_buffer struct_table =
1647 input.buffer_from_offset(h.off_dt_struct, h.size_dt_struct);
1648 input_buffer strings_table =
1649 input.buffer_from_offset(h.off_dt_strings, h.size_dt_strings);
1651 if (!(struct_table.consume_binary(token) &&
1652 (token == dtb::FDT_BEGIN_NODE)))
1654 fprintf(stderr, "Expected FDT_BEGIN_NODE token.\n");
1658 root = node::parse_dtb(struct_table, strings_table);
1659 if (!(struct_table.consume_binary(token) && (token == dtb::FDT_END)))
1661 fprintf(stderr, "Expected FDT_END token after parsing root node.\n");
1665 valid = (root != 0);
1669 device_tree::node_path::to_string() const
1674 if ((p == pe) || (p+1 == pe))
1678 // Skip the first name in the path. It's always "", and implicitly /
1679 for (++p ; p!=pe ; ++p)
1683 if (!(p->second.empty()))
1693 device_tree::create_fragment_wrapper(node_ptr &node, int &fragnum)
1695 // In a plugin, we can massage these non-/ root nodes into into a fragment
1696 std::string fragment_address = "fragment@" + std::to_string(fragnum);
1699 std::vector<property_ptr> symbols;
1701 // Intentionally left empty
1702 node_ptr newroot = node::create_special_node("", symbols);
1703 node_ptr wrapper = node::create_special_node("__overlay__", symbols);
1705 // Generate the fragment with target = <&name>
1707 v.string_data = node->name;
1708 v.type = property_value::PHANDLE;
1709 auto prop = std::make_shared<property>(std::string("target"));
1711 symbols.push_back(prop);
1713 node_ptr fragment = node::create_special_node(fragment_address, symbols);
1715 wrapper->merge_node(node);
1716 fragment->add_child(std::move(wrapper));
1717 newroot->add_child(std::move(fragment));
1722 device_tree::generate_root(node_ptr &node, int &fragnum)
1725 string name = node->name;
1726 if (name == string())
1728 return std::move(node);
1730 else if (!is_plugin)
1735 return create_fragment_wrapper(node, fragnum);
1739 device_tree::reassign_fragment_numbers(node_ptr &node, int &delta)
1742 for (auto &c : node->child_nodes())
1744 if (c->name == std::string("fragment"))
1746 int current_address = std::stoi(c->unit_address, nullptr, 16);
1747 std::ostringstream new_address;
1748 current_address += delta;
1749 // It's possible that we hopped more than one somewhere, so just reset
1750 // delta to the next in sequence.
1751 delta = current_address + 1;
1752 new_address << std::hex << current_address;
1753 c->unit_address = new_address.str();
1759 device_tree::parse_dts(const string &fn, FILE *depfile)
1761 auto in = input_buffer::buffer_for_file(fn);
1767 std::vector<node_ptr> roots;
1768 std::unordered_set<string> defnames;
1769 for (auto &i : defines)
1771 defnames.insert(i.first);
1773 text_input_buffer input(std::move(in),
1774 std::move(defnames),
1775 std::vector<string>(include_paths),
1778 bool read_header = false;
1780 parse_file(input, roots, read_header);
1781 switch (roots.size())
1785 input.parse_error("Failed to find root node /.");
1788 root = generate_root(roots[0], fragnum);
1792 input.parse_error("Failed to find root node /.");
1798 root = generate_root(roots[0], fragnum);
1802 input.parse_error("Failed to find root node /.");
1805 for (auto i=++(roots.begin()), e=roots.end() ; i!=e ; ++i)
1808 string name = node->name;
1809 if (name == string())
1813 // Re-assign any fragment numbers based on a delta of
1814 // fragnum before we merge it
1815 reassign_fragment_numbers(node, fragnum);
1817 root->merge_node(node);
1821 auto existing = node_names.find(name);
1822 if (existing == node_names.end())
1825 existing = node_names.find(name);
1827 if (existing == node_names.end())
1831 auto fragment = create_fragment_wrapper(node, fragnum);
1832 root->merge_node(fragment);
1836 fprintf(stderr, "Unable to merge node: %s\n", name.c_str());
1841 existing->second->merge_node(node);
1848 uint32_t phandle = 1;
1849 // If we're writing symbols, go ahead and assign phandles to the entire
1850 // tree. We'll do this before we resolve cross references, just to keep
1851 // order semi-predictable and stable.
1854 assign_phandles(root, phandle);
1856 resolve_cross_references(phandle);
1859 std::vector<property_ptr> symbols;
1860 // Create a symbol table. Each label in this device tree may be
1861 // referenced by other plugins, so we create a __symbols__ node inside
1862 // the root that contains mappings (properties) from label names to
1864 for (auto &s : node_paths)
1867 v.string_data = s.second.to_string();
1868 v.type = property_value::STRING;
1869 string name = s.first;
1870 auto prop = std::make_shared<property>(std::move(name));
1872 symbols.push_back(prop);
1874 root->add_child(node::create_special_node("__symbols__", symbols));
1875 // If this is a plugin, then we also need to create two extra nodes.
1876 // Internal phandles will need to be renumbered to avoid conflicts with
1877 // already-loaded nodes and external references will need to be
1881 // Create the fixups entry. This is of the form:
1882 // {target} = {path}:{property name}:{offset}
1883 auto create_fixup_entry = [&](fixup &i, string target)
1885 string value = i.path.to_string();
1887 value += i.prop->get_key();
1889 value += std::to_string(i.prop->offset_of_value(i.val));
1891 v.string_data = value;
1892 v.type = property_value::STRING;
1893 auto prop = std::make_shared<property>(std::move(target));
1897 // If we have any unresolved phandle references in this plugin,
1898 // then we must update them to 0xdeadbeef and leave a property in
1899 // the /__fixups__ node whose key is the label and whose value is
1900 // as described above.
1901 if (!unresolved_fixups.empty())
1904 for (auto &i : unresolved_fixups)
1906 auto &val = i.get().val;
1907 symbols.push_back(create_fixup_entry(i, val.string_data));
1908 val.byte_data.push_back(0xde);
1909 val.byte_data.push_back(0xad);
1910 val.byte_data.push_back(0xbe);
1911 val.byte_data.push_back(0xef);
1912 val.type = property_value::BINARY;
1914 root->add_child(node::create_special_node("__fixups__", symbols));
1917 // If we have any resolved phandle references in this plugin, then
1918 // we must create a child in the __local_fixups__ node whose path
1919 // matches the node path from the root and whose value contains the
1920 // location of the reference within a property.
1922 // Create a local_fixups node that is initially empty.
1923 node_ptr local_fixups = node::create_special_node("__local_fixups__", symbols);
1924 for (auto &i : fixups)
1926 if (!i.val.is_phandle())
1930 node *n = local_fixups.get();
1931 for (auto &p : i.path)
1933 // Skip the implicit root
1934 if (p.first.empty())
1939 for (auto &c : n->child_nodes())
1941 if (c->name == p.first)
1950 n->add_child(node::create_special_node(p.first, symbols));
1951 n = (--n->child_end())->get();
1956 push_big_endian(pv.byte_data, static_cast<uint32_t>(i.prop->offset_of_value(i.val)));
1957 pv.type = property_value::BINARY;
1958 auto key = i.prop->get_key();
1959 property_ptr prop = n->get_property(key);
1960 // If we don't have an existing property then create one and
1961 // use this property value
1964 prop = std::make_shared<property>(std::move(key));
1965 n->add_property(prop);
1966 prop->add_value(pv);
1970 // If we do have an existing property value, try to append
1972 property_value &old_val = *(--prop->end());
1973 if (!old_val.try_to_merge(pv))
1975 prop->add_value(pv);
1979 // We've iterated over all fixups, but only emit the
1980 // __local_fixups__ if we found some that were resolved internally.
1981 if (local_fixups->child_begin() != local_fixups->child_end())
1983 root->add_child(std::move(local_fixups));
1989 bool device_tree::parse_define(const char *def)
1991 const char *val = strchr(def, '=');
1994 if (strlen(def) != 0)
2002 string name(def, val-def);
2003 string name_copy = name;
2005 std::unique_ptr<input_buffer> raw(new input_buffer(val, strlen(val)));
2006 text_input_buffer in(std::move(raw),
2007 std::unordered_set<string>(),
2008 std::vector<string>(),
2011 property_ptr p = property::parse(in, std::move(name_copy), string_set(), false);