checks.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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(5, 6) check_msg(struct check *c, struct dt_info *dti,
  68. struct node *node,
  69. struct property *prop,
  70. const char *fmt, ...)
  71. {
  72. va_list ap;
  73. va_start(ap, fmt);
  74. if ((c->warn && (quiet < 1))
  75. || (c->error && (quiet < 2))) {
  76. fprintf(stderr, "%s: %s (%s): ",
  77. strcmp(dti->outname, "-") ? dti->outname : "<stdout>",
  78. (c->error) ? "ERROR" : "Warning", c->name);
  79. if (node) {
  80. fprintf(stderr, "%s", node->fullpath);
  81. if (prop)
  82. fprintf(stderr, ":%s", prop->name);
  83. fputs(": ", stderr);
  84. }
  85. vfprintf(stderr, fmt, ap);
  86. fprintf(stderr, "\n");
  87. }
  88. va_end(ap);
  89. }
  90. #define FAIL(c, dti, node, ...) \
  91. do { \
  92. TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
  93. (c)->status = FAILED; \
  94. check_msg((c), dti, node, NULL, __VA_ARGS__); \
  95. } while (0)
  96. #define FAIL_PROP(c, dti, node, prop, ...) \
  97. do { \
  98. TRACE((c), "\t\tFAILED at %s:%d", __FILE__, __LINE__); \
  99. (c)->status = FAILED; \
  100. check_msg((c), dti, node, prop, __VA_ARGS__); \
  101. } while (0)
  102. static void check_nodes_props(struct check *c, struct dt_info *dti, struct node *node)
  103. {
  104. struct node *child;
  105. TRACE(c, "%s", node->fullpath);
  106. if (c->fn)
  107. c->fn(c, dti, node);
  108. for_each_child(node, child)
  109. check_nodes_props(c, dti, child);
  110. }
  111. static bool run_check(struct check *c, struct dt_info *dti)
  112. {
  113. struct node *dt = dti->dt;
  114. bool error = false;
  115. int i;
  116. assert(!c->inprogress);
  117. if (c->status != UNCHECKED)
  118. goto out;
  119. c->inprogress = true;
  120. for (i = 0; i < c->num_prereqs; i++) {
  121. struct check *prq = c->prereq[i];
  122. error = error || run_check(prq, dti);
  123. if (prq->status != PASSED) {
  124. c->status = PREREQ;
  125. check_msg(c, dti, NULL, NULL, "Failed prerequisite '%s'",
  126. c->prereq[i]->name);
  127. }
  128. }
  129. if (c->status != UNCHECKED)
  130. goto out;
  131. check_nodes_props(c, dti, dt);
  132. if (c->status == UNCHECKED)
  133. c->status = PASSED;
  134. TRACE(c, "\tCompleted, status %d", c->status);
  135. out:
  136. c->inprogress = false;
  137. if ((c->status != PASSED) && (c->error))
  138. error = true;
  139. return error;
  140. }
  141. /*
  142. * Utility check functions
  143. */
  144. /* A check which always fails, for testing purposes only */
  145. static inline void check_always_fail(struct check *c, struct dt_info *dti,
  146. struct node *node)
  147. {
  148. FAIL(c, dti, node, "always_fail check");
  149. }
  150. CHECK(always_fail, check_always_fail, NULL);
  151. static void check_is_string(struct check *c, struct dt_info *dti,
  152. struct node *node)
  153. {
  154. struct property *prop;
  155. char *propname = c->data;
  156. prop = get_property(node, propname);
  157. if (!prop)
  158. return; /* Not present, assumed ok */
  159. if (!data_is_one_string(prop->val))
  160. FAIL_PROP(c, dti, node, prop, "property is not a string");
  161. }
  162. #define WARNING_IF_NOT_STRING(nm, propname) \
  163. WARNING(nm, check_is_string, (propname))
  164. #define ERROR_IF_NOT_STRING(nm, propname) \
  165. ERROR(nm, check_is_string, (propname))
  166. static void check_is_string_list(struct check *c, struct dt_info *dti,
  167. struct node *node)
  168. {
  169. int rem, l;
  170. struct property *prop;
  171. char *propname = c->data;
  172. char *str;
  173. prop = get_property(node, propname);
  174. if (!prop)
  175. return; /* Not present, assumed ok */
  176. str = prop->val.val;
  177. rem = prop->val.len;
  178. while (rem > 0) {
  179. l = strnlen(str, rem);
  180. if (l == rem) {
  181. FAIL_PROP(c, dti, node, prop, "property is not a string list");
  182. break;
  183. }
  184. rem -= l + 1;
  185. str += l + 1;
  186. }
  187. }
  188. #define WARNING_IF_NOT_STRING_LIST(nm, propname) \
  189. WARNING(nm, check_is_string_list, (propname))
  190. #define ERROR_IF_NOT_STRING_LIST(nm, propname) \
  191. ERROR(nm, check_is_string_list, (propname))
  192. static void check_is_cell(struct check *c, struct dt_info *dti,
  193. struct node *node)
  194. {
  195. struct property *prop;
  196. char *propname = c->data;
  197. prop = get_property(node, propname);
  198. if (!prop)
  199. return; /* Not present, assumed ok */
  200. if (prop->val.len != sizeof(cell_t))
  201. FAIL_PROP(c, dti, node, prop, "property is not a single cell");
  202. }
  203. #define WARNING_IF_NOT_CELL(nm, propname) \
  204. WARNING(nm, check_is_cell, (propname))
  205. #define ERROR_IF_NOT_CELL(nm, propname) \
  206. ERROR(nm, check_is_cell, (propname))
  207. /*
  208. * Structural check functions
  209. */
  210. static void check_duplicate_node_names(struct check *c, struct dt_info *dti,
  211. struct node *node)
  212. {
  213. struct node *child, *child2;
  214. for_each_child(node, child)
  215. for (child2 = child->next_sibling;
  216. child2;
  217. child2 = child2->next_sibling)
  218. if (streq(child->name, child2->name))
  219. FAIL(c, dti, node, "Duplicate node name");
  220. }
  221. ERROR(duplicate_node_names, check_duplicate_node_names, NULL);
  222. static void check_duplicate_property_names(struct check *c, struct dt_info *dti,
  223. struct node *node)
  224. {
  225. struct property *prop, *prop2;
  226. for_each_property(node, prop) {
  227. for (prop2 = prop->next; prop2; prop2 = prop2->next) {
  228. if (prop2->deleted)
  229. continue;
  230. if (streq(prop->name, prop2->name))
  231. FAIL_PROP(c, dti, node, prop, "Duplicate property name");
  232. }
  233. }
  234. }
  235. ERROR(duplicate_property_names, check_duplicate_property_names, NULL);
  236. #define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
  237. #define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  238. #define DIGITS "0123456789"
  239. #define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"
  240. #define PROPNODECHARSSTRICT LOWERCASE UPPERCASE DIGITS ",-"
  241. static void check_node_name_chars(struct check *c, struct dt_info *dti,
  242. struct node *node)
  243. {
  244. int n = strspn(node->name, c->data);
  245. if (n < strlen(node->name))
  246. FAIL(c, dti, node, "Bad character '%c' in node name",
  247. node->name[n]);
  248. }
  249. ERROR(node_name_chars, check_node_name_chars, PROPNODECHARS "@");
  250. static void check_node_name_chars_strict(struct check *c, struct dt_info *dti,
  251. struct node *node)
  252. {
  253. int n = strspn(node->name, c->data);
  254. if (n < node->basenamelen)
  255. FAIL(c, dti, node, "Character '%c' not recommended in node name",
  256. node->name[n]);
  257. }
  258. CHECK(node_name_chars_strict, check_node_name_chars_strict, PROPNODECHARSSTRICT);
  259. static void check_node_name_format(struct check *c, struct dt_info *dti,
  260. struct node *node)
  261. {
  262. if (strchr(get_unitname(node), '@'))
  263. FAIL(c, dti, node, "multiple '@' characters in node name");
  264. }
  265. ERROR(node_name_format, check_node_name_format, NULL, &node_name_chars);
  266. static void check_unit_address_vs_reg(struct check *c, struct dt_info *dti,
  267. struct node *node)
  268. {
  269. const char *unitname = get_unitname(node);
  270. struct property *prop = get_property(node, "reg");
  271. if (!prop) {
  272. prop = get_property(node, "ranges");
  273. if (prop && !prop->val.len)
  274. prop = NULL;
  275. }
  276. if (prop) {
  277. if (!unitname[0])
  278. FAIL(c, dti, node, "node has a reg or ranges property, but no unit name");
  279. } else {
  280. if (unitname[0])
  281. FAIL(c, dti, node, "node has a unit name, but no reg property");
  282. }
  283. }
  284. WARNING(unit_address_vs_reg, check_unit_address_vs_reg, NULL);
  285. static void check_property_name_chars(struct check *c, struct dt_info *dti,
  286. struct node *node)
  287. {
  288. struct property *prop;
  289. for_each_property(node, prop) {
  290. int n = strspn(prop->name, c->data);
  291. if (n < strlen(prop->name))
  292. FAIL_PROP(c, dti, node, prop, "Bad character '%c' in property name",
  293. prop->name[n]);
  294. }
  295. }
  296. ERROR(property_name_chars, check_property_name_chars, PROPNODECHARS);
  297. static void check_property_name_chars_strict(struct check *c,
  298. struct dt_info *dti,
  299. struct node *node)
  300. {
  301. struct property *prop;
  302. for_each_property(node, prop) {
  303. const char *name = prop->name;
  304. int n = strspn(name, c->data);
  305. if (n == strlen(prop->name))
  306. continue;
  307. /* Certain names are whitelisted */
  308. if (streq(name, "device_type"))
  309. continue;
  310. /*
  311. * # is only allowed at the beginning of property names not counting
  312. * the vendor prefix.
  313. */
  314. if (name[n] == '#' && ((n == 0) || (name[n-1] == ','))) {
  315. name += n + 1;
  316. n = strspn(name, c->data);
  317. }
  318. if (n < strlen(name))
  319. FAIL_PROP(c, dti, node, prop, "Character '%c' not recommended in property name",
  320. name[n]);
  321. }
  322. }
  323. CHECK(property_name_chars_strict, check_property_name_chars_strict, PROPNODECHARSSTRICT);
  324. #define DESCLABEL_FMT "%s%s%s%s%s"
  325. #define DESCLABEL_ARGS(node,prop,mark) \
  326. ((mark) ? "value of " : ""), \
  327. ((prop) ? "'" : ""), \
  328. ((prop) ? (prop)->name : ""), \
  329. ((prop) ? "' in " : ""), (node)->fullpath
  330. static void check_duplicate_label(struct check *c, struct dt_info *dti,
  331. const char *label, struct node *node,
  332. struct property *prop, struct marker *mark)
  333. {
  334. struct node *dt = dti->dt;
  335. struct node *othernode = NULL;
  336. struct property *otherprop = NULL;
  337. struct marker *othermark = NULL;
  338. othernode = get_node_by_label(dt, label);
  339. if (!othernode)
  340. otherprop = get_property_by_label(dt, label, &othernode);
  341. if (!othernode)
  342. othermark = get_marker_label(dt, label, &othernode,
  343. &otherprop);
  344. if (!othernode)
  345. return;
  346. if ((othernode != node) || (otherprop != prop) || (othermark != mark))
  347. FAIL(c, dti, node, "Duplicate label '%s' on " DESCLABEL_FMT
  348. " and " DESCLABEL_FMT,
  349. label, DESCLABEL_ARGS(node, prop, mark),
  350. DESCLABEL_ARGS(othernode, otherprop, othermark));
  351. }
  352. static void check_duplicate_label_node(struct check *c, struct dt_info *dti,
  353. struct node *node)
  354. {
  355. struct label *l;
  356. struct property *prop;
  357. for_each_label(node->labels, l)
  358. check_duplicate_label(c, dti, l->label, node, NULL, NULL);
  359. for_each_property(node, prop) {
  360. struct marker *m = prop->val.markers;
  361. for_each_label(prop->labels, l)
  362. check_duplicate_label(c, dti, l->label, node, prop, NULL);
  363. for_each_marker_of_type(m, LABEL)
  364. check_duplicate_label(c, dti, m->ref, node, prop, m);
  365. }
  366. }
  367. ERROR(duplicate_label, check_duplicate_label_node, NULL);
  368. static cell_t check_phandle_prop(struct check *c, struct dt_info *dti,
  369. struct node *node, const char *propname)
  370. {
  371. struct node *root = dti->dt;
  372. struct property *prop;
  373. struct marker *m;
  374. cell_t phandle;
  375. prop = get_property(node, propname);
  376. if (!prop)
  377. return 0;
  378. if (prop->val.len != sizeof(cell_t)) {
  379. FAIL_PROP(c, dti, node, prop, "bad length (%d) %s property",
  380. prop->val.len, prop->name);
  381. return 0;
  382. }
  383. m = prop->val.markers;
  384. for_each_marker_of_type(m, REF_PHANDLE) {
  385. assert(m->offset == 0);
  386. if (node != get_node_by_ref(root, m->ref))
  387. /* "Set this node's phandle equal to some
  388. * other node's phandle". That's nonsensical
  389. * by construction. */ {
  390. FAIL(c, dti, node, "%s is a reference to another node",
  391. prop->name);
  392. }
  393. /* But setting this node's phandle equal to its own
  394. * phandle is allowed - that means allocate a unique
  395. * phandle for this node, even if it's not otherwise
  396. * referenced. The value will be filled in later, so
  397. * we treat it as having no phandle data for now. */
  398. return 0;
  399. }
  400. phandle = propval_cell(prop);
  401. if ((phandle == 0) || (phandle == -1)) {
  402. FAIL_PROP(c, dti, node, prop, "bad value (0x%x) in %s property",
  403. phandle, prop->name);
  404. return 0;
  405. }
  406. return phandle;
  407. }
  408. static void check_explicit_phandles(struct check *c, struct dt_info *dti,
  409. struct node *node)
  410. {
  411. struct node *root = dti->dt;
  412. struct node *other;
  413. cell_t phandle, linux_phandle;
  414. /* Nothing should have assigned phandles yet */
  415. assert(!node->phandle);
  416. phandle = check_phandle_prop(c, dti, node, "phandle");
  417. linux_phandle = check_phandle_prop(c, dti, node, "linux,phandle");
  418. if (!phandle && !linux_phandle)
  419. /* No valid phandles; nothing further to check */
  420. return;
  421. if (linux_phandle && phandle && (phandle != linux_phandle))
  422. FAIL(c, dti, node, "mismatching 'phandle' and 'linux,phandle'"
  423. " properties");
  424. if (linux_phandle && !phandle)
  425. phandle = linux_phandle;
  426. other = get_node_by_phandle(root, phandle);
  427. if (other && (other != node)) {
  428. FAIL(c, dti, node, "duplicated phandle 0x%x (seen before at %s)",
  429. phandle, other->fullpath);
  430. return;
  431. }
  432. node->phandle = phandle;
  433. }
  434. ERROR(explicit_phandles, check_explicit_phandles, NULL);
  435. static void check_name_properties(struct check *c, struct dt_info *dti,
  436. struct node *node)
  437. {
  438. struct property **pp, *prop = NULL;
  439. for (pp = &node->proplist; *pp; pp = &((*pp)->next))
  440. if (streq((*pp)->name, "name")) {
  441. prop = *pp;
  442. break;
  443. }
  444. if (!prop)
  445. return; /* No name property, that's fine */
  446. if ((prop->val.len != node->basenamelen+1)
  447. || (memcmp(prop->val.val, node->name, node->basenamelen) != 0)) {
  448. FAIL(c, dti, node, "\"name\" property is incorrect (\"%s\" instead"
  449. " of base node name)", prop->val.val);
  450. } else {
  451. /* The name property is correct, and therefore redundant.
  452. * Delete it */
  453. *pp = prop->next;
  454. free(prop->name);
  455. data_free(prop->val);
  456. free(prop);
  457. }
  458. }
  459. ERROR_IF_NOT_STRING(name_is_string, "name");
  460. ERROR(name_properties, check_name_properties, NULL, &name_is_string);
  461. /*
  462. * Reference fixup functions
  463. */
  464. static void fixup_phandle_references(struct check *c, struct dt_info *dti,
  465. struct node *node)
  466. {
  467. struct node *dt = dti->dt;
  468. struct property *prop;
  469. for_each_property(node, prop) {
  470. struct marker *m = prop->val.markers;
  471. struct node *refnode;
  472. cell_t phandle;
  473. for_each_marker_of_type(m, REF_PHANDLE) {
  474. assert(m->offset + sizeof(cell_t) <= prop->val.len);
  475. refnode = get_node_by_ref(dt, m->ref);
  476. if (! refnode) {
  477. if (!(dti->dtsflags & DTSF_PLUGIN))
  478. FAIL(c, dti, node, "Reference to non-existent node or "
  479. "label \"%s\"\n", m->ref);
  480. else /* mark the entry as unresolved */
  481. *((fdt32_t *)(prop->val.val + m->offset)) =
  482. cpu_to_fdt32(0xffffffff);
  483. continue;
  484. }
  485. phandle = get_node_phandle(dt, refnode);
  486. *((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
  487. }
  488. }
  489. }
  490. ERROR(phandle_references, fixup_phandle_references, NULL,
  491. &duplicate_node_names, &explicit_phandles);
  492. static void fixup_path_references(struct check *c, struct dt_info *dti,
  493. struct node *node)
  494. {
  495. struct node *dt = dti->dt;
  496. struct property *prop;
  497. for_each_property(node, prop) {
  498. struct marker *m = prop->val.markers;
  499. struct node *refnode;
  500. char *path;
  501. for_each_marker_of_type(m, REF_PATH) {
  502. assert(m->offset <= prop->val.len);
  503. refnode = get_node_by_ref(dt, m->ref);
  504. if (!refnode) {
  505. FAIL(c, dti, node, "Reference to non-existent node or label \"%s\"\n",
  506. m->ref);
  507. continue;
  508. }
  509. path = refnode->fullpath;
  510. prop->val = data_insert_at_marker(prop->val, m, path,
  511. strlen(path) + 1);
  512. }
  513. }
  514. }
  515. ERROR(path_references, fixup_path_references, NULL, &duplicate_node_names);
  516. /*
  517. * Semantic checks
  518. */
  519. WARNING_IF_NOT_CELL(address_cells_is_cell, "#address-cells");
  520. WARNING_IF_NOT_CELL(size_cells_is_cell, "#size-cells");
  521. WARNING_IF_NOT_CELL(interrupt_cells_is_cell, "#interrupt-cells");
  522. WARNING_IF_NOT_STRING(device_type_is_string, "device_type");
  523. WARNING_IF_NOT_STRING(model_is_string, "model");
  524. WARNING_IF_NOT_STRING(status_is_string, "status");
  525. WARNING_IF_NOT_STRING(label_is_string, "label");
  526. WARNING_IF_NOT_STRING_LIST(compatible_is_string_list, "compatible");
  527. static void check_names_is_string_list(struct check *c, struct dt_info *dti,
  528. struct node *node)
  529. {
  530. struct property *prop;
  531. for_each_property(node, prop) {
  532. const char *s = strrchr(prop->name, '-');
  533. if (!s || !streq(s, "-names"))
  534. continue;
  535. c->data = prop->name;
  536. check_is_string_list(c, dti, node);
  537. }
  538. }
  539. WARNING(names_is_string_list, check_names_is_string_list, NULL);
  540. static void check_alias_paths(struct check *c, struct dt_info *dti,
  541. struct node *node)
  542. {
  543. struct property *prop;
  544. if (!streq(node->name, "aliases"))
  545. return;
  546. for_each_property(node, prop) {
  547. if (!prop->val.val || !get_node_by_path(dti->dt, prop->val.val)) {
  548. FAIL_PROP(c, dti, node, prop, "aliases property is not a valid node (%s)",
  549. prop->val.val);
  550. continue;
  551. }
  552. if (strspn(prop->name, LOWERCASE DIGITS "-") != strlen(prop->name))
  553. FAIL(c, dti, node, "aliases property name must include only lowercase and '-'");
  554. }
  555. }
  556. WARNING(alias_paths, check_alias_paths, NULL);
  557. static void fixup_addr_size_cells(struct check *c, struct dt_info *dti,
  558. struct node *node)
  559. {
  560. struct property *prop;
  561. node->addr_cells = -1;
  562. node->size_cells = -1;
  563. prop = get_property(node, "#address-cells");
  564. if (prop)
  565. node->addr_cells = propval_cell(prop);
  566. prop = get_property(node, "#size-cells");
  567. if (prop)
  568. node->size_cells = propval_cell(prop);
  569. }
  570. WARNING(addr_size_cells, fixup_addr_size_cells, NULL,
  571. &address_cells_is_cell, &size_cells_is_cell);
  572. #define node_addr_cells(n) \
  573. (((n)->addr_cells == -1) ? 2 : (n)->addr_cells)
  574. #define node_size_cells(n) \
  575. (((n)->size_cells == -1) ? 1 : (n)->size_cells)
  576. static void check_reg_format(struct check *c, struct dt_info *dti,
  577. struct node *node)
  578. {
  579. struct property *prop;
  580. int addr_cells, size_cells, entrylen;
  581. prop = get_property(node, "reg");
  582. if (!prop)
  583. return; /* No "reg", that's fine */
  584. if (!node->parent) {
  585. FAIL(c, dti, node, "Root node has a \"reg\" property");
  586. return;
  587. }
  588. if (prop->val.len == 0)
  589. FAIL_PROP(c, dti, node, prop, "property is empty");
  590. addr_cells = node_addr_cells(node->parent);
  591. size_cells = node_size_cells(node->parent);
  592. entrylen = (addr_cells + size_cells) * sizeof(cell_t);
  593. if (!entrylen || (prop->val.len % entrylen) != 0)
  594. FAIL_PROP(c, dti, node, prop, "property has invalid length (%d bytes) "
  595. "(#address-cells == %d, #size-cells == %d)",
  596. prop->val.len, addr_cells, size_cells);
  597. }
  598. WARNING(reg_format, check_reg_format, NULL, &addr_size_cells);
  599. static void check_ranges_format(struct check *c, struct dt_info *dti,
  600. struct node *node)
  601. {
  602. struct property *prop;
  603. int c_addr_cells, p_addr_cells, c_size_cells, p_size_cells, entrylen;
  604. prop = get_property(node, "ranges");
  605. if (!prop)
  606. return;
  607. if (!node->parent) {
  608. FAIL_PROP(c, dti, node, prop, "Root node has a \"ranges\" property");
  609. return;
  610. }
  611. p_addr_cells = node_addr_cells(node->parent);
  612. p_size_cells = node_size_cells(node->parent);
  613. c_addr_cells = node_addr_cells(node);
  614. c_size_cells = node_size_cells(node);
  615. entrylen = (p_addr_cells + c_addr_cells + c_size_cells) * sizeof(cell_t);
  616. if (prop->val.len == 0) {
  617. if (p_addr_cells != c_addr_cells)
  618. FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
  619. "#address-cells (%d) differs from %s (%d)",
  620. c_addr_cells, node->parent->fullpath,
  621. p_addr_cells);
  622. if (p_size_cells != c_size_cells)
  623. FAIL_PROP(c, dti, node, prop, "empty \"ranges\" property but its "
  624. "#size-cells (%d) differs from %s (%d)",
  625. c_size_cells, node->parent->fullpath,
  626. p_size_cells);
  627. } else if ((prop->val.len % entrylen) != 0) {
  628. FAIL_PROP(c, dti, node, prop, "\"ranges\" property has invalid length (%d bytes) "
  629. "(parent #address-cells == %d, child #address-cells == %d, "
  630. "#size-cells == %d)", prop->val.len,
  631. p_addr_cells, c_addr_cells, c_size_cells);
  632. }
  633. }
  634. WARNING(ranges_format, check_ranges_format, NULL, &addr_size_cells);
  635. static const struct bus_type pci_bus = {
  636. .name = "PCI",
  637. };
  638. static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *node)
  639. {
  640. struct property *prop;
  641. cell_t *cells;
  642. prop = get_property(node, "device_type");
  643. if (!prop || !streq(prop->val.val, "pci"))
  644. return;
  645. node->bus = &pci_bus;
  646. if (!strprefixeq(node->name, node->basenamelen, "pci") &&
  647. !strprefixeq(node->name, node->basenamelen, "pcie"))
  648. FAIL(c, dti, node, "node name is not \"pci\" or \"pcie\"");
  649. prop = get_property(node, "ranges");
  650. if (!prop)
  651. FAIL(c, dti, node, "missing ranges for PCI bridge (or not a bridge)");
  652. if (node_addr_cells(node) != 3)
  653. FAIL(c, dti, node, "incorrect #address-cells for PCI bridge");
  654. if (node_size_cells(node) != 2)
  655. FAIL(c, dti, node, "incorrect #size-cells for PCI bridge");
  656. prop = get_property(node, "bus-range");
  657. if (!prop) {
  658. FAIL(c, dti, node, "missing bus-range for PCI bridge");
  659. return;
  660. }
  661. if (prop->val.len != (sizeof(cell_t) * 2)) {
  662. FAIL_PROP(c, dti, node, prop, "value must be 2 cells");
  663. return;
  664. }
  665. cells = (cell_t *)prop->val.val;
  666. if (fdt32_to_cpu(cells[0]) > fdt32_to_cpu(cells[1]))
  667. FAIL_PROP(c, dti, node, prop, "1st cell must be less than or equal to 2nd cell");
  668. if (fdt32_to_cpu(cells[1]) > 0xff)
  669. FAIL_PROP(c, dti, node, prop, "maximum bus number must be less than 256");
  670. }
  671. WARNING(pci_bridge, check_pci_bridge, NULL,
  672. &device_type_is_string, &addr_size_cells);
  673. static void check_pci_device_bus_num(struct check *c, struct dt_info *dti, struct node *node)
  674. {
  675. struct property *prop;
  676. unsigned int bus_num, min_bus, max_bus;
  677. cell_t *cells;
  678. if (!node->parent || (node->parent->bus != &pci_bus))
  679. return;
  680. prop = get_property(node, "reg");
  681. if (!prop)
  682. return;
  683. cells = (cell_t *)prop->val.val;
  684. bus_num = (fdt32_to_cpu(cells[0]) & 0x00ff0000) >> 16;
  685. prop = get_property(node->parent, "bus-range");
  686. if (!prop) {
  687. min_bus = max_bus = 0;
  688. } else {
  689. cells = (cell_t *)prop->val.val;
  690. min_bus = fdt32_to_cpu(cells[0]);
  691. max_bus = fdt32_to_cpu(cells[0]);
  692. }
  693. if ((bus_num < min_bus) || (bus_num > max_bus))
  694. FAIL_PROP(c, dti, node, prop, "PCI bus number %d out of range, expected (%d - %d)",
  695. bus_num, min_bus, max_bus);
  696. }
  697. WARNING(pci_device_bus_num, check_pci_device_bus_num, NULL, &reg_format, &pci_bridge);
  698. static void check_pci_device_reg(struct check *c, struct dt_info *dti, struct node *node)
  699. {
  700. struct property *prop;
  701. const char *unitname = get_unitname(node);
  702. char unit_addr[5];
  703. unsigned int dev, func, reg;
  704. cell_t *cells;
  705. if (!node->parent || (node->parent->bus != &pci_bus))
  706. return;
  707. prop = get_property(node, "reg");
  708. if (!prop) {
  709. FAIL(c, dti, node, "missing PCI reg property");
  710. return;
  711. }
  712. cells = (cell_t *)prop->val.val;
  713. if (cells[1] || cells[2])
  714. FAIL_PROP(c, dti, node, prop, "PCI reg config space address cells 2 and 3 must be 0");
  715. reg = fdt32_to_cpu(cells[0]);
  716. dev = (reg & 0xf800) >> 11;
  717. func = (reg & 0x700) >> 8;
  718. if (reg & 0xff000000)
  719. FAIL_PROP(c, dti, node, prop, "PCI reg address is not configuration space");
  720. if (reg & 0x000000ff)
  721. FAIL_PROP(c, dti, node, prop, "PCI reg config space address register number must be 0");
  722. if (func == 0) {
  723. snprintf(unit_addr, sizeof(unit_addr), "%x", dev);
  724. if (streq(unitname, unit_addr))
  725. return;
  726. }
  727. snprintf(unit_addr, sizeof(unit_addr), "%x,%x", dev, func);
  728. if (streq(unitname, unit_addr))
  729. return;
  730. FAIL(c, dti, node, "PCI unit address format error, expected \"%s\"",
  731. unit_addr);
  732. }
  733. WARNING(pci_device_reg, check_pci_device_reg, NULL, &reg_format, &pci_bridge);
  734. static const struct bus_type simple_bus = {
  735. .name = "simple-bus",
  736. };
  737. static bool node_is_compatible(struct node *node, const char *compat)
  738. {
  739. struct property *prop;
  740. const char *str, *end;
  741. prop = get_property(node, "compatible");
  742. if (!prop)
  743. return false;
  744. for (str = prop->val.val, end = str + prop->val.len; str < end;
  745. str += strnlen(str, end - str) + 1) {
  746. if (strprefixeq(str, end - str, compat))
  747. return true;
  748. }
  749. return false;
  750. }
  751. static void check_simple_bus_bridge(struct check *c, struct dt_info *dti, struct node *node)
  752. {
  753. if (node_is_compatible(node, "simple-bus"))
  754. node->bus = &simple_bus;
  755. }
  756. WARNING(simple_bus_bridge, check_simple_bus_bridge, NULL, &addr_size_cells);
  757. static void check_simple_bus_reg(struct check *c, struct dt_info *dti, struct node *node)
  758. {
  759. struct property *prop;
  760. const char *unitname = get_unitname(node);
  761. char unit_addr[17];
  762. unsigned int size;
  763. uint64_t reg = 0;
  764. cell_t *cells = NULL;
  765. if (!node->parent || (node->parent->bus != &simple_bus))
  766. return;
  767. prop = get_property(node, "reg");
  768. if (prop)
  769. cells = (cell_t *)prop->val.val;
  770. else {
  771. prop = get_property(node, "ranges");
  772. if (prop && prop->val.len)
  773. /* skip of child address */
  774. cells = ((cell_t *)prop->val.val) + node_addr_cells(node);
  775. }
  776. if (!cells) {
  777. if (node->parent->parent && !(node->bus == &simple_bus))
  778. FAIL(c, dti, node, "missing or empty reg/ranges property");
  779. return;
  780. }
  781. size = node_addr_cells(node->parent);
  782. while (size--)
  783. reg = (reg << 32) | fdt32_to_cpu(*(cells++));
  784. snprintf(unit_addr, sizeof(unit_addr), "%"PRIx64, reg);
  785. if (!streq(unitname, unit_addr))
  786. FAIL(c, dti, node, "simple-bus unit address format error, expected \"%s\"",
  787. unit_addr);
  788. }
  789. WARNING(simple_bus_reg, check_simple_bus_reg, NULL, &reg_format, &simple_bus_bridge);
  790. static void check_unit_address_format(struct check *c, struct dt_info *dti,
  791. struct node *node)
  792. {
  793. const char *unitname = get_unitname(node);
  794. if (node->parent && node->parent->bus)
  795. return;
  796. if (!unitname[0])
  797. return;
  798. if (!strncmp(unitname, "0x", 2)) {
  799. FAIL(c, dti, node, "unit name should not have leading \"0x\"");
  800. /* skip over 0x for next test */
  801. unitname += 2;
  802. }
  803. if (unitname[0] == '0' && isxdigit(unitname[1]))
  804. FAIL(c, dti, node, "unit name should not have leading 0s");
  805. }
  806. WARNING(unit_address_format, check_unit_address_format, NULL,
  807. &node_name_format, &pci_bridge, &simple_bus_bridge);
  808. /*
  809. * Style checks
  810. */
  811. static void check_avoid_default_addr_size(struct check *c, struct dt_info *dti,
  812. struct node *node)
  813. {
  814. struct property *reg, *ranges;
  815. if (!node->parent)
  816. return; /* Ignore root node */
  817. reg = get_property(node, "reg");
  818. ranges = get_property(node, "ranges");
  819. if (!reg && !ranges)
  820. return;
  821. if (node->parent->addr_cells == -1)
  822. FAIL(c, dti, node, "Relying on default #address-cells value");
  823. if (node->parent->size_cells == -1)
  824. FAIL(c, dti, node, "Relying on default #size-cells value");
  825. }
  826. WARNING(avoid_default_addr_size, check_avoid_default_addr_size, NULL,
  827. &addr_size_cells);
  828. static void check_avoid_unnecessary_addr_size(struct check *c, struct dt_info *dti,
  829. struct node *node)
  830. {
  831. struct property *prop;
  832. struct node *child;
  833. bool has_reg = false;
  834. if (!node->parent || node->addr_cells < 0 || node->size_cells < 0)
  835. return;
  836. if (get_property(node, "ranges") || !node->children)
  837. return;
  838. for_each_child(node, child) {
  839. prop = get_property(child, "reg");
  840. if (prop)
  841. has_reg = true;
  842. }
  843. if (!has_reg)
  844. FAIL(c, dti, node, "unnecessary #address-cells/#size-cells without \"ranges\" or child \"reg\" property");
  845. }
  846. WARNING(avoid_unnecessary_addr_size, check_avoid_unnecessary_addr_size, NULL, &avoid_default_addr_size);
  847. static void check_obsolete_chosen_interrupt_controller(struct check *c,
  848. struct dt_info *dti,
  849. struct node *node)
  850. {
  851. struct node *dt = dti->dt;
  852. struct node *chosen;
  853. struct property *prop;
  854. if (node != dt)
  855. return;
  856. chosen = get_node_by_path(dt, "/chosen");
  857. if (!chosen)
  858. return;
  859. prop = get_property(chosen, "interrupt-controller");
  860. if (prop)
  861. FAIL_PROP(c, dti, node, prop,
  862. "/chosen has obsolete \"interrupt-controller\" property");
  863. }
  864. WARNING(obsolete_chosen_interrupt_controller,
  865. check_obsolete_chosen_interrupt_controller, NULL);
  866. static void check_chosen_node_is_root(struct check *c, struct dt_info *dti,
  867. struct node *node)
  868. {
  869. if (!streq(node->name, "chosen"))
  870. return;
  871. if (node->parent != dti->dt)
  872. FAIL(c, dti, node, "chosen node must be at root node");
  873. }
  874. WARNING(chosen_node_is_root, check_chosen_node_is_root, NULL);
  875. static void check_chosen_node_bootargs(struct check *c, struct dt_info *dti,
  876. struct node *node)
  877. {
  878. struct property *prop;
  879. if (!streq(node->name, "chosen"))
  880. return;
  881. prop = get_property(node, "bootargs");
  882. if (!prop)
  883. return;
  884. c->data = prop->name;
  885. check_is_string(c, dti, node);
  886. }
  887. WARNING(chosen_node_bootargs, check_chosen_node_bootargs, NULL);
  888. static void check_chosen_node_stdout_path(struct check *c, struct dt_info *dti,
  889. struct node *node)
  890. {
  891. struct property *prop;
  892. if (!streq(node->name, "chosen"))
  893. return;
  894. prop = get_property(node, "stdout-path");
  895. if (!prop) {
  896. prop = get_property(node, "linux,stdout-path");
  897. if (!prop)
  898. return;
  899. FAIL_PROP(c, dti, node, prop, "Use 'stdout-path' instead");
  900. }
  901. c->data = prop->name;
  902. check_is_string(c, dti, node);
  903. }
  904. WARNING(chosen_node_stdout_path, check_chosen_node_stdout_path, NULL);
  905. struct provider {
  906. const char *prop_name;
  907. const char *cell_name;
  908. bool optional;
  909. };
  910. static void check_property_phandle_args(struct check *c,
  911. struct dt_info *dti,
  912. struct node *node,
  913. struct property *prop,
  914. const struct provider *provider)
  915. {
  916. struct node *root = dti->dt;
  917. int cell, cellsize = 0;
  918. if (prop->val.len % sizeof(cell_t)) {
  919. FAIL_PROP(c, dti, node, prop,
  920. "property size (%d) is invalid, expected multiple of %zu",
  921. prop->val.len, sizeof(cell_t));
  922. return;
  923. }
  924. for (cell = 0; cell < prop->val.len / sizeof(cell_t); cell += cellsize + 1) {
  925. struct node *provider_node;
  926. struct property *cellprop;
  927. int phandle;
  928. phandle = propval_cell_n(prop, cell);
  929. /*
  930. * Some bindings use a cell value 0 or -1 to skip over optional
  931. * entries when each index position has a specific definition.
  932. */
  933. if (phandle == 0 || phandle == -1) {
  934. /* Give up if this is an overlay with external references */
  935. if (dti->dtsflags & DTSF_PLUGIN)
  936. break;
  937. cellsize = 0;
  938. continue;
  939. }
  940. /* If we have markers, verify the current cell is a phandle */
  941. if (prop->val.markers) {
  942. struct marker *m = prop->val.markers;
  943. for_each_marker_of_type(m, REF_PHANDLE) {
  944. if (m->offset == (cell * sizeof(cell_t)))
  945. break;
  946. }
  947. if (!m)
  948. FAIL_PROP(c, dti, node, prop,
  949. "cell %d is not a phandle reference",
  950. cell);
  951. }
  952. provider_node = get_node_by_phandle(root, phandle);
  953. if (!provider_node) {
  954. FAIL_PROP(c, dti, node, prop,
  955. "Could not get phandle node for (cell %d)",
  956. cell);
  957. break;
  958. }
  959. cellprop = get_property(provider_node, provider->cell_name);
  960. if (cellprop) {
  961. cellsize = propval_cell(cellprop);
  962. } else if (provider->optional) {
  963. cellsize = 0;
  964. } else {
  965. FAIL(c, dti, node, "Missing property '%s' in node %s or bad phandle (referred from %s[%d])",
  966. provider->cell_name,
  967. provider_node->fullpath,
  968. prop->name, cell);
  969. break;
  970. }
  971. if (prop->val.len < ((cell + cellsize + 1) * sizeof(cell_t))) {
  972. FAIL_PROP(c, dti, node, prop,
  973. "property size (%d) too small for cell size %d",
  974. prop->val.len, cellsize);
  975. }
  976. }
  977. }
  978. static void check_provider_cells_property(struct check *c,
  979. struct dt_info *dti,
  980. struct node *node)
  981. {
  982. struct provider *provider = c->data;
  983. struct property *prop;
  984. prop = get_property(node, provider->prop_name);
  985. if (!prop)
  986. return;
  987. check_property_phandle_args(c, dti, node, prop, provider);
  988. }
  989. #define WARNING_PROPERTY_PHANDLE_CELLS(nm, propname, cells_name, ...) \
  990. static struct provider nm##_provider = { (propname), (cells_name), __VA_ARGS__ }; \
  991. WARNING(nm##_property, check_provider_cells_property, &nm##_provider, &phandle_references);
  992. WARNING_PROPERTY_PHANDLE_CELLS(clocks, "clocks", "#clock-cells");
  993. WARNING_PROPERTY_PHANDLE_CELLS(cooling_device, "cooling-device", "#cooling-cells");
  994. WARNING_PROPERTY_PHANDLE_CELLS(dmas, "dmas", "#dma-cells");
  995. WARNING_PROPERTY_PHANDLE_CELLS(hwlocks, "hwlocks", "#hwlock-cells");
  996. WARNING_PROPERTY_PHANDLE_CELLS(interrupts_extended, "interrupts-extended", "#interrupt-cells");
  997. WARNING_PROPERTY_PHANDLE_CELLS(io_channels, "io-channels", "#io-channel-cells");
  998. WARNING_PROPERTY_PHANDLE_CELLS(iommus, "iommus", "#iommu-cells");
  999. WARNING_PROPERTY_PHANDLE_CELLS(mboxes, "mboxes", "#mbox-cells");
  1000. WARNING_PROPERTY_PHANDLE_CELLS(msi_parent, "msi-parent", "#msi-cells", true);
  1001. WARNING_PROPERTY_PHANDLE_CELLS(mux_controls, "mux-controls", "#mux-control-cells");
  1002. WARNING_PROPERTY_PHANDLE_CELLS(phys, "phys", "#phy-cells");
  1003. WARNING_PROPERTY_PHANDLE_CELLS(power_domains, "power-domains", "#power-domain-cells");
  1004. WARNING_PROPERTY_PHANDLE_CELLS(pwms, "pwms", "#pwm-cells");
  1005. WARNING_PROPERTY_PHANDLE_CELLS(resets, "resets", "#reset-cells");
  1006. WARNING_PROPERTY_PHANDLE_CELLS(sound_dai, "sound-dai", "#sound-dai-cells");
  1007. WARNING_PROPERTY_PHANDLE_CELLS(thermal_sensors, "thermal-sensors", "#thermal-sensor-cells");
  1008. static bool prop_is_gpio(struct property *prop)
  1009. {
  1010. char *str;
  1011. /*
  1012. * *-gpios and *-gpio can appear in property names,
  1013. * so skip over any false matches (only one known ATM)
  1014. */
  1015. if (strstr(prop->name, "nr-gpio"))
  1016. return false;
  1017. str = strrchr(prop->name, '-');
  1018. if (str)
  1019. str++;
  1020. else
  1021. str = prop->name;
  1022. if (!(streq(str, "gpios") || streq(str, "gpio")))
  1023. return false;
  1024. return true;
  1025. }
  1026. static void check_gpios_property(struct check *c,
  1027. struct dt_info *dti,
  1028. struct node *node)
  1029. {
  1030. struct property *prop;
  1031. /* Skip GPIO hog nodes which have 'gpios' property */
  1032. if (get_property(node, "gpio-hog"))
  1033. return;
  1034. for_each_property(node, prop) {
  1035. struct provider provider;
  1036. if (!prop_is_gpio(prop))
  1037. continue;
  1038. provider.prop_name = prop->name;
  1039. provider.cell_name = "#gpio-cells";
  1040. provider.optional = false;
  1041. check_property_phandle_args(c, dti, node, prop, &provider);
  1042. }
  1043. }
  1044. WARNING(gpios_property, check_gpios_property, NULL, &phandle_references);
  1045. static void check_deprecated_gpio_property(struct check *c,
  1046. struct dt_info *dti,
  1047. struct node *node)
  1048. {
  1049. struct property *prop;
  1050. for_each_property(node, prop) {
  1051. char *str;
  1052. if (!prop_is_gpio(prop))
  1053. continue;
  1054. str = strstr(prop->name, "gpio");
  1055. if (!streq(str, "gpio"))
  1056. continue;
  1057. FAIL_PROP(c, dti, node, prop,
  1058. "'[*-]gpio' is deprecated, use '[*-]gpios' instead");
  1059. }
  1060. }
  1061. CHECK(deprecated_gpio_property, check_deprecated_gpio_property, NULL);
  1062. static bool node_is_interrupt_provider(struct node *node)
  1063. {
  1064. struct property *prop;
  1065. prop = get_property(node, "interrupt-controller");
  1066. if (prop)
  1067. return true;
  1068. prop = get_property(node, "interrupt-map");
  1069. if (prop)
  1070. return true;
  1071. return false;
  1072. }
  1073. static void check_interrupts_property(struct check *c,
  1074. struct dt_info *dti,
  1075. struct node *node)
  1076. {
  1077. struct node *root = dti->dt;
  1078. struct node *irq_node = NULL, *parent = node;
  1079. struct property *irq_prop, *prop = NULL;
  1080. int irq_cells, phandle;
  1081. irq_prop = get_property(node, "interrupts");
  1082. if (!irq_prop)
  1083. return;
  1084. if (irq_prop->val.len % sizeof(cell_t))
  1085. FAIL_PROP(c, dti, node, irq_prop, "size (%d) is invalid, expected multiple of %zu",
  1086. irq_prop->val.len, sizeof(cell_t));
  1087. while (parent && !prop) {
  1088. if (parent != node && node_is_interrupt_provider(parent)) {
  1089. irq_node = parent;
  1090. break;
  1091. }
  1092. prop = get_property(parent, "interrupt-parent");
  1093. if (prop) {
  1094. phandle = propval_cell(prop);
  1095. /* Give up if this is an overlay with external references */
  1096. if ((phandle == 0 || phandle == -1) &&
  1097. (dti->dtsflags & DTSF_PLUGIN))
  1098. return;
  1099. irq_node = get_node_by_phandle(root, phandle);
  1100. if (!irq_node) {
  1101. FAIL_PROP(c, dti, parent, prop, "Bad phandle");
  1102. return;
  1103. }
  1104. if (!node_is_interrupt_provider(irq_node))
  1105. FAIL(c, dti, irq_node,
  1106. "Missing interrupt-controller or interrupt-map property");
  1107. break;
  1108. }
  1109. parent = parent->parent;
  1110. }
  1111. if (!irq_node) {
  1112. FAIL(c, dti, node, "Missing interrupt-parent");
  1113. return;
  1114. }
  1115. prop = get_property(irq_node, "#interrupt-cells");
  1116. if (!prop) {
  1117. FAIL(c, dti, irq_node, "Missing #interrupt-cells in interrupt-parent");
  1118. return;
  1119. }
  1120. irq_cells = propval_cell(prop);
  1121. if (irq_prop->val.len % (irq_cells * sizeof(cell_t))) {
  1122. FAIL_PROP(c, dti, node, prop,
  1123. "size is (%d), expected multiple of %d",
  1124. irq_prop->val.len, (int)(irq_cells * sizeof(cell_t)));
  1125. }
  1126. }
  1127. WARNING(interrupts_property, check_interrupts_property, &phandle_references);
  1128. static struct check *check_table[] = {
  1129. &duplicate_node_names, &duplicate_property_names,
  1130. &node_name_chars, &node_name_format, &property_name_chars,
  1131. &name_is_string, &name_properties,
  1132. &duplicate_label,
  1133. &explicit_phandles,
  1134. &phandle_references, &path_references,
  1135. &address_cells_is_cell, &size_cells_is_cell, &interrupt_cells_is_cell,
  1136. &device_type_is_string, &model_is_string, &status_is_string,
  1137. &label_is_string,
  1138. &compatible_is_string_list, &names_is_string_list,
  1139. &property_name_chars_strict,
  1140. &node_name_chars_strict,
  1141. &addr_size_cells, &reg_format, &ranges_format,
  1142. &unit_address_vs_reg,
  1143. &unit_address_format,
  1144. &pci_bridge,
  1145. &pci_device_reg,
  1146. &pci_device_bus_num,
  1147. &simple_bus_bridge,
  1148. &simple_bus_reg,
  1149. &avoid_default_addr_size,
  1150. &avoid_unnecessary_addr_size,
  1151. &obsolete_chosen_interrupt_controller,
  1152. &chosen_node_is_root, &chosen_node_bootargs, &chosen_node_stdout_path,
  1153. &clocks_property,
  1154. &cooling_device_property,
  1155. &dmas_property,
  1156. &hwlocks_property,
  1157. &interrupts_extended_property,
  1158. &io_channels_property,
  1159. &iommus_property,
  1160. &mboxes_property,
  1161. &msi_parent_property,
  1162. &mux_controls_property,
  1163. &phys_property,
  1164. &power_domains_property,
  1165. &pwms_property,
  1166. &resets_property,
  1167. &sound_dai_property,
  1168. &thermal_sensors_property,
  1169. &deprecated_gpio_property,
  1170. &gpios_property,
  1171. &interrupts_property,
  1172. &alias_paths,
  1173. &always_fail,
  1174. };
  1175. static void enable_warning_error(struct check *c, bool warn, bool error)
  1176. {
  1177. int i;
  1178. /* Raising level, also raise it for prereqs */
  1179. if ((warn && !c->warn) || (error && !c->error))
  1180. for (i = 0; i < c->num_prereqs; i++)
  1181. enable_warning_error(c->prereq[i], warn, error);
  1182. c->warn = c->warn || warn;
  1183. c->error = c->error || error;
  1184. }
  1185. static void disable_warning_error(struct check *c, bool warn, bool error)
  1186. {
  1187. int i;
  1188. /* Lowering level, also lower it for things this is the prereq
  1189. * for */
  1190. if ((warn && c->warn) || (error && c->error)) {
  1191. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  1192. struct check *cc = check_table[i];
  1193. int j;
  1194. for (j = 0; j < cc->num_prereqs; j++)
  1195. if (cc->prereq[j] == c)
  1196. disable_warning_error(cc, warn, error);
  1197. }
  1198. }
  1199. c->warn = c->warn && !warn;
  1200. c->error = c->error && !error;
  1201. }
  1202. void parse_checks_option(bool warn, bool error, const char *arg)
  1203. {
  1204. int i;
  1205. const char *name = arg;
  1206. bool enable = true;
  1207. if ((strncmp(arg, "no-", 3) == 0)
  1208. || (strncmp(arg, "no_", 3) == 0)) {
  1209. name = arg + 3;
  1210. enable = false;
  1211. }
  1212. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  1213. struct check *c = check_table[i];
  1214. if (streq(c->name, name)) {
  1215. if (enable)
  1216. enable_warning_error(c, warn, error);
  1217. else
  1218. disable_warning_error(c, warn, error);
  1219. return;
  1220. }
  1221. }
  1222. die("Unrecognized check name \"%s\"\n", name);
  1223. }
  1224. void process_checks(bool force, struct dt_info *dti)
  1225. {
  1226. int i;
  1227. int error = 0;
  1228. for (i = 0; i < ARRAY_SIZE(check_table); i++) {
  1229. struct check *c = check_table[i];
  1230. if (c->warn || c->error)
  1231. error = error || run_check(c, dti);
  1232. }
  1233. if (error) {
  1234. if (!force) {
  1235. fprintf(stderr, "ERROR: Input tree has errors, aborting "
  1236. "(use -f to force output)\n");
  1237. exit(2);
  1238. } else if (quiet < 3) {
  1239. fprintf(stderr, "Warning: Input tree has errors, "
  1240. "output forced\n");
  1241. }
  1242. }
  1243. }