David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 1 | #ifndef _LIBFDT_H |
| 2 | #define _LIBFDT_H |
| 3 | /* |
| 4 | * libfdt - Flat Device Tree manipulation |
| 5 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public License |
| 9 | * as published by the Free Software Foundation; either version 2.1 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <fdt.h> |
| 23 | |
| 24 | #define FDT_FIRST_SUPPORTED_VERSION 0x10 |
| 25 | #define FDT_LAST_SUPPORTED_VERSION 0x10 |
| 26 | |
| 27 | /* Errors */ |
| 28 | #define FDT_ERR_OK 0 |
| 29 | #define FDT_ERR_BADMAGIC 1 |
| 30 | #define FDT_ERR_BADVERSION 2 |
| 31 | #define FDT_ERR_BADPOINTER 3 |
| 32 | #define FDT_ERR_BADHEADER 4 |
| 33 | #define FDT_ERR_BADSTRUCTURE 5 |
| 34 | #define FDT_ERR_BADOFFSET 6 |
| 35 | #define FDT_ERR_NOTFOUND 7 |
| 36 | #define FDT_ERR_BADPATH 8 |
| 37 | #define FDT_ERR_TRUNCATED 9 |
| 38 | #define FDT_ERR_NOSPACE 10 |
| 39 | #define FDT_ERR_BADSTATE 11 |
| 40 | #define FDT_ERR_SIZE_MISMATCH 12 |
| 41 | #define FDT_ERR_INTERNAL 13 |
| 42 | |
| 43 | #define FDT_ERR_MAX 13 |
| 44 | |
| 45 | /* Offset handling functions */ |
| 46 | void *fdt_offset_ptr(const struct fdt_header *fdt, int offset, int checklen); |
| 47 | |
| 48 | #define fdt_offset_ptr_typed(fdt, offset, var) \ |
| 49 | ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var))))) |
| 50 | |
| 51 | #define fdt_offset_error(offset) \ |
| 52 | ( (offset) < 0 ? -(offset) : 0 ) |
| 53 | |
| 54 | #define fdt_ptr_error(ptr) \ |
| 55 | ( (((long)(ptr) < 0) && ((long)(ptr) >= -FDT_ERR_MAX)) ? -(long)(ptr) : 0 ) |
| 56 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 57 | /* Read-only functions */ |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 58 | char *fdt_string(const struct fdt_header *fdt, int stroffset); |
| 59 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 60 | int fdt_property_offset(const struct fdt_header *fdt, int nodeoffset, |
| 61 | const char *name); |
| 62 | int fdt_subnode_offset_namelen(const struct fdt_header *fdt, int parentoffset, |
| 63 | const char *name, int namelen); |
| 64 | int fdt_subnode_offset(const struct fdt_header *fdt, int parentoffset, |
| 65 | const char *name); |
| 66 | |
| 67 | int fdt_path_offset(const struct fdt_header *fdt, const char *path); |
| 68 | |
| 69 | void *fdt_getprop(const struct fdt_header *fdt, int nodeoffset, |
| 70 | const char *name, int *lenp); |
| 71 | |
| 72 | /* Write-in-place functions */ |
| 73 | int fdt_setprop_inplace(struct fdt_header *fdt, int nodeoffset, const char *name, |
| 74 | const void *val, int len); |
| 75 | |
| 76 | #define fdt_setprop_inplace_typed(fdt, nodeoffset, name, val) \ |
| 77 | ({ \ |
| 78 | typeof(val) x = val; \ |
| 79 | fdt_setprop_inplace(fdt, nodeoffset, name, &x, sizeof(x)); \ |
| 80 | }) |
| 81 | |
| 82 | int fdt_nop_property(struct fdt_header *fdt, int nodeoffset, const char *name); |
| 83 | int fdt_nop_node(struct fdt_header *fdt, int nodeoffset); |
| 84 | |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 85 | /* Sequential-write functions */ |
| 86 | struct fdt_header *fdt_create(void *buf, int bufsize); |
| 87 | int fdt_add_reservemap_entry(struct fdt_header *fdt, uint64_t addr, uint64_t size); |
David Gibson | 063693a | 2006-11-29 16:45:46 +1100 | [diff] [blame^] | 88 | int fdt_finish_reservemap(struct fdt_header *fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 89 | int fdt_begin_node(struct fdt_header *fdt, const char *name); |
| 90 | int fdt_property(struct fdt_header *fdt, const char *name, const void *val, int len); |
David Gibson | 063693a | 2006-11-29 16:45:46 +1100 | [diff] [blame^] | 91 | #define fdt_property_typed(fdt, name, val) \ |
| 92 | ({ \ |
| 93 | typeof(val) x = (val); \ |
| 94 | fdt_property((fdt), (name), &x, sizeof(x)); \ |
| 95 | }) |
| 96 | #define fdt_property_string(fdt, name, str) \ |
| 97 | fdt_property(fdt, name, str, strlen(str)+1) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 98 | int fdt_end_node(struct fdt_header *fdt); |
David Gibson | 063693a | 2006-11-29 16:45:46 +1100 | [diff] [blame^] | 99 | int fdt_finish(struct fdt_header *fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 100 | |
David Gibson | 063693a | 2006-11-29 16:45:46 +1100 | [diff] [blame^] | 101 | #if 0 |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 102 | /* Read-write functions */ |
| 103 | struct fdt_header *fdt_open(struct fdt_header *fdt, int bufsize); |
| 104 | int fdt_add_subnode(struct fdt_header *fdtx, void *node, const char *name); |
| 105 | int fdt_set_property(struct fdt_header *fdtx, void *node, const char *name, |
| 106 | const void *val, int len); |
| 107 | int fdt_del_property(struct fdt_header *fdtx, void *node, const char *name); |
| 108 | |
| 109 | /* Misc functions */ |
| 110 | struct fdt_header *fdt_move(struct fdt_header *fdt, void *buf, int bufsize); |
| 111 | #endif |
| 112 | |
| 113 | #endif /* _LIBFDT_H */ |