From patchwork Thu Nov 13 02:56:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 693 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=1763002638; bh=ikNL4tN3Qi22bv/D3mmQ+MVm95Yd21LVQEvqVc9QJOI=; 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=oLRx7p89StMAo1iVMZxh7i9clvSk7Y75OV1h4XFd8Az9Gl/24mdk0RiR/s0sAgMEG knuG/YD06Q1C+u7vnMrHX2Mu/7GouQA0LSlzKY1RODPtPxc254bPfRyHcVaYyoz2Ie 95xTenjdC80IPC0aCTH1R2QM8Vuuv86vHNIwfYLE4AxORbRuKLHuuBl3HjYb3MqChU BBerJ3hVq8W09XTXJvhqNI/J4Srn7fHJiQDv5nck7mX1JCoDiVlSHogPR4RN8Coi2K SZPvdHS2o9IlB8bwdsX5Ivfa6edKtxo7nq+rocMw1cIZjtb3Gra1axKD7NKMesaS11 4vdlmLEgaFrjQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 5F90D68580 for ; Wed, 12 Nov 2025 19:57:18 -0700 (MST) 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 l-oa8lsT5ppU for ; Wed, 12 Nov 2025 19:57:18 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763002638; bh=ikNL4tN3Qi22bv/D3mmQ+MVm95Yd21LVQEvqVc9QJOI=; 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=oLRx7p89StMAo1iVMZxh7i9clvSk7Y75OV1h4XFd8Az9Gl/24mdk0RiR/s0sAgMEG knuG/YD06Q1C+u7vnMrHX2Mu/7GouQA0LSlzKY1RODPtPxc254bPfRyHcVaYyoz2Ie 95xTenjdC80IPC0aCTH1R2QM8Vuuv86vHNIwfYLE4AxORbRuKLHuuBl3HjYb3MqChU BBerJ3hVq8W09XTXJvhqNI/J4Srn7fHJiQDv5nck7mX1JCoDiVlSHogPR4RN8Coi2K SZPvdHS2o9IlB8bwdsX5Ivfa6edKtxo7nq+rocMw1cIZjtb3Gra1axKD7NKMesaS11 4vdlmLEgaFrjQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4E56E6856C for ; Wed, 12 Nov 2025 19:57:18 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763002636; bh=4EXadnJX0/YFh8ouCxc0TlMYGEsU82gM15QsXAm+oto=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SAjbmU4pzZaWLkr9P6Ep9o3CRXsttQJXXGnJPMlKYdQfcgle3COdXpWMa2WiC/0TC 2G2tPqIwJwMVvBmOeHsQpFT1KxW/mqaCmBb+X9+XcueQn3HvpS010kNT9HzZL1NKVo LUUou4gkNB+BaPA8rajJEHXXLWCqZ5dalovG/8Cg1eZtzzBwqFIEWlrFZNfx7lU/45 LFmUVmcz8Mu9wfn9S9ZSDDSuqOo9aYOS/Au8rhqxFxkkDbn3W4POnmmqQWl22RJNYh 3PYL5wsR1vILf/NqmmRNhpSj6E7oWXz1TVvSDDuai3N5j7MVNCHJlJnmUDJzPNCAGt lEUJUhefNHCiA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 563AB68546; Wed, 12 Nov 2025 19:57:16 -0700 (MST) 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 4naovU1vW9F2; Wed, 12 Nov 2025 19:57:16 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763002633; bh=520VtNS/mHTc2jzAGjU+3BDJAosUjZcT24ULcqToGRQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ua11KOowAJXlZGdXK8sS2t4TY0oFrANWl10zL/LIibLtd5J2Io8TB9pDN2ZWHi2XA 2014aBRUpo99cX1IJzvb1CBal7mH8MoV+BQs4JVZhO5INd33f3Ft6Vg3OO2jsFht2P XHf0NGwa6tX13Qy3T/eIHnTqawpW21k39Es4UERDzKv2wZBKOgODy/YaTSpw/GeA9n xC3n1qCoTQn1f7IRm4n0Fa87ddh5HHpJPsKbiNF97Y+vp3jduD/3WRKIHclfrIwzgc oaC9KOMem6a8T6w75e59nZQso4d0BxFh1mH6v0lw8vQSICJQsomI42ie49XLSHBT6Y zv0JKuwqMVKBw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 7460768564; Wed, 12 Nov 2025 19:57:13 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Wed, 12 Nov 2025 19:56:49 -0700 Message-ID: <20251113025657.3862482-2-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251113025657.3862482-1-sjg@u-boot.org> References: <20251113025657.3862482-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 33JLILDT72EGSE4DRQGDOKHZ2H37PD5T X-Message-ID-Hash: 33JLILDT72EGSE4DRQGDOKHZ2H37PD5T 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: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 1/6] fat: Create an internal header file 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 As a first step towards separating fat.c from fat_write.c, create a header file for the definitions. Co-developed-by: Claude Signed-off-by: Simon Glass --- fs/fat/fat.c | 91 +--------------------------------- fs/fat/fat_internal.h | 112 ++++++++++++++++++++++++++++++++++++++++++ fs/fat/fat_write.c | 1 + 3 files changed, 114 insertions(+), 90 deletions(-) create mode 100644 fs/fat/fat_internal.h diff --git a/fs/fat/fat.c b/fs/fat/fat.c index 0cbfdf49a32..b1a3d71e2fe 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -25,9 +25,7 @@ #include #include #include - -/* maximum number of clusters for FAT12 */ -#define MAX_FAT12 0xFF4 +#include "fat_internal.h" /* * Convert a string to lowercase. Converts at most 'len' characters, @@ -45,10 +43,6 @@ static void downcase(char *str, size_t len) static struct blk_desc *cur_dev; static struct disk_partition cur_part_info; -#define DOS_BOOT_MAGIC_OFFSET 0x1fe -#define DOS_FS_TYPE_OFFSET 0x36 -#define DOS_FS32_TYPE_OFFSET 0x52 - static int disk_read(__u32 block, __u32 nr_blocks, void *buf) { ulong ret; @@ -704,89 +698,6 @@ static int get_fs_info(fsdata *mydata) return 0; } -/** - * struct fat_itr - directory iterator, to simplify filesystem traversal - * - * Implements an iterator pattern to traverse directory tables, - * transparently handling directory tables split across multiple - * clusters, and the difference between FAT12/FAT16 root directory - * (contiguous) and subdirectories + FAT32 root (chained). - * - * Rough usage - * - * .. code-block:: c - * - * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) { - * // to traverse down to a subdirectory pointed to by - * // current iterator position: - * fat_itr_child(&itr, &itr); - * } - * - * For a more complete example, see fat_itr_resolve(). - */ -struct fat_itr { - /** - * @fsdata: filesystem parameters - */ - fsdata *fsdata; - /** - * @start_clust: first cluster - */ - unsigned int start_clust; - /** - * @clust: current cluster - */ - unsigned int clust; - /** - * @next_clust: next cluster if remaining == 0 - */ - unsigned int next_clust; - /** - * @last_cluster: set if last cluster of directory reached - */ - int last_cluster; - /** - * @is_root: is iterator at root directory - */ - int is_root; - /** - * @remaining: remaining directory entries in current cluster - */ - int remaining; - /** - * @dent: current directory entry - */ - dir_entry *dent; - /** - * @dent_rem: remaining entries after long name start - */ - int dent_rem; - /** - * @dent_clust: cluster of long name start - */ - unsigned int dent_clust; - /** - * @dent_start: first directory entry for long name - */ - dir_entry *dent_start; - /** - * @l_name: long name of current directory entry - */ - char l_name[VFAT_MAXLEN_BYTES]; - /** - * @s_name: short 8.3 name of current directory entry - */ - char s_name[14]; - /** - * @name: l_name if there is one, else s_name - */ - char *name; - /** - * @block: buffer for current cluster - */ - u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN); -}; - static int fat_itr_isdir(fat_itr *itr); /** diff --git a/fs/fat/fat_internal.h b/fs/fat/fat_internal.h new file mode 100644 index 00000000000..0174cd611e7 --- /dev/null +++ b/fs/fat/fat_internal.h @@ -0,0 +1,112 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * fat_internal.h + * + * Internal definitions and structures for FAT filesystem implementation + */ + +#ifndef _FAT_INTERNAL_H_ +#define _FAT_INTERNAL_H_ + +#include +#include + +struct blk_desc; +struct disk_partition; + +/* Maximum number of clusters for FAT12 */ +#define MAX_FAT12 0xFF4 + +/* Boot sector offsets */ +#define DOS_BOOT_MAGIC_OFFSET 0x1fe +#define DOS_FS_TYPE_OFFSET 0x36 +#define DOS_FS32_TYPE_OFFSET 0x52 + +/** + * struct fat_itr - directory iterator, to simplify filesystem traversal + * + * Implements an iterator pattern to traverse directory tables, + * transparently handling directory tables split across multiple + * clusters, and the difference between FAT12/FAT16 root directory + * (contiguous) and subdirectories + FAT32 root (chained). + * + * Rough usage + * + * .. code-block:: c + * + * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) { + * // to traverse down to a subdirectory pointed to by + * // current iterator position: + * fat_itr_child(&itr, &itr); + * } + * + * For a more complete example, see fat_itr_resolve(). + */ +struct fat_itr { + /** + * @fsdata: filesystem parameters + */ + fsdata *fsdata; + /** + * @start_clust: first cluster + */ + unsigned int start_clust; + /** + * @clust: current cluster + */ + unsigned int clust; + /** + * @next_clust: next cluster if remaining == 0 + */ + unsigned int next_clust; + /** + * @last_cluster: set if last cluster of directory reached + */ + int last_cluster; + /** + * @is_root: is iterator at root directory + */ + int is_root; + /** + * @remaining: remaining directory entries in current cluster + */ + int remaining; + /** + * @dent: current directory entry + */ + dir_entry *dent; + /** + * @dent_rem: remaining entries after long name start + */ + int dent_rem; + /** + * @dent_clust: cluster of long name start + */ + unsigned int dent_clust; + /** + * @dent_start: first directory entry for long name + */ + dir_entry *dent_start; + /** + * @l_name: long name of current directory entry + */ + char l_name[VFAT_MAXLEN_BYTES]; + /** + * @s_name: short 8.3 name of current directory entry + */ + char s_name[14]; + /** + * @name: l_name if there is one, else s_name + */ + char *name; + /** + * @block: buffer for current cluster + */ + u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN); +}; + +#define TYPE_FILE 0x1 +#define TYPE_DIR 0x2 +#define TYPE_ANY (TYPE_FILE | TYPE_DIR) + +#endif /* _FAT_INTERNAL_H_ */ diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 0b924541187..45a2eef712b 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -20,6 +20,7 @@ #include #include #include +#include "fat_internal.h" #include "fat.c" static dir_entry *find_directory_entry(fat_itr *itr, char *filename);