blob: 0a34109dc0fb0b01f63956e1885e7da789a00296 [file] [log] [blame]
David Gibson2f1ccc32007-11-01 16:49:26 +11001/*
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
21#include "dtc.h"
22
23/*
24 * Structural check functions
25 */
26
27#define ERRMSG(...) if (quiet < 2) fprintf(stderr, "ERROR: " __VA_ARGS__)
28#define WARNMSG(...) if (quiet < 1) fprintf(stderr, "Warning: " __VA_ARGS__)
29
30#define DO_ERR(...) do {ERRMSG(__VA_ARGS__); ok = 0; } while (0)
31
32static int check_names(struct node *tree)
33{
34 struct node *child, *child2;
35 struct property *prop, *prop2;
36 int len = strlen(tree->name);
37 int ok = 1;
38
39 if (len == 0 && tree->parent)
40 DO_ERR("Empty, non-root nodename at %s\n", tree->fullpath);
41
42 if (len > MAX_NODENAME_LEN)
43 WARNMSG("Overlength nodename at %s\n", tree->fullpath);
44
45 for_each_property(tree, prop) {
46 /* check for duplicates */
47 /* FIXME: do this more efficiently */
48 for (prop2 = prop->next; prop2; prop2 = prop2->next) {
49 if (streq(prop->name, prop2->name)) {
50 DO_ERR("Duplicate propertyname %s in node %s\n",
51 prop->name, tree->fullpath);
52 }
53 }
54
55 /* check name length */
56 if (strlen(prop->name) > MAX_PROPNAME_LEN)
57 WARNMSG("Property name %s is too long in %s\n",
58 prop->name, tree->fullpath);
59 }
60
61 for_each_child(tree, child) {
62 /* Check for duplicates */
63
64 for (child2 = child->next_sibling;
65 child2;
66 child2 = child2->next_sibling) {
67 if (streq(child->name, child2->name))
68 DO_ERR("Duplicate node name %s\n",
69 child->fullpath);
70 }
71 if (! check_names(child))
72 ok = 0;
73 }
74
75 return ok;
76}
77
78static int check_phandles(struct node *root, struct node *node)
79{
80 struct property *prop;
81 struct node *child, *other;
82 cell_t phandle;
83 int ok = 1;
84
85 prop = get_property(node, "linux,phandle");
86 if (prop) {
87 phandle = propval_cell(prop);
88 if ((phandle == 0) || (phandle == -1)) {
89 DO_ERR("%s has invalid linux,phandle %x\n",
90 node->fullpath, phandle);
91 } else {
92 other = get_node_by_phandle(root, phandle);
93 if (other)
94 DO_ERR("%s has duplicated phandle %x (seen before at %s)\n",
95 node->fullpath, phandle, other->fullpath);
96
97 node->phandle = phandle;
98 }
99 }
100
101 for_each_child(node, child)
102 ok = ok && check_phandles(root, child);
103
David Gibson0d6ade22007-11-20 16:24:23 +1100104 return ok;
David Gibson2f1ccc32007-11-01 16:49:26 +1100105}
106
107int check_structure(struct node *dt)
108{
109 int ok = 1;
110
111 ok = ok && check_names(dt);
112 ok = ok && check_phandles(dt, dt);
113
114 return ok;
115}
116
117/*
118 * Semantic check functions
119 */
120
121static int must_be_one_cell(struct property *prop, struct node *node)
122{
123 if (prop->val.len != sizeof(cell_t)) {
124 ERRMSG("\"%s\" property in %s has the wrong length (should be 1 cell)\n",
125 prop->name, node->fullpath);
126 return 0;
127 }
128
129 return 1;
130}
131
132static int must_be_cells(struct property *prop, struct node *node)
133{
134 if ((prop->val.len % sizeof(cell_t)) != 0) {
135 ERRMSG("\"%s\" property in %s is not a multiple of cell size\n",
136 prop->name, node->fullpath);
137 return 0;
138 }
139
140 return 1;
141}
142
143static int must_be_string(struct property *prop, struct node *node)
144{
145 if (! data_is_one_string(prop->val)) {
146 ERRMSG("\"%s\" property in %s is not a string\n",
147 prop->name, node->fullpath);
148 return 0;
149 }
150
151 return 1;
152}
153
154static int name_prop_check(struct property *prop, struct node *node)
155{
156 if ((prop->val.len != node->basenamelen+1)
157 || !strneq(prop->val.val, node->name, node->basenamelen)) {
158 ERRMSG("name property \"%s\" does not match node basename in %s\n",
159 prop->val.val,
160 node->fullpath);
161 return 0;
162 }
163
164 return 1;
165}
166
167static struct {
168 char *propname;
169 int (*check_fn)(struct property *prop, struct node *node);
170} prop_checker_table[] = {
171 {"name", must_be_string},
172 {"name", name_prop_check},
173 {"linux,phandle", must_be_one_cell},
174 {"#address-cells", must_be_one_cell},
175 {"#size-cells", must_be_one_cell},
176 {"reg", must_be_cells},
177 {"model", must_be_string},
178 {"device_type", must_be_string},
179};
180
181static int check_properties(struct node *node)
182{
183 struct property *prop;
184 struct node *child;
185 int i;
186 int ok = 1;
187
188 for_each_property(node, prop)
189 for (i = 0; i < ARRAY_SIZE(prop_checker_table); i++)
190 if (streq(prop->name, prop_checker_table[i].propname))
191 if (! prop_checker_table[i].check_fn(prop, node)) {
192 ok = 0;
193 break;
194 }
195
196 for_each_child(node, child)
197 if (! check_properties(child))
198 ok = 0;
199
200 return ok;
201}
202
203#define CHECK_HAVE(node, propname) \
204 do { \
205 if (! (prop = get_property((node), (propname)))) \
206 DO_ERR("Missing \"%s\" property in %s\n", (propname), \
207 (node)->fullpath); \
208 } while (0);
209
210#define CHECK_HAVE_WARN(node, propname) \
211 do { \
212 if (! (prop = get_property((node), (propname)))) \
213 WARNMSG("%s has no \"%s\" property\n", \
214 (node)->fullpath, (propname)); \
215 } while (0)
216
217#define CHECK_HAVE_STRING(node, propname) \
218 do { \
219 CHECK_HAVE((node), (propname)); \
220 if (prop && !data_is_one_string(prop->val)) \
221 DO_ERR("\"%s\" property in %s is not a string\n", \
222 (propname), (node)->fullpath); \
223 } while (0)
224
225#define CHECK_HAVE_STREQ(node, propname, value) \
226 do { \
227 CHECK_HAVE_STRING((node), (propname)); \
228 if (prop && !streq(prop->val.val, (value))) \
229 DO_ERR("%s has wrong %s, %s (should be %s\n", \
230 (node)->fullpath, (propname), \
231 prop->val.val, (value)); \
232 } while (0)
233
234#define CHECK_HAVE_ONECELL(node, propname) \
235 do { \
236 CHECK_HAVE((node), (propname)); \
237 if (prop && (prop->val.len != sizeof(cell_t))) \
238 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
239 } while (0)
240
241#define CHECK_HAVE_WARN_ONECELL(node, propname) \
242 do { \
243 CHECK_HAVE_WARN((node), (propname)); \
244 if (prop && (prop->val.len != sizeof(cell_t))) \
245 DO_ERR("\"%s\" property in %s has wrong size %d (should be 1 cell)\n", (propname), (node)->fullpath, prop->val.len); \
246 } while (0)
247
248#define CHECK_HAVE_WARN_PHANDLE(xnode, propname, root) \
249 do { \
250 struct node *ref; \
251 CHECK_HAVE_WARN_ONECELL((xnode), (propname)); \
252 if (prop) {\
253 cell_t phandle = propval_cell(prop); \
254 if ((phandle == 0) || (phandle == -1)) { \
255 DO_ERR("\"%s\" property in %s contains an invalid phandle %x\n", (propname), (xnode)->fullpath, phandle); \
256 } else { \
257 ref = get_node_by_phandle((root), propval_cell(prop)); \
258 if (! ref) \
259 DO_ERR("\"%s\" property in %s refers to non-existant phandle %x\n", (propname), (xnode)->fullpath, propval_cell(prop)); \
260 } \
261 } \
262 } while (0)
263
264#define CHECK_HAVE_WARN_STRING(node, propname) \
265 do { \
266 CHECK_HAVE_WARN((node), (propname)); \
267 if (prop && !data_is_one_string(prop->val)) \
268 DO_ERR("\"%s\" property in %s is not a string\n", \
269 (propname), (node)->fullpath); \
270 } while (0)
271
272static int check_root(struct node *root)
273{
274 struct property *prop;
275 int ok = 1;
276
277 CHECK_HAVE_STRING(root, "model");
278
279 CHECK_HAVE(root, "#address-cells");
280 CHECK_HAVE(root, "#size-cells");
281
282 CHECK_HAVE_WARN(root, "compatible");
283
284 return ok;
285}
286
287static int check_cpus(struct node *root, int outversion, int boot_cpuid_phys)
288{
289 struct node *cpus, *cpu;
290 struct property *prop;
291 struct node *bootcpu = NULL;
292 int ok = 1;
293
294 cpus = get_subnode(root, "cpus");
295 if (! cpus) {
296 ERRMSG("Missing /cpus node\n");
297 return 0;
298 }
299
300 CHECK_HAVE_WARN(cpus, "#address-cells");
301 CHECK_HAVE_WARN(cpus, "#size-cells");
302
303 for_each_child(cpus, cpu) {
304 CHECK_HAVE_STREQ(cpu, "device_type", "cpu");
305
306 if (cpu->addr_cells != 1)
307 DO_ERR("%s has bad #address-cells value %d (should be 1)\n",
308 cpu->fullpath, cpu->addr_cells);
309 if (cpu->size_cells != 0)
310 DO_ERR("%s has bad #size-cells value %d (should be 0)\n",
311 cpu->fullpath, cpu->size_cells);
312
313 CHECK_HAVE_ONECELL(cpu, "reg");
314 if (prop) {
315 cell_t unitnum;
316 char *eptr;
317
318 unitnum = strtol(get_unitname(cpu), &eptr, 16);
319 if (*eptr) {
320 WARNMSG("%s has bad format unit name %s (should be CPU number\n",
321 cpu->fullpath, get_unitname(cpu));
322 } else if (unitnum != propval_cell(prop)) {
323 WARNMSG("%s unit name \"%s\" does not match \"reg\" property <%x>\n",
324 cpu->fullpath, get_unitname(cpu),
325 propval_cell(prop));
326 }
327 }
328
329/* CHECK_HAVE_ONECELL(cpu, "d-cache-line-size"); */
330/* CHECK_HAVE_ONECELL(cpu, "i-cache-line-size"); */
331 CHECK_HAVE_ONECELL(cpu, "d-cache-size");
332 CHECK_HAVE_ONECELL(cpu, "i-cache-size");
333
334 CHECK_HAVE_WARN_ONECELL(cpu, "clock-frequency");
335 CHECK_HAVE_WARN_ONECELL(cpu, "timebase-frequency");
336
337 prop = get_property(cpu, "linux,boot-cpu");
338 if (prop) {
339 if (prop->val.len)
340 WARNMSG("\"linux,boot-cpu\" property in %s is non-empty\n",
341 cpu->fullpath);
342 if (bootcpu)
343 DO_ERR("Multiple boot cpus (%s and %s)\n",
344 bootcpu->fullpath, cpu->fullpath);
345 else
346 bootcpu = cpu;
347 }
348 }
349
350 if (outversion < 2) {
351 if (! bootcpu)
352 WARNMSG("No cpu has \"linux,boot-cpu\" property\n");
353 } else {
354 if (bootcpu)
355 WARNMSG("\"linux,boot-cpu\" property is deprecated in blob version 2 or higher\n");
356 if (boot_cpuid_phys == 0xfeedbeef)
357 WARNMSG("physical boot CPU not set. Use -b option to set\n");
358 }
359
360 return ok;
361}
362
363static int check_memory(struct node *root)
364{
365 struct node *mem;
366 struct property *prop;
367 int nnodes = 0;
368 int ok = 1;
369
370 for_each_child(root, mem) {
371 if (! strneq(mem->name, "memory", mem->basenamelen))
372 continue;
373
374 nnodes++;
375
376 CHECK_HAVE_STREQ(mem, "device_type", "memory");
377 CHECK_HAVE(mem, "reg");
378 }
379
380 if (nnodes == 0) {
381 ERRMSG("No memory nodes\n");
382 return 0;
383 }
384
385 return ok;
386}
387
388static int check_chosen(struct node *root)
389{
390 struct node *chosen;
391 struct property *prop;
392 int ok = 1;
393
394 chosen = get_subnode(root, "chosen");
395 if (!chosen)
396 return ok;
397
398 /* give warning for obsolete interrupt-controller property */
399 do {
400 if ((prop = get_property(chosen, "interrupt-controller")) != NULL) {
401 WARNMSG("%s has obsolete \"%s\" property\n",
402 chosen->fullpath, "interrupt-controller");
403 }
404 } while (0);
405
406 return ok;
407}
408
409static int check_addr_size_reg(struct node *node,
410 int p_addr_cells, int p_size_cells)
411{
412 int addr_cells = p_addr_cells;
413 int size_cells = p_size_cells;
414 struct property *prop;
415 struct node *child;
416 int ok = 1;
417
418 node->addr_cells = addr_cells;
419 node->size_cells = size_cells;
420
421 prop = get_property(node, "reg");
422 if (prop) {
423 int len = prop->val.len / 4;
424
425 if ((len % (addr_cells+size_cells)) != 0)
426 DO_ERR("\"reg\" property in %s has invalid length (%d) for given #address-cells (%d) and #size-cells (%d)\n",
427 node->fullpath, prop->val.len,
428 addr_cells, size_cells);
429 }
430
431 prop = get_property(node, "#address-cells");
432 if (prop)
433 addr_cells = propval_cell(prop);
434
435 prop = get_property(node, "#size-cells");
436 if (prop)
437 size_cells = propval_cell(prop);
438
439 for_each_child(node, child) {
440 ok = ok && check_addr_size_reg(child, addr_cells, size_cells);
441 }
442
443 return ok;
444}
445
446int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys)
447{
448 int ok = 1;
449
450 ok = ok && check_properties(dt);
451 ok = ok && check_addr_size_reg(dt, -1, -1);
452 ok = ok && check_root(dt);
453 ok = ok && check_cpus(dt, outversion, boot_cpuid_phys);
454 ok = ok && check_memory(dt);
455 ok = ok && check_chosen(dt);
456 if (! ok)
457 return 0;
458
459 return 1;
460}