From patchwork Mon Nov 12 10:32:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bean Huo X-Patchwork-Id: 10678461 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9DEDE14BD for ; Mon, 12 Nov 2018 10:32:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8D9F029DFE for ; Mon, 12 Nov 2018 10:32:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 817D029E2A; Mon, 12 Nov 2018 10:32:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C9E829DFE for ; Mon, 12 Nov 2018 10:32:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728054AbeKLUZW (ORCPT ); Mon, 12 Nov 2018 15:25:22 -0500 Received: from mailout.micron.com ([137.201.242.129]:15065 "EHLO mailout.micron.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727204AbeKLUZV (ORCPT ); Mon, 12 Nov 2018 15:25:21 -0500 Received: from mail.micron.com (boexchangeum.micron.com [137.201.21.209]) by mailout.micron.com (8.14.4/8.14.6) with ESMTP id wACAWbv7001517; Mon, 12 Nov 2018 03:32:38 -0700 Received: from bowex36c.micron.com (137.201.84.132) by bowex17a.micron.com (137.201.21.209) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Mon, 12 Nov 2018 03:32:37 -0700 Received: from NAM03-CO1-obe.outbound.protection.outlook.com (137.201.84.23) by bowex36c.micron.com (137.201.84.132) with Microsoft SMTP Server (TLS) id 15.0.1347.2 via Frontend Transport; Mon, 12 Nov 2018 03:32:37 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=MicronCorp.onmicrosoft.com; s=selector1-micron-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mLIkmZSjulQij4GWpbEDbP2TvH6+yTd+MZypvlTnWpA=; b=qsTSXhZ6aHRqlTWTonVJ9ckphKClF3aaV8PNB4YAyaWad3YSMLQbDNYC8tp0XBw0JhPgnDdx41+Rnud03StUXiRoNlFqJS2jIzstDK1LHfGY5XTOtkx4t9lumtjDuj/Dzh0rPhjnAmVVjXml2Kv/2jUM/mDXbvSqjNMqNkZ/mTA= Received: from BYAPR08MB4533.namprd08.prod.outlook.com (52.135.234.15) by BYAPR08MB4229.namprd08.prod.outlook.com (20.176.251.17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1294.21; Mon, 12 Nov 2018 10:32:34 +0000 Received: from BYAPR08MB4533.namprd08.prod.outlook.com ([fe80::79a1:21fc:771b:7e20]) by BYAPR08MB4533.namprd08.prod.outlook.com ([fe80::79a1:21fc:771b:7e20%6]) with mapi id 15.20.1294.045; Mon, 12 Nov 2018 10:32:34 +0000 From: "Bean Huo (beanhuo)" To: "dgilbert@interlog.com" , "linux-scsi@vger.kernel.org" CC: Hannes Reinecke , Bart Van Assche , "beanhuo@outlook.com" Subject: [PATCH V2] sg3_utils: sg_write_buffer: convert string to integer while reading from stdio Thread-Topic: [PATCH V2] sg3_utils: sg_write_buffer: convert string to integer while reading from stdio Thread-Index: AdR6cjMAK4Tn1g+0RN6EO+8P6m98xg== Date: Mon, 12 Nov 2018 10:32:33 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [165.225.81.44] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;BYAPR08MB4229;6:h1QyvH48Z82RNAUVhXk/kPw28/ywrMCsYqNJpOaB8YawCp/BJkB98nN4RVKSU+0d0SLWuut4WGk+hrGL+KMd3CK4JFLy00XqCaGMduT464dpnPk0rmFqreiuHM5d3S14czUYRiZNCd7s4HvDr8li/efGRnEbvmvd9PMdv6dl3SpCest5weInN9jiEcc/+iGdSiY9/jD8pge7joPImyXYC+KWutWy15HLJqWFrvqon0fXJ7LZ6LWZLn7Avle7NneUEgG6ZrajKokhjvgN8EIhchGn5dSIO2/AD/QI1LrY0xSKroZlHaRrC8Z9+xFMbfvF01vwsWL+kSOPcXzoaOmYSTJj6YBwGfdG0IW3Ae1lHEVT6veAZAa6O7LtbMgpiPapLRYNnC0a/UdEtbe/UxMHyxXOXgA89d6fg3DY5l4g0hAuoGPSHQ94+rGDEq6+dk9FNPVH5H2KuYPaxp/I+K4I/A==;5:ZaiTn6RqeWSqcfGp7P5CCTFb74a8CN8ThB9eNvglFhtr2XjVdwQjgd0Wr9h6PgNRdltkUNARrrR3a0PfBQzSaxL88hl9d43RiXTDqSWWYLCHWZlyo7hXvyKfkadKa6ucmC2++GT/xLVzOg+epNon+CjHOW2IG6rUpXYRNx1qk/o=;7:rlkj3c2xMINu607RtydIENoY49+bFYTw1ImDJvnjkxpGaH4FlfNRD2R5qScoAtTTcqoB4KKR/lAwxQVu+JhhlCbo3dkKPl2NUQStdx/gvEQ7bTc4OXFMFCEMX+lYzUpP5JAhU4Z+JfJF2JZzJ8gKfg== x-ms-exchange-antispam-srfa-diagnostics: SOS; x-ms-office365-filtering-correlation-id: 6889d626-efd7-4620-2614-08d6488a287a x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390040)(7020095)(4652040)(8989299)(5600074)(711020)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020);SRVR:BYAPR08MB4229; x-ms-traffictypediagnostic: BYAPR08MB4229:|BYAPR08MB4229: authentication-results: spf=none (sender IP is ) smtp.mailfrom=beanhuo@micron.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(34377916053724); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(3231402)(944501410)(52105112)(3002001)(93006095)(93001095)(10201501046)(148016)(149066)(150057)(6041310)(20161123558120)(20161123560045)(20161123564045)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(201708071742011)(7699051)(76991095);SRVR:BYAPR08MB4229;BCL:0;PCL:0;RULEID:;SRVR:BYAPR08MB4229; x-forefront-prvs: 0854128AF0 x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(346002)(376002)(366004)(396003)(136003)(39860400002)(199004)(189003)(8676002)(316002)(66066001)(575784001)(2501003)(86362001)(305945005)(2900100001)(6506007)(39060400002)(7736002)(7696005)(102836004)(2906002)(9686003)(8936002)(4326008)(68736007)(97736004)(55236004)(478600001)(99286004)(5660300001)(14454004)(71190400001)(486006)(476003)(71200400001)(105586002)(3846002)(256004)(33656002)(186003)(26005)(6116002)(6436002)(110136005)(25786009)(81156014)(74316002)(81166006)(106356001)(54906003)(14444005)(55016002)(53936002);DIR:OUT;SFP:1101;SCL:1;SRVR:BYAPR08MB4229;H:BYAPR08MB4533.namprd08.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: micron.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: 1SSR4shZUwkpEp0fs3qbZ6aMMuaCJGsDqaLeg11UPeS/OdlUhSNI3pxA8WV8xeOG76w+82fIpTMw/2fdWFEQzlzRGd4cV3k2caNr67LDlkSelaDKUPIYla41lv0OCmodOlKRTZJaS7VIAenR4sNYs4jLHsTdMLcoS0ahLjlwaG9JdXjZjdcUZt+C/PLF/+zNjlfSFwmReCAhkFQgqZSxebgS8CwIwHXWT/ztt2wQfIcBZFHwwv2d7cyFCiiCpaXCQYnG6ysMKtwNK3x6np/8W2c05xd9x1+WYOUzX39hmfK60eTjn6Gl7Z0OXDLOYA+Gotmdlsm5e3D7uv2X/xa9gxdzFyYvPri2P4MEufMmXJY= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 6889d626-efd7-4620-2614-08d6488a287a X-MS-Exchange-CrossTenant-originalarrivaltime: 12 Nov 2018 10:32:33.9783 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f38a5ecd-2813-4862-b11b-ac1d563c806f X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR08MB4229 X-OriginatorOrg: micron.com X-TM-AS-Product-Ver: SMEX-12.0.0.1782-8.200.1013-24218.005 X-TM-AS-Result: No--5.829200-0.000000-31 X-TM-AS-MatchedID: 709251-703788-707321-700398-703283-700383-709755-700038-7 80052-705861-707451-705567-703529-851458-303283-851106-188019-704852-708712 -700324-105630-701339-137717-700059-700008-188199-106230-701594-148004-1480 07-148020-148050-42000-42003-63 X-TM-AS-User-Approved-Sender: Yes X-TM-AS-User-Blocked-Sender: No X-MT-CheckInternalSenderRule: True X-Scanned-By: MIMEDefang 2.78 on 137.201.82.98 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch is to convert inputted string to the integer when read data from stdin. While entering data flow, the data between bytes can be separated by either space, or ',' (or by '.'). V1-V2: 1. Rebased the patch on the latest sg_write_buffer.c 2. Added the wrong input checkup, and process 3. Changed the delimer from ",. " to ",. /n/t", in order to skip the spaces at the end of string because of misoperation. 4. Modified some wrong indents. Tested on my own UFS platform, used to issue VU command: ./sg_write_buffer -b 0 -i 0 -v -l 0x2c -m 1 -S 7 -r /dev/block/sda 0x01 0x40 0x20 tried to read 44 bytes from -, got 3 bytes pad with 0xff bytes and continue sending single write buffer, mode=0x1, mpsec=7, id=0, offset=0, len=44 Write buffer cdb: 3b e1 00 00 00 00 00 00 2c 00 ./sg_read_buffer -l 32 -m 1 -S 6 -v /dev/block/sda Read buffer(10) cdb: 3c c1 00 00 00 00 00 00 20 00 00 61 34 64 36 63 38 61 42 65 36 4b 4d 4c 34 4c 30 10 30 34 44 43 45 32 30 32 57 30 00 00 00 00 00 00 Signed-off-by: Bean Huo --- src/sg_write_buffer.c | 58 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/sg_write_buffer.c b/src/sg_write_buffer.c index 9c81e93..115f0af 100644 --- a/src/sg_write_buffer.c +++ b/src/sg_write_buffer.c @@ -212,6 +212,7 @@ main(int argc, char * argv[]) const char * device_name = NULL; const char * file_name = NULL; uint8_t * dop = NULL; + uint8_t * read_buf = NULL; uint8_t * free_dop = NULL; char * cp; const struct mode_s * mp; @@ -440,16 +441,51 @@ main(int argc, char * argv[]) } } } - res = read(infd, dop, wb_len); - if (res < 0) { - ret = sg_convert_errno(errno); - snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s", - file_name); - perror(ebuff); - if (! got_stdin) - close(infd); - goto err_out; - } + if (infd == STDIN_FILENO) { + if (NULL == (read_buf = (unsigned char *)malloc(DEF_XFER_LEN))) { + pr2serr(ME "out of memory\n"); + ret = SG_LIB_SYNTAX_ERROR; + goto err_out; + } + res = read(infd, read_buf, DEF_XFER_LEN); + if (res < 0) { + snprintf(ebuff, EBUFF_SZ, ME "couldn't read from STDIN"); + perror(ebuff); + ret = SG_LIB_FILE_ERROR; + goto err_out; + } + char * pch; + int val = 0; + res = 0; + pch = strtok((char*)read_buf, ",. \n\t"); + while (pch != NULL) { + val = sg_get_num_nomult(pch); + if (val >= 0 && val < 255) { + dop[res] = val; + res++; + } else { + pr2serr("Data read from STDIO is wrong.\n" + "Please input the data by byte, the bytes should be seperated\n" + "by either space, or ',' ( or by '.'), and the value per byte should\n" + "be between 0~255. Hexadecimal number preceded by either '0x' or\n" + "'OX' (or has a trailing 'h' or 'H').\n"); + ret = SG_LIB_SYNTAX_ERROR; + goto err_out; + } + pch = strtok(NULL, ",. \n\t"); + } + } else { + res = read(infd, dop, wb_len); + if (res < 0) { + ret = sg_convert_errno(errno); + snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s", + file_name); + perror(ebuff); + if (! got_stdin) + close(infd); + goto err_out; + } + } if (res < wb_len) { if (wb_len_given) { pr2serr("tried to read %d bytes from %s, got %d bytes\n", @@ -537,6 +573,8 @@ main(int argc, char * argv[]) err_out: if (free_dop) free(free_dop); + if (read_buf) + free(read_buf); if (sg_fd >= 0) { res = sg_cmds_close_device(sg_fd); if (res < 0) {