From patchwork Mon Dec 2 11:11:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 1203101 X-Patchwork-Delegate: matthias.bgg@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=samsung.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=samsung.com header.i=@samsung.com header.b="exTZAjm+"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 47RMrD6QGMz9sP3 for ; Mon, 2 Dec 2019 22:11:40 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 47311C21E74; Mon, 2 Dec 2019 11:11:37 +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=SPF_HELO_PASS, 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 84D96C21C50; Mon, 2 Dec 2019 11:11:35 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 983A0C21DA1; Mon, 2 Dec 2019 11:11:33 +0000 (UTC) Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by lists.denx.de (Postfix) with ESMTPS id D0D84C21C50 for ; Mon, 2 Dec 2019 11:11:32 +0000 (UTC) Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20191202111130euoutp024545186ca11bec1a8bb10d1c3d70cefa~ciXGSm6M_0636106361euoutp02D for ; Mon, 2 Dec 2019 11:11:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20191202111130euoutp024545186ca11bec1a8bb10d1c3d70cefa~ciXGSm6M_0636106361euoutp02D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1575285090; bh=nZT1VUDZg4JvTLgo8cklvSXznSdlUSKdeoZpWQxd4F8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=exTZAjm+AiarZX3ezrWL7iEy5KZ1OVl7p8qb2gmomInb5hx/Pz4NH/4ij+cQ1j/Dk KDMRZzyXb26fwXsUDTSdJiq4i/kiHFaoTQEyUwLtz3tiy3XbuMsroBxhe7BSqE8ag0 mc4R/xb9U5Ckx/rdcm1CUNbSMTMt3pSCPE8XKAF0= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20191202111130eucas1p2b3ef85b0015dc4f4769d639d7485d730~ciXF9-R571754317543eucas1p29; Mon, 2 Dec 2019 11:11:30 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id 43.F2.60698.261F4ED5; Mon, 2 Dec 2019 11:11:30 +0000 (GMT) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20191202111129eucas1p29846d2823438c5f9e7192925862e1df5~ciXFp2NOW2095220952eucas1p2g; Mon, 2 Dec 2019 11:11:29 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20191202111129eusmtrp103ec3591255c3653b566871b6d9937b5~ciXFpOepp1453714537eusmtrp1Q; Mon, 2 Dec 2019 11:11:29 +0000 (GMT) X-AuditID: cbfec7f5-a0fff7000001ed1a-b8-5de4f1621b31 Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id D2.F8.07950.161F4ED5; Mon, 2 Dec 2019 11:11:29 +0000 (GMT) Received: from AMDC2765.digital.local (unknown [106.120.51.73]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20191202111129eusmtip241f198452794c203332681513b49da3e~ciXFGDLmA1824718247eusmtip2M; Mon, 2 Dec 2019 11:11:29 +0000 (GMT) From: Marek Szyprowski To: u-boot@lists.denx.de Date: Mon, 2 Dec 2019 12:11:14 +0100 Message-Id: <20191202111118.12868-3-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191202111118.12868-1-m.szyprowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA0VSa0hTYRju2znbOc5NTtPwaxXGMKUsL904WI0ChUP9sF8JxdKpJ7XclDOn WVBiIDqtdEO8ldqy1IVuXhIR87JVi6RpiWVg0VQQB5uTtPKC5XbU/j3P8z7P97y8fDgiquOK 8XRlNs0o5RkSHh/tfrtsO5K0MCOLnB7CyPYqI5ecWCnkkrOuMQ7Z+vobRjboZrikqd6JklW6 OR65NrCMkhXaTox0virGSEuFG5z1pepq3qGUe+Eq5Rg0YNTk5z4e9WGyB1APugyAMnaNo1SH g7qIX+afTqEz0nNoJkKayE/Tj/zlZL0Pu2kx6Dn5QBesAT44JI7DjupFoAF8XEQ0Azg6Vcph ySKAqy/1GEt+AtjmnEK2IqtNdoQdNAFYOV/P244UOOs4HhePiIIap4bnwQEEhCsPx70lCGFC 4PdCM/AM/Ik4aOq5z9UAHEeJA7Dt636PLCTOwAZbOWDbguAL06C32YeQwoHip6jnHUg8x+CQ d1mPKQauuEdQFvtDh7ULY/FeOKwr3QzcA9Bua8VYUgrgWEHVZsUpaLF+9G6BEAehsTeClc/B soZxzCNDwg9OOHd6ZGQDarsrEVYWwqJCEesOgTXWtu3aodFPm9eioLt52KuLiPKNA7dcLwNB N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FW652dUW5S tqsThP4Z8SRJ8NrMOloSnKWbenlFjvUzAxHWm3/3qC5eGGApfaxhdtidvC0+8p9KeDdYOi9y nKx0csoKcfb5lbtP+Guxz3FR7/uaMyf0bkxook03f0Rsy4mA5Krt1m1KLMUZiYZazEXFiQA8 go8ikQIAAA== X-CMS-MailID: 20191202111129eucas1p29846d2823438c5f9e7192925862e1df5 X-Msg-Generator: CA X-RootMTR: 20191202111129eucas1p29846d2823438c5f9e7192925862e1df5 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20191202111129eucas1p29846d2823438c5f9e7192925862e1df5 References: <20191202111118.12868-1-m.szyprowski@samsung.com> Cc: Matthias Brugger , Bartlomiej Zolnierkiewicz , Heinrich Schuchardt , Tom Rini , Marek Szyprowski Subject: [U-Boot] [PATCH v4 2/6] fat: write: adjust data written in each partial write 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" The code for handing file overwrite incorrectly calculated the amount of data to write when writing to the last non-cluster aligned chunk. Fix this by ensuring that no more data than the 'filesize' is written to disk. While touching min()-based calculations, change it to type-safe min_t() function. Signed-off-by: Marek Szyprowski --- This patch finally fixes the issue revealed by the test script from the previous patch. The correctness of the change has been also verified by the following additional test scripts: --->8-fat_test2.sh--- #!/bin/bash make sandbox_defconfig make dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k mkfs.vfat -v /tmp/10M.img cat >/tmp/cmds </tmp/model/file0001.raw yes ABC | head -c 4096 >/tmp/model/file0003.raw yes ABC | head -c 4096 >/tmp/model/file0005.raw yes DEF | head -c 7936 >/tmp/model/file0007.raw mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result hd /tmp/10M.img if diff -urq /tmp/model /tmp/result then echo Test okay else echo Test fail fi --->8-fat_test3.sh--- #!/bin/bash make sandbox_defconfig make dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k mkfs.vfat -v /tmp/10M.img cat >/tmp/cmds </tmp/model/file0001.raw yes ABC | head -c 4096 >/tmp/model/file0003.raw yes ABC | head -c 4096 >/tmp/model/file0005.raw yes DEF | head -c 8448 >/tmp/model/file0007.raw mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result hd /tmp/10M.img if diff -urq /tmp/model /tmp/result then echo Test okay else echo Test fail fi --->8-fat_test4.sh--- #!/bin/bash make sandbox_defconfig make dd if=/dev/zero of=/tmp/10M.img bs=1024 count=10k mkfs.vfat -v /tmp/10M.img cat >/tmp/cmds </tmp/model/file0001.raw yes ABC | head -c 4096 >/tmp/model/file0003.raw yes ABC | head -c 4096 >/tmp/model/file0005.raw yes DEF | head -c 2304 >/tmp/model/file0007.raw yes GHI | head -c 2304 >>/tmp/model/file0007.raw yes DEF | head -c 2304 >>/tmp/model/file0007.raw yes GHI | head -c 2304 >>/tmp/model/file0007.raw mcopy -n -i /tmp/10M.img ::file0001.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0003.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0005.raw /tmp/result mcopy -n -i /tmp/10M.img ::file0007.raw /tmp/result hd /tmp/10M.img if diff -urq /tmp/model /tmp/result then echo Test okay else echo Test fail fi --->8--- Feel free to prepare a proper sandbox/py_test based tests based on the provided test scripts. --- fs/fat/fat_write.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index f946030f7d..8e4a235d7c 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -813,7 +813,9 @@ set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer, offset = 0; else offset = pos - cur_pos; - wsize = min(cur_pos + actsize, filesize) - pos; + wsize = min_t(unsigned long long, actsize, filesize - cur_pos); + wsize -= offset; + if (get_set_cluster(mydata, curclust, offset, buffer, wsize, &actsize)) { printf("Error get-and-setting cluster\n");