uboot/cmd/dtimg.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * (C) Copyright 2018 Linaro Ltd.
   4 * Sam Protsenko <semen.protsenko@linaro.org>
   5 */
   6
   7#include <image-android-dt.h>
   8#include <common.h>
   9
  10enum cmd_dtimg_info {
  11        CMD_DTIMG_START = 0,
  12        CMD_DTIMG_SIZE,
  13};
  14
  15static int do_dtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
  16                         char * const argv[])
  17{
  18        char *endp;
  19        ulong hdr_addr;
  20
  21        if (argc != 2)
  22                return CMD_RET_USAGE;
  23
  24        hdr_addr = simple_strtoul(argv[1], &endp, 16);
  25        if (*endp != '\0') {
  26                printf("Error: Wrong image address\n");
  27                return CMD_RET_FAILURE;
  28        }
  29
  30        if (!android_dt_check_header(hdr_addr)) {
  31                printf("Error: DT image header is incorrect\n");
  32                return CMD_RET_FAILURE;
  33        }
  34
  35        android_dt_print_contents(hdr_addr);
  36
  37        return CMD_RET_SUCCESS;
  38}
  39
  40static int dtimg_get_fdt(int argc, char * const argv[], enum cmd_dtimg_info cmd)
  41{
  42        ulong hdr_addr;
  43        u32 index;
  44        char *endp;
  45        ulong fdt_addr;
  46        u32 fdt_size;
  47        char buf[65];
  48
  49        if (argc != 4)
  50                return CMD_RET_USAGE;
  51
  52        hdr_addr = simple_strtoul(argv[1], &endp, 16);
  53        if (*endp != '\0') {
  54                printf("Error: Wrong image address\n");
  55                return CMD_RET_FAILURE;
  56        }
  57
  58        if (!android_dt_check_header(hdr_addr)) {
  59                printf("Error: DT image header is incorrect\n");
  60                return CMD_RET_FAILURE;
  61        }
  62
  63        index = simple_strtoul(argv[2], &endp, 0);
  64        if (*endp != '\0') {
  65                printf("Error: Wrong index\n");
  66                return CMD_RET_FAILURE;
  67        }
  68
  69        if (!android_dt_get_fdt_by_index(hdr_addr, index, &fdt_addr, &fdt_size))
  70                return CMD_RET_FAILURE;
  71
  72        switch (cmd) {
  73        case CMD_DTIMG_START:
  74                snprintf(buf, sizeof(buf), "%lx", fdt_addr);
  75                break;
  76        case CMD_DTIMG_SIZE:
  77                snprintf(buf, sizeof(buf), "%x", fdt_size);
  78                break;
  79        default:
  80                printf("Error: Unknown cmd_dtimg_info value: %d\n", cmd);
  81                return CMD_RET_FAILURE;
  82        }
  83
  84        env_set(argv[3], buf);
  85
  86        return CMD_RET_SUCCESS;
  87}
  88
  89static int do_dtimg_start(cmd_tbl_t *cmdtp, int flag, int argc,
  90                          char * const argv[])
  91{
  92        return dtimg_get_fdt(argc, argv, CMD_DTIMG_START);
  93}
  94
  95static int do_dtimg_size(cmd_tbl_t *cmdtp, int flag, int argc,
  96                         char * const argv[])
  97{
  98        return dtimg_get_fdt(argc, argv, CMD_DTIMG_SIZE);
  99}
 100
 101static 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, "", ""),
 105};
 106
 107static int do_dtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 108{
 109        cmd_tbl_t *cp;
 110
 111        cp = find_cmd_tbl(argv[1], cmd_dtimg_sub, ARRAY_SIZE(cmd_dtimg_sub));
 112
 113        /* Strip off leading 'dtimg' command argument */
 114        argc--;
 115        argv++;
 116
 117        if (!cp || argc > cp->maxargs)
 118                return CMD_RET_USAGE;
 119        if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
 120                return CMD_RET_SUCCESS;
 121
 122        return cp->cmd(cmdtp, flag, argc, argv);
 123}
 124
 125U_BOOT_CMD(
 126        dtimg, CONFIG_SYS_MAXARGS, 0, do_dtimg,
 127        "manipulate dtb/dtbo Android image",
 128        "dump <addr>\n"
 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"
 141);
 142