2005-06-08 09:18:34 +02:00
|
|
|
/*
|
|
|
|
* (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
|
|
|
|
*
|
2007-09-18 03:44:04 +02:00
|
|
|
*
|
2005-06-08 09:18:34 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
2007-09-18 03:44:04 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
2005-06-08 09:18:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dtc.h"
|
2007-03-23 21:18:41 +01:00
|
|
|
#include "srcpos.h"
|
2005-06-08 09:18:34 +02:00
|
|
|
|
|
|
|
extern FILE *yyin;
|
|
|
|
extern int yyparse(void);
|
2005-07-15 09:14:24 +02:00
|
|
|
extern void yyerror(char const *);
|
|
|
|
|
|
|
|
struct boot_info *the_boot_info;
|
|
|
|
|
2007-03-23 21:18:41 +01:00
|
|
|
struct boot_info *dt_from_source(const char *fname)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
2005-07-15 09:14:24 +02:00
|
|
|
the_boot_info = NULL;
|
|
|
|
|
2007-03-23 21:18:41 +01:00
|
|
|
push_input_file(fname);
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
if (yyparse() != 0)
|
|
|
|
return NULL;
|
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
fill_fullpaths(the_boot_info->dt, "");
|
2005-06-08 09:18:34 +02:00
|
|
|
|
2005-07-15 09:14:24 +02:00
|
|
|
return the_boot_info;
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_prefix(FILE *f, int level)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < level; i++)
|
|
|
|
fputc('\t', f);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum proptype {
|
|
|
|
PROP_EMPTY,
|
|
|
|
PROP_STRING,
|
|
|
|
PROP_CELLS,
|
|
|
|
PROP_BYTES,
|
|
|
|
};
|
|
|
|
|
2007-10-17 04:39:10 +02:00
|
|
|
int isstring(char c)
|
|
|
|
{
|
|
|
|
return (isprint(c)
|
|
|
|
|| (c == '\0')
|
|
|
|
|| strchr("\a\b\t\n\v\f\r", c));
|
|
|
|
}
|
|
|
|
|
2005-06-08 09:18:34 +02:00
|
|
|
static enum proptype guess_type(struct property *prop)
|
|
|
|
{
|
|
|
|
int len = prop->val.len;
|
|
|
|
char *p = prop->val.val;
|
2007-10-17 04:39:10 +02:00
|
|
|
int nnotstring = 0, nnul = 0;
|
2005-06-08 09:18:34 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return PROP_EMPTY;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2007-10-17 04:39:10 +02:00
|
|
|
if (! isstring(p[i]))
|
|
|
|
nnotstring++;
|
|
|
|
if (p[i] == '\0')
|
|
|
|
nnul++;
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
|
2007-10-17 04:39:10 +02:00
|
|
|
if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul)))
|
2005-06-08 09:18:34 +02:00
|
|
|
return PROP_STRING;
|
|
|
|
else if ((len % sizeof(cell_t)) == 0)
|
|
|
|
return PROP_CELLS;
|
|
|
|
else
|
|
|
|
return PROP_BYTES;
|
|
|
|
}
|
|
|
|
|
2007-10-17 04:39:10 +02:00
|
|
|
static void write_propval_string(FILE *f, struct data val)
|
|
|
|
{
|
|
|
|
char *str = val.val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(str[val.len-1] == '\0');
|
|
|
|
|
|
|
|
fprintf(f, " = \"");
|
|
|
|
for (i = 0; i < (val.len-1); i++) {
|
|
|
|
char c = str[i];
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\a':
|
|
|
|
fprintf(f, "\\a");
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
fprintf(f, "\\b");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
fprintf(f, "\\t");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
fprintf(f, "\\n");
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
fprintf(f, "\\v");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
fprintf(f, "\\f");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
fprintf(f, "\\r");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
fprintf(f, "\\\\");
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
fprintf(f, "\\\"");
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
fprintf(f, "\", \"");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isprint(c))
|
|
|
|
fprintf(f, "%c", c);
|
|
|
|
else
|
|
|
|
fprintf(f, "\\x%02hhx", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(f, "\";\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_propval_cells(FILE *f, struct data val)
|
|
|
|
{
|
|
|
|
void *propend = val.val + val.len;
|
|
|
|
cell_t *cp = (cell_t *)val.val;
|
|
|
|
|
|
|
|
fprintf(f, " = <");
|
|
|
|
for (;;) {
|
|
|
|
fprintf(f, "%x", be32_to_cpu(*cp++));
|
|
|
|
if ((void *)cp >= propend)
|
|
|
|
break;
|
|
|
|
fprintf(f, " ");
|
|
|
|
}
|
|
|
|
fprintf(f, ">;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_propval_bytes(FILE *f, struct data val)
|
|
|
|
{
|
|
|
|
void *propend = val.val + val.len;
|
|
|
|
char *bp = val.val;
|
|
|
|
|
|
|
|
fprintf(f, " = [");
|
|
|
|
for (;;) {
|
|
|
|
fprintf(f, "%02hhx", *bp++);
|
|
|
|
if ((void *)bp >= propend)
|
|
|
|
break;
|
|
|
|
fprintf(f, " ");
|
|
|
|
}
|
|
|
|
fprintf(f, "];\n");
|
|
|
|
}
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-08-29 04:48:02 +02:00
|
|
|
static void write_tree_source_node(FILE *f, struct node *tree, int level)
|
2005-06-08 09:18:34 +02:00
|
|
|
{
|
|
|
|
struct property *prop;
|
|
|
|
struct node *child;
|
|
|
|
|
|
|
|
write_prefix(f, level);
|
2007-11-02 06:10:30 +01:00
|
|
|
if (tree->label)
|
|
|
|
fprintf(f, "%s: ", tree->label);
|
2005-06-08 09:18:34 +02:00
|
|
|
if (tree->name && (*tree->name))
|
|
|
|
fprintf(f, "%s {\n", tree->name);
|
|
|
|
else
|
|
|
|
fprintf(f, "/ {\n");
|
|
|
|
|
|
|
|
for_each_property(tree, prop) {
|
|
|
|
enum proptype type;
|
|
|
|
|
2007-11-02 06:10:30 +01:00
|
|
|
write_prefix(f, level+1);
|
|
|
|
if (prop->label)
|
|
|
|
fprintf(f, "%s: ", prop->label);
|
|
|
|
fprintf(f, "%s", prop->name);
|
2005-06-08 09:18:34 +02:00
|
|
|
type = guess_type(prop);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case PROP_EMPTY:
|
|
|
|
fprintf(f, ";\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_STRING:
|
2007-10-17 04:39:10 +02:00
|
|
|
write_propval_string(f, prop->val);
|
2005-06-08 09:18:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CELLS:
|
2007-10-17 04:39:10 +02:00
|
|
|
write_propval_cells(f, prop->val);
|
2005-06-08 09:18:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_BYTES:
|
2007-10-17 04:39:10 +02:00
|
|
|
write_propval_bytes(f, prop->val);
|
2005-06-08 09:18:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for_each_child(tree, child) {
|
|
|
|
fprintf(f, "\n");
|
2005-07-15 09:14:24 +02:00
|
|
|
write_tree_source_node(f, child, level+1);
|
2005-06-08 09:18:34 +02:00
|
|
|
}
|
|
|
|
write_prefix(f, level);
|
|
|
|
fprintf(f, "};\n");
|
|
|
|
}
|
2005-07-15 09:14:24 +02:00
|
|
|
|
|
|
|
|
2005-10-26 08:56:26 +02:00
|
|
|
void dt_to_source(FILE *f, struct boot_info *bi)
|
2005-07-15 09:14:24 +02:00
|
|
|
{
|
2005-10-24 10:18:38 +02:00
|
|
|
struct reserve_info *re;
|
2005-07-15 09:14:24 +02:00
|
|
|
|
2005-10-24 10:18:38 +02:00
|
|
|
for (re = bi->reservelist; re; re = re->next) {
|
2007-11-02 06:10:30 +01:00
|
|
|
if (re->label)
|
|
|
|
fprintf(f, "%s: ", re->label);
|
2005-07-15 09:14:24 +02:00
|
|
|
fprintf(f, "/memreserve/\t%016llx-%016llx;\n",
|
2005-10-24 10:18:38 +02:00
|
|
|
(unsigned long long)re->re.address,
|
|
|
|
(unsigned long long)(re->re.address + re->re.size - 1));
|
2005-07-15 09:14:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
write_tree_source_node(f, bi->dt, 0);
|
|
|
|
}
|
|
|
|
|