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
51 #include <sys/types.h>
64 property_value::get_as_uint32()
66 if (byte_data.size() != 4)
71 v &= byte_data[0] << 24;
72 v &= byte_data[1] << 16;
73 v &= byte_data[2] << 8;
74 v &= byte_data[3] << 0;
79 property_value::push_to_buffer(byte_buffer &buffer)
81 if (!byte_data.empty())
83 buffer.insert(buffer.end(), byte_data.begin(), byte_data.end());
87 push_string(buffer, string_data, true);
94 property_value::write_dts(FILE *file)
100 assert(0 && "Invalid type");
103 case CROSS_REFERENCE:
104 write_as_string(file);
107 write_as_cells(file);
110 if (byte_data.size() % 4 == 0)
112 write_as_cells(file);
115 write_as_bytes(file);
121 property_value::resolve_type()
127 if (byte_data.empty())
132 if (byte_data.back() == 0)
134 bool is_all_printable = true;
137 bool lastWasNull = false;
138 for (auto i : byte_data)
141 is_all_printable &= (i == '\0') || isprint(i);
144 // If there are two nulls in a row, then we're probably binary.
157 if (!is_all_printable)
162 if ((is_all_printable && (bytes > nuls)) || bytes == 0)
176 property_value::size()
178 if (!byte_data.empty())
180 return byte_data.size();
182 return string_data.size() + 1;
186 property_value::write_as_string(FILE *file)
189 if (byte_data.empty())
191 fputs(string_data.c_str(), file);
195 bool hasNull = (byte_data.back() == '\0');
196 // Remove trailing null bytes from the string before printing as dts.
199 byte_data.pop_back();
201 for (auto i : byte_data)
203 // FIXME Escape tabs, newlines, and so on.
206 fputs("\", \"", file);
213 byte_data.push_back('\0');
220 property_value::write_as_cells(FILE *file)
223 assert((byte_data.size() % 4) == 0);
224 for (auto i=byte_data.begin(), e=byte_data.end(); i!=e ; ++i)
234 fprintf(file, "0x%" PRIx32, v);
244 property_value::write_as_bytes(FILE *file)
247 for (auto i=byte_data.begin(), e=byte_data.end(); i!=e ; i++)
249 fprintf(file, "%02hhx", *i);
259 property::parse_string(text_input_buffer &input)
262 assert(*input == '"');
264 std::vector<char> bytes;
265 bool isEscaped = false;
266 while (char c = *input)
268 if (c == '"' && !isEscaped)
273 isEscaped = (c == '\\');
277 v.string_data = string(bytes.begin(), bytes.end());
282 property::parse_cells(text_input_buffer &input, int cell_size)
284 assert(*input == '<');
288 while (!input.consume('>'))
291 // If this is a phandle then we need to get the name of the
293 if (input.consume('&'))
297 input.parse_error("reference only permitted in 32-bit arrays");
303 if (!input.consume('{'))
305 referenced = input.parse_node_name();
309 referenced = input.parse_to('}');
312 if (referenced.empty())
314 input.parse_error("Expected node name");
319 // If we already have some bytes, make the phandle a
320 // separate component.
321 if (!v.byte_data.empty())
324 v = property_value();
326 v.string_data = referenced;
327 v.type = property_value::PHANDLE;
329 v = property_value();
333 //FIXME: We should support labels in the middle
334 //of these, but we don't.
335 unsigned long long val;
336 if (!input.consume_integer_expression(val))
338 input.parse_error("Expected numbers in array of cells");
345 v.byte_data.push_back(val);
348 push_big_endian(v.byte_data, (uint16_t)val);
351 push_big_endian(v.byte_data, (uint32_t)val);
354 push_big_endian(v.byte_data, (uint64_t)val);
357 assert(0 && "Invalid cell size!");
362 // Don't store an empty string value here.
363 if (v.byte_data.size() > 0)
370 property::parse_bytes(text_input_buffer &input)
372 assert(*input == '[');
376 while (!input.consume(']'))
379 //FIXME: We should support
380 //labels in the middle of
381 //these, but we don't.
383 if (!input.consume_hex_byte(val))
385 input.parse_error("Expected hex bytes in array of bytes");
389 v.byte_data.push_back(val);
397 property::parse_reference(text_input_buffer &input)
399 assert(*input == '&');
403 v.string_data = input.parse_node_name();
404 if (v.string_data.empty())
406 input.parse_error("Expected node name");
410 v.type = property_value::CROSS_REFERENCE;
414 property::property(input_buffer &structs, input_buffer &strings)
416 uint32_t name_offset;
418 valid = structs.consume_binary(length) &&
419 structs.consume_binary(name_offset);
422 fprintf(stderr, "Failed to read property\n");
426 input_buffer name_buffer = strings.buffer_from_offset(name_offset);
427 if (name_buffer.finished())
429 fprintf(stderr, "Property name offset %" PRIu32
430 " is past the end of the strings table\n",
435 key = name_buffer.parse_to(0);
437 // If we're empty, do not push anything as value.
444 for (uint32_t i=0 ; i<length ; i++)
446 if (!(valid = structs.consume_binary(byte)))
448 fprintf(stderr, "Failed to read property value\n");
451 v.byte_data.push_back(byte);
456 void property::parse_define(text_input_buffer &input, define_map *defines)
461 input.parse_error("No predefined properties to match name\n");
465 string name = input.parse_property_name();
466 define_map::iterator found;
467 if ((name == string()) ||
468 ((found = defines->find(name)) == defines->end()))
470 input.parse_error("Undefined property name\n");
474 values.push_back((*found).second->values[0]);
477 property::property(text_input_buffer &input,
480 bool semicolonTerminated,
481 define_map *defines) : key(k), labels(l), valid(true)
489 parse_define(input, defines);
497 input.parse_error("Invalid property value.");
502 if (input.consume("/incbin/(\""))
504 auto loc = input.location();
505 std::string filename = input.parse_to('"');
506 if (!(valid = input.consume('"')))
508 loc.report_error("Syntax error, expected '\"' to terminate /incbin/(");
512 if (!(valid = input.read_binary_file(filename, v.byte_data)))
514 input.parse_error("Cannot open binary include file");
517 if (!(valid &= input.consume(')')))
519 input.parse_error("Syntax error, expected ')' to terminate /incbin/(");
525 unsigned long long bits = 0;
526 valid = input.consume("/bits/");
528 valid &= input.consume_integer(bits);
533 input.parse_error("Invalid size for elements");
540 input.parse_error("/bits/ directive is only valid on arrays");
544 parse_cells(input, bits);
551 parse_cells(input, 32);
557 parse_reference(input);
565 } while (input.consume(','));
566 if (semicolonTerminated && !input.consume(';'))
568 input.parse_error("Expected ; at end of property");
574 property::parse_dtb(input_buffer &structs, input_buffer &strings)
576 property_ptr p(new property(structs, strings));
585 property::parse(text_input_buffer &input, string &&key, string_set &&label,
586 bool semicolonTerminated, define_map *defines)
588 property_ptr p(new property(input,
601 property::write(dtb::output_writer &writer, dtb::string_table &strings)
603 writer.write_token(dtb::FDT_PROP);
604 byte_buffer value_buffer;
605 for (value_iterator i=begin(), e=end() ; i!=e ; ++i)
607 i->push_to_buffer(value_buffer);
609 writer.write_data((uint32_t)value_buffer.size());
610 writer.write_comment(key);
611 writer.write_data(strings.add_string(key));
612 writer.write_data(value_buffer);
616 property_value::try_to_merge(property_value &other)
622 __builtin_unreachable();
630 case CROSS_REFERENCE:
634 if (other.type == PHANDLE || other.type == BINARY)
637 byte_data.insert(byte_data.end(), other.byte_data.begin(),
638 other.byte_data.end());
646 property::write_dts(FILE *file, int indent)
648 for (int i=0 ; i<indent ; i++)
653 for (auto &l : labels)
655 fputs(l.c_str(), file);
661 fputs(key.c_str(), file);
665 std::vector<property_value> *vals = &values;
666 std::vector<property_value> v;
667 // If we've got multiple values then try to merge them all together.
668 if (values.size() > 1)
671 v.push_back(values.front());
672 for (auto i=(++begin()), e=end() ; i!=e ; ++i)
674 if (!v.back().try_to_merge(*i))
681 for (auto i=vals->begin(), e=vals->end() ; i!=e ; ++i)
695 property::offset_of_value(property_value &val)
698 for (auto &v : values)
710 node::parse_name(text_input_buffer &input, bool &is_property, const char *error)
719 return input.parse_property_name();
721 string n = input.parse_node_or_property_name(is_property);
726 input.parse_error(error);
734 node::visit(std::function<visit_behavior(node&, node*)> fn, node *parent)
736 visit_behavior behavior;
737 behavior = fn(*this, parent);
738 if (behavior == VISIT_BREAK)
742 else if (behavior != VISIT_CONTINUE)
744 for (auto &&c : children)
746 behavior = c->visit(fn, this);
747 // Any status other than VISIT_RECURSE stops our execution and
748 // bubbles up to our caller. The caller may then either continue
749 // visiting nodes that are siblings to this one or completely halt
751 if (behavior != VISIT_RECURSE)
757 // Continue recursion by default
758 return VISIT_RECURSE;
761 node::node(input_buffer &structs, input_buffer &strings) : valid(true)
763 std::vector<char> bytes;
764 while (structs[0] != '\0' && structs[0] != '@')
766 bytes.push_back(structs[0]);
769 name = string(bytes.begin(), bytes.end());
771 if (structs[0] == '@')
774 while (structs[0] != '\0')
776 bytes.push_back(structs[0]);
779 unit_address = string(bytes.begin(), bytes.end());
783 while (structs.consume_binary(token))
788 fprintf(stderr, "Unexpected token 0x%" PRIx32
789 " while parsing node.\n", token);
792 // Child node, parse it.
793 case dtb::FDT_BEGIN_NODE:
795 node_ptr child = node::parse_dtb(structs, strings);
801 children.push_back(std::move(child));
804 // End of this node, no errors.
805 case dtb::FDT_END_NODE:
807 // Property, parse it.
810 property_ptr prop = property::parse_dtb(structs, strings);
816 props.push_back(prop);
820 // End of structs table. Should appear after
821 // the end of the last node.
823 fprintf(stderr, "Unexpected FDT_END token while parsing node.\n");
826 // NOPs are padding. Ignore them.
831 fprintf(stderr, "Failed to read token from structs table while parsing node.\n");
837 node::node(const string &n,
838 const std::vector<property_ptr> &p)
841 props.insert(props.begin(), p.begin(), p.end());
844 node_ptr node::create_special_node(const string &name,
845 const std::vector<property_ptr> &props)
847 node_ptr n(new node(name, props));
851 node::node(text_input_buffer &input,
854 std::unordered_set<string> &&l,
857 : labels(l), name(n), unit_address(a), valid(true)
859 if (!input.consume('{'))
861 input.parse_error("Expected { to start new device tree node.\n");
864 while (valid && !input.consume('}'))
866 // flag set if we find any characters that are only in
867 // the property name character set, not the node
868 bool is_property = false;
869 // flag set if our node is marked as /omit-if-no-ref/ to be
870 // garbage collected later if nothing references it
871 bool marked_omit_if_no_ref = false;
872 string child_name, child_address;
873 std::unordered_set<string> child_labels;
874 auto parse_delete = [&](const char *expected, bool at)
876 if (child_name == string())
878 input.parse_error(expected);
883 if (at && input.consume('@'))
886 child_name += parse_name(input, is_property, "Expected unit address");
888 if (!input.consume(';'))
890 input.parse_error("Expected semicolon");
896 if (input.consume("/delete-node/"))
899 child_name = input.parse_node_name();
900 parse_delete("Expected node name", true);
903 deleted_children.insert(child_name);
907 if (input.consume("/delete-property/"))
910 child_name = input.parse_property_name();
911 parse_delete("Expected property name", false);
914 deleted_props.insert(child_name);
918 if (input.consume("/omit-if-no-ref/"))
921 marked_omit_if_no_ref = true;
922 tree.set_needs_garbage_collection();
924 child_name = parse_name(input, is_property,
925 "Expected property or node name");
926 while (input.consume(':'))
928 // Node labels can contain any characters? The
929 // spec doesn't say, so we guess so...
931 child_labels.insert(std::move(child_name));
932 child_name = parse_name(input, is_property, "Expected property or node name");
934 if (input.consume('@'))
936 child_address = parse_name(input, is_property, "Expected unit address");
943 // If we're parsing a property, then we must actually do that.
944 if (input.consume('='))
946 property_ptr p = property::parse(input, std::move(child_name),
947 std::move(child_labels), true, defines);
957 else if (!is_property && *input == ('{'))
959 node_ptr child = node::parse(input, tree, std::move(child_name),
960 std::move(child_labels), std::move(child_address), defines);
963 child->omit_if_no_ref = marked_omit_if_no_ref;
964 children.push_back(std::move(child));
971 else if (input.consume(';'))
973 props.push_back(property_ptr(new property(std::move(child_name), std::move(child_labels))));
977 input.parse_error("Error parsing property. Expected property value");
987 node::cmp_properties(property_ptr &p1, property_ptr &p2)
989 return p1->get_key() < p2->get_key();
993 node::cmp_children(node_ptr &c1, node_ptr &c2)
995 if (c1->name == c2->name)
997 return c1->unit_address < c2->unit_address;
999 return c1->name < c2->name;
1005 std::sort(property_begin(), property_end(), cmp_properties);
1006 std::sort(child_begin(), child_end(), cmp_children);
1007 for (auto &c : child_nodes())
1014 node::parse(text_input_buffer &input,
1019 define_map *defines)
1021 node_ptr n(new node(input,
1035 node::parse_dtb(input_buffer &structs, input_buffer &strings)
1037 node_ptr n(new node(structs, strings));
1046 node::get_property(const string &key)
1048 for (auto &i : props)
1050 if (i->get_key() == key)
1059 node::merge_node(node_ptr &other)
1061 for (auto &l : other->labels)
1065 children.erase(std::remove_if(children.begin(), children.end(),
1066 [&](const node_ptr &p) {
1067 string full_name = p->name;
1068 if (p->unit_address != string())
1071 full_name += p->unit_address;
1073 if (other->deleted_children.count(full_name) > 0)
1075 other->deleted_children.erase(full_name);
1079 }), children.end());
1080 props.erase(std::remove_if(props.begin(), props.end(),
1081 [&](const property_ptr &p) {
1082 if (other->deleted_props.count(p->get_key()) > 0)
1084 other->deleted_props.erase(p->get_key());
1089 // Note: this is an O(n*m) operation. It might be sensible to
1090 // optimise this if we find that there are nodes with very
1091 // large numbers of properties, but for typical usage the
1092 // entire vector will fit (easily) into cache, so iterating
1093 // over it repeatedly isn't that expensive.
1094 for (auto &p : other->properties())
1097 for (auto &mp : properties())
1099 if (mp->get_key() == p->get_key())
1111 for (auto &c : other->children)
1114 for (auto &i : children)
1116 if (i->name == c->name && i->unit_address == c->unit_address)
1125 children.push_back(std::move(c));
1131 node::write(dtb::output_writer &writer, dtb::string_table &strings)
1133 writer.write_token(dtb::FDT_BEGIN_NODE);
1134 byte_buffer name_buffer;
1135 push_string(name_buffer, name);
1136 if (unit_address != string())
1138 name_buffer.push_back('@');
1139 push_string(name_buffer, unit_address);
1141 writer.write_comment(name);
1142 writer.write_data(name_buffer);
1143 writer.write_data((uint8_t)0);
1144 for (auto p : properties())
1146 p->write(writer, strings);
1148 for (auto &c : child_nodes())
1150 c->write(writer, strings);
1152 writer.write_token(dtb::FDT_END_NODE);
1156 node::write_dts(FILE *file, int indent)
1158 for (int i=0 ; i<indent ; i++)
1163 for (auto &label : labels)
1165 fprintf(file, "%s: ", label.c_str());
1168 if (name != string())
1170 fputs(name.c_str(), file);
1172 if (unit_address != string())
1175 fputs(unit_address.c_str(), file);
1177 fputs(" {\n\n", file);
1178 for (auto p : properties())
1180 p->write_dts(file, indent+1);
1182 for (auto &c : child_nodes())
1184 c->write_dts(file, indent+1);
1186 for (int i=0 ; i<indent ; i++)
1190 fputs("};\n", file);
1194 device_tree::collect_names_recursive(node_ptr &n, node_path &path)
1196 path.push_back(std::make_pair(n->name, n->unit_address));
1197 for (const string &name : n->labels)
1199 if (name != string())
1201 auto iter = node_names.find(name);
1202 if (iter == node_names.end())
1204 node_names.insert(std::make_pair(name, n.get()));
1205 node_paths.insert(std::make_pair(name, path));
1206 ordered_node_paths.push_back({name, path});
1210 node_names.erase(iter);
1211 auto i = node_paths.find(name);
1212 if (i != node_paths.end())
1214 node_paths.erase(name);
1216 fprintf(stderr, "Label not unique: %s. References to this label will not be resolved.\n", name.c_str());
1220 for (auto &c : n->child_nodes())
1222 collect_names_recursive(c, path);
1224 // Now we collect the phandles and properties that reference
1226 for (auto &p : n->properties())
1232 fixups.push_back({path, p, v});
1234 if (v.is_cross_reference())
1236 cross_references.push_back(&v);
1239 if ((p->get_key() == "phandle") ||
1240 (p->get_key() == "linux,phandle"))
1242 if (p->begin()->byte_data.size() != 4)
1244 fprintf(stderr, "Invalid phandle value for node %s. Should be a 4-byte value.\n", n->name.c_str());
1249 uint32_t phandle = p->begin()->get_as_uint32();
1250 used_phandles.insert(std::make_pair(phandle, n.get()));
1258 device_tree::collect_names()
1263 ordered_node_paths.clear();
1264 cross_references.clear();
1266 collect_names_recursive(root, p);
1270 device_tree::assign_phandle(node *n, uint32_t &phandle)
1272 // If there is an existing phandle, use it
1273 property_ptr p = n->get_property("phandle");
1276 p = n->get_property("linux,phandle");
1280 // Otherwise insert a new phandle node
1282 while (used_phandles.find(phandle) != used_phandles.end())
1284 // Note that we only don't need to
1285 // store this phandle in the set,
1286 // because we are monotonically
1287 // increasing the value of phandle and
1288 // so will only ever revisit this value
1289 // if we have used 2^32 phandles, at
1290 // which point our blob won't fit in
1291 // any 32-bit system and we've done
1292 // something badly wrong elsewhere
1296 push_big_endian(v.byte_data, phandle++);
1297 if (phandle_node_name == BOTH || phandle_node_name == LINUX)
1299 p.reset(new property("linux,phandle"));
1303 if (phandle_node_name == BOTH || phandle_node_name == EPAPR)
1305 p.reset(new property("phandle"));
1315 device_tree::assign_phandles(node_ptr &n, uint32_t &next)
1317 if (!n->labels.empty())
1319 assign_phandle(n.get(), next);
1322 for (auto &c : n->child_nodes())
1324 assign_phandles(c, next);
1329 device_tree::resolve_cross_references(uint32_t &phandle)
1331 for (auto *pv : cross_references)
1333 node_path path = node_paths[pv->string_data];
1334 auto p = path.begin();
1335 auto pe = path.end();
1338 // Skip the first name in the path. It's always "", and implicitly /
1339 for (++p ; p!=pe ; ++p)
1341 pv->byte_data.push_back('/');
1342 push_string(pv->byte_data, p->first);
1343 if (!(p->second.empty()))
1345 pv->byte_data.push_back('@');
1346 push_string(pv->byte_data, p->second);
1349 pv->byte_data.push_back(0);
1352 std::unordered_map<property_value*, fixup&> phandle_set;
1353 for (auto &i : fixups)
1355 phandle_set.insert({&i.val, i});
1357 std::vector<std::reference_wrapper<fixup>> sorted_phandles;
1358 root->visit([&](node &n, node *) {
1359 for (auto &p : n.properties())
1363 auto i = phandle_set.find(&v);
1364 if (i != phandle_set.end())
1366 sorted_phandles.push_back(i->second);
1371 return node::VISIT_RECURSE;
1373 assert(sorted_phandles.size() == fixups.size());
1374 for (auto &i : sorted_phandles)
1376 string target_name = i.get().val.string_data;
1377 node *target = nullptr;
1379 // If the node name is a path, then look it up by following the path,
1380 // otherwise jump directly to the named node.
1381 if (target_name[0] == '/')
1384 target = root.get();
1385 std::istringstream ss(target_name);
1386 string path_element;
1387 // Read the leading /
1388 std::getline(ss, path_element, '/');
1389 // Iterate over path elements
1393 std::getline(ss, path_element, '/');
1394 std::istringstream nss(path_element);
1395 string node_name, node_address;
1396 std::getline(nss, node_name, '@');
1397 std::getline(nss, node_address, '@');
1398 node *next = nullptr;
1399 for (auto &c : target->child_nodes())
1401 if (c->name == node_name)
1403 if (c->unit_address == node_address)
1410 possible = path + c->name;
1411 if (c->unit_address != string())
1414 possible += c->unit_address;
1420 if (node_address != string())
1423 path += node_address;
1426 if (target == nullptr)
1434 target = node_names[target_name];
1436 if (target == nullptr)
1440 unresolved_fixups.push_back(i);
1445 fprintf(stderr, "Failed to find node with label: %s\n", target_name.c_str());
1446 if (possible != string())
1448 fprintf(stderr, "Possible intended match: %s\n", possible.c_str());
1454 // If there is an existing phandle, use it
1455 property_ptr p = assign_phandle(target, phandle);
1456 p->begin()->push_to_buffer(i.get().val.byte_data);
1457 assert(i.get().val.byte_data.size() == 4);
1462 device_tree::garbage_collect_marked_nodes()
1464 std::unordered_set<node*> previously_referenced_nodes;
1465 std::unordered_set<node*> newly_referenced_nodes;
1467 auto mark_referenced_nodes_used = [&](node &n)
1469 for (auto &p : n.properties())
1475 node *nx = node_names[v.string_data];
1478 // Try it again, but as a path
1479 for (auto &s : node_paths)
1481 if (v.string_data == s.second.to_string())
1483 nx = node_names[s.first];
1490 // Couldn't resolve this one?
1493 // Only mark those currently unmarked
1497 newly_referenced_nodes.insert(nx);
1504 // Seed our referenced nodes with those that have been seen by a node that
1505 // either will not be omitted if it's unreferenced or has a symbol.
1506 // Nodes with symbols are explicitly not garbage collected because they may
1507 // be expected for referencing by an overlay, and we do not want surprises
1509 root->visit([&](node &n, node *) {
1510 if (!n.omit_if_no_ref || (write_symbols && !n.labels.empty()))
1512 mark_referenced_nodes_used(n);
1514 // Recurse as normal
1515 return node::VISIT_RECURSE;
1518 while (!newly_referenced_nodes.empty())
1520 previously_referenced_nodes = std::move(newly_referenced_nodes);
1521 for (auto *n : previously_referenced_nodes)
1523 mark_referenced_nodes_used(*n);
1527 previously_referenced_nodes.clear();
1528 bool children_deleted = false;
1531 root->visit([&](node &n, node *) {
1532 bool gc_children = false;
1534 for (auto &cn : n.child_nodes())
1536 if (cn->omit_if_no_ref && !cn->used)
1545 children_deleted = true;
1546 n.delete_children_if([](node_ptr &nx) {
1547 return (nx->omit_if_no_ref && !nx->used);
1550 return node::VISIT_CONTINUE;
1553 return node::VISIT_RECURSE;
1556 return children_deleted;
1560 device_tree::parse_file(text_input_buffer &input,
1561 std::vector<node_ptr> &roots,
1566 while (input.consume("/dts-v1/;"))
1571 if (input.consume("/plugin/;"))
1578 input.parse_error("Expected /dts-v1/; version string");
1580 // Read any memory reservations
1581 while (input.consume("/memreserve/"))
1583 unsigned long long start, len;
1585 // Read the start and length.
1586 if (!(input.consume_integer_expression(start) &&
1587 (input.next_token(),
1588 input.consume_integer_expression(len))))
1590 input.parse_error("Expected size on /memreserve/ node.");
1594 reservations.push_back(reservation(start, len));
1600 while (valid && !input.finished())
1603 if (input.consume('/'))
1606 n = node::parse(input, *this, string(), string_set(), string(), &defines);
1608 else if (input.consume('&'))
1612 bool name_is_path_reference = false;
1613 // This is to deal with names intended as path references, e.g. &{/path}.
1614 // While it may make sense in a non-plugin context, we don't support such
1615 // usage at this time.
1616 if (input.consume('{') && is_plugin)
1618 name = input.parse_to('}');
1620 name_is_path_reference = true;
1624 name = input.parse_node_name();
1627 n = node::parse(input, *this, std::move(name), string_set(), string(), &defines);
1630 n->name_is_path_reference = name_is_path_reference;
1635 input.parse_error("Failed to find root node /.");
1639 roots.push_back(std::move(n));
1649 template<class writer> void
1650 device_tree::write(int fd)
1652 dtb::string_table st;
1655 writer reservation_writer;
1656 writer struct_writer;
1657 writer strings_writer;
1659 // Build the reservation table
1660 reservation_writer.write_comment(string("Memory reservations"));
1661 reservation_writer.write_label(string("dt_reserve_map"));
1662 for (auto &i : reservations)
1664 reservation_writer.write_comment(string("Reservation start"));
1665 reservation_writer.write_data(i.first);
1666 reservation_writer.write_comment(string("Reservation length"));
1667 reservation_writer.write_data(i.second);
1669 // Write n spare reserve map entries, plus the trailing 0.
1670 for (uint32_t i=0 ; i<=spare_reserve_map_entries ; i++)
1672 reservation_writer.write_data((uint64_t)0);
1673 reservation_writer.write_data((uint64_t)0);
1677 struct_writer.write_comment(string("Device tree"));
1678 struct_writer.write_label(string("dt_struct_start"));
1679 root->write(struct_writer, st);
1680 struct_writer.write_token(dtb::FDT_END);
1681 struct_writer.write_label(string("dt_struct_end"));
1683 st.write(strings_writer);
1684 // Find the strings size before we stick padding on the end.
1685 // Note: We should possibly use a new writer for the padding.
1686 head.size_dt_strings = strings_writer.size();
1688 // Stick the padding in the strings writer, but after the
1689 // marker indicating that it's the end.
1690 // Note: We probably should add a padding call to the writer so
1691 // that the asm back end can write padding directives instead
1692 // of a load of 0 bytes.
1693 for (uint32_t i=0 ; i<blob_padding ; i++)
1695 strings_writer.write_data((uint8_t)0);
1697 head.totalsize = sizeof(head) + strings_writer.size() +
1698 struct_writer.size() + reservation_writer.size();
1699 while (head.totalsize < minimum_blob_size)
1702 strings_writer.write_data((uint8_t)0);
1704 head.off_dt_struct = sizeof(head) + reservation_writer.size();;
1705 head.off_dt_strings = head.off_dt_struct + struct_writer.size();
1706 head.off_mem_rsvmap = sizeof(head);
1707 head.boot_cpuid_phys = boot_cpu;
1708 head.size_dt_struct = struct_writer.size();
1709 head.write(head_writer);
1711 head_writer.write_to_file(fd);
1712 reservation_writer.write_to_file(fd);
1713 struct_writer.write_to_file(fd);
1714 strings_writer.write_label(string("dt_blob_end"));
1715 strings_writer.write_to_file(fd);
1719 device_tree::referenced_node(property_value &v)
1723 return node_names[v.string_data];
1727 return used_phandles[v.get_as_uint32()];
1733 device_tree::write_binary(int fd)
1735 write<dtb::binary_writer>(fd);
1739 device_tree::write_asm(int fd)
1741 write<dtb::asm_writer>(fd);
1745 device_tree::write_dts(int fd)
1747 FILE *file = fdopen(fd, "w");
1748 fputs("/dts-v1/;\n\n", file);
1750 if (!reservations.empty())
1752 const char msg[] = "/memreserve/";
1753 // Exclude the null byte when we're writing it out to the file.
1754 fwrite(msg, sizeof(msg) - 1, 1, file);
1755 for (auto &i : reservations)
1757 fprintf(file, " 0x%" PRIx64 " 0x%" PRIx64, i.first, i.second);
1759 fputs(";\n\n", file);
1763 root->write_dts(file, 0);
1768 device_tree::parse_dtb(const string &fn, FILE *)
1770 auto in = input_buffer::buffer_for_file(fn);
1776 input_buffer &input = *in;
1778 valid = h.read_dtb(input);
1779 boot_cpu = h.boot_cpuid_phys;
1780 if (h.last_comp_version > 17)
1782 fprintf(stderr, "Don't know how to read this version of the device tree blob");
1789 input_buffer reservation_map =
1790 input.buffer_from_offset(h.off_mem_rsvmap, 0);
1791 uint64_t start, length;
1794 if (!(reservation_map.consume_binary(start) &&
1795 reservation_map.consume_binary(length)))
1797 fprintf(stderr, "Failed to read memory reservation table\n");
1801 if (start != 0 || length != 0)
1803 reservations.push_back(reservation(start, length));
1805 } while (!((start == 0) && (length == 0)));
1806 input_buffer struct_table =
1807 input.buffer_from_offset(h.off_dt_struct, h.size_dt_struct);
1808 input_buffer strings_table =
1809 input.buffer_from_offset(h.off_dt_strings, h.size_dt_strings);
1811 if (!(struct_table.consume_binary(token) &&
1812 (token == dtb::FDT_BEGIN_NODE)))
1814 fprintf(stderr, "Expected FDT_BEGIN_NODE token.\n");
1818 root = node::parse_dtb(struct_table, strings_table);
1819 if (!(struct_table.consume_binary(token) && (token == dtb::FDT_END)))
1821 fprintf(stderr, "Expected FDT_END token after parsing root node.\n");
1825 valid = (root != 0);
1829 device_tree::node_path::to_string() const
1834 if ((p == pe) || (p+1 == pe))
1838 // Skip the first name in the path. It's always "", and implicitly /
1839 for (++p ; p!=pe ; ++p)
1843 if (!(p->second.empty()))
1853 device_tree::create_fragment_wrapper(node_ptr &node, int &fragnum)
1855 // In a plugin, we can massage these non-/ root nodes into into a fragment
1856 std::string fragment_address = "fragment@" + std::to_string(fragnum);
1859 std::vector<property_ptr> symbols;
1861 // Intentionally left empty
1862 node_ptr newroot = node::create_special_node("", symbols);
1863 node_ptr wrapper = node::create_special_node("__overlay__", symbols);
1865 // Generate the fragment with $propname = <&name>
1867 std::string propname;
1868 v.string_data = node->name;
1869 if (!node->name_is_path_reference)
1871 propname = "target";
1872 v.type = property_value::PHANDLE;
1876 propname = "target-path";
1877 v.type = property_value::STRING;
1879 auto prop = std::make_shared<property>(std::string(propname));
1881 symbols.push_back(prop);
1883 node_ptr fragment = node::create_special_node(fragment_address, symbols);
1885 wrapper->merge_node(node);
1886 fragment->add_child(std::move(wrapper));
1887 newroot->add_child(std::move(fragment));
1892 device_tree::generate_root(node_ptr &node, int &fragnum)
1895 string name = node->name;
1896 if (name == string())
1898 return std::move(node);
1900 else if (!is_plugin)
1905 return create_fragment_wrapper(node, fragnum);
1909 device_tree::reassign_fragment_numbers(node_ptr &node, int &delta)
1912 for (auto &c : node->child_nodes())
1914 if (c->name == std::string("fragment"))
1916 int current_address = std::stoi(c->unit_address, nullptr, 16);
1917 std::ostringstream new_address;
1918 current_address += delta;
1919 // It's possible that we hopped more than one somewhere, so just reset
1920 // delta to the next in sequence.
1921 delta = current_address + 1;
1922 new_address << std::hex << current_address;
1923 c->unit_address = new_address.str();
1929 device_tree::parse_dts(const string &fn, FILE *depfile)
1931 auto in = input_buffer::buffer_for_file(fn);
1937 std::vector<node_ptr> roots;
1938 std::unordered_set<string> defnames;
1939 for (auto &i : defines)
1941 defnames.insert(i.first);
1943 text_input_buffer input(std::move(in),
1944 std::move(defnames),
1945 std::vector<string>(include_paths),
1948 bool read_header = false;
1950 parse_file(input, roots, read_header);
1951 switch (roots.size())
1955 input.parse_error("Failed to find root node /.");
1958 root = generate_root(roots[0], fragnum);
1962 input.parse_error("Failed to find root node /.");
1968 root = generate_root(roots[0], fragnum);
1972 input.parse_error("Failed to find root node /.");
1975 for (auto i=++(roots.begin()), e=roots.end() ; i!=e ; ++i)
1978 string name = node->name;
1979 if (name == string())
1983 // Re-assign any fragment numbers based on a delta of
1984 // fragnum before we merge it
1985 reassign_fragment_numbers(node, fragnum);
1987 root->merge_node(node);
1991 auto existing = node_names.find(name);
1992 if (existing == node_names.end())
1995 existing = node_names.find(name);
1997 if (existing == node_names.end())
2001 auto fragment = create_fragment_wrapper(node, fragnum);
2002 root->merge_node(fragment);
2006 fprintf(stderr, "Unable to merge node: %s\n", name.c_str());
2011 existing->second->merge_node(node);
2018 // Return value indicates whether we've dirtied the tree or not and need to
2020 if (garbage_collect && garbage_collect_marked_nodes())
2024 uint32_t phandle = 1;
2025 // If we're writing symbols, go ahead and assign phandles to the entire
2026 // tree. We'll do this before we resolve cross references, just to keep
2027 // order semi-predictable and stable.
2030 assign_phandles(root, phandle);
2032 resolve_cross_references(phandle);
2035 std::vector<property_ptr> symbols;
2036 // Create a symbol table. Each label in this device tree may be
2037 // referenced by other plugins, so we create a __symbols__ node inside
2038 // the root that contains mappings (properties) from label names to
2040 for (auto i=ordered_node_paths.rbegin(), e=ordered_node_paths.rend() ; i!=e ; ++i)
2043 if (node_paths.find(s.first) == node_paths.end())
2045 // Erased node, skip it.
2049 v.string_data = s.second.to_string();
2050 v.type = property_value::STRING;
2051 string name = s.first;
2052 auto prop = std::make_shared<property>(std::move(name));
2054 symbols.push_back(prop);
2056 root->add_child(node::create_special_node("__symbols__", symbols));
2058 // If this is a plugin, then we also need to create two extra nodes.
2059 // Internal phandles will need to be renumbered to avoid conflicts with
2060 // already-loaded nodes and external references will need to be
2064 std::vector<property_ptr> symbols;
2065 // Create the fixups entry. This is of the form:
2066 // {target} = {path}:{property name}:{offset}
2067 auto create_fixup_entry = [&](fixup &i, string target)
2069 string value = i.path.to_string();
2071 value += i.prop->get_key();
2073 value += std::to_string(i.prop->offset_of_value(i.val));
2075 v.string_data = value;
2076 v.type = property_value::STRING;
2077 auto prop = std::make_shared<property>(std::move(target));
2081 // If we have any unresolved phandle references in this plugin,
2082 // then we must update them to 0xdeadbeef and leave a property in
2083 // the /__fixups__ node whose key is the label and whose value is
2084 // as described above.
2085 if (!unresolved_fixups.empty())
2087 for (auto &i : unresolved_fixups)
2089 auto &val = i.get().val;
2090 symbols.push_back(create_fixup_entry(i, val.string_data));
2091 val.byte_data.push_back(0xde);
2092 val.byte_data.push_back(0xad);
2093 val.byte_data.push_back(0xbe);
2094 val.byte_data.push_back(0xef);
2095 val.type = property_value::BINARY;
2097 root->add_child(node::create_special_node("__fixups__", symbols));
2100 // If we have any resolved phandle references in this plugin, then
2101 // we must create a child in the __local_fixups__ node whose path
2102 // matches the node path from the root and whose value contains the
2103 // location of the reference within a property.
2105 // Create a local_fixups node that is initially empty.
2106 node_ptr local_fixups = node::create_special_node("__local_fixups__", symbols);
2107 for (auto &i : fixups)
2109 if (!i.val.is_phandle())
2113 node *n = local_fixups.get();
2114 for (auto &p : i.path)
2116 // Skip the implicit root
2117 if (p.first.empty())
2122 for (auto &c : n->child_nodes())
2124 if (c->name == p.first)
2126 if (c->unit_address == p.second)
2136 string path = p.first;
2137 if (!(p.second.empty()))
2142 n->add_child(node::create_special_node(path, symbols));
2143 n = (--n->child_end())->get();
2148 push_big_endian(pv.byte_data, static_cast<uint32_t>(i.prop->offset_of_value(i.val)));
2149 pv.type = property_value::BINARY;
2150 auto key = i.prop->get_key();
2151 property_ptr prop = n->get_property(key);
2152 // If we don't have an existing property then create one and
2153 // use this property value
2156 prop = std::make_shared<property>(std::move(key));
2157 n->add_property(prop);
2158 prop->add_value(pv);
2162 // If we do have an existing property value, try to append
2164 property_value &old_val = *(--prop->end());
2165 if (!old_val.try_to_merge(pv))
2167 prop->add_value(pv);
2171 // We've iterated over all fixups, but only emit the
2172 // __local_fixups__ if we found some that were resolved internally.
2173 if (local_fixups->child_begin() != local_fixups->child_end())
2175 root->add_child(std::move(local_fixups));
2180 bool device_tree::parse_define(const char *def)
2182 const char *val = strchr(def, '=');
2185 if (strlen(def) != 0)
2193 string name(def, val-def);
2194 string name_copy = name;
2196 std::unique_ptr<input_buffer> raw(new input_buffer(val, strlen(val)));
2197 text_input_buffer in(std::move(raw),
2198 std::unordered_set<string>(),
2199 std::vector<string>(),
2202 property_ptr p = property::parse(in, std::move(name_copy), string_set(), false);