Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation; either version 2.1 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #include "libfdt_env.h" |
| 20 | |
| 21 | #include <fdt.h> |
| 22 | #include <libfdt.h> |
| 23 | |
| 24 | #include "libfdt_internal.h" |
| 25 | |
| 26 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, |
| 27 | const void *val, int len) |
| 28 | { |
| 29 | void *propval; |
| 30 | int proplen; |
| 31 | |
| 32 | propval = fdt_getprop(fdt, nodeoffset, name, &proplen); |
| 33 | if (! propval) |
| 34 | return proplen; |
| 35 | |
| 36 | if (proplen != len) |
| 37 | return -FDT_ERR_NOSPACE; |
| 38 | |
| 39 | memcpy(propval, val, len); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static void nop_region(void *start, int len) |
| 44 | { |
| 45 | uint32_t *p; |
| 46 | |
| 47 | for (p = start; (void *)p < (start + len); p++) |
| 48 | *p = cpu_to_fdt32(FDT_NOP); |
| 49 | } |
| 50 | |
| 51 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name) |
| 52 | { |
| 53 | struct fdt_property *prop; |
| 54 | int len; |
| 55 | |
| 56 | prop = fdt_get_property(fdt, nodeoffset, name, &len); |
| 57 | if (! prop) |
| 58 | return len; |
| 59 | |
| 60 | nop_region(prop, len + sizeof(*prop)); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int _fdt_node_end_offset(void *fdt, int nodeoffset) |
| 66 | { |
| 67 | int level = 0; |
| 68 | uint32_t tag; |
| 69 | int offset, nextoffset; |
| 70 | |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 71 | tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 72 | if (tag != FDT_BEGIN_NODE) |
| 73 | return -FDT_ERR_BADOFFSET; |
| 74 | do { |
| 75 | offset = nextoffset; |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 76 | tag = fdt_next_tag(fdt, offset, &nextoffset, NULL); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 77 | |
| 78 | switch (tag) { |
| 79 | case FDT_END: |
| 80 | return offset; |
| 81 | |
| 82 | case FDT_BEGIN_NODE: |
| 83 | level++; |
| 84 | break; |
| 85 | |
| 86 | case FDT_END_NODE: |
| 87 | level--; |
| 88 | break; |
| 89 | |
| 90 | case FDT_PROP: |
| 91 | case FDT_NOP: |
| 92 | break; |
| 93 | |
| 94 | default: |
| 95 | return -FDT_ERR_BADSTRUCTURE; |
| 96 | } |
| 97 | } while (level >= 0); |
| 98 | |
| 99 | return nextoffset; |
| 100 | } |
| 101 | |
| 102 | int fdt_nop_node(void *fdt, int nodeoffset) |
| 103 | { |
| 104 | int endoffset; |
| 105 | |
| 106 | endoffset = _fdt_node_end_offset(fdt, nodeoffset); |
| 107 | if (endoffset < 0) |
| 108 | return endoffset; |
| 109 | |
| 110 | nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset); |
| 111 | return 0; |
| 112 | } |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame^] | 113 | |
| 114 | /* |
| 115 | * Replace a reserve map entry in the nth slot. |
| 116 | */ |
| 117 | int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size) |
| 118 | { |
| 119 | struct fdt_reserve_entry *re; |
| 120 | int used; |
| 121 | int total; |
| 122 | int err; |
| 123 | |
| 124 | err = fdt_num_reservemap(fdt, &used, &total); |
| 125 | if (err != 0) |
| 126 | return err; |
| 127 | |
| 128 | if (n >= total) |
| 129 | return -FDT_ERR_NOSPACE; |
| 130 | re = (struct fdt_reserve_entry *) |
| 131 | (fdt + fdt_off_mem_rsvmap(fdt) + |
| 132 | (n * sizeof(struct fdt_reserve_entry))); |
| 133 | re->address = cpu_to_fdt64(addr); |
| 134 | re->size = cpu_to_fdt64(size); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |