2 * SPDX-License-Identifier: BSD-2-Clause
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
33 #include "checking.hh"
47 struct deleted_node_checker : public checker
49 deleted_node_checker(const char *name) : checker(name) {}
50 virtual bool check_node(device_tree *, const node_ptr &n)
52 auto &deleted = n->deleted_child_nodes();
57 bool plural = deleted.size() > 1;
58 string errmsg("Attempts to delete ");
59 errmsg += plural ? "nodes" : "node";
61 errmsg += plural ? "were" : "was";
62 errmsg += " not added in merge: ";
63 for (auto &d : deleted)
67 report_error(errmsg.c_str());
72 * Checker that verifies that every node that has children has
73 * #address-cells and #size-cells properties.
75 struct address_cells_checker : public checker
77 address_cells_checker(const char *name) : checker(name) {}
78 virtual bool check_node(device_tree *, const node_ptr &n)
80 // If this has no children, it trivially meets the
82 if (n->child_begin() == n->child_end())
86 bool found_address = false;
87 bool found_size = false;
88 for (auto i=n->property_begin(), e=n->property_end() ; i!=e ; ++i)
92 found_address = ((*i)->get_key() == "#address-cells");
96 found_size = ((*i)->get_key() == "#size-cells");
98 if (found_size && found_address)
105 report_error("Missing #address-cells property");
109 report_error("Missing #size-cells property");
111 return found_address && found_size;
114 } // anonymous namespace
117 checker::visit_node(device_tree *tree, const node_ptr &n)
119 path.push_back(std::make_pair(n->name, n->unit_address));
121 if (!check_node(tree, n))
125 // Now check its properties
126 for (auto i=n->property_begin(), e=n->property_end() ; i!=e ; ++i)
128 if (!check_property(tree, n, *i))
133 // And then recursively check the children
134 for (node::child_iterator i=n->child_begin(), e=n->child_end() ; i!=e ;
137 if (!visit_node(tree, *i))
147 checker::report_error(const char *errmsg)
149 fprintf(stderr, "Error: %s, while checking node: ", errmsg);
153 puts(p.first.c_str());
154 if (!(p.second.empty()))
157 puts(p.second.c_str());
160 fprintf(stderr, " [-W%s]\n", checker_name);
164 property_checker::check_property(device_tree *tree, const node_ptr &n, property_ptr p)
166 if (p->get_key() == key)
168 if (!check(tree, n, p))
170 report_error("property check failed");
178 property_size_checker::check(device_tree *, const node_ptr &, property_ptr p)
181 for (property::value_iterator i=p->begin(),e=p->end() ; i!=e ; ++i)
187 psize += i->byte_data.size();
189 return psize == size;
192 template<property_value::value_type T>
194 check_manager::add_property_type_checker(const char *name, const string &prop)
196 checkers.insert(std::make_pair(string(name),
197 new property_type_checker<T>(name, prop)));
201 check_manager::add_property_size_checker(const char *name,
205 checkers.insert(std::make_pair(string(name),
206 new property_size_checker(name, prop, size)));
209 check_manager::~check_manager()
211 while (checkers.begin() != checkers.end())
213 delete checkers.begin()->second;
214 checkers.erase(checkers.begin());
216 while (disabled_checkers.begin() != disabled_checkers.end())
218 delete disabled_checkers.begin()->second;
219 disabled_checkers.erase(disabled_checkers.begin());
223 check_manager::check_manager()
225 // NOTE: All checks listed here MUST have a corresponding line
227 add_property_type_checker<property_value::STRING_LIST>(
228 "type-compatible", string("compatible"));
229 add_property_type_checker<property_value::STRING>(
230 "type-model", string("model"));
231 add_property_size_checker("type-phandle", string("phandle"), 4);
232 disabled_checkers.insert(std::make_pair(string("cells-attributes"),
233 new address_cells_checker("cells-attributes")));
234 checkers.insert(std::make_pair(string("deleted-nodes"),
235 new deleted_node_checker("deleted-nodes")));
239 check_manager::run_checks(device_tree *tree, bool keep_going)
242 for (auto &i : checkers)
244 success &= i.second->check_tree(tree);
245 if (!(success || keep_going))
254 check_manager::disable_checker(const string &name)
256 auto checker = checkers.find(name);
257 if (checker != checkers.end())
259 disabled_checkers.insert(std::make_pair(name,
261 checkers.erase(checker);
268 check_manager::enable_checker(const string &name)
270 auto checker = disabled_checkers.find(name);
271 if (checker != disabled_checkers.end())
273 checkers.insert(std::make_pair(name, checker->second));
274 disabled_checkers.erase(checker);
280 } // namespace checking