From patchwork Thu Nov 13 02:56:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 695 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=1763002645; bh=1s93FfZFUrB9w44E/dNbMKNLmc9KOJnGcOYuTCvMda0=; 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=plJC/n23eWzl0jIyBegcX+x6GXJ7Ye+ItSkT/0dMITsv5qJ8npSAJXHDHPvJ8Rdlc eJZDVdjmR8bcjOEmJAhVOskiDoOpT9hpDcgqCNmTj2rhQGGN964qFazrS/OsHzktZy zVu1v3nDW//zb+aYGk8eWk3m27Fb6ZNP0h1d+7xxqU2O7AYtdZ2SoTa/1DxRSNZUOe vJyQwyJw5p4gkfrvoQIUbKVvcEEtU2AHML2ht8RFPkogvOBEmFcRtaX7SEHYTjuwv6 pmnPuDUWWPxRtq9pmNAeXrzI+rbF2x3FzaLQrytgZjZSnzJCy7pCyFKZFuPbPuU6K7 ZxcacE2+Aiy8w== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3452D68582 for ; Wed, 12 Nov 2025 19:57:25 -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 5C3KKJ3EMwYt for ; Wed, 12 Nov 2025 19:57:25 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763002643; bh=1s93FfZFUrB9w44E/dNbMKNLmc9KOJnGcOYuTCvMda0=; 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=DPzxHqDvdu+kMutK1auUB9/Hl98W+lxvHXi9qc83Aly9zyLx3xb6FdLC3jMXHCqKQ 4195eFx4vVo3sFXCh84awyqtacirSYIdLPZ1Inff1Y5d98JXeWcb4klO1TV5WA6qFD JcJGAT2iuinLObQU7s6AXDXm9SD6iudQulK1VvqgJkTT/3GAiPUCpGNX5I0Xsj5zt7 CioiXMoCNvRiFC0yiR1FkmKGXYRKxQjmEu70kiIWFF+InVNw13DHdSVzez3LqqUJPm Gp9E2Szr0Y5hbHoJii3PsE+n/xDA4oqKDvgTHIEQf3fHRQJE4vonRB6OI1/OoRdXkp Emyu2R2mOtbGw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2C4316838E for ; Wed, 12 Nov 2025 19:57:23 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763002640; bh=7EkuoolfJY5pUBwF4s7bLOXSJc/ShcoD05l1Ycwdcok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ny5gRMojYIMF4qgik35r3KEsK8IG6GQ1Q9xRA14HG5Q+fnZLXSw8ip5EiCUjHq9Uc xMMvE+1jA7NmslzpzNvs6pBitY1piB0ZBwMj4iryzGUV9eNa1EyiuWxprW6Wqijx3+ cr1kaB6NTm51x1wwh8bf0ra45mOsJQSzMLvch/fuXsc8QW0mFX+D2OIM/gzKyPwr8g zLwSQf8ZegtofQsp8jwErgfakVFS+F0ytsG7ysGSmHMVlkVoAQRGdMxDQ4zAKovo40 7PXfmXccbNix0N299u9iTgdM6msaQR3Pnnd55PNHgkS/OKTgzML2N0IFdjdZhm+Tj8 Q0kvNHYASwmSA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id BEDEA68580; Wed, 12 Nov 2025 19:57:20 -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 77tx1QMF6qMg; Wed, 12 Nov 2025 19:57:20 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1763002634; bh=biPmxsoebNU7yUGf7zmVS5aKRSV9QtLfzCpC1PROlKo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tbU+58EHQfVm8B8qI1vKwyWGBFxrMkNX5fd1mYYjjOgyBoOTImSaNixCwNeKpRMEm +G1iFpI8kHBexf7oAPnvy3TniXEyUycT9EyN2U6jd4cP6/py5+El+9+g/q/GIYprhC kNAz8SQ3NzHtm0cLN8C0mAY+2hYmJY6dY9zEL/eYobD5BQw9M9a8KZ2hkTNtR425Rr z3Kgpe326aqT/am8H4GORVUiRgfg310nz5m4dQ+/vi9FuvYlURLrbhY396KJAF1v7a tPFVvIGYVT1IK/F+7Xn4eEDTo9ZW4ftE5huP+kS/g3B+4phi9THenixevf6L55qYhR 9IAOjaucMYuww== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 799A268507; Wed, 12 Nov 2025 19:57:14 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Wed, 12 Nov 2025 19:56:50 -0700 Message-ID: <20251113025657.3862482-3-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: NVQICCMARQ37Z6S4NNXQXN3ER7UCKHLR X-Message-ID-Hash: NVQICCMARQ37Z6S4NNXQXN3ER7UCKHLR 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 2/6] fat: Separate fat.c from fat_write.c 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 Currently fat_write.c includes fat.c directly, which is unusual and makes the code harder to maintain. Use the internal header file to hold shared functions, to avoid this. Co-developed-by: Claude Signed-off-by: Simon Glass --- fs/fat/Makefile | 2 +- fs/fat/fat.c | 91 ++++++----------------------------------- fs/fat/fat_internal.h | 95 +++++++++++++++++++++++++++++++++++++++++-- fs/fat/fat_write.c | 4 +- 4 files changed, 107 insertions(+), 85 deletions(-) diff --git a/fs/fat/Makefile b/fs/fat/Makefile index 3e739044537..f3982fca4c6 100644 --- a/fs/fat/Makefile +++ b/fs/fat/Makefile @@ -2,4 +2,4 @@ # obj-$(CONFIG_$(PHASE_)FS_FAT) = fat.o -obj-$(CONFIG_$(PHASE_)FAT_WRITE) = fat_write.o +obj-$(CONFIG_$(PHASE_)FAT_WRITE) += fat_write.o diff --git a/fs/fat/fat.c b/fs/fat/fat.c index b1a3d71e2fe..f62a9ceaa64 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -32,7 +32,7 @@ * 'len' may be larger than the length of 'str' if 'str' is NULL * terminated. */ -static void downcase(char *str, size_t len) +void downcase(char *str, size_t len) { while (*str != '\0' && len--) { *str = tolower(*str); @@ -40,10 +40,10 @@ static void downcase(char *str, size_t len) } } -static struct blk_desc *cur_dev; -static struct disk_partition cur_part_info; +struct blk_desc *cur_dev; +struct disk_partition cur_part_info; -static int disk_read(__u32 block, __u32 nr_blocks, void *buf) +int disk_read(__u32 block, __u32 nr_blocks, void *buf) { ulong ret; @@ -144,8 +144,6 @@ static void get_name(dir_entry *dirent, char *s_name) *s_name = DELETED_FLAG; } -static int flush_dirty_fat_buffer(fsdata *mydata); - #if !CONFIG_IS_ENABLED(FAT_WRITE) /* Stub for read only operation */ int flush_dirty_fat_buffer(fsdata *mydata) @@ -159,7 +157,7 @@ int flush_dirty_fat_buffer(fsdata *mydata) * Get the entry at index 'entry' in a FAT (12/16/32) table. * On failure 0x00 is returned. */ -static __u32 get_fatent(fsdata *mydata, __u32 entry) +__u32 get_fatent(fsdata *mydata, __u32 entry) { __u32 bufnum; __u32 offset, off8; @@ -468,7 +466,7 @@ static int slot2str(dir_slot *slotptr, char *l_name, int *idx) } /* Calculate short name checksum */ -static __u8 mkcksum(struct nameext *nameext) +__u8 mkcksum(struct nameext *nameext) { int i; u8 *pos = (void *)nameext; @@ -698,17 +696,7 @@ static int get_fs_info(fsdata *mydata) return 0; } -static int fat_itr_isdir(fat_itr *itr); - -/** - * fat_itr_root() - initialize an iterator to start at the root - * directory - * - * @itr: iterator to initialize - * @fsdata: filesystem data for the partition - * Return: 0 on success, else -errno - */ -static int fat_itr_root(fat_itr *itr, fsdata *fsdata) +int fat_itr_root(fat_itr *itr, fsdata *fsdata) { if (get_fs_info(fsdata)) return -ENXIO; @@ -725,24 +713,7 @@ static int fat_itr_root(fat_itr *itr, fsdata *fsdata) return 0; } -/** - * fat_itr_child() - initialize an iterator to descend into a sub- - * directory - * - * Initializes 'itr' to iterate the contents of the directory at - * the current cursor position of 'parent'. It is an error to - * call this if the current cursor of 'parent' is pointing at a - * regular file. - * - * Note that 'itr' and 'parent' can be the same pointer if you do - * not need to preserve 'parent' after this call, which is useful - * for traversing directory structure to resolve a file/directory. - * - * @itr: iterator to initialize - * @parent: the iterator pointing at a directory entry in the - * parent directory of the directory to iterate - */ -static void fat_itr_child(fat_itr *itr, fat_itr *parent) +void fat_itr_child(fat_itr *itr, fat_itr *parent) { fsdata *mydata = parent->fsdata; /* for silly macros */ unsigned clustnum = START(parent->dent); @@ -843,7 +814,7 @@ void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes) return itr->block; } -static dir_entry *next_dent(fat_itr *itr) +dir_entry *next_dent(fat_itr *itr) { if (itr->remaining == 0) { unsigned nbytes; @@ -919,16 +890,7 @@ static dir_entry *extract_vfat_name(fat_itr *itr) return dent; } -/** - * fat_itr_next() - step to the next entry in a directory - * - * Must be called once on a new iterator before the cursor is valid. - * - * @itr: the iterator to iterate - * Return: boolean, 1 if success or 0 if no more entries in the - * current directory - */ -static int fat_itr_next(fat_itr *itr) +int fat_itr_next(fat_itr *itr) { dir_entry *dent; @@ -991,41 +953,12 @@ static int fat_itr_next(fat_itr *itr) return 1; } -/** - * fat_itr_isdir() - is current cursor position pointing to a directory - * - * @itr: the iterator - * Return: true if cursor is at a directory - */ -static int fat_itr_isdir(fat_itr *itr) +int fat_itr_isdir(fat_itr *itr) { return !!(itr->dent->attr & ATTR_DIR); } -/* - * Helpers: - */ - -#define TYPE_FILE 0x1 -#define TYPE_DIR 0x2 -#define TYPE_ANY (TYPE_FILE | TYPE_DIR) - -/** - * fat_itr_resolve() - traverse directory structure to resolve the - * requested path. - * - * Traverse directory structure to the requested path. If the specified - * path is to a directory, this will descend into the directory and - * leave it iterator at the start of the directory. If the path is to a - * file, it will leave the iterator in the parent directory with current - * cursor at file's entry in the directory. - * - * @itr: iterator initialized to root - * @path: the requested path - * @type: bitmask of allowable file types - * Return: 0 on success or -errno - */ -static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type) +int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type) { const char *next; diff --git a/fs/fat/fat_internal.h b/fs/fat/fat_internal.h index 0174cd611e7..10881a15569 100644 --- a/fs/fat/fat_internal.h +++ b/fs/fat/fat_internal.h @@ -22,6 +22,14 @@ struct disk_partition; #define DOS_FS_TYPE_OFFSET 0x36 #define DOS_FS32_TYPE_OFFSET 0x52 +#define TYPE_FILE 0x1 +#define TYPE_DIR 0x2 +#define TYPE_ANY (TYPE_FILE | TYPE_DIR) + +/* Global variables shared between fat.c and fat_write.c */ +extern struct blk_desc *cur_dev; +extern struct disk_partition cur_part_info; + /** * struct fat_itr - directory iterator, to simplify filesystem traversal * @@ -105,8 +113,89 @@ struct fat_itr { u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN); }; -#define TYPE_FILE 0x1 -#define TYPE_DIR 0x2 -#define TYPE_ANY (TYPE_FILE | TYPE_DIR) +/** + * downcase() - convert a string to lowercase + * @str: string to convert + * @len: maximum number of characters to convert + */ +void downcase(char *str, size_t len); + +/** + * next_dent() - get next directory entry + * @itr: directory iterator + * Return: pointer to next directory entry, or NULL if at end + */ +dir_entry *next_dent(fat_itr *itr); + +/** + * disk_read() - read sectors from the current FAT device + * @block: logical block number + * @nr_blocks: number of blocks to read + * @buf: buffer to read data into + * Return: number of blocks read, -1 on error + */ +int disk_read(__u32 block, __u32 nr_blocks, void *buf); + +/** + * flush_dirty_fat_buffer() - write fat buffer to disk if dirty + * @mydata: filesystem data + * Return: 0 on success, -1 on error + */ +int flush_dirty_fat_buffer(fsdata *mydata); + +/* Internal function declarations */ + +/** + * get_fatent() - get the entry at index 'entry' in a FAT (12/16/32) table + * @mydata: filesystem data + * @entry: FAT entry index + * Return: FAT entry value, 0x00 on failure + */ +__u32 get_fatent(fsdata *mydata, __u32 entry); + +/** + * mkcksum() - calculate short name checksum + * @nameext: name and extension structure + * Return: checksum value + */ +__u8 mkcksum(struct nameext *nameext); + +/** + * fat_itr_root() - initialize an iterator to start at the root directory + * @itr: iterator to initialize + * @fsdata: filesystem data for the partition + * Return: 0 on success, else -errno + */ +int fat_itr_root(fat_itr *itr, fsdata *fsdata); + +/** + * fat_itr_child() - initialize an iterator to descend into a sub-directory + * @itr: iterator to initialize + * @parent: the iterator pointing at a directory entry in the parent directory + */ +void fat_itr_child(fat_itr *itr, fat_itr *parent); + +/** + * fat_itr_next() - step to the next entry in a directory + * @itr: the iterator to iterate + * Return: 1 if success or 0 if no more entries in the current directory + */ +int fat_itr_next(fat_itr *itr); + +/** + * fat_itr_isdir() - is current cursor position pointing to a directory + * @itr: the iterator + * Return: true if cursor is at a directory + */ +int fat_itr_isdir(fat_itr *itr); + +/** + * fat_itr_resolve() - traverse directory structure to resolve the requested path + * @itr: iterator initialized to root + * @path: the requested path + * @type: bitmask of allowable file types + * Return: 0 on success or -errno + */ +int fat_itr_resolve(fat_itr *itr, const char *path, uint type); #endif /* _FAT_INTERNAL_H_ */ diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 45a2eef712b..c202f013f43 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -21,7 +22,6 @@ #include #include #include "fat_internal.h" -#include "fat.c" static dir_entry *find_directory_entry(fat_itr *itr, char *filename); static int new_dir_table(fat_itr *itr); @@ -216,7 +216,7 @@ static int disk_write(__u32 block, __u32 nr_blocks, void *buf) /* * Write fat buffer into block device */ -static int flush_dirty_fat_buffer(fsdata *mydata) +int flush_dirty_fat_buffer(fsdata *mydata) { int getsize = FATBUFBLOCKS; __u32 fatlength = mydata->fatlength;