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 #include "checking.hh"
48 * Checker that verifies that every node that has children has
49 * #address-cells and #size-cells properties.
51 struct address_cells_checker : public checker
53 address_cells_checker(const char *name) : checker(name) {}
54 virtual bool check_node(device_tree *tree, node *n)
56 // If this has no children, it trivially meets the
58 if (n->child_begin() == n->child_end())
62 bool found_address = false;
63 bool found_size = false;
64 for (node::property_iterator i=n->property_begin(),
65 e=n->property_end() ; i!=e ; ++i)
69 found_address = ((*i)->get_key() == "#address-cells");
73 found_size = ((*i)->get_key() == "#size-cells");
75 if (found_size && found_address)
82 report_error("Missing #address-cells property");
86 report_error("Missing #size-cells property");
88 return found_address && found_size;
91 } // anonymous namespace
94 checker::visit_node(device_tree *tree, node *n)
96 path.push_back(std::make_pair(n->name, n->unit_address));
98 if (!check_node(tree, n))
102 // Now check its properties
103 for (node::property_iterator i=n->property_begin(), e=n->property_end()
106 if (!check_property(tree, n, *i))
111 // And then recursively check the children
112 for (node::child_iterator i=n->child_begin(), e=n->child_end() ; i!=e ;
115 if (!visit_node(tree, *i))
125 checker::report_error(const char *errmsg)
127 fprintf(stderr, "Error: %s, while checking node: ", errmsg);
128 for (device_tree::node_path::iterator p=path.begin()+1, pe=path.end() ;
133 if (!(p->second.empty()))
139 fprintf(stderr, " [-W%s]\n", checker_name);
143 property_checker::check_property(device_tree *tree, node *n, property *p)
145 if (p->get_key() == key)
147 if (!check(tree, n, p))
149 report_error("property check failed");
157 property_size_checker::check(device_tree *tree, node *n, property *p)
160 for (property::value_iterator i=p->begin(),e=p->end() ; i!=e ; ++i)
166 psize += i->byte_data.size();
168 return psize == size;
171 template<property_value::value_type T>
173 check_manager::add_property_type_checker(const char *name, string prop)
175 checkers.insert(std::make_pair(string(name),
176 new property_type_checker<T>(name, prop)));
180 check_manager::add_property_size_checker(const char *name,
184 checkers.insert(std::make_pair(string(name),
185 new property_size_checker(name, prop, size)));
188 check_manager::~check_manager()
190 while (checkers.begin() != checkers.end())
192 delete checkers.begin()->second;
193 checkers.erase(checkers.begin());
195 while (disabled_checkers.begin() != disabled_checkers.end())
197 delete disabled_checkers.begin()->second;
198 disabled_checkers.erase(disabled_checkers.begin());
202 check_manager::check_manager()
204 // NOTE: All checks listed here MUST have a corresponding line
206 add_property_type_checker<property_value::STRING_LIST>(
207 "type-compatible", string("compatible"));
208 add_property_type_checker<property_value::STRING>(
209 "type-model", string("model"));
210 add_property_size_checker("type-phandle", string("phandle"), 4);
211 disabled_checkers.insert(std::make_pair(string("cells-attributes"),
212 new address_cells_checker("cells-attributes")));
216 check_manager::run_checks(device_tree *tree, bool keep_going)
219 for (std::map<string, checker*>::iterator i=checkers.begin(),
220 e=checkers.end() ; i!=e ; ++i)
222 success &= i->second->check_tree(tree);
223 if (!(success || keep_going))
232 check_manager::disable_checker(string name)
234 std::map<string, checker*>::iterator checker = checkers.find(name);
235 if (checker != checkers.end())
237 disabled_checkers.insert(std::make_pair(name,
239 checkers.erase(checker);
246 check_manager::enable_checker(string name)
248 std::map<string, checker*>::iterator checker =
249 disabled_checkers.find(name);
250 if (checker != disabled_checkers.end())
252 checkers.insert(std::make_pair(name, checker->second));
253 disabled_checkers.erase(checker);
259 } // namespace checking