blob: a04c173dc02704e3e12ceea61ebfd9433e11ee85 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * 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.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
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
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
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;
David Gibsonfc14dad2005-06-08 17:18:34 +100080}
81
David Gibsonf0517db2005-07-15 17:14:24 +100082
David Gibson230f2532005-08-29 12:48:02 +100083static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +100084{
85 struct property *prop;
86 struct node *child;
87
88 write_prefix(f, level);
89 if (tree->name && (*tree->name))
90 fprintf(f, "%s {\n", tree->name);
91 else
92 fprintf(f, "/ {\n");
93
94 for_each_property(tree, prop) {
95 cell_t *cp;
96 char *bp;
97 void *propend;
98 enum proptype type;
99
100 write_prefix(f, level);
101 fprintf(f, "\t%s", prop->name);
102 type = guess_type(prop);
103 propend = prop->val.val + prop->val.len;
104
105 switch (type) {
106 case PROP_EMPTY:
107 fprintf(f, ";\n");
108 break;
109
110 case PROP_STRING:
111 fprintf(f, " = \"%s\";\n", (char *)prop->val.val);
112 break;
113
114 case PROP_CELLS:
115 fprintf(f, " = <");
116 cp = (cell_t *)prop->val.val;
117 for (;;) {
118 fprintf(f, "%x", be32_to_cpu(*cp++));
119 if ((void *)cp >= propend)
120 break;
121 fprintf(f, " ");
122 }
123 fprintf(f, ">;\n");
124 break;
125
126 case PROP_BYTES:
127 fprintf(f, " = [");
128 bp = prop->val.val;
129 for (;;) {
130 fprintf(f, "%02hhx", *bp++);
131 if ((void *)bp >= propend)
132 break;
133 fprintf(f, " ");
134 }
135 fprintf(f, "];\n");
136 break;
137 }
138 }
139 for_each_child(tree, child) {
140 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000141 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000142 }
143 write_prefix(f, level);
144 fprintf(f, "};\n");
145}
David Gibsonf0517db2005-07-15 17:14:24 +1000146
147
David Gibson712e52e2005-10-26 16:56:26 +1000148void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000149{
David Gibsonf040d952005-10-24 18:18:38 +1000150 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000151
David Gibsonf040d952005-10-24 18:18:38 +1000152 for (re = bi->reservelist; re; re = re->next) {
David Gibsonf0517db2005-07-15 17:14:24 +1000153 fprintf(f, "/memreserve/\t%016llx-%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000154 (unsigned long long)re->re.address,
155 (unsigned long long)(re->re.address + re->re.size - 1));
David Gibsonf0517db2005-07-15 17:14:24 +1000156 }
157
158 write_tree_source_node(f, bi->dt, 0);
159}
160