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 | * |
| 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" |
| 22 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 23 | extern FILE *yyin; |
| 24 | extern int yyparse(void); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 25 | extern void yyerror(char const *); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 26 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 27 | struct boot_info *the_boot_info; |
| 28 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 29 | struct boot_info *dt_from_source(FILE *f) |
| 30 | { |
| 31 | the_boot_info = NULL; |
| 32 | |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 33 | yyin = f; |
| 34 | if (yyparse() != 0) |
| 35 | return NULL; |
| 36 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 37 | fill_fullpaths(the_boot_info->dt, ""); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 38 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 39 | return the_boot_info; |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void write_prefix(FILE *f, int level) |
| 43 | { |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0; i < level; i++) |
| 47 | fputc('\t', f); |
| 48 | } |
| 49 | |
| 50 | enum proptype { |
| 51 | PROP_EMPTY, |
| 52 | PROP_STRING, |
| 53 | PROP_CELLS, |
| 54 | PROP_BYTES, |
| 55 | }; |
| 56 | |
| 57 | static enum proptype guess_type(struct property *prop) |
| 58 | { |
| 59 | int len = prop->val.len; |
| 60 | char *p = prop->val.val; |
| 61 | int nnoprint = 0; |
| 62 | int i; |
| 63 | |
| 64 | if (len == 0) |
| 65 | return PROP_EMPTY; |
| 66 | |
| 67 | for (i = 0; i < len; i++) { |
| 68 | if (! isprint(p[i])) |
| 69 | nnoprint++; |
| 70 | } |
| 71 | |
| 72 | if ((nnoprint == 1) && (p[len-1] == '\0')) |
| 73 | return PROP_STRING; |
| 74 | else if ((len % sizeof(cell_t)) == 0) |
| 75 | return PROP_CELLS; |
| 76 | else |
| 77 | return PROP_BYTES; |
| 78 | |
| 79 | } |
| 80 | |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 81 | |
David Gibson | 230f253 | 2005-08-29 12:48:02 +1000 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | struct property *prop; |
| 85 | struct node *child; |
| 86 | |
| 87 | write_prefix(f, level); |
| 88 | if (tree->name && (*tree->name)) |
| 89 | fprintf(f, "%s {\n", tree->name); |
| 90 | else |
| 91 | fprintf(f, "/ {\n"); |
| 92 | |
| 93 | for_each_property(tree, prop) { |
| 94 | cell_t *cp; |
| 95 | char *bp; |
| 96 | void *propend; |
| 97 | enum proptype type; |
| 98 | |
| 99 | write_prefix(f, level); |
| 100 | fprintf(f, "\t%s", prop->name); |
| 101 | type = guess_type(prop); |
| 102 | propend = prop->val.val + prop->val.len; |
| 103 | |
| 104 | switch (type) { |
| 105 | case PROP_EMPTY: |
| 106 | fprintf(f, ";\n"); |
| 107 | break; |
| 108 | |
| 109 | case PROP_STRING: |
| 110 | fprintf(f, " = \"%s\";\n", (char *)prop->val.val); |
| 111 | break; |
| 112 | |
| 113 | case PROP_CELLS: |
| 114 | fprintf(f, " = <"); |
| 115 | cp = (cell_t *)prop->val.val; |
| 116 | for (;;) { |
| 117 | fprintf(f, "%x", be32_to_cpu(*cp++)); |
| 118 | if ((void *)cp >= propend) |
| 119 | break; |
| 120 | fprintf(f, " "); |
| 121 | } |
| 122 | fprintf(f, ">;\n"); |
| 123 | break; |
| 124 | |
| 125 | case PROP_BYTES: |
| 126 | fprintf(f, " = ["); |
| 127 | bp = prop->val.val; |
| 128 | for (;;) { |
| 129 | fprintf(f, "%02hhx", *bp++); |
| 130 | if ((void *)bp >= propend) |
| 131 | break; |
| 132 | fprintf(f, " "); |
| 133 | } |
| 134 | fprintf(f, "];\n"); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | for_each_child(tree, child) { |
| 139 | fprintf(f, "\n"); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 140 | write_tree_source_node(f, child, level+1); |
David Gibson | fc14dad | 2005-06-08 17:18:34 +1000 | [diff] [blame] | 141 | } |
| 142 | write_prefix(f, level); |
| 143 | fprintf(f, "};\n"); |
| 144 | } |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | void write_tree_source(FILE *f, struct boot_info *bi) |
| 148 | { |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame^] | 149 | struct reserve_info *re; |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 150 | |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame^] | 151 | for (re = bi->reservelist; re; re = re->next) { |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 152 | fprintf(f, "/memreserve/\t%016llx-%016llx;\n", |
David Gibson | f040d95 | 2005-10-24 18:18:38 +1000 | [diff] [blame^] | 153 | (unsigned long long)re->re.address, |
| 154 | (unsigned long long)(re->re.address + re->re.size - 1)); |
David Gibson | f0517db | 2005-07-15 17:14:24 +1000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | write_tree_source_node(f, bi->dt, 0); |
| 158 | } |
| 159 | |