]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/tree.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / sim / ppc / tree.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (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
13 GNU 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, see <http://www.gnu.org/licenses/>.
17
18 */
19
20
21 #ifndef _PARSE_C_
22 #define _PARSE_C_
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "basics.h"
28
29 #include "device.h"
30 #include "tree.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include "libiberty.h"
37
38 /* manipulate/lookup device names */
39
40 typedef struct _name_specifier {
41 /* components in the full length name */
42 char *path;
43 char *property;
44 char *value;
45 /* current device */
46 char *name;
47 char *base;
48 char *unit;
49 char *args;
50 /* previous device */
51 char *last_name;
52 char *last_base;
53 char *last_unit;
54 char *last_args;
55 /* work area */
56 char buf[1024];
57 } name_specifier;
58
59
60
61 /* Given a device specifier, break it up into its main components:
62 path (and if present) property name and property value. */
63
64 STATIC_INLINE_TREE\
65 (int)
66 split_device_specifier(device *current,
67 const char *device_specifier,
68 name_specifier *spec)
69 {
70 char *chp = NULL;
71
72 /* expand any leading alias if present */
73 if (current != NULL
74 && *device_specifier != '\0'
75 && *device_specifier != '.'
76 && *device_specifier != '/') {
77 device *aliases = tree_find_device(current, "/aliases");
78 char alias[32];
79 int len = 0;
80 while (device_specifier[len] != '\0'
81 && device_specifier[len] != '/'
82 && device_specifier[len] != ':'
83 && !isspace(device_specifier[len])) {
84 alias[len] = device_specifier[len];
85 len++;
86 if (len >= sizeof(alias))
87 error("split_device_specifier: buffer overflow");
88 }
89 alias[len] = '\0';
90 if (aliases != NULL
91 && device_find_property(aliases, alias)) {
92 strcpy(spec->buf, device_find_string_property(aliases, alias));
93 strcat(spec->buf, device_specifier + len);
94 }
95 else {
96 strcpy(spec->buf, device_specifier);
97 }
98 }
99 else {
100 strcpy(spec->buf, device_specifier);
101 }
102
103 /* check no overflow */
104 if (strlen(spec->buf) >= sizeof(spec->buf))
105 error("split_device_specifier: buffer overflow\n");
106
107 /* strip leading spaces */
108 chp = spec->buf;
109 while (*chp != '\0' && isspace(*chp))
110 chp++;
111 if (*chp == '\0')
112 return 0;
113
114 /* find the path and terminate it with null */
115 spec->path = chp;
116 while (*chp != '\0' && !isspace(*chp))
117 chp++;
118 if (*chp != '\0') {
119 *chp = '\0';
120 chp++;
121 }
122
123 /* and any value */
124 while (*chp != '\0' && isspace(*chp))
125 chp++;
126 spec->value = chp;
127
128 /* now go back and chop the property off of the path */
129 if (spec->value[0] == '\0') {
130 spec->property = NULL; /*not a property*/
131 spec->value = NULL;
132 }
133 else if (spec->value[0] == '>'
134 || spec->value[0] == '<') {
135 /* an interrupt spec */
136 spec->property = NULL;
137 }
138 else {
139 chp = strrchr(spec->path, '/');
140 if (chp == NULL) {
141 spec->property = spec->path;
142 spec->path = strchr(spec->property, '\0');
143 }
144 else {
145 *chp = '\0';
146 spec->property = chp+1;
147 }
148 }
149
150 /* and mark the rest as invalid */
151 spec->name = NULL;
152 spec->base = NULL;
153 spec->unit = NULL;
154 spec->args = NULL;
155 spec->last_name = NULL;
156 spec->last_base = NULL;
157 spec->last_unit = NULL;
158 spec->last_args = NULL;
159
160 return 1;
161 }
162
163
164 /* given a device specifier break it up into its main components -
165 path and property name - assuming that the last `device' is a
166 property name. */
167
168 STATIC_INLINE_DEVICE\
169 (int)
170 split_property_specifier(device *current,
171 const char *property_specifier,
172 name_specifier *spec)
173 {
174 if (split_device_specifier(current, property_specifier, spec)) {
175 if (spec->property == NULL) {
176 /* force the last name to be a property name */
177 char *chp = strrchr(spec->path, '/');
178 if (chp == NULL) {
179 spec->property = spec->path;
180 spec->path = strrchr(spec->property, '\0');;
181 }
182 else {
183 *chp = '\0';
184 spec->property = chp+1;
185 }
186 }
187 return 1;
188 }
189 else
190 return 0;
191 }
192
193
194 /* device the next device name and split it up, return 0 when no more
195 names to device */
196
197 STATIC_INLINE_TREE\
198 (int)
199 split_device_name(name_specifier *spec)
200 {
201 char *chp;
202 /* remember what came before */
203 spec->last_name = spec->name;
204 spec->last_base = spec->base;
205 spec->last_unit = spec->unit;
206 spec->last_args = spec->args;
207 /* finished? */
208 if (spec->path[0] == '\0') {
209 spec->name = NULL;
210 spec->base = NULL;
211 spec->unit = NULL;
212 spec->args = NULL;
213 return 0;
214 }
215 /* break the current device spec from the path */
216 spec->name = spec->path;
217 chp = strchr(spec->name, '/');
218 if (chp == NULL)
219 spec->path = strchr(spec->name, '\0');
220 else {
221 spec->path = chp+1;
222 *chp = '\0';
223 }
224 /* break out the base */
225 if (spec->name[0] == '(') {
226 chp = strchr(spec->name, ')');
227 if (chp == NULL) {
228 spec->base = spec->name;
229 }
230 else {
231 *chp = '\0';
232 spec->base = spec->name + 1;
233 spec->name = chp + 1;
234 }
235 }
236 else {
237 spec->base = spec->name;
238 }
239 /* now break out the unit */
240 chp = strchr(spec->name, '@');
241 if (chp == NULL) {
242 spec->unit = NULL;
243 chp = spec->name;
244 }
245 else {
246 *chp = '\0';
247 chp += 1;
248 spec->unit = chp;
249 }
250 /* finally any args */
251 chp = strchr(chp, ':');
252 if (chp == NULL)
253 spec->args = NULL;
254 else {
255 *chp = '\0';
256 spec->args = chp+1;
257 }
258 return 1;
259 }
260
261
262 /* device the value, returning the next non-space token */
263
264 STATIC_INLINE_TREE\
265 (char *)
266 split_value(name_specifier *spec)
267 {
268 char *token;
269 if (spec->value == NULL)
270 return NULL;
271 /* skip leading white space */
272 while (isspace(spec->value[0]))
273 spec->value++;
274 if (spec->value[0] == '\0') {
275 spec->value = NULL;
276 return NULL;
277 }
278 token = spec->value;
279 /* find trailing space */
280 while (spec->value[0] != '\0' && !isspace(spec->value[0]))
281 spec->value++;
282 /* chop this value out */
283 if (spec->value[0] != '\0') {
284 spec->value[0] = '\0';
285 spec->value++;
286 }
287 return token;
288 }
289
290
291
292 /* traverse the path specified by spec starting at current */
293
294 STATIC_INLINE_TREE\
295 (device *)
296 split_find_device(device *current,
297 name_specifier *spec)
298 {
299 /* strip off (and process) any leading ., .., ./ and / */
300 while (1) {
301 if (strncmp(spec->path, "/", strlen("/")) == 0) {
302 /* cd /... */
303 while (current != NULL && device_parent(current) != NULL)
304 current = device_parent(current);
305 spec->path += strlen("/");
306 }
307 else if (strncmp(spec->path, "./", strlen("./")) == 0) {
308 /* cd ./... */
309 spec->path += strlen("./");
310 }
311 else if (strncmp(spec->path, "../", strlen("../")) == 0) {
312 /* cd ../... */
313 if (current != NULL && device_parent(current) != NULL)
314 current = device_parent(current);
315 spec->path += strlen("../");
316 }
317 else if (strcmp(spec->path, ".") == 0) {
318 /* cd . */
319 spec->path += strlen(".");
320 }
321 else if (strcmp(spec->path, "..") == 0) {
322 /* cd . */
323 if (current != NULL && device_parent(current) != NULL)
324 current = device_parent(current);
325 spec->path += strlen("..");
326 }
327 else
328 break;
329 }
330
331 /* now go through the path proper */
332
333 if (current == NULL) {
334 split_device_name(spec);
335 return NULL;
336 }
337
338 while (split_device_name(spec)) {
339 device *child;
340 for (child = device_child(current);
341 child != NULL; child = device_sibling(child)) {
342 if (strcmp(spec->name, device_name(child)) == 0) {
343 if (spec->unit == NULL)
344 break;
345 else {
346 device_unit phys;
347 device_decode_unit(current, spec->unit, &phys);
348 if (memcmp(&phys, device_unit_address(child),
349 sizeof(device_unit)) == 0)
350 break;
351 }
352 }
353 }
354 if (child == NULL)
355 return current; /* search failed */
356 current = child;
357 }
358
359 return current;
360 }
361
362
363 STATIC_INLINE_TREE\
364 (device *)
365 split_fill_path(device *current,
366 const char *device_specifier,
367 name_specifier *spec)
368 {
369 /* break it up */
370 if (!split_device_specifier(current, device_specifier, spec))
371 device_error(current, "error parsing %s\n", device_specifier);
372
373 /* fill our tree with its contents */
374 current = split_find_device(current, spec);
375
376 /* add any additional devices as needed */
377 if (spec->name != NULL) {
378 do {
379 current = device_create(current, spec->base, spec->name,
380 spec->unit, spec->args);
381 } while (split_device_name(spec));
382 }
383
384 return current;
385 }
386
387
388 INLINE_TREE\
389 (void)
390 tree_init(device *root,
391 psim *system)
392 {
393 TRACE(trace_device_tree, ("tree_init(root=%p, system=%p)\n",
394 root,
395 system));
396 /* remove the old, rebuild the new */
397 tree_traverse(root, device_clean, NULL, system);
398 tree_traverse(root, device_init_static_properties, NULL, system);
399 tree_traverse(root, device_init_address, NULL, system);
400 tree_traverse(root, device_init_runtime_properties, NULL, system);
401 tree_traverse(root, device_init_data, NULL, system);
402 }
403
404
405 \f
406 /* <non-white-space> */
407
408 STATIC_INLINE_TREE\
409 (const char *)
410 skip_token(const char *chp)
411 {
412 while (!isspace(*chp) && *chp != '\0')
413 chp++;
414 while (isspace(*chp) && *chp != '\0')
415 chp++;
416 return chp;
417 }
418
419
420 /* count the number of entries */
421
422 STATIC_INLINE_TREE\
423 (int)
424 count_entries(device *current,
425 const char *property_name,
426 const char *property_value,
427 int modulo)
428 {
429 const char *chp = property_value;
430 int nr_entries = 0;
431 while (*chp != '\0') {
432 nr_entries += 1;
433 chp = skip_token(chp);
434 }
435 if ((nr_entries % modulo) != 0) {
436 device_error(current, "incorrect number of entries for %s property %s, should be multiple of %d",
437 property_name, property_value, modulo);
438 }
439 return nr_entries / modulo;
440 }
441
442
443
444 /* parse: <address> ::= <token> ; device dependant */
445
446 STATIC_INLINE_TREE\
447 (const char *)
448 parse_address(device *current,
449 device *bus,
450 const char *chp,
451 device_unit *address)
452 {
453 ASSERT(device_nr_address_cells(bus) > 0);
454 if (device_decode_unit(bus, chp, address) < 0)
455 device_error(current, "invalid unit address in %s", chp);
456 return skip_token(chp);
457 }
458
459
460 /* parse: <size> ::= <number> { "," <number> } ; */
461
462 STATIC_INLINE_TREE\
463 (const char *)
464 parse_size(device *current,
465 device *bus,
466 const char *chp,
467 device_unit *size)
468 {
469 int i;
470 int nr;
471 const char *curr = chp;
472 memset(size, 0, sizeof(*size));
473 /* parse the numeric list */
474 size->nr_cells = device_nr_size_cells(bus);
475 nr = 0;
476 ASSERT(size->nr_cells > 0);
477 while (1) {
478 char *next;
479 size->cells[nr] = strtoul(curr, &next, 0);
480 if (curr == next)
481 device_error(current, "Problem parsing <size> %s", chp);
482 nr += 1;
483 if (next[0] != ',')
484 break;
485 if (nr == size->nr_cells)
486 device_error(current, "Too many values in <size> %s", chp);
487 curr = next + 1;
488 }
489 ASSERT(nr > 0 && nr <= size->nr_cells);
490 /* right align the numbers */
491 for (i = 1; i <= size->nr_cells; i++) {
492 if (i <= nr)
493 size->cells[size->nr_cells - i] = size->cells[nr - i];
494 else
495 size->cells[size->nr_cells - i] = 0;
496 }
497 return skip_token(chp);
498 }
499
500
501 /* parse: <reg> ::= { <address> <size> } ; */
502
503 STATIC_INLINE_TREE\
504 (void)
505 parse_reg_property(device *current,
506 const char *property_name,
507 const char *property_value)
508 {
509 int nr_regs;
510 int reg_nr;
511 reg_property_spec *regs;
512 const char *chp;
513 device *bus = device_parent(current);
514
515 /* determine the number of reg entries by counting tokens */
516 nr_regs = count_entries(current, property_name, property_value,
517 1 + (device_nr_size_cells(bus) > 0));
518
519 /* create working space */
520 regs = zalloc(nr_regs * sizeof(*regs));
521
522 /* fill it in */
523 chp = property_value;
524 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) {
525 chp = parse_address(current, bus, chp, &regs[reg_nr].address);
526 if (device_nr_size_cells(bus) > 0)
527 chp = parse_size(current, bus, chp, &regs[reg_nr].size);
528 else
529 memset(&regs[reg_nr].size, 0, sizeof (regs[reg_nr].size));
530 }
531
532 /* create it */
533 device_add_reg_array_property(current, property_name,
534 regs, nr_regs);
535
536 free(regs);
537 }
538
539
540 /* { <child-address> <parent-address> <child-size> }* */
541
542 STATIC_INLINE_TREE\
543 (void)
544 parse_ranges_property(device *current,
545 const char *property_name,
546 const char *property_value)
547 {
548 int nr_ranges;
549 int range_nr;
550 range_property_spec *ranges;
551 const char *chp;
552
553 /* determine the number of ranges specified */
554 nr_ranges = count_entries(current, property_name, property_value, 3);
555
556 /* create a property of that size */
557 ranges = zalloc(nr_ranges * sizeof(*ranges));
558
559 /* fill it in */
560 chp = property_value;
561 for (range_nr = 0; range_nr < nr_ranges; range_nr++) {
562 chp = parse_address(current, current,
563 chp, &ranges[range_nr].child_address);
564 chp = parse_address(current, device_parent(current),
565 chp, &ranges[range_nr].parent_address);
566 chp = parse_size(current, current,
567 chp, &ranges[range_nr].size);
568 }
569
570 /* create it */
571 device_add_range_array_property(current, property_name, ranges, nr_ranges);
572
573 free(ranges);
574 }
575
576
577 /* <integer> ... */
578
579 STATIC_INLINE_TREE\
580 (void)
581 parse_integer_property(device *current,
582 const char *property_name,
583 const char *property_value)
584 {
585 int nr_entries;
586 unsigned_cell words[1024];
587 /* integer or integer array? */
588 nr_entries = 0;
589 while (1) {
590 char *end;
591 words[nr_entries] = strtoul(property_value, &end, 0);
592 if (property_value == end)
593 break;
594 nr_entries += 1;
595 if (nr_entries * sizeof(words[0]) >= sizeof(words))
596 device_error(current, "buffer overflow");
597 property_value = end;
598 }
599 if (nr_entries == 0)
600 device_error(current, "error parsing integer property %s (%s)",
601 property_name, property_value);
602 else if (nr_entries == 1)
603 device_add_integer_property(current, property_name, words[0]);
604 else {
605 int i;
606 for (i = 0; i < nr_entries; i++) {
607 H2BE(words[i]);
608 }
609 /* perhaps integer array property is better */
610 device_add_array_property(current, property_name, words,
611 sizeof(words[0]) * nr_entries);
612 }
613 }
614
615 /* PROPERTY_VALUE is a raw property value. Quote it as required by
616 parse_string_property. It is the caller's responsibility to free
617 the memory returned. */
618
619 EXTERN_TREE\
620 (char *)
621 tree_quote_property(const char *property_value)
622 {
623 char *p;
624 char *ret;
625 const char *chp;
626 int quotees;
627
628 /* Count characters needing quotes in PROPERTY_VALUE. */
629 quotees = 0;
630 for (chp = property_value; *chp; ++chp)
631 if (*chp == '\\' || *chp == '"')
632 ++quotees;
633
634 ret = (char *) xmalloc (strlen (property_value)
635 + 2 /* quotes */
636 + quotees
637 + 1 /* terminator */);
638
639 p = ret;
640 /* Add the opening quote. */
641 *p++ = '"';
642 /* Copy the value. */
643 for (chp = property_value; *chp; ++chp)
644 if (*chp == '\\' || *chp == '"')
645 {
646 /* Quote this character. */
647 *p++ = '\\';
648 *p++ = *chp;
649 }
650 else
651 *p++ = *chp;
652 /* Add the closing quote. */
653 *p++ = '"';
654 /* Terminate the string. */
655 *p++ = '\0';
656
657 return ret;
658 }
659
660 /* <string> ... */
661
662 STATIC_INLINE_TREE\
663 (void)
664 parse_string_property(device *current,
665 const char *property_name,
666 const char *property_value)
667 {
668 char **strings;
669 const char *chp;
670 int nr_strings;
671 int approx_nr_strings;
672
673 /* get an estimate as to the number of strings by counting double
674 quotes */
675 approx_nr_strings = 2;
676 for (chp = property_value; *chp; chp++) {
677 if (*chp == '"')
678 approx_nr_strings++;
679 }
680 approx_nr_strings = (approx_nr_strings) / 2;
681
682 /* create a string buffer for that many (plus a null) */
683 strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
684
685 /* now find all the strings */
686 chp = property_value;
687 nr_strings = 0;
688 while (1) {
689
690 /* skip leading space */
691 while (*chp != '\0' && isspace(*chp))
692 chp += 1;
693 if (*chp == '\0')
694 break;
695
696 /* copy it in */
697 if (*chp == '"') {
698 /* a quoted string - watch for '\' et.al. */
699 /* estimate the size and allocate space for it */
700 int pos;
701 chp++;
702 pos = 0;
703 while (chp[pos] != '\0' && chp[pos] != '"') {
704 if (chp[pos] == '\\' && chp[pos+1] != '\0')
705 pos += 2;
706 else
707 pos += 1;
708 }
709 strings[nr_strings] = zalloc(pos + 1);
710 /* copy the string over */
711 pos = 0;
712 while (*chp != '\0' && *chp != '"') {
713 if (*chp == '\\' && *(chp+1) != '\0') {
714 strings[nr_strings][pos] = *(chp+1);
715 chp += 2;
716 pos++;
717 }
718 else {
719 strings[nr_strings][pos] = *chp;
720 chp += 1;
721 pos++;
722 }
723 }
724 if (*chp != '\0')
725 chp++;
726 strings[nr_strings][pos] = '\0';
727 }
728 else {
729 /* copy over a single unquoted token */
730 int len = 0;
731 while (chp[len] != '\0' && !isspace(chp[len]))
732 len++;
733 strings[nr_strings] = zalloc(len + 1);
734 strncpy(strings[nr_strings], chp, len);
735 strings[nr_strings][len] = '\0';
736 chp += len;
737 }
738 nr_strings++;
739 if (nr_strings > approx_nr_strings)
740 device_error(current, "String property %s badly formatted",
741 property_name);
742 }
743 ASSERT(strings[nr_strings] == NULL); /* from zalloc */
744
745 /* install it */
746 if (nr_strings == 0)
747 device_add_string_property(current, property_name, "");
748 else if (nr_strings == 1)
749 device_add_string_property(current, property_name, strings[0]);
750 else {
751 const char **specs = (const char**)strings; /* stop a bogus error */
752 device_add_string_array_property(current, property_name,
753 specs, nr_strings);
754 }
755
756 /* flush the created string */
757 while (nr_strings > 0) {
758 nr_strings--;
759 free(strings[nr_strings]);
760 }
761 free(strings);
762 }
763
764
765 /* <path-to-ihandle-device> */
766
767 STATIC_INLINE_TREE\
768 (void)
769 parse_ihandle_property(device *current,
770 const char *property,
771 const char *value)
772 {
773 ihandle_runtime_property_spec ihandle;
774
775 /* pass the full path */
776 ihandle.full_path = value;
777
778 /* save this ready for the ihandle create */
779 device_add_ihandle_runtime_property(current, property,
780 &ihandle);
781 }
782
783
784
785 EXTERN_TREE\
786 (device *)
787 tree_parse(device *current,
788 const char *fmt,
789 ...)
790 {
791 char device_specifier[1024];
792 name_specifier spec;
793
794 /* format the path */
795 {
796 va_list ap;
797 va_start(ap, fmt);
798 vsprintf(device_specifier, fmt, ap);
799 va_end(ap);
800 if (strlen(device_specifier) >= sizeof(device_specifier))
801 error("device_tree_add_deviced: buffer overflow\n");
802 }
803
804 /* construct the tree down to the final device */
805 current = split_fill_path(current, device_specifier, &spec);
806
807 /* is there an interrupt spec */
808 if (spec.property == NULL
809 && spec.value != NULL) {
810 char *op = split_value(&spec);
811 switch (op[0]) {
812 case '>':
813 {
814 char *my_port_name = split_value(&spec);
815 int my_port;
816 char *dest_port_name = split_value(&spec);
817 int dest_port;
818 name_specifier dest_spec;
819 char *dest_device_name = split_value(&spec);
820 device *dest;
821 /* find my name */
822 my_port = device_interrupt_decode(current, my_port_name,
823 output_port);
824 /* find the dest device and port */
825 dest = split_fill_path(current, dest_device_name, &dest_spec);
826 dest_port = device_interrupt_decode(dest, dest_port_name,
827 input_port);
828 /* connect the two */
829 device_interrupt_attach(current,
830 my_port,
831 dest,
832 dest_port,
833 permanent_object);
834 }
835 break;
836 default:
837 device_error(current, "unreconised interrupt spec %s\n", spec.value);
838 break;
839 }
840 }
841
842 /* is there a property */
843 if (spec.property != NULL) {
844 if (strcmp(spec.value, "true") == 0)
845 device_add_boolean_property(current, spec.property, 1);
846 else if (strcmp(spec.value, "false") == 0)
847 device_add_boolean_property(current, spec.property, 0);
848 else {
849 const device_property *property;
850 switch (spec.value[0]) {
851 case '*':
852 parse_ihandle_property(current, spec.property, spec.value + 1);
853 break;
854 case '[':
855 {
856 uint8_t words[1024];
857 char *curr = spec.value + 1;
858 int nr_words = 0;
859 while (1) {
860 char *next;
861 words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
862 if (curr == next)
863 break;
864 curr = next;
865 nr_words += 1;
866 }
867 device_add_array_property(current, spec.property,
868 words, sizeof(words[0]) * nr_words);
869 }
870 break;
871 case '"':
872 parse_string_property(current, spec.property, spec.value);
873 break;
874 case '!':
875 spec.value++;
876 property = tree_find_property(current, spec.value);
877 if (property == NULL)
878 device_error(current, "property %s not found\n", spec.value);
879 device_add_duplicate_property(current,
880 spec.property,
881 property);
882 break;
883 default:
884 if (strcmp(spec.property, "reg") == 0
885 || strcmp(spec.property, "assigned-addresses") == 0
886 || strcmp(spec.property, "alternate-reg") == 0){
887 parse_reg_property(current, spec.property, spec.value);
888 }
889 else if (strcmp(spec.property, "ranges") == 0) {
890 parse_ranges_property(current, spec.property, spec.value);
891 }
892 else if (isdigit(spec.value[0])
893 || (spec.value[0] == '-' && isdigit(spec.value[1]))
894 || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
895 parse_integer_property(current, spec.property, spec.value);
896 }
897 else
898 parse_string_property(current, spec.property, spec.value);
899 break;
900 }
901 }
902 }
903 return current;
904 }
905
906
907 INLINE_TREE\
908 (void)
909 tree_traverse(device *root,
910 tree_traverse_function *prefix,
911 tree_traverse_function *postfix,
912 void *data)
913 {
914 device *child;
915 if (prefix != NULL)
916 prefix(root, data);
917 for (child = device_child(root);
918 child != NULL;
919 child = device_sibling(child)) {
920 tree_traverse(child, prefix, postfix, data);
921 }
922 if (postfix != NULL)
923 postfix(root, data);
924 }
925
926
927 STATIC_INLINE_TREE\
928 (void)
929 print_address(device *bus,
930 const device_unit *phys)
931 {
932 char unit[32];
933 device_encode_unit(bus, phys, unit, sizeof(unit));
934 printf_filtered(" %s", unit);
935 }
936
937 STATIC_INLINE_TREE\
938 (void)
939 print_size(device *bus,
940 const device_unit *size)
941 {
942 int i;
943 for (i = 0; i < size->nr_cells; i++)
944 if (size->cells[i] != 0)
945 break;
946 if (i < size->nr_cells) {
947 printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
948 i++;
949 for (; i < size->nr_cells; i++)
950 printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
951 }
952 else
953 printf_filtered(" 0");
954 }
955
956 STATIC_INLINE_TREE\
957 (void)
958 print_reg_property(device *me,
959 const device_property *property)
960 {
961 int reg_nr;
962 reg_property_spec reg;
963 for (reg_nr = 0;
964 device_find_reg_array_property(me, property->name, reg_nr, &reg);
965 reg_nr++) {
966 print_address(device_parent(me), &reg.address);
967 print_size(me, &reg.size);
968 }
969 }
970
971 STATIC_INLINE_TREE\
972 (void)
973 print_ranges_property(device *me,
974 const device_property *property)
975 {
976 int range_nr;
977 range_property_spec range;
978 for (range_nr = 0;
979 device_find_range_array_property(me, property->name, range_nr, &range);
980 range_nr++) {
981 print_address(me, &range.child_address);
982 print_address(device_parent(me), &range.parent_address);
983 print_size(me, &range.size);
984 }
985 }
986
987 STATIC_INLINE_TREE\
988 (void)
989 print_string(const char *string)
990 {
991 printf_filtered(" \"");
992 while (*string != '\0') {
993 switch (*string) {
994 case '"':
995 printf_filtered("\\\"");
996 break;
997 case '\\':
998 printf_filtered("\\\\");
999 break;
1000 default:
1001 printf_filtered("%c", *string);
1002 break;
1003 }
1004 string++;
1005 }
1006 printf_filtered("\"");
1007 }
1008
1009 STATIC_INLINE_TREE\
1010 (void)
1011 print_string_array_property(device *me,
1012 const device_property *property)
1013 {
1014 int nr;
1015 string_property_spec string;
1016 for (nr = 0;
1017 device_find_string_array_property(me, property->name, nr, &string);
1018 nr++) {
1019 print_string(string);
1020 }
1021 }
1022
1023 STATIC_INLINE_TREE\
1024 (void)
1025 print_properties(device *me)
1026 {
1027 const device_property *property;
1028 for (property = device_find_property(me, NULL);
1029 property != NULL;
1030 property = device_next_property(property)) {
1031 printf_filtered("%s/%s", device_path(me), property->name);
1032 if (property->original != NULL) {
1033 printf_filtered(" !");
1034 printf_filtered("%s/%s",
1035 device_path(property->original->owner),
1036 property->original->name);
1037 }
1038 else {
1039 switch (property->type) {
1040 case array_property:
1041 if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
1042 unsigned_cell *w = (unsigned_cell*)property->array;
1043 int cell_nr;
1044 for (cell_nr = 0;
1045 cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
1046 cell_nr++) {
1047 printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
1048 }
1049 }
1050 else {
1051 uint8_t *w = (uint8_t*)property->array;
1052 printf_filtered(" [");
1053 while ((char*)w - (char*)property->array < property->sizeof_array) {
1054 printf_filtered(" 0x%2x", BE2H_1(*w));
1055 w++;
1056 }
1057 }
1058 break;
1059 case boolean_property:
1060 {
1061 int b = device_find_boolean_property(me, property->name);
1062 printf_filtered(" %s", b ? "true" : "false");
1063 }
1064 break;
1065 case ihandle_property:
1066 {
1067 if (property->array != NULL) {
1068 device_instance *instance = device_find_ihandle_property(me, property->name);
1069 printf_filtered(" *%s", device_instance_path(instance));
1070 }
1071 else {
1072 /* not yet initialized, ask the device for the path */
1073 ihandle_runtime_property_spec spec;
1074 device_find_ihandle_runtime_property(me, property->name, &spec);
1075 printf_filtered(" *%s", spec.full_path);
1076 }
1077 }
1078 break;
1079 case integer_property:
1080 {
1081 unsigned_word w = device_find_integer_property(me, property->name);
1082 printf_filtered(" 0x%lx", (unsigned long)w);
1083 }
1084 break;
1085 case range_array_property:
1086 print_ranges_property(me, property);
1087 break;
1088 case reg_array_property:
1089 print_reg_property(me, property);
1090 break;
1091 case string_property:
1092 {
1093 const char *s = device_find_string_property(me, property->name);
1094 print_string(s);
1095 }
1096 break;
1097 case string_array_property:
1098 print_string_array_property(me, property);
1099 break;
1100 }
1101 }
1102 printf_filtered("\n");
1103 }
1104 }
1105
1106 STATIC_INLINE_TREE\
1107 (void)
1108 print_interrupts(device *me,
1109 int my_port,
1110 device *dest,
1111 int dest_port,
1112 void *ignore_or_null)
1113 {
1114 char src[32];
1115 char dst[32];
1116 device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
1117 device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
1118 printf_filtered("%s > %s %s %s\n",
1119 device_path(me),
1120 src, dst,
1121 device_path(dest));
1122 }
1123
1124 STATIC_INLINE_TREE\
1125 (void)
1126 print_device(device *me,
1127 void *ignore_or_null)
1128 {
1129 printf_filtered("%s\n", device_path(me));
1130 print_properties(me);
1131 device_interrupt_traverse(me, print_interrupts, NULL);
1132 }
1133
1134 INLINE_TREE\
1135 (void)
1136 tree_print(device *root)
1137 {
1138 tree_traverse(root,
1139 print_device, NULL,
1140 NULL);
1141 }
1142
1143
1144 INLINE_TREE\
1145 (void)
1146 tree_usage(int verbose)
1147 {
1148 if (verbose == 1) {
1149 printf_filtered("\n");
1150 printf_filtered("A device/property specifier has the form:\n");
1151 printf_filtered("\n");
1152 printf_filtered(" /path/to/a/device [ property-value ]\n");
1153 printf_filtered("\n");
1154 printf_filtered("and a possible device is\n");
1155 printf_filtered("\n");
1156 }
1157 if (verbose > 1) {
1158 printf_filtered("\n");
1159 printf_filtered("A device/property specifier (<spec>) has the format:\n");
1160 printf_filtered("\n");
1161 printf_filtered(" <spec> ::= <path> [ <value> ] ;\n");
1162 printf_filtered(" <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
1163 printf_filtered(" <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
1164 printf_filtered(" <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
1165 printf_filtered(" <unit> ::= <number> { \",\" <number> } ;\n");
1166 printf_filtered("\n");
1167 printf_filtered("Where:\n");
1168 printf_filtered("\n");
1169 printf_filtered(" <name> is the name of a device (list below)\n");
1170 printf_filtered(" <unit> is the unit-address relative to the parent bus\n");
1171 printf_filtered(" <args> additional arguments used when creating the device\n");
1172 printf_filtered(" <value> ::= ( <number> # integer property\n");
1173 printf_filtered(" | \"[\" { <number> } # array property (byte)\n");
1174 printf_filtered(" | \"{\" { <number> } # array property (cell)\n");
1175 printf_filtered(" | [ \"true\" | \"false\" ] # boolean property\n");
1176 printf_filtered(" | \"*\" <path> # ihandle property\n");
1177 printf_filtered(" | \"!\" <path> # copy property\n");
1178 printf_filtered(" | \">\" [ <number> ] <path> # attach interrupt\n");
1179 printf_filtered(" | \"<\" <path> # attach child interrupt\n");
1180 printf_filtered(" | \"\\\"\" <text> # string property\n");
1181 printf_filtered(" | <text> # string property\n");
1182 printf_filtered(" ) ;\n");
1183 printf_filtered("\n");
1184 printf_filtered("And the following are valid device names:\n");
1185 printf_filtered("\n");
1186 }
1187 }
1188
1189
1190
1191 INLINE_TREE\
1192 (device_instance *)
1193 tree_instance(device *root,
1194 const char *device_specifier)
1195 {
1196 /* find the device node */
1197 device *me;
1198 name_specifier spec;
1199 if (!split_device_specifier(root, device_specifier, &spec))
1200 return NULL;
1201 me = split_find_device(root, &spec);
1202 if (spec.name != NULL)
1203 return NULL;
1204 /* create the instance */
1205 return device_create_instance(me, device_specifier, spec.last_args);
1206 }
1207
1208
1209 INLINE_TREE\
1210 (device *)
1211 tree_find_device(device *root,
1212 const char *path_to_device)
1213 {
1214 device *node;
1215 name_specifier spec;
1216
1217 /* parse the path */
1218 split_device_specifier(root, path_to_device, &spec);
1219 if (spec.value != NULL)
1220 return NULL; /* something wierd */
1221
1222 /* now find it */
1223 node = split_find_device(root, &spec);
1224 if (spec.name != NULL)
1225 return NULL; /* not a leaf */
1226
1227 return node;
1228 }
1229
1230
1231 INLINE_TREE\
1232 (const device_property *)
1233 tree_find_property(device *root,
1234 const char *path_to_property)
1235 {
1236 name_specifier spec;
1237 if (!split_property_specifier(root, path_to_property, &spec))
1238 device_error(root, "Invalid property path %s", path_to_property);
1239 root = split_find_device(root, &spec);
1240 return device_find_property(root, spec.property);
1241 }
1242
1243 INLINE_TREE\
1244 (int)
1245 tree_find_boolean_property(device *root,
1246 const char *path_to_property)
1247 {
1248 name_specifier spec;
1249 if (!split_property_specifier(root, path_to_property, &spec))
1250 device_error(root, "Invalid property path %s", path_to_property);
1251 root = split_find_device(root, &spec);
1252 return device_find_boolean_property(root, spec.property);
1253 }
1254
1255 INLINE_TREE\
1256 (signed_cell)
1257 tree_find_integer_property(device *root,
1258 const char *path_to_property)
1259 {
1260 name_specifier spec;
1261 if (!split_property_specifier(root, path_to_property, &spec))
1262 device_error(root, "Invalid property path %s", path_to_property);
1263 root = split_find_device(root, &spec);
1264 return device_find_integer_property(root, spec.property);
1265 }
1266
1267 INLINE_TREE\
1268 (device_instance *)
1269 tree_find_ihandle_property(device *root,
1270 const char *path_to_property)
1271 {
1272 name_specifier spec;
1273 if (!split_property_specifier(root, path_to_property, &spec))
1274 device_error(root, "Invalid property path %s", path_to_property);
1275 root = split_find_device(root, &spec);
1276 return device_find_ihandle_property(root, spec.property);
1277 }
1278
1279 INLINE_TREE\
1280 (const char *)
1281 tree_find_string_property(device *root,
1282 const char *path_to_property)
1283 {
1284 name_specifier spec;
1285 if (!split_property_specifier(root, path_to_property, &spec))
1286 device_error(root, "Invalid property path %s", path_to_property);
1287 root = split_find_device(root, &spec);
1288 return device_find_string_property(root, spec.property);
1289 }
1290
1291
1292 #endif /* _PARSE_C_ */