Gerald Van Baren | 7cd5da0 | 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 | 3508476 | 2013-07-27 01:12:38 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 5 | */ |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 6 | #include <libfdt_env.h> |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 7 | |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 8 | #ifndef USE_HOSTCC |
Gerald Van Baren | 7cd5da0 | 2007-03-31 11:59:59 -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 | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 14 | |
| 15 | #include "libfdt_internal.h" |
| 16 | |
Gerald Van Baren | 6679f92 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 17 | int fdt_check_header(const void *fdt) |
Gerald Van Baren | 7cd5da0 | 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 | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 25 | } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { |
Gerald Van Baren | 7cd5da0 | 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 | 741a6d0 | 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 | 7cd5da0 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 37 | { |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 38 | const char *p; |
Gerald Van Baren | 7cd5da0 | 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 | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 52 | uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 53 | { |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 54 | const fdt32_t *tagp, *lenp; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 55 | uint32_t tag; |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 56 | int offset = startoffset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 57 | const char *p; |
| 58 | |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 59 | *nextoffset = -FDT_ERR_TRUNCATED; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 60 | tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 61 | if (!tagp) |
Kumar Gala | 8d04f02 | 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 | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 66 | *nextoffset = -FDT_ERR_BADSTRUCTURE; |
Kumar Gala | 8d04f02 | 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 | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 73 | if (!p) |
| 74 | return FDT_END; /* premature end */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 75 | break; |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 76 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 77 | case FDT_PROP: |
| 78 | lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); |
David Gibson | a22d9cf | 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); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 84 | break; |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 85 | |
| 86 | case FDT_END: |
| 87 | case FDT_END_NODE: |
| 88 | case FDT_NOP: |
| 89 | break; |
| 90 | |
| 91 | default: |
| 92 | return FDT_END; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 93 | } |
| 94 | |
David Gibson | a22d9cf | 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 | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 97 | |
David Gibson | a22d9cf | 2009-02-06 14:03:24 +1100 | [diff] [blame] | 98 | *nextoffset = FDT_TAGALIGN(offset); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 99 | return tag; |
| 100 | } |
| 101 | |
David Gibson | 2f08bfa | 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 | d1c6314 | 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 | ae0b590 | 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 | 2f08bfa | 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 | ae0b590 | 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 | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 144 | if (depth && ((--(*depth)) < 0)) |
| 145 | return nextoffset; |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 146 | break; |
| 147 | |
| 148 | case FDT_END: |
David Gibson | a22d9cf | 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 | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 154 | } |
| 155 | } while (tag != FDT_BEGIN_NODE); |
| 156 | |
| 157 | return offset; |
| 158 | } |
| 159 | |
Simon Glass | 88f95bb | 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 | 7cd5da0 | 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 | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 195 | if (memcmp(p, s, len) == 0) |
Gerald Van Baren | 7cd5da0 | 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 | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 202 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 7cd5da0 | 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 | } |