From patchwork Tue Jul 4 19:48:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 784263 X-Patchwork-Delegate: jh80.chung@samsung.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3x2F9Y6Xxgz9sNw for ; Wed, 5 Jul 2017 05:55:45 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="uwfgjg9i"; dkim-atps=neutral Received: by lists.denx.de (Postfix, from userid 105) id 9C442C21EBE; Tue, 4 Jul 2017 19:51:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id C1D82C21EA1; Tue, 4 Jul 2017 19:50:50 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 2F0E2C21E93; Tue, 4 Jul 2017 19:50:41 +0000 (UTC) Received: from mail-oi0-f46.google.com (mail-oi0-f46.google.com [209.85.218.46]) by lists.denx.de (Postfix) with ESMTPS id 5E33BC21E52 for ; Tue, 4 Jul 2017 19:50:41 +0000 (UTC) Received: by mail-oi0-f46.google.com with SMTP id 191so84552896oii.2 for ; Tue, 04 Jul 2017 12:50:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=KPZVbKOSVJNpKzwH+rDkT2gXhz5MWy5oxyze4Whbk54=; b=uwfgjg9iDbt8laqCqJR8J0aFOZgXYy2XtZPtRqoHE5kusJX0KkbLoya8yEV4v9b846 XZQrsfLh6GHwTLTb/uhML14hSObdLTz2ZIlX2oefrgHvAOTSvnUTOdhggqrdjE7CRSZc 1lPIbdembpIt3SDLniV8z0yzoYLDXucpIWogsS6fGHS+QLGbMfXbhbuVIAV2CFKt8n74 3rweADUzvE9eK30cLyD6wAXleodAFkKQCtkaxtYlFAx9shSkXipcjGAMl75sgKUtRdRy 4UCKrXd7eEnXLddDIgOrczTa2+lvmE9O/kxebAwZHRd+VbYYIoebD6PYAKCmrgDaoi42 RkqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=KPZVbKOSVJNpKzwH+rDkT2gXhz5MWy5oxyze4Whbk54=; b=cVc9uo4YF1DMKrKsjNyUDVNTWGL8WN5BVm3krQU1HlMgU0Lm7KpmCy6pcP40xuZMsL 6OsF/FsZaKZ6H+LCXRu6JkVHrNRF1UV+Mpz1rlpn0MZyFac8cmPUryrgIomtlZ+uSvIN uidf9Fkef/YEd+5nHTV4rdLDXiaZ15ObWU5hcV2j5aQzcAMXH8mAHdxqo15Vya4fJgjC p55u8DHLLBUduQQV6dhhwT6bxYieE1kw4KzbpC7L//ZN9Bqt8E/2UD4tz9ybOgsn9Y51 0bEAIO2L4L5LCU9ja5K6EB3wp9I4rbPui9QZ0VMU/Km6fLiuBTQkM2iTtOqsxzqNs/Je RtEA== X-Gm-Message-State: AIVw1108x1zVeSI1McaTC9/lenwX/tzcyXqlpwN+QDVyeNXMygzCe8qn hynjExgMIgzMPzM1 X-Received: by 10.202.86.83 with SMTP id k80mr2248550oib.154.1499197839986; Tue, 04 Jul 2017 12:50:39 -0700 (PDT) Received: from kiwi.bld.corp.google.com ([100.101.164.89]) by smtp.gmail.com with ESMTPSA id w131sm37444695oiw.30.2017.07.04.12.50.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Jul 2017 12:50:39 -0700 (PDT) Received: by kiwi.bld.corp.google.com (Postfix, from userid 121222) id CBEE0140FD4; Tue, 4 Jul 2017 13:50:38 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Tue, 4 Jul 2017 13:48:54 -0600 Message-Id: <20170704194931.88240-3-sjg@chromium.org> X-Mailer: git-send-email 2.13.2.725.g09c95d1e9-goog In-Reply-To: <20170704194931.88240-1-sjg@chromium.org> References: <20170704194931.88240-1-sjg@chromium.org> Cc: Tom Rini , Ziping Chen , Stefan Roese Subject: [U-Boot] [PATCH 02/39] dm: blk: Add a generic function for block device commands X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Most block devices provide a command (e.g. 'sata', 'scsi', 'ide') and these commands generally do the same thing. This makes it harder to maintain this code and keep it consistent. We now have a block device interface which is either implemented by driver model (when CONFIG_BLK is enabled) or with a legacy interface. Therefore it is possible to handle most of what these commands do with generic code. Add a new generic function to process block-device commands using the interface type and the current device number for that type. Signed-off-by: Simon Glass --- cmd/Makefile | 1 + cmd/blk_common.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/blk.h | 12 +++++++ 3 files changed, 117 insertions(+) create mode 100644 cmd/blk_common.c diff --git a/cmd/Makefile b/cmd/Makefile index bd231f24d8..e2b3a9cc71 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -15,6 +15,7 @@ obj-y += version.o # command obj-$(CONFIG_CMD_AES) += aes.o obj-$(CONFIG_CMD_ARMFLASH) += armflash.o +obj-y += blk_common.o obj-$(CONFIG_SOURCE) += source.o obj-$(CONFIG_CMD_SOURCE) += source.o obj-$(CONFIG_CMD_BDI) += bdinfo.o diff --git a/cmd/blk_common.c b/cmd/blk_common.c new file mode 100644 index 0000000000..86c75e78d8 --- /dev/null +++ b/cmd/blk_common.c @@ -0,0 +1,104 @@ +/* + * Handling of common block commands + * + * Copyright (c) 2017 Google, Inc + * + * (C) Copyright 2000-2011 + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include + +#ifdef HAVE_BLOCK_DEVICE +int blk_common_cmd(int argc, char * const argv[], enum if_type if_type, + int *cur_devnump) +{ + const char *if_name = blk_get_if_type_name(if_type); + + switch (argc) { + case 0: + case 1: + return CMD_RET_USAGE; + case 2: + if (strncmp(argv[1], "inf", 3) == 0) { + blk_list_devices(if_type); + return 0; + } else if (strncmp(argv[1], "dev", 3) == 0) { + if (blk_print_device_num(if_type, *cur_devnump)) { + printf("\nno %s devices available\n", if_name); + return CMD_RET_FAILURE; + } + return 0; + } else if (strncmp(argv[1], "part", 4) == 0) { + if (blk_list_part(if_type)) + printf("\nno %s devices available\n", if_name); + return 0; + } + return CMD_RET_USAGE; + case 3: + if (strncmp(argv[1], "dev", 3) == 0) { + int dev = (int)simple_strtoul(argv[2], NULL, 10); + + if (!blk_show_device(if_type, dev)) { + *cur_devnump = dev; + printf("... is now current device\n"); + } else { + return CMD_RET_FAILURE; + } + return 0; + } else if (strncmp(argv[1], "part", 4) == 0) { + int dev = (int)simple_strtoul(argv[2], NULL, 10); + + if (blk_print_part_devnum(if_type, dev)) { + printf("\n%s device %d not available\n", + if_name, dev); + return CMD_RET_FAILURE; + } + return 0; + } + return CMD_RET_USAGE; + + default: /* at least 4 args */ + if (strcmp(argv[1], "read") == 0) { + ulong addr = simple_strtoul(argv[2], NULL, 16); + lbaint_t blk = simple_strtoul(argv[3], NULL, 16); + ulong cnt = simple_strtoul(argv[4], NULL, 16); + ulong n; + + printf("\n%s read: device %d block # %lld, count %ld ... ", + if_name, *cur_devnump, (unsigned long long)blk, + cnt); + + n = blk_read_devnum(if_type, *cur_devnump, blk, cnt, + (ulong *)addr); + + printf("%ld blocks read: %s\n", n, + n == cnt ? "OK" : "ERROR"); + return n == cnt ? 0 : 1; + } else if (strcmp(argv[1], "write") == 0) { + ulong addr = simple_strtoul(argv[2], NULL, 16); + lbaint_t blk = simple_strtoul(argv[3], NULL, 16); + ulong cnt = simple_strtoul(argv[4], NULL, 16); + ulong n; + + printf("\n%s write: device %d block # %lld, count %ld ... ", + if_name, *cur_devnump, (unsigned long long)blk, + cnt); + + n = blk_write_devnum(if_type, *cur_devnump, blk, cnt, + (ulong *)addr); + + printf("%ld blocks written: %s\n", n, + n == cnt ? "OK" : "ERROR"); + return n == cnt ? 0 : 1; + } else { + return CMD_RET_USAGE; + } + + return 0; + } +} +#endif diff --git a/include/blk.h b/include/blk.h index 69076d3683..5e29c6a21f 100644 --- a/include/blk.h +++ b/include/blk.h @@ -631,4 +631,16 @@ int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart); */ const char *blk_get_if_type_name(enum if_type if_type); +/** + * blk_common_cmd() - handle common commands with block devices + * + * @args: Number of arguments to the command (argv[0] is the command itself) + * @argv: Command arguments + * @if_type: Interface type + * @cur_devnump: Current device number for this interface type + * @return 0 if OK, CMD_RET_ERROR on error + */ +int blk_common_cmd(int argc, char * const argv[], enum if_type if_type, + int *cur_devnump); + #endif