checks.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2007.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. */
  20. #include "dtc.h"
  21. #ifdef TRACE_CHECKS
  22. #define TRACE(c, ...) \
  23. do { \
  24. fprintf(stderr, "=== %s: ", (c)->name); \
  25. fprintf(stderr, __VA_ARGS__); \
  26. fprintf(stderr, "\n"); \
  27. } while (0)
  28. #else
  29. #define TRACE(c, fmt, ...) do { } while (0)
  30. #endif
  31. enum checkstatus {
  32. UNCHECKED = 0,
  33. PREREQ,
  34. PASSED,
  35. FAILED,
  36. };
  37. struct check;
  38. typedef void (*check_fn)(struct check *c, struct dt_info *dti, struct node *node);
  39. struct check {
  40. const char *name;
  41. check_fn fn;
  42. void *data;
  43. bool warn, error;
  44. enum checkstatus status;
  45. bool inprogress;
  46. int num_prereqs;
  47. struct check **prereq;
  48. };
  49. #define CHECK_ENTRY(_nm, _fn, _d, _w, _e, ...) \
  50. static struct check *_nm##_prereqs[] = { __VA_ARGS__ }; \
  51. static struct check _nm = { \
  52. .name = #_nm, \
  53. .fn = (_fn), \
  54. .data = (_d), \
  55. .warn = (_w), \
  56. .error = (_e), \
  57. .status = UNCHECKED, \
  58. .num_prereqs = ARRAY_SIZE(_nm##_prereqs), \
  59. .prereq = _nm##_prereqs, \
  60. };
  61. #define WARNING(_nm, _fn, _d, ...) \
  62. CHECK_ENTRY(_nm, _fn, _d, true, false, __VA_ARGS__)
  63. #define ERROR(_nm, _fn, _d, ...) \
  64. CHECK_ENTRY(_nm, _fn, _d, false, true, __VA_ARGS__)
  65. #define CHECK(_nm, _fn, _d, ...) \
  66. CHECK_ENTRY(_nm, _fn, _d, false, false, __VA_ARGS__)
  67. static inline void PRINTF(3, 4) check_msg(struct check *c, struct dt_info *dti,
  68. const char *fmt, ...)
  69. {
  70. va_list ap;
  71. va_start(ap, fmt);
  72. if ((c->warn && (quiet < 1))
  73. || (c->error && (quiet < 2))) {
  74. fprintf(stderr, "%s: %s (%s): ",
  75. strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
  76. (c->error) ? "ERROR" : "Warning", c->name);
  77. vfprintf(stderr, fmt, ap);
  78. fprintf(stderr, "\n");
  79. }
  80. va_end(ap);
  81. }
  82. #define FAIL(c, dti, ...) \
  83. do { \
  84. TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
  85. (c)->status = FAILED; \
  86. check_msg((c), dti, __VA_ARGS__); \
  87. } while (0)
  88. static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
  89. {
  90. struct node *child;
  91. TRACE(c, "%s", node->fullpath);
  92. if (c->fn)
  93. c->fn(c, dti, node);
  94. for_each_child(node, child)
  95. check_nodes_props(c, dti, child);
  96. }
  97. static bool run_check(struct check *c, struct dt_info *dti)
  98. {
  99. struct node *dt = dti->dt;
  100. bool error = false;
  101. int i;
  102. assert(!c->inprogress);
  103. if (c->status != UNCHECKED)
  104. goto out;
  105. c->inprogress = true;
  106. for (i = 0; i < c->num_prereqs; i++) {
  107. struct check *prq = c->prereq[i];
  108. error = error || run_check(prq, dti);
  109. if (prq->status != PASSED) {
  110. c->status = PREREQ;
  111. check_msg(c, dti, "Failed prerequisite '%s'",
  112. c->prereq[i]->name);
  113. }
  114. }
  115. if (c->status != UNCHECKED)
  116. goto out;
  117. check_nodes_props(c, dti, dt);
  118. if (c->status == UNCHECKED)
  119. c->status = PASSED;
  120. TRACE(c, "\tCompleted, status %d", c->status);
  121. out:
  122. c->inprogress = false;
  123. if ((c->status != PASSED) && (c->error))
  124. error = true;
  125. return error;
  126. }
  127. /*
  128. * Utility check functions
  129. */
  130. /* A check which always fails, for testing purposes only */
  131. static inline void check_always_fail(struct check *c, struct dt_info *dti,
  132. struct node *node)
  133. {
  134. FAIL(c, dti, "always_fail check");
  135. }
  136. CHECK(always_fail, check_always_fail, NULL);
  137. static void check_is_string(struct check *c, struct dt_info *dti,
  138. struct node *node)
  139. {
  140. struct property *prop;
  141. char *propname = c->data;
  142. prop = get_property(node, propname);
  143. if (!prop)
  144. return; /* Not present, assumed ok */
  145. if (!data_is_one_string(prop->val))
  146. FAIL(c, dti, "\"%s\" property in %s is not a string",
  147. propname, node->fullpath);
  148. }
  149. #define WARNING_IF_NOT_STRING(nm, propname) \
  150. WARNING(nm, check_is_string, (propname))
  151. #define ERROR_IF_NOT_STRING(nm, propname) \
  152. ERROR(nm, check_is_string, (propname))
  153. static void check_is_cell(struct check *c, struct dt_info *dti,
  154. struct node *node)
  155. {
  156. struct property *prop;
  157. char *propname = c->data;
  158. prop = get_property(node, propname);
  159. if (!prop)
  160. return; /* Not present, assumed ok */
  161. if (prop->val.len != sizeof(cell_t))
  162. FAIL(c, dti, "\"%s\" property in %s is not a single cell",
  163. propname, node->fullpath);
  164. }
  165. #define WARNING_IF_NOT_CELL(nm, propname) \
  166. WARNING(nm, check_is_cell, (propname))
  167. #define ERROR_IF_NOT_CELL(nm, propname) \
  168. ERROR(nm, check_is_cell, (propname))
  169. /*
  170. * Structural check functions
  171. */
  172. static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
  173. struct node *node)
  174. {
  175. struct node *child, *child2;
  176. for_each_child(node, child)
  177. for (child2 = child->next_sibling;
  178. child2;
  179. child2 = child2->next_sibling)
  180. if (streq(child->name, child2->name))
  181. FAIL(c, dti, "Duplicate node name %s",
  182. child->fullpath);
  183. }
  184. ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
  185. static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
  186. struct node *node)
  187. {
  188. struct property *prop, *prop2;
  189. for_each_property(node, prop) {
  190. for (prop2 = prop->next; prop2; prop2 = prop2->next) {
  191. if (prop2->deleted)
  192. continue;
  193. if (streq(prop->name, prop2->name))
  194. FAIL(c, dti, "Duplicate property name %s in %s",
  195. prop->name, node->fullpath);
  196. }
  197. }
  198. }
  199. ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
  200. #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
  201. #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  202. #define DIGITS "0123456789"
  203. #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
  204. #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
  205. static void check_node_name_chars(struct check *c, struct dt_info *dti,
  206. struct node *node)
  207. {
  208. int n = strspn(node->name, c->data);
  209. if (n < strlen(node->name))
  210. FAIL(c, dti, "Bad character '%c' in node %s",
  211. node->name[n], node->fullpath);
  212. }
  213. ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
  214. static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
  215. struct node *node)
  216. {
  217. int n = strspn(node->name, c->data);
  218. if (n < node->basenamelen)
  219. FAIL(c, dti, "Character '%c' not recommended in node %s",
  220. node->name[n], node->fullpath);
  221. }
  222. CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
  223. static void check_node_name_format(struct check *c, struct dt_info *dti,
  224. struct node *node)
  225. {
  226. if (strchr(get_unitname(node), '@'))
  227. FAIL(c, dti, "Node %s has multiple '@' characters in name",
  228. node->fullpath);
  229. }
  230. ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
  231. static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
  232. struct node *node)
  233. {
  234. const char *unitname = get_unitname(node);
  235. struct property *prop = get_property(node, "reg");
  236. if (!prop) {
  237. prop = get_property(node, "ranges");
  238. if (prop && !prop->val.len)
  239. prop = NULL;
  240. }
  241. if (prop) {
  242. if (!unitname[0])
  243. FAIL(c, dti, "Node %s has a reg or ranges property, but no unit name",
  244. node->fullpath);
  245. } else {
  246. if (unitname[0])
  247. FAIL(c, dti, "Node %s has a unit name, but no reg property",
  248. node->fullpath);
  249. }
  250. }
  251. WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
  252. static void check_property_name_chars(struct check *c, struct dt_info *dti,
  253. struct node *node)
  254. {
  255. struct property *prop;
  256. for_each_property(node, prop) {
  257. int n = strspn(prop->name, c->data);
  258. if (n < strlen(prop->name))
  259. FAIL(c, dti, "Bad character '%c' in property name \"%s\", node %s",
  260. prop->name[n], prop->name, node->fullpath);
  261. }
  262. }
  263. ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
  264. static void check_property_name_chars_strict(struct check *c,
  265. struct dt_info *dti,
  266. struct node *node)
  267. {
  268. struct property *prop;
  269. for_each_property(node, prop) {
  270. const char *name = prop->name;
  271. int n = strspn(name, c->data);
  272. if (n == strlen(prop->name))
  273. continue;
  274. /* Certain names are whitelisted */
  275. if (streq(name, "device_type"))
  276. continue;
  277. /*
  278. * # is only allowed at the beginning of property names not counting
  279. * the vendor prefix.
  280. */
  281. if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
  282. name += n + 1;
  283. n = strspn(name, c->data);
  284. }
  285. if (n < strlen(name))
  286. FAIL(c, dti, "Character '%c' not recommended in property name \"%s\", node %s",
  287. name[n], prop->name, node->fullpath);
  288. }
  289. }
  290. CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
  291. #define DESCLABEL_FMT "%s%s%s%s%s"
  292. #define DESCLABEL_ARGS(node,prop,mark) \
  293. ((mark) ? "value of " : ""), \
  294. ((prop) ? "'" : ""), \
  295. ((prop) ? (prop)->name : ""), \
  296. ((prop) ? "' in " : ""), (node)->fullpath
  297. static void check_duplicate_label(struct check *c, struct dt_info *dti,
  298. const char *label, struct node *node,
  299. struct property *prop, struct marker *mark)
  300. {
  301. struct node *dt = dti->dt;
  302. struct node *othernode = NULL;
  303. struct property *otherprop = NULL;
  304. struct marker *othermark = NULL;
  305. othernode = get_node_by_label(dt, label);
  306. if (!othernode)
  307. otherprop = get_property_by_label(dt, label, &othernode);
  308. if (!othernode)
  309. othermark = get_marker_label(dt, label, &othernode,
  310. &otherprop);
  311. if (!othernode)
  312. return;
  313. if ((othernode != node) || (otherprop != prop) || (othermark != mark))
  314. FAIL(c, dti, "Duplicate label '%s' on " DESCLABEL_FMT
  315. " and " DESCLABEL_FMT,
  316. label, DESCLABEL_ARGS(node, prop, mark),
  317. DESCLABEL_ARGS(othernode, otherprop, othermark));
  318. }
  319. static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
  320. struct node *node)
  321. {
  322. struct label *l;
  323. struct property *prop;
  324. for_each_label(node->labels, l)
  325. check_duplicate_label(c, dti, l->label, node, NULL, NULL);
  326. for_each_property(node, prop) {
  327. struct marker *m = prop->val.markers;
  328. for_each_label(prop->labels, l)
  329. check_duplicate_label(c, dti, l->label, node, prop, NULL);
  330. for_each_marker_of_type(m, LABEL)
  331. check_duplicate_label(c, dti, m->ref, node, prop, m);
  332. }
  333. }
  334. ERROR(duplicate_label, check_duplicate_label_node, NULL);
  335. static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
  336. struct node *node, const char *propname)
  337. {
  338. struct node *root = dti->dt;
  339. struct property *prop;
  340. struct marker *m;
  341. cell_t phandle;
  342. prop = get_property(node, propname);
  343. if (!prop)
  344. return 0;
  345. if (prop->val.len != sizeof(cell_t)) {
  346. FAIL(c, dti, "%s has bad length (%d) %s property",
  347. node->fullpath, prop->val.len, prop->name);
  348. return 0;
  349. }
  350. m = prop->val.markers;
  351. for_each_marker_of_type(m, REF_PHANDLE) {
  352. assert(m->offset == 0);
  353. if (node != get_node_by_ref(root, m->ref))
  354. /* "Set this node's phandle equal to some
  355. * other node's phandle". That's nonsensical
  356. * by construction. */ {
  357. FAIL(c, dti, "%s in %s is a reference to another node",
  358. prop->name, node->fullpath);
  359. }
  360. /* But setting this node's phandle equal to its own
  361. * phandle is allowed - that means allocate a unique
  362. * phandle for this node, even if it's not otherwise
  363. * referenced. The value will be filled in later, so
  364. * we treat it as having no phandle data for now. */
  365. return 0;
  366. }
  367. phandle = propval_cell(prop);
  368. if ((phandle == 0) || (phandle == -1)) {
  369. FAIL(c, dti, "%s has bad value (0x%x) in %s property",
  370. node->fullpath, phandle, prop->name);
  371. return 0;
  372. }
  373. return phandle;
  374. }
  375. static void check_explicit_phandles(struct check *c, struct dt_info *dti,
  376. struct node *node)
  377. {
  378. struct node *root = dti->dt;
  379. struct node *other;
  380. cell_t phandle, linux_phandle;
  381. /* Nothing should have assigned phandles yet */
  382. assert(!node->phandle);
  383. phandle = check_phandle_prop(c, dti, node, "phandle");
  384. linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
  385. if (!phandle && !linux_phandle)
  386. /* No valid phandles; nothing further to check */
  387. return;
  388. if (linux_phandle && phandle && (phandle != linux_phandle))
  389. FAIL(c, dti, "%s has mismatching 'phandle' and 'linux,phandle'"
  390. " properties", node->fullpath);
  391. if (linux_phandle && !phandle)
  392. phandle = linux_phandle;
  393. other = get_node_by_phandle(root, phandle);
  394. if (other && (other != node)) {
  395. FAIL(c, dti, "%s has duplicated phandle 0x%x (seen before at %s)",
  396. node->fullpath, phandle, other->fullpath);
  397. return;
  398. }
  399. node->phandle = phandle;
  400. }
  401. ERROR(explicit_phandles, check_explicit_phandles, NULL);
  402. static void check_name_properties(struct check *c, struct dt_info *dti,
  403. struct node *node)
  404. {
  405. struct property **pp, *prop = NULL;
  406. for (pp = &node->proplist; *pp; pp = &((*pp)->next))
  407. if (streq((*pp)->name, "name")) {
  408. prop = *pp;
  409. break;
  410. }
  411. if (!prop)
  412. return; /* No name property, that's fine */
  413. if ((prop->val.len != node->basenamelen+1)
  414. || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
  415. FAIL(c, dti, "\"name\" property in %s is incorrect (\"%s\" instead"
  416. " of base node name)", node->fullpath, prop->val.val);
  417. } else {
  418. /* The name property is correct, and therefore redundant.
  419. * Delete it */
  420. *pp = prop->next;
  421. free(prop->name);
  422. data_free(prop->val);
  423. free(prop);
  424. }
  425. }
  426. ERROR_IF_NOT_STRING(name_is_string, "name");
  427. ERROR(name_properties, check_name_properties, NULL, &name_is_string);
  428. /*
  429. * Reference fixup functions
  430. */
  431. static void fixup_phandle_references(struct check *c, struct dt_info *dti,
  432. struct node *node)
  433. {
  434. struct node *dt = dti->dt;
  435. struct property *prop;
  436. for_each_property(node, prop) {
  437. struct marker *m = prop->val.markers;
  438. struct node *refnode;
  439. cell_t phandle;
  440. for_each_marker_of_type(m, REF_PHANDLE) {
  441. assert(m->offset + sizeof(cell_t) <= prop->val.len);
  442. refnode = get_node_by_ref(dt, m->ref);
  443. if (! refnode) {
  444. if (!(dti->dtsflags & DTSF_PLUGIN))
  445. FAIL(c, dti, "Reference to non-existent node or "
  446. "label \"%s\"\n", m->ref);
  447. else /* mark the entry as unresolved */
  448. *((fdt32_t *)(prop->val.val + m->offset)) =
  449. cpu_to_fdt32(0xffffffff);
  450. continue;
  451. }
  452. phandle = get_node_phandle(dt, refnode);
  453. *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
  454. }
  455. }
  456. }
  457. ERROR(phandle_references, fixup_phandle_references, NULL,
  458. &duplicate_node_names, &explicit_phandles);
  459. static void fixup_path_references(struct check *c, struct dt_info *dti,
  460. struct node *node)
  461. {
  462. struct node *dt = dti->dt;
  463. struct property *prop;
  464. for_each_property(node, prop) {
  465. struct marker *m = prop->val.markers;
  466. struct node *refnode;
  467. char *path;
  468. for_each_marker_of_type(m, REF_PATH) {
  469. assert(m->offset <= prop->val.len);
  470. refnode = get_node_by_ref(dt, m->ref);
  471. if (!refnode) {
  472. FAIL(c, dti, "Reference to non-existent node or label \"%s\"\n",
  473. m->ref);
  474. continue;
  475. }
  476. path = refnode->fullpath;
  477. prop->val = data_insert_at_marker(prop->val, m, path,
  478. strlen(path) + 1);
  479. }
  480. }
  481. }
  482. ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
  483. /*
  484. * Semantic checks
  485. */
  486. WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
  487. WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
  488. WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
  489. WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
  490. WARNING_IF_NOT_STRING(model_is_string, "model");
  491. WARNING_IF_NOT_STRING(status_is_string, "status");
  492. static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
  493. struct node *node)
  494. {
  495. struct property *prop;
  496. node->addr_cells = -1;
  497. node->size_cells = -1;
  498. prop = get_property(node, "#address-cells");
  499. if (prop)
  500. node->addr_cells = propval_cell(prop);
  501. prop = get_property(node, "#size-cells");
  502. if (prop)
  503. node->size_cells = propval_cell(prop);
  504. }
  505. WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
  506. &address_cells_is_cell, &size_cells_is_cell);
  507. #define node_addr_cells(n) \
  508. (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
  509. #define node_size_cells(n) \
  510. (((n)->size_cells == -1) ? 1 : (n)->size_cells)
  511. static void check_reg_format(struct check *c, struct dt_info *dti,
  512. struct node *node)
  513. {
  514. struct property *prop;
  515. int addr_cells, size_cells, entrylen;
  516. prop = get_property(node, "reg");
  517. if (!prop)
  518. return; /* No "reg", that's fine */
  519. if (!node->parent) {
  520. FAIL(c, dti, "Root node has a \"reg\" property");
  521. return;
  522. }
  523. if (prop->val.len == 0)
  524. FAIL(c, dti, "\"reg\" property in %s is empty", node->fullpath);
  525. addr_cells = node_addr_cells(node->parent);
  526. size_cells = node_size_cells(node->parent);
  527. entrylen = (addr_cells + size_cells) * sizeof(cell_t);
  528. if (!entrylen || (prop->val.len % entrylen) != 0)
  529. FAIL(c, dti, "\"reg\" property in %s has invalid length (%d bytes) "
  530. "(#address-cells == %d, #size-cells == %d)",
  531. node->fullpath, prop->val.len, addr_cells, size_cells);
  532. }
  533. WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
  534. static void check_ranges_format(struct check *c, struct dt_info *dti,
  535. struct node *node)
  536. {
  537. struct property *prop;
  538. int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
  539. prop = get_property(node, "ranges");
  540. if (!prop)
  541. return;
  542. if (!node->parent) {
  543. FAIL(c, dti, "Root node has a \"ranges\" property");
  544. return;
  545. }
  546. p_addr_cells = node_addr_cells(node->parent);
  547. p_size_cells = node_size_cells(node->parent);
  548. c_addr_cells = node_addr_cells(node);
  549. c_size_cells = node_size_cells(node);
  550. entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
  551. if (prop->val.len == 0) {
  552. if (p_addr_cells != c_addr_cells)
  553. FAIL(c, dti, "%s has empty \"ranges\" property but its "
  554. "#address-cells (%d) differs from %s (%d)",
  555. node->fullpath, c_addr_cells, node->parent->fullpath,
  556. p_addr_cells);
  557. if (p_size_cells != c_size_cells)
  558. FAIL(c, dti, "%s has empty \"ranges\" property but its "
  559. "#size-cells (%d) differs from %s (%d)",
  560. node->fullpath, c_size_cells, node->parent->fullpath,
  561. p_size_cells);
  562. } else if ((prop->val.len % entrylen) != 0) {
  563. FAIL(c, dti, "\"ranges\" property in %s has invalid length (%d bytes) "
  564. "(parent #address-cells == %d, child #address-cells == %d, "
  565. "#size-cells == %d)", node->fullpath, prop->val.len,
  566. p_addr_cells, c_addr_cells, c_size_cells);
  567. }
  568. }
  569. WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
  570. /*
  571. * Style checks
  572. */
  573. static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
  574. struct node *node)
  575. {
  576. struct property *reg, *ranges;
  577. if (!node->parent)
  578. return; /* Ignore root node */
  579. reg = get_property(node, "reg");
  580. ranges = get_property(node, "ranges");
  581. if (!reg && !ranges)
  582. return;
  583. if (node->parent->addr_cells == -1)
  584. FAIL(c, dti, "Relying on default #address-cells value for %s",
  585. node->fullpath);
  586. if (node->parent->size_cells == -1)
  587. FAIL(c, dti, "Relying on default #size-cells value for %s",
  588. node->fullpath);
  589. }
  590. WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
  591. &addr_size_cells);
  592. static void check_obsolete_chosen_interrupt_controller(struct check *c,
  593. struct dt_info *dti,
  594. struct node *node)
  595. {
  596. struct node *dt = dti->dt;
  597. struct node *chosen;
  598. struct property *prop;
  599. if (node != dt)
  600. return;
  601. chosen = get_node_by_path(dt, "/chosen");
  602. if (!chosen)
  603. return;
  604. prop = get_property(chosen, "interrupt-controller");
  605. if (prop)
  606. FAIL(c, dti, "/chosen has obsolete \"interrupt-controller\" "
  607. "property");
  608. }
  609. WARNING(obsolete_chosen_interrupt_controller,
  610. check_obsolete_chosen_interrupt_controller, NULL);
  611. static struct check *check_table[] = {
  612. &duplicate_node_names, &duplicate_property_names,
  613. &node_name_chars, &node_name_format, &property_name_chars,
  614. &name_is_string, &name_properties,
  615. &duplicate_label,
  616. &explicit_phandles,
  617. &phandle_references, &path_references,
  618. &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
  619. &device_type_is_string, &model_is_string, &status_is_string,
  620. &property_name_chars_strict,
  621. &node_name_chars_strict,
  622. &addr_size_cells, &reg_format, &ranges_format,
  623. &unit_address_vs_reg,
  624. &avoid_default_addr_size,
  625. &obsolete_chosen_interrupt_controller,
  626. &always_fail,
  627. };
  628. static void enable_warning_error(struct check *c, bool warn, bool error)
  629. {
  630. int i;
  631. /* Raising level, also raise it for prereqs */
  632. if ((warn && !c->warn) || (error && !c->error))
  633. for (i = 0; i < c->num_prereqs; i++)
  634. enable_warning_error(c->prereq[i], warn, error);
  635. c->warn = c->warn || warn;
  636. c->error = c->error || error;
  637. }
  638. static void disable_warning_error(struct check *c, bool warn, bool error)
  639. {
  640. int i;
  641. /* Lowering level, also lower it for things this is the prereq
  642. * for */
  643. if ((warn && c->warn) || (error && c->error)) {
  644. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  645. struct check *cc = check_table[i];
  646. int j;
  647. for (j = 0; j < cc->num_prereqs; j++)
  648. if (cc->prereq[j] == c)
  649. disable_warning_error(cc, warn, error);
  650. }
  651. }
  652. c->warn = c->warn && !warn;
  653. c->error = c->error && !error;
  654. }
  655. void parse_checks_option(bool warn, bool error, const char *arg)
  656. {
  657. int i;
  658. const char *name = arg;
  659. bool enable = true;
  660. if ((strncmp(arg, "no-", 3) == 0)
  661. || (strncmp(arg, "no_", 3) == 0)) {
  662. name = arg + 3;
  663. enable = false;
  664. }
  665. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  666. struct check *c = check_table[i];
  667. if (streq(c->name, name)) {
  668. if (enable)
  669. enable_warning_error(c, warn, error);
  670. else
  671. disable_warning_error(c, warn, error);
  672. return;
  673. }
  674. }
  675. die("Unrecognized check name \"%s\"\n", name);
  676. }
  677. void process_checks(bool force, struct dt_info *dti)
  678. {
  679. int i;
  680. int error = 0;
  681. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  682. struct check *c = check_table[i];
  683. if (c->warn || c->error)
  684. error = error || run_check(c, dti);
  685. }
  686. if (error) {
  687. if (!force) {
  688. fprintf(stderr, "ERROR: Input tree has errors, aborting "
  689. "(use -f to force output)\n");
  690. exit(2);
  691. } else if (quiet < 3) {
  692. fprintf(stderr, "Warning: Input tree has errors, "
  693. "output forced\n");
  694. }
  695. }
  696. }