183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22e192b24SSimon Glass /*
32e192b24SSimon Glass * (C) Copyright 2007
42e192b24SSimon Glass * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
52e192b24SSimon Glass * Based on code written by:
62e192b24SSimon Glass * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
72e192b24SSimon Glass * Matthew McClintock <msm@freescale.com>
82e192b24SSimon Glass */
92e192b24SSimon Glass
102e192b24SSimon Glass #include <common.h>
112e192b24SSimon Glass #include <command.h>
122e192b24SSimon Glass #include <linux/ctype.h>
132e192b24SSimon Glass #include <linux/types.h>
142e192b24SSimon Glass #include <asm/global_data.h>
15b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
162e192b24SSimon Glass #include <fdt_support.h>
172e192b24SSimon Glass #include <mapmem.h>
182e192b24SSimon Glass #include <asm/io.h>
192e192b24SSimon Glass
202e192b24SSimon Glass #define MAX_LEVEL 32 /* how deeply nested we will go */
212e192b24SSimon Glass #define SCRATCHPAD 1024 /* bytes of scratchpad memory */
225d927b42SSimon Glass #define CMD_FDT_MAX_DUMP 64
232e192b24SSimon Glass
242e192b24SSimon Glass /*
252e192b24SSimon Glass * Global data (for the gd->bd)
262e192b24SSimon Glass */
272e192b24SSimon Glass DECLARE_GLOBAL_DATA_PTR;
282e192b24SSimon Glass
292e192b24SSimon Glass static int fdt_valid(struct fdt_header **blobp);
302e192b24SSimon Glass static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
312e192b24SSimon Glass static int fdt_print(const char *pathp, char *prop, int depth);
322e192b24SSimon Glass static int is_printable_string(const void *data, int len);
332e192b24SSimon Glass
342e192b24SSimon Glass /*
352e192b24SSimon Glass * The working_fdt points to our working flattened device tree.
362e192b24SSimon Glass */
372e192b24SSimon Glass struct fdt_header *working_fdt;
382e192b24SSimon Glass
set_working_fdt_addr(ulong addr)392e192b24SSimon Glass void set_working_fdt_addr(ulong addr)
402e192b24SSimon Glass {
412e192b24SSimon Glass void *buf;
422e192b24SSimon Glass
432e192b24SSimon Glass buf = map_sysmem(addr, 0);
442e192b24SSimon Glass working_fdt = buf;
45018f5303SSimon Glass env_set_hex("fdtaddr", addr);
462e192b24SSimon Glass }
472e192b24SSimon Glass
482e192b24SSimon Glass /*
492e192b24SSimon Glass * Get a value from the fdt and format it to be set in the environment
502e192b24SSimon Glass */
fdt_value_env_set(const void * nodep,int len,const char * var)51382bee57SSimon Glass static int fdt_value_env_set(const void *nodep, int len, const char *var)
522e192b24SSimon Glass {
532e192b24SSimon Glass if (is_printable_string(nodep, len))
54382bee57SSimon Glass env_set(var, (void *)nodep);
552e192b24SSimon Glass else if (len == 4) {
562e192b24SSimon Glass char buf[11];
572e192b24SSimon Glass
58b05bf6c7SAndreas Färber sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
59382bee57SSimon Glass env_set(var, buf);
602e192b24SSimon Glass } else if (len%4 == 0 && len <= 20) {
612e192b24SSimon Glass /* Needed to print things like sha1 hashes. */
622e192b24SSimon Glass char buf[41];
632e192b24SSimon Glass int i;
642e192b24SSimon Glass
652e192b24SSimon Glass for (i = 0; i < len; i += sizeof(unsigned int))
662e192b24SSimon Glass sprintf(buf + (i * 2), "%08x",
672e192b24SSimon Glass *(unsigned int *)(nodep + i));
68382bee57SSimon Glass env_set(var, buf);
692e192b24SSimon Glass } else {
702e192b24SSimon Glass printf("error: unprintable value\n");
712e192b24SSimon Glass return 1;
722e192b24SSimon Glass }
732e192b24SSimon Glass return 0;
742e192b24SSimon Glass }
752e192b24SSimon Glass
76*8244127dSHeiko Schocher static const char * const fdt_member_table[] = {
77*8244127dSHeiko Schocher "magic",
78*8244127dSHeiko Schocher "totalsize",
79*8244127dSHeiko Schocher "off_dt_struct",
80*8244127dSHeiko Schocher "off_dt_strings",
81*8244127dSHeiko Schocher "off_mem_rsvmap",
82*8244127dSHeiko Schocher "version",
83*8244127dSHeiko Schocher "last_comp_version",
84*8244127dSHeiko Schocher "boot_cpuid_phys",
85*8244127dSHeiko Schocher "size_dt_strings",
86*8244127dSHeiko Schocher "size_dt_struct",
87*8244127dSHeiko Schocher };
88*8244127dSHeiko Schocher
fdt_get_header_value(int argc,char * const argv[])89*8244127dSHeiko Schocher static int fdt_get_header_value(int argc, char * const argv[])
90*8244127dSHeiko Schocher {
91*8244127dSHeiko Schocher fdt32_t *fdtp = (fdt32_t *)working_fdt;
92*8244127dSHeiko Schocher ulong val;
93*8244127dSHeiko Schocher int i;
94*8244127dSHeiko Schocher
95*8244127dSHeiko Schocher if (argv[2][0] != 'g')
96*8244127dSHeiko Schocher return CMD_RET_FAILURE;
97*8244127dSHeiko Schocher
98*8244127dSHeiko Schocher for (i = 0; i < ARRAY_SIZE(fdt_member_table); i++) {
99*8244127dSHeiko Schocher if (strcmp(fdt_member_table[i], argv[4]))
100*8244127dSHeiko Schocher continue;
101*8244127dSHeiko Schocher
102*8244127dSHeiko Schocher val = fdt32_to_cpu(fdtp[i]);
103*8244127dSHeiko Schocher env_set_hex(argv[3], val);
104*8244127dSHeiko Schocher return CMD_RET_SUCCESS;
105*8244127dSHeiko Schocher }
106*8244127dSHeiko Schocher
107*8244127dSHeiko Schocher return CMD_RET_FAILURE;
108*8244127dSHeiko Schocher }
109*8244127dSHeiko Schocher
1102e192b24SSimon Glass /*
1112e192b24SSimon Glass * Flattened Device Tree command, see the help for parameter definitions.
1122e192b24SSimon Glass */
do_fdt(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1132e192b24SSimon Glass static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1142e192b24SSimon Glass {
1152e192b24SSimon Glass if (argc < 2)
1162e192b24SSimon Glass return CMD_RET_USAGE;
1172e192b24SSimon Glass
1182e192b24SSimon Glass /*
1192e192b24SSimon Glass * Set the address of the fdt
1202e192b24SSimon Glass */
121f0ed68e2SMaxime Ripard if (strncmp(argv[1], "ad", 2) == 0) {
1222e192b24SSimon Glass unsigned long addr;
1232e192b24SSimon Glass int control = 0;
1242e192b24SSimon Glass struct fdt_header *blob;
1252e192b24SSimon Glass /*
1262e192b24SSimon Glass * Set the address [and length] of the fdt.
1272e192b24SSimon Glass */
1282e192b24SSimon Glass argc -= 2;
1292e192b24SSimon Glass argv += 2;
1302e192b24SSimon Glass /* Temporary #ifdef - some archs don't have fdt_blob yet */
1312e192b24SSimon Glass #ifdef CONFIG_OF_CONTROL
1322e192b24SSimon Glass if (argc && !strcmp(*argv, "-c")) {
1332e192b24SSimon Glass control = 1;
1342e192b24SSimon Glass argc--;
1352e192b24SSimon Glass argv++;
1362e192b24SSimon Glass }
1372e192b24SSimon Glass #endif
1382e192b24SSimon Glass if (argc == 0) {
1392e192b24SSimon Glass if (control)
1402e192b24SSimon Glass blob = (struct fdt_header *)gd->fdt_blob;
1412e192b24SSimon Glass else
1422e192b24SSimon Glass blob = working_fdt;
1432e192b24SSimon Glass if (!blob || !fdt_valid(&blob))
1442e192b24SSimon Glass return 1;
1452e192b24SSimon Glass printf("The address of the fdt is %#08lx\n",
1462e192b24SSimon Glass control ? (ulong)map_to_sysmem(blob) :
147bfebc8c9SSimon Glass env_get_hex("fdtaddr", 0));
1482e192b24SSimon Glass return 0;
1492e192b24SSimon Glass }
1502e192b24SSimon Glass
1512e192b24SSimon Glass addr = simple_strtoul(argv[0], NULL, 16);
1522e192b24SSimon Glass blob = map_sysmem(addr, 0);
1532e192b24SSimon Glass if (!fdt_valid(&blob))
1542e192b24SSimon Glass return 1;
1552e192b24SSimon Glass if (control)
1562e192b24SSimon Glass gd->fdt_blob = blob;
1572e192b24SSimon Glass else
1582e192b24SSimon Glass set_working_fdt_addr(addr);
1592e192b24SSimon Glass
1602e192b24SSimon Glass if (argc >= 2) {
1612e192b24SSimon Glass int len;
1622e192b24SSimon Glass int err;
1632e192b24SSimon Glass /*
1642e192b24SSimon Glass * Optional new length
1652e192b24SSimon Glass */
1662e192b24SSimon Glass len = simple_strtoul(argv[1], NULL, 16);
1672e192b24SSimon Glass if (len < fdt_totalsize(blob)) {
1682e192b24SSimon Glass printf ("New length %d < existing length %d, "
1692e192b24SSimon Glass "ignoring.\n",
1702e192b24SSimon Glass len, fdt_totalsize(blob));
1712e192b24SSimon Glass } else {
1722e192b24SSimon Glass /*
1732e192b24SSimon Glass * Open in place with a new length.
1742e192b24SSimon Glass */
1752e192b24SSimon Glass err = fdt_open_into(blob, blob, len);
1762e192b24SSimon Glass if (err != 0) {
1772e192b24SSimon Glass printf ("libfdt fdt_open_into(): %s\n",
1782e192b24SSimon Glass fdt_strerror(err));
1792e192b24SSimon Glass }
1802e192b24SSimon Glass }
1812e192b24SSimon Glass }
1822e192b24SSimon Glass
1832e192b24SSimon Glass return CMD_RET_SUCCESS;
1842e192b24SSimon Glass }
1852e192b24SSimon Glass
1862e192b24SSimon Glass if (!working_fdt) {
1872e192b24SSimon Glass puts(
1882e192b24SSimon Glass "No FDT memory address configured. Please configure\n"
1892e192b24SSimon Glass "the FDT address via \"fdt addr <address>\" command.\n"
1902e192b24SSimon Glass "Aborting!\n");
1912e192b24SSimon Glass return CMD_RET_FAILURE;
1922e192b24SSimon Glass }
1932e192b24SSimon Glass
1942e192b24SSimon Glass /*
1952e192b24SSimon Glass * Move the working_fdt
1962e192b24SSimon Glass */
1972e192b24SSimon Glass if (strncmp(argv[1], "mo", 2) == 0) {
1982e192b24SSimon Glass struct fdt_header *newaddr;
1992e192b24SSimon Glass int len;
2002e192b24SSimon Glass int err;
2012e192b24SSimon Glass
2022e192b24SSimon Glass if (argc < 4)
2032e192b24SSimon Glass return CMD_RET_USAGE;
2042e192b24SSimon Glass
2052e192b24SSimon Glass /*
2062e192b24SSimon Glass * Set the address and length of the fdt.
2072e192b24SSimon Glass */
2082e192b24SSimon Glass working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
2092e192b24SSimon Glass if (!fdt_valid(&working_fdt))
2102e192b24SSimon Glass return 1;
2112e192b24SSimon Glass
2122e192b24SSimon Glass newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
2132e192b24SSimon Glass
2142e192b24SSimon Glass /*
2152e192b24SSimon Glass * If the user specifies a length, use that. Otherwise use the
2162e192b24SSimon Glass * current length.
2172e192b24SSimon Glass */
2182e192b24SSimon Glass if (argc <= 4) {
2192e192b24SSimon Glass len = fdt_totalsize(working_fdt);
2202e192b24SSimon Glass } else {
2212e192b24SSimon Glass len = simple_strtoul(argv[4], NULL, 16);
2222e192b24SSimon Glass if (len < fdt_totalsize(working_fdt)) {
2232e192b24SSimon Glass printf ("New length 0x%X < existing length "
2242e192b24SSimon Glass "0x%X, aborting.\n",
2252e192b24SSimon Glass len, fdt_totalsize(working_fdt));
2262e192b24SSimon Glass return 1;
2272e192b24SSimon Glass }
2282e192b24SSimon Glass }
2292e192b24SSimon Glass
2302e192b24SSimon Glass /*
2312e192b24SSimon Glass * Copy to the new location.
2322e192b24SSimon Glass */
2332e192b24SSimon Glass err = fdt_open_into(working_fdt, newaddr, len);
2342e192b24SSimon Glass if (err != 0) {
2352e192b24SSimon Glass printf ("libfdt fdt_open_into(): %s\n",
2362e192b24SSimon Glass fdt_strerror(err));
2372e192b24SSimon Glass return 1;
2382e192b24SSimon Glass }
239b1a7e799SHiroyuki Yokoyama set_working_fdt_addr((ulong)newaddr);
240f7f191eeSFabien Parent #ifdef CONFIG_OF_SYSTEM_SETUP
241f7f191eeSFabien Parent /* Call the board-specific fixup routine */
242f7f191eeSFabien Parent } else if (strncmp(argv[1], "sys", 3) == 0) {
243f7f191eeSFabien Parent int err = ft_system_setup(working_fdt, gd->bd);
2442e192b24SSimon Glass
245f7f191eeSFabien Parent if (err) {
246f7f191eeSFabien Parent printf("Failed to add system information to FDT: %s\n",
247f7f191eeSFabien Parent fdt_strerror(err));
248f7f191eeSFabien Parent return CMD_RET_FAILURE;
249f7f191eeSFabien Parent }
250f7f191eeSFabien Parent #endif
2512e192b24SSimon Glass /*
2522e192b24SSimon Glass * Make a new node
2532e192b24SSimon Glass */
2542e192b24SSimon Glass } else if (strncmp(argv[1], "mk", 2) == 0) {
2552e192b24SSimon Glass char *pathp; /* path */
2562e192b24SSimon Glass char *nodep; /* new node to add */
2572e192b24SSimon Glass int nodeoffset; /* node offset from libfdt */
2582e192b24SSimon Glass int err;
2592e192b24SSimon Glass
2602e192b24SSimon Glass /*
2612e192b24SSimon Glass * Parameters: Node path, new node to be appended to the path.
2622e192b24SSimon Glass */
2632e192b24SSimon Glass if (argc < 4)
2642e192b24SSimon Glass return CMD_RET_USAGE;
2652e192b24SSimon Glass
2662e192b24SSimon Glass pathp = argv[2];
2672e192b24SSimon Glass nodep = argv[3];
2682e192b24SSimon Glass
2692e192b24SSimon Glass nodeoffset = fdt_path_offset (working_fdt, pathp);
2702e192b24SSimon Glass if (nodeoffset < 0) {
2712e192b24SSimon Glass /*
2722e192b24SSimon Glass * Not found or something else bad happened.
2732e192b24SSimon Glass */
2742e192b24SSimon Glass printf ("libfdt fdt_path_offset() returned %s\n",
2752e192b24SSimon Glass fdt_strerror(nodeoffset));
2762e192b24SSimon Glass return 1;
2772e192b24SSimon Glass }
2782e192b24SSimon Glass err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
2792e192b24SSimon Glass if (err < 0) {
2802e192b24SSimon Glass printf ("libfdt fdt_add_subnode(): %s\n",
2812e192b24SSimon Glass fdt_strerror(err));
2822e192b24SSimon Glass return 1;
2832e192b24SSimon Glass }
2842e192b24SSimon Glass
2852e192b24SSimon Glass /*
2862e192b24SSimon Glass * Set the value of a property in the working_fdt.
2872e192b24SSimon Glass */
2882e192b24SSimon Glass } else if (argv[1][0] == 's') {
2892e192b24SSimon Glass char *pathp; /* path */
2902e192b24SSimon Glass char *prop; /* property */
2912e192b24SSimon Glass int nodeoffset; /* node offset from libfdt */
2926dfd65f8SBernhard Messerklinger static char data[SCRATCHPAD] __aligned(4);/* property storage */
2939620d872SHannes Schmelzer const void *ptmp;
2942e192b24SSimon Glass int len; /* new length of the property */
2952e192b24SSimon Glass int ret; /* return value */
2962e192b24SSimon Glass
2972e192b24SSimon Glass /*
2982e192b24SSimon Glass * Parameters: Node path, property, optional value.
2992e192b24SSimon Glass */
3002e192b24SSimon Glass if (argc < 4)
3012e192b24SSimon Glass return CMD_RET_USAGE;
3022e192b24SSimon Glass
3032e192b24SSimon Glass pathp = argv[2];
3042e192b24SSimon Glass prop = argv[3];
3052e192b24SSimon Glass
3062e192b24SSimon Glass nodeoffset = fdt_path_offset (working_fdt, pathp);
3072e192b24SSimon Glass if (nodeoffset < 0) {
3082e192b24SSimon Glass /*
3092e192b24SSimon Glass * Not found or something else bad happened.
3102e192b24SSimon Glass */
3112e192b24SSimon Glass printf ("libfdt fdt_path_offset() returned %s\n",
3122e192b24SSimon Glass fdt_strerror(nodeoffset));
3132e192b24SSimon Glass return 1;
3142e192b24SSimon Glass }
3152e192b24SSimon Glass
3169620d872SHannes Schmelzer if (argc == 4) {
3179620d872SHannes Schmelzer len = 0;
3189620d872SHannes Schmelzer } else {
3199620d872SHannes Schmelzer ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len);
3209620d872SHannes Schmelzer if (len > SCRATCHPAD) {
3219620d872SHannes Schmelzer printf("prop (%d) doesn't fit in scratchpad!\n",
3229620d872SHannes Schmelzer len);
3239620d872SHannes Schmelzer return 1;
3249620d872SHannes Schmelzer }
325cee8c35dSHannes Schmelzer if (ptmp != NULL)
3269620d872SHannes Schmelzer memcpy(data, ptmp, len);
327cee8c35dSHannes Schmelzer
3289620d872SHannes Schmelzer ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
3299620d872SHannes Schmelzer if (ret != 0)
3309620d872SHannes Schmelzer return ret;
3319620d872SHannes Schmelzer }
3329620d872SHannes Schmelzer
3332e192b24SSimon Glass ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
3342e192b24SSimon Glass if (ret < 0) {
3352e192b24SSimon Glass printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
3362e192b24SSimon Glass return 1;
3372e192b24SSimon Glass }
3382e192b24SSimon Glass
3392e192b24SSimon Glass /********************************************************************
3402e192b24SSimon Glass * Get the value of a property in the working_fdt.
3412e192b24SSimon Glass ********************************************************************/
3422e192b24SSimon Glass } else if (argv[1][0] == 'g') {
3432e192b24SSimon Glass char *subcmd; /* sub-command */
3442e192b24SSimon Glass char *pathp; /* path */
3452e192b24SSimon Glass char *prop; /* property */
3462e192b24SSimon Glass char *var; /* variable to store result */
3472e192b24SSimon Glass int nodeoffset; /* node offset from libfdt */
3482e192b24SSimon Glass const void *nodep; /* property node pointer */
3492e192b24SSimon Glass int len = 0; /* new length of the property */
3502e192b24SSimon Glass
3512e192b24SSimon Glass /*
3522e192b24SSimon Glass * Parameters: Node path, property, optional value.
3532e192b24SSimon Glass */
3542e192b24SSimon Glass if (argc < 5)
3552e192b24SSimon Glass return CMD_RET_USAGE;
3562e192b24SSimon Glass
3572e192b24SSimon Glass subcmd = argv[2];
3582e192b24SSimon Glass
3592e192b24SSimon Glass if (argc < 6 && subcmd[0] != 's')
3602e192b24SSimon Glass return CMD_RET_USAGE;
3612e192b24SSimon Glass
3622e192b24SSimon Glass var = argv[3];
3632e192b24SSimon Glass pathp = argv[4];
3642e192b24SSimon Glass prop = argv[5];
3652e192b24SSimon Glass
3662e192b24SSimon Glass nodeoffset = fdt_path_offset(working_fdt, pathp);
3672e192b24SSimon Glass if (nodeoffset < 0) {
3682e192b24SSimon Glass /*
3692e192b24SSimon Glass * Not found or something else bad happened.
3702e192b24SSimon Glass */
3712e192b24SSimon Glass printf("libfdt fdt_path_offset() returned %s\n",
3722e192b24SSimon Glass fdt_strerror(nodeoffset));
3732e192b24SSimon Glass return 1;
3742e192b24SSimon Glass }
3752e192b24SSimon Glass
3762e192b24SSimon Glass if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
3772e192b24SSimon Glass int reqIndex = -1;
3782e192b24SSimon Glass int startDepth = fdt_node_depth(
3792e192b24SSimon Glass working_fdt, nodeoffset);
3802e192b24SSimon Glass int curDepth = startDepth;
3812e192b24SSimon Glass int curIndex = -1;
3822e192b24SSimon Glass int nextNodeOffset = fdt_next_node(
3832e192b24SSimon Glass working_fdt, nodeoffset, &curDepth);
3842e192b24SSimon Glass
3852e192b24SSimon Glass if (subcmd[0] == 'n')
3862e192b24SSimon Glass reqIndex = simple_strtoul(argv[5], NULL, 16);
3872e192b24SSimon Glass
3882e192b24SSimon Glass while (curDepth > startDepth) {
3892e192b24SSimon Glass if (curDepth == startDepth + 1)
3902e192b24SSimon Glass curIndex++;
3912e192b24SSimon Glass if (subcmd[0] == 'n' && curIndex == reqIndex) {
392382bee57SSimon Glass const char *node_name;
3932e192b24SSimon Glass
394382bee57SSimon Glass node_name = fdt_get_name(working_fdt,
395382bee57SSimon Glass nextNodeOffset,
396382bee57SSimon Glass NULL);
397382bee57SSimon Glass env_set(var, node_name);
3982e192b24SSimon Glass return 0;
3992e192b24SSimon Glass }
4002e192b24SSimon Glass nextNodeOffset = fdt_next_node(
4012e192b24SSimon Glass working_fdt, nextNodeOffset, &curDepth);
4022e192b24SSimon Glass if (nextNodeOffset < 0)
4032e192b24SSimon Glass break;
4042e192b24SSimon Glass }
4052e192b24SSimon Glass if (subcmd[0] == 's') {
4062e192b24SSimon Glass /* get the num nodes at this level */
407018f5303SSimon Glass env_set_ulong(var, curIndex + 1);
4082e192b24SSimon Glass } else {
4092e192b24SSimon Glass /* node index not found */
4102e192b24SSimon Glass printf("libfdt node not found\n");
4112e192b24SSimon Glass return 1;
4122e192b24SSimon Glass }
4132e192b24SSimon Glass } else {
4142e192b24SSimon Glass nodep = fdt_getprop(
4152e192b24SSimon Glass working_fdt, nodeoffset, prop, &len);
4162e192b24SSimon Glass if (len == 0) {
4172e192b24SSimon Glass /* no property value */
418382bee57SSimon Glass env_set(var, "");
4192e192b24SSimon Glass return 0;
42072c98ed1SSimon Glass } else if (nodep && len > 0) {
4212e192b24SSimon Glass if (subcmd[0] == 'v') {
4222e192b24SSimon Glass int ret;
4232e192b24SSimon Glass
424382bee57SSimon Glass ret = fdt_value_env_set(nodep, len,
425382bee57SSimon Glass var);
4262e192b24SSimon Glass if (ret != 0)
4272e192b24SSimon Glass return ret;
4282e192b24SSimon Glass } else if (subcmd[0] == 'a') {
4292e192b24SSimon Glass /* Get address */
4302e192b24SSimon Glass char buf[11];
4312e192b24SSimon Glass
4322e192b24SSimon Glass sprintf(buf, "0x%p", nodep);
433382bee57SSimon Glass env_set(var, buf);
4342e192b24SSimon Glass } else if (subcmd[0] == 's') {
4352e192b24SSimon Glass /* Get size */
4362e192b24SSimon Glass char buf[11];
4372e192b24SSimon Glass
4382e192b24SSimon Glass sprintf(buf, "0x%08X", len);
439382bee57SSimon Glass env_set(var, buf);
4402e192b24SSimon Glass } else
4412e192b24SSimon Glass return CMD_RET_USAGE;
4422e192b24SSimon Glass return 0;
4432e192b24SSimon Glass } else {
4442e192b24SSimon Glass printf("libfdt fdt_getprop(): %s\n",
4452e192b24SSimon Glass fdt_strerror(len));
4462e192b24SSimon Glass return 1;
4472e192b24SSimon Glass }
4482e192b24SSimon Glass }
4492e192b24SSimon Glass
4502e192b24SSimon Glass /*
4512e192b24SSimon Glass * Print (recursive) / List (single level)
4522e192b24SSimon Glass */
4532e192b24SSimon Glass } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
4542e192b24SSimon Glass int depth = MAX_LEVEL; /* how deep to print */
4552e192b24SSimon Glass char *pathp; /* path */
4562e192b24SSimon Glass char *prop; /* property */
4572e192b24SSimon Glass int ret; /* return value */
4582e192b24SSimon Glass static char root[2] = "/";
4592e192b24SSimon Glass
4602e192b24SSimon Glass /*
4612e192b24SSimon Glass * list is an alias for print, but limited to 1 level
4622e192b24SSimon Glass */
4632e192b24SSimon Glass if (argv[1][0] == 'l') {
4642e192b24SSimon Glass depth = 1;
4652e192b24SSimon Glass }
4662e192b24SSimon Glass
4672e192b24SSimon Glass /*
4682e192b24SSimon Glass * Get the starting path. The root node is an oddball,
4692e192b24SSimon Glass * the offset is zero and has no name.
4702e192b24SSimon Glass */
4712e192b24SSimon Glass if (argc == 2)
4722e192b24SSimon Glass pathp = root;
4732e192b24SSimon Glass else
4742e192b24SSimon Glass pathp = argv[2];
4752e192b24SSimon Glass if (argc > 3)
4762e192b24SSimon Glass prop = argv[3];
4772e192b24SSimon Glass else
4782e192b24SSimon Glass prop = NULL;
4792e192b24SSimon Glass
4802e192b24SSimon Glass ret = fdt_print(pathp, prop, depth);
4812e192b24SSimon Glass if (ret != 0)
4822e192b24SSimon Glass return ret;
4832e192b24SSimon Glass
4842e192b24SSimon Glass /*
4852e192b24SSimon Glass * Remove a property/node
4862e192b24SSimon Glass */
4872e192b24SSimon Glass } else if (strncmp(argv[1], "rm", 2) == 0) {
4882e192b24SSimon Glass int nodeoffset; /* node offset from libfdt */
4892e192b24SSimon Glass int err;
4902e192b24SSimon Glass
4912e192b24SSimon Glass /*
4922e192b24SSimon Glass * Get the path. The root node is an oddball, the offset
4932e192b24SSimon Glass * is zero and has no name.
4942e192b24SSimon Glass */
4952e192b24SSimon Glass nodeoffset = fdt_path_offset (working_fdt, argv[2]);
4962e192b24SSimon Glass if (nodeoffset < 0) {
4972e192b24SSimon Glass /*
4982e192b24SSimon Glass * Not found or something else bad happened.
4992e192b24SSimon Glass */
5002e192b24SSimon Glass printf ("libfdt fdt_path_offset() returned %s\n",
5012e192b24SSimon Glass fdt_strerror(nodeoffset));
5022e192b24SSimon Glass return 1;
5032e192b24SSimon Glass }
5042e192b24SSimon Glass /*
5052e192b24SSimon Glass * Do the delete. A fourth parameter means delete a property,
5062e192b24SSimon Glass * otherwise delete the node.
5072e192b24SSimon Glass */
5082e192b24SSimon Glass if (argc > 3) {
5092e192b24SSimon Glass err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
5102e192b24SSimon Glass if (err < 0) {
5112e192b24SSimon Glass printf("libfdt fdt_delprop(): %s\n",
5122e192b24SSimon Glass fdt_strerror(err));
5132e192b24SSimon Glass return err;
5142e192b24SSimon Glass }
5152e192b24SSimon Glass } else {
5162e192b24SSimon Glass err = fdt_del_node(working_fdt, nodeoffset);
5172e192b24SSimon Glass if (err < 0) {
5182e192b24SSimon Glass printf("libfdt fdt_del_node(): %s\n",
5192e192b24SSimon Glass fdt_strerror(err));
5202e192b24SSimon Glass return err;
5212e192b24SSimon Glass }
5222e192b24SSimon Glass }
5232e192b24SSimon Glass
5242e192b24SSimon Glass /*
5252e192b24SSimon Glass * Display header info
5262e192b24SSimon Glass */
5272e192b24SSimon Glass } else if (argv[1][0] == 'h') {
528*8244127dSHeiko Schocher if (argc == 5)
529*8244127dSHeiko Schocher return fdt_get_header_value(argc, argv);
530*8244127dSHeiko Schocher
5312e192b24SSimon Glass u32 version = fdt_version(working_fdt);
5322e192b24SSimon Glass printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
5332e192b24SSimon Glass printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
5342e192b24SSimon Glass fdt_totalsize(working_fdt));
5352e192b24SSimon Glass printf("off_dt_struct:\t\t0x%x\n",
5362e192b24SSimon Glass fdt_off_dt_struct(working_fdt));
5372e192b24SSimon Glass printf("off_dt_strings:\t\t0x%x\n",
5382e192b24SSimon Glass fdt_off_dt_strings(working_fdt));
5392e192b24SSimon Glass printf("off_mem_rsvmap:\t\t0x%x\n",
5402e192b24SSimon Glass fdt_off_mem_rsvmap(working_fdt));
5412e192b24SSimon Glass printf("version:\t\t%d\n", version);
5422e192b24SSimon Glass printf("last_comp_version:\t%d\n",
5432e192b24SSimon Glass fdt_last_comp_version(working_fdt));
5442e192b24SSimon Glass if (version >= 2)
5452e192b24SSimon Glass printf("boot_cpuid_phys:\t0x%x\n",
5462e192b24SSimon Glass fdt_boot_cpuid_phys(working_fdt));
5472e192b24SSimon Glass if (version >= 3)
5482e192b24SSimon Glass printf("size_dt_strings:\t0x%x\n",
5492e192b24SSimon Glass fdt_size_dt_strings(working_fdt));
5502e192b24SSimon Glass if (version >= 17)
5512e192b24SSimon Glass printf("size_dt_struct:\t\t0x%x\n",
5522e192b24SSimon Glass fdt_size_dt_struct(working_fdt));
5532e192b24SSimon Glass printf("number mem_rsv:\t\t0x%x\n",
5542e192b24SSimon Glass fdt_num_mem_rsv(working_fdt));
5552e192b24SSimon Glass printf("\n");
5562e192b24SSimon Glass
5572e192b24SSimon Glass /*
5582e192b24SSimon Glass * Set boot cpu id
5592e192b24SSimon Glass */
5602e192b24SSimon Glass } else if (strncmp(argv[1], "boo", 3) == 0) {
5612e192b24SSimon Glass unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
5622e192b24SSimon Glass fdt_set_boot_cpuid_phys(working_fdt, tmp);
5632e192b24SSimon Glass
5642e192b24SSimon Glass /*
5652e192b24SSimon Glass * memory command
5662e192b24SSimon Glass */
5672e192b24SSimon Glass } else if (strncmp(argv[1], "me", 2) == 0) {
5682e192b24SSimon Glass uint64_t addr, size;
5692e192b24SSimon Glass int err;
5702e192b24SSimon Glass addr = simple_strtoull(argv[2], NULL, 16);
5712e192b24SSimon Glass size = simple_strtoull(argv[3], NULL, 16);
5722e192b24SSimon Glass err = fdt_fixup_memory(working_fdt, addr, size);
5732e192b24SSimon Glass if (err < 0)
5742e192b24SSimon Glass return err;
5752e192b24SSimon Glass
5762e192b24SSimon Glass /*
5772e192b24SSimon Glass * mem reserve commands
5782e192b24SSimon Glass */
5792e192b24SSimon Glass } else if (strncmp(argv[1], "rs", 2) == 0) {
5802e192b24SSimon Glass if (argv[2][0] == 'p') {
5812e192b24SSimon Glass uint64_t addr, size;
5822e192b24SSimon Glass int total = fdt_num_mem_rsv(working_fdt);
5832e192b24SSimon Glass int j, err;
5842e192b24SSimon Glass printf("index\t\t start\t\t size\n");
5852e192b24SSimon Glass printf("-------------------------------"
5862e192b24SSimon Glass "-----------------\n");
5872e192b24SSimon Glass for (j = 0; j < total; j++) {
5882e192b24SSimon Glass err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
5892e192b24SSimon Glass if (err < 0) {
5902e192b24SSimon Glass printf("libfdt fdt_get_mem_rsv(): %s\n",
5912e192b24SSimon Glass fdt_strerror(err));
5922e192b24SSimon Glass return err;
5932e192b24SSimon Glass }
5942e192b24SSimon Glass printf(" %x\t%08x%08x\t%08x%08x\n", j,
5952e192b24SSimon Glass (u32)(addr >> 32),
5962e192b24SSimon Glass (u32)(addr & 0xffffffff),
5972e192b24SSimon Glass (u32)(size >> 32),
5982e192b24SSimon Glass (u32)(size & 0xffffffff));
5992e192b24SSimon Glass }
6002e192b24SSimon Glass } else if (argv[2][0] == 'a') {
6012e192b24SSimon Glass uint64_t addr, size;
6022e192b24SSimon Glass int err;
6032e192b24SSimon Glass addr = simple_strtoull(argv[3], NULL, 16);
6042e192b24SSimon Glass size = simple_strtoull(argv[4], NULL, 16);
6052e192b24SSimon Glass err = fdt_add_mem_rsv(working_fdt, addr, size);
6062e192b24SSimon Glass
6072e192b24SSimon Glass if (err < 0) {
6082e192b24SSimon Glass printf("libfdt fdt_add_mem_rsv(): %s\n",
6092e192b24SSimon Glass fdt_strerror(err));
6102e192b24SSimon Glass return err;
6112e192b24SSimon Glass }
6122e192b24SSimon Glass } else if (argv[2][0] == 'd') {
6132e192b24SSimon Glass unsigned long idx = simple_strtoul(argv[3], NULL, 16);
6142e192b24SSimon Glass int err = fdt_del_mem_rsv(working_fdt, idx);
6152e192b24SSimon Glass
6162e192b24SSimon Glass if (err < 0) {
6172e192b24SSimon Glass printf("libfdt fdt_del_mem_rsv(): %s\n",
6182e192b24SSimon Glass fdt_strerror(err));
6192e192b24SSimon Glass return err;
6202e192b24SSimon Glass }
6212e192b24SSimon Glass } else {
6222e192b24SSimon Glass /* Unrecognized command */
6232e192b24SSimon Glass return CMD_RET_USAGE;
6242e192b24SSimon Glass }
6252e192b24SSimon Glass }
6262e192b24SSimon Glass #ifdef CONFIG_OF_BOARD_SETUP
6272e192b24SSimon Glass /* Call the board-specific fixup routine */
6282e192b24SSimon Glass else if (strncmp(argv[1], "boa", 3) == 0) {
6292e192b24SSimon Glass int err = ft_board_setup(working_fdt, gd->bd);
6302e192b24SSimon Glass
6312e192b24SSimon Glass if (err) {
6322e192b24SSimon Glass printf("Failed to update board information in FDT: %s\n",
6332e192b24SSimon Glass fdt_strerror(err));
6342e192b24SSimon Glass return CMD_RET_FAILURE;
6352e192b24SSimon Glass }
6362c76d311SNicholas Faustini #ifdef CONFIG_SOC_KEYSTONE
6372c76d311SNicholas Faustini ft_board_setup_ex(working_fdt, gd->bd);
6382c76d311SNicholas Faustini #endif
6392e192b24SSimon Glass }
6402e192b24SSimon Glass #endif
6412e192b24SSimon Glass /* Create a chosen node */
6422e192b24SSimon Glass else if (strncmp(argv[1], "cho", 3) == 0) {
6432e192b24SSimon Glass unsigned long initrd_start = 0, initrd_end = 0;
6442e192b24SSimon Glass
6452e192b24SSimon Glass if ((argc != 2) && (argc != 4))
6462e192b24SSimon Glass return CMD_RET_USAGE;
6472e192b24SSimon Glass
6482e192b24SSimon Glass if (argc == 4) {
6492e192b24SSimon Glass initrd_start = simple_strtoul(argv[2], NULL, 16);
6502e192b24SSimon Glass initrd_end = simple_strtoul(argv[3], NULL, 16);
6512e192b24SSimon Glass }
6522e192b24SSimon Glass
6532e192b24SSimon Glass fdt_chosen(working_fdt);
6542e192b24SSimon Glass fdt_initrd(working_fdt, initrd_start, initrd_end);
6552e192b24SSimon Glass
6562e192b24SSimon Glass #if defined(CONFIG_FIT_SIGNATURE)
6572e192b24SSimon Glass } else if (strncmp(argv[1], "che", 3) == 0) {
6582e192b24SSimon Glass int cfg_noffset;
6592e192b24SSimon Glass int ret;
6602e192b24SSimon Glass unsigned long addr;
6612e192b24SSimon Glass struct fdt_header *blob;
6622e192b24SSimon Glass
6632e192b24SSimon Glass if (!working_fdt)
6642e192b24SSimon Glass return CMD_RET_FAILURE;
6652e192b24SSimon Glass
6662e192b24SSimon Glass if (argc > 2) {
6672e192b24SSimon Glass addr = simple_strtoul(argv[2], NULL, 16);
6682e192b24SSimon Glass blob = map_sysmem(addr, 0);
6692e192b24SSimon Glass } else {
6702e192b24SSimon Glass blob = (struct fdt_header *)gd->fdt_blob;
6712e192b24SSimon Glass }
6722e192b24SSimon Glass if (!fdt_valid(&blob))
6732e192b24SSimon Glass return 1;
6742e192b24SSimon Glass
6752e192b24SSimon Glass gd->fdt_blob = blob;
6762e192b24SSimon Glass cfg_noffset = fit_conf_get_node(working_fdt, NULL);
6772e192b24SSimon Glass if (!cfg_noffset) {
6782e192b24SSimon Glass printf("Could not find configuration node: %s\n",
6792e192b24SSimon Glass fdt_strerror(cfg_noffset));
6802e192b24SSimon Glass return CMD_RET_FAILURE;
6812e192b24SSimon Glass }
6822e192b24SSimon Glass
6832e192b24SSimon Glass ret = fit_config_verify(working_fdt, cfg_noffset);
6842e192b24SSimon Glass if (ret == 0)
6852e192b24SSimon Glass return CMD_RET_SUCCESS;
6862e192b24SSimon Glass else
6872e192b24SSimon Glass return CMD_RET_FAILURE;
6882e192b24SSimon Glass #endif
6892e192b24SSimon Glass
6902e192b24SSimon Glass }
691e6628ad7SMaxime Ripard #ifdef CONFIG_OF_LIBFDT_OVERLAY
692e6628ad7SMaxime Ripard /* apply an overlay */
693e6628ad7SMaxime Ripard else if (strncmp(argv[1], "ap", 2) == 0) {
694e6628ad7SMaxime Ripard unsigned long addr;
695e6628ad7SMaxime Ripard struct fdt_header *blob;
696082b1414SStefan Agner int ret;
697e6628ad7SMaxime Ripard
698e6628ad7SMaxime Ripard if (argc != 3)
699e6628ad7SMaxime Ripard return CMD_RET_USAGE;
700e6628ad7SMaxime Ripard
701e6628ad7SMaxime Ripard if (!working_fdt)
702e6628ad7SMaxime Ripard return CMD_RET_FAILURE;
703e6628ad7SMaxime Ripard
704e6628ad7SMaxime Ripard addr = simple_strtoul(argv[2], NULL, 16);
705e6628ad7SMaxime Ripard blob = map_sysmem(addr, 0);
706e6628ad7SMaxime Ripard if (!fdt_valid(&blob))
707e6628ad7SMaxime Ripard return CMD_RET_FAILURE;
708e6628ad7SMaxime Ripard
70981ecc5d9SPantelis Antoniou /* apply method prints messages on error */
71081ecc5d9SPantelis Antoniou ret = fdt_overlay_apply_verbose(working_fdt, blob);
71181ecc5d9SPantelis Antoniou if (ret)
712e6628ad7SMaxime Ripard return CMD_RET_FAILURE;
713e6628ad7SMaxime Ripard }
714e6628ad7SMaxime Ripard #endif
7152e192b24SSimon Glass /* resize the fdt */
7162e192b24SSimon Glass else if (strncmp(argv[1], "re", 2) == 0) {
717ef476836SHannes Schmelzer uint extrasize;
718ef476836SHannes Schmelzer if (argc > 2)
719ef476836SHannes Schmelzer extrasize = simple_strtoul(argv[2], NULL, 16);
720ef476836SHannes Schmelzer else
721ef476836SHannes Schmelzer extrasize = 0;
722ef476836SHannes Schmelzer fdt_shrink_to_minimum(working_fdt, extrasize);
7232e192b24SSimon Glass }
7242e192b24SSimon Glass else {
7252e192b24SSimon Glass /* Unrecognized command */
7262e192b24SSimon Glass return CMD_RET_USAGE;
7272e192b24SSimon Glass }
7282e192b24SSimon Glass
7292e192b24SSimon Glass return 0;
7302e192b24SSimon Glass }
7312e192b24SSimon Glass
7322e192b24SSimon Glass /****************************************************************************/
7332e192b24SSimon Glass
7342e192b24SSimon Glass /**
7352e192b24SSimon Glass * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
7362e192b24SSimon Glass *
7372e192b24SSimon Glass * @blobp: Pointer to FDT pointer
7382e192b24SSimon Glass * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
7392e192b24SSimon Glass */
fdt_valid(struct fdt_header ** blobp)7402e192b24SSimon Glass static int fdt_valid(struct fdt_header **blobp)
7412e192b24SSimon Glass {
7422e192b24SSimon Glass const void *blob = *blobp;
7432e192b24SSimon Glass int err;
7442e192b24SSimon Glass
7452e192b24SSimon Glass if (blob == NULL) {
7462e192b24SSimon Glass printf ("The address of the fdt is invalid (NULL).\n");
7472e192b24SSimon Glass return 0;
7482e192b24SSimon Glass }
7492e192b24SSimon Glass
7502e192b24SSimon Glass err = fdt_check_header(blob);
7512e192b24SSimon Glass if (err == 0)
7522e192b24SSimon Glass return 1; /* valid */
7532e192b24SSimon Glass
7542e192b24SSimon Glass if (err < 0) {
7552e192b24SSimon Glass printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
7562e192b24SSimon Glass /*
7572e192b24SSimon Glass * Be more informative on bad version.
7582e192b24SSimon Glass */
7592e192b24SSimon Glass if (err == -FDT_ERR_BADVERSION) {
7602e192b24SSimon Glass if (fdt_version(blob) <
7612e192b24SSimon Glass FDT_FIRST_SUPPORTED_VERSION) {
7622e192b24SSimon Glass printf (" - too old, fdt %d < %d",
7632e192b24SSimon Glass fdt_version(blob),
7642e192b24SSimon Glass FDT_FIRST_SUPPORTED_VERSION);
7652e192b24SSimon Glass }
7662e192b24SSimon Glass if (fdt_last_comp_version(blob) >
7672e192b24SSimon Glass FDT_LAST_SUPPORTED_VERSION) {
7682e192b24SSimon Glass printf (" - too new, fdt %d > %d",
7692e192b24SSimon Glass fdt_version(blob),
7702e192b24SSimon Glass FDT_LAST_SUPPORTED_VERSION);
7712e192b24SSimon Glass }
7722e192b24SSimon Glass }
7732e192b24SSimon Glass printf("\n");
7742e192b24SSimon Glass *blobp = NULL;
7752e192b24SSimon Glass return 0;
7762e192b24SSimon Glass }
7772e192b24SSimon Glass return 1;
7782e192b24SSimon Glass }
7792e192b24SSimon Glass
7802e192b24SSimon Glass /****************************************************************************/
7812e192b24SSimon Glass
7822e192b24SSimon Glass /*
7832e192b24SSimon Glass * Parse the user's input, partially heuristic. Valid formats:
7842e192b24SSimon Glass * <0x00112233 4 05> - an array of cells. Numbers follow standard
7852e192b24SSimon Glass * C conventions.
7862e192b24SSimon Glass * [00 11 22 .. nn] - byte stream
7872e192b24SSimon Glass * "string" - If the the value doesn't start with "<" or "[", it is
7882e192b24SSimon Glass * treated as a string. Note that the quotes are
7892e192b24SSimon Glass * stripped by the parser before we get the string.
7902e192b24SSimon Glass * newval: An array of strings containing the new property as specified
7912e192b24SSimon Glass * on the command line
7922e192b24SSimon Glass * count: The number of strings in the array
7932e192b24SSimon Glass * data: A bytestream to be placed in the property
7942e192b24SSimon Glass * len: The length of the resulting bytestream
7952e192b24SSimon Glass */
fdt_parse_prop(char * const * newval,int count,char * data,int * len)7962e192b24SSimon Glass static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
7972e192b24SSimon Glass {
7982e192b24SSimon Glass char *cp; /* temporary char pointer */
7992e192b24SSimon Glass char *newp; /* temporary newval char pointer */
8002e192b24SSimon Glass unsigned long tmp; /* holds converted values */
8012e192b24SSimon Glass int stridx = 0;
8022e192b24SSimon Glass
8032e192b24SSimon Glass *len = 0;
8042e192b24SSimon Glass newp = newval[0];
8052e192b24SSimon Glass
8062e192b24SSimon Glass /* An array of cells */
8072e192b24SSimon Glass if (*newp == '<') {
8082e192b24SSimon Glass newp++;
8092e192b24SSimon Glass while ((*newp != '>') && (stridx < count)) {
8102e192b24SSimon Glass /*
8112e192b24SSimon Glass * Keep searching until we find that last ">"
8122e192b24SSimon Glass * That way users don't have to escape the spaces
8132e192b24SSimon Glass */
8142e192b24SSimon Glass if (*newp == '\0') {
8152e192b24SSimon Glass newp = newval[++stridx];
8162e192b24SSimon Glass continue;
8172e192b24SSimon Glass }
8182e192b24SSimon Glass
8192e192b24SSimon Glass cp = newp;
8202e192b24SSimon Glass tmp = simple_strtoul(cp, &newp, 0);
8219620d872SHannes Schmelzer if (*cp != '?')
822b05bf6c7SAndreas Färber *(fdt32_t *)data = cpu_to_fdt32(tmp);
8239620d872SHannes Schmelzer else
8249620d872SHannes Schmelzer newp++;
8259620d872SHannes Schmelzer
8262e192b24SSimon Glass data += 4;
8272e192b24SSimon Glass *len += 4;
8282e192b24SSimon Glass
8292e192b24SSimon Glass /* If the ptr didn't advance, something went wrong */
8302e192b24SSimon Glass if ((newp - cp) <= 0) {
8312e192b24SSimon Glass printf("Sorry, I could not convert \"%s\"\n",
8322e192b24SSimon Glass cp);
8332e192b24SSimon Glass return 1;
8342e192b24SSimon Glass }
8352e192b24SSimon Glass
8362e192b24SSimon Glass while (*newp == ' ')
8372e192b24SSimon Glass newp++;
8382e192b24SSimon Glass }
8392e192b24SSimon Glass
8402e192b24SSimon Glass if (*newp != '>') {
8412e192b24SSimon Glass printf("Unexpected character '%c'\n", *newp);
8422e192b24SSimon Glass return 1;
8432e192b24SSimon Glass }
8442e192b24SSimon Glass } else if (*newp == '[') {
8452e192b24SSimon Glass /*
8462e192b24SSimon Glass * Byte stream. Convert the values.
8472e192b24SSimon Glass */
8482e192b24SSimon Glass newp++;
8492e192b24SSimon Glass while ((stridx < count) && (*newp != ']')) {
8502e192b24SSimon Glass while (*newp == ' ')
8512e192b24SSimon Glass newp++;
8522e192b24SSimon Glass if (*newp == '\0') {
8532e192b24SSimon Glass newp = newval[++stridx];
8542e192b24SSimon Glass continue;
8552e192b24SSimon Glass }
8562e192b24SSimon Glass if (!isxdigit(*newp))
8572e192b24SSimon Glass break;
8582e192b24SSimon Glass tmp = simple_strtoul(newp, &newp, 16);
8592e192b24SSimon Glass *data++ = tmp & 0xFF;
8602e192b24SSimon Glass *len = *len + 1;
8612e192b24SSimon Glass }
8622e192b24SSimon Glass if (*newp != ']') {
8632e192b24SSimon Glass printf("Unexpected character '%c'\n", *newp);
8642e192b24SSimon Glass return 1;
8652e192b24SSimon Glass }
8662e192b24SSimon Glass } else {
8672e192b24SSimon Glass /*
8682e192b24SSimon Glass * Assume it is one or more strings. Copy it into our
8692e192b24SSimon Glass * data area for convenience (including the
8702e192b24SSimon Glass * terminating '\0's).
8712e192b24SSimon Glass */
8722e192b24SSimon Glass while (stridx < count) {
8732e192b24SSimon Glass size_t length = strlen(newp) + 1;
8742e192b24SSimon Glass strcpy(data, newp);
8752e192b24SSimon Glass data += length;
8762e192b24SSimon Glass *len += length;
8772e192b24SSimon Glass newp = newval[++stridx];
8782e192b24SSimon Glass }
8792e192b24SSimon Glass }
8802e192b24SSimon Glass return 0;
8812e192b24SSimon Glass }
8822e192b24SSimon Glass
8832e192b24SSimon Glass /****************************************************************************/
8842e192b24SSimon Glass
8852e192b24SSimon Glass /*
8862e192b24SSimon Glass * Heuristic to guess if this is a string or concatenated strings.
8872e192b24SSimon Glass */
8882e192b24SSimon Glass
is_printable_string(const void * data,int len)8892e192b24SSimon Glass static int is_printable_string(const void *data, int len)
8902e192b24SSimon Glass {
8912e192b24SSimon Glass const char *s = data;
8922e192b24SSimon Glass
8932e192b24SSimon Glass /* zero length is not */
8942e192b24SSimon Glass if (len == 0)
8952e192b24SSimon Glass return 0;
8962e192b24SSimon Glass
8972e192b24SSimon Glass /* must terminate with zero or '\n' */
8982e192b24SSimon Glass if (s[len - 1] != '\0' && s[len - 1] != '\n')
8992e192b24SSimon Glass return 0;
9002e192b24SSimon Glass
9012e192b24SSimon Glass /* printable or a null byte (concatenated strings) */
9022e192b24SSimon Glass while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
9032e192b24SSimon Glass /*
9042e192b24SSimon Glass * If we see a null, there are three possibilities:
9052e192b24SSimon Glass * 1) If len == 1, it is the end of the string, printable
9062e192b24SSimon Glass * 2) Next character also a null, not printable.
9072e192b24SSimon Glass * 3) Next character not a null, continue to check.
9082e192b24SSimon Glass */
9092e192b24SSimon Glass if (s[0] == '\0') {
9102e192b24SSimon Glass if (len == 1)
9112e192b24SSimon Glass return 1;
9122e192b24SSimon Glass if (s[1] == '\0')
9132e192b24SSimon Glass return 0;
9142e192b24SSimon Glass }
9152e192b24SSimon Glass s++;
9162e192b24SSimon Glass len--;
9172e192b24SSimon Glass }
9182e192b24SSimon Glass
9192e192b24SSimon Glass /* Not the null termination, or not done yet: not printable */
9202e192b24SSimon Glass if (*s != '\0' || (len != 0))
9212e192b24SSimon Glass return 0;
9222e192b24SSimon Glass
9232e192b24SSimon Glass return 1;
9242e192b24SSimon Glass }
9252e192b24SSimon Glass
9262e192b24SSimon Glass
9272e192b24SSimon Glass /*
9282e192b24SSimon Glass * Print the property in the best format, a heuristic guess. Print as
9292e192b24SSimon Glass * a string, concatenated strings, a byte, word, double word, or (if all
9302e192b24SSimon Glass * else fails) it is printed as a stream of bytes.
9312e192b24SSimon Glass */
print_data(const void * data,int len)9322e192b24SSimon Glass static void print_data(const void *data, int len)
9332e192b24SSimon Glass {
9342e192b24SSimon Glass int j;
9352e192b24SSimon Glass
9362e192b24SSimon Glass /* no data, don't print */
9372e192b24SSimon Glass if (len == 0)
9382e192b24SSimon Glass return;
9392e192b24SSimon Glass
9402e192b24SSimon Glass /*
9412e192b24SSimon Glass * It is a string, but it may have multiple strings (embedded '\0's).
9422e192b24SSimon Glass */
9432e192b24SSimon Glass if (is_printable_string(data, len)) {
9442e192b24SSimon Glass puts("\"");
9452e192b24SSimon Glass j = 0;
9462e192b24SSimon Glass while (j < len) {
9472e192b24SSimon Glass if (j > 0)
9482e192b24SSimon Glass puts("\", \"");
9492e192b24SSimon Glass puts(data);
9502e192b24SSimon Glass j += strlen(data) + 1;
9512e192b24SSimon Glass data += strlen(data) + 1;
9522e192b24SSimon Glass }
9532e192b24SSimon Glass puts("\"");
9542e192b24SSimon Glass return;
9552e192b24SSimon Glass }
9562e192b24SSimon Glass
9572e192b24SSimon Glass if ((len %4) == 0) {
9585d927b42SSimon Glass if (len > CMD_FDT_MAX_DUMP)
9592e192b24SSimon Glass printf("* 0x%p [0x%08x]", data, len);
9602e192b24SSimon Glass else {
9612e192b24SSimon Glass const __be32 *p;
9622e192b24SSimon Glass
9632e192b24SSimon Glass printf("<");
9642e192b24SSimon Glass for (j = 0, p = data; j < len/4; j++)
9652e192b24SSimon Glass printf("0x%08x%s", fdt32_to_cpu(p[j]),
9662e192b24SSimon Glass j < (len/4 - 1) ? " " : "");
9672e192b24SSimon Glass printf(">");
9682e192b24SSimon Glass }
9692e192b24SSimon Glass } else { /* anything else... hexdump */
9705d927b42SSimon Glass if (len > CMD_FDT_MAX_DUMP)
9712e192b24SSimon Glass printf("* 0x%p [0x%08x]", data, len);
9722e192b24SSimon Glass else {
9732e192b24SSimon Glass const u8 *s;
9742e192b24SSimon Glass
9752e192b24SSimon Glass printf("[");
9762e192b24SSimon Glass for (j = 0, s = data; j < len; j++)
9772e192b24SSimon Glass printf("%02x%s", s[j], j < len - 1 ? " " : "");
9782e192b24SSimon Glass printf("]");
9792e192b24SSimon Glass }
9802e192b24SSimon Glass }
9812e192b24SSimon Glass }
9822e192b24SSimon Glass
9832e192b24SSimon Glass /****************************************************************************/
9842e192b24SSimon Glass
9852e192b24SSimon Glass /*
9862e192b24SSimon Glass * Recursively print (a portion of) the working_fdt. The depth parameter
9872e192b24SSimon Glass * determines how deeply nested the fdt is printed.
9882e192b24SSimon Glass */
fdt_print(const char * pathp,char * prop,int depth)9892e192b24SSimon Glass static int fdt_print(const char *pathp, char *prop, int depth)
9902e192b24SSimon Glass {
9912e192b24SSimon Glass static char tabs[MAX_LEVEL+1] =
9922e192b24SSimon Glass "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
9932e192b24SSimon Glass "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
9942e192b24SSimon Glass const void *nodep; /* property node pointer */
9952e192b24SSimon Glass int nodeoffset; /* node offset from libfdt */
9962e192b24SSimon Glass int nextoffset; /* next node offset from libfdt */
9972e192b24SSimon Glass uint32_t tag; /* tag */
9982e192b24SSimon Glass int len; /* length of the property */
9992e192b24SSimon Glass int level = 0; /* keep track of nesting level */
10002e192b24SSimon Glass const struct fdt_property *fdt_prop;
10012e192b24SSimon Glass
10022e192b24SSimon Glass nodeoffset = fdt_path_offset (working_fdt, pathp);
10032e192b24SSimon Glass if (nodeoffset < 0) {
10042e192b24SSimon Glass /*
10052e192b24SSimon Glass * Not found or something else bad happened.
10062e192b24SSimon Glass */
10072e192b24SSimon Glass printf ("libfdt fdt_path_offset() returned %s\n",
10082e192b24SSimon Glass fdt_strerror(nodeoffset));
10092e192b24SSimon Glass return 1;
10102e192b24SSimon Glass }
10112e192b24SSimon Glass /*
10122e192b24SSimon Glass * The user passed in a property as well as node path.
10132e192b24SSimon Glass * Print only the given property and then return.
10142e192b24SSimon Glass */
10152e192b24SSimon Glass if (prop) {
10162e192b24SSimon Glass nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
10172e192b24SSimon Glass if (len == 0) {
10182e192b24SSimon Glass /* no property value */
10192e192b24SSimon Glass printf("%s %s\n", pathp, prop);
10202e192b24SSimon Glass return 0;
10219f952672SSimon Glass } else if (nodep && len > 0) {
10222e192b24SSimon Glass printf("%s = ", prop);
10232e192b24SSimon Glass print_data (nodep, len);
10242e192b24SSimon Glass printf("\n");
10252e192b24SSimon Glass return 0;
10262e192b24SSimon Glass } else {
10272e192b24SSimon Glass printf ("libfdt fdt_getprop(): %s\n",
10282e192b24SSimon Glass fdt_strerror(len));
10292e192b24SSimon Glass return 1;
10302e192b24SSimon Glass }
10312e192b24SSimon Glass }
10322e192b24SSimon Glass
10332e192b24SSimon Glass /*
10342e192b24SSimon Glass * The user passed in a node path and no property,
10352e192b24SSimon Glass * print the node and all subnodes.
10362e192b24SSimon Glass */
10372e192b24SSimon Glass while(level >= 0) {
10382e192b24SSimon Glass tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
10392e192b24SSimon Glass switch(tag) {
10402e192b24SSimon Glass case FDT_BEGIN_NODE:
10412e192b24SSimon Glass pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
10422e192b24SSimon Glass if (level <= depth) {
10432e192b24SSimon Glass if (pathp == NULL)
10442e192b24SSimon Glass pathp = "/* NULL pointer error */";
10452e192b24SSimon Glass if (*pathp == '\0')
10462e192b24SSimon Glass pathp = "/"; /* root is nameless */
10472e192b24SSimon Glass printf("%s%s {\n",
10482e192b24SSimon Glass &tabs[MAX_LEVEL - level], pathp);
10492e192b24SSimon Glass }
10502e192b24SSimon Glass level++;
10512e192b24SSimon Glass if (level >= MAX_LEVEL) {
10522e192b24SSimon Glass printf("Nested too deep, aborting.\n");
10532e192b24SSimon Glass return 1;
10542e192b24SSimon Glass }
10552e192b24SSimon Glass break;
10562e192b24SSimon Glass case FDT_END_NODE:
10572e192b24SSimon Glass level--;
10582e192b24SSimon Glass if (level <= depth)
10592e192b24SSimon Glass printf("%s};\n", &tabs[MAX_LEVEL - level]);
10602e192b24SSimon Glass if (level == 0) {
10612e192b24SSimon Glass level = -1; /* exit the loop */
10622e192b24SSimon Glass }
10632e192b24SSimon Glass break;
10642e192b24SSimon Glass case FDT_PROP:
10652e192b24SSimon Glass fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
10662e192b24SSimon Glass sizeof(*fdt_prop));
10672e192b24SSimon Glass pathp = fdt_string(working_fdt,
10682e192b24SSimon Glass fdt32_to_cpu(fdt_prop->nameoff));
10692e192b24SSimon Glass len = fdt32_to_cpu(fdt_prop->len);
10702e192b24SSimon Glass nodep = fdt_prop->data;
10712e192b24SSimon Glass if (len < 0) {
10722e192b24SSimon Glass printf ("libfdt fdt_getprop(): %s\n",
10732e192b24SSimon Glass fdt_strerror(len));
10742e192b24SSimon Glass return 1;
10752e192b24SSimon Glass } else if (len == 0) {
10762e192b24SSimon Glass /* the property has no value */
10772e192b24SSimon Glass if (level <= depth)
10782e192b24SSimon Glass printf("%s%s;\n",
10792e192b24SSimon Glass &tabs[MAX_LEVEL - level],
10802e192b24SSimon Glass pathp);
10812e192b24SSimon Glass } else {
10822e192b24SSimon Glass if (level <= depth) {
10832e192b24SSimon Glass printf("%s%s = ",
10842e192b24SSimon Glass &tabs[MAX_LEVEL - level],
10852e192b24SSimon Glass pathp);
10862e192b24SSimon Glass print_data (nodep, len);
10872e192b24SSimon Glass printf(";\n");
10882e192b24SSimon Glass }
10892e192b24SSimon Glass }
10902e192b24SSimon Glass break;
10912e192b24SSimon Glass case FDT_NOP:
10922e192b24SSimon Glass printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
10932e192b24SSimon Glass break;
10942e192b24SSimon Glass case FDT_END:
10952e192b24SSimon Glass return 1;
10962e192b24SSimon Glass default:
10972e192b24SSimon Glass if (level <= depth)
10982e192b24SSimon Glass printf("Unknown tag 0x%08X\n", tag);
10992e192b24SSimon Glass return 1;
11002e192b24SSimon Glass }
11012e192b24SSimon Glass nodeoffset = nextoffset;
11022e192b24SSimon Glass }
11032e192b24SSimon Glass return 0;
11042e192b24SSimon Glass }
11052e192b24SSimon Glass
11062e192b24SSimon Glass /********************************************************************/
11072e192b24SSimon Glass #ifdef CONFIG_SYS_LONGHELP
11082e192b24SSimon Glass static char fdt_help_text[] =
11092e192b24SSimon Glass "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n"
1110e6628ad7SMaxime Ripard #ifdef CONFIG_OF_LIBFDT_OVERLAY
1111e6628ad7SMaxime Ripard "fdt apply <addr> - Apply overlay to the DT\n"
1112e6628ad7SMaxime Ripard #endif
11132e192b24SSimon Glass #ifdef CONFIG_OF_BOARD_SETUP
11142e192b24SSimon Glass "fdt boardsetup - Do board-specific set up\n"
11152e192b24SSimon Glass #endif
11162e192b24SSimon Glass #ifdef CONFIG_OF_SYSTEM_SETUP
11172e192b24SSimon Glass "fdt systemsetup - Do system-specific set up\n"
11182e192b24SSimon Glass #endif
11192e192b24SSimon Glass "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
1120ef476836SHannes Schmelzer "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
11212e192b24SSimon Glass "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
11222e192b24SSimon Glass "fdt list <path> [<prop>] - Print one level starting at <path>\n"
11232e192b24SSimon Glass "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
11242e192b24SSimon Glass "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
11252e192b24SSimon Glass "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
11262e192b24SSimon Glass "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
11272e192b24SSimon Glass "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
11282e192b24SSimon Glass "fdt mknode <path> <node> - Create a new node after <path>\n"
11292e192b24SSimon Glass "fdt rm <path> [<prop>] - Delete the node or <property>\n"
1130*8244127dSHeiko Schocher "fdt header [get <var> <member>] - Display header info\n"
1131*8244127dSHeiko Schocher " get - get header member <member> and store it in <var>\n"
11322e192b24SSimon Glass "fdt bootcpu <id> - Set boot cpuid\n"
11332e192b24SSimon Glass "fdt memory <addr> <size> - Add/Update memory node\n"
11342e192b24SSimon Glass "fdt rsvmem print - Show current mem reserves\n"
11352e192b24SSimon Glass "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
11362e192b24SSimon Glass "fdt rsvmem delete <index> - Delete a mem reserves\n"
11372e192b24SSimon Glass "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
11382e192b24SSimon Glass " <start>/<end> - initrd start/end addr\n"
11392e192b24SSimon Glass #if defined(CONFIG_FIT_SIGNATURE)
11402e192b24SSimon Glass "fdt checksign [<addr>] - check FIT signature\n"
11412e192b24SSimon Glass " <start> - addr of key blob\n"
11422e192b24SSimon Glass " default gd->fdt_blob\n"
11432e192b24SSimon Glass #endif
11441cc0a9f4SRobert P. J. Day "NOTE: Dereference aliases by omitting the leading '/', "
11452e192b24SSimon Glass "e.g. fdt print ethernet0.";
11462e192b24SSimon Glass #endif
11472e192b24SSimon Glass
11482e192b24SSimon Glass U_BOOT_CMD(
11492e192b24SSimon Glass fdt, 255, 0, do_fdt,
11502e192b24SSimon Glass "flattened device tree utility commands", fdt_help_text
11512e192b24SSimon Glass );
1152