From patchwork Tue Dec 24 16:51:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eugeniu Rosca X-Patchwork-Id: 1215259 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="q7ZkKQJA"; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47j2M30PcYz9sPn for ; Wed, 25 Dec 2019 03:52:14 +1100 (AEDT) Received: from phobos.denx.de (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A4B5A81677; Tue, 24 Dec 2019 17:52:04 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="q7ZkKQJA"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CF3D681657; Tue, 24 Dec 2019 17:52:02 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, SPF_HELO_NONE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E103D81672 for ; Tue, 24 Dec 2019 17:51:58 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=roscaeugeniu@gmail.com Received: by mail-wr1-x436.google.com with SMTP id q10so20245660wrm.11 for ; Tue, 24 Dec 2019 08:51:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wjKw7MtcUOTlNuCiho2oyVSeqolWAvHHNUP4FQDUkfU=; b=q7ZkKQJAYIbHXjKk/aLy5B1AHrc61p28jWmSWqL0tIXe8umUJnsoelAm2ut7C2JMGm 4E2R3p+q4JmmfpGD7WZxjC4A+jx3rGOVtK2Mfmz1FiYYH+SilhY0yB6T+M7G33Tzsxfb 6Frwn7n7vcZQjC549oCmvh5vl5b3+aJtPuyUhn0ZTcKNHOdusTbhxAWznVjvtbEfJ5Mt NsJssVYFl+SKhvMMEb5MYoDEv996X5ULRAUJVD1sqHOMVCIW3h1ZIQVRbLLjGmlNmdoH DL3QzZvCFOycsoFtEkx7geoiAroUdycGgc5H5l+1ctVCRdPZKCc2uBLLqLfSE1LhacbI 4/5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wjKw7MtcUOTlNuCiho2oyVSeqolWAvHHNUP4FQDUkfU=; b=rz4K5ux0/KuDZE8h5xjeAOB/7Px1KWFStrXtFt36Pzte2inssXORcdHc4yuRU5aSX9 6DtpnbXdNcxTFfFgn+6VDCpXffaCD3Hd5CCcENzi/4+TVSS5G1JaIZTljMTEfCeFfJLN PjtKQ5GtYntizvjml2ncSKp/23aBu+06uCyvF15unY9kSBd2uILShNJStkqi820FEdgj p/KRrJWQ5glBK1WTssR9/6s5vKjg6ZPCg6ipAtEgs50XxgCiQRE2oDchDmQQmMuZiMuV CFTbaBuPpuX+FKEJ6be4P1WZc1G9Irg/PXWY1+E2jEnwLPo+ZcGS2jkQCSsYnmeh1YLV OS1Q== X-Gm-Message-State: APjAAAUElK6A3tF7tsBfoao3OyF8lfp7N290v6eYx/cSBRxlKyUXxsQr keTrn5BJ1YgoaMVWWZN2XXQ= X-Google-Smtp-Source: APXvYqwoQl9jdnSpwegWuljF1OAbs+zzlWcZso9JcXVAQhevLutOab7CU0QI/HhVKSps1N42Z8XY4A== X-Received: by 2002:adf:e70d:: with SMTP id c13mr36375857wrm.248.1577206318391; Tue, 24 Dec 2019 08:51:58 -0800 (PST) Received: from erosca.fritz.box (x4e37979c.dyn.telefonica.de. [78.55.151.156]) by smtp.gmail.com with ESMTPSA id s128sm3136376wme.39.2019.12.24.08.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2019 08:51:58 -0800 (PST) From: Eugeniu Rosca X-Google-Original-From: Eugeniu Rosca To: Sam Protsenko , u-boot@lists.denx.de Subject: [PATCH 3/3] cmd: adtimg: Refactor usage style Date: Tue, 24 Dec 2019 17:51:08 +0100 Message-Id: <20191224165108.2836-4-erosca@de.adit-jv.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191224165108.2836-1-erosca@de.adit-jv.com> References: <20191224165108.2836-1-erosca@de.adit-jv.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.26 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tom Rini , Eugeniu Rosca , Eugeniu Rosca Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.101.4 at phobos.denx.de X-Virus-Status: Clean Trying to extend 'adtimg' functionality [1], we've been severely hit by a major limitation in the command's usage scheme. Specifically, the command's user interface appears to be too centric to getting the DTB/DTBO entry [3] based on the index of the desired DT in the image, which makes it really difficult retrieving the DT entry based on alternative criteria (e.g. filtering by id/rev fields), the latter being demanded by real life customer use-cases [1]. This went to the point of receiving below feedback from Sam [2]: -- snip -- As for 'dtimg' command: after giving it some thought, I think not much people using it yet. So in this particular case I don't have some strong preference, and if you think the 'dtimg' interface is ugly, and it overcomes "don't break interfaces" rule, maybe now is a good time to rework it (before it gets widely used). -- snip -- Given the above, rework the usage pattern from [4] to [5], in order to allow an intuitive enablement of "by id|rev" DT search [6]. [1] https://patchwork.ozlabs.org/cover/1202575/ ("cmd: dtimg: Enhance with --id and --rev options (take #1)") [2] https://patchwork.ozlabs.org/patch/1182207/#2317020 [3] https://source.android.com/devices/architecture/dto/partitions [4] Old usage adtimg dump - Print image contents adtimg start - Get DT address by index adtimg size - Get DT size by index [5] New usage adtimg addr - Set image location to adtimg dump - Print out image contents adtimg get dt --index= [avar [svar]] - Get DT address and size by index [6] Soon-to-be-provided "by id|rev" add-on functionality adtimg get dt --id= --rev= [avar [svar [ivar]]] - Get DT address/size/index by id|rev fields Cc: Sam Protsenko Signed-off-by: Eugeniu Rosca --- cmd/adtimg.c | 217 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 158 insertions(+), 59 deletions(-) diff --git a/cmd/adtimg.c b/cmd/adtimg.c index 22b4f5e1a83f..60bb01c3498a 100644 --- a/cmd/adtimg.c +++ b/cmd/adtimg.c @@ -2,18 +2,22 @@ /* * (C) Copyright 2018 Linaro Ltd. * Sam Protsenko + * Eugeniu Rosca */ #include #include #include -enum cmd_adtimg_info { - CMD_ADTIMG_START = 0, - CMD_ADTIMG_SIZE, -}; +#define OPT_INDEX "--index" -static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc, +/* + * Current/working DTB/DTBO Android image address. + * Similar to 'working_fdt' variable in 'fdt' command. + */ +static ulong working_img; + +static int do_adtimg_addr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { char *endp; @@ -24,86 +28,185 @@ static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc, hdr_addr = simple_strtoul(argv[1], &endp, 16); if (*endp != '\0') { - printf("Error: Wrong image address\n"); + printf("Error: Wrong image address '%s'\n", argv[1]); return CMD_RET_FAILURE; } - if (!android_dt_check_header(hdr_addr)) { - printf("Error: DT image header is incorrect\n"); + /* + * Allow users to set an address prior to copying the DTB/DTBO + * image to that same address, i.e. skip header verification. + */ + + working_img = hdr_addr; + return CMD_RET_SUCCESS; +} + +static int adtimg_check_working_img(void) +{ + if (!working_img) { + printf("Error: Please, call 'adtimg addr '. Aborting!\n"); return CMD_RET_FAILURE; } - android_dt_print_contents(hdr_addr); + if (!android_dt_check_header(working_img)) { + printf("Error: Invalid image header at 0x%lx\n", working_img); + return CMD_RET_FAILURE; + } return CMD_RET_SUCCESS; } -static int adtimg_get_fdt(int argc, char * const argv[], - enum cmd_adtimg_info cmd) +static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) { - ulong hdr_addr; - u32 index; - char *endp; - ulong fdt_addr; - u32 fdt_size; - char buf[65]; - - if (argc != 4) + if (argc != 1) return CMD_RET_USAGE; - hdr_addr = simple_strtoul(argv[1], &endp, 16); - if (*endp != '\0') { - printf("Error: Wrong image address\n"); + if (adtimg_check_working_img() != CMD_RET_SUCCESS) + return CMD_RET_FAILURE; + + android_dt_print_contents(working_img); + + return CMD_RET_SUCCESS; +} + +static int adtimg_getopt_u32(char * const opt, char * const name, u32 *optval) +{ + char *endp, *str; + u32 val; + + if (!opt || !name || !optval) + return CMD_RET_FAILURE; + + str = strchr(opt, '='); + if (!str) { + printf("Error: Option '%s' not followed by '='\n", name); return CMD_RET_FAILURE; } - if (!android_dt_check_header(hdr_addr)) { - printf("Error: DT image header is incorrect\n"); + if (*++str == '\0') { + printf("Error: Option '%s=' not followed by value\n", name); return CMD_RET_FAILURE; } - index = simple_strtoul(argv[2], &endp, 0); + val = simple_strtoul(str, &endp, 0); if (*endp != '\0') { - printf("Error: Wrong index\n"); + printf("Error: Wrong integer value '%s=%s'\n", name, str); return CMD_RET_FAILURE; } - if (!android_dt_get_fdt_by_index(hdr_addr, index, &fdt_addr, &fdt_size)) + *optval = val; + return CMD_RET_SUCCESS; +} + +static int adtimg_getopt_index(int argc, char * const argv[], u32 *index, + char **avar, char **svar) +{ + int ret; + + if (!argv || !avar || !svar) return CMD_RET_FAILURE; - switch (cmd) { - case CMD_ADTIMG_START: - snprintf(buf, sizeof(buf), "%lx", fdt_addr); - break; - case CMD_ADTIMG_SIZE: - snprintf(buf, sizeof(buf), "%x", fdt_size); - break; - default: - printf("Error: Unknown cmd_adtimg_info value: %d\n", cmd); + if (argc > 3) { + printf("Error: Unexpected argument '%s'\n", argv[3]); return CMD_RET_FAILURE; } - env_set(argv[3], buf); + ret = adtimg_getopt_u32(argv[0], OPT_INDEX, index); + if (ret != CMD_RET_SUCCESS) + return ret; + + if (argc > 1) + *avar = argv[1]; + if (argc > 2) + *svar = argv[2]; return CMD_RET_SUCCESS; } -static int do_adtimg_start(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) +static int adtimg_get_dt_by_index(int argc, char * const argv[]) { - return adtimg_get_fdt(argc, argv, CMD_ADTIMG_START); + ulong addr; + u32 index, size; + int ret; + char *avar = NULL, *svar = NULL; + + ret = adtimg_getopt_index(argc, argv, &index, &avar, &svar); + if (ret != CMD_RET_SUCCESS) + return ret; + + if (!android_dt_get_fdt_by_index(working_img, index, &addr, &size)) + return CMD_RET_FAILURE; + + if (avar && svar) { + ret = env_set_hex(avar, addr); + if (ret) { + printf("Error: Can't set '%s' to 0x%lx\n", avar, addr); + return CMD_RET_FAILURE; + } + ret = env_set_hex(svar, size); + if (ret) { + printf("Error: Can't set '%s' to 0x%x\n", svar, size); + return CMD_RET_FAILURE; + } + } else if (avar) { + ret = env_set_hex(avar, addr); + if (ret) { + printf("Error: Can't set '%s' to 0x%lx\n", avar, addr); + return CMD_RET_FAILURE; + } + printf("0x%x (%d)\n", size, size); + } else { + printf("0x%lx, 0x%x (%d)\n", addr, size, size); + } + + return CMD_RET_SUCCESS; } -static int do_adtimg_size(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) +static int adtimg_get_dt(int argc, char * const argv[]) { - return adtimg_get_fdt(argc, argv, CMD_ADTIMG_SIZE); + if (argc < 2) { + printf("Error: No options passed to '%s'\n", argv[0]); + return CMD_RET_FAILURE; + } + + /* Strip off leading 'dt' command argument */ + argc--; + argv++; + + if (!strncmp(argv[0], OPT_INDEX, sizeof(OPT_INDEX) - 1)) + return adtimg_get_dt_by_index(argc, argv); + + printf("Error: Option '%s' not supported\n", argv[0]); + return CMD_RET_FAILURE; +} + +static int do_adtimg_get(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + if (argc < 2) { + printf("Error: No arguments passed to '%s'\n", argv[0]); + return CMD_RET_FAILURE; + } + + if (adtimg_check_working_img() != CMD_RET_SUCCESS) + return CMD_RET_FAILURE; + + /* Strip off leading 'get' command argument */ + argc--; + argv++; + + if (!strcmp(argv[0], "dt")) + return adtimg_get_dt(argc, argv); + + printf("Error: Wrong argument '%s'\n", argv[0]); + return CMD_RET_FAILURE; } static cmd_tbl_t cmd_adtimg_sub[] = { - U_BOOT_CMD_MKENT(dump, 2, 0, do_adtimg_dump, "", ""), - U_BOOT_CMD_MKENT(start, 4, 0, do_adtimg_start, "", ""), - U_BOOT_CMD_MKENT(size, 4, 0, do_adtimg_size, "", ""), + U_BOOT_CMD_MKENT(addr, CONFIG_SYS_MAXARGS, 1, do_adtimg_addr, "", ""), + U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_adtimg_dump, "", ""), + U_BOOT_CMD_MKENT(get, CONFIG_SYS_MAXARGS, 1, do_adtimg_get, "", ""), }; static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) @@ -127,17 +230,13 @@ static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) U_BOOT_CMD( adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg, "manipulate dtb/dtbo Android image", - "dump \n" - " - parse specified image and print its structure info\n" - " : image address in RAM, in hex\n" - "adtimg start \n" - " - get address (hex) of FDT in the image, by index\n" - " : image address in RAM, in hex\n" - " : index of desired FDT in the image\n" - " : name of variable where to store address of FDT\n" - "adtimg size \n" - " - get size (hex, bytes) of FDT in the image, by index\n" - " : image address in RAM, in hex\n" - " : index of desired FDT in the image\n" - " : name of variable where to store size of FDT" + "addr - Set image location to \n" + "adtimg dump - Print out image contents\n" + "adtimg get dt --index= [avar [svar]] - Get DT address/size by index\n" + "\n" + "Legend:\n" + " - : DTB/DTBO image address (hex) in RAM\n" + " - : index (hex/dec) of desired DT in the image\n" + " - : variable name to contain DT address (hex)\n" + " - : variable name to contain DT size (hex)" );