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> 28238742Simp#include <stdbool.h> 29204431Sraj#include <stdarg.h> 30204431Sraj#include <assert.h> 31204431Sraj#include <ctype.h> 32204431Sraj#include <errno.h> 33204431Sraj#include <unistd.h> 34204431Sraj 35204431Sraj#include <libfdt_env.h> 36204431Sraj#include <fdt.h> 37204431Sraj 38204433Sraj#include "util.h" 39204433Sraj 40204433Sraj#ifdef DEBUG 41204433Sraj#define debug(fmt,args...) printf(fmt, ##args) 42204433Sraj#else 43204433Sraj#define debug(fmt,args...) 44204433Sraj#endif 45204433Sraj 46204433Sraj 47204431Sraj#define DEFAULT_FDT_VERSION 17 48204433Sraj 49204431Sraj/* 50204431Sraj * Command line options 51204431Sraj */ 52204431Srajextern int quiet; /* Level of quietness */ 53204431Srajextern int reservenum; /* Number of memory reservation slots */ 54204431Srajextern int minsize; /* Minimum blob size */ 55204431Srajextern int padsize; /* Additional padding to blob */ 56204433Srajextern int phandle_format; /* Use linux,phandle or phandle properties */ 57204431Sraj 58204433Sraj#define PHANDLE_LEGACY 0x1 59204433Sraj#define PHANDLE_EPAPR 0x2 60204433Sraj#define PHANDLE_BOTH 0x3 61204431Sraj 62204431Srajtypedef uint32_t cell_t; 63204431Sraj 64204431Sraj 65204431Sraj#define streq(a, b) (strcmp((a), (b)) == 0) 66204431Sraj#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) 67204431Sraj 68204431Sraj#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 69204431Sraj#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 70204431Sraj 71204431Sraj/* Data blobs */ 72204431Srajenum markertype { 73204431Sraj REF_PHANDLE, 74204431Sraj REF_PATH, 75204431Sraj LABEL, 76204431Sraj}; 77204431Sraj 78204431Srajstruct marker { 79204431Sraj enum markertype type; 80204431Sraj int offset; 81204431Sraj char *ref; 82204431Sraj struct marker *next; 83204431Sraj}; 84204431Sraj 85204431Srajstruct data { 86204431Sraj int len; 87204431Sraj char *val; 88204431Sraj struct marker *markers; 89204431Sraj}; 90204431Sraj 91204431Sraj 92204431Sraj#define empty_data ((struct data){ /* all .members = 0 or NULL */ }) 93204431Sraj 94204431Sraj#define for_each_marker(m) \ 95204431Sraj for (; (m); (m) = (m)->next) 96204431Sraj#define for_each_marker_of_type(m, t) \ 97204431Sraj for_each_marker(m) \ 98204431Sraj if ((m)->type == (t)) 99204431Sraj 100204431Srajvoid data_free(struct data d); 101204431Sraj 102204431Srajstruct data data_grow_for(struct data d, int xlen); 103204431Sraj 104204431Srajstruct data data_copy_mem(const char *mem, int len); 105204431Srajstruct data data_copy_escape_string(const char *s, int len); 106204431Srajstruct data data_copy_file(FILE *f, size_t len); 107204431Sraj 108204431Srajstruct data data_append_data(struct data d, const void *p, int len); 109204431Srajstruct data data_insert_at_marker(struct data d, struct marker *m, 110204431Sraj const void *p, int len); 111204431Srajstruct data data_merge(struct data d1, struct data d2); 112204431Srajstruct data data_append_cell(struct data d, cell_t word); 113238742Simpstruct data data_append_integer(struct data d, uint64_t word, int bits); 114204431Srajstruct data data_append_re(struct data d, const struct fdt_reserve_entry *re); 115204431Srajstruct data data_append_addr(struct data d, uint64_t addr); 116204431Srajstruct data data_append_byte(struct data d, uint8_t byte); 117204431Srajstruct data data_append_zeroes(struct data d, int len); 118204431Srajstruct data data_append_align(struct data d, int align); 119204431Sraj 120204431Srajstruct data data_add_marker(struct data d, enum markertype type, char *ref); 121204431Sraj 122204431Srajint data_is_one_string(struct data d); 123204431Sraj 124204431Sraj/* DT constraints */ 125204431Sraj 126204431Sraj#define MAX_PROPNAME_LEN 31 127204431Sraj#define MAX_NODENAME_LEN 31 128204431Sraj 129204431Sraj/* Live trees */ 130238742Simpstruct label { 131238742Simp char *label; 132238742Simp struct label *next; 133238742Simp}; 134238742Simp 135204431Srajstruct property { 136204431Sraj char *name; 137204431Sraj struct data val; 138204431Sraj 139204431Sraj struct property *next; 140204431Sraj 141238742Simp struct label *labels; 142204431Sraj}; 143204431Sraj 144204431Srajstruct node { 145204431Sraj char *name; 146204431Sraj struct property *proplist; 147204431Sraj struct node *children; 148204431Sraj 149204431Sraj struct node *parent; 150204431Sraj struct node *next_sibling; 151204431Sraj 152204431Sraj char *fullpath; 153204431Sraj int basenamelen; 154204431Sraj 155204431Sraj cell_t phandle; 156204431Sraj int addr_cells, size_cells; 157204431Sraj 158238742Simp struct label *labels; 159204431Sraj}; 160204431Sraj 161238742Simp#define for_each_label(l0, l) \ 162238742Simp for ((l) = (l0); (l); (l) = (l)->next) 163238742Simp 164204431Sraj#define for_each_property(n, p) \ 165204431Sraj for ((p) = (n)->proplist; (p); (p) = (p)->next) 166204431Sraj 167204431Sraj#define for_each_child(n, c) \ 168204431Sraj for ((c) = (n)->children; (c); (c) = (c)->next_sibling) 169204431Sraj 170238742Simpvoid add_label(struct label **labels, char *label); 171238742Simp 172238742Simpstruct property *build_property(char *name, struct data val); 173204431Srajstruct property *chain_property(struct property *first, struct property *list); 174204431Srajstruct property *reverse_properties(struct property *first); 175204431Sraj 176204431Srajstruct node *build_node(struct property *proplist, struct node *children); 177238742Simpstruct node *name_node(struct node *node, char *name); 178204431Srajstruct node *chain_node(struct node *first, struct node *list); 179238742Simpstruct node *merge_nodes(struct node *old_node, struct node *new_node); 180204431Sraj 181204431Srajvoid add_property(struct node *node, struct property *prop); 182204431Srajvoid add_child(struct node *parent, struct node *child); 183204431Sraj 184204431Srajconst char *get_unitname(struct node *node); 185204431Srajstruct property *get_property(struct node *node, const char *propname); 186204431Srajcell_t propval_cell(struct property *prop); 187238742Simpstruct property *get_property_by_label(struct node *tree, const char *label, 188238742Simp struct node **node); 189238742Simpstruct marker *get_marker_label(struct node *tree, const char *label, 190238742Simp struct node **node, struct property **prop); 191204431Srajstruct node *get_subnode(struct node *node, const char *nodename); 192204431Srajstruct node *get_node_by_path(struct node *tree, const char *path); 193204431Srajstruct node *get_node_by_label(struct node *tree, const char *label); 194204431Srajstruct node *get_node_by_phandle(struct node *tree, cell_t phandle); 195204431Srajstruct node *get_node_by_ref(struct node *tree, const char *ref); 196204431Srajcell_t get_node_phandle(struct node *root, struct node *node); 197204431Sraj 198238742Simpuint32_t guess_boot_cpuid(struct node *tree); 199238742Simp 200204431Sraj/* Boot info (tree plus memreserve information */ 201204431Sraj 202204431Srajstruct reserve_info { 203204431Sraj struct fdt_reserve_entry re; 204204431Sraj 205204431Sraj struct reserve_info *next; 206204431Sraj 207238742Simp struct label *labels; 208204431Sraj}; 209204431Sraj 210238742Simpstruct reserve_info *build_reserve_entry(uint64_t start, uint64_t len); 211204431Srajstruct reserve_info *chain_reserve_entry(struct reserve_info *first, 212204431Sraj struct reserve_info *list); 213204431Srajstruct reserve_info *add_reserve_entry(struct reserve_info *list, 214204431Sraj struct reserve_info *new); 215204431Sraj 216204431Sraj 217204431Srajstruct boot_info { 218204431Sraj struct reserve_info *reservelist; 219204431Sraj struct node *dt; /* the device tree */ 220204431Sraj uint32_t boot_cpuid_phys; 221204431Sraj}; 222204431Sraj 223204431Srajstruct boot_info *build_boot_info(struct reserve_info *reservelist, 224204431Sraj struct node *tree, uint32_t boot_cpuid_phys); 225238742Simpvoid sort_tree(struct boot_info *bi); 226204431Sraj 227204431Sraj/* Checks */ 228204431Sraj 229238742Simpvoid parse_checks_option(bool warn, bool error, const char *optarg); 230204431Srajvoid process_checks(int force, struct boot_info *bi); 231204431Sraj 232204431Sraj/* Flattened trees */ 233204431Sraj 234204431Srajvoid dt_to_blob(FILE *f, struct boot_info *bi, int version); 235204431Srajvoid dt_to_asm(FILE *f, struct boot_info *bi, int version); 236204431Sraj 237204431Srajstruct boot_info *dt_from_blob(const char *fname); 238204431Sraj 239204431Sraj/* Tree source */ 240204431Sraj 241204431Srajvoid dt_to_source(FILE *f, struct boot_info *bi); 242204431Srajstruct boot_info *dt_from_source(const char *f); 243204431Sraj 244204431Sraj/* FS trees */ 245204431Sraj 246204431Srajstruct boot_info *dt_from_fs(const char *dirname); 247204431Sraj 248204431Sraj#endif /* _DTC_H */ 249