From patchwork Sat Aug 12 00:06:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Edwards X-Patchwork-Id: 1820446 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; 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=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=ko/+bAOh; dkim-atps=neutral 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RN1DL1HK3z1yf2 for ; Sat, 12 Aug 2023 10:06:42 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1584586961; Sat, 12 Aug 2023 02:06:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.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=gmail.com header.i=@gmail.com header.b="ko/+bAOh"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C72988693A; Sat, 12 Aug 2023 02:06:21 +0200 (CEST) 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,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9D2D38693A for ; Sat, 12 Aug 2023 02:06:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=cfsworks@gmail.com Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6873a30d02eso1899186b3a.3 for ; Fri, 11 Aug 2023 17:06:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691798778; x=1692403578; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZbbsDIf4C6pfkATnl2uZzdgV8NsTXUdrdyGi5hFKqaw=; b=ko/+bAOhIXGU2QhaiIArdh7mw/9jv36VO45vivh5a16KJ+Xt5pLNu4kPQq8PUGgMBv DHd3QXto70zU25HOhbGNGhrFuFBwzh9cyuW7B9wV9FPSjaPZ4cMV8tG+kF3ZEekhuFWl CLmsfJURUL2cyOUpvTRwTAJizZbXhKY7J2mgc3OfQWvv/qMXHrb+wdZZF8AuGZ04hd5G Jex0eGUXj7qb5ye//xSLhNh8cm0HcsMZAr5X3U2CtEgqcOUgdp6NVHPjCaoxMotrEeL1 7dLKF/7neW10UM8JmsHn0ZhcFv/MrIr5H6HUfcv4OSL1NN74KeaitSxA/4pu81+b3B/h 3Zzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691798778; x=1692403578; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZbbsDIf4C6pfkATnl2uZzdgV8NsTXUdrdyGi5hFKqaw=; b=HgQdNFqfqF1ufA9SJCjsAl98jsgQH+udhxb4rmXdkJNzLhrb9Mlwvhozse3J7JCsrm 7mm/XKvZW901ycetTNENxxvlV68IO0Skzse5gp3EzUWlxq/A7DXNoZUGn76YLPD2thc5 EdnjlPWUQ3zCYvixt7j243kVSY6koAsEEFxrntAi+PNyxgAMtXwYnMHY0kaZT68tcbk/ u/JfKBoL0ArTB9xe1YxDOR5EUUQu9R+illERFQ2YWarhcxUOzoS7yJ9kqxqAB5xo6iBx CWcnhyU5RO10psDrq64jR5I7H6NKsffcF4Cc5vko3AdRvFk1d6CW27FppFqQe4D7ecc5 +7XA== X-Gm-Message-State: AOJu0YyflhAl4R60Km2NDE7lW44T97knKXxIb8iS36DzvG3KJ6b1FXBn bm3frt/KCKhrICX1jmB0NBk= X-Google-Smtp-Source: AGHT+IGUFXxAhltzhc0ZCEJ67gUnLWkzmkGyidV0gLrMk+nMbJOZV8Qfv3dbyTHY/M+0TYEVIUvcXQ== X-Received: by 2002:a05:6a00:1703:b0:668:83b7:fd02 with SMTP id h3-20020a056a00170300b0066883b7fd02mr4151944pfc.31.1691798777814; Fri, 11 Aug 2023 17:06:17 -0700 (PDT) Received: from celestia.nettie.lan (static-198-54-134-172.cust.tzulo.com. [198.54.134.172]) by smtp.gmail.com with ESMTPSA id x16-20020a62fb10000000b00686bef8e55csm3848834pfm.39.2023.08.11.17.06.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 17:06:17 -0700 (PDT) From: Sam Edwards X-Google-Original-From: Sam Edwards To: Heiko Schocher , Kyungmin Park Cc: u-boot@lists.denx.de, Sam Edwards Subject: [RFC PATCH 2/4] mtd: ubi: bind block device driver for static volumes Date: Fri, 11 Aug 2023 18:06:04 -0600 Message-ID: <20230812000606.72319-3-CFSworks@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230812000606.72319-1-CFSworks@gmail.com> References: <20230812000606.72319-1-CFSworks@gmail.com> MIME-Version: 1.0 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 This makes static UBI volumes readable as block devices, however no mechanism for selecting these volume devices yet exists. Signed-off-by: Sam Edwards --- drivers/mtd/ubi/ubi-uclass.c | 110 +++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/drivers/mtd/ubi/ubi-uclass.c b/drivers/mtd/ubi/ubi-uclass.c index f8971e793e..231d6d90c7 100644 --- a/drivers/mtd/ubi/ubi-uclass.c +++ b/drivers/mtd/ubi/ubi-uclass.c @@ -8,10 +8,119 @@ #define LOG_CATEGORY UCLASS_UBI #include +#include #include #include #include +static ulong ubi_bread(struct udevice *dev, lbaint_t lba, lbaint_t blkcnt, + void *dst) +{ + int err, lnum; + struct blk_desc *blk = dev_get_uclass_plat(dev); + struct ubi_device *ubi = dev_get_plat(dev->parent); + struct ubi_volume *vol = ubi->volumes[blk->devnum]; + lbaint_t lba_per_peb = vol->usable_leb_size / blk->blksz; + lbaint_t lba_off, lba_len, total = 0; + + while (blkcnt) { + lnum = lba / lba_per_peb; + lba_off = lba % lba_per_peb; + lba_len = lba_per_peb - lba_off; + if (lba_len > blkcnt) + lba_len = blkcnt; + + err = ubi_eba_read_leb(ubi, vol, lnum, dst, + lba_off << blk->log2blksz, + lba_len << blk->log2blksz, 0); + if (err) { + pr_err("UBI read error %x\n", err); + break; + } + + lba += lba_len; + blkcnt -= lba_len; + dst += lba_len << blk->log2blksz; + total += lba_len; + } + + return total; +} + +static const struct blk_ops ubi_block_ops = { + .read = ubi_bread, +}; + +U_BOOT_DRIVER(ubi_block) = { + .name = "ubi_block", + .id = UCLASS_BLK, + .ops = &ubi_block_ops, +}; + +static bool is_power_of_two(unsigned int x) +{ + return (x & -x) == x; +} + +static unsigned int choose_blksz_for_volume(const struct ubi_volume *vol) +{ + /* + * U-Boot assumes a power-of-two blksz; however, UBI LEBs are + * very often not suitably sized. To solve this, we divide the + * LEBs into a whole number of LBAs per LEB, such that each LBA + * addresses a power-of-two-sized block. To choose the blksz, + * we either: + * 1) Use the volume alignment, if it's a non-unity power of + * two. The LEB size is a multiple of this alignment, and it + * allows the user to force a particular blksz if needed for + * their use case. + * 2) Otherwise, find the greatest power-of-two factor of the + * LEB size. + */ + if (vol->alignment > 1 && is_power_of_two(vol->alignment)) + return vol->alignment; + + unsigned int blksz = 1; + while ((vol->usable_leb_size & blksz) == 0) + blksz <<= 1; + return blksz; +} + +static int ubi_post_bind(struct udevice *dev) +{ + int i; + int ret; + unsigned int blksz; + lbaint_t lba; + struct ubi_device *ubi = dev_get_plat(dev); + + for (i = 0; i < ubi->vtbl_slots; i++) { + struct ubi_volume *vol = ubi->volumes[i]; + if (!vol || vol->vol_id >= UBI_INTERNAL_VOL_START || + vol->vol_type != UBI_STATIC_VOLUME) + continue; + + if (vol->updating || vol->upd_marker) { + pr_err("** UBI volume %d (\"%s\") midupdate; ignored\n", + vol->vol_id, vol->name); + continue; + } + + blksz = choose_blksz_for_volume(vol); + lba = DIV_ROUND_UP((unsigned long long)vol->used_bytes, blksz); + + pr_debug("UBI volume %d (\"%s\"): %lu blocks, %d bytes each\n", + vol->vol_id, vol->name, lba, blksz); + + ret = blk_create_device(dev, "ubi_block", vol->name, UCLASS_UBI, + vol->vol_id, blksz, lba, NULL); + if (ret) + return ret; + } + + return 0; +} + int ubi_dm_bind(unsigned int index) { struct udevice *dev; @@ -71,4 +180,5 @@ U_BOOT_DRIVER(ubi) = { UCLASS_DRIVER(ubi) = { .id = UCLASS_UBI, .name = "ubi", + .post_bind = ubi_post_bind, };