Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
Roger Meier | 3508476 | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include "libfdt_env.h" |
| 7 | |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 8 | #ifndef USE_HOSTCC |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 9 | #include <fdt.h> |
| 10 | #include <libfdt.h> |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 11 | #else |
| 12 | #include "fdt_host.h" |
| 13 | #endif |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 14 | |
| 15 | #include "libfdt_internal.h" |
| 16 | |
| 17 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, |
| 18 | const void *val, int len) |
| 19 | { |
| 20 | void *propval; |
| 21 | int proplen; |
| 22 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 23 | propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 24 | if (! propval) |
| 25 | return proplen; |
| 26 | |
| 27 | if (proplen != len) |
| 28 | return -FDT_ERR_NOSPACE; |
| 29 | |
| 30 | memcpy(propval, val, len); |
| 31 | return 0; |
| 32 | } |
| 33 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 34 | static void _fdt_nop_region(void *start, int len) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 35 | { |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 36 | fdt32_t *p; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 37 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 38 | for (p = start; (char *)p < ((char *)start + len); p++) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 39 | *p = cpu_to_fdt32(FDT_NOP); |
| 40 | } |
| 41 | |
| 42 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name) |
| 43 | { |
| 44 | struct fdt_property *prop; |
| 45 | int len; |
| 46 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 47 | prop = fdt_get_property_w(fdt, nodeoffset, name, &len); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 48 | if (! prop) |
| 49 | return len; |
| 50 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 51 | _fdt_nop_region(prop, len + sizeof(*prop)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 56 | int _fdt_node_end_offset(void *fdt, int offset) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 57 | { |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 58 | int depth = 0; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 59 | |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 60 | while ((offset >= 0) && (depth >= 0)) |
| 61 | offset = fdt_next_node(fdt, offset, &depth); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 62 | |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 63 | return offset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int fdt_nop_node(void *fdt, int nodeoffset) |
| 67 | { |
| 68 | int endoffset; |
| 69 | |
| 70 | endoffset = _fdt_node_end_offset(fdt, nodeoffset); |
| 71 | if (endoffset < 0) |
| 72 | return endoffset; |
| 73 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 74 | _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0), |
| 75 | endoffset - nodeoffset); |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 76 | return 0; |
| 77 | } |
Simon Glass | 3e06cd1 | 2013-06-13 15:10:08 -0700 | [diff] [blame] | 78 | |
| 79 | #define FDT_MAX_DEPTH 32 |
| 80 | |
| 81 | static int str_in_list(const char *str, char * const list[], int count) |
| 82 | { |
| 83 | int i; |
| 84 | |
| 85 | for (i = 0; i < count; i++) |
| 86 | if (!strcmp(list[i], str)) |
| 87 | return 1; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int fdt_find_regions(const void *fdt, char * const inc[], int inc_count, |
| 93 | char * const exc_prop[], int exc_prop_count, |
| 94 | struct fdt_region region[], int max_regions, |
| 95 | char *path, int path_len, int add_string_tab) |
| 96 | { |
| 97 | int stack[FDT_MAX_DEPTH]; |
| 98 | char *end; |
| 99 | int nextoffset = 0; |
| 100 | uint32_t tag; |
| 101 | int count = 0; |
| 102 | int start = -1; |
| 103 | int depth = -1; |
| 104 | int want = 0; |
| 105 | int base = fdt_off_dt_struct(fdt); |
| 106 | |
| 107 | end = path; |
| 108 | *end = '\0'; |
| 109 | do { |
| 110 | const struct fdt_property *prop; |
| 111 | const char *name; |
| 112 | const char *str; |
| 113 | int include = 0; |
| 114 | int stop_at = 0; |
| 115 | int offset; |
| 116 | int len; |
| 117 | |
| 118 | offset = nextoffset; |
| 119 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
| 120 | stop_at = nextoffset; |
| 121 | |
| 122 | switch (tag) { |
| 123 | case FDT_PROP: |
| 124 | include = want >= 2; |
| 125 | stop_at = offset; |
| 126 | prop = fdt_get_property_by_offset(fdt, offset, NULL); |
| 127 | str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); |
| 128 | if (str_in_list(str, exc_prop, exc_prop_count)) |
| 129 | include = 0; |
| 130 | break; |
| 131 | |
| 132 | case FDT_NOP: |
| 133 | include = want >= 2; |
| 134 | stop_at = offset; |
| 135 | break; |
| 136 | |
| 137 | case FDT_BEGIN_NODE: |
| 138 | depth++; |
| 139 | if (depth == FDT_MAX_DEPTH) |
| 140 | return -FDT_ERR_BADSTRUCTURE; |
| 141 | name = fdt_get_name(fdt, offset, &len); |
| 142 | if (end - path + 2 + len >= path_len) |
| 143 | return -FDT_ERR_NOSPACE; |
| 144 | if (end != path + 1) |
| 145 | *end++ = '/'; |
| 146 | strcpy(end, name); |
| 147 | end += len; |
| 148 | stack[depth] = want; |
| 149 | if (want == 1) |
| 150 | stop_at = offset; |
| 151 | if (str_in_list(path, inc, inc_count)) |
| 152 | want = 2; |
| 153 | else if (want) |
| 154 | want--; |
| 155 | else |
| 156 | stop_at = offset; |
| 157 | include = want; |
| 158 | break; |
| 159 | |
| 160 | case FDT_END_NODE: |
| 161 | include = want; |
| 162 | want = stack[depth--]; |
| 163 | while (end > path && *--end != '/') |
| 164 | ; |
| 165 | *end = '\0'; |
| 166 | break; |
| 167 | |
| 168 | case FDT_END: |
| 169 | include = 1; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if (include && start == -1) { |
| 174 | /* Should we merge with previous? */ |
| 175 | if (count && count <= max_regions && |
| 176 | offset == region[count - 1].offset + |
| 177 | region[count - 1].size - base) |
| 178 | start = region[--count].offset - base; |
| 179 | else |
| 180 | start = offset; |
| 181 | } |
| 182 | |
| 183 | if (!include && start != -1) { |
| 184 | if (count < max_regions) { |
| 185 | region[count].offset = base + start; |
| 186 | region[count].size = stop_at - start; |
| 187 | } |
| 188 | count++; |
| 189 | start = -1; |
| 190 | } |
| 191 | } while (tag != FDT_END); |
| 192 | |
| 193 | if (nextoffset != fdt_size_dt_struct(fdt)) |
| 194 | return -FDT_ERR_BADLAYOUT; |
| 195 | |
| 196 | /* Add a region for the END tag and the string table */ |
| 197 | if (count < max_regions) { |
| 198 | region[count].offset = base + start; |
| 199 | region[count].size = nextoffset - start; |
| 200 | if (add_string_tab) |
| 201 | region[count].size += fdt_size_dt_strings(fdt); |
| 202 | } |
| 203 | count++; |
| 204 | |
| 205 | return count; |
| 206 | } |