From patchwork Tue Jun 28 14:40:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Matic X-Patchwork-Id: 12898396 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEE69C43334 for ; Tue, 28 Jun 2022 14:40:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346201AbiF1OkP (ORCPT ); Tue, 28 Jun 2022 10:40:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235217AbiF1OkO (ORCPT ); Tue, 28 Jun 2022 10:40:14 -0400 Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70127.outbound.protection.outlook.com [40.107.7.127]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9439F2A268 for ; Tue, 28 Jun 2022 07:40:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PPz4xy0BZpMKFITB1Y7WSz5FD16SRlfPQE6LAjSzY+q8LSCYn7CqU+rZ0WfuiQwVGB174eHN9HEjJMzdqimtVbPZ6KNYMeykXuBL2iFcH5DJeXI+2IkC26GGYyKBOkMhvlOXAD8mhbJ7gtackhT70VBSKEdppelv7m8e9M5QNrUhiWcMUyufbqw9umfWGpyyHPhYyAZMIj2BTgAlAupPLu6Rt/nISdHi811GU93qpbiDU2UkyyCHSO6OKW+wByrKPMzRCeNkXm3O5qIlTP4Qr8cCCLP99tLj022j0nt2LE9r4IlChq1e631swQf4M1cQrnTVPs9qu/YIfLDzSxidBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3IBYtgiz9eT+JUHxd9fmr8FLlHLAtr9NH1jBzd/zmII=; b=E7SLDYoAOR5yc42G9VizDJ7A4CwmGKo8vuCYAUXMZgLZyjCe4cUarYhgFhVRHnLUN32iG6mWWLNIcAeHJABKlkJ8VOk36VjM6oC4hzj0jKvFgevtZ5cHRTafejl6DoC2BQ8jnBI7vwgdx0A5HnHh2ZVl+txhWT+i55mlZXQM0pQz5WqKJsEgTpCb3lUrN0WqccBH9ZExBPUnyDAqc/Vu5PoPhLv8u8L3VgZ5q7xXRc0I3/RckR3AguK46Ibl21jEd2in1ikOtr2f7FhaYuhCjF/yE5DAisP8yohTGohqAu2fWpIbqeEXl6YskDd/poSgDsOEsktXxjm6F5r3cUINqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nokia.com; dmarc=pass action=none header.from=nokia.com; dkim=pass header.d=nokia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nokia.onmicrosoft.com; s=selector1-nokia-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3IBYtgiz9eT+JUHxd9fmr8FLlHLAtr9NH1jBzd/zmII=; b=bU6etLHgPLccV9l2gSOi0Twsx3i9EYcObuJq5eLNtnOm9JqmlCM4/x+4yRoKcHaMd65PkJLWvC9cETL82iZYayiRoRWawK97gMlr6kOak5abQffjpNlo6XjmlW8Jbne+Ia322C8k88y0kFzLTUTYyV1O4OZ49vmCez1bHiX3zFs= Received: from AM5PR0701MB2964.eurprd07.prod.outlook.com (2603:10a6:203:4e::11) by DB6PR0701MB2646.eurprd07.prod.outlook.com (2603:10a6:4:22::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Tue, 28 Jun 2022 14:40:10 +0000 Received: from AM5PR0701MB2964.eurprd07.prod.outlook.com ([fe80::3593:a351:dfd5:7f98]) by AM5PR0701MB2964.eurprd07.prod.outlook.com ([fe80::3593:a351:dfd5:7f98%4]) with mapi id 15.20.5395.012; Tue, 28 Jun 2022 14:40:10 +0000 From: "Matic, Bruno (Nokia - DE/Ulm)" To: "linux-mmc@vger.kernel.org" CC: Avri Altman , Ulf Hansson Subject: [PATCH] mmc-utils: correct and clean up the file handling Thread-Topic: [PATCH] mmc-utils: correct and clean up the file handling Thread-Index: AdiK+e63vfHiFbPsQZCmC+FOzGq/7Q== Date: Tue, 28 Jun 2022 14:40:09 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nokia.com; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 26832eea-3d55-48e8-9b8e-08da59141a30 x-ms-traffictypediagnostic: DB6PR0701MB2646:EE_ x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: xDvaALbGWZF4M3gefLgRmtwMSNFnX0CpbrGq9sa9UO0WizFFujRIlTgoMZv1j2TsD1omWKqWHMRf6Nrr5eEYYOKa10HtTQP9xthiSxEPpoEgYtFUz300wcBH7XpOAB98Gz3wRJp3pGIqDS5HRE0H9EliExjR/pF5ipjjEM8RxAqgR7N/2FyxN+/n+anFA66AhqVPg2SiNfaN8Xq8S8xBnawhBT33wGgS9VzL5y5MnqI5BwpxI4CJuxT8ZIHtZjnIRfSWSu8DQ0xL1IP5gpvpQuOBgX1TCcz5uf26JBSlyjMpwO+OoMo5cUQ6c8tT5x3tZqCNJKIXEE7YEidP4W+T/MdU4YPkjPL/akO8GVu672YB1zPWpWlidtsY6ytBUTyat9cj7CSDZQIPYXlI+Lt229Qqa7h8hNlAIj6kW0xUTRjixO9908Tz1GkBzPOA27mdT06s9pBwEpuCEZigSKdeJDuhn9wwO/lSxAxy1Swcb8skF7zbdSCgoogVeLvCh+YGJZ8M9FWxZOCwWCItFqsp65TOIlAnMntqGVbDIlcE1RRA3ZIEYMDU/zH43NzzgxUm9OJHm9QpkCrglDPdjsGZ0tcmkVBC4kpoJqOQNlqiGRA6FsPpHInZxkgXIbEg4CQLdujdL9YOPDmzGaGMZxts+4rCXLyzuopOBWe3FfLclIUTC/iqpsk5Ja8Bx3bk0gY4Q47dr+SEQwT073ArtRNkZsfctHnRP6RFz/NTT89Mrnvhunjgtk200bTWvt+CVIG4/k4vxNL1xinwnG1sEXm4nXZyYslFprzdKHRoXLE/qGs= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM5PR0701MB2964.eurprd07.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(396003)(366004)(376002)(346002)(136003)(39860400002)(71200400001)(64756008)(8676002)(86362001)(76116006)(66946007)(66446008)(83380400001)(4326008)(55016003)(66476007)(66556008)(33656002)(26005)(9686003)(6506007)(7696005)(82960400001)(52536014)(478600001)(8936002)(122000001)(6916009)(38070700005)(2906002)(5660300002)(316002)(54906003)(38100700002)(186003)(41300700001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: PEEShQMGwOwuXhX/CSIFPhh+tmKZiopqoGk92HwtC1KKhcq/gg+Qs0+xx4J/n3KE2aAQ2hyHfnRk8UGq3ACMV0l8ho9WbOrrIT3emtMD6Gas31NtvKh5Sk4IOaOZ8OVAWT3TaN1q/CW5CBaz7np+RrjWq8iWeoVVjPE1kCkNzN/x0A4IrJKGaO+JkAZkQjjUOD9D0L9XVjbX6E8vZJBWrp4g1ajyFE6fI8COZHPjZvyMMxnHw8nalJUUGV5bw0aKd5IRA7pKhGJ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ySuUOYy3+LcBdKeaIeyuEPnMkvqe3sDMd8RGgo9u1/l+TO4te7/3kJQEl9x60ocKsMfbOMNKxdgx9/LgWE5KWlpvUzndAv0SNF3hiV2yyebjknblMsbDbJBFJX6QxvAhKTetGi5APSKZc1NhFf7fSiUnLaqcW39AJ5QooXAIMHVStr33cFaZAfOHrhW114M0liPdkgLwjk47x8oZZvuvW9Op72+Y3+FJHQVgsTYYFbG6XU7frNHt7hjq8bESOOYC0G MIME-Version: 1.0 X-OriginatorOrg: nokia.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: AM5PR0701MB2964.eurprd07.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 26832eea-3d55-48e8-9b8e-08da59141a30 X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Jun 2022 14:40:09.9332 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 5d471751-9675-428d-917b-70f44f9630b0 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: ATaONxZbkz4M3VzWY65qfL+nNZP0YIDFZYRe7PPwAQRpFZBFOrVYDrKsHaGN695Qmm2qjwngCmBgT0CqoBMIyA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0701MB2646 Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org Add the check if the whole firmware was loaded. Cleaned up the leftovers of handling the file in chunks. Signed-off-by: Bruno Matic Signed-off-by: Bruno Matic Signed-off-by: Bruno Matic Reviewed-by: Avri Altman Reviewed-by: Avri Altman --- mmc_cmds.c | 69 +++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/mmc_cmds.c b/mmc_cmds.c index 70480df..e64c747 100644 --- a/mmc_cmds.c +++ b/mmc_cmds.c @@ -2812,7 +2812,6 @@ int do_ffu(int nargs, char **argv) __u8 *buf = NULL; __u32 arg; off_t fw_size; - ssize_t chunk_size; char *device; struct mmc_ioc_multi_cmd *multi_cmd = NULL; __u32 blocks = 1; @@ -2925,45 +2924,45 @@ int do_ffu(int nargs, char **argv) multi_cmd->cmds[3].flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; multi_cmd->cmds[3].write_flag = 1; -do_retry: - /* read firmware chunk */ + /* read firmware */ lseek(img_fd, 0, SEEK_SET); - chunk_size = read(img_fd, buf, fw_size); + if (read(img_fd, buf, fw_size) != fw_size) { + fprintf(stderr, "Could not read the whole firmware file\n"); + ret = -ENOSPC; + goto out; + } - if (chunk_size > 0) { - /* send ioctl with multi-cmd */ - ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); +do_retry: + /* send ioctl with multi-cmd */ + ret = ioctl(dev_fd, MMC_IOC_MULTI_CMD, multi_cmd); - if (ret) { - perror("Multi-cmd ioctl"); - /* In case multi-cmd ioctl failed before exiting from ffu mode */ - ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); - goto out; - } + if (ret) { + perror("Multi-cmd ioctl"); + /* In case multi-cmd ioctl failed before exiting from ffu mode */ + ioctl(dev_fd, MMC_IOC_CMD, &multi_cmd->cmds[3]); + goto out; + } - ret = read_extcsd(dev_fd, ext_csd); - if (ret) { - fprintf(stderr, "Could not read EXT_CSD from %s\n", device); - goto out; - } + ret = read_extcsd(dev_fd, ext_csd); + if (ret) { + fprintf(stderr, "Could not read EXT_CSD from %s\n", device); + goto out; + } - /* Test if we need to restart the download */ - sect_done = ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0] | - ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_1] << 8 | - ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_2] << 16 | - ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_3] << 24; - /* By spec, host should re-start download from the first sector if sect_done is 0 */ - if (sect_done == 0) { - if (retry > 0) { - retry--; - fprintf(stderr, "Programming failed. Retrying... (%d)\n", retry); - goto do_retry; - } - fprintf(stderr, "Programming failed! Aborting...\n"); - goto out; - } else { - fprintf(stderr, "Programmed %d/%jd bytes\r", sect_done * sect_size, (intmax_t)fw_size); + /* Test if we need to restart the download */ + sect_done = ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0] | + ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_1] << 8 | + ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_2] << 16 | + ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_3] << 24; + /* By spec, host should re-start download from the first sector if sect_done is 0 */ + if (sect_done == 0) { + if (retry > 0) { + retry--; + fprintf(stderr, "Programming failed. Retrying... (%d)\n", retry); + goto do_retry; } + fprintf(stderr, "Programming failed! Aborting...\n"); + goto out; } if ((sect_done * sect_size) == fw_size) {