| #ifndef _DTC_H |
| #define _DTC_H |
| |
| /* |
| * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
| * |
| * |
| * This program is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU General Public License as |
| * published by the Free Software Foundation; either version 2 of the |
| * License, or (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| * USA |
| */ |
| |
| #include <stdio.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <stdint.h> |
| #include <stdarg.h> |
| #include <assert.h> |
| #include <ctype.h> |
| #include <errno.h> |
| #include <unistd.h> |
| #include <netinet/in.h> |
| #include <endian.h> |
| #include <byteswap.h> |
| |
| #include "flat_dt.h" |
| |
| static inline void die(char * str, ...) |
| { |
| va_list ap; |
| |
| va_start(ap, str); |
| fprintf(stderr, "FATAL ERROR: "); |
| vfprintf(stderr, str, ap); |
| exit(1); |
| } |
| |
| static inline void *xmalloc(size_t len) |
| { |
| void *new = malloc(len); |
| |
| if (! new) |
| die("malloc() failed\n"); |
| |
| return new; |
| } |
| |
| static inline void *xrealloc(void *p, size_t len) |
| { |
| void *new = realloc(p, len); |
| |
| if (! new) |
| die("realloc() failed (len=%d)\n", len); |
| |
| return new; |
| } |
| |
| typedef uint8_t u8; |
| typedef uint16_t u16; |
| typedef uint32_t u32; |
| typedef uint64_t u64; |
| typedef u32 cell_t; |
| |
| #define cpu_to_be16(x) htons(x) |
| #define be16_to_cpu(x) ntohs(x) |
| |
| #define cpu_to_be32(x) htonl(x) |
| #define be32_to_cpu(x) ntohl(x) |
| |
| #if __BYTE_ORDER == __BIG_ENDIAN |
| #define cpu_to_be64(x) (x) |
| #define be64_to_cpu(x) (x) |
| #else |
| #define cpu_to_be64(x) bswap_64(x) |
| #define be64_to_cpu(x) bswap_64(x) |
| #endif |
| |
| #define streq(a, b) (strcmp((a), (b)) == 0) |
| #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) |
| |
| #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) |
| #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| |
| /* Data blobs */ |
| struct fixup { |
| int offset; |
| char *ref; |
| struct fixup *next; |
| }; |
| |
| struct data { |
| int len; |
| char *val; |
| int asize; |
| struct fixup *refs; |
| }; |
| |
| #define empty_data \ |
| ((struct data){.len = 0, .val = NULL, .asize = 0, .refs = NULL}) |
| |
| void fixup_free(struct fixup *f); |
| void data_free(struct data d); |
| |
| struct data data_grow_for(struct data d, int xlen); |
| |
| struct data data_copy_mem(char *mem, int len); |
| struct data data_copy_escape_string(char *s, int len); |
| struct data data_copy_file(FILE *f, size_t len); |
| |
| struct data data_append_data(struct data d, void *p, int len); |
| struct data data_append_cell(struct data d, cell_t word); |
| struct data data_append_addr(struct data d, u64 addr); |
| struct data data_append_byte(struct data d, uint8_t byte); |
| struct data data_append_zeroes(struct data d, int len); |
| struct data data_append_align(struct data d, int align); |
| |
| struct data data_add_fixup(struct data d, char *ref); |
| |
| int data_is_one_string(struct data d); |
| |
| struct data build_mem_reserve(struct data d); |
| |
| /* DT constraints */ |
| |
| #define MAX_PROPNAME_LEN 31 |
| #define MAX_NODENAME_LEN 31 |
| |
| /* Live trees */ |
| struct property { |
| char *name; |
| struct data val; |
| |
| struct property *next; |
| |
| char *label; |
| }; |
| |
| struct node { |
| char *name; |
| struct property *proplist; |
| struct node *children; |
| |
| struct node *parent; |
| struct node *next_sibling; |
| |
| char *fullpath; |
| int basenamelen; |
| |
| cell_t phandle; |
| int addr_cells, size_cells; |
| |
| char *label; |
| }; |
| |
| #define for_each_property(n, p) \ |
| for ((p) = (n)->proplist; (p); (p) = (p)->next) |
| |
| #define for_each_child(n, c) \ |
| for ((c) = (n)->children; (c); (c) = (c)->next_sibling) |
| |
| struct property *build_property(char *name, struct data val, char *label); |
| struct property *chain_property(struct property *first, struct property *list); |
| |
| struct node *build_node(struct property *proplist, struct node *children); |
| struct node *name_node(struct node *node, char *name, char *label); |
| struct node *chain_node(struct node *first, struct node *list); |
| |
| void add_property(struct node *node, struct property *prop); |
| void add_child(struct node *parent, struct node *child); |
| |
| int check_device_tree(struct node *dt); |
| |
| /* Boot info (tree plus memreserve information */ |
| |
| struct boot_info { |
| struct data mem_reserve_data; /* mem reserve from header */ |
| struct node *dt; /* the device tree */ |
| }; |
| |
| struct boot_info *build_boot_info(struct data mem_reserve_data, |
| struct node *tree); |
| |
| /* Flattened trees */ |
| |
| enum flat_dt_format { |
| FFMT_BIN, |
| FFMT_ASM, |
| }; |
| |
| void write_dt_blob(FILE *f, struct boot_info *bi, int version); |
| void write_dt_asm(FILE *f, struct boot_info *bi, int version); |
| |
| struct boot_info *dt_from_blob(FILE *f); |
| |
| /* Tree source */ |
| |
| void write_tree_source(FILE *f, struct boot_info *bi); |
| struct boot_info *dt_from_source(FILE *f); |
| |
| /* FS trees */ |
| |
| struct boot_info *dt_from_fs(char *dirname); |
| |
| /* misc */ |
| |
| char *join_path(char *path, char *name); |
| void fill_fullpaths(struct node *tree, char *prefix); |
| |
| #endif /* _DTC_H */ |