From patchwork Mon Mar 25 14:41:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Romanov X-Patchwork-Id: 1915658 X-Patchwork-Delegate: dario.binacchi@amarulasolutions.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=salutedevices.com header.i=@salutedevices.com header.a=rsa-sha256 header.s=mail header.b=mRSElf+4; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V3G264P4gz1yXy for ; Tue, 26 Mar 2024 01:45:34 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E66BE87F00; Mon, 25 Mar 2024 15:44:49 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=salutedevices.com 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=salutedevices.com header.i=@salutedevices.com header.b="mRSElf+4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3275087EDD; Mon, 25 Mar 2024 15:42:10 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 300FF871C7 for ; Mon, 25 Mar 2024 15:42:08 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=salutedevices.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=avromanov@salutedevices.com Received: from p-infra-ksmg-sc-msk02 (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id C6946120008; Mon, 25 Mar 2024 17:42:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru C6946120008 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1711377727; bh=AQdZn+kVZMfQr5LtJWIH6s5s1cPWwgr2Deo3jKacVSw=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=mRSElf+4IUXjZr6EGNWsLy2t8yVMcSKR6l8umaWvhHDsbxtMh+gVk0MK6v0Ys9YDT qWhAYgTZYhOIrI/OXS+8p2EEr1nii3rl13vGaMSopOFTcBt0pEK0jEVjb25ECokm5d fh5RUGgaj5b5mEbhCQWY4LGWAiBDSvgzmQJ/RtbdWBXDlimoCROpau+t4YDcG8kSuP 83VWDBt0hljW0mAr5AlIdoFabbRlOOQs47OyU539ywDTnZl4c7hI2W22gPo9RnXfIB u34iT7BdOLyuUutJw7dUMcYiao3CodNuhRpaeQM9Y69H9B7/K5qYd1oDxTpgIHn4oG 8DiJ96BwZBwEg== Received: from smtp.sberdevices.ru (p-i-exch-sc-m02.sberdevices.ru [172.16.192.103]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Mon, 25 Mar 2024 17:42:07 +0300 (MSK) Received: from user-A520M-DS3H.sigma.sbrf.ru (100.64.160.123) by p-i-exch-sc-m02.sberdevices.ru (172.16.192.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 25 Mar 2024 17:42:05 +0300 From: Alexey Romanov To: , , , , , , , , , , , , , , , , , CC: , , Alexey Romanov Subject: [PATCH v2 2/6] ubi: allow to write to volume with offset Date: Mon, 25 Mar 2024 17:41:44 +0300 Message-ID: <20240325144148.3738195-3-avromanov@salutedevices.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240325144148.3738195-1-avromanov@salutedevices.com> References: <20240325144148.3738195-1-avromanov@salutedevices.com> MIME-Version: 1.0 X-Originating-IP: [100.64.160.123] X-ClientProxiedBy: p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) To p-i-exch-sc-m02.sberdevices.ru (172.16.192.103) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 184400 [Mar 25 2024] X-KSMG-AntiSpam-Version: 6.1.0.4 X-KSMG-AntiSpam-Envelope-From: avromanov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 11 0.3.11 5ecf9895443a5066245fcb91e8430edf92b1b594, {Tracking_from_domain_doesnt_match_to}, salutedevices.com:7.1.1; d41d8cd98f00b204e9800998ecf8427e.com:7.1.1; 127.0.0.199:7.1.2; smtp.sberdevices.ru:7.1.1,5.0.1; 100.64.160.123:7.1.2, FromAlignment: s, ApMailHostAddress: 100.64.160.123 X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2024/03/25 10:48:00 #24431293 X-KSMG-AntiVirus-Status: Clean, skipped X-Mailman-Approved-At: Mon, 25 Mar 2024 15:44:47 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.8 at phobos.denx.de X-Virus-Status: Clean Introduce ubi_volume_offset_write() helper, which allow to write to ubi volume with specified offset. Signed-off-by: Alexey Romanov Reviewed-by: Heiko Schocher Acked-by: Heiko Schocher --- cmd/ubi.c | 71 +++++++++++++++++++++++++++++++++++++++++++-- env/ubi.c | 10 +++---- include/ubi_uboot.h | 2 +- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/cmd/ubi.c b/cmd/ubi.c index 2257f68498..af874b6a46 100644 --- a/cmd/ubi.c +++ b/cmd/ubi.c @@ -415,9 +415,74 @@ int ubi_volume_begin_write(char *volume, void *buf, size_t size, return ubi_volume_continue_write(volume, buf, size); } -int ubi_volume_write(char *volume, void *buf, size_t size) +static int ubi_volume_offset_write(char *volume, void *buf, loff_t offset, + size_t size) { - return ubi_volume_begin_write(volume, buf, size, size); + int lnum, len, tbuf_size, ret; + struct ubi_volume *vol; + loff_t off = offset; + void *tbuf; + + vol = ubi_find_volume(volume); + if (!vol) + return -ENODEV; + + if (size > vol->reserved_pebs * (ubi->leb_size - vol->data_pad)) + return -EINVAL; + + tbuf_size = vol->usable_leb_size; + tbuf = malloc_cache_aligned(tbuf_size); + if (!tbuf) + return -ENOMEM; + + lnum = off; + off = do_div(lnum, vol->usable_leb_size); + + do { + struct ubi_volume_desc desc = { + .vol = vol, + .mode = UBI_READWRITE, + }; + + len = size > tbuf_size ? tbuf_size : size; + if (off + len >= vol->usable_leb_size) + len = vol->usable_leb_size - off; + + ret = ubi_read(&desc, lnum, tbuf, 0, tbuf_size); + if (ret) { + pr_err("Failed to read leb %d (%d)\n", lnum, ret); + goto exit; + } + + memcpy(tbuf + off, buf, len); + + ret = ubi_leb_change(&desc, lnum, tbuf, tbuf_size); + if (ret) { + pr_err("Failed to write leb %d(%d)\n", lnum, ret); + goto exit; + } + + off += len; + if (off >= vol->usable_leb_size) { + lnum++; + off -= vol->usable_leb_size; + } + + buf += len; + size -= len; + } while (size); + +exit: + free(tbuf); + return ret; +} + +int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size) +{ + if (!offset) + return ubi_volume_begin_write(volume, buf, size, size); + + return ubi_volume_offset_write(volume, buf, offset, size); } int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size) @@ -761,7 +826,7 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) (void *)addr, size, full_size); } } else { - ret = ubi_volume_write(argv[3], (void *)addr, size); + ret = ubi_volume_write(argv[3], (void *)addr, 0, size); } if (!ret) { printf("%lld bytes written to volume %s\n", size, diff --git a/env/ubi.c b/env/ubi.c index 661801d5a9..6ae74a500b 100644 --- a/env/ubi.c +++ b/env/ubi.c @@ -54,7 +54,7 @@ static int env_ubi_save(void) if (gd->env_valid == ENV_VALID) { puts("Writing to redundant UBI... "); if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND, - (void *)env_new, CONFIG_ENV_SIZE)) { + (void *)env_new, 0, CONFIG_ENV_SIZE)) { printf("\n** Unable to write env to %s:%s **\n", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); @@ -63,7 +63,7 @@ static int env_ubi_save(void) } else { puts("Writing to UBI... "); if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, - (void *)env_new, CONFIG_ENV_SIZE)) { + (void *)env_new, 0, CONFIG_ENV_SIZE)) { printf("\n** Unable to write env to %s:%s **\n", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); @@ -93,7 +93,7 @@ static int env_ubi_save(void) return 1; } - if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, + if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new, 0, CONFIG_ENV_SIZE)) { printf("\n** Unable to write env to %s:%s **\n", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); @@ -197,7 +197,7 @@ static int env_ubi_erase(void) memset(env_buf, 0x0, CONFIG_ENV_SIZE); if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, - (void *)env_buf, CONFIG_ENV_SIZE)) { + (void *)env_buf, 0, CONFIG_ENV_SIZE)) { printf("\n** Unable to erase env to %s:%s **\n", CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME); @@ -205,7 +205,7 @@ static int env_ubi_erase(void) } if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) { if (ubi_volume_write(ENV_UBI_VOLUME_REDUND, - (void *)env_buf, CONFIG_ENV_SIZE)) { + (void *)env_buf, 0, CONFIG_ENV_SIZE)) { printf("\n** Unable to erase env to %s:%s **\n", CONFIG_ENV_UBI_PART, ENV_UBI_VOLUME_REDUND); diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h index bcccb76e6c..5f046b91a2 100644 --- a/include/ubi_uboot.h +++ b/include/ubi_uboot.h @@ -49,7 +49,7 @@ extern int ubi_mtd_param_parse(const char *val, struct kernel_param *kp); extern int ubi_init(void); extern void ubi_exit(void); extern int ubi_part(char *part_name, const char *vid_header_offset); -extern int ubi_volume_write(char *volume, void *buf, size_t size); +extern int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size); extern int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size); extern struct ubi_device *ubi_devices[];