From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A036C433F5 for ; Thu, 30 Sep 2021 16:24:17 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F023D61139 for ; Thu, 30 Sep 2021 16:24:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org F023D61139 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=fris.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CD67D83257; Thu, 30 Sep 2021 18:23:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=fris.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=fris.de header.i=@fris.de header.b="ZMEohdIk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 58CED801AC; Thu, 30 Sep 2021 18:22:35 +0200 (CEST) Received: from mail.fris.de (mail.fris.de [IPv6:2a01:4f8:c2c:390b::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6864E80F16 for ; Thu, 30 Sep 2021 18:22:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=fris.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=frieder@fris.de Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 78087BFC98; Thu, 30 Sep 2021 18:22:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fris.de; s=dkim; t=1633018943; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=3IVi0pIUxfWu1L05iVJjvfrkJFi+JGFYL2q06Hjdxz8=; b=ZMEohdIkhAaX6p7p1fs/ldbgiWi3sXNXH+H1rA1k4jBKd/+bHJZ2RlPOeghZFFLpHWHIEM aoDPg2DySKEh8UWqfj4lxvxNvEcTyDTwbu2d7IQeaT0bbxGcbpwHyLz12J7kvSzjyAqLaY vCPzGJFPMaOxTtreOU9YZU4PPDgvrr8V4f1Efo7XoL1dAPeA5R15T3SuJ9cV31I0tgkjsZ OT4tdr6BIsiLBre/XyzEsBvtg6JYBai4f35CLDMG76PeuYZvZcR8tzFjb715gQJNqwNlsb g0h+rDdBe+59nwVxoRbHDxDnIRGTGOmtJAq9ty5aTbSVd6nLnPKyMADxshh3Ww== From: Frieder Schrempf To: Lukasz Majewski , u-boot@lists.denx.de Cc: Heiko Thiery , Michael Walle , Frieder Schrempf , AKASHI Takahiro , Heinrich Schuchardt , Jaehoon Chung , Marek Szyprowski , Patrice Chotard , Patrick Delaunay , Simon Glass Subject: [PATCH 3/3] dfu: dfu_sf: Support unaligned offsets for writing to serial flash Date: Thu, 30 Sep 2021 18:22:08 +0200 Message-Id: <20210930162208.2749155-3-frieder@fris.de> In-Reply-To: <20210930162208.2749155-1-frieder@fris.de> References: <20210930162208.2749155-1-frieder@fris.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Last-TLS-Session-Version: TLSv1.3 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Frieder Schrempf Currently using an offset that is not aligned to the erase block size of the flash results in incorrect data on the device. To support this use case, we prevent the first cycle to write beyond the erase block border and return the actual bytes written to the calling functions. That way the subsequent write cycles will be aligned and the correct position in the source buffer can be tracked. This was tested on a SPI NOR with 4k erase blocks and with: dfu_alt_info=sf 0:0=flash-bin raw 0x400 0x1f0000 Please note, that this implementation doesn't preserve the data in the first erase block (before the start offset), but neither does the previous implementation do that for the last erase block when the end of the data is not aligned. So this seems like an acceptable limitation. Signed-off-by: Frieder Schrempf --- drivers/dfu/dfu.c | 9 ++++++++- drivers/dfu/dfu_sf.c | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index 5b2659ee7d..c43db77823 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -242,11 +242,12 @@ static char *dfu_get_hash_algo(void) static int dfu_write_buffer_drain(struct dfu_entity *dfu) { - long w_size; + long w_size, w_size_req; int ret; /* flush size? */ w_size = dfu->i_buf - dfu->i_buf_start; + w_size_req = w_size; if (w_size == 0) return 0; @@ -264,6 +265,9 @@ static int dfu_write_buffer_drain(struct dfu_entity *dfu) /* update offset */ dfu->offset += w_size; + /* store remaining bytes */ + dfu->b_left = w_size_req - w_size; + puts("#"); return ret; @@ -402,6 +406,9 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int *size, int blk_seq_num) } } + /* if not all bytes have been written, adjust size */ + *size -= dfu->b_left; + return 0; } diff --git a/drivers/dfu/dfu_sf.c b/drivers/dfu/dfu_sf.c index c7ed674092..814df6ef88 100644 --- a/drivers/dfu/dfu_sf.c +++ b/drivers/dfu/dfu_sf.c @@ -38,13 +38,25 @@ static int dfu_write_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf, long *len) { u64 start = dfu->data.sf.start + offset; + u32 sector_offset; + u64 tmp = start; int ret; + /* Calculate the offset into the sector for unaligned writes */ + sector_offset = do_div(tmp, dfu->data.sf.dev->sector_size); + ret = spi_flash_erase(dfu->data.sf.dev, find_sector(dfu, start), dfu->data.sf.dev->sector_size); if (ret) return ret; + /* + * In case of an unaligned start address, only write until the + * next sector boundary + */ + if (sector_offset) + *len = dfu->data.sf.dev->sector_size - sector_offset; + ret = spi_flash_write(dfu->data.sf.dev, start, *len, buf); if (ret) return ret; -- 2.33.0