checks.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 checklevel {
  32. IGNORE = 0,
  33. WARN = 1,
  34. ERROR = 2,
  35. };
  36. enum checkstatus {
  37. UNCHECKED = 0,
  38. PREREQ,
  39. PASSED,
  40. FAILED,
  41. };
  42. struct check;
  43. typedef void (*tree_check_fn)(struct check *c, struct node *dt);
  44. typedef void (*node_check_fn)(struct check *c, struct node *dt, struct node *node);
  45. typedef void (*prop_check_fn)(struct check *c, struct node *dt,
  46. struct node *node, struct property *prop);
  47. struct check {
  48. const char *name;
  49. tree_check_fn tree_fn;
  50. node_check_fn node_fn;
  51. prop_check_fn prop_fn;
  52. void *data;
  53. enum checklevel level;
  54. enum checkstatus status;
  55. int inprogress;
  56. int num_prereqs;
  57. struct check **prereq;
  58. };
  59. #define CHECK(nm, tfn, nfn, pfn, d, lvl, ...) \
  60. static struct check *nm##_prereqs[] = { __VA_ARGS__ }; \
  61. static struct check nm = { \
  62. .name = #nm, \
  63. .tree_fn = (tfn), \
  64. .node_fn = (nfn), \
  65. .prop_fn = (pfn), \
  66. .data = (d), \
  67. .level = (lvl), \
  68. .status = UNCHECKED, \
  69. .num_prereqs = ARRAY_SIZE(nm##_prereqs), \
  70. .prereq = nm##_prereqs, \
  71. };
  72. #define TREE_CHECK(nm, d, lvl, ...) \
  73. CHECK(nm, check_##nm, NULL, NULL, d, lvl, __VA_ARGS__)
  74. #define NODE_CHECK(nm, d, lvl, ...) \
  75. CHECK(nm, NULL, check_##nm, NULL, d, lvl, __VA_ARGS__)
  76. #define PROP_CHECK(nm, d, lvl, ...) \
  77. CHECK(nm, NULL, NULL, check_##nm, d, lvl, __VA_ARGS__)
  78. #define BATCH_CHECK(nm, lvl, ...) \
  79. CHECK(nm, NULL, NULL, NULL, NULL, lvl, __VA_ARGS__)
  80. #ifdef __GNUC__
  81. static inline void check_msg(struct check *c, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
  82. #endif
  83. static inline void check_msg(struct check *c, const char *fmt, ...)
  84. {
  85. va_list ap;
  86. va_start(ap, fmt);
  87. if ((c->level < WARN) || (c->level <= quiet))
  88. return; /* Suppress message */
  89. fprintf(stderr, "%s (%s): ",
  90. (c->level == ERROR) ? "ERROR" : "Warning", c->name);
  91. vfprintf(stderr, fmt, ap);
  92. fprintf(stderr, "\n");
  93. }
  94. #define FAIL(c, ...) \
  95. do { \
  96. TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
  97. (c)->status = FAILED; \
  98. check_msg((c), __VA_ARGS__); \
  99. } while (0)
  100. static void check_nodes_props(struct check *c, struct node *dt, struct node *node)
  101. {
  102. struct node *child;
  103. struct property *prop;
  104. TRACE(c, "%s", node->fullpath);
  105. if (c->node_fn)
  106. c->node_fn(c, dt, node);
  107. if (c->prop_fn)
  108. for_each_property(node, prop) {
  109. TRACE(c, "%s\t'%s'", node->fullpath, prop->name);
  110. c->prop_fn(c, dt, node, prop);
  111. }
  112. for_each_child(node, child)
  113. check_nodes_props(c, dt, child);
  114. }
  115. static int run_check(struct check *c, struct node *dt)
  116. {
  117. int error = 0;
  118. int i;
  119. assert(!c->inprogress);
  120. if (c->status != UNCHECKED)
  121. goto out;
  122. c->inprogress = 1;
  123. for (i = 0; i < c->num_prereqs; i++) {
  124. struct check *prq = c->prereq[i];
  125. error |= run_check(prq, dt);
  126. if (prq->status != PASSED) {
  127. c->status = PREREQ;
  128. check_msg(c, "Failed prerequisite '%s'",
  129. c->prereq[i]->name);
  130. }
  131. }
  132. if (c->status != UNCHECKED)
  133. goto out;
  134. if (c->node_fn || c->prop_fn)
  135. check_nodes_props(c, dt, dt);
  136. if (c->tree_fn)
  137. c->tree_fn(c, dt);
  138. if (c->status == UNCHECKED)
  139. c->status = PASSED;
  140. TRACE(c, "\tCompleted, status %d", c->status);
  141. out:
  142. c->inprogress = 0;
  143. if ((c->status != PASSED) && (c->level == ERROR))
  144. error = 1;
  145. return error;
  146. }
  147. /*
  148. * Utility check functions
  149. */
  150. static void check_is_string(struct check *c, struct node *root,
  151. struct node *node)
  152. {
  153. struct property *prop;
  154. char *propname = c->data;
  155. prop = get_property(node, propname);
  156. if (!prop)
  157. return; /* Not present, assumed ok */
  158. if (!data_is_one_string(prop->val))
  159. FAIL(c, "\"%s\" property in %s is not a string",
  160. propname, node->fullpath);
  161. }
  162. #define CHECK_IS_STRING(nm, propname, lvl) \
  163. CHECK(nm, NULL, check_is_string, NULL, (propname), (lvl))
  164. static void check_is_cell(struct check *c, struct node *root,
  165. struct node *node)
  166. {
  167. struct property *prop;
  168. char *propname = c->data;
  169. prop = get_property(node, propname);
  170. if (!prop)
  171. return; /* Not present, assumed ok */
  172. if (prop->val.len != sizeof(cell_t))
  173. FAIL(c, "\"%s\" property in %s is not a single cell",
  174. propname, node->fullpath);
  175. }
  176. #define CHECK_IS_CELL(nm, propname, lvl) \
  177. CHECK(nm, NULL, check_is_cell, NULL, (propname), (lvl))
  178. /*
  179. * Structural check functions
  180. */
  181. static void check_duplicate_node_names(struct check *c, struct node *dt,
  182. struct node *node)
  183. {
  184. struct node *child, *child2;
  185. for_each_child(node, child)
  186. for (child2 = child->next_sibling;
  187. child2;
  188. child2 = child2->next_sibling)
  189. if (streq(child->name, child2->name))
  190. FAIL(c, "Duplicate node name %s",
  191. child->fullpath);
  192. }
  193. NODE_CHECK(duplicate_node_names, NULL, ERROR);
  194. static void check_duplicate_property_names(struct check *c, struct node *dt,
  195. struct node *node)
  196. {
  197. struct property *prop, *prop2;
  198. for_each_property(node, prop)
  199. for (prop2 = prop->next; prop2; prop2 = prop2->next)
  200. if (streq(prop->name, prop2->name))
  201. FAIL(c, "Duplicate property name %s in %s",
  202. prop->name, node->fullpath);
  203. }
  204. NODE_CHECK(duplicate_property_names, NULL, ERROR);
  205. #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
  206. #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  207. #define DIGITS "0123456789"
  208. #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
  209. static void check_node_name_chars(struct check *c, struct node *dt,
  210. struct node *node)
  211. {
  212. int n = strspn(node->name, c->data);
  213. if (n < strlen(node->name))
  214. FAIL(c, "Bad character '%c' in node %s",
  215. node->name[n], node->fullpath);
  216. }
  217. NODE_CHECK(node_name_chars, PROPNODECHARS "@", ERROR);
  218. static void check_node_name_format(struct check *c, struct node *dt,
  219. struct node *node)
  220. {
  221. if (strchr(get_unitname(node), '@'))
  222. FAIL(c, "Node %s has multiple '@' characters in name",
  223. node->fullpath);
  224. }
  225. NODE_CHECK(node_name_format, NULL, ERROR, &node_name_chars);
  226. static void check_property_name_chars(struct check *c, struct node *dt,
  227. struct node *node, struct property *prop)
  228. {
  229. int n = strspn(prop->name, c->data);
  230. if (n < strlen(prop->name))
  231. FAIL(c, "Bad character '%c' in property name \"%s\", node %s",
  232. prop->name[n], prop->name, node->fullpath);
  233. }
  234. PROP_CHECK(property_name_chars, PROPNODECHARS, ERROR);
  235. static void check_explicit_phandles(struct check *c, struct node *root,
  236. struct node *node)
  237. {
  238. struct property *prop;
  239. struct node *other;
  240. cell_t phandle;
  241. prop = get_property(node, "linux,phandle");
  242. if (! prop)
  243. return; /* No phandle, that's fine */
  244. if (prop->val.len != sizeof(cell_t)) {
  245. FAIL(c, "%s has bad length (%d) linux,phandle property",
  246. node->fullpath, prop->val.len);
  247. return;
  248. }
  249. phandle = propval_cell(prop);
  250. if ((phandle == 0) || (phandle == -1)) {
  251. FAIL(c, "%s has invalid linux,phandle value 0x%x",
  252. node->fullpath, phandle);
  253. return;
  254. }
  255. other = get_node_by_phandle(root, phandle);
  256. if (other) {
  257. FAIL(c, "%s has duplicated phandle 0x%x (seen before at %s)",
  258. node->fullpath, phandle, other->fullpath);
  259. return;
  260. }
  261. node->phandle = phandle;
  262. }
  263. NODE_CHECK(explicit_phandles, NULL, ERROR);
  264. static void check_name_properties(struct check *c, struct node *root,
  265. struct node *node)
  266. {
  267. struct property **pp, *prop = NULL;
  268. for (pp = &node->proplist; *pp; pp = &((*pp)->next))
  269. if (streq((*pp)->name, "name")) {
  270. prop = *pp;
  271. break;
  272. }
  273. if (!prop)
  274. return; /* No name property, that's fine */
  275. if ((prop->val.len != node->basenamelen+1)
  276. || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
  277. FAIL(c, "\"name\" property in %s is incorrect (\"%s\" instead"
  278. " of base node name)", node->fullpath, prop->val.val);
  279. } else {
  280. /* The name property is correct, and therefore redundant.
  281. * Delete it */
  282. *pp = prop->next;
  283. free(prop->name);
  284. data_free(prop->val);
  285. free(prop);
  286. }
  287. }
  288. CHECK_IS_STRING(name_is_string, "name", ERROR);
  289. NODE_CHECK(name_properties, NULL, ERROR, &name_is_string);
  290. /*
  291. * Reference fixup functions
  292. */
  293. static void fixup_phandle_references(struct check *c, struct node *dt,
  294. struct node *node, struct property *prop)
  295. {
  296. struct marker *m = prop->val.markers;
  297. struct node *refnode;
  298. cell_t phandle;
  299. for_each_marker_of_type(m, REF_PHANDLE) {
  300. assert(m->offset + sizeof(cell_t) <= prop->val.len);
  301. refnode = get_node_by_ref(dt, m->ref);
  302. if (! refnode) {
  303. FAIL(c, "Reference to non-existent node or label \"%s\"\n",
  304. m->ref);
  305. continue;
  306. }
  307. phandle = get_node_phandle(dt, refnode);
  308. *((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
  309. }
  310. }
  311. CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,
  312. &duplicate_node_names, &explicit_phandles);
  313. static void fixup_path_references(struct check *c, struct node *dt,
  314. struct node *node, struct property *prop)
  315. {
  316. struct marker *m = prop->val.markers;
  317. struct node *refnode;
  318. char *path;
  319. for_each_marker_of_type(m, REF_PATH) {
  320. assert(m->offset <= prop->val.len);
  321. refnode = get_node_by_ref(dt, m->ref);
  322. if (!refnode) {
  323. FAIL(c, "Reference to non-existent node or label \"%s\"\n",
  324. m->ref);
  325. continue;
  326. }
  327. path = refnode->fullpath;
  328. prop->val = data_insert_at_marker(prop->val, m, path,
  329. strlen(path) + 1);
  330. }
  331. }
  332. CHECK(path_references, NULL, NULL, fixup_path_references, NULL, ERROR,
  333. &duplicate_node_names);
  334. /*
  335. * Semantic checks
  336. */
  337. CHECK_IS_CELL(address_cells_is_cell, "#address-cells", WARN);
  338. CHECK_IS_CELL(size_cells_is_cell, "#size-cells", WARN);
  339. CHECK_IS_CELL(interrupt_cells_is_cell, "#interrupt-cells", WARN);
  340. CHECK_IS_STRING(device_type_is_string, "device_type", WARN);
  341. CHECK_IS_STRING(model_is_string, "model", WARN);
  342. CHECK_IS_STRING(status_is_string, "status", WARN);
  343. static void fixup_addr_size_cells(struct check *c, struct node *dt,
  344. struct node *node)
  345. {
  346. struct property *prop;
  347. node->addr_cells = -1;
  348. node->size_cells = -1;
  349. prop = get_property(node, "#address-cells");
  350. if (prop)
  351. node->addr_cells = propval_cell(prop);
  352. prop = get_property(node, "#size-cells");
  353. if (prop)
  354. node->size_cells = propval_cell(prop);
  355. }
  356. CHECK(addr_size_cells, NULL, fixup_addr_size_cells, NULL, NULL, WARN,
  357. &address_cells_is_cell, &size_cells_is_cell);
  358. #define node_addr_cells(n) \
  359. (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
  360. #define node_size_cells(n) \
  361. (((n)->size_cells == -1) ? 1 : (n)->size_cells)
  362. static void check_reg_format(struct check *c, struct node *dt,
  363. struct node *node)
  364. {
  365. struct property *prop;
  366. int addr_cells, size_cells, entrylen;
  367. prop = get_property(node, "reg");
  368. if (!prop)
  369. return; /* No "reg", that's fine */
  370. if (!node->parent) {
  371. FAIL(c, "Root node has a \"reg\" property");
  372. return;
  373. }
  374. if (prop->val.len == 0)
  375. FAIL(c, "\"reg\" property in %s is empty", node->fullpath);
  376. addr_cells = node_addr_cells(node->parent);
  377. size_cells = node_size_cells(node->parent);
  378. entrylen = (addr_cells + size_cells) * sizeof(cell_t);
  379. if ((prop->val.len % entrylen) != 0)
  380. FAIL(c, "\"reg\" property in %s has invalid length (%d bytes) "
  381. "(#address-cells == %d, #size-cells == %d)",
  382. node->fullpath, prop->val.len, addr_cells, size_cells);
  383. }
  384. NODE_CHECK(reg_format, NULL, WARN, &addr_size_cells);
  385. static void check_ranges_format(struct check *c, struct node *dt,
  386. struct node *node)
  387. {
  388. struct property *prop;
  389. int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
  390. prop = get_property(node, "ranges");
  391. if (!prop)
  392. return;
  393. if (!node->parent) {
  394. FAIL(c, "Root node has a \"ranges\" property");
  395. return;
  396. }
  397. p_addr_cells = node_addr_cells(node->parent);
  398. p_size_cells = node_size_cells(node->parent);
  399. c_addr_cells = node_addr_cells(node);
  400. c_size_cells = node_size_cells(node);
  401. entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
  402. if (prop->val.len == 0) {
  403. if (p_addr_cells != c_addr_cells)
  404. FAIL(c, "%s has empty \"ranges\" property but its "
  405. "#address-cells (%d) differs from %s (%d)",
  406. node->fullpath, c_addr_cells, node->parent->fullpath,
  407. p_addr_cells);
  408. if (p_size_cells != c_size_cells)
  409. FAIL(c, "%s has empty \"ranges\" property but its "
  410. "#size-cells (%d) differs from %s (%d)",
  411. node->fullpath, c_size_cells, node->parent->fullpath,
  412. p_size_cells);
  413. } else if ((prop->val.len % entrylen) != 0) {
  414. FAIL(c, "\"ranges\" property in %s has invalid length (%d bytes) "
  415. "(parent #address-cells == %d, child #address-cells == %d, "
  416. "#size-cells == %d)", node->fullpath, prop->val.len,
  417. p_addr_cells, c_addr_cells, c_size_cells);
  418. }
  419. }
  420. NODE_CHECK(ranges_format, NULL, WARN, &addr_size_cells);
  421. /*
  422. * Style checks
  423. */
  424. static void check_avoid_default_addr_size(struct check *c, struct node *dt,
  425. struct node *node)
  426. {
  427. struct property *reg, *ranges;
  428. if (!node->parent)
  429. return; /* Ignore root node */
  430. reg = get_property(node, "reg");
  431. ranges = get_property(node, "ranges");
  432. if (!reg && !ranges)
  433. return;
  434. if ((node->parent->addr_cells == -1))
  435. FAIL(c, "Relying on default #address-cells value for %s",
  436. node->fullpath);
  437. if ((node->parent->size_cells == -1))
  438. FAIL(c, "Relying on default #size-cells value for %s",
  439. node->fullpath);
  440. }
  441. NODE_CHECK(avoid_default_addr_size, NULL, WARN, &addr_size_cells);
  442. static void check_obsolete_chosen_interrupt_controller(struct check *c,
  443. struct node *dt)
  444. {
  445. struct node *chosen;
  446. struct property *prop;
  447. chosen = get_node_by_path(dt, "/chosen");
  448. if (!chosen)
  449. return;
  450. prop = get_property(chosen, "interrupt-controller");
  451. if (prop)
  452. FAIL(c, "/chosen has obsolete \"interrupt-controller\" "
  453. "property");
  454. }
  455. TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN);
  456. static struct check *check_table[] = {
  457. &duplicate_node_names, &duplicate_property_names,
  458. &node_name_chars, &node_name_format, &property_name_chars,
  459. &name_is_string, &name_properties,
  460. &explicit_phandles,
  461. &phandle_references, &path_references,
  462. &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
  463. &device_type_is_string, &model_is_string, &status_is_string,
  464. &addr_size_cells, &reg_format, &ranges_format,
  465. &avoid_default_addr_size,
  466. &obsolete_chosen_interrupt_controller,
  467. };
  468. void process_checks(int force, struct boot_info *bi)
  469. {
  470. struct node *dt = bi->dt;
  471. int i;
  472. int error = 0;
  473. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  474. struct check *c = check_table[i];
  475. if (c->level != IGNORE)
  476. error = error || run_check(c, dt);
  477. }
  478. if (error) {
  479. if (!force) {
  480. fprintf(stderr, "ERROR: Input tree has errors, aborting "
  481. "(use -f to force output)\n");
  482. exit(2);
  483. } else if (quiet < 3) {
  484. fprintf(stderr, "Warning: Input tree has errors, "
  485. "output forced\n");
  486. }
  487. }
  488. }