1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Linaro Ltd.
8 #include <image-android-dt.h>
16 static int do_dtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
25 hdr_addr = simple_strtoul(argv[1], &endp, 16);
27 printf("Error: Wrong image address\n");
28 return CMD_RET_FAILURE;
31 if (!android_dt_check_header(hdr_addr)) {
32 printf("Error: DT image header is incorrect\n");
33 return CMD_RET_FAILURE;
36 android_dt_print_contents(hdr_addr);
38 return CMD_RET_SUCCESS;
41 static int dtimg_get_fdt(int argc, char * const argv[], enum cmd_dtimg_info cmd)
53 hdr_addr = simple_strtoul(argv[1], &endp, 16);
55 printf("Error: Wrong image address\n");
56 return CMD_RET_FAILURE;
59 if (!android_dt_check_header(hdr_addr)) {
60 printf("Error: DT image header is incorrect\n");
61 return CMD_RET_FAILURE;
64 index = simple_strtoul(argv[2], &endp, 0);
66 printf("Error: Wrong index\n");
67 return CMD_RET_FAILURE;
70 if (!android_dt_get_fdt_by_index(hdr_addr, index, &fdt_addr, &fdt_size))
71 return CMD_RET_FAILURE;
75 snprintf(buf, sizeof(buf), "%lx", fdt_addr);
78 snprintf(buf, sizeof(buf), "%x", fdt_size);
81 printf("Error: Unknown cmd_dtimg_info value: %d\n", cmd);
82 return CMD_RET_FAILURE;
85 env_set(argv[3], buf);
87 return CMD_RET_SUCCESS;
90 static int do_dtimg_start(cmd_tbl_t *cmdtp, int flag, int argc,
93 return dtimg_get_fdt(argc, argv, CMD_DTIMG_START);
96 static int do_dtimg_size(cmd_tbl_t *cmdtp, int flag, int argc,
99 return dtimg_get_fdt(argc, argv, CMD_DTIMG_SIZE);
102 static cmd_tbl_t cmd_dtimg_sub[] = {
103 U_BOOT_CMD_MKENT(dump, 2, 0, do_dtimg_dump, "", ""),
104 U_BOOT_CMD_MKENT(start, 4, 0, do_dtimg_start, "", ""),
105 U_BOOT_CMD_MKENT(size, 4, 0, do_dtimg_size, "", ""),
108 static int do_dtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
112 cp = find_cmd_tbl(argv[1], cmd_dtimg_sub, ARRAY_SIZE(cmd_dtimg_sub));
114 /* Strip off leading 'dtimg' command argument */
118 if (!cp || argc > cp->maxargs)
119 return CMD_RET_USAGE;
120 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
121 return CMD_RET_SUCCESS;
123 return cp->cmd(cmdtp, flag, argc, argv);
127 dtimg, CONFIG_SYS_MAXARGS, 0, do_dtimg,
128 "manipulate dtb/dtbo Android image",
130 " - parse specified image and print its structure info\n"
131 " <addr>: image address in RAM, in hex\n"
132 "dtimg start <addr> <index> <varname>\n"
133 " - get address (hex) of FDT in the image, by index\n"
134 " <addr>: image address in RAM, in hex\n"
135 " <index>: index of desired FDT in the image\n"
136 " <varname>: name of variable where to store address of FDT\n"
137 "dtimg size <addr> <index> <varname>\n"
138 " - get size (hex, bytes) of FDT in the image, by index\n"
139 " <addr>: image address in RAM, in hex\n"
140 " <index>: index of desired FDT in the image\n"
141 " <varname>: name of variable where to store size of FDT"