David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file does the necessary interface mapping between the bootwrapper |
| 3 | * device tree operations and the interface provided by shared source |
| 4 | * files flatdevicetree.[ch]. |
| 5 | * |
| 6 | * Copyright 2007 David Gibson, IBM Corporation. |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | #include <stdio.h> |
| 26 | #include <page.h> |
| 27 | #include <libfdt.h> |
| 28 | #include "ops.h" |
| 29 | |
| 30 | #define DEBUG 0 |
| 31 | #define BAD_ERROR(err) (((err) < 0) \ |
| 32 | && ((err) != -FDT_ERR_NOTFOUND) \ |
| 33 | && ((err) != -FDT_ERR_EXISTS)) |
| 34 | |
| 35 | #define check_err(err) \ |
| 36 | ({ \ |
| 37 | if (BAD_ERROR(err) || ((err < 0) && DEBUG)) \ |
Harvey Harrison | e48b1b4 | 2008-03-29 08:21:07 +1100 | [diff] [blame] | 38 | printf("%s():%d %s\n\r", __func__, __LINE__, \ |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 39 | fdt_strerror(err)); \ |
| 40 | if (BAD_ERROR(err)) \ |
| 41 | exit(); \ |
| 42 | (err < 0) ? -1 : 0; \ |
| 43 | }) |
| 44 | |
| 45 | #define offset_devp(off) \ |
| 46 | ({ \ |
Scott Wood | 754e5f3 | 2007-12-12 08:23:04 +1100 | [diff] [blame] | 47 | int _offset = (off); \ |
| 48 | check_err(_offset) ? NULL : (void *)(_offset+1); \ |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 49 | }) |
| 50 | |
Scott Wood | 2a9d2d9 | 2007-12-12 08:23:05 +1100 | [diff] [blame] | 51 | #define devp_offset_find(devp) (((int)(devp))-1) |
| 52 | #define devp_offset(devp) (devp ? ((int)(devp))-1 : 0) |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 53 | |
| 54 | static void *fdt; |
| 55 | static void *buf; /* = NULL */ |
| 56 | |
| 57 | #define EXPAND_GRANULARITY 1024 |
| 58 | |
| 59 | static void expand_buf(int minexpand) |
| 60 | { |
| 61 | int size = fdt_totalsize(fdt); |
| 62 | int rc; |
| 63 | |
| 64 | size = _ALIGN(size + minexpand, EXPAND_GRANULARITY); |
| 65 | buf = platform_ops.realloc(buf, size); |
| 66 | if (!buf) |
| 67 | fatal("Couldn't find %d bytes to expand device tree\n\r", size); |
| 68 | rc = fdt_open_into(fdt, buf, size); |
| 69 | if (rc != 0) |
| 70 | fatal("Couldn't expand fdt into new buffer: %s\n\r", |
| 71 | fdt_strerror(rc)); |
| 72 | |
| 73 | fdt = buf; |
| 74 | } |
| 75 | |
| 76 | static void *fdt_wrapper_finddevice(const char *path) |
| 77 | { |
| 78 | return offset_devp(fdt_path_offset(fdt, path)); |
| 79 | } |
| 80 | |
| 81 | static int fdt_wrapper_getprop(const void *devp, const char *name, |
| 82 | void *buf, const int buflen) |
| 83 | { |
| 84 | const void *p; |
| 85 | int len; |
| 86 | |
| 87 | p = fdt_getprop(fdt, devp_offset(devp), name, &len); |
| 88 | if (!p) |
| 89 | return check_err(len); |
| 90 | memcpy(buf, p, min(len, buflen)); |
| 91 | return len; |
| 92 | } |
| 93 | |
| 94 | static int fdt_wrapper_setprop(const void *devp, const char *name, |
| 95 | const void *buf, const int len) |
| 96 | { |
| 97 | int rc; |
| 98 | |
| 99 | rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len); |
| 100 | if (rc == -FDT_ERR_NOSPACE) { |
| 101 | expand_buf(len + 16); |
| 102 | rc = fdt_setprop(fdt, devp_offset(devp), name, buf, len); |
| 103 | } |
| 104 | |
| 105 | return check_err(rc); |
| 106 | } |
| 107 | |
Mike Ditto | 71773f0 | 2008-10-21 11:32:29 +0000 | [diff] [blame^] | 108 | static int fdt_wrapper_del_node(const void *devp) |
| 109 | { |
| 110 | return fdt_del_node(fdt, devp_offset(devp)); |
| 111 | } |
| 112 | |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 113 | static void *fdt_wrapper_get_parent(const void *devp) |
| 114 | { |
| 115 | return offset_devp(fdt_parent_offset(fdt, devp_offset(devp))); |
| 116 | } |
| 117 | |
| 118 | static void *fdt_wrapper_create_node(const void *devp, const char *name) |
| 119 | { |
| 120 | int offset; |
| 121 | |
| 122 | offset = fdt_add_subnode(fdt, devp_offset(devp), name); |
| 123 | if (offset == -FDT_ERR_NOSPACE) { |
| 124 | expand_buf(strlen(name) + 16); |
| 125 | offset = fdt_add_subnode(fdt, devp_offset(devp), name); |
| 126 | } |
| 127 | |
| 128 | return offset_devp(offset); |
| 129 | } |
| 130 | |
| 131 | static void *fdt_wrapper_find_node_by_prop_value(const void *prev, |
| 132 | const char *name, |
| 133 | const char *val, |
| 134 | int len) |
| 135 | { |
Scott Wood | 2a9d2d9 | 2007-12-12 08:23:05 +1100 | [diff] [blame] | 136 | int offset = fdt_node_offset_by_prop_value(fdt, devp_offset_find(prev), |
| 137 | name, val, len); |
| 138 | return offset_devp(offset); |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 139 | } |
| 140 | |
Kumar Gala | b3bea15 | 2008-01-24 22:41:05 -0600 | [diff] [blame] | 141 | static void *fdt_wrapper_find_node_by_compatible(const void *prev, |
| 142 | const char *val) |
| 143 | { |
| 144 | int offset = fdt_node_offset_by_compatible(fdt, devp_offset_find(prev), |
| 145 | val); |
| 146 | return offset_devp(offset); |
| 147 | } |
| 148 | |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 149 | static char *fdt_wrapper_get_path(const void *devp, char *buf, int len) |
| 150 | { |
| 151 | int rc; |
| 152 | |
| 153 | rc = fdt_get_path(fdt, devp_offset(devp), buf, len); |
| 154 | if (check_err(rc)) |
| 155 | return NULL; |
| 156 | return buf; |
| 157 | } |
| 158 | |
| 159 | static unsigned long fdt_wrapper_finalize(void) |
| 160 | { |
| 161 | int rc; |
| 162 | |
| 163 | rc = fdt_pack(fdt); |
| 164 | if (rc != 0) |
| 165 | fatal("Couldn't pack flat tree: %s\n\r", |
| 166 | fdt_strerror(rc)); |
| 167 | return (unsigned long)fdt; |
| 168 | } |
| 169 | |
| 170 | void fdt_init(void *blob) |
| 171 | { |
| 172 | int err; |
Sebastian Siewior | c10c178 | 2008-10-12 23:15:26 +0000 | [diff] [blame] | 173 | int bufsize; |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 174 | |
| 175 | dt_ops.finddevice = fdt_wrapper_finddevice; |
| 176 | dt_ops.getprop = fdt_wrapper_getprop; |
| 177 | dt_ops.setprop = fdt_wrapper_setprop; |
| 178 | dt_ops.get_parent = fdt_wrapper_get_parent; |
| 179 | dt_ops.create_node = fdt_wrapper_create_node; |
| 180 | dt_ops.find_node_by_prop_value = fdt_wrapper_find_node_by_prop_value; |
Kumar Gala | b3bea15 | 2008-01-24 22:41:05 -0600 | [diff] [blame] | 181 | dt_ops.find_node_by_compatible = fdt_wrapper_find_node_by_compatible; |
Mike Ditto | 71773f0 | 2008-10-21 11:32:29 +0000 | [diff] [blame^] | 182 | dt_ops.del_node = fdt_wrapper_del_node; |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 183 | dt_ops.get_path = fdt_wrapper_get_path; |
| 184 | dt_ops.finalize = fdt_wrapper_finalize; |
| 185 | |
| 186 | /* Make sure the dt blob is the right version and so forth */ |
| 187 | fdt = blob; |
Sebastian Siewior | c10c178 | 2008-10-12 23:15:26 +0000 | [diff] [blame] | 188 | bufsize = fdt_totalsize(fdt) + 4; |
| 189 | buf = malloc(bufsize); |
| 190 | if(!buf) |
| 191 | fatal("malloc failed. can't relocate the device tree\n\r"); |
| 192 | |
| 193 | err = fdt_open_into(fdt, buf, bufsize); |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 194 | |
| 195 | if (err != 0) |
| 196 | fatal("fdt_init(): %s\n\r", fdt_strerror(err)); |
| 197 | |
Sebastian Siewior | c10c178 | 2008-10-12 23:15:26 +0000 | [diff] [blame] | 198 | fdt = buf; |
David Gibson | 2f0dfea | 2007-12-10 14:28:39 +1100 | [diff] [blame] | 199 | } |