David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [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 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 26 | int _fdt_check_header(const void *fdt) |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 27 | { |
| 28 | if (fdt_magic(fdt) == FDT_MAGIC) { |
| 29 | /* Complete tree */ |
| 30 | if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 31 | return -FDT_ERR_BADVERSION; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 32 | if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 33 | return -FDT_ERR_BADVERSION; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 34 | } else if (fdt_magic(fdt) == SW_MAGIC) { |
| 35 | /* Unfinished sequential-write blob */ |
David Gibson | fe92f6b | 2006-12-01 16:25:39 +1100 | [diff] [blame] | 36 | if (fdt_size_dt_struct(fdt) == 0) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 37 | return -FDT_ERR_BADSTATE; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 38 | } else { |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 39 | return -FDT_ERR_BADMAGIC; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame^] | 45 | const void *fdt_offset_ptr(const void *fdt, int offset, int len) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 46 | { |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame^] | 47 | const void *p; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 48 | |
David Gibson | fe92f6b | 2006-12-01 16:25:39 +1100 | [diff] [blame] | 49 | if (fdt_version(fdt) >= 0x11) |
| 50 | if (((offset + len) < offset) |
| 51 | || ((offset + len) > fdt_size_dt_struct(fdt))) |
| 52 | return NULL; |
| 53 | |
David Gibson | 568b569 | 2006-12-12 15:46:14 +1100 | [diff] [blame] | 54 | p = _fdt_offset_ptr(fdt, offset); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 55 | |
| 56 | if (p + len < p) |
| 57 | return NULL; |
| 58 | return p; |
| 59 | } |
| 60 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 61 | uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 62 | { |
| 63 | const uint32_t *tagp, *lenp; |
| 64 | uint32_t tag; |
| 65 | const char *p; |
| 66 | |
| 67 | if (offset % FDT_TAGSIZE) |
| 68 | return -1; |
| 69 | |
| 70 | tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); |
| 71 | if (! tagp) |
| 72 | return FDT_END; /* premature end */ |
| 73 | tag = fdt32_to_cpu(*tagp); |
| 74 | offset += FDT_TAGSIZE; |
| 75 | |
| 76 | switch (tag) { |
| 77 | case FDT_BEGIN_NODE: |
| 78 | /* skip name */ |
| 79 | do { |
| 80 | p = fdt_offset_ptr(fdt, offset++, 1); |
| 81 | } while (p && (*p != '\0')); |
| 82 | if (! p) |
| 83 | return FDT_END; |
| 84 | break; |
| 85 | case FDT_PROP: |
David Gibson | 07a12a0 | 2007-02-23 14:40:14 +1100 | [diff] [blame] | 86 | lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 87 | if (! lenp) |
| 88 | return FDT_END; |
| 89 | /* skip name offset, length and value */ |
| 90 | offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | if (nextoffset) |
| 95 | *nextoffset = ALIGN(offset, FDT_TAGSIZE); |
| 96 | |
| 97 | return tag; |
| 98 | } |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 99 | |
David Gibson | aeddfe2 | 2006-12-01 15:11:58 +1100 | [diff] [blame] | 100 | const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) |
| 101 | { |
| 102 | int len = strlen(s) + 1; |
| 103 | const char *last = strtab + tabsize - len; |
| 104 | const char *p; |
| 105 | |
| 106 | for (p = strtab; p <= last; p++) |
| 107 | if (memeq(p, s, len)) |
| 108 | return p; |
| 109 | return NULL; |
| 110 | } |
| 111 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 112 | int fdt_move(const void *fdt, void *buf, int bufsize) |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 113 | { |
| 114 | int err = _fdt_check_header(fdt); |
| 115 | |
| 116 | if (err) |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 117 | return err; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 118 | |
| 119 | if (fdt_totalsize(fdt) > bufsize) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 120 | return -FDT_ERR_NOSPACE; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 121 | |
| 122 | memmove(buf, fdt, fdt_totalsize(fdt)); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 123 | return 0; |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 124 | } |