From patchwork Wed Feb 1 18:10:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735816 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=iHLNIpF2; 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 4P6Vbj4HgJz23gY for ; Thu, 2 Feb 2023 05:21:05 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0587E85C65; Wed, 1 Feb 2023 19:20:46 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="iHLNIpF2"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3B79485C0C; Wed, 1 Feb 2023 19:10:42 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) (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 DADBE85C59 for ; Wed, 1 Feb 2023 19:10:39 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x52e.google.com with SMTP id q19so8636038edd.2 for ; Wed, 01 Feb 2023 10:10:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=Qd46g6P9wcK+ZYYb/N8DIumZwEGdTmJOFu0WSrdj9a8=; b=iHLNIpF2v8Y7bWY8ObNJj7r0jLVIHG+l3FEESgSnjzIk0u1APPLcJncr43ASdq1FxH UHMUWWdlx0iLayVedDOBY7hLtmdM8HTWXiWWRsuUF/rwYg5CkUVg0/Oh99ZavoC0a/P/ d1ivM3xDSXtvEvL96d/SkAPtmjJp5W8gc3FypMa1odBOISuabb5uGRXoqUdOWs4csq9T C9OGpUoma97UduC4B9tD4SicC3AsgaMB/IVOzB3m2B7e1/1UWfbL1MLcARJ49Avk6KUw ZfOgCRt/y7NTzHRv3o1rcpv0qKcApl27DjNyLnQdbb4YLdHnMgxeY0yXZCMtQO3hSye8 wBZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Qd46g6P9wcK+ZYYb/N8DIumZwEGdTmJOFu0WSrdj9a8=; b=aeEeehWf73WKq9PhpWsY+n0gybzU68issjsr2CzM9d0ZirGqHD0KKBYPch0v9ojmCS duykJ4iZXIDMBNfBSBnubuWeqYuk1a/XYN1mPy6BuM9E4ULYLPvL3rhl7+lyJbI8lr4C SkdoU0q5nQ2MVfskZaAd0QZjCRMJOTAoNRXg+zg91XkuRH4inQVoU2M98i1ct+bppoiN kf3OlK2d82IbTg+rkgvgkdOrjfEdrO/EKaU1AkbRc7Ov6n5GTo5JbWouYBXunXMEb0eO KeXOU2li6SaQ1869a4CLVwDSrh+YJniz93XtXyCHStfOm9FbpCmKjiGTKcnfbNiHIzOL pUDw== X-Gm-Message-State: AO0yUKWGpYo9zHctXHwNFCKPNdzNWRXRhCHa5rR4PTEwRlOIuuxENKGB 7ZAHJlU6XctnLr/zGL7UXx0FSE2J1XFj0y/L X-Google-Smtp-Source: AK7set+GdmTh057BkqdWfSTzBXey/wxazWVhvxcXGqs07TcJFvu9E5NrmjCObQxnWdUSe+3OMJ08LQ== X-Received: by 2002:aa7:dd59:0:b0:4a3:43c1:842d with SMTP id o25-20020aa7dd59000000b004a343c1842dmr2183520edw.1.1675275039456; Wed, 01 Feb 2023 10:10:39 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:38 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 1/8] image: Fix script execution from FIT images with external data Date: Wed, 1 Feb 2023 19:10:09 +0100 Message-Id: <20230201181016.4145834-2-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Update the script loading code to recognize when script data is stored externally from the FIT metadata (i.e., built with `mkimage -E`). Signed-off-by: Tobias Waldekranz Reviewed-by: Simon Glass --- boot/image-board.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boot/image-board.c b/boot/image-board.c index e5d71a3d54..74b2ad3580 100644 --- a/boot/image-board.c +++ b/boot/image-board.c @@ -1111,7 +1111,8 @@ fallback: } /* get script subimage data address and length */ - if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) { + if (fit_image_get_data_and_size(fit_hdr, noffset, + &fit_data, &fit_len)) { puts("Could not find script subimage data\n"); return 1; } From patchwork Wed Feb 1 18:10:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735817 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=0ZUlAcQG; 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)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P6Vbx0KdPz23gY for ; Thu, 2 Feb 2023 05:21:17 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 00CC585C6D; Wed, 1 Feb 2023 19:20:48 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="0ZUlAcQG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D4B5685C5B; Wed, 1 Feb 2023 19:10:43 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) (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 5556885C61 for ; Wed, 1 Feb 2023 19:10:41 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x52c.google.com with SMTP id cw4so13440138edb.13 for ; Wed, 01 Feb 2023 10:10:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=EIT6B+TGv8uvm+yU8oUB7NXDgz0+7o1S9Asc9aJxgb0=; b=0ZUlAcQGDBbXcQkLXL6d2IGDgQIwlXZdKvs6Oxz4fini7QxfPCPegrwteRDFO41vlS f7lHl6tTDpBWqDHDezifJIr9Hc1V9K/pgE8R83uew1y5UNLcAbRLgrgachj3DvMVIURQ 2i+u1J4ybXJGATWe4mC7W66KICy5KGWVzi/N8SeSwX57Gx5SQNjcZR1fx0e3UhGgMBKk yieWq7UDeQOFo/liCDN6XG749kZCyR9R3z3WW6OjkU66X6DuFpU9BX0tL3/V8xKhgrFe kC8xDpl+4CFy7VXwsPB+TRcykvHqZrnhw1srmxEdG/ac9blKrGkARP843S4ACTaFqzXS BCzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=EIT6B+TGv8uvm+yU8oUB7NXDgz0+7o1S9Asc9aJxgb0=; b=knsMnRV9FLPbMqxTwiLHG6PtH1Xbpc0FxwFTINZhiUQml7P4Qy9/+wnVTaNnpQrRje HYjKb9+jI2dFUzBiFSe4vnIFT9O9AqExEhO1fzKaRo9gDFqtPcJS0aR8kXTe1gSWGouV rpyUl5/Kl7uTVtOs2gxAOqyedUH+tuqj8Ke3J7Nj6uw9zmWjajTryy37RZ13d/4cHKtN juU8/Flbp02/BgdfTTgQ0z9CIGKfdviF8BJJb6SCvSfYyU7R9eauje1L+n/l9MfKPMaR k4iyEpRt15kqs4rlUaOqNJF9CvT9PorYSdlpFEc9QjFJ4f3hnGzJ+FKdslIDJ1hefYKJ FSAw== X-Gm-Message-State: AO0yUKWDiQAR572eLqoVFhw1hayktswfmEdcJ8I+P2VOktGqv9UH8lMG Y/TwdDdnLuqYO97BvO/kd0TB3A== X-Google-Smtp-Source: AK7set9sBRc5Ixoidp2qOJFN/jH1vphSQcSRy092IYEYEydnB/4jCB0AKdgdsN8lu6aNbAiL8opMOg== X-Received: by 2002:aa7:c487:0:b0:4a0:a850:176c with SMTP id m7-20020aa7c487000000b004a0a850176cmr2786951edq.11.1675275040877; Wed, 01 Feb 2023 10:10:40 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:40 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 2/8] cmd: blk: Allow generic read/write operations to work in sandbox Date: Wed, 1 Feb 2023 19:10:10 +0100 Message-Id: <20230201181016.4145834-3-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Ensure that the memory destination/source addresses of block read/write operations are mapped in before access. Currently, this is only needed on sandbox builds. Signed-off-by: Tobias Waldekranz Reviewed-by: Simon Glass --- cmd/blk_common.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/blk_common.c b/cmd/blk_common.c index 75a072caf5..9f9d4327a9 100644 --- a/cmd/blk_common.c +++ b/cmd/blk_common.c @@ -11,6 +11,7 @@ #include #include #include +#include int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, int *cur_devnump) @@ -63,31 +64,37 @@ int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, default: /* at least 4 args */ if (strcmp(argv[1], "read") == 0) { - ulong addr = hextoul(argv[2], NULL); + phys_addr_t paddr = hextoul(argv[2], NULL); lbaint_t blk = hextoul(argv[3], NULL); ulong cnt = hextoul(argv[4], NULL); + void *vaddr; ulong n; printf("\n%s read: device %d block # "LBAFU", count %lu ... ", if_name, *cur_devnump, blk, cnt); + vaddr = map_sysmem(paddr, 512 * cnt); n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt, - (ulong *)addr); + vaddr); + unmap_sysmem(vaddr); printf("%ld blocks read: %s\n", n, n == cnt ? "OK" : "ERROR"); return n == cnt ? 0 : 1; } else if (strcmp(argv[1], "write") == 0) { - ulong addr = hextoul(argv[2], NULL); + phys_addr_t paddr = hextoul(argv[2], NULL); lbaint_t blk = hextoul(argv[3], NULL); ulong cnt = hextoul(argv[4], NULL); + void *vaddr; ulong n; printf("\n%s write: device %d block # "LBAFU", count %lu ... ", if_name, *cur_devnump, blk, cnt); + vaddr = map_sysmem(paddr, 512 * cnt); n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt, - (ulong *)addr); + vaddr); + unmap_sysmem(vaddr); printf("%ld blocks written: %s\n", n, n == cnt ? "OK" : "ERROR"); From patchwork Wed Feb 1 18:10:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735818 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=1dH4B8Y2; 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 4P6Vc941Rsz23gY for ; Thu, 2 Feb 2023 05:21:29 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7D60385BDD; Wed, 1 Feb 2023 19:20:50 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="1dH4B8Y2"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3563E85C63; Wed, 1 Feb 2023 19:10:46 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) (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 A9A2F85C0C for ; Wed, 1 Feb 2023 19:10:42 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x530.google.com with SMTP id z11so18594249ede.1 for ; Wed, 01 Feb 2023 10:10:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=bGQJZ/7YZ4TMG3NPePh7kNuLcSiZqE4YWz5s/xI9zGY=; b=1dH4B8Y2W9Sua4/XwR+KzyHDo9wpV8svAE/ESOXRGpu9zLIQAr/X0pXwzPsKz2ooJN lDJGsrZQ/LklY7EzOdNR+uuwA15mEE4d9kBchKqixsO4M/e68YLyjjO8iNBG61Asugyv D/sLQYkc5fIVfTjhGvA9BG6vqQ52nZ6RrxE/NuMuvml2inhbsfuThEmZFBSwyevyJ2cH P2P4m9tYr6YRI8Ig1SZpPOV8hdam2H+1EEKpNPrSsMqnxbmgutactCwc22o14yCIWGlI n7x5aMi3Hqy7ZwJ9GCmI7hapeNhigyGyX7kr6Hd1yYpOsVXW+I51+x+1aY9oaxz/ipII skUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=bGQJZ/7YZ4TMG3NPePh7kNuLcSiZqE4YWz5s/xI9zGY=; b=oaKWSl+P9q56frK5LKD7Ns4b3pUvFN5heRSicxxMbwMqN9wRBszij6JtTXHi+5eHIu F0g62rlri9LyneOtLSoNUhkbkI2rw4kDXbIbZXPWzNJRC2zJP8dVKSgBynn6g1Xh5CyC 7KCBC28j3vU/19eM7TEIOlwWPKRnp/vowuNpEYUd41HzDmgSM3Umcd89MlS3+zzIEM8C qsYzKJFA/fSbldDfjb5kZShebQPPLOLj6FZcJnEZOadjh7HQVjWayxIvb4Ix4bVjjOxX XmJ0bEaEKlfj3BmS6UoaAJu60WnDDL+WEDFwsQPH+1CO+bws/XF9WQ239QeoeZRygAA0 xQWQ== X-Gm-Message-State: AO0yUKVHBQVtT2XrKTvB1GYcUaTyPMbDBWC6ZgfkDHmUUX1ewwBjKCWh 2kw4oCaMrpfFwrabZyiwYb3FAQ== X-Google-Smtp-Source: AK7set/VGCEhZnku8syTijgEYKntYD3y6P+me8blFPuP5VcX6OzXXC+iItz+smR8LPi1j9sYG923Fg== X-Received: by 2002:a05:6402:3206:b0:4a2:112c:ca11 with SMTP id g6-20020a056402320600b004a2112cca11mr3204735eda.31.1675275042073; Wed, 01 Feb 2023 10:10:42 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:41 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 3/8] blk: blkmap: Add basic infrastructure Date: Wed, 1 Feb 2023 19:10:11 +0100 Message-Id: <20230201181016.4145834-4-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean blkmaps are loosely modeled on Linux's device mapper subsystem. The basic idea is that you can create virtual block devices whose blocks can be backed by a plethora of sources that are user configurable. This change just adds the basic infrastructure for creating and removing blkmap devices. Subsequent changes will extend this to add support for actual mappings. Signed-off-by: Tobias Waldekranz --- MAINTAINERS | 6 + disk/part.c | 1 + drivers/block/Kconfig | 18 ++ drivers/block/Makefile | 1 + drivers/block/blk-uclass.c | 1 + drivers/block/blkmap.c | 275 +++++++++++++++++++++++++++++++ include/blkmap.h | 15 ++ include/dm/uclass-id.h | 1 + include/efi_loader.h | 4 + lib/efi_loader/efi_device_path.c | 30 ++++ 10 files changed, 352 insertions(+) create mode 100644 drivers/block/blkmap.c create mode 100644 include/blkmap.h diff --git a/MAINTAINERS b/MAINTAINERS index 3e8e193ecc..28a34231bf 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -786,6 +786,12 @@ M: Alper Nebi Yasak S: Maintained F: tools/binman/ +BLKMAP +M: Tobias Waldekranz +S: Maintained +F: drivers/block/blkmap.c +F: include/blkmap.h + BOOTDEVICE M: Simon Glass S: Maintained diff --git a/disk/part.c b/disk/part.c index d449635254..35300df590 100644 --- a/disk/part.c +++ b/disk/part.c @@ -140,6 +140,7 @@ void dev_print(struct blk_desc *dev_desc) case UCLASS_NVME: case UCLASS_PVBLOCK: case UCLASS_HOST: + case UCLASS_BLKMAP: printf ("Vendor: %s Rev: %s Prod: %s\n", dev_desc->vendor, dev_desc->revision, diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index e95da48bdc..5a1aeb3d2b 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -67,6 +67,24 @@ config BLOCK_CACHE it will prevent repeated reads from directory structures and other filesystem data structures. +config BLKMAP + bool "Composable virtual block devices (blkmap)" + depends on BLK + help + Create virtual block devices that are backed by various sources, + e.g. RAM, or parts of an existing block device. Though much more + rudimentary, it borrows a lot of ideas from Linux's device mapper + subsystem. + + Example use-cases: + - Treat a region of RAM as a block device, i.e. a RAM disk. This let's + you extract files from filesystem images stored in RAM (perhaps as a + result of a TFTP transfer). + - Create a virtual partition on an existing device. This let's you + access filesystems that aren't stored at an exact partition + boundary. A common example is a filesystem image embedded in an FIT + image. + config SPL_BLOCK_CACHE bool "Use block device cache in SPL" depends on SPL_BLK diff --git a/drivers/block/Makefile b/drivers/block/Makefile index f12447d78d..a161d145fd 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_IDE) += ide.o endif obj-$(CONFIG_SANDBOX) += sandbox.o host-uclass.o host_dev.o obj-$(CONFIG_$(SPL_TPL_)BLOCK_CACHE) += blkcache.o +obj-$(CONFIG_BLKMAP) += blkmap.o obj-$(CONFIG_EFI_MEDIA) += efi-media-uclass.o obj-$(CONFIG_EFI_MEDIA_SANDBOX) += sb_efi_media.o diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index c69fc4d518..cb73faaeda 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -32,6 +32,7 @@ static struct { { UCLASS_EFI_LOADER, "efiloader" }, { UCLASS_VIRTIO, "virtio" }, { UCLASS_PVBLOCK, "pvblock" }, + { UCLASS_BLKMAP, "blkmap" }, }; static enum uclass_id uclass_name_to_iftype(const char *uclass_idname) diff --git a/drivers/block/blkmap.c b/drivers/block/blkmap.c new file mode 100644 index 0000000000..a6ba07404c --- /dev/null +++ b/drivers/block/blkmap.c @@ -0,0 +1,275 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2023 Addiva Elektronik + * Author: Tobias Waldekranz + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct blkmap; + +struct blkmap_slice { + struct list_head node; + + lbaint_t blknr; + lbaint_t blkcnt; + + ulong (*read)(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, void *buffer); + ulong (*write)(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, const void *buffer); + void (*destroy)(struct blkmap *bm, struct blkmap_slice *bms); +}; + +struct blkmap { + struct udevice *dev; + struct list_head slices; +}; + +static bool blkmap_slice_contains(struct blkmap_slice *bms, lbaint_t blknr) +{ + return (blknr >= bms->blknr) && (blknr < (bms->blknr + bms->blkcnt)); +} + +static bool blkmap_slice_available(struct blkmap *bm, struct blkmap_slice *new) +{ + struct blkmap_slice *bms; + lbaint_t first, last; + + first = new->blknr; + last = new->blknr + new->blkcnt - 1; + + list_for_each_entry(bms, &bm->slices, node) { + if (blkmap_slice_contains(bms, first) || + blkmap_slice_contains(bms, last) || + blkmap_slice_contains(new, bms->blknr) || + blkmap_slice_contains(new, bms->blknr + bms->blkcnt - 1)) + return false; + } + + return true; +} + +static struct blkmap *blkmap_from_devnum(int devnum) +{ + struct udevice *dev; + int err; + + err = blk_find_device(UCLASS_BLKMAP, devnum, &dev); + + return err ? NULL : dev_get_priv(dev); +} + +static int blkmap_add(struct blkmap *bm, struct blkmap_slice *new) +{ + struct blk_desc *bd = dev_get_uclass_plat(bm->dev); + struct list_head *insert = &bm->slices; + struct blkmap_slice *bms; + + if (!blkmap_slice_available(bm, new)) + return -EBUSY; + + list_for_each_entry(bms, &bm->slices, node) { + if (bms->blknr < new->blknr) + continue; + + insert = &bms->node; + break; + } + + list_add_tail(&new->node, insert); + + /* Disk might have grown, update the size */ + bms = list_last_entry(&bm->slices, struct blkmap_slice, node); + bd->lba = bms->blknr + bms->blkcnt; + return 0; +} + +static struct udevice *blkmap_root(void) +{ + static struct udevice *dev; + int err; + + if (dev) + return dev; + + err = device_bind_driver(dm_root(), "blkmap_root", "blkmap", &dev); + if (err) + return NULL; + + err = device_probe(dev); + if (err) { + device_unbind(dev); + return NULL; + } + + return dev; +} + +int blkmap_create(int devnum) +{ + struct udevice *root; + struct blk_desc *bd; + struct blkmap *bm; + int err; + + if (devnum >= 0 && blkmap_from_devnum(devnum)) + return -EBUSY; + + root = blkmap_root(); + if (!root) + return -ENODEV; + + bm = calloc(1, sizeof(*bm)); + if (!bm) + return -ENOMEM; + + err = blk_create_devicef(root, "blkmap_blk", "blk", UCLASS_BLKMAP, + devnum, 512, 0, &bm->dev); + if (err) + goto err_free; + + bd = dev_get_uclass_plat(bm->dev); + + /* EFI core isn't keen on zero-sized disks, so we lie. This is + * updated with the correct size once the user adds a + * mapping. + */ + bd->lba = 1; + + dev_set_priv(bm->dev, bm); + INIT_LIST_HEAD(&bm->slices); + + err = blk_probe_or_unbind(bm->dev); + if (err) + goto err_remove; + + return bd->devnum; + +err_remove: + device_remove(bm->dev, DM_REMOVE_NORMAL); +err_free: + free(bm); + return err; +} + +int blkmap_destroy(int devnum) +{ + struct blkmap_slice *bms, *tmp; + struct blkmap *bm; + int err; + + bm = blkmap_from_devnum(devnum); + if (!bm) + return -ENODEV; + + err = device_remove(bm->dev, DM_REMOVE_NORMAL); + if (err) + return err; + + err = device_unbind(bm->dev); + if (err) + return err; + + list_for_each_entry_safe(bms, tmp, &bm->slices, node) { + list_del(&bms->node); + free(bms); + } + + free(bm); + return 0; +} + +static ulong blkmap_read_slice(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, void *buffer) +{ + lbaint_t nr, cnt; + + nr = blknr - bms->blknr; + cnt = (blkcnt < bms->blkcnt) ? blkcnt : bms->blkcnt; + return bms->read(bm, bms, nr, cnt, buffer); +} + +static ulong blkmap_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, + void *buffer) +{ + struct blk_desc *bd = dev_get_uclass_plat(dev); + struct blkmap *bm = dev_get_priv(dev); + struct blkmap_slice *bms; + lbaint_t cnt, total = 0; + + list_for_each_entry(bms, &bm->slices, node) { + if (!blkmap_slice_contains(bms, blknr)) + continue; + + cnt = blkmap_read_slice(bm, bms, blknr, blkcnt, buffer); + blknr += cnt; + blkcnt -= cnt; + buffer += cnt << bd->log2blksz; + total += cnt; + } + + return total; +} + +static ulong blkmap_write_slice(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, + const void *buffer) +{ + lbaint_t nr, cnt; + + nr = blknr - bms->blknr; + cnt = (blkcnt < bms->blkcnt) ? blkcnt : bms->blkcnt; + return bms->write(bm, bms, nr, cnt, buffer); +} + +static ulong blkmap_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, + const void *buffer) +{ + struct blk_desc *bd = dev_get_uclass_plat(dev); + struct blkmap *bm = dev_get_priv(dev); + struct blkmap_slice *bms; + lbaint_t cnt, total = 0; + + list_for_each_entry(bms, &bm->slices, node) { + if (!blkmap_slice_contains(bms, blknr)) + continue; + + cnt = blkmap_write_slice(bm, bms, blknr, blkcnt, buffer); + blknr += cnt; + blkcnt -= cnt; + buffer += cnt << bd->log2blksz; + total += cnt; + } + + return total; +} + +static const struct blk_ops blkmap_ops = { + .read = blkmap_read, + .write = blkmap_write, +}; + +U_BOOT_DRIVER(blkmap_blk) = { + .name = "blkmap_blk", + .id = UCLASS_BLK, + .ops = &blkmap_ops, +}; + +U_BOOT_DRIVER(blkmap_root) = { + .name = "blkmap_root", + .id = UCLASS_BLKMAP, +}; + +UCLASS_DRIVER(blkmap) = { + .id = UCLASS_BLKMAP, + .name = "blkmap", +}; diff --git a/include/blkmap.h b/include/blkmap.h new file mode 100644 index 0000000000..37c0c31c3f --- /dev/null +++ b/include/blkmap.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (c) 2023 Addiva Elektronik + * Author: Tobias Waldekranz + */ + +#ifndef _BLKMAP_H +#define _BLKMAP_H + +#include + +int blkmap_create(int devnum); +int blkmap_destroy(int devnum); + +#endif /* _BLKMAP_H */ diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h index 33e43c20db..576237b954 100644 --- a/include/dm/uclass-id.h +++ b/include/dm/uclass-id.h @@ -37,6 +37,7 @@ enum uclass_id { UCLASS_AUDIO_CODEC, /* Audio codec with control and data path */ UCLASS_AXI, /* AXI bus */ UCLASS_BLK, /* Block device */ + UCLASS_BLKMAP, /* Composable virtual block device */ UCLASS_BOOTCOUNT, /* Bootcount backing store */ UCLASS_BOOTDEV, /* Boot device for locating an OS to boot */ UCLASS_BOOTMETH, /* Bootmethod for booting an OS */ diff --git a/include/efi_loader.h b/include/efi_loader.h index 4560b0d04c..59687f44de 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -134,6 +134,10 @@ static inline efi_status_t efi_launch_capsules(void) #define U_BOOT_GUID \ EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \ 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b) +/* GUID used as root for blkmap devices */ +#define U_BOOT_BLKMAP_DEV_GUID \ + EFI_GUID(0x4cad859d, 0xd644, 0x42ff, \ + 0x87, 0x0b, 0xc0, 0x2e, 0xac, 0x05, 0x58, 0x63) /* GUID used as host device on sandbox */ #define U_BOOT_HOST_DEV_GUID \ EFI_GUID(0xbbe4e671, 0x5773, 0x4ea1, \ diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c index 3b267b713e..4b4c96bc2e 100644 --- a/lib/efi_loader/efi_device_path.c +++ b/lib/efi_loader/efi_device_path.c @@ -21,6 +21,9 @@ #include #include /* U16_MAX */ +#ifdef CONFIG_BLKMAP +const efi_guid_t efi_guid_blkmap_dev = U_BOOT_BLKMAP_DEV_GUID; +#endif #ifdef CONFIG_SANDBOX const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID; #endif @@ -573,6 +576,16 @@ __maybe_unused static unsigned int dp_size(struct udevice *dev) */ return dp_size(dev->parent) + sizeof(struct efi_device_path_vendor) + 1; +#endif +#ifdef CONFIG_BLKMAP + case UCLASS_BLKMAP: + /* + * blkmap devices will be represented as a vendor + * device node with an extra byte for the device + * number. + */ + return dp_size(dev->parent) + + sizeof(struct efi_device_path_vendor) + 1; #endif default: return dp_size(dev->parent); @@ -631,6 +644,23 @@ __maybe_unused static void *dp_fill(void *buf, struct udevice *dev) #endif case UCLASS_BLK: switch (dev->parent->uclass->uc_drv->id) { +#ifdef CONFIG_BLKMAP + case UCLASS_BLKMAP: { + struct efi_device_path_vendor *dp; + struct blk_desc *desc = dev_get_uclass_plat(dev); + + dp_fill(buf, dev->parent); + dp = buf; + ++dp; + dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; + dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; + dp->dp.length = sizeof(*dp) + 1; + memcpy(&dp->guid, &efi_guid_blkmap_dev, + sizeof(efi_guid_t)); + dp->vendor_data[0] = desc->devnum; + return &dp->vendor_data[1]; + } +#endif #ifdef CONFIG_SANDBOX case UCLASS_HOST: { /* stop traversing parents at this point: */ From patchwork Wed Feb 1 18:10:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735819 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=DJ+l9ziy; 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)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P6VcQ5bYFz23gY for ; Thu, 2 Feb 2023 05:21:42 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 43D5E85CA2; Wed, 1 Feb 2023 19:20:52 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="DJ+l9ziy"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0454A85C4E; Wed, 1 Feb 2023 19:10:47 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) (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 CDBC785C59 for ; Wed, 1 Feb 2023 19:10:43 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x52d.google.com with SMTP id fi26so18566073edb.7 for ; Wed, 01 Feb 2023 10:10:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=jY8Gs29dbRAp3KtHxtIuCy/NNerSZ2Ws5EkpjAyA9fY=; b=DJ+l9ziyLiAC4B6jiv5NgDMFPjhqWTBJdVWPTAMBbu5elEkGCsvrfgFsbrmH2EF5nw SRPjZe1KP7Wlbo6VJKJ/pI0YwWFZiaHxPsB5dwGY9oBXCzrnu61C+RMFig6XANbeYUwi NLlb9eyp13WVYkY6G6MciCVfuf1sei2mrJhB5uE8lzc24wh719I8q9hxi7OM8lbjq0US 1g/+oGq8df2Bd3l1CCU/bHmKfzgXk7uzXyINvNALC9w5Uzl5f5GSDNZ4OnGVR9UjiFbB 5pcbMGZvBUA6oo/T9/K1NW//BuDxtjlK+ieVuOY/SIypmEzFkxO5fiV+u3B4Hfx1r7sD IIag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=jY8Gs29dbRAp3KtHxtIuCy/NNerSZ2Ws5EkpjAyA9fY=; b=F3wYIry6R/+7ACXwkQtcSvZAKlN0NQ2/hk6LD6MGJiiCGO2d66Asl5bNxpP/0z4WL2 PhRsI9ZgYH0nXwVqxwDJhH3gZX8tR43X8EkXK7kbJz1eSIy0mYD+Yvr+WLoWeo/tXlvP Pd7yo2nRcoyOOJoQ9tpENxAUC2kc+YHZMgxk/UYdgYxY/0ETYZoc69YAmZ8s5wazq3fG UpAiiP/FaC7q7Zs4nJXlkwZRfDFQ8+y+4H9YBY/UVlj1pnkVE3ZkFkZRTeLftPO6FK76 fl9ypZawOLHygtO+oQ2vNd32jj8KOdA8vhqC9XB6eCdAG8XAWDSVBvWOEpGGV9pSbVZU EC/A== X-Gm-Message-State: AO0yUKVQg2V76cgM4ISu4/yayqImxnOM0F2JigRGUI2OZxRjXJCYb45t a2HwTvyY+5T0owItY1xsOSda0w== X-Google-Smtp-Source: AK7set/yyOr44JvEvuHXME14Lp4YA7FZtijbBVBrCwwc7Xp8c8i5wmqzGn+6KXCregyNbWJ/7kSA0g== X-Received: by 2002:a05:6402:1779:b0:4a2:3bef:e000 with SMTP id da25-20020a056402177900b004a23befe000mr2496109edb.42.1675275043305; Wed, 01 Feb 2023 10:10:43 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:42 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 4/8] blk: blkmap: Add memory mapping support Date: Wed, 1 Feb 2023 19:10:12 +0100 Message-Id: <20230201181016.4145834-5-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Allow a slice of RAM to be mapped to a blkmap. This means that RAM can now be accessed as if it was a block device, meaning that existing filesystem drivers can now be used to access ramdisks. Signed-off-by: Tobias Waldekranz --- drivers/block/blkmap.c | 106 +++++++++++++++++++++++++++++++++++++++++ include/blkmap.h | 4 ++ 2 files changed, 110 insertions(+) diff --git a/drivers/block/blkmap.c b/drivers/block/blkmap.c index a6ba07404c..c8c2dcac11 100644 --- a/drivers/block/blkmap.c +++ b/drivers/block/blkmap.c @@ -12,6 +12,7 @@ #include #include #include +#include #include struct blkmap; @@ -93,6 +94,111 @@ static int blkmap_add(struct blkmap *bm, struct blkmap_slice *new) return 0; } +struct blkmap_mem { + struct blkmap_slice slice; + void *addr; + bool remapped; +}; + +static ulong blkmap_mem_read(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, void *buffer) +{ + struct blkmap_mem *bmm = container_of(bms, struct blkmap_mem, slice); + struct blk_desc *bd = dev_get_uclass_plat(bm->dev); + char *src; + + src = bmm->addr + (blknr << bd->log2blksz); + memcpy(buffer, src, blkcnt << bd->log2blksz); + return blkcnt; +} + +static ulong blkmap_mem_write(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, + const void *buffer) +{ + struct blkmap_mem *bmm = container_of(bms, struct blkmap_mem, slice); + struct blk_desc *bd = dev_get_uclass_plat(bm->dev); + char *dst; + + dst = bmm->addr + (blknr << bd->log2blksz); + memcpy(dst, buffer, blkcnt << bd->log2blksz); + return blkcnt; +} + +static void blkmap_mem_destroy(struct blkmap *bm, struct blkmap_slice *bms) +{ + struct blkmap_mem *bmm = container_of(bms, struct blkmap_mem, slice); + + if (bmm->remapped) + unmap_sysmem(bmm->addr); +} + +int __blkmap_map_mem(int devnum, lbaint_t blknr, lbaint_t blkcnt, void *addr, + bool remapped) +{ + struct blkmap_mem *bmm; + struct blkmap *bm; + int err; + + bm = blkmap_from_devnum(devnum); + if (!bm) + return -ENODEV; + + bmm = malloc(sizeof(*bmm)); + if (!bmm) + return -ENOMEM; + + *bmm = (struct blkmap_mem) { + .slice = { + .blknr = blknr, + .blkcnt = blkcnt, + + .read = blkmap_mem_read, + .write = blkmap_mem_write, + .destroy = blkmap_mem_destroy, + }, + + .addr = addr, + .remapped = remapped, + }; + + err = blkmap_add(bm, &bmm->slice); + if (err) + free(bmm); + + return err; +} + +int blkmap_map_mem(int devnum, lbaint_t blknr, lbaint_t blkcnt, void *addr) +{ + return __blkmap_map_mem(devnum, blknr, blkcnt, addr, false); +} + +int blkmap_map_pmem(int devnum, lbaint_t blknr, lbaint_t blkcnt, + phys_addr_t paddr) +{ + struct blk_desc *bd; + struct blkmap *bm; + void *addr; + int err; + + bm = blkmap_from_devnum(devnum); + if (!bm) + return -ENODEV; + + bd = dev_get_uclass_plat(bm->dev); + + addr = map_sysmem(paddr, blkcnt << bd->log2blksz); + if (!addr) + return -ENOMEM; + + err = __blkmap_map_mem(devnum, blknr, blkcnt, addr, true); + if (err) + unmap_sysmem(addr); + + return err; +} + static struct udevice *blkmap_root(void) { static struct udevice *dev; diff --git a/include/blkmap.h b/include/blkmap.h index 37c0c31c3f..a93611ff62 100644 --- a/include/blkmap.h +++ b/include/blkmap.h @@ -9,6 +9,10 @@ #include +int blkmap_map_mem(int devnum, lbaint_t blknr, lbaint_t blkcnt, void *addr); +int blkmap_map_pmem(int devnum, lbaint_t blknr, lbaint_t blkcnt, + phys_addr_t paddr); + int blkmap_create(int devnum); int blkmap_destroy(int devnum); From patchwork Wed Feb 1 18:10:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735821 X-Patchwork-Delegate: trini@ti.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=85.214.62.61; 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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=adBt3XYz; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P6Vcf6Klzz23gY for ; Thu, 2 Feb 2023 05:21:54 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5D9B885CBF; Wed, 1 Feb 2023 19:20:54 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="adBt3XYz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4B81785C59; Wed, 1 Feb 2023 19:10:47 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) (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 D198085C67 for ; Wed, 1 Feb 2023 19:10:44 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x52d.google.com with SMTP id fi26so18566161edb.7 for ; Wed, 01 Feb 2023 10:10:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=cUVh1i/CJWMOCg30h9lkKNlu+lDB5FtfmzlwKcgT5Ls=; b=adBt3XYzI1TEZ8EogqNG9W1SDrYAPJ7zbXyC39QepNfhlQZlvXE03gr+w0RSSNh6AM mGbhXcfwaq2nfkk2ZFKUTJTTSuMxOfW4NfVrVgxDrwsPH9n1qkCXJ33v0vjVFxdguQe4 zlSZ9QYgp/0WuEcp7AxOfZTK5aYFwGR4Mc8HUcZ+B5aVyslan2ArVU3zRtC709eYxZQA w3YwG3IwOj9kG1iczblmJ1nv8Itdgg3/UXDNu+qAKmxQxzP7vvGrs80STbYvbfFzGepa D84VQthDc9X5DXm9KFjYlJ7FHG5P6Q6K7EvNbTpQK5PyN9uVJ1YdhcSEAhz33VCFQozd Gkjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=cUVh1i/CJWMOCg30h9lkKNlu+lDB5FtfmzlwKcgT5Ls=; b=Z7ljGmH+qc8K+YWiJUuvnY6CzzTmhR/e/MJ1vCdHFzH11yk87fbm9/4tHDY3h/N+3T Uvclpg03me0XUQfdziQOlfGvgpY6QnCIfZvCpxKel3MFTcxOUqqXMOYExGA/3tmBDM22 DYsXsu3hcASSCpr0+I7h6nj4Dz6yAcPUEnlwlWLbUgQg8RIPsdQ0Bv9CcJ9wDVa/iZ4b VH2YkU+vog4XNg98rFxkl5NkBTkl6A232YksIObHdq2BWYAKKFS0OvsgOnR1MRDAlgxP Dh9XZV8kR1nOjoFX+JhNO15e/RqLYv8D3RAB/PTcmMmk3jUYWSX4JqBnZGwmMM/ywF7R Gsaw== X-Gm-Message-State: AO0yUKWW1AzJUNAOz+26NQDSFZpuGxLHtPb4mzwVzDKrxTwmA1ZAAKkE Bws7yXkSu1IEeiiyQ+O4nDyPWg== X-Google-Smtp-Source: AK7set846cHZDeeozyheyfi8s1zelmXlAbGGl2DTxhlsSyxFW00EbBvrY+Lm7aCYl1MO8t2wcs24zg== X-Received: by 2002:a05:6402:b32:b0:49c:1fe4:9efc with SMTP id bo18-20020a0564020b3200b0049c1fe49efcmr3187368edb.40.1675275044603; Wed, 01 Feb 2023 10:10:44 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:44 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 5/8] blk: blkmap: Add linear device mapping support Date: Wed, 1 Feb 2023 19:10:13 +0100 Message-Id: <20230201181016.4145834-6-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Allow a slice of an existing block device to be mapped to a blkmap. This means that filesystems that are not stored at exact partition boundaries can be accessed by remapping a slice of the existing device to a blkmap device. Signed-off-by: Tobias Waldekranz --- drivers/block/blkmap.c | 71 ++++++++++++++++++++++++++++++++++++++++++ include/blkmap.h | 2 ++ 2 files changed, 73 insertions(+) diff --git a/drivers/block/blkmap.c b/drivers/block/blkmap.c index c8c2dcac11..14d2ec3f78 100644 --- a/drivers/block/blkmap.c +++ b/drivers/block/blkmap.c @@ -94,6 +94,77 @@ static int blkmap_add(struct blkmap *bm, struct blkmap_slice *new) return 0; } +struct blkmap_linear { + struct blkmap_slice slice; + + struct blk_desc *bd; + lbaint_t blknr; +}; + +static ulong blkmap_linear_read(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, void *buffer) +{ + struct blkmap_linear *bml = container_of(bms, struct blkmap_linear, slice); + + return blk_dread(bml->bd, bml->blknr + blknr, blkcnt, buffer); +} + +static ulong blkmap_linear_write(struct blkmap *bm, struct blkmap_slice *bms, + lbaint_t blknr, lbaint_t blkcnt, + const void *buffer) +{ + struct blkmap_linear *bml = container_of(bms, struct blkmap_linear, slice); + + return blk_dwrite(bml->bd, bml->blknr + blknr, blkcnt, buffer); +} + +int blkmap_map_linear(int devnum, lbaint_t blknr, lbaint_t blkcnt, + enum uclass_id lcls, int ldevnum, lbaint_t lblknr) +{ + struct blkmap_linear *linear; + struct blk_desc *bd, *lbd; + struct blkmap *bm; + int err; + + bm = blkmap_from_devnum(devnum); + if (!bm) + return -ENODEV; + + bd = dev_get_uclass_plat(bm->dev); + lbd = blk_get_devnum_by_uclass_id(lcls, ldevnum); + if (!lbd) + return -ENODEV; + + if (lbd->blksz != bd->blksz) + /* We could support block size translation, but we + * don't yet. + */ + return -EINVAL; + + linear = malloc(sizeof(*linear)); + if (!linear) + return -ENOMEM; + + *linear = (struct blkmap_linear) { + .slice = { + .blknr = blknr, + .blkcnt = blkcnt, + + .read = blkmap_linear_read, + .write = blkmap_linear_write, + }, + + .bd = lbd, + .blknr = lblknr, + }; + + err = blkmap_add(bm, &linear->slice); + if (err) + free(linear); + + return err; +} + struct blkmap_mem { struct blkmap_slice slice; void *addr; diff --git a/include/blkmap.h b/include/blkmap.h index a93611ff62..dca6e3fe6a 100644 --- a/include/blkmap.h +++ b/include/blkmap.h @@ -9,6 +9,8 @@ #include +int blkmap_map_linear(int devnum, lbaint_t blknr, lbaint_t blkcnt, + enum uclass_id lcls, int ldevnum, lbaint_t lblknr); int blkmap_map_mem(int devnum, lbaint_t blknr, lbaint_t blkcnt, void *addr); int blkmap_map_pmem(int devnum, lbaint_t blknr, lbaint_t blkcnt, phys_addr_t paddr); From patchwork Wed Feb 1 18:10:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735822 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=8VOdcLXK; 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)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P6Vcv2pN8z23gY for ; Thu, 2 Feb 2023 05:22:07 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DD6C385CA4; Wed, 1 Feb 2023 19:20:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="8VOdcLXK"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BAABF85C65; Wed, 1 Feb 2023 19:10:49 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) (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 6CF8F85C64 for ; Wed, 1 Feb 2023 19:10:46 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x52e.google.com with SMTP id k4so7821569edo.12 for ; Wed, 01 Feb 2023 10:10:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=ef4PKYB9t1Yh64GFiPyjDutderZ4TBRcQ7CsURxH5WU=; b=8VOdcLXKyqa+TLcrMlt/HDliDXvpb2uG8x24/LNRDem97VW8e1/WXJsb1YGJi03L+c Byoec1nDYm4ju3kgmHVIfPM8XUO0E7+HUWXmewOjErZ0K5D45PHPOcZgbIVHKiXRBMSS FoHHdA4xwnnbwdsTRPKNArqFzjhbCiaoj6zaNGVOB3gHyRr6brlzOmF643X57sMoETZK dCUbI40/wfrnvs8cpH0dvPa8PAiGUw4jXEqbxUJSZjx+on4f8x226C3M2Gdh/uJG+mco hws2QejNwNAPmmH0DiZGs0gr/CtulZ0NTtnr818SbyC+JhcDlXjY9RHPeXIeLQTUbbAg uU2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=ef4PKYB9t1Yh64GFiPyjDutderZ4TBRcQ7CsURxH5WU=; b=44A3sb3BgufEgjKfLzdXCn5m1hhGDFEwPIqeaTOag0wyGIo7zoNRnul/sI4C2BqTmR 3tJrD//q66ZXGAUd5xgrNUGO25vaiIxA/Ne5yvCLosUyN/93XNureOZES0Z7+A2BktI+ X0DZByA6lHZFEu3jlTCq8NOmx8XJXpROwysL+As/IGnUGrklNIwDvEEd9tfDsAys/tij Lk8zFBo4Q7+MMo1l78k5SnaNcoU4NnQa8c6S2/KHUkpna1aIkzgqcZGZUOqyLzBIL22Q hvlzFbINkCahLVgPkj5jLW3eJPFAMY24EoZGhYj6Xbpqt/TVJ9i1mnDwnNUzDtGcG/jC RbDA== X-Gm-Message-State: AO0yUKXdVMEAcWKcB+Af8/8nF8MkOjB8B8iLjJuMZCBUkwW9G3hnNDlq ZHEKj70A0kXqqZVZe6HJ59105A== X-Google-Smtp-Source: AK7set8X2VDIvqUy3vOHFkcbdH6EpPqMIXaWv+5yTxngmxGMBz3dHmv/aBmx3Kh5pXMbG09aioU/fA== X-Received: by 2002:a05:6402:2206:b0:492:bf3d:1a16 with SMTP id cq6-20020a056402220600b00492bf3d1a16mr2765227edb.4.1675275045844; Wed, 01 Feb 2023 10:10:45 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:45 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 6/8] cmd: blkmap: Add blkmap command Date: Wed, 1 Feb 2023 19:10:14 +0100 Message-Id: <20230201181016.4145834-7-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Add a frontend for the blkmap subsystem. In addition to the common block device operations, this allows users to create and destroy devices, and map in memory and slices of other block devices. With that we support two primary use-cases: - Being able to "distro boot" from a RAM disk. I.e., from an image where the kernel is stored in /boot of some filesystem supported by U-Boot. - Accessing filesystems not located on exact partition boundaries, e.g. when a filesystem image is wrapped in an FIT image and stored in a disk partition. Signed-off-by: Tobias Waldekranz --- MAINTAINERS | 1 + cmd/Kconfig | 19 ++++++ cmd/Makefile | 1 + cmd/blkmap.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+) create mode 100644 cmd/blkmap.c diff --git a/MAINTAINERS b/MAINTAINERS index 28a34231bf..83c0f90a53 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -789,6 +789,7 @@ F: tools/binman/ BLKMAP M: Tobias Waldekranz S: Maintained +F: cmd/blkmap.c F: drivers/block/blkmap.c F: include/blkmap.h diff --git a/cmd/Kconfig b/cmd/Kconfig index dc0446e02e..cd35b8318d 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1953,6 +1953,25 @@ config CMD_BLOCK_CACHE during development, but also allows the cache to be disabled when it might hurt performance (e.g. when using the ums command). +config CMD_BLKMAP + bool "blkmap - Composable virtual block devices" + depends on BLKMAP + default y if BLKMAP + help + Create virtual block devices that are backed by various sources, + e.g. RAM, or parts of an existing block device. Though much more + rudimentary, it borrows a lot of ideas from Linux's device mapper + subsystem. + + Example use-cases: + - Treat a region of RAM as a block device, i.e. a RAM disk. This let's + you extract files from filesystem images stored in RAM (perhaps as a + result of a TFTP transfer). + - Create a virtual partition on an existing device. This let's you + access filesystems that aren't stored at an exact partition + boundary. A common example is a filesystem image embedded in an FIT + image. + config CMD_BUTTON bool "button" depends on BUTTON diff --git a/cmd/Makefile b/cmd/Makefile index 7b6ff73186..1d51fddec1 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -27,6 +27,7 @@ obj-$(CONFIG_CMD_BCB) += bcb.o obj-$(CONFIG_CMD_BDI) += bdinfo.o obj-$(CONFIG_CMD_BIND) += bind.o obj-$(CONFIG_CMD_BINOP) += binop.o +obj-$(CONFIG_CMD_BLKMAP) += blkmap.o obj-$(CONFIG_CMD_BLOBLIST) += bloblist.o obj-$(CONFIG_CMD_BLOCK_CACHE) += blkcache.o obj-$(CONFIG_CMD_BMP) += bmp.o diff --git a/cmd/blkmap.c b/cmd/blkmap.c new file mode 100644 index 0000000000..f1d4a4bab0 --- /dev/null +++ b/cmd/blkmap.c @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2023 Addiva Elektronik + * Author: Tobias Waldekranz + */ + +#include +#include +#include +#include +#include + +static int blkmap_curr_dev; + +struct map_ctx { + int devnum; + lbaint_t blknr, blkcnt; +}; + +typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]); + +struct map_handler { + const char *name; + map_parser_fn fn; +}; + +int do_blkmap_map_linear(struct map_ctx *ctx, int argc, char *const argv[]) +{ + struct blk_desc *lbd; + int err, ldevnum; + lbaint_t lblknr; + + if (argc < 4) + return CMD_RET_USAGE; + + ldevnum = dectoul(argv[2], NULL); + lblknr = dectoul(argv[3], NULL); + + lbd = blk_get_devnum_by_uclass_idname(argv[1], ldevnum); + if (!lbd) { + printf("Found no device matching \"%s %d\"\n", + argv[1], ldevnum); + return CMD_RET_FAILURE; + } + + err = blkmap_map_linear(ctx->devnum, ctx->blknr, ctx->blkcnt, + lbd->uclass_id, ldevnum, lblknr); + if (err) { + printf("Unable to map \"%s %d\" at block 0x" LBAF ": %d\n", + argv[1], ldevnum, ctx->blknr, err); + + return CMD_RET_FAILURE; + } + + printf("Block 0x" LBAF "+0x" LBAF " mapped to block 0x" LBAF " of \"%s %d\"\n", + ctx->blknr, ctx->blkcnt, lblknr, argv[1], ldevnum); + return CMD_RET_SUCCESS; +} + +int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[]) +{ + phys_addr_t addr; + int err; + + if (argc < 2) + return CMD_RET_USAGE; + + addr = hextoul(argv[1], NULL); + + err = blkmap_map_pmem(ctx->devnum, ctx->blknr, ctx->blkcnt, addr); + if (err) { + printf("Unable to map %#llx at block 0x" LBAF ": %d\n", + (unsigned long long)addr, ctx->blknr, err); + return CMD_RET_FAILURE; + } + + printf("Block 0x" LBAF "+0x" LBAF " mapped to %#llx\n", + ctx->blknr, ctx->blkcnt, (unsigned long long)addr); + return CMD_RET_SUCCESS; +} + +struct map_handler map_handlers[] = { + { .name = "linear", .fn = do_blkmap_map_linear }, + { .name = "mem", .fn = do_blkmap_map_mem }, + + { .name = NULL } +}; + +static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) +{ + struct map_handler *handler; + struct map_ctx ctx; + + if (argc < 5) + return CMD_RET_USAGE; + + ctx.devnum = dectoul(argv[1], NULL); + ctx.blknr = hextoul(argv[2], NULL); + ctx.blkcnt = hextoul(argv[3], NULL); + argc -= 4; + argv += 4; + + for (handler = map_handlers; handler->name; handler++) { + if (!strcmp(handler->name, argv[0])) + return handler->fn(&ctx, argc, argv); + } + + printf("Unknown map type \"%s\"\n", argv[0]); + return CMD_RET_USAGE; +} + +static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) +{ + int devnum = -1; + + if (argc == 2) + devnum = dectoul(argv[1], NULL); + + devnum = blkmap_create(devnum); + if (devnum < 0) { + printf("Unable to create device: %d\n", devnum); + return CMD_RET_FAILURE; + } + + printf("Created device %d\n", devnum); + return CMD_RET_SUCCESS; +} + +static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) +{ + int err, devnum; + + if (argc != 2) + return CMD_RET_USAGE; + + devnum = dectoul(argv[1], NULL); + + err = blkmap_destroy(devnum); + if (err) { + printf("Unable to destroy device %d: %d\n", devnum, err); + return CMD_RET_FAILURE; + } + + printf("Destroyed device %d\n", devnum); + return CMD_RET_SUCCESS; +} + +static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag, + int argc, char *const argv[]) +{ + /* The subcommand parsing pops the original argv[0] ("blkmap") + * which blk_common_cmd expects. Push it back again. + */ + argc++; + argv--; + + return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev); +} + +U_BOOT_CMD_WITH_SUBCMDS( + blkmap, "Composeable virtual block devices", + "info - list configured devices\n" + "blkmap part - list available partitions on current blkmap device\n" + "blkmap dev [] - show or set current blkmap device\n" + "blkmap read \n" + "blkmap write \n" + "blkmap create [] - create device\n" + "blkmap destroy - destroy device\n" + "blkmap map linear - device mapping\n" + "blkmap map mem - memory mapping\n", + U_BOOT_SUBCMD_MKENT(info, 2, 1, do_blkmap_common), + U_BOOT_SUBCMD_MKENT(part, 2, 1, do_blkmap_common), + U_BOOT_SUBCMD_MKENT(dev, 4, 1, do_blkmap_common), + U_BOOT_SUBCMD_MKENT(read, 5, 1, do_blkmap_common), + U_BOOT_SUBCMD_MKENT(write, 5, 1, do_blkmap_common), + U_BOOT_SUBCMD_MKENT(create, 2, 1, do_blkmap_create), + U_BOOT_SUBCMD_MKENT(destroy, 2, 1, do_blkmap_destroy), + U_BOOT_SUBCMD_MKENT(map, 32, 1, do_blkmap_map)); From patchwork Wed Feb 1 18:10:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735823 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=0rMTBdWv; 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)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P6Vd63vCmz23gY for ; Thu, 2 Feb 2023 05:22:18 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A932F85CC2; Wed, 1 Feb 2023 19:20:58 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="0rMTBdWv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4E0EC85C71; Wed, 1 Feb 2023 19:10:51 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) (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 7D56885C5B for ; Wed, 1 Feb 2023 19:10:47 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x531.google.com with SMTP id v13so18543426eda.11 for ; Wed, 01 Feb 2023 10:10:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=NiJoPze2Rd9/cmi6Bjr6QrL9XsMMnAXV9xkuZvwhTF8=; b=0rMTBdWvL147Q4Bh+mL3D2h9ZatH54BIzmrD3dLRDjVPe8M8yNcdU0JrweI5kKvNTU gUml9HvWAipLsPLfu2oo7EGDKXb24Zk3xD5/TKg4B+fgoBxosMY3fuhegwif4YC3UgU8 Vg/EhmiuiHTbhL38vt2VQZy0zL5fqdQC1LXkDafMZbLOeU/F3XhuJ5HJ/QQHy+Zd2SvR 5S/NQC6qLPBpdk96+zR3KBkjqonWak91bu/+aAkU5/p+ocqvrtVJpxMht/5NAcRXj/+v 4UsIH/OM4YvUKBk1ymMZ0oE3lp4qw3ojv0uBPJ1TEZo0elvytqO5aaD8Eytu11mOfuwj 8i8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=NiJoPze2Rd9/cmi6Bjr6QrL9XsMMnAXV9xkuZvwhTF8=; b=sA/aApp5tbmkkf6uGqxqeXXgpTlWO3k42NXXqia2XFWZslm9Eu7y9OqQm79bBog0Mc KXOtR9tKJa7skeSFJr1K0TX2wIEX43/5udfmFhLPyM6Zp+rHFyniwNto6zK+PfhG4goD lStPRTZyVftiNinO36ZH0FxJniu3IXlJl2EjdCqNpHOBJYCB4f2yKjSMGXhrzTNYZtDy M0LyDe/KBPam8V4DDRQUtZ3b5M4vy0lm4hmDAQVohvqGfblI8x3/2ghABZf83vAv2cuR HtymRnhUw/OY2mk7SPJLtWcU82wxLiHV3SnkvfuUsmKwRp24r/6NSH8rVdP4WyZ1Oy1p pArw== X-Gm-Message-State: AO0yUKVEPgYymtPTvy9edyMIqO65JiUYJojv67W1V5zI494PJALCQRJ/ AqGwCAXgnxJ0ytLSPzNJDrLybw== X-Google-Smtp-Source: AK7set+IeMs3L4hrtEJZk4rrpOXkYRMtq9bvD9//iJLlW6+ixbDPFY8usGKRTzYsKtRoNc71z2Dq3A== X-Received: by 2002:aa7:d852:0:b0:4a2:b97:adc4 with SMTP id f18-20020aa7d852000000b004a20b97adc4mr2995047eds.38.1675275046935; Wed, 01 Feb 2023 10:10:46 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:46 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 7/8] test: blkmap: Add test suite Date: Wed, 1 Feb 2023 19:10:15 +0100 Message-Id: <20230201181016.4145834-8-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Verify that: - Block maps can be created and destroyed - Mappings aren't allowed to overlap - Multiple mappings can be attached and be read/written from/to Signed-off-by: Tobias Waldekranz --- MAINTAINERS | 1 + configs/sandbox_defconfig | 1 + test/py/tests/test_blkmap.py | 164 +++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 test/py/tests/test_blkmap.py diff --git a/MAINTAINERS b/MAINTAINERS index 83c0f90a53..c420c8e1f9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -792,6 +792,7 @@ S: Maintained F: cmd/blkmap.c F: drivers/block/blkmap.c F: include/blkmap.h +F: test/py/tests/test_blkmap.py BOOTDEVICE M: Simon Glass diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index 34c342b6f5..06021e4902 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -145,6 +145,7 @@ CONFIG_ADC=y CONFIG_ADC_SANDBOX=y CONFIG_AXI=y CONFIG_AXI_SANDBOX=y +CONFIG_BLKMAP=y CONFIG_SYS_IDE_MAXBUS=1 CONFIG_SYS_ATA_BASE_ADDR=0x100 CONFIG_SYS_ATA_STRIDE=4 diff --git a/test/py/tests/test_blkmap.py b/test/py/tests/test_blkmap.py new file mode 100644 index 0000000000..5a4c770c81 --- /dev/null +++ b/test/py/tests/test_blkmap.py @@ -0,0 +1,164 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (c) 2023 Addiva Elektronik +# Author: Tobias Waldekranz + +""" Unit test for blkmap command +""" + +import pytest + +BLKSZ = 0x200 + +MAPPING = [ + ((0, 1), 3), + ((1, 3), 0), + ((4, 2), 6), + ((6, 2), 4), +] + +ORDERED = 0x0000 +UNORDERED = 0x1000 +BUFFER = 0x2000 + +def mkblob(base, mapping): + cmds = [] + + for ((blksrc, blkcnt), blkdst) in mapping: + for blknr in range(blkcnt): + cmds.append(f"mw.b 0x{base + (blkdst + blknr) * BLKSZ:x}" + + f" 0x{blksrc + blknr:x} 0x{BLKSZ:x}") + return cmds + +class Blkmap(object): + def __init__(self, console, num): + self.console, self.num = console, num + + def __enter__(self): + r = self.console.run_command(f"blkmap create {self.num}") + assert(f"Created device {self.num}" in r) + + r = self.console.run_command(f"blkmap dev {self.num}") + assert("is now current device" in r) + + return self + + def __exit__(self, typ, value, traceback): + r = self.console.run_command(f"blkmap destroy {self.num}") + assert(f"Destroyed device {self.num}" in r) + + def map_mem(self, blknr, blkcnt, addr): + r = self.console.run_command( + f"blkmap map {self.num} {blknr:#x} {blkcnt:#x} mem {addr:#x}" + ) + assert(" mapped to " in r) + + def read(self, addr, blknr, blkcnt): + r = self.console.run_command( + f"blkmap read {addr:#x} {blknr:#x} {blkcnt:#x}" + ) + assert(" OK" in r) + + def write(self, addr, blknr, blkcnt): + r = self.console.run_command( + f"blkmap write {addr:#x} {blknr:#x} {blkcnt:#x}" + ) + assert(" OK" in r) + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_blkmap') +def test_blkmap_creation(u_boot_console): + """ Verify that blkmaps can be created and destroyed + + Args: + u_boot_console -- U-Boot console + """ + with Blkmap(u_boot_console, 0): + # Can't have 2 blkmap 0's + with pytest.raises(AssertionError): + with Blkmap(u_boot_console, 0): + pass + + # But blkmap 1 should be fine + with Blkmap(u_boot_console, 1): + pass + + # Once blkmap 0 is destroyed, we should be able to create it again + with Blkmap(u_boot_console, 0): + pass + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_blkmap') +def test_blkmap_slicing(u_boot_console): + """ Verify that slices aren't allowed to overlap + + Args: + u_boot_console -- U-Boot console + """ + with Blkmap(u_boot_console, 0) as bm: + bm.map_mem(8, 8, 0) + + # Can't overlap on the low end + with pytest.raises(AssertionError): + bm.map_mem(4, 5, 0) + + # Can't be inside + with pytest.raises(AssertionError): + bm.map_mem(10, 2, 0) + + # Can't overlap on the high end + with pytest.raises(AssertionError): + bm.map_mem(15, 4, 0) + + # But we should be able to add slices right before and after + bm.map_mem( 4, 4, 0) + bm.map_mem(16, 4, 0) + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_blkmap') +def test_blkmap_mem_read(u_boot_console): + """ Test reading from a memory backed blkmap + + Args: + u_boot_console -- U-Boot console + """ + + # Generate an ordered and an unordered pattern in memory + u_boot_console.run_command_list(mkblob(ORDERED, (((0, 8), 0),))) + u_boot_console.run_command_list(mkblob(UNORDERED, MAPPING)) + + with Blkmap(u_boot_console, 0) as bm: + # Create a blkmap that cancels out the disorder + for ((blksrc, blkcnt), blkdst) in MAPPING: + bm.map_mem(blksrc, blkcnt, UNORDERED + blkdst * BLKSZ) + + # Read out the data via the blkmap device to another area + bm.read(BUFFER, 0, 8) + + # And verify that it matches the ordered pattern + response = u_boot_console.run_command(f"cmp.b 0x{BUFFER:x} 0x{ORDERED:x} 0x1000") + assert("Total of 4096 byte(s) were the same" in response) + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_blkmap') +def test_blkmap_mem_write(u_boot_console): + """ Test writing to a memory backed blkmap + + Args: + u_boot_console -- U-Boot console + """ + # Generate an ordered and an unordered pattern in memory + u_boot_console.run_command_list(mkblob(ORDERED, (((0, 8), 0),))) + u_boot_console.run_command_list(mkblob(UNORDERED, MAPPING)) + + with Blkmap(u_boot_console, 0) as bm: + # Create a blkmap that mimics the disorder + for ((blksrc, blkcnt), blkdst) in MAPPING: + bm.map_mem(blksrc, blkcnt, BUFFER + blkdst * BLKSZ) + + # Write the ordered data via the blkmap device to another area + bm.write(ORDERED, 0, 8) + + # And verify that the result matches the unordered pattern + response = u_boot_console.run_command(f"cmp.b 0x{BUFFER:x} 0x{UNORDERED:x} 0x1000") + assert("Total of 4096 byte(s) were the same" in response) From patchwork Wed Feb 1 18:10:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Waldekranz X-Patchwork-Id: 1735824 X-Patchwork-Delegate: trini@ti.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=jykwwoFU; 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)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4P6VdK3yclz23gY for ; Thu, 2 Feb 2023 05:22:29 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8140185CCB; Wed, 1 Feb 2023 19:21:00 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.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=waldekranz-com.20210112.gappssmtp.com header.i=@waldekranz-com.20210112.gappssmtp.com header.b="jykwwoFU"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A8E7B856B9; Wed, 1 Feb 2023 19:10:52 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) (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 09AFE85C67 for ; Wed, 1 Feb 2023 19:10:49 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=waldekranz.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=tobias@waldekranz.com Received: by mail-ed1-x52e.google.com with SMTP id k4so7821692edo.12 for ; Wed, 01 Feb 2023 10:10:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=waldekranz-com.20210112.gappssmtp.com; s=20210112; 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=rkpOK+14lM95cBe4281qXlVpvQAGK9wUpLh4rrS2tFo=; b=jykwwoFUIgJd+bZ09XlZ1IDjiNBMb9CO3PUDFTIluphPybySnU7FSxkyYaQWAmJM0q eGwPFYFB7w20vtJonbXQvn1CPckAR8iEbgT2pYAPwuoPeqXcUIesvdw7MQHfpvgzX0P5 yug5zB0aB0uF4jggFwc2cIy94joad16j0leqqfF8ttQ5XbnSLrzleWmweql0WzJygK1+ OmD78Guy6Ku5xgOmupw/BMLTKzYHK/v4lJX9EVX4I3XQyy7CBvPjcgo01dXl9tL1Uodh tvQoaBr/pYy9BUvlFwrMnM22xDWJeB0p8eMt0f8c3pGLSl8cP3QF96t8K+HWkGl3QWG5 GkcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=rkpOK+14lM95cBe4281qXlVpvQAGK9wUpLh4rrS2tFo=; b=F47bWJpD5GK05TCi++A/zxMlomU7T0d8GIZ50I26aOC245UP9uQAJZDvUm3PBYUymo 1yVYp/if4xjS8+GhHC/FbaI9wr6jLF6Mg/szQy4I92v+ePy8esDSxRNKfYQEFMnZebFK COsfdD8VMRn52/18kHjN1V0lN6Fef0D8SqHA2gExwjwxfTu/hEPU/EOUFGWCOCuvVs3a Y7QZfizCPRLWTmqvFlvSiMLUp5J0+3JOcbIZ6jGhNaFSyzVF3HOz+cjhG0l95f5HFL21 u8/8RRlPp1xhcbvyVADLjJ2WteWLb3owAFTi1o/9TTE2b9/OLGyKKzEypWcZUjfvy+0q DbFQ== X-Gm-Message-State: AO0yUKUtNg1pb2c3mj9vwe4OcuSpdgKNwY2meGDZg5UPiqTp1J03RbmM aOpxMjhMDKg/b5jMlDWeJ5imeED1BcsscW51 X-Google-Smtp-Source: AK7set8gd/TAjM/ZVWAAmIzWkv9z7LcXHf0LFMqL0Pdyu9lp4pENZAURIyVm9tgiwkyQZm/Hro47EQ== X-Received: by 2002:a05:6402:520e:b0:4a0:8ea1:cce with SMTP id s14-20020a056402520e00b004a08ea10ccemr3978840edd.25.1675275048716; Wed, 01 Feb 2023 10:10:48 -0800 (PST) Received: from wkz-x13.addiva.ad (h-98-128-229-186.NA.cust.bahnhof.se. [98.128.229.186]) by smtp.gmail.com with ESMTPSA id n3-20020a05640204c300b004a23609fab4sm5987171edw.70.2023.02.01.10.10.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 10:10:47 -0800 (PST) From: Tobias Waldekranz To: sjg@chromium.org, xypron.glpk@gmx.de, ilias.apalodimas@linaro.org Cc: u-boot@lists.denx.de Subject: [PATCH 8/8] doc: blkmap: Add introduction and examples Date: Wed, 1 Feb 2023 19:10:16 +0100 Message-Id: <20230201181016.4145834-9-tobias@waldekranz.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230201181016.4145834-1-tobias@waldekranz.com> References: <20230201181016.4145834-1-tobias@waldekranz.com> MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 01 Feb 2023 19:20:41 +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.6 at phobos.denx.de X-Virus-Status: Clean Explain block maps by going through two common use-cases. Signed-off-by: Tobias Waldekranz Reviewed-by: Simon Glass --- MAINTAINERS | 1 + doc/usage/blkmap.rst | 109 +++++++++++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 3 files changed, 111 insertions(+) create mode 100644 doc/usage/blkmap.rst diff --git a/MAINTAINERS b/MAINTAINERS index c420c8e1f9..de0e41487d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -790,6 +790,7 @@ BLKMAP M: Tobias Waldekranz S: Maintained F: cmd/blkmap.c +F: doc/usage/blkmap.rst F: drivers/block/blkmap.c F: include/blkmap.h F: test/py/tests/test_blkmap.py diff --git a/doc/usage/blkmap.rst b/doc/usage/blkmap.rst new file mode 100644 index 0000000000..1cf6d97c1b --- /dev/null +++ b/doc/usage/blkmap.rst @@ -0,0 +1,109 @@ +.. SPDX-License-Identifier: GPL-2.0+ +.. +.. Copyright (c) 2023 Addiva Elektronik +.. Author: Tobias Waldekranz + +Block Maps (blkmap) +=================== + +Block maps are a way of looking at various sources of data through the +lens of a regular block device. It lets you treat devices that are not +block devices, like RAM, as if they were. It also lets you export a +slice of an existing block device, which does not have to correspond +to a partition boundary, as a new block device. + +This is primarily useful because U-Boot's filesystem drivers only +operate on block devices, so a block map lets you access filesystems +wherever they might be located. + +The implementation is loosely modeled on Linux's "Device Mapper" +subsystem, see `kernel documentation`_ for more information. + +.. _kernel documentation: https://docs.kernel.org/admin-guide/device-mapper/index.html + + +Example: Netbooting an Ext4 Image +--------------------------------- + +Say that our system is using an Ext4 filesystem as its rootfs, where +the kernel is stored in ``/boot``. This image is then typically stored +in an eMMC partition. In this configuration, we can use something like +``load mmc 0 ${kernel_addr_r} /boot/Image`` to load the kernel image +into the expected location, and then boot the system. No problems. + +Now imagine that during development, or as a recovery mechanism, we +want to boot the same type of image by downloading it over the +network. Getting the image to the target is easy enough: + +:: + + dhcp ${ramdisk_addr_r} rootfs.ext4 + +But now we are faced with a predicament: how to we extract the kernel +image? Block maps to the rescue! + +We start by creating a new device: + +:: + + blkmap create 0 + +Before setting up the mapping, we figure out the size of the +downloaded file, in blocks: + +:: + + setexpr fileblks ${filesize} + 0x1ff + setexpr fileblks ${filesize} / 0x200 + +Then we can add a mapping to the start of our device, backed by the +memory at `${loadaddr}`: + +:: + + blkmap map 0 0 ${fileblks} mem ${fileaddr} + +Now we can access the filesystem via the virtual device: + +:: + + load blkmap 0 ${kernel_addr_r} /boot/Image + + +Example: Accessing a filesystem inside an FIT image +--------------------------------------------------- + +In this example, an FIT image is stored in an eMMC partition. We would +like to read the file ``/etc/version``, stored inside a Squashfs image +in the FIT. Since the Squashfs image is not stored on a partition +boundary, there is no way of accessing it via ``load mmc ...``. + +What we can to instead is to first figure out the offset and size of +the filesystem: + +:: + + mmc dev 0 + mmc read ${loadaddr} 0 0x100 + + fdt addr ${loadaddr} + fdt get value squashaddr /images/ramdisk data-position + fdt get value squashsize /images/ramdisk data-size + + setexpr squashblk ${squashaddr} / 0x200 + setexpr squashsize ${squashsize} + 0x1ff + setexpr squashsize ${squashsize} / 0x200 + +Then we can create a block map that maps to that slice of the full +partition: + +:: + + blkmap create 0 + blkmap map 0 0 ${squashsize} linear mmc 0 ${squashblk} + +Now we can access the filesystem: + +:: + + load blkmap 0 ${loadaddr} /etc/version diff --git a/doc/usage/index.rst b/doc/usage/index.rst index 3804046835..856a3da28e 100644 --- a/doc/usage/index.rst +++ b/doc/usage/index.rst @@ -4,6 +4,7 @@ Use U-Boot .. toctree:: :maxdepth: 1 + blkmap dfu environment fdt_overlays