dtc.h revision 204431
1204431Sraj#ifndef _DTC_H 2204431Sraj#define _DTC_H 3204431Sraj 4204431Sraj/* 5204431Sraj * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. 6204431Sraj * 7204431Sraj * 8204431Sraj * This program is free software; you can redistribute it and/or 9204431Sraj * modify it under the terms of the GNU General Public License as 10204431Sraj * published by the Free Software Foundation; either version 2 of the 11204431Sraj * License, or (at your option) any later version. 12204431Sraj * 13204431Sraj * This program is distributed in the hope that it will be useful, 14204431Sraj * but WITHOUT ANY WARRANTY; without even the implied warranty of 15204431Sraj * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16204431Sraj * General Public License for more details. 17204431Sraj * 18204431Sraj * You should have received a copy of the GNU General Public License 19204431Sraj * along with this program; if not, write to the Free Software 20204431Sraj * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 21204431Sraj * USA 22204431Sraj */ 23204431Sraj 24204431Sraj#include <stdio.h> 25204431Sraj#include <string.h> 26204431Sraj#include <stdlib.h> 27204431Sraj#include <stdint.h> 28204431Sraj#include <stdarg.h> 29204431Sraj#include <assert.h> 30204431Sraj#include <ctype.h> 31204431Sraj#include <errno.h> 32204431Sraj#include <unistd.h> 33204431Sraj 34204431Sraj#include <libfdt_env.h> 35204431Sraj#include <fdt.h> 36204431Sraj 37204431Sraj#define DEFAULT_FDT_VERSION 17 38204431Sraj/* 39204431Sraj * Command line options 40204431Sraj */ 41204431Srajextern int quiet; /* Level of quietness */ 42204431Srajextern int reservenum; /* Number of memory reservation slots */ 43204431Srajextern int minsize; /* Minimum blob size */ 44204431Srajextern int padsize; /* Additional padding to blob */ 45204431Sraj 46204431Srajstatic inline void __attribute__((noreturn)) die(char * str, ...) 47204431Sraj{ 48204431Sraj va_list ap; 49204431Sraj 50204431Sraj va_start(ap, str); 51204431Sraj fprintf(stderr, "FATAL ERROR: "); 52204431Sraj vfprintf(stderr, str, ap); 53204431Sraj exit(1); 54204431Sraj} 55204431Sraj 56204431Srajstatic inline void *xmalloc(size_t len) 57204431Sraj{ 58204431Sraj void *new = malloc(len); 59204431Sraj 60204431Sraj if (! new) 61204431Sraj die("malloc() failed\n"); 62204431Sraj 63204431Sraj return new; 64204431Sraj} 65204431Sraj 66204431Srajstatic inline void *xrealloc(void *p, size_t len) 67204431Sraj{ 68204431Sraj void *new = realloc(p, len); 69204431Sraj 70204431Sraj if (! new) 71204431Sraj die("realloc() failed (len=%d)\n", len); 72204431Sraj 73204431Sraj return new; 74204431Sraj} 75204431Sraj 76204431Srajtypedef uint32_t cell_t; 77204431Sraj 78204431Sraj 79204431Sraj#define streq(a, b) (strcmp((a), (b)) == 0) 80204431Sraj#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) 81204431Sraj 82204431Sraj#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 83204431Sraj#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 84204431Sraj 85204431Sraj/* Data blobs */ 86204431Srajenum markertype { 87204431Sraj REF_PHANDLE, 88204431Sraj REF_PATH, 89204431Sraj LABEL, 90204431Sraj}; 91204431Sraj 92204431Srajstruct marker { 93204431Sraj enum markertype type; 94204431Sraj int offset; 95204431Sraj char *ref; 96204431Sraj struct marker *next; 97204431Sraj}; 98204431Sraj 99204431Srajstruct data { 100204431Sraj int len; 101204431Sraj char *val; 102204431Sraj struct marker *markers; 103204431Sraj}; 104204431Sraj 105204431Sraj 106204431Sraj#define empty_data ((struct data){ /* all .members = 0 or NULL */ }) 107204431Sraj 108204431Sraj#define for_each_marker(m) \ 109204431Sraj for (; (m); (m) = (m)->next) 110204431Sraj#define for_each_marker_of_type(m, t) \ 111204431Sraj for_each_marker(m) \ 112204431Sraj if ((m)->type == (t)) 113204431Sraj 114204431Srajvoid data_free(struct data d); 115204431Sraj 116204431Srajstruct data data_grow_for(struct data d, int xlen); 117204431Sraj 118204431Srajstruct data data_copy_mem(const char *mem, int len); 119204431Srajstruct data data_copy_escape_string(const char *s, int len); 120204431Srajstruct data data_copy_file(FILE *f, size_t len); 121204431Sraj 122204431Srajstruct data data_append_data(struct data d, const void *p, int len); 123204431Srajstruct data data_insert_at_marker(struct data d, struct marker *m, 124204431Sraj const void *p, int len); 125204431Srajstruct data data_merge(struct data d1, struct data d2); 126204431Srajstruct data data_append_cell(struct data d, cell_t word); 127204431Srajstruct data data_append_re(struct data d, const struct fdt_reserve_entry *re); 128204431Srajstruct data data_append_addr(struct data d, uint64_t addr); 129204431Srajstruct data data_append_byte(struct data d, uint8_t byte); 130204431Srajstruct data data_append_zeroes(struct data d, int len); 131204431Srajstruct data data_append_align(struct data d, int align); 132204431Sraj 133204431Srajstruct data data_add_marker(struct data d, enum markertype type, char *ref); 134204431Sraj 135204431Srajint data_is_one_string(struct data d); 136204431Sraj 137204431Sraj/* DT constraints */ 138204431Sraj 139204431Sraj#define MAX_PROPNAME_LEN 31 140204431Sraj#define MAX_NODENAME_LEN 31 141204431Sraj 142204431Sraj/* Live trees */ 143204431Srajstruct property { 144204431Sraj char *name; 145204431Sraj struct data val; 146204431Sraj 147204431Sraj struct property *next; 148204431Sraj 149204431Sraj char *label; 150204431Sraj}; 151204431Sraj 152204431Srajstruct node { 153204431Sraj char *name; 154204431Sraj struct property *proplist; 155204431Sraj struct node *children; 156204431Sraj 157204431Sraj struct node *parent; 158204431Sraj struct node *next_sibling; 159204431Sraj 160204431Sraj char *fullpath; 161204431Sraj int basenamelen; 162204431Sraj 163204431Sraj cell_t phandle; 164204431Sraj int addr_cells, size_cells; 165204431Sraj 166204431Sraj char *label; 167204431Sraj}; 168204431Sraj 169204431Sraj#define for_each_property(n, p) \ 170204431Sraj for ((p) = (n)->proplist; (p); (p) = (p)->next) 171204431Sraj 172204431Sraj#define for_each_child(n, c) \ 173204431Sraj for ((c) = (n)->children; (c); (c) = (c)->next_sibling) 174204431Sraj 175204431Srajstruct property *build_property(char *name, struct data val, char *label); 176204431Srajstruct property *chain_property(struct property *first, struct property *list); 177204431Srajstruct property *reverse_properties(struct property *first); 178204431Sraj 179204431Srajstruct node *build_node(struct property *proplist, struct node *children); 180204431Srajstruct node *name_node(struct node *node, char *name, char *label); 181204431Srajstruct node *chain_node(struct node *first, struct node *list); 182204431Sraj 183204431Srajvoid add_property(struct node *node, struct property *prop); 184204431Srajvoid add_child(struct node *parent, struct node *child); 185204431Sraj 186204431Srajconst char *get_unitname(struct node *node); 187204431Srajstruct property *get_property(struct node *node, const char *propname); 188204431Srajcell_t propval_cell(struct property *prop); 189204431Srajstruct node *get_subnode(struct node *node, const char *nodename); 190204431Srajstruct node *get_node_by_path(struct node *tree, const char *path); 191204431Srajstruct node *get_node_by_label(struct node *tree, const char *label); 192204431Srajstruct node *get_node_by_phandle(struct node *tree, cell_t phandle); 193204431Srajstruct node *get_node_by_ref(struct node *tree, const char *ref); 194204431Srajcell_t get_node_phandle(struct node *root, struct node *node); 195204431Sraj 196204431Sraj/* Boot info (tree plus memreserve information */ 197204431Sraj 198204431Srajstruct reserve_info { 199204431Sraj struct fdt_reserve_entry re; 200204431Sraj 201204431Sraj struct reserve_info *next; 202204431Sraj 203204431Sraj char *label; 204204431Sraj}; 205204431Sraj 206204431Srajstruct reserve_info *build_reserve_entry(uint64_t start, uint64_t len, char *label); 207204431Srajstruct reserve_info *chain_reserve_entry(struct reserve_info *first, 208204431Sraj struct reserve_info *list); 209204431Srajstruct reserve_info *add_reserve_entry(struct reserve_info *list, 210204431Sraj struct reserve_info *new); 211204431Sraj 212204431Sraj 213204431Srajstruct boot_info { 214204431Sraj struct reserve_info *reservelist; 215204431Sraj struct node *dt; /* the device tree */ 216204431Sraj uint32_t boot_cpuid_phys; 217204431Sraj}; 218204431Sraj 219204431Srajstruct boot_info *build_boot_info(struct reserve_info *reservelist, 220204431Sraj struct node *tree, uint32_t boot_cpuid_phys); 221204431Sraj 222204431Sraj/* Checks */ 223204431Sraj 224204431Srajvoid process_checks(int force, struct boot_info *bi); 225204431Sraj 226204431Sraj/* Flattened trees */ 227204431Sraj 228204431Srajvoid dt_to_blob(FILE *f, struct boot_info *bi, int version); 229204431Srajvoid dt_to_asm(FILE *f, struct boot_info *bi, int version); 230204431Sraj 231204431Srajstruct boot_info *dt_from_blob(const char *fname); 232204431Sraj 233204431Sraj/* Tree source */ 234204431Sraj 235204431Srajvoid dt_to_source(FILE *f, struct boot_info *bi); 236204431Srajstruct boot_info *dt_from_source(const char *f); 237204431Sraj 238204431Sraj/* FS trees */ 239204431Sraj 240204431Srajstruct boot_info *dt_from_fs(const char *dirname); 241204431Sraj 242204431Sraj/* misc */ 243204431Sraj 244204431Srajchar *join_path(const char *path, const char *name); 245204431Sraj 246204431Sraj#endif /* _DTC_H */ 247