1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Linaro Ltd.
4 * Sam Protsenko <semen.protsenko@linaro.org>
7 #include <image-android-dt.h>
15 static int do_dtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
24 hdr_addr = simple_strtoul(argv[1], &endp, 16);
26 printf("Error: Wrong image address\n");
27 return CMD_RET_FAILURE;
30 if (!android_dt_check_header(hdr_addr)) {
31 printf("Error: DT image header is incorrect\n");
32 return CMD_RET_FAILURE;
35 android_dt_print_contents(hdr_addr);
37 return CMD_RET_SUCCESS;
40 static int dtimg_get_fdt(int argc, char * const argv[], enum cmd_dtimg_info cmd)
52 hdr_addr = simple_strtoul(argv[1], &endp, 16);
54 printf("Error: Wrong image address\n");
55 return CMD_RET_FAILURE;
58 if (!android_dt_check_header(hdr_addr)) {
59 printf("Error: DT image header is incorrect\n");
60 return CMD_RET_FAILURE;
63 index = simple_strtoul(argv[2], &endp, 0);
65 printf("Error: Wrong index\n");
66 return CMD_RET_FAILURE;
69 if (!android_dt_get_fdt_by_index(hdr_addr, index, &fdt_addr, &fdt_size))
70 return CMD_RET_FAILURE;
74 snprintf(buf, sizeof(buf), "%lx", fdt_addr);
77 snprintf(buf, sizeof(buf), "%x", fdt_size);
80 printf("Error: Unknown cmd_dtimg_info value: %d\n", cmd);
81 return CMD_RET_FAILURE;
84 env_set(argv[3], buf);
86 return CMD_RET_SUCCESS;
89 static int do_dtimg_start(cmd_tbl_t *cmdtp, int flag, int argc,
92 return dtimg_get_fdt(argc, argv, CMD_DTIMG_START);
95 static int do_dtimg_size(cmd_tbl_t *cmdtp, int flag, int argc,
98 return dtimg_get_fdt(argc, argv, CMD_DTIMG_SIZE);
101 static cmd_tbl_t cmd_dtimg_sub[] = {
102 U_BOOT_CMD_MKENT(dump, 2, 0, do_dtimg_dump, "", ""),
103 U_BOOT_CMD_MKENT(start, 4, 0, do_dtimg_start, "", ""),
104 U_BOOT_CMD_MKENT(size, 4, 0, do_dtimg_size, "", ""),
107 static int do_dtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
111 cp = find_cmd_tbl(argv[1], cmd_dtimg_sub, ARRAY_SIZE(cmd_dtimg_sub));
113 /* Strip off leading 'dtimg' command argument */
117 if (!cp || argc > cp->maxargs)
118 return CMD_RET_USAGE;
119 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
120 return CMD_RET_SUCCESS;
122 return cp->cmd(cmdtp, flag, argc, argv);
126 dtimg, CONFIG_SYS_MAXARGS, 0, do_dtimg,
127 "manipulate dtb/dtbo Android image",
129 " - parse specified image and print its structure info\n"
130 " <addr>: image address in RAM, in hex\n"
131 "dtimg start <addr> <index> <varname>\n"
132 " - get address (hex) of FDT in the image, by index\n"
133 " <addr>: image address in RAM, in hex\n"
134 " <index>: index of desired FDT in the image\n"
135 " <varname>: name of variable where to store address of FDT\n"
136 "dtimg size <addr> <index> <varname>\n"
137 " - get size (hex, bytes) of FDT in the image, by index\n"
138 " <addr>: image address in RAM, in hex\n"
139 " <index>: index of desired FDT in the image\n"
140 " <varname>: name of variable where to store size of FDT"