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)) || bytes == 0)
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, "%02hhx", *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);
371 // If we're empty, do not push anything as value.
378 for (uint32_t i=0 ; i<length ; i++)
380 if (!(valid = structs.consume_binary(byte)))
382 fprintf(stderr, "Failed to read property value\n");
385 v.byte_data.push_back(byte);
390 void property::parse_define(input_buffer &input, define_map *defines)
395 input.parse_error("No predefined properties to match name\n");
399 string name = string::parse_property_name(input);
400 define_map::iterator found;
401 if ((name == string()) ||
402 ((found = defines->find(name)) == defines->end()))
404 input.parse_error("Undefined property name\n");
408 values.push_back((*found).second->values[0]);
411 property::property(input_buffer &input,
414 bool semicolonTerminated,
415 define_map *defines) : key(k), label(l), valid(true)
423 parse_define(input, defines);
430 input.parse_error("Invalid property value.");
443 parse_reference(input);
451 } while (input.consume(','));
452 if (semicolonTerminated && !input.consume(';'))
454 input.parse_error("Expected ; at end of property");
460 property::parse_dtb(input_buffer &structs, input_buffer &strings)
462 property *p = new property(structs, strings);
472 property::parse(input_buffer &input, string key, string label,
473 bool semicolonTerminated, define_map *defines)
475 property *p = new property(input, key, label, semicolonTerminated, defines);
485 property::write(dtb::output_writer &writer, dtb::string_table &strings)
487 writer.write_token(dtb::FDT_PROP);
488 byte_buffer value_buffer;
489 for (value_iterator i=begin(), e=end() ; i!=e ; ++i)
491 i->push_to_buffer(value_buffer);
493 writer.write_data((uint32_t)value_buffer.size());
494 writer.write_comment(key);
495 writer.write_data(strings.add_string(key));
496 writer.write_data(value_buffer);
500 property::write_dts(FILE *file, int indent)
502 for (int i=0 ; i<indent ; i++)
506 if (label != string())
518 for (value_iterator i=begin(), e=end() ; i!=e ; ++i)
532 node::parse_name(input_buffer &input, bool &is_property, const char *error)
541 return string::parse_property_name(input);
543 string n = string::parse_node_or_property_name(input, is_property);
548 input.parse_error(error);
555 node::node(input_buffer &structs, input_buffer &strings) : valid(true)
557 const char *name_start = (const char*)structs;
559 while (structs[0] != '\0' && structs[0] != '@')
564 name = string(name_start, name_length);
565 if (structs[0] == '@')
568 name_start = (const char*)structs;
570 while (structs[0] != '\0')
575 unit_address = string(name_start, name_length);
579 while (structs.consume_binary(token))
584 fprintf(stderr, "Unexpected token 0x%" PRIx32
585 " while parsing node.\n", token);
588 // Child node, parse it.
589 case dtb::FDT_BEGIN_NODE:
591 node *child = node::parse_dtb(structs, strings);
597 children.push_back(child);
600 // End of this node, no errors.
601 case dtb::FDT_END_NODE:
603 // Property, parse it.
606 property *prop = property::parse_dtb(structs, strings);
612 properties.push_back(prop);
616 // End of structs table. Should appear after
617 // the end of the last node.
619 fprintf(stderr, "Unexpected FDT_END token while parsing node.\n");
622 // NOPs are padding. Ignore them.
627 fprintf(stderr, "Failed to read token from structs table while parsing node.\n");
632 node::node(input_buffer &input, string n, string l, string a, define_map *defines) :
633 label(l), name(n), unit_address(a), valid(true)
635 if (!input.consume('{'))
637 input.parse_error("Expected { to start new device tree node.\n");
640 while (valid && !input.consume('}'))
642 // flag set if we find any characters that are only in
643 // the property name character set, not the node
644 bool is_property = false;
645 string child_name, child_label, child_address;
646 child_name = parse_name(input, is_property,
647 "Expected property or node name");
648 if (input.consume(':'))
650 // Node labels can contain any characters? The
651 // spec doesn't say, so we guess so...
653 child_label = child_name;
654 child_name = parse_name(input, is_property, "Expected property or node name");
656 if (input.consume('@'))
658 child_address = parse_name(input, is_property, "Expected unit address");
665 // If we're parsing a property, then we must actually do that.
666 if (input.consume('='))
668 property *p= property::parse(input, child_name,
669 child_label, true, defines);
676 properties.push_back(p);
679 else if (!is_property && input[0] == ('{'))
681 node *child = node::parse(input, child_name,
682 child_label, child_address, defines);
685 children.push_back(child);
692 else if (input.consume(';'))
694 properties.push_back(new property(child_name, child_label));
698 input.parse_error("Error parsing property.");
707 node::cmp_properties(property *p1, property *p2)
709 return p1->get_key() < p2->get_key();
713 node::cmp_children(node *c1, node *c2)
715 if (c1->name == c2->name)
717 return c1->unit_address < c2->unit_address;
719 return c1->name < c2->name;
725 std::sort(property_begin(), property_end(), cmp_properties);
726 std::sort(child_begin(), child_end(), cmp_children);
727 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
734 node::parse(input_buffer &input,
740 node *n = new node(input, name, label, address, defines);
750 node::parse_dtb(input_buffer &structs, input_buffer &strings)
752 node *n = new node(structs, strings);
763 while (!children.empty())
765 delete children.back();
768 while (!properties.empty())
770 delete properties.back();
771 properties.pop_back();
776 node::get_property(string key)
778 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
780 if ((*i)->get_key() == key)
789 node::merge_node(node *other)
791 if (!other->label.empty())
793 label = other->label;
795 // Note: this is an O(n*m) operation. It might be sensible to
796 // optimise this if we find that there are nodes with very
797 // large numbers of properties, but for typical usage the
798 // entire vector will fit (easily) into cache, so iterating
799 // over it repeatedly isn't that expensive.
800 while (!other->properties.empty())
802 property *p = other->properties.front();
803 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
805 if ((*i)->get_key() == p->get_key())
813 other->properties.erase(other->properties.begin());
815 while (!other->children.empty())
817 node *c = other->children.front();
819 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
821 if ((*i)->name == c->name && (*i)->unit_address == c->unit_address)
831 children.push_back(c);
833 other->children.erase(other->children.begin());
838 node::write(dtb::output_writer &writer, dtb::string_table &strings)
840 writer.write_token(dtb::FDT_BEGIN_NODE);
841 byte_buffer name_buffer;
842 name.push_to_buffer(name_buffer);
843 if (unit_address != string())
845 name_buffer.push_back('@');
846 unit_address.push_to_buffer(name_buffer);
848 writer.write_comment(name);
849 writer.write_data(name_buffer);
850 writer.write_data((uint8_t)0);
851 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
853 (*i)->write(writer, strings);
855 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
857 (*i)->write(writer, strings);
859 writer.write_token(dtb::FDT_END_NODE);
863 node::write_dts(FILE *file, int indent)
865 for (int i=0 ; i<indent ; i++)
869 if (label != string())
874 if (name != string())
878 if (unit_address != string())
881 unit_address.print(file);
883 fputs(" {\n\n", file);
884 for (property_iterator i=property_begin(), e=property_end() ; i!=e ; ++i)
886 (*i)->write_dts(file, indent+1);
888 for (child_iterator i=child_begin(), e=child_end() ; i!=e ; ++i)
890 (*i)->write_dts(file, indent+1);
892 for (int i=0 ; i<indent ; i++)
900 device_tree::collect_names_recursive(node* n, node_path &path)
902 string name = n->label;
903 path.push_back(std::make_pair(n->name, n->unit_address));
904 if (name != string())
906 if (node_names.find(name) == node_names.end())
908 node_names.insert(std::make_pair(name, n));
909 node_paths.insert(std::make_pair(name, path));
913 node_names[name] = (node*)-1;
914 std::map<string, node_path>::iterator i = node_paths.find(name);
915 if (i != node_paths.end())
917 node_paths.erase(name);
919 fprintf(stderr, "Label not unique: ");
921 fprintf(stderr, ". References to this label will not be resolved.");
924 for (node::child_iterator i=n->child_begin(), e=n->child_end() ; i!=e ; ++i)
926 collect_names_recursive(*i, path);
929 // Now we collect the phandles and properties that reference
931 for (node::property_iterator i=n->property_begin(), e=n->property_end() ; i!=e ; ++i)
933 for (property::value_iterator p=(*i)->begin(),pe=(*i)->end() ; p!=pe ; ++p)
937 phandles.push_back(&*p);
939 if (p->is_cross_reference())
941 cross_references.push_back(&*p);
944 if ((*i)->get_key() == string("phandle") ||
945 (*i)->get_key() == string("linux,phandle"))
947 if ((*i)->begin()->byte_data.size() != 4)
949 fprintf(stderr, "Invalid phandle value for node ");
951 fprintf(stderr, ". Should be a 4-byte value.\n");
956 uint32_t phandle = (*i)->begin()->get_as_uint32();
957 used_phandles.insert(std::make_pair(phandle, n));
964 device_tree::collect_names()
967 collect_names_recursive(root, p);
971 device_tree::resolve_cross_references()
973 for (std::vector<property_value*>::iterator i=cross_references.begin(), e=cross_references.end() ; i!=e ; ++i)
975 property_value* pv = *i;
976 node_path path = node_paths[pv->string_data];
977 // Skip the first name in the path. It's always "", and implicitly /
978 for (node_path::iterator p=path.begin()+1, pe=path.end() ; p!=pe ; ++p)
980 pv->byte_data.push_back('/');
981 p->first.push_to_buffer(pv->byte_data);
982 if (!(p->second.empty()))
984 pv->byte_data.push_back('@');
985 p->second.push_to_buffer(pv->byte_data);
988 pv->byte_data.push_back(0);
990 uint32_t phandle = 1;
991 for (std::vector<property_value*>::iterator i=phandles.begin(), e=phandles.end() ; i!=e ; ++i)
993 string target_name = (*i)->string_data;
994 node *target = node_names[target_name];
997 fprintf(stderr, "Failed to find node with label:");
999 fprintf(stderr, "\n");
1003 // If there is an existing phandle, use it
1004 property *p = target->get_property("phandle");
1007 p = target->get_property("linux,phandle");
1011 // Otherwise insert a new phandle node
1013 while (used_phandles.find(phandle) != used_phandles.end())
1015 // Note that we only don't need to
1016 // store this phandle in the set,
1017 // because we are monotonically
1018 // increasing the value of phandle and
1019 // so will only ever revisit this value
1020 // if we have used 2^32 phandles, at
1021 // which point our blob won't fit in
1022 // any 32-bit system and we've done
1023 // something badly wrong elsewhere
1027 push_big_endian(v.byte_data, phandle++);
1028 if (phandle_node_name == BOTH || phandle_node_name == LINUX)
1030 p = new property(string("linux,phandle"));
1032 target->add_property(p);
1034 if (phandle_node_name == BOTH || phandle_node_name == EPAPR)
1036 p = new property(string("phandle"));
1038 target->add_property(p);
1041 p->begin()->push_to_buffer((*i)->byte_data);
1042 assert((*i)->byte_data.size() == 4);
1047 device_tree::parse_roots(input_buffer &input, std::vector<node*> &roots)
1050 while (valid && input.consume('/'))
1053 node *n = node::parse(input, string("", 1), string(), string(), &defines);
1067 device_tree::buffer_for_file(const char *path)
1069 if (string(path) == string("-"))
1071 input_buffer *b = new stream_input_buffer();
1072 buffers.push_back(b);
1075 int source = open(path, O_RDONLY);
1078 fprintf(stderr, "Unable to open file %s\n", path);
1081 input_buffer *b = new mmap_input_buffer(source);
1082 // Keep the buffer that owns the memory around for the lifetime
1083 // of this FDT. Ones simply referring to it may have shorter
1085 buffers.push_back(b);
1090 template<class writer> void
1091 device_tree::write(int fd)
1093 dtb::string_table st;
1096 writer reservation_writer;
1097 writer struct_writer;
1098 writer strings_writer;
1100 // Build the reservation table
1101 reservation_writer.write_comment(string("Memory reservations"));
1102 reservation_writer.write_label(string("dt_reserve_map"));
1103 for (std::vector<reservation>::iterator i=reservations.begin(),
1104 e=reservations.end() ; i!=e ; ++i)
1106 reservation_writer.write_comment(string("Reservation start"));
1107 reservation_writer.write_data(i->first);
1108 reservation_writer.write_comment(string("Reservation length"));
1109 reservation_writer.write_data(i->first);
1111 // Write n spare reserve map entries, plus the trailing 0.
1112 for (uint32_t i=0 ; i<=spare_reserve_map_entries ; i++)
1114 reservation_writer.write_data((uint64_t)0);
1115 reservation_writer.write_data((uint64_t)0);
1119 struct_writer.write_comment(string("Device tree"));
1120 struct_writer.write_label(string("dt_struct_start"));
1121 root->write(struct_writer, st);
1122 struct_writer.write_token(dtb::FDT_END);
1123 struct_writer.write_label(string("dt_struct_end"));
1125 st.write(strings_writer);
1126 // Find the strings size before we stick padding on the end.
1127 // Note: We should possibly use a new writer for the padding.
1128 head.size_dt_strings = strings_writer.size();
1130 // Stick the padding in the strings writer, but after the
1131 // marker indicating that it's the end.
1132 // Note: We probably should add a padding call to the writer so
1133 // that the asm back end can write padding directives instead
1134 // of a load of 0 bytes.
1135 for (uint32_t i=0 ; i<blob_padding ; i++)
1137 strings_writer.write_data((uint8_t)0);
1139 head.totalsize = sizeof(head) + strings_writer.size() +
1140 struct_writer.size() + reservation_writer.size();
1141 while (head.totalsize < minimum_blob_size)
1144 strings_writer.write_data((uint8_t)0);
1146 head.off_dt_struct = sizeof(head) + reservation_writer.size();;
1147 head.off_dt_strings = head.off_dt_struct + struct_writer.size();
1148 head.off_mem_rsvmap = sizeof(head);
1149 head.boot_cpuid_phys = boot_cpu;
1150 head.size_dt_struct = struct_writer.size();
1151 head.write(head_writer);
1153 head_writer.write_to_file(fd);
1154 reservation_writer.write_to_file(fd);
1155 struct_writer.write_to_file(fd);
1156 strings_writer.write_label(string("dt_blob_end"));
1157 strings_writer.write_to_file(fd);
1161 device_tree::referenced_node(property_value &v)
1165 return node_names[v.string_data];
1169 return used_phandles[v.get_as_uint32()];
1175 device_tree::write_binary(int fd)
1177 write<dtb::binary_writer>(fd);
1181 device_tree::write_asm(int fd)
1183 write<dtb::asm_writer>(fd);
1187 device_tree::write_dts(int fd)
1189 FILE *file = fdopen(fd, "w");
1190 fputs("/dts-v1/;\n\n", file);
1192 if (!reservations.empty())
1194 const char msg[] = "/memreserve/";
1195 fwrite(msg, sizeof(msg), 1, file);
1196 for (std::vector<reservation>::iterator i=reservations.begin(),
1197 e=reservations.end() ; i!=e ; ++i)
1199 fprintf(file, " %" PRIx64 " %" PRIx64, i->first, i->second);
1201 fputs(";\n\n", file);
1205 root->write_dts(file, 0);
1210 device_tree::parse_dtb(const char *fn, FILE *depfile)
1212 input_buffer *in = buffer_for_file(fn);
1218 input_buffer &input = *in;
1220 valid = h.read_dtb(input);
1221 boot_cpu = h.boot_cpuid_phys;
1222 if (h.last_comp_version > 17)
1224 fprintf(stderr, "Don't know how to read this version of the device tree blob");
1231 input_buffer reservation_map =
1232 input.buffer_from_offset(h.off_mem_rsvmap, 0);
1233 uint64_t start, length;
1236 if (!(reservation_map.consume_binary(start) &&
1237 reservation_map.consume_binary(length)))
1239 fprintf(stderr, "Failed to read memory reservation table\n");
1243 } while (!((start == 0) && (length == 0)));
1244 input_buffer struct_table =
1245 input.buffer_from_offset(h.off_dt_struct, h.size_dt_struct);
1246 input_buffer strings_table =
1247 input.buffer_from_offset(h.off_dt_strings, h.size_dt_strings);
1249 if (!(struct_table.consume_binary(token) &&
1250 (token == dtb::FDT_BEGIN_NODE)))
1252 fprintf(stderr, "Expected FDT_BEGIN_NODE token.\n");
1256 root = node::parse_dtb(struct_table, strings_table);
1257 if (!(struct_table.consume_binary(token) && (token == dtb::FDT_END)))
1259 fprintf(stderr, "Expected FDT_END token after parsing root node.\n");
1263 valid = (root != 0);
1267 device_tree::parse_dts(const char *fn, FILE *depfile)
1269 input_buffer *in = buffer_for_file(fn);
1275 std::vector<node*> roots;
1276 input_buffer &input = *in;
1278 bool read_header = false;
1280 if (input.consume("/dts-v1/;"))
1285 while(input.consume("/include/"))
1287 bool reallyInclude = true;
1288 if (input.consume("if "))
1291 string name = string::parse_property_name(input);
1292 // XXX: Error handling
1293 if (defines.find(name) == defines.end())
1295 reallyInclude = false;
1300 if (!input.consume('"'))
1302 input.parse_error("Expected quoted filename");
1307 while (input[length] != '"') length++;
1309 const char *file = (const char*)input;
1310 const char *dir = dirname(fn);
1311 int dir_length = strlen(dir);
1312 char *include_file = (char*)malloc(strlen(dir) + length + 2);
1313 memcpy(include_file, dir, dir_length);
1314 include_file[dir_length] = '/';
1315 memcpy(include_file+dir_length+1, file, length);
1316 include_file[dir_length+length+1] = 0;
1318 input.consume(include_file+dir_length+1);
1325 input_buffer *include_buffer = buffer_for_file(include_file);
1327 if (include_buffer == 0)
1329 for (std::vector<const char*>::iterator i=include_paths.begin(), e=include_paths.end() ; e!=i ; ++i)
1333 dir_length = strlen(dir);
1334 include_file = (char*)malloc(strlen(dir) +
1336 memcpy(include_file, dir, dir_length);
1337 include_file[dir_length] = '/';
1338 memcpy(include_file+dir_length+1, file, length);
1339 include_file[dir_length+length+1] = 0;
1340 include_buffer = buffer_for_file(include_file);
1341 if (include_buffer != 0)
1350 fputs(include_file, depfile);
1352 if (include_buffer == 0)
1357 input_buffer &include = *include_buffer;
1358 free((void*)include_file);
1362 include.next_token();
1363 read_header = include.consume("/dts-v1/;");
1365 parse_roots(include, roots);
1370 input.parse_error("Expected /dts-v1/; version string");
1372 // Read any memory reservations
1373 while(input.consume("/memreserve/"))
1375 long long start, len;
1377 // Read the start and length.
1378 if (!(input.consume_integer(start) &&
1379 (input.next_token(),
1380 input.consume_integer(len))))
1382 input.parse_error("Expected size on /memreserve/ node.");
1386 reservations.push_back(reservation(start, len));
1388 parse_roots(input, roots);
1389 switch (roots.size())
1393 input.parse_error("Failed to find root node /.");
1401 for (std::vector<node*>::iterator i=roots.begin()+1,
1402 e=roots.end() ; i!=e ; ++i)
1404 root->merge_node(*i);
1411 resolve_cross_references();
1414 device_tree::~device_tree()
1420 while (!buffers.empty())
1422 delete buffers.back();
1425 for (define_map::iterator i=defines.begin(), e=defines.end() ;
1432 bool device_tree::parse_define(const char *def)
1434 char *val = strchr(def, '=');
1437 if (strlen(def) != 0)
1445 string name(def, val-def);
1447 input_buffer in = input_buffer(val, strlen(val));
1448 property *p = property::parse(in, name, string(), false);