blob: c067b20cebe6f1c54d3b2b5a2b11fe2a7ad21605 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
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"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
David Gibsonfc14dad2005-06-08 17:18:34 +100024extern FILE *yyin;
25extern int yyparse(void);
David Gibsonf0517db2005-07-15 17:14:24 +100026extern void yyerror(char const *);
David Gibsonfc14dad2005-06-08 17:18:34 +100027
David Gibsonf0517db2005-07-15 17:14:24 +100028struct boot_info *the_boot_info;
29
Jon Loeligere45e6fd2007-03-23 15:18:41 -050030struct boot_info *dt_from_source(const char *fname)
David Gibsonf0517db2005-07-15 17:14:24 +100031{
32 the_boot_info = NULL;
33
Jon Loeligere45e6fd2007-03-23 15:18:41 -050034 push_input_file(fname);
35
David Gibsonfc14dad2005-06-08 17:18:34 +100036 if (yyparse() != 0)
37 return NULL;
38
David Gibsonf0517db2005-07-15 17:14:24 +100039 fill_fullpaths(the_boot_info->dt, "");
David Gibsonfc14dad2005-06-08 17:18:34 +100040
David Gibsonf0517db2005-07-15 17:14:24 +100041 return the_boot_info;
David Gibsonfc14dad2005-06-08 17:18:34 +100042}
43
44static void write_prefix(FILE *f, int level)
45{
46 int i;
47
48 for (i = 0; i < level; i++)
49 fputc('\t', f);
50}
51
52enum proptype {
53 PROP_EMPTY,
54 PROP_STRING,
55 PROP_CELLS,
56 PROP_BYTES,
57};
58
59static enum proptype guess_type(struct property *prop)
60{
61 int len = prop->val.len;
62 char *p = prop->val.val;
63 int nnoprint = 0;
64 int i;
65
66 if (len == 0)
67 return PROP_EMPTY;
68
69 for (i = 0; i < len; i++) {
70 if (! isprint(p[i]))
71 nnoprint++;
72 }
73
74 if ((nnoprint == 1) && (p[len-1] == '\0'))
75 return PROP_STRING;
76 else if ((len % sizeof(cell_t)) == 0)
77 return PROP_CELLS;
78 else
79 return PROP_BYTES;
80
81}
82
David Gibsonf0517db2005-07-15 17:14:24 +100083
David Gibson230f2532005-08-29 12:48:02 +100084static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +100085{
86 struct property *prop;
87 struct node *child;
88
89 write_prefix(f, level);
90 if (tree->name && (*tree->name))
91 fprintf(f, "%s {\n", tree->name);
92 else
93 fprintf(f, "/ {\n");
94
95 for_each_property(tree, prop) {
96 cell_t *cp;
97 char *bp;
98 void *propend;
99 enum proptype type;
100
101 write_prefix(f, level);
102 fprintf(f, "\t%s", prop->name);
103 type = guess_type(prop);
104 propend = prop->val.val + prop->val.len;
105
106 switch (type) {
107 case PROP_EMPTY:
108 fprintf(f, ";\n");
109 break;
110
111 case PROP_STRING:
112 fprintf(f, " = \"%s\";\n", (char *)prop->val.val);
113 break;
114
115 case PROP_CELLS:
116 fprintf(f, " = <");
117 cp = (cell_t *)prop->val.val;
118 for (;;) {
119 fprintf(f, "%x", be32_to_cpu(*cp++));
120 if ((void *)cp >= propend)
121 break;
122 fprintf(f, " ");
123 }
124 fprintf(f, ">;\n");
125 break;
126
127 case PROP_BYTES:
128 fprintf(f, " = [");
129 bp = prop->val.val;
130 for (;;) {
131 fprintf(f, "%02hhx", *bp++);
132 if ((void *)bp >= propend)
133 break;
134 fprintf(f, " ");
135 }
136 fprintf(f, "];\n");
137 break;
138 }
139 }
140 for_each_child(tree, child) {
141 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000142 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000143 }
144 write_prefix(f, level);
145 fprintf(f, "};\n");
146}
David Gibsonf0517db2005-07-15 17:14:24 +1000147
148
David Gibson712e52e2005-10-26 16:56:26 +1000149void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000150{
David Gibsonf040d952005-10-24 18:18:38 +1000151 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000152
David Gibsonf040d952005-10-24 18:18:38 +1000153 for (re = bi->reservelist; re; re = re->next) {
David Gibsonf0517db2005-07-15 17:14:24 +1000154 fprintf(f, "/memreserve/\t%016llx-%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000155 (unsigned long long)re->re.address,
156 (unsigned long long)(re->re.address + re->re.size - 1));
David Gibsonf0517db2005-07-15 17:14:24 +1000157 }
158
159 write_tree_source_node(f, bi->dt, 0);
160}
161