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. |
| 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 | */ |
Gerald Van Baren | 960caf3 | 2007-04-20 22:46:53 -0400 | [diff] [blame] | 19 | #include "config.h" |
| 20 | #if CONFIG_OF_LIBFDT |
| 21 | |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 22 | #include "libfdt_env.h" |
| 23 | |
| 24 | #include <fdt.h> |
| 25 | #include <libfdt.h> |
| 26 | |
| 27 | #include "libfdt_internal.h" |
| 28 | |
Gerald Van Baren | 9f0f358 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 29 | int fdt_check_header(const void *fdt) |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 30 | { |
| 31 | if (fdt_magic(fdt) == FDT_MAGIC) { |
| 32 | /* Complete tree */ |
| 33 | if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) |
| 34 | return -FDT_ERR_BADVERSION; |
| 35 | if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) |
| 36 | return -FDT_ERR_BADVERSION; |
| 37 | } else if (fdt_magic(fdt) == SW_MAGIC) { |
| 38 | /* Unfinished sequential-write blob */ |
| 39 | if (fdt_size_dt_struct(fdt) == 0) |
| 40 | return -FDT_ERR_BADSTATE; |
| 41 | } else { |
| 42 | return -FDT_ERR_BADMAGIC; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | void *fdt_offset_ptr(const void *fdt, int offset, int len) |
| 49 | { |
| 50 | void *p; |
| 51 | |
| 52 | if (fdt_version(fdt) >= 0x11) |
| 53 | if (((offset + len) < offset) |
| 54 | || ((offset + len) > fdt_size_dt_struct(fdt))) |
| 55 | return NULL; |
| 56 | |
| 57 | p = _fdt_offset_ptr(fdt, offset); |
| 58 | |
| 59 | if (p + len < p) |
| 60 | return NULL; |
| 61 | return p; |
| 62 | } |
| 63 | |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 64 | const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) |
| 65 | { |
| 66 | int len = strlen(s) + 1; |
| 67 | const char *last = strtab + tabsize - len; |
| 68 | const char *p; |
| 69 | |
| 70 | for (p = strtab; p <= last; p++) |
| 71 | if (memeq(p, s, len)) |
| 72 | return p; |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | int fdt_move(const void *fdt, void *buf, int bufsize) |
| 77 | { |
Gerald Van Baren | 9f0f358 | 2007-04-06 14:17:14 -0400 | [diff] [blame] | 78 | int err = fdt_check_header(fdt); |
Gerald Van Baren | c0577a6 | 2007-03-31 11:59:59 -0400 | [diff] [blame] | 79 | |
| 80 | if (err) |
| 81 | return err; |
| 82 | |
| 83 | if (fdt_totalsize(fdt) > bufsize) |
| 84 | return -FDT_ERR_NOSPACE; |
| 85 | |
| 86 | memmove(buf, fdt, fdt_totalsize(fdt)); |
| 87 | return 0; |
| 88 | } |
Gerald Van Baren | 960caf3 | 2007-04-20 22:46:53 -0400 | [diff] [blame] | 89 | |
| 90 | #endif /* CONFIG_OF_LIBFDT */ |