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 #include "checking.hh"
49 struct deleted_node_checker : public checker
51 deleted_node_checker(const char *name) : checker(name) {}
52 virtual bool check_node(device_tree *, const node_ptr &n)
54 auto &deleted = n->deleted_child_nodes();
59 bool plural = deleted.size() > 1;
60 string errmsg("Attempts to delete ");
61 errmsg += plural ? "nodes" : "node";
63 errmsg += plural ? "were" : "was";
64 errmsg += " not added in merge: ";
65 for (auto &d : deleted)
69 report_error(errmsg.c_str());
74 * Checker that verifies that every node that has children has
75 * #address-cells and #size-cells properties.
77 struct address_cells_checker : public checker
79 address_cells_checker(const char *name) : checker(name) {}
80 virtual bool check_node(device_tree *, const node_ptr &n)
82 // If this has no children, it trivially meets the
84 if (n->child_begin() == n->child_end())
88 bool found_address = false;
89 bool found_size = false;
90 for (auto i=n->property_begin(), e=n->property_end() ; i!=e ; ++i)
94 found_address = ((*i)->get_key() == "#address-cells");
98 found_size = ((*i)->get_key() == "#size-cells");
100 if (found_size && found_address)
107 report_error("Missing #address-cells property");
111 report_error("Missing #size-cells property");
113 return found_address && found_size;
116 } // anonymous namespace
119 checker::visit_node(device_tree *tree, const node_ptr &n)
121 path.push_back(std::make_pair(n->name, n->unit_address));
123 if (!check_node(tree, n))
127 // Now check its properties
128 for (auto i=n->property_begin(), e=n->property_end() ; i!=e ; ++i)
130 if (!check_property(tree, n, *i))
135 // And then recursively check the children
136 for (node::child_iterator i=n->child_begin(), e=n->child_end() ; i!=e ;
139 if (!visit_node(tree, *i))
149 checker::report_error(const char *errmsg)
151 fprintf(stderr, "Error: %s, while checking node: ", errmsg);
155 puts(p.first.c_str());
156 if (!(p.second.empty()))
159 puts(p.second.c_str());
162 fprintf(stderr, " [-W%s]\n", checker_name);
166 property_checker::check_property(device_tree *tree, const node_ptr &n, property_ptr p)
168 if (p->get_key() == key)
170 if (!check(tree, n, p))
172 report_error("property check failed");
180 property_size_checker::check(device_tree *, const node_ptr &, property_ptr p)
183 for (property::value_iterator i=p->begin(),e=p->end() ; i!=e ; ++i)
189 psize += i->byte_data.size();
191 return psize == size;
194 template<property_value::value_type T>
196 check_manager::add_property_type_checker(const char *name, const string &prop)
198 checkers.insert(std::make_pair(string(name),
199 new property_type_checker<T>(name, prop)));
203 check_manager::add_property_size_checker(const char *name,
207 checkers.insert(std::make_pair(string(name),
208 new property_size_checker(name, prop, size)));
211 check_manager::~check_manager()
213 while (checkers.begin() != checkers.end())
215 delete checkers.begin()->second;
216 checkers.erase(checkers.begin());
218 while (disabled_checkers.begin() != disabled_checkers.end())
220 delete disabled_checkers.begin()->second;
221 disabled_checkers.erase(disabled_checkers.begin());
225 check_manager::check_manager()
227 // NOTE: All checks listed here MUST have a corresponding line
229 add_property_type_checker<property_value::STRING_LIST>(
230 "type-compatible", string("compatible"));
231 add_property_type_checker<property_value::STRING>(
232 "type-model", string("model"));
233 add_property_size_checker("type-phandle", string("phandle"), 4);
234 disabled_checkers.insert(std::make_pair(string("cells-attributes"),
235 new address_cells_checker("cells-attributes")));
236 checkers.insert(std::make_pair(string("deleted-nodes"),
237 new deleted_node_checker("deleted-nodes")));
241 check_manager::run_checks(device_tree *tree, bool keep_going)
244 for (auto &i : checkers)
246 success &= i.second->check_tree(tree);
247 if (!(success || keep_going))
256 check_manager::disable_checker(const string &name)
258 auto checker = checkers.find(name);
259 if (checker != checkers.end())
261 disabled_checkers.insert(std::make_pair(name,
263 checkers.erase(checker);
270 check_manager::enable_checker(const string &name)
272 auto checker = disabled_checkers.find(name);
273 if (checker != disabled_checkers.end())
275 checkers.insert(std::make_pair(name, checker->second));
276 disabled_checkers.erase(checker);
282 } // namespace checking