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
33 #define __STDC_LIMIT_MACROS 1
54 property_value::get_as_uint32()
56 if (byte_data.size() != 4)
61 v &= byte_data[0] << 24;
62 v &= byte_data[1] << 16;
63 v &= byte_data[2] << 8;
64 v &= byte_data[3] << 0;
69 property_value::push_to_buffer(byte_buffer &buffer)
71 if (!byte_data.empty())
73 buffer.insert(buffer.end(), byte_data.begin(), byte_data.end());
77 string_data.push_to_buffer(buffer, true);
84 property_value::write_dts(FILE *file)
90 assert(0 && "Invalid type");
94 write_as_string(file);
100 if (byte_data.size() % 4 == 0)
102 write_as_cells(file);
105 write_as_bytes(file);
111 property_value::resolve_type()
117 if (byte_data.empty())
122 if (byte_data.back() == 0)
124 bool is_all_printable = true;
127 for (byte_buffer::iterator i=byte_data.begin(), e=byte_data.end()-1; i<e ; i++)
130 is_all_printable &= (*i == '\0') || isprint(*i);
135 if (!is_all_printable)
140 if (is_all_printable && (bytes > nuls))
154 property_value::write_as_string(FILE *file)
157 if (byte_data.empty())
159 string_data.print(file);
163 for (byte_buffer::iterator i=byte_data.begin(), e=byte_data.end()-1; i!=e ; ++i)
165 // FIXME Escape tabs, newlines, and so on.
168 fputs("\", \"", file);
178 property_value::write_as_cells(FILE *file)
181 assert((byte_data.size() % 4) == 0);
182 for (byte_buffer::iterator i=byte_data.begin(), e=byte_data.end(); i!=e ; ++i)
192 fprintf(file, "0x%" PRIx32, v);
202 property_value::write_as_bytes(FILE *file)
205 for (byte_buffer::iterator i=byte_data.begin(), e=byte_data.end(); i!=e ; i++)
207 fprintf(file, "%hhx", *i);
217 property::parse_string(input_buffer &input)
220 assert(input[0] == '"');
222 const char *start = (const char*)input;
224 while (char c = input[0])
226 if (c == '"' && input[-1] != '\\')
234 v.string_data = string(start, length);
239 property::parse_cells(input_buffer &input)
241 assert(input[0] == '<');
245 while (!input.consume('>'))
248 // If this is a phandle then we need to get the name of the
250 if (input.consume('&'))
253 // FIXME: We should support full paths here, but we
255 string referenced = string::parse_node_name(input);
256 if (referenced.empty())
258 input.parse_error("Expected node name");
263 // If we already have some bytes, make the phandle a
264 // separate component.
265 if (!v.byte_data.empty())
268 v = property_value();
270 v.string_data = referenced;
271 v.type = property_value::PHANDLE;
273 v = property_value();
277 //FIXME: We should support labels in the middle
278 //of these, but we don't.
280 if (!input.consume_integer(val))
282 input.parse_error("Expected numbers in array of cells");
286 if ((val < 0) || (val > UINT32_MAX))
288 input.parse_error("Value out of range");
292 push_big_endian(v.byte_data, (uint32_t)val);
296 // Don't store an empty string value here.
297 if (v.byte_data.size() > 0)
304 property::parse_bytes(input_buffer &input)
306 assert(input[0] == '[');
310 while (!input.consume(']'))
313 //FIXME: We should support
314 //labels in the middle of
315 //these, but we don't.
317 if (!input.consume_hex_byte(val))
319 input.parse_error("Expected hex bytes in array of bytes");
323 v.byte_data.push_back(val);
331 property::parse_reference(input_buffer &input)
333 assert(input[0] == '&');
337 v.string_data = string::parse_node_name(input);
338 if (v.string_data.empty())
340 input.parse_error("Expected node name");
344 v.type = property_value::CROSS_REFERENCE;
348 property::property(input_buffer &structs, input_buffer &strings)
350 uint32_t name_offset;
352 valid = structs.consume_binary(length) &&
353 structs.consume_binary(name_offset);
356 fprintf(stderr, "Failed to read property\n");
360 input_buffer name_buffer = strings.buffer_from_offset(name_offset);
361 if (name_buffer.empty())
363 fprintf(stderr, "Property name offset %" PRIu32
364 " is past the end of the strings table\n",
369 key = string(name_buffer);
373 for (uint32_t i=0 ; i<length ; i++)
375 if (!(valid = structs.consume_binary(byte)))
377 fprintf(stderr, "Failed to read property value\n");
380 v.byte_data.push_back(byte);
385 void property::parse_define(input_buffer &input, define_map *defines)
390 input.parse_error("No predefined properties to match name\n");
394 string name = string::parse_property_name(input);
395 define_map::iterator found;
396 if ((name == string()) ||
397 ((found = defines->find(name)) == defines->end()))
399 input.parse_error("Undefined property name\n");
403 values.push_back((*found).second->values[0]);
406 property::property(input_buffer &input,
409 bool semicolonTerminated,
410 define_map *defines) : key(k), label(l), valid(true)
418 parse_define(input, defines);
425 input.parse_error("Invalid property value.");
438 parse_reference(input);
446 } while (input.consume(','));
447 if (semicolonTerminated && !input.consume(';'))
449 input.parse_error("Expected ; at end of property");
455 property::parse_dtb(input_buffer &structs, input_buffer &strings)
457 property *p = new property(structs, strings);
467 property::parse(input_buffer &input, string key, string label,
468 bool semicolonTerminated, define_map *defines)
470 property *p = new property(input, key, label, semicolonTerminated, defines);
480 property::write(dtb::output_writer &writer, dtb::string_table &strings)
482 writer.write_token(dtb::FDT_PROP);
483 byte_buffer value_buffer;
484 for (value_iterator i=begin(), e=end() ; i!=e ; ++i)
486 i->push_to_buffer(value_buffer);
488 writer.write_data((uint32_t)value_buffer.size());
489 writer.write_comment(key);
490 writer.write_data(strings.add_string(key));
491 writer.write_data(value_buffer);
495 property::write_dts(FILE *file, int indent)
497 for (int i=0 ; i<indent ; i++)
501 if (label != string())
513 for (value_iterator i=begin(), e=end() ; i!=e ; ++i)
527 node::parse_name(input_buffer &input, bool &is_property, const char *error)
536 return string::parse_property_name(input);
538 string n = string::parse_node_or_property_name(input, is_property);
543 input.parse_error(error);
550 node::node(input_buffer &structs, input_buffer &strings) : valid(true)
552 const char *name_start = (const char*)structs;
554 while (structs[0] != '\0' && structs[0] != '@')
559 name = string(name_start, name_length);
560 if (structs[0] == '@')
563 name_start = (const char*)structs;
565 while (structs[0] != '\0')
570 unit_address = string(name_start, name_length);
574 while (structs.consume_binary(token))
579 fprintf(stderr, "Unexpected token 0x%" PRIx32
580 " while parsing node.\n", token);
583 // Child node, parse it.
584 case dtb::FDT_BEGIN_NODE:
586 node *child = node::parse_dtb(structs, strings);
592 children.push_back(child);
595 // End of this node, no errors.
596 case dtb::FDT_END_NODE:
598 // Property, parse it.
601 property *prop = property::parse_dtb(structs, strings);
607 properties.push_back(prop);
611 // End of structs table. Should appear after
612 // the end of the last node.
614 fprintf(stderr, "Unexpected FDT_END token while parsing node.\n");
617 // NOPs are padding. Ignore them.
622 fprintf(stderr, "Failed to read token from structs table while parsing node.\n");
627 node::node(input_buffer &input, string n, string l, string a, define_map *defines) :
628 label(l), name(n), unit_address(a), valid(true)
630 if (!input.consume('{'))
632 input.parse_error("Expected { to start new device tree node.\n");
635 while (valid && !input.consume('}'))
637 // flag set if we find any characters that are only in
638 // the property name character set, not the node
639 bool is_property = false;
640 string child_name, child_label, child_address;
641 child_name = parse_name(input, is_property,
642 "Expected property or node name");
643 if (input.consume(':'))
645 // Node labels can contain any characters? The
646 // spec doesn't say, so we guess so...
648 child_label = child_name;
649 child_name = parse_name(input, is_property, "Expected property or node name");
651 if (input.consume('@'))
653 child_address = parse_name(input, is_property, "Expected unit address");
660 // If we're parsing a property, then we must actually do that.
661 if (input.consume('='))
663 property *p= property::parse(input, child_name,
664 child_label, true, defines);
671 properties.push_back(p);
674 else if (!is_property && input[0] == ('{'))
676 node *child = node::parse(input, child_name,
677 child_label, child_address, defines);
680 children.push_back(child);
687 else if (input.consume(';'))
689 properties.push_back(new property(child_name, child_label));
693 input.parse_error("Error parsing property.");
702 node::cmp_properties(property *p1, property *p2)
704 return p1->get_key() < p2->get_key();
708 node::cmp_children(node *c1, node *c2)
710 if (c1->name == c2->name)
712 return c1->unit_address < c2->unit_address;
714 return c1->name < c2->name;
720 std::sort(property_begin(), property_end(), cmp_properties);
721 std::sort(child_begin(), child_end(), cmp_children);
722 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
729 node::parse(input_buffer &input,
735 node *n = new node(input, name, label, address, defines);
745 node::parse_dtb(input_buffer &structs, input_buffer &strings)
747 node *n = new node(structs, strings);
758 while (!children.empty())
760 delete children.back();
763 while (!properties.empty())
765 delete properties.back();
766 properties.pop_back();
771 node::get_property(string key)
773 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
775 if ((*i)->get_key() == key)
784 node::merge_node(node *other)
786 if (!other->label.empty())
788 label = other->label;
790 // Note: this is an O(n*m) operation. It might be sensible to
791 // optimise this if we find that there are nodes with very
792 // large numbers of properties, but for typical usage the
793 // entire vector will fit (easily) into cache, so iterating
794 // over it repeatedly isn't that expensive.
795 while (!other->properties.empty())
797 property *p = other->properties.front();
798 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
800 if ((*i)->get_key() == p->get_key())
808 other->properties.erase(other->properties.begin());
810 while (!other->children.empty())
812 node *c = other->children.front();
814 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
816 if ((*i)->name == c->name && (*i)->unit_address == c->unit_address)
826 children.push_back(c);
828 other->children.erase(other->children.begin());
833 node::write(dtb::output_writer &writer, dtb::string_table &strings)
835 writer.write_token(dtb::FDT_BEGIN_NODE);
836 byte_buffer name_buffer;
837 name.push_to_buffer(name_buffer);
838 if (unit_address != string())
840 name_buffer.push_back('@');
841 unit_address.push_to_buffer(name_buffer);
843 writer.write_comment(name);
844 writer.write_data(name_buffer);
845 writer.write_data((uint8_t)0);
846 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
848 (*i)->write(writer, strings);
850 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
852 (*i)->write(writer, strings);
854 writer.write_token(dtb::FDT_END_NODE);
858 node::write_dts(FILE *file, int indent)
860 for (int i=0 ; i<indent ; i++)
864 if (label != string())
869 if (name != string())
873 if (unit_address != string())
876 unit_address.print(file);
878 fputs(" {\n\n", file);
879 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
881 (*i)->write_dts(file, indent+1);
883 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
885 (*i)->write_dts(file, indent+1);
887 for (int i=0 ; i<indent ; i++)
895 device_tree::collect_names_recursive(node* n, node_path &path)
897 string name = n->label;
898 path.push_back(std::make_pair(n->name, n->unit_address));
899 if (name != string())
901 if (node_names.find(name) == node_names.end())
903 node_names.insert(std::make_pair(name, n));
904 node_paths.insert(std::make_pair(name, path));
908 node_names[name] = (node*)-1;
909 std::map<string, node_path>::iterator i = node_paths.find(name);
910 if (i != node_paths.end())
912 node_paths.erase(name);
914 fprintf(stderr, "Label not unique: ");
916 fprintf(stderr, ". References to this label will not be resolved.");
919 for (node::child_iterator i=n->child_begin(), e=n->child_end() ; i!=e ; ++i)
921 collect_names_recursive(*i, path);
924 // Now we collect the phandles and properties that reference
926 for (node::property_iterator i=n->property_begin(), e=n->property_end() ; i!=e ; ++i)
928 for (property::value_iterator p=(*i)->begin(),pe=(*i)->end() ; p!=pe ; ++p)
932 phandles.push_back(&*p);
934 if (p->is_cross_reference())
936 cross_references.push_back(&*p);
939 if ((*i)->get_key() == string("phandle") ||
940 (*i)->get_key() == string("linux,phandle"))
942 if ((*i)->begin()->byte_data.size() != 4)
944 fprintf(stderr, "Invalid phandle value for node ");
946 fprintf(stderr, ". Should be a 4-byte value.\n");
951 uint32_t phandle = (*i)->begin()->get_as_uint32();
952 used_phandles.insert(std::make_pair(phandle, n));
959 device_tree::collect_names()
962 collect_names_recursive(root, p);
966 device_tree::resolve_cross_references()
968 for (std::vector<property_value*>::iterator i=cross_references.begin(), e=cross_references.end() ; i!=e ; ++i)
970 property_value* pv = *i;
971 node_path path = node_paths[pv->string_data];
972 // Skip the first name in the path. It's always "", and implicitly /
973 for (node_path::iterator p=path.begin()+1, pe=path.end() ; p!=pe ; ++p)
975 pv->byte_data.push_back('/');
976 p->first.push_to_buffer(pv->byte_data);
977 if (!(p->second.empty()))
979 pv->byte_data.push_back('@');
980 p->second.push_to_buffer(pv->byte_data);
983 pv->byte_data.push_back(0);
985 uint32_t phandle = 1;
986 for (std::vector<property_value*>::iterator i=phandles.begin(), e=phandles.end() ; i!=e ; ++i)
988 string target_name = (*i)->string_data;
989 node *target = node_names[target_name];
992 fprintf(stderr, "Failed to find node with label:");
994 fprintf(stderr, "\n");
998 // If there is an existing phandle, use it
999 property *p = target->get_property("phandle");
1002 p = target->get_property("linux,phandle");
1006 // Otherwise insert a new phandle node
1008 while (used_phandles.find(phandle) != used_phandles.end())
1010 // Note that we only don't need to
1011 // store this phandle in the set,
1012 // because we are monotonically
1013 // increasing the value of phandle and
1014 // so will only ever revisit this value
1015 // if we have used 2^32 phandles, at
1016 // which point our blob won't fit in
1017 // any 32-bit system and we've done
1018 // something badly wrong elsewhere
1022 push_big_endian(v.byte_data, phandle++);
1023 if (phandle_node_name == BOTH || phandle_node_name == LINUX)
1025 p = new property(string("linux,phandle"));
1027 target->add_property(p);
1029 if (phandle_node_name == BOTH || phandle_node_name == EPAPR)
1031 p = new property(string("phandle"));
1033 target->add_property(p);
1036 p->begin()->push_to_buffer((*i)->byte_data);
1037 assert((*i)->byte_data.size() == 4);
1042 device_tree::parse_roots(input_buffer &input, std::vector<node*> &roots)
1045 while (valid && input.consume('/'))
1048 node *n = node::parse(input, string("", 1), string(), string(), &defines);
1061 device_tree::buffer_for_file(const char *path)
1063 if (string(path) == string("-"))
1065 input_buffer *b = new stream_input_buffer();
1066 buffers.push_back(b);
1069 int source = open(path, O_RDONLY);
1072 fprintf(stderr, "Unable to open file %s\n", path);
1075 input_buffer *b = new mmap_input_buffer(source);
1076 // Keep the buffer that owns the memory around for the lifetime
1077 // of this FDT. Ones simply referring to it may have shorter
1079 buffers.push_back(b);
1084 template<class writer> void
1085 device_tree::write(int fd)
1087 dtb::string_table st;
1090 writer reservation_writer;
1091 writer struct_writer;
1092 writer strings_writer;
1094 // Build the reservation table
1095 reservation_writer.write_comment(string("Memory reservations"));
1096 reservation_writer.write_label(string("dt_reserve_map"));
1097 for (std::vector<reservation>::iterator i=reservations.begin(),
1098 e=reservations.end() ; i!=e ; ++i)
1100 reservation_writer.write_comment(string("Reservation start"));
1101 reservation_writer.write_data(i->first);
1102 reservation_writer.write_comment(string("Reservation length"));
1103 reservation_writer.write_data(i->first);
1105 // Write n spare reserve map entries, plus the trailing 0.
1106 for (uint32_t i=0 ; i<=spare_reserve_map_entries ; i++)
1108 reservation_writer.write_data((uint64_t)0);
1109 reservation_writer.write_data((uint64_t)0);
1113 struct_writer.write_comment(string("Device tree"));
1114 struct_writer.write_label(string("dt_struct_start"));
1115 root->write(struct_writer, st);
1116 struct_writer.write_token(dtb::FDT_END);
1117 struct_writer.write_label(string("dt_struct_end"));
1119 st.write(strings_writer);
1120 // Find the strings size before we stick padding on the end.
1121 // Note: We should possibly use a new writer for the padding.
1122 head.size_dt_strings = strings_writer.size();
1124 // Stick the padding in the strings writer, but after the
1125 // marker indicating that it's the end.
1126 // Note: We probably should add a padding call to the writer so
1127 // that the asm back end can write padding directives instead
1128 // of a load of 0 bytes.
1129 for (uint32_t i=0 ; i<blob_padding ; i++)
1131 strings_writer.write_data((uint8_t)0);
1133 head.totalsize = sizeof(head) + strings_writer.size() +
1134 struct_writer.size() + reservation_writer.size();
1135 while (head.totalsize < minimum_blob_size)
1138 strings_writer.write_data((uint8_t)0);
1140 head.off_dt_struct = sizeof(head) + reservation_writer.size();;
1141 head.off_dt_strings = head.off_dt_struct + struct_writer.size();
1142 head.off_mem_rsvmap = sizeof(head);
1143 head.boot_cpuid_phys = boot_cpu;
1144 head.size_dt_struct = struct_writer.size();
1145 head.write(head_writer);
1147 head_writer.write_to_file(fd);
1148 reservation_writer.write_to_file(fd);
1149 struct_writer.write_to_file(fd);
1150 strings_writer.write_label(string("dt_blob_end"));
1151 strings_writer.write_to_file(fd);
1155 device_tree::referenced_node(property_value &v)
1159 return node_names[v.string_data];
1163 return used_phandles[v.get_as_uint32()];
1169 device_tree::write_binary(int fd)
1171 write<dtb::binary_writer>(fd);
1175 device_tree::write_asm(int fd)
1177 write<dtb::asm_writer>(fd);
1181 device_tree::write_dts(int fd)
1183 FILE *file = fdopen(fd, "w");
1184 fputs("/dtc-v1/;\n\n", file);
1186 if (!reservations.empty())
1188 const char msg[] = "/memreserve/";
1189 fwrite(msg, sizeof(msg), 1, file);
1190 for (std::vector<reservation>::iterator i=reservations.begin(),
1191 e=reservations.end() ; i!=e ; ++i)
1193 fprintf(stderr, " %" PRIx64 " %" PRIx64, i->first, i->second);
1195 fputs(";\n\n", file);
1199 root->write_dts(file, 0);
1204 device_tree::parse_dtb(const char *fn, FILE *depfile)
1206 input_buffer *in = buffer_for_file(fn);
1212 input_buffer &input = *in;
1214 valid = h.read_dtb(input);
1215 boot_cpu = h.boot_cpuid_phys;
1216 if (h.last_comp_version > 17)
1218 fprintf(stderr, "Don't know how to read this version of the device tree blob");
1225 input_buffer reservation_map =
1226 input.buffer_from_offset(h.off_mem_rsvmap, 0);
1227 uint64_t start, length;
1230 if (!(reservation_map.consume_binary(start) &&
1231 reservation_map.consume_binary(length)))
1233 fprintf(stderr, "Failed to read memory reservation table\n");
1237 } while (!((start == 0) && (length == 0)));
1238 input_buffer struct_table =
1239 input.buffer_from_offset(h.off_dt_struct, h.size_dt_struct);
1240 input_buffer strings_table =
1241 input.buffer_from_offset(h.off_dt_strings, h.size_dt_strings);
1243 if (!(struct_table.consume_binary(token) &&
1244 (token == dtb::FDT_BEGIN_NODE)))
1246 fprintf(stderr, "Expected FDT_BEGIN_NODE token.\n");
1250 root = node::parse_dtb(struct_table, strings_table);
1251 if (!(struct_table.consume_binary(token) && (token == dtb::FDT_END)))
1253 fprintf(stderr, "Expected FDT_END token after parsing root node.\n");
1257 valid = (root != 0);
1261 device_tree::parse_dts(const char *fn, FILE *depfile)
1263 input_buffer *in = buffer_for_file(fn);
1269 std::vector<node*> roots;
1270 input_buffer &input = *in;
1272 bool read_header = false;
1274 if (input.consume("/dts-v1/;"))
1279 while(input.consume("/include/"))
1281 bool reallyInclude = true;
1282 if (input.consume("if "))
1285 string name = string::parse_property_name(input);
1286 // XXX: Error handling
1287 if (defines.find(name) == defines.end())
1289 reallyInclude = false;
1294 if (!input.consume('"'))
1296 input.parse_error("Expected quoted filename");
1301 while (input[length] != '"') length++;
1303 const char *file = (const char*)input;
1304 const char *dir = dirname(fn);
1305 int dir_length = strlen(dir);
1306 char *include_file = (char*)malloc(strlen(dir) + length + 2);
1307 memcpy(include_file, dir, dir_length);
1308 include_file[dir_length] = '/';
1309 memcpy(include_file+dir_length+1, file, length);
1310 include_file[dir_length+length+1] = 0;
1312 input.consume(include_file+dir_length+1);
1319 input_buffer *include_buffer = buffer_for_file(include_file);
1321 if (include_buffer == 0)
1323 for (std::vector<const char*>::iterator i=include_paths.begin(), e=include_paths.end() ; e!=i ; ++i)
1327 dir_length = strlen(dir);
1328 include_file = (char*)malloc(strlen(dir) +
1330 memcpy(include_file, dir, dir_length);
1331 include_file[dir_length] = '/';
1332 memcpy(include_file+dir_length+1, file, length);
1333 include_file[dir_length+length+1] = 0;
1334 include_buffer = buffer_for_file(include_file);
1335 if (include_buffer != 0)
1344 fputs(include_file, depfile);
1346 if (include_buffer == 0)
1351 input_buffer &include = *include_buffer;
1352 free((void*)include_file);
1356 include.next_token();
1357 read_header = include.consume("/dts-v1/;");
1359 parse_roots(include, roots);
1364 input.parse_error("Expected /dts-v1/; version string");
1366 // Read any memory reservations
1367 while(input.consume("/memreserve/"))
1369 long long start, len;
1371 // Read the start and length.
1372 if (!(input.consume_integer(start) &&
1373 (input.next_token(),
1374 input.consume_integer(len))))
1376 input.parse_error("Expected /dts-v1/; version string");
1380 reservations.push_back(reservation(start, len));
1382 parse_roots(input, roots);
1383 switch (roots.size())
1387 input.parse_error("Failed to find root node /.");
1395 for (std::vector<node*>::iterator i=roots.begin()+1,
1396 e=roots.end() ; i!=e ; ++i)
1398 root->merge_node(*i);
1405 resolve_cross_references();
1408 device_tree::~device_tree()
1414 while (!buffers.empty())
1416 delete buffers.back();
1419 for (define_map::iterator i=defines.begin(), e=defines.end() ;
1426 bool device_tree::parse_define(const char *def)
1428 char *val = strchr(def, '=');
1431 if (strlen(def) != 0)
1439 string name(def, val-def);
1441 input_buffer in = input_buffer(val, strlen(val));
1442 property *p = property::parse(in, name, string(), false);