From patchwork Fri Apr 3 14:04:34 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 2097 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225174; bh=p61vFgsaKXpRfhNKuiUJDvwlXyBP5r/DkPlNhM+NbeA=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=reNdvUau1BOT/dDbMGy/eYAL+hdcP5l4+GssFFTr6UeOGzHWtjjKy+Fgj6ejDloXJ X3nR8j7X/NL/AVpyri+LnAIcPdJIaxsJlbu21oBFF8x4ZaRkYTGsNmkdv0vNcJ0Det agMJeq1uVSskWRoi/YHxMybhYxtWaHJ6ID/QePJlg4Do95JybwBRQ73Nq+fQmu4gbO Brge98NGYWUMwVYXxFe1PK8KgrbjMrI0p55cB6i6DUiGBHtFfnhrBoqWHZXxZQvbOK SpiPX9qHNp2kNB+gWj2AgcBzkID3mK7HGk8UtGU09zxXM7zfDbeFP+1AdFaLacUWux GNDi+eDwyfrZg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 88E8C6A37F for ; Fri, 3 Apr 2026 08:06:14 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id ExIcak6ghnaU for ; Fri, 3 Apr 2026 08:06:14 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225170; bh=p61vFgsaKXpRfhNKuiUJDvwlXyBP5r/DkPlNhM+NbeA=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=U+x3uUhBa7moVvYsEmseGcB8TUilBY9FsR3OckEEaNK5vTWGbo268nfLC9ahfTOY2 gLHZLNIVpBv4MpEaQaWphnoVXXigGSSowSuL8rOaQVCO3EAWmlnfXVPvrXIPWVJ25p zi0/ZLIW2xPTEOeHw/z3mZpJ69z+weClF4N1xrkmE7VSz8UZfzjmAF6z7evD+IrBJj plISAXllgpcFslQxUIPzYTBWZ1cUIxAlWnnvEFAXM+FklX7Z/z1MfQlXlo0V07DNaK FNbzgnxJjwbl8z3YsvlUaZXwguWuCFlWy0k/lPd62tq//KpZYVQFVtkaeb5D1KogUv V857iT0vD9C5A== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 87E6A6A35A for ; Fri, 3 Apr 2026 08:06:10 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225167; bh=b8FTcoUPLSHR9eyuA4eWV819yR/IN7VPL15UUtLOy8Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WX3s1il0nwBvMYm/vJBGypN336+kYXB2Juynq0f+2tfLpU9pnBu87tjYAVEBP5CUR 1UwEDlV8xnmYMKLcnEeF6C4CKa7hmbKzik5mWV0Stf8/75xBwQqngTtWjL1fufThHe EdPPmsLvyx7KnylF1TBrkQabVdImNOAF8lGsvLKd6DMtxZbtH9auzLqj7WL3+8m58W H8Xq5GLABCpK0blEGTzkN/ATUlHtmFhFU/uH996dZj/VjnhPGCVhraRVVunlDXLrWd jHVggsC0uL6jAPorNqhj1Be+mTeEAhOZtTM/hVQk0Rmd5OvpgR+9wlP+mwliuPkc7H QEiSNr5JH1YsA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 188D46A366; Fri, 3 Apr 2026 08:06:07 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id c_ilVg8Moxvt; Fri, 3 Apr 2026 08:06:07 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225160; bh=CVGiVLicUEeY+an0oq6F0Wbk+XZxtv6wFrCBujb7tQM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N7OAj4s9CEpkMtiBbtFK87E/HYNsyuYLNLs8cOKTIMEVt+LLVRldHBjL7cMnu44hH uS8oERLvCgZieYOHHXBDm5XxiBMmMxMZ6V/dCaGsNOfRCwoJ09HsrZaiXoncB3QcE6 iJ0zGwPnJ14BjZ9PL1k/5LBjcUPOD52b5Bi3T8a+ePS6jkF1cUn1B4+0HCrx6GhGP5 R+Tj3I+Cdt9PqDc74PWxNMiJjoTuOFTZ3ZSNILu6A7OaqAW81TRo8cGpwq2L3qoEYT rJwhHsS5mwskOr9+lYp4O8cN6VS0oaCmvoD8ot/vPWKQjt/Q2FfGiCDdzmmhxBS+Ug f7sZEX7MpsRig== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 24D046A34C; Fri, 3 Apr 2026 08:06:00 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Fri, 3 Apr 2026 08:04:34 -0600 Message-ID: <20260403140523.1998228-13-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260403140523.1998228-1-sjg@u-boot.org> References: <20260403140523.1998228-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: XXNCW6V2L5J6GHADJYCAWSHGQKO47NKS X-Message-ID-Hash: XXNCW6V2L5J6GHADJYCAWSHGQKO47NKS X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 12/34] vfs: Add ls support using dir open/read/close List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add vfs_ls() which resolves a path through the mount table and lists directory entries using the dir_open(), dir_read() and dir_close() interface. At the root level, mount-point directories are shown. Add an 'fs ls' subcommand and update the help text to include it along with the mount -t syntax for block-device mounts. Signed-off-by: Simon Glass --- cmd/vfs.c | 28 +++++++++++++++++++++++----- fs/vfs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ include/vfs.h | 11 +++++++++++ test/dm/fs.c | 10 ++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) diff --git a/cmd/vfs.c b/cmd/vfs.c index 23b8cabdcdb..5779f3f098d 100644 --- a/cmd/vfs.c +++ b/cmd/vfs.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* - * VFS commands - 'fs mount', 'fs umount' + * VFS commands - 'fs mount', 'fs umount', 'fs ls' * * Provides a new 'fs' command with subcommands for the virtual filesystem * layer, co-existing with the legacy filesystem commands in cmd/fs.c. @@ -87,10 +87,28 @@ static int do_fs_umount(struct cmd_tbl *cmdtp, int flag, int argc, return CMD_RET_SUCCESS; } +static int do_fs_ls(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + const char *path = argc >= 2 ? argv[1] : "/"; + int ret; + + ret = vfs_ls(path); + if (ret) { + printf("fs ls failed: %dE\n", ret); + return CMD_RET_FAILURE; + } + + return CMD_RET_SUCCESS; +} + U_BOOT_LONGHELP(fs, - "mount [ ] - list or create mounts\n" - "fs umount - unmount a filesystem"); + "mount [ ] - list or create mounts\n" + "fs mount -t - mount from block device\n" + "fs umount - unmount a filesystem\n" + "fs ls [] - list directory (default /)"); U_BOOT_CMD_WITH_SUBCMDS(fs, "Filesystem operations", fs_help_text, - U_BOOT_SUBCMD_MKENT(mount, 3, 1, do_fs_mount), - U_BOOT_SUBCMD_MKENT(umount, 2, 1, do_fs_umount)); + U_BOOT_SUBCMD_MKENT(mount, 6, 1, do_fs_mount), + U_BOOT_SUBCMD_MKENT(umount, 2, 1, do_fs_umount), + U_BOOT_SUBCMD_MKENT(ls, 2, 1, do_fs_ls)); diff --git a/fs/vfs.c b/fs/vfs.c index db1aa84bfd6..e05a02ad5b0 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -331,6 +331,52 @@ void vfs_print_mounts(void) } } +int vfs_ls(const char *path) +{ + struct udevice *vfs, *mnt, *dir = NULL; + struct fs_dir_stream *strm; + struct fs_dirent dent; + const char *subpath; + bool empty = true; + int ret; + + vfs = vfs_root(); + if (!vfs) + return -ENXIO; + + ret = vfs_find_mount(vfs, path, &mnt, &subpath); + if (!ret && mnt) { + struct vfsmount *m = dev_get_uclass_priv(mnt); + + ret = fs_lookup_dir(m->target, subpath, &dir); + if (ret) + return ret; + } else if (!ret || !path[1]) { + /* Root "/" - list the VFS root dir */ + ret = fs_lookup_dir(vfs, "", &dir); + if (ret) + return ret; + } else { + return ret; + } + + ret = dir_open(dir, &strm); + if (ret) + return ret; + + while (!dir_read(dir, strm, &dent)) { + if (dent.type == FS_DT_DIR) + printf("DIR %10u %s\n", 0, dent.name); + else + printf(" %10llu %s\n", dent.size, dent.name); + empty = false; + } + + dir_close(dir, strm); + + return 0; +} + struct udevice *vfs_root(void) { struct udevice *dev; diff --git a/include/vfs.h b/include/vfs.h index 6673f9a5ef1..f85f45da4dd 100644 --- a/include/vfs.h +++ b/include/vfs.h @@ -125,4 +125,15 @@ bool vfs_is_mount_point(struct udevice *dir); */ void vfs_print_mounts(void); +/** + * vfs_ls() - List directory contents + * + * Resolves the path through the mount table and lists directory entries + * using dir_open(), dir_read() and dir_close(). + * + * @path: Absolute path to list, or "/" for root + * Return: 0 if OK, -ve on error + */ +int vfs_ls(const char *path); + #endif diff --git a/test/dm/fs.c b/test/dm/fs.c index f71ad361bcd..b14083590c6 100644 --- a/test/dm/fs.c +++ b/test/dm/fs.c @@ -262,6 +262,16 @@ static int dm_test_vfs_cmd(struct unit_test_state *uts) ut_assert_nextlinen("/host"); ut_assert_console_end(); + /* Root should show the "host" mount point */ + ut_assertok(run_command("fs ls /", 0)); + ut_assert_nextline("DIR %10u host", 0); + ut_assert_console_end(); + + /* Listing /host should show sandbox directory contents */ + ut_assertok(run_command("fs ls /host", 0)); + ut_assert_skip_to_linen("DIR "); + console_record_reset_enable(); + /* Unmount */ ut_assertok(run_command("fs umount /host", 0)); ut_assert_console_end();