1238737Simp/* 2238737Simp * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. 3238737Simp * 4238737Simp * Portions from U-Boot cmd_fdt.c (C) Copyright 2007 5238737Simp * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com 6238737Simp * Based on code written by: 7238737Simp * Pantelis Antoniou <pantelis.antoniou@gmail.com> and 8238737Simp * Matthew McClintock <msm@freescale.com> 9238737Simp * 10238737Simp * This program is free software; you can redistribute it and/or 11238737Simp * modify it under the terms of the GNU General Public License as 12238737Simp * published by the Free Software Foundation; either version 2 of 13238737Simp * the License, or (at your option) any later version. 14238737Simp * 15238737Simp * This program is distributed in the hope that it will be useful, 16238737Simp * but WITHOUT ANY WARRANTY; without even the implied warranty of 17238737Simp * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18238737Simp * GNU General Public License for more details. 19238737Simp * 20238737Simp * You should have received a copy of the GNU General Public License 21238737Simp * along with this program; if not, write to the Free Software 22238737Simp * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 23238737Simp * MA 02111-1307 USA 24238737Simp */ 25238737Simp 26238737Simp#include <assert.h> 27238737Simp#include <ctype.h> 28238737Simp#include <getopt.h> 29238737Simp#include <stdio.h> 30238737Simp#include <stdlib.h> 31238737Simp#include <string.h> 32238737Simp 33238737Simp#include <libfdt.h> 34238737Simp 35238737Simp#include "util.h" 36238737Simp 37238737Simpenum display_mode { 38238737Simp MODE_SHOW_VALUE, /* show values for node properties */ 39238737Simp MODE_LIST_PROPS, /* list the properties for a node */ 40238737Simp MODE_LIST_SUBNODES, /* list the subnodes of a node */ 41238737Simp}; 42238737Simp 43238737Simp/* Holds information which controls our output and options */ 44238737Simpstruct display_info { 45238737Simp int type; /* data type (s/i/u/x or 0 for default) */ 46238737Simp int size; /* data size (1/2/4) */ 47238737Simp enum display_mode mode; /* display mode that we are using */ 48238737Simp const char *default_val; /* default value if node/property not found */ 49238737Simp}; 50238737Simp 51238737Simpstatic void report_error(const char *where, int err) 52238737Simp{ 53238737Simp fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err)); 54238737Simp} 55238737Simp 56238737Simp/** 57238737Simp * Displays data of a given length according to selected options 58238737Simp * 59238737Simp * If a specific data type is provided in disp, then this is used. Otherwise 60238737Simp * we try to guess the data type / size from the contents. 61238737Simp * 62238737Simp * @param disp Display information / options 63238737Simp * @param data Data to display 64238737Simp * @param len Maximum length of buffer 65238737Simp * @return 0 if ok, -1 if data does not match format 66238737Simp */ 67238737Simpstatic int show_data(struct display_info *disp, const char *data, int len) 68238737Simp{ 69238737Simp int i, size; 70238737Simp const uint8_t *p = (const uint8_t *)data; 71238737Simp const char *s; 72238737Simp int value; 73238737Simp int is_string; 74238737Simp char fmt[3]; 75238737Simp 76238737Simp /* no data, don't print */ 77238737Simp if (len == 0) 78238737Simp return 0; 79238737Simp 80238737Simp is_string = (disp->type) == 's' || 81238737Simp (!disp->type && util_is_printable_string(data, len)); 82238737Simp if (is_string) { 83238737Simp if (data[len - 1] != '\0') { 84238737Simp fprintf(stderr, "Unterminated string\n"); 85238737Simp return -1; 86238737Simp } 87238737Simp for (s = data; s - data < len; s += strlen(s) + 1) { 88238737Simp if (s != data) 89238737Simp printf(" "); 90238737Simp printf("%s", (const char *)s); 91238737Simp } 92238737Simp return 0; 93238737Simp } 94238737Simp size = disp->size; 95238737Simp if (size == -1) { 96238737Simp size = (len % 4) == 0 ? 4 : 1; 97238737Simp } else if (len % size) { 98238737Simp fprintf(stderr, "Property length must be a multiple of " 99238737Simp "selected data size\n"); 100238737Simp return -1; 101238737Simp } 102238737Simp fmt[0] = '%'; 103238737Simp fmt[1] = disp->type ? disp->type : 'd'; 104238737Simp fmt[2] = '\0'; 105238737Simp for (i = 0; i < len; i += size, p += size) { 106238737Simp if (i) 107238737Simp printf(" "); 108238737Simp value = size == 4 ? fdt32_to_cpu(*(const uint32_t *)p) : 109238737Simp size == 2 ? (*p << 8) | p[1] : *p; 110238737Simp printf(fmt, value); 111238737Simp } 112238737Simp return 0; 113238737Simp} 114238737Simp 115238737Simp/** 116238737Simp * List all properties in a node, one per line. 117238737Simp * 118238737Simp * @param blob FDT blob 119238737Simp * @param node Node to display 120238737Simp * @return 0 if ok, or FDT_ERR... if not. 121238737Simp */ 122238737Simpstatic int list_properties(const void *blob, int node) 123238737Simp{ 124238737Simp const struct fdt_property *data; 125238737Simp const char *name; 126238737Simp int prop; 127238737Simp 128238737Simp prop = fdt_first_property_offset(blob, node); 129238737Simp do { 130238737Simp /* Stop silently when there are no more properties */ 131238737Simp if (prop < 0) 132238737Simp return prop == -FDT_ERR_NOTFOUND ? 0 : prop; 133238737Simp data = fdt_get_property_by_offset(blob, prop, NULL); 134238737Simp name = fdt_string(blob, fdt32_to_cpu(data->nameoff)); 135238737Simp if (name) 136238737Simp puts(name); 137238737Simp prop = fdt_next_property_offset(blob, prop); 138238737Simp } while (1); 139238737Simp} 140238737Simp 141238737Simp#define MAX_LEVEL 32 /* how deeply nested we will go */ 142238737Simp 143238737Simp/** 144238737Simp * List all subnodes in a node, one per line 145238737Simp * 146238737Simp * @param blob FDT blob 147238737Simp * @param node Node to display 148238737Simp * @return 0 if ok, or FDT_ERR... if not. 149238737Simp */ 150238737Simpstatic int list_subnodes(const void *blob, int node) 151238737Simp{ 152238737Simp int nextoffset; /* next node offset from libfdt */ 153238737Simp uint32_t tag; /* current tag */ 154238737Simp int level = 0; /* keep track of nesting level */ 155238737Simp const char *pathp; 156238737Simp int depth = 1; /* the assumed depth of this node */ 157238737Simp 158238737Simp while (level >= 0) { 159238737Simp tag = fdt_next_tag(blob, node, &nextoffset); 160238737Simp switch (tag) { 161238737Simp case FDT_BEGIN_NODE: 162238737Simp pathp = fdt_get_name(blob, node, NULL); 163238737Simp if (level <= depth) { 164238737Simp if (pathp == NULL) 165238737Simp pathp = "/* NULL pointer error */"; 166238737Simp if (*pathp == '\0') 167238737Simp pathp = "/"; /* root is nameless */ 168238737Simp if (level == 1) 169238737Simp puts(pathp); 170238737Simp } 171238737Simp level++; 172238737Simp if (level >= MAX_LEVEL) { 173238737Simp printf("Nested too deep, aborting.\n"); 174238737Simp return 1; 175238737Simp } 176238737Simp break; 177238737Simp case FDT_END_NODE: 178238737Simp level--; 179238737Simp if (level == 0) 180238737Simp level = -1; /* exit the loop */ 181238737Simp break; 182238737Simp case FDT_END: 183238737Simp return 1; 184238737Simp case FDT_PROP: 185238737Simp break; 186238737Simp default: 187238737Simp if (level <= depth) 188238737Simp printf("Unknown tag 0x%08X\n", tag); 189238737Simp return 1; 190238737Simp } 191238737Simp node = nextoffset; 192238737Simp } 193238737Simp return 0; 194238737Simp} 195238737Simp 196238737Simp/** 197238737Simp * Show the data for a given node (and perhaps property) according to the 198238737Simp * display option provided. 199238737Simp * 200238737Simp * @param blob FDT blob 201238737Simp * @param disp Display information / options 202238737Simp * @param node Node to display 203238737Simp * @param property Name of property to display, or NULL if none 204238737Simp * @return 0 if ok, -ve on error 205238737Simp */ 206238737Simpstatic int show_data_for_item(const void *blob, struct display_info *disp, 207238737Simp int node, const char *property) 208238737Simp{ 209238737Simp const void *value = NULL; 210238737Simp int len, err = 0; 211238737Simp 212238737Simp switch (disp->mode) { 213238737Simp case MODE_LIST_PROPS: 214238737Simp err = list_properties(blob, node); 215238737Simp break; 216238737Simp 217238737Simp case MODE_LIST_SUBNODES: 218238737Simp err = list_subnodes(blob, node); 219238737Simp break; 220238737Simp 221238737Simp default: 222238737Simp assert(property); 223238737Simp value = fdt_getprop(blob, node, property, &len); 224238737Simp if (value) { 225238737Simp if (show_data(disp, value, len)) 226238737Simp err = -1; 227238737Simp else 228238737Simp printf("\n"); 229238737Simp } else if (disp->default_val) { 230238737Simp puts(disp->default_val); 231238737Simp } else { 232238737Simp report_error(property, len); 233238737Simp err = -1; 234238737Simp } 235238737Simp break; 236238737Simp } 237238737Simp 238238737Simp return err; 239238737Simp} 240238737Simp 241238737Simp/** 242238737Simp * Run the main fdtget operation, given a filename and valid arguments 243238737Simp * 244238737Simp * @param disp Display information / options 245238737Simp * @param filename Filename of blob file 246238737Simp * @param arg List of arguments to process 247238737Simp * @param arg_count Number of arguments 248238737Simp * @param return 0 if ok, -ve on error 249238737Simp */ 250238737Simpstatic int do_fdtget(struct display_info *disp, const char *filename, 251238737Simp char **arg, int arg_count, int args_per_step) 252238737Simp{ 253238737Simp char *blob; 254238737Simp const char *prop; 255238737Simp int i, node; 256238737Simp 257238737Simp blob = utilfdt_read(filename); 258238737Simp if (!blob) 259238737Simp return -1; 260238737Simp 261238737Simp for (i = 0; i + args_per_step <= arg_count; i += args_per_step) { 262238737Simp node = fdt_path_offset(blob, arg[i]); 263238737Simp if (node < 0) { 264238737Simp if (disp->default_val) { 265238737Simp puts(disp->default_val); 266238737Simp continue; 267238737Simp } else { 268238737Simp report_error(arg[i], node); 269318102Sgonzo free(blob); 270238737Simp return -1; 271238737Simp } 272238737Simp } 273238737Simp prop = args_per_step == 1 ? NULL : arg[i + 1]; 274238737Simp 275318102Sgonzo if (show_data_for_item(blob, disp, node, prop)) { 276318102Sgonzo free(blob); 277238737Simp return -1; 278318102Sgonzo } 279238737Simp } 280318102Sgonzo 281318102Sgonzo free(blob); 282318102Sgonzo 283238737Simp return 0; 284238737Simp} 285238737Simp 286261215Simp/* Usage related data. */ 287261215Simpstatic const char usage_synopsis[] = 288261215Simp "read values from device tree\n" 289238737Simp " fdtget <options> <dt file> [<node> <property>]...\n" 290238737Simp " fdtget -p <options> <dt file> [<node> ]...\n" 291261215Simp "\n" 292261215Simp "Each value is printed on a new line.\n" 293238737Simp USAGE_TYPE_MSG; 294261215Simpstatic const char usage_short_opts[] = "t:pld:" USAGE_COMMON_SHORT_OPTS; 295261215Simpstatic struct option const usage_long_opts[] = { 296261215Simp {"type", a_argument, NULL, 't'}, 297261215Simp {"properties", no_argument, NULL, 'p'}, 298261215Simp {"list", no_argument, NULL, 'l'}, 299261215Simp {"default", a_argument, NULL, 'd'}, 300261215Simp USAGE_COMMON_LONG_OPTS, 301261215Simp}; 302261215Simpstatic const char * const usage_opts_help[] = { 303261215Simp "Type of data", 304261215Simp "List properties for each node", 305261215Simp "List subnodes for each node", 306261215Simp "Default value to display when the property is missing", 307261215Simp USAGE_COMMON_OPTS_HELP 308261215Simp}; 309238737Simp 310238737Simpint main(int argc, char *argv[]) 311238737Simp{ 312261215Simp int opt; 313238737Simp char *filename = NULL; 314238737Simp struct display_info disp; 315238737Simp int args_per_step = 2; 316238737Simp 317238737Simp /* set defaults */ 318238737Simp memset(&disp, '\0', sizeof(disp)); 319238737Simp disp.size = -1; 320238737Simp disp.mode = MODE_SHOW_VALUE; 321261215Simp while ((opt = util_getopt_long()) != EOF) { 322261215Simp switch (opt) { 323261215Simp case_USAGE_COMMON_FLAGS 324238737Simp 325238737Simp case 't': 326238737Simp if (utilfdt_decode_type(optarg, &disp.type, 327238737Simp &disp.size)) 328261215Simp usage("invalid type string"); 329238737Simp break; 330238737Simp 331238737Simp case 'p': 332238737Simp disp.mode = MODE_LIST_PROPS; 333238737Simp args_per_step = 1; 334238737Simp break; 335238737Simp 336238737Simp case 'l': 337238737Simp disp.mode = MODE_LIST_SUBNODES; 338238737Simp args_per_step = 1; 339238737Simp break; 340238737Simp 341238737Simp case 'd': 342238737Simp disp.default_val = optarg; 343238737Simp break; 344238737Simp } 345238737Simp } 346238737Simp 347238737Simp if (optind < argc) 348238737Simp filename = argv[optind++]; 349238737Simp if (!filename) 350261215Simp usage("missing filename"); 351238737Simp 352238737Simp argv += optind; 353238737Simp argc -= optind; 354238737Simp 355238737Simp /* Allow no arguments, and silently succeed */ 356238737Simp if (!argc) 357238737Simp return 0; 358238737Simp 359238737Simp /* Check for node, property arguments */ 360238737Simp if (args_per_step == 2 && (argc % 2)) 361261215Simp usage("must have an even number of arguments"); 362238737Simp 363238737Simp if (do_fdtget(&disp, filename, argv, argc, args_per_step)) 364238737Simp return 1; 365238737Simp return 0; 366238737Simp} 367