cregit-Linux how code gets into the kernel

Release 4.15 scripts/dtc/checks.c

Directory: scripts/dtc
/*
 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2007.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *                                                                   USA
 */

#include "dtc.h"

#ifdef TRACE_CHECKS

#define TRACE(c, ...) \
	do { \
                fprintf(stderr, "=== %s: ", (c)->name); \
                fprintf(stderr, __VA_ARGS__); \
                fprintf(stderr, "\n"); \
        } while (0)
#else

#define TRACE(c, fmt, ...)	do { } while (0)
#endif


enum checkstatus {
	
UNCHECKED = 0,
	
PREREQ,
	
PASSED,
	
FAILED,
};

struct check;


typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);


struct check {
	
const char *name;
	
check_fn fn;
	
void *data;
	

bool warn, error;
	
enum checkstatus status;
	
bool inprogress;
	
int num_prereqs;
	
struct check **prereq;
};


#define CHECK_ENTRY(_nm, _fn, _d, _w, _e, ...)	       \
	static struct check *_nm##_prereqs[] = { __VA_ARGS__ }; \
        static struct check _nm = { \
                .name = #_nm, \
                .fn = (_fn), \
                .data = (_d), \
                .warn = (_w), \
                .error = (_e), \
                .status = UNCHECKED, \
                .num_prereqs = ARRAY_SIZE(_nm##_prereqs), \
                .prereq = _nm##_prereqs, \
        };

#define WARNING(_nm, _fn, _d, ...) \
	CHECK_ENTRY(_nm, _fn, _d, true, false, __VA_ARGS__)

#define ERROR(_nm, _fn, _d, ...) \
	CHECK_ENTRY(_nm, _fn, _d, false, true, __VA_ARGS__)

#define CHECK(_nm, _fn, _d, ...) \
	CHECK_ENTRY(_nm, _fn, _d, false, false, __VA_ARGS__)


static inline void  PRINTF(3, 4) check_msg(struct check *c, struct dt_info *dti,
					   const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	if ((c->warn && (quiet < 1))
	    || (c->error && (quiet < 2))) {
		fprintf(stderr, "%s: %s (%s): ",
			strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
			(c->error) ? "ERROR" : "Warning", c->name);
		vfprintf(stderr, fmt, ap);
		fprintf(stderr, "\n");
	}
	va_end(ap);
}


#define FAIL(c, dti, ...)						\
	do {                                                            \
                TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__);  \
                (c)->status = FAILED;                                   \
                check_msg((c), dti, __VA_ARGS__);                       \
        } while (0)


static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node) { struct node *child; TRACE(c, "%s", node->fullpath); if (c->fn) c->fn(c, dti, node); for_each_child(node, child) check_nodes_props(c, dti, child); }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson6391.30%150.00%
Rob Herring68.70%150.00%
Total69100.00%2100.00%


static bool run_check(struct check *c, struct dt_info *dti) { struct node *dt = dti->dt; bool error = false; int i; assert(!c->inprogress); if (c->status != UNCHECKED) goto out; c->inprogress = true; for (i = 0; i < c->num_prereqs; i++) { struct check *prq = c->prereq[i]; error = error || run_check(prq, dti); if (prq->status != PASSED) { c->status = PREREQ; check_msg(c, dti, "Failed prerequisite '%s'", c->prereq[i]->name); } } if (c->status != UNCHECKED) goto out; check_nodes_props(c, dti, dt); if (c->status == UNCHECKED) c->status = PASSED; TRACE(c, "\tCompleted, status %d", c->status); out: c->inprogress = false; if ((c->status != PASSED) && (c->error)) error = true; return error; }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson18387.98%120.00%
Rob Herring2411.54%360.00%
Stephen Warren10.48%120.00%
Total208100.00%5100.00%

/* * Utility check functions */ /* A check which always fails, for testing purposes only */
static inline void check_always_fail(struct check *c, struct dt_info *dti, struct node *node) { FAIL(c, dti, "always_fail check"); }

Contributors

PersonTokensPropCommitsCommitProp
Stephen Warren2374.19%133.33%
Rob Herring825.81%266.67%
Total31100.00%3100.00%

CHECK(always_fail, check_always_fail, NULL);
static void check_is_string(struct check *c, struct dt_info *dti, struct node *node) { struct property *prop; char *propname = c->data; prop = get_property(node, propname); if (!prop) return; /* Not present, assumed ok */ if (!data_is_one_string(prop->val)) FAIL(c, dti, "\"%s\" property in %s is not a string", propname, node->fullpath); }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson7194.67%133.33%
Rob Herring45.33%266.67%
Total75100.00%3100.00%

#define WARNING_IF_NOT_STRING(nm, propname) \ WARNING(nm, check_is_string, (propname)) #define ERROR_IF_NOT_STRING(nm, propname) \ ERROR(nm, check_is_string, (propname))
static void check_is_cell(struct check *c, struct dt_info *dti, struct node *node) { struct property *prop; char *propname = c->data; prop = get_property(node, propname); if (!prop) return; /* Not present, assumed ok */ if (prop->val.len != sizeof(cell_t)) FAIL(c, dti, "\"%s\" property in %s is not a single cell", propname, node->fullpath); }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson7494.87%133.33%
Rob Herring45.13%266.67%
Total78100.00%3100.00%

#define WARNING_IF_NOT_CELL(nm, propname) \ WARNING(nm, check_is_cell, (propname)) #define ERROR_IF_NOT_CELL(nm, propname) \ ERROR(nm, check_is_cell, (propname)) /* * Structural check functions */
static void check_duplicate_node_names(struct check *c, struct dt_info *dti, struct node *node) { struct node *child, *child2; for_each_child(node, child) for (child2 = child->next_sibling; child2; child2 = child2->next_sibling) if (streq(child->name, child2->name)) FAIL(c, dti, "Duplicate node name %s", child->fullpath); }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson7394.81%133.33%
Rob Herring45.19%266.67%
Total77100.00%3100.00%

ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
static void check_duplicate_property_names(struct check *c, struct dt_info *dti, struct node *node) { struct property *prop, *prop2; for_each_property(node, prop) { for (prop2 = prop->next; prop2; prop2 = prop2->next) { if (prop2->deleted) continue; if (streq(prop->name, prop2->name)) FAIL(c, dti, "Duplicate property name %s in %s", prop->name, node->fullpath); } } }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson7783.70%125.00%
Stephen Warren1111.96%125.00%
Rob Herring44.35%250.00%
Total92100.00%4100.00%

ERROR(duplicate_property_names, check_duplicate_property_names, NULL); #define LOWERCASE "abcdefghijklmnopqrstuvwxyz" #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define DIGITS "0123456789" #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-" #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
static void check_node_name_chars(struct check *c, struct dt_info *dti, struct node *node) { int n = strspn(node->name, c->data); if (n < strlen(node->name)) FAIL(c, dti, "Bad character '%c' in node %s", node->name[n], node->fullpath); }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson6293.94%133.33%
Rob Herring46.06%266.67%
Total66100.00%3100.00%

ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
static void check_node_name_chars_strict(struct check *c, struct dt_info *dti, struct node *node) { int n = strspn(node->name, c->data); if (n < node->basenamelen) FAIL(c, dti, "Character '%c' not recommended in node %s", node->name[n], node->fullpath); }

Contributors

PersonTokensPropCommitsCommitProp
Rob Herring63100.00%1100.00%
Total63100.00%1100.00%

CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
static void check_node_name_format(struct check *c, struct dt_info *dti, struct node *node) { if (strchr(get_unitname(node), '@')) FAIL(c, dti, "Node %s has multiple '@' characters in name", node->fullpath); }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson4291.30%133.33%
Rob Herring48.70%266.67%
Total46100.00%3100.00%

ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti, struct node *node) { const char *unitname = get_unitname(node); struct property *prop = get_property(node, "reg"); if (!prop) { prop = get_property(node, "ranges"); if (prop && !prop->val.len) prop = NULL; } if (prop) { if (!unitname[0]) FAIL(c, dti, "Node %s has a reg or ranges property, but no unit name", node->fullpath); } else { if (unitname[0]) FAIL(c, dti, "Node %s has a unit name, but no reg property", node->fullpath); } }

Contributors

PersonTokensPropCommitsCommitProp
Rob Herring124100.00%3100.00%
Total124100.00%3100.00%

WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
static void check_property_name_chars(struct check *c, struct dt_info *dti, struct node *node) { struct property *prop; for_each_property(node, prop) { int n = strspn(prop->name, c->data); if (n < strlen(prop->name)) FAIL(c, dti, "Bad character '%c' in property name \"%s\", node %s", prop->name[n], prop->name, node->fullpath); } }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson6983.13%133.33%
Rob Herring1416.87%266.67%
Total83100.00%3100.00%

ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
static void check_property_name_chars_strict(struct check *c, struct dt_info *dti, struct node *node) { struct property *prop; for_each_property(node, prop) { const char *name = prop->name; int n = strspn(name, c->data); if (n == strlen(prop->name)) continue; /* Certain names are whitelisted */ if (streq(name, "device_type")) continue; /* * # is only allowed at the beginning of property names not counting * the vendor prefix. */ if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) { name += n + 1; n = strspn(name, c->data); } if (n < strlen(name)) FAIL(c, dti, "Character '%c' not recommended in property name \"%s\", node %s", name[n], prop->name, node->fullpath); } }

Contributors

PersonTokensPropCommitsCommitProp
Rob Herring13284.08%250.00%
David Gibson159.55%125.00%
John Bonesio106.37%125.00%
Total157100.00%4100.00%

CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT); #define DESCLABEL_FMT "%s%s%s%s%s" #define DESCLABEL_ARGS(node,prop,mark) \ ((mark) ? "value of " : ""), \ ((prop) ? "'" : ""), \ ((prop) ? (prop)->name : ""), \ ((prop) ? "' in " : ""), (node)->fullpath
static void check_duplicate_label(struct check *c, struct dt_info *dti, const char *label, struct node *node, struct property *prop, struct marker *mark) { struct node *dt = dti->dt; struct node *othernode = NULL; struct property *otherprop = NULL; struct marker *othermark = NULL; othernode = get_node_by_label(dt, label); if (!othernode) otherprop = get_property_by_label(dt, label, &othernode); if (!othernode) othermark = get_marker_label(dt, label, &othernode, &otherprop); if (!othernode) return; if ((othernode != node) || (otherprop != prop) || (othermark != mark)) FAIL(c, dti, "Duplicate label '%s' on " DESCLABEL_FMT " and " DESCLABEL_FMT, label, DESCLABEL_ARGS(node, prop, mark), DESCLABEL_ARGS(othernode, otherprop, othermark)); }

Contributors

PersonTokensPropCommitsCommitProp
John Bonesio9555.88%133.33%
Rob Herring7443.53%133.33%
David Gibson10.59%133.33%
Total170100.00%3100.00%


static void check_duplicate_label_node(struct check *c, struct dt_info *dti, struct node *node) { struct label *l; struct property *prop; for_each_label(node->labels, l) check_duplicate_label(c, dti, l->label, node, NULL, NULL); for_each_property(node, prop) { struct marker *m = prop->val.markers; for_each_label(prop->labels, l) check_duplicate_label(c, dti, l->label, node, prop, NULL); for_each_marker_of_type(m, LABEL) check_duplicate_label(c, dti, m->ref, node, prop, m); } }

Contributors

PersonTokensPropCommitsCommitProp
John Bonesio9680.67%133.33%
Rob Herring1714.29%133.33%
David Gibson65.04%133.33%
Total119100.00%3100.00%

ERROR(duplicate_label, check_duplicate_label_node, NULL);
static cell_t check_phandle_prop(struct check *c, struct dt_info *dti, struct node *node, const char *propname) { struct node *root = dti->dt; struct property *prop; struct marker *m; cell_t phandle; prop = get_property(node, propname); if (!prop) return 0; if (prop->val.len != sizeof(cell_t)) { FAIL(c, dti, "%s has bad length (%d) %s property", node->fullpath, prop->val.len, prop->name); return 0; } m = prop->val.markers; for_each_marker_of_type(m, REF_PHANDLE) { assert(m->offset == 0); if (node != get_node_by_ref(root, m->ref)) /* "Set this node's phandle equal to some * other node's phandle". That's nonsensical * by construction. */ { FAIL(c, dti, "%s in %s is a reference to another node", prop->name, node->fullpath); } /* But setting this node's phandle equal to its own * phandle is allowed - that means allocate a unique * phandle for this node, even if it's not otherwise * referenced. The value will be filled in later, so * we treat it as having no phandle data for now. */ return 0; } phandle = propval_cell(prop); if ((phandle == 0) || (phandle == -1)) { FAIL(c, dti, "%s has bad value (0x%x) in %s property", node->fullpath, phandle, prop->name); return 0; } return phandle; }

Contributors

PersonTokensPropCommitsCommitProp
John Bonesio9744.70%125.00%
David Gibson6931.80%125.00%
Rob Herring5123.50%250.00%
Total217100.00%4100.00%


static void check_explicit_phandles(struct check *c, struct dt_info *dti, struct node *node) { struct node *root = dti->dt; struct node *other; cell_t phandle, linux_phandle; /* Nothing should have assigned phandles yet */ assert(!node->phandle); phandle = check_phandle_prop(c, dti, node, "phandle"); linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle"); if (!phandle && !linux_phandle) /* No valid phandles; nothing further to check */ return; if (linux_phandle && phandle && (phandle != linux_phandle)) FAIL(c, dti, "%s has mismatching 'phandle' and 'linux,phandle'" " properties", node->fullpath); if (linux_phandle && !phandle) phandle = linux_phandle; other = get_node_by_phandle(root, phandle); if (other && (other != node)) { FAIL(c, dti, "%s has duplicated phandle 0x%x (seen before at %s)", node->fullpath, phandle, other->fullpath); return; } node->phandle = phandle; }

Contributors

PersonTokensPropCommitsCommitProp
Rob Herring10461.54%250.00%
David Gibson4023.67%125.00%
John Bonesio2514.79%125.00%
Total169100.00%4100.00%

ERROR(explicit_phandles, check_explicit_phandles, NULL);
static void check_name_properties(struct check *c, struct dt_info *dti, struct node *node) { struct property **pp, *prop = NULL; for (pp = &node->proplist; *pp; pp = &((*pp)->next)) if (streq((*pp)->name, "name")) { prop = *pp; break; } if (!prop) return; /* No name property, that's fine */ if ((prop->val.len != node->basenamelen+1) || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) { FAIL(c, dti, "\"name\" property in %s is incorrect (\"%s\" instead" " of base node name)", node->fullpath, prop->val.val); } else { /* The name property is correct, and therefore redundant. * Delete it */ *pp = prop->next; free(prop->name); data_free(prop->val); free(prop); } }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson17097.70%250.00%
Rob Herring42.30%250.00%
Total174100.00%4100.00%

ERROR_IF_NOT_STRING(name_is_string, "name"); ERROR(name_properties, check_name_properties, NULL, &name_is_string); /* * Reference fixup functions */
static void fixup_phandle_references(struct check *c, struct dt_info *dti, struct node *node) { struct node *dt = dti->dt; struct property *prop; for_each_property(node, prop) { struct marker *m = prop->val.markers; struct node *refnode; cell_t phandle; for_each_marker_of_type(m, REF_PHANDLE) { assert(m->offset + sizeof(cell_t) <= prop->val.len); refnode = get_node_by_ref(dt, m->ref); if (! refnode) { if (!(dti->dtsflags & DTSF_PLUGIN)) FAIL(c, dti, "Reference to non-existent node or " "label \"%s\"\n", m->ref); else /* mark the entry as unresolved */ *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(0xffffffff); continue; } phandle = get_node_phandle(dt, refnode); *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle); } } }

Contributors

PersonTokensPropCommitsCommitProp
David Gibson12867.02%250.00%
Rob Herring6332.98%250.00%
Total191100.00%4100.00%

ERROR(phandle_references, fixup_phandle_references, NULL, &duplicate_node_names, &explicit_phandles);
static void fixup_path_references(struct check *c, struct dt_info *dti, struct node *node) { struct node *dt = dti->dt; struct property *prop; for_each_property(node, prop) { struct marker *m = prop->val.markers; struct node *refnode; char *path; for_each_marker_of_type(m, REF_PATH) { assert(m->offset <= prop->val.len); refnode = get_node_by_ref(dt, m->ref); if