Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
Roger Meier | 3eaedea | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include "libfdt_env.h" |
| 7 | |
Bartlomiej Sieka | 6125086 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 8 | #ifndef USE_HOSTCC |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 9 | #include <fdt.h> |
| 10 | #include <libfdt.h> |
Bartlomiej Sieka | 6125086 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 11 | #else |
| 12 | #include "fdt_host.h" |
| 13 | #endif |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 14 | |
| 15 | #include "libfdt_internal.h" |
| 16 | |
Gerald Van Baren | 9f0f358 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 17 | int fdt_check_header(const void *fdt) |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 18 | { |
| 19 | if (fdt_magic(fdt) == FDT_MAGIC) { |
| 20 | /* Complete tree */ |
| 21 | if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) |
| 22 | return -FDT_ERR_BADVERSION; |
| 23 | if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) |
| 24 | return -FDT_ERR_BADVERSION; |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 25 | } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 26 | /* Unfinished sequential-write blob */ |
| 27 | if (fdt_size_dt_struct(fdt) == 0) |
| 28 | return -FDT_ERR_BADSTATE; |
| 29 | } else { |
| 30 | return -FDT_ERR_BADMAGIC; |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
Jon Loeliger | d94d0f1 | 2008-09-25 11:02:17 -0500 | [diff] [blame] | 36 | const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 37 | { |
David Gibson | b4141b8 | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 38 | const char *p; |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 39 | |
| 40 | if (fdt_version(fdt) >= 0x11) |
| 41 | if (((offset + len) < offset) |
| 42 | || ((offset + len) > fdt_size_dt_struct(fdt))) |
| 43 | return NULL; |
| 44 | |
| 45 | p = _fdt_offset_ptr(fdt, offset); |
| 46 | |
| 47 | if (p + len < p) |
| 48 | return NULL; |
| 49 | return p; |
| 50 | } |
| 51 | |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 52 | uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 53 | { |
Kim Phillips | 38c3815 | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 54 | const fdt32_t *tagp, *lenp; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 55 | uint32_t tag; |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 56 | int offset = startoffset; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 57 | const char *p; |
| 58 | |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 59 | *nextoffset = -FDT_ERR_TRUNCATED; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 60 | tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 61 | if (!tagp) |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 62 | return FDT_END; /* premature end */ |
| 63 | tag = fdt32_to_cpu(*tagp); |
| 64 | offset += FDT_TAGSIZE; |
| 65 | |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 66 | *nextoffset = -FDT_ERR_BADSTRUCTURE; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 67 | switch (tag) { |
| 68 | case FDT_BEGIN_NODE: |
| 69 | /* skip name */ |
| 70 | do { |
| 71 | p = fdt_offset_ptr(fdt, offset++, 1); |
| 72 | } while (p && (*p != '\0')); |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 73 | if (!p) |
| 74 | return FDT_END; /* premature end */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 75 | break; |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 76 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 77 | case FDT_PROP: |
| 78 | lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 79 | if (!lenp) |
| 80 | return FDT_END; /* premature end */ |
| 81 | /* skip-name offset, length and value */ |
| 82 | offset += sizeof(struct fdt_property) - FDT_TAGSIZE |
| 83 | + fdt32_to_cpu(*lenp); |
| 84 | break; |
| 85 | |
| 86 | case FDT_END: |
| 87 | case FDT_END_NODE: |
| 88 | case FDT_NOP: |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 89 | break; |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 90 | |
| 91 | default: |
| 92 | return FDT_END; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 93 | } |
| 94 | |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 95 | if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) |
| 96 | return FDT_END; /* premature end */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 97 | |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 98 | *nextoffset = FDT_TAGALIGN(offset); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 99 | return tag; |
| 100 | } |
| 101 | |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 102 | int _fdt_check_node_offset(const void *fdt, int offset) |
| 103 | { |
| 104 | if ((offset < 0) || (offset % FDT_TAGSIZE) |
| 105 | || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)) |
| 106 | return -FDT_ERR_BADOFFSET; |
| 107 | |
| 108 | return offset; |
| 109 | } |
| 110 | |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 111 | int _fdt_check_prop_offset(const void *fdt, int offset) |
| 112 | { |
| 113 | if ((offset < 0) || (offset % FDT_TAGSIZE) |
| 114 | || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)) |
| 115 | return -FDT_ERR_BADOFFSET; |
| 116 | |
| 117 | return offset; |
| 118 | } |
| 119 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 120 | int fdt_next_node(const void *fdt, int offset, int *depth) |
| 121 | { |
| 122 | int nextoffset = 0; |
| 123 | uint32_t tag; |
| 124 | |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 125 | if (offset >= 0) |
| 126 | if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0) |
| 127 | return nextoffset; |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 128 | |
| 129 | do { |
| 130 | offset = nextoffset; |
| 131 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
| 132 | |
| 133 | switch (tag) { |
| 134 | case FDT_PROP: |
| 135 | case FDT_NOP: |
| 136 | break; |
| 137 | |
| 138 | case FDT_BEGIN_NODE: |
| 139 | if (depth) |
| 140 | (*depth)++; |
| 141 | break; |
| 142 | |
| 143 | case FDT_END_NODE: |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 144 | if (depth && ((--(*depth)) < 0)) |
| 145 | return nextoffset; |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | case FDT_END: |
David Gibson | 82c1023 | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 149 | if ((nextoffset >= 0) |
| 150 | || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) |
| 151 | return -FDT_ERR_NOTFOUND; |
| 152 | else |
| 153 | return nextoffset; |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 154 | } |
| 155 | } while (tag != FDT_BEGIN_NODE); |
| 156 | |
| 157 | return offset; |
| 158 | } |
| 159 | |
Simon Glass | 3a4de33 | 2013-05-07 06:11:50 +0000 | [diff] [blame] | 160 | int fdt_first_subnode(const void *fdt, int offset) |
| 161 | { |
| 162 | int depth = 0; |
| 163 | |
| 164 | offset = fdt_next_node(fdt, offset, &depth); |
| 165 | if (offset < 0 || depth != 1) |
| 166 | return -FDT_ERR_NOTFOUND; |
| 167 | |
| 168 | return offset; |
| 169 | } |
| 170 | |
| 171 | int fdt_next_subnode(const void *fdt, int offset) |
| 172 | { |
| 173 | int depth = 1; |
| 174 | |
| 175 | /* |
| 176 | * With respect to the parent, the depth of the next subnode will be |
| 177 | * the same as the last. |
| 178 | */ |
| 179 | do { |
| 180 | offset = fdt_next_node(fdt, offset, &depth); |
| 181 | if (offset < 0 || depth < 1) |
| 182 | return -FDT_ERR_NOTFOUND; |
| 183 | } while (depth > 1); |
| 184 | |
| 185 | return offset; |
| 186 | } |
| 187 | |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 188 | const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) |
| 189 | { |
| 190 | int len = strlen(s) + 1; |
| 191 | const char *last = strtab + tabsize - len; |
| 192 | const char *p; |
| 193 | |
| 194 | for (p = strtab; p <= last; p++) |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 195 | if (memcmp(p, s, len) == 0) |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 196 | return p; |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | int fdt_move(const void *fdt, void *buf, int bufsize) |
| 201 | { |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 202 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 203 | |
| 204 | if (fdt_totalsize(fdt) > bufsize) |
| 205 | return -FDT_ERR_NOSPACE; |
| 206 | |
| 207 | memmove(buf, fdt, fdt_totalsize(fdt)); |
| 208 | return 0; |
| 209 | } |