David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| 3 | * |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 4 | * |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 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. |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame^] | 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 |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "dtc.h" |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 22 | #include "srcpos.h" |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 23 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 24 | extern FILE *yyin; |
| 25 | extern int yyparse(void); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 26 | extern void yyerror(char const *); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 27 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 28 | struct boot_info *the_boot_info; |
| 29 | |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 30 | struct boot_info *dt_from_source(const char *fname) |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 31 | { |
| 32 | the_boot_info = NULL; |
| 33 | |
Jon Loeliger | e45e6fd | 2007-03-23 15:18:41 -0500 | [diff] [blame] | 34 | push_input_file(fname); |
| 35 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 36 | if (yyparse() != 0) |
| 37 | return NULL; |
| 38 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 39 | fill_fullpaths(the_boot_info->dt, ""); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 40 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 41 | return the_boot_info; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static 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 | |
| 52 | enum proptype { |
| 53 | PROP_EMPTY, |
| 54 | PROP_STRING, |
| 55 | PROP_CELLS, |
| 56 | PROP_BYTES, |
| 57 | }; |
| 58 | |
| 59 | static 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 Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 80 | } |
| 81 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 82 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 83 | static void write_tree_source_node(FILE *f, struct node *tree, int level) |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 84 | { |
| 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 Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 141 | write_tree_source_node(f, child, level+1); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 142 | } |
| 143 | write_prefix(f, level); |
| 144 | fprintf(f, "};\n"); |
| 145 | } |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 146 | |
| 147 | |
David Gibson | 712e52e | 2005-10-26 16:56:26 +1000 | [diff] [blame] | 148 | void dt_to_source(FILE *f, struct boot_info *bi) |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 149 | { |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 150 | struct reserve_info *re; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 151 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 152 | for (re = bi->reservelist; re; re = re->next) { |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 153 | fprintf(f, "/memreserve/\t%016llx-%016llx;\n", |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame] | 154 | (unsigned long long)re->re.address, |
| 155 | (unsigned long long)(re->re.address + re->re.size - 1)); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | write_tree_source_node(f, bi->dt, 0); |
| 159 | } |
| 160 | |