summaryrefslogtreecommitdiff
path: root/fs/fat
AgeCommit message (Collapse)AuthorFilesLines
2019-04-10fs: fat: fix reading non-cluster-aligned root directoryAnssi Hannula1-13/+34
A FAT12/FAT16 root directory location is specified by a sector offset and it might not start at a cluster boundary. It also resides before the data area (before cluster 2). However, the current code assumes that the root directory is located at a beginning of a cluster, causing no files to be found if that is not the case. Since the FAT12/FAT16 root directory is located before the data area and is not aligned to clusters, using unsigned cluster numbers to refer to the root directory does not work well (the "cluster number" may be negative, and even allowing it be signed would not make it properly aligned). Modify the code to not use the normal cluster numbering when referring to the root directory of FAT12/FAT16 and instead use a cluster-sized offsets counted from the root directory start sector. This is a relatively common case as at least the filesystem formatter on Win7 seems to create such filesystems by default on 2GB USB sticks when "FAT" is selected (cluster size 64 sectors, rootdir size 32 sectors, rootdir starts at half a cluster before cluster 2). dosfstools mkfs.vfat does not seem to create affected filesystems. Signed-off-by: Anssi Hannula <anssi.hannula@bitwise.fi> Reviewed-by: Bernhard Messerklinger <bernhard.messerklinger@br-automation.com> Tested-by: Bernhard Messerklinger <bernhard.messerklinger@br-automation.com>
2019-02-28fs: fat: fix link error when building with DEBUG=1Heinrich Schuchardt1-3/+3
When compiling with DEBUG=1 an error fs/fat/fat_write.c:831: undefined reference to `__aeabi_ldivmod' occurred. We should use do_div() instead of the modulus operator. filesize and cur_pos cannot be negative. So let's use u64 to avoid warnings. Fixes: cb8af8af5ba0 ("fs: fat: support write with non-zero offset") Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2019-02-19fs: fat: Reduce default max clustersize 64KiB from malloc poolTien Fong Chee1-1/+9
Release cluster block immediately when no longer use would help to reduce 64KiB memory allocated to the memory pool. Signed-off-by: Tien Fong Chee <tien.fong.chee@intel.com>
2019-02-19fs: fat: dynamically allocate memory for temporary bufferTien Fong Chee1-6/+12
Drop the statically allocated get_contents_vfatname_block and dynamically allocate a buffer only if required. This saves 64KiB of memory. Signed-off-by: Stefan Agner <stefan.ag...@toradex.com> Signed-off-by: Tien Fong Chee <tien.fong.chee@intel.com>
2019-02-01spl: fat/fs: Add option to include/exclude FAT write build in SPLTien Fong Chee2-3/+4
Most of the time SPL only needs very simple FAT reading, so having CONFIG_IS_ENABLED(FAT_WRITE) to exclude it from SPL build would help to save 64KiB default max clustersize from memory. Signed-off-by: Tien Fong Chee <tien.fong.chee@intel.com> Reviewed-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com> Reviewed-by: Tom Rini <trini@konsulko.com>
2019-01-11Revert "fs: fat: assign rootdir sector when accessing root directory"Tom Rini1-4/+1
This particular commit is causing a regression on stih410-b2260 and other platforms when reading from FAT16. Noting that I had rebased the original fix from Thomas onto then-current master, there is also question from Akashi-san if the change is still needed after other FAT fixes that have gone in. This reverts commit a68b0e11ea774492713a65d9fd5bb525fcaefff3. Reported-by: Patrice Chotard <patrice.chotard@st.com> Cc: AKASHI Takahiro <takahiro.akashi@linaro.org> Cc: Thomas RIENOESSL <thomas.rienoessl@bachmann.info> Signed-off-by: Tom Rini <trini@konsulko.com>
2018-12-07fs: fix FAT name extractionPatrick Wildt1-0/+3
The long name apparently can be accumulated using multiple 13-byte slots. Unfortunately we never checked how many we can actually fit in the buffer we are reading to. Signed-off-by: Patrick Wildt <patrick@blueri.se>
2018-12-07fs: check FAT cluster sizePatrick Wildt1-0/+11
The cluster size specifies how many sectors make up a cluster. A cluster size of zero makes no sense, as it would mean that the cluster is made up of no sectors. This will later lead into a division by zero in sect_to_clust(), so better take care of that early. The MAX_CLUSTSIZE define can reduced using a define to make some room in low-memory system. Unfortunately if the code reads a filesystem with a bigger cluster size it will overflow the buffer. Signed-off-by: Patrick Wildt <patrick@blueri.se>
2018-11-20fs: fat: assign rootdir sector when accessing root directoryThomas RIENOESSL1-1/+4
This fixes problems accessing drives formated under Windows as FAT16. Signed-off-by: Thomas RIENOESSL <thomas.rienoessl@bachmann.info> [trini: Rebase on top of f528c140c801] Signed-off-by: Tom Rini <trini@konsulko.com>
2018-10-16fs: fat: Fix warning in normalize_longname()Tom Rini1-1/+1
As observed with clang: fs/fat/fat_write.c:1024:13: warning: comparison of constant 128 with expression of type 'char' is always false [-Wtautological-constant-out-of-range-compare] if ((0x80 <= c) && (c <= 0xff)) ~~~~ ^ ~ fs/fat/fat_write.c:1024:25: warning: comparison of constant 255 with expression of type 'char' is always true [-Wtautological-constant-out-of-range-compare] if ((0x80 <= c) && (c <= 0xff)) ~ ^ ~~~~ Fixes: 25bb9dab14f4 ("fs: fat: check and normalize file name") Signed-off-by: Tom Rini <trini@konsulko.com> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-10-06fs: fat: fix set_cluster()Heinrich Schuchardt1-10/+17
Avoid CoverityScan warning SIGN_EXTENSION by changing the type of parameter size of set_cluster(). Avoid leaking stack content when writing an incomplete last sector. Reported-by: Coverity (CID: 184096) Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2018-10-06fs: fat: memory leak in fat_unlink()Heinrich Schuchardt1-1/+7
Do not leak filename_copy in case of error. Catch out of memory when calling strdup. Reported-by: Coverity (CID: 184086) Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2018-09-23fs: fat: unaligned buffers are not an errorHeinrich Schuchardt2-2/+2
The FAT driver supports unaligned reads and writes and EFI applications will make use of these. So a misaligned buffer is only worth a debug message. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: support unlinkAKASHI Takahiro1-0/+132
In this patch, unlink support is added to FAT file system. A directory can be deleted only if it is empty. In this implementation, only a directory entry for a short file name will be removed. So entries for a long file name can and should be reclaimed with fsck. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: support mkdirAKASHI Takahiro1-0/+136
In this patch, mkdir support is added to FAT file system. A newly created directory contains only "." and ".." entries. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: remember the starting cluster number of directoryAKASHI Takahiro1-0/+3
The starting cluster number of directory is needed to initialize ".." (parent directory) entry when creating a new directory. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: support write with non-zero offsetAKASHI Takahiro1-15/+273
In this patch, all the necessary code for allowing for a file offset at write is implemented. What plays a major roll here is get_set_cluster(), which, in contrast to its counterpart, set_cluster(), only operates on already-allocated clusters, overwriting with data. So, with a file offset specified, set_contents() seeks and writes data with set_get_cluster() until the end of a file, and, once it reaches there, continues writing with set_cluster() for the rest. Please note that a file will be trimmed as a result of write operation if write ends before reaching file's end. This is an intended behavior in order to maintain compatibility with the current interface. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: refactor write interface for a file offsetAKASHI Takahiro1-114/+65
The current write implementation is quite simple: remove existing clusters and then allocating new ones and filling them with data. This, inevitably, enforces always writing from the beginning of a file. As the first step to lift this restriction, fat_file_write() and set_contents() are modified to accept an additional parameter, file offset and further re-factored so that, in the next patch, all the necessary code will be put into set_contents(). Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: support write with sub-directory pathAKASHI Takahiro2-322/+156
In this patch, write implementation is overhauled and rewritten by making full use of directory iterator. The obvious bonus is that we are now able to write to a file with a directory path, like /A/B/C/FILE. Please note that, as there is no notion of "current directory" on u-boot, a file name specified must contain an absolute directory path. Otherwise, "/" (root directory) is assumed. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: write returns error code instead of -1AKASHI Takahiro1-4/+15
It would be good that FAT write function return error code instead of just returning -1 as fat_read_file() does. This patch attempts to address this issue although it is 'best effort (or estimate)' for now. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: check and normalize file nameAKASHI Takahiro1-8/+44
FAT file system's long file name support is a bit complicated and has some restrictions on its naming. We should be careful about it especially for write as it may easily end up with wrong file system. normalize_longname() check for the rules and normalize a file name if necessary. Please note, however, that this function is yet to be extended to fully comply with the standard. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23Revert "fs: fat: cannot write to subdirectories"AKASHI Takahiro1-15/+1
This reverts commit 0dc1bfb7302d220a48364263d5632d6d572b069b. The succeeding patch series will supersede it. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: assure iterator's ->dent belongs to ->clustAKASHI Takahiro1-8/+17
In my attempt to re-work write operation, it was revealed that iterator's "clust" does not always point to a cluster to which a current directory entry ("dent") belongs. This patch assures that it is always true by adding "next_clust" which is used solely for dereferencing a cluster chain. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: handle "." and ".." of root dir correctly with fat_itr_resolve()AKASHI Takahiro1-0/+21
FAT's root directory does not have "." nor ".." So care must be taken when scanning root directory with fat_itr_resolve(). Without this patch, any file path starting with "." or ".." will not be resolved at all. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-09-23fs: fat: extend get_fs_info() for write useAKASHI Takahiro1-0/+7
get_fs_info() was introduced in major re-work of read operation by Rob. We want to reuse this function in write operation by extending it with additional members in fsdata structure. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-08-20fs/fat: debug-print file read position during file_fat_read_at()Andreas Dannenberg1-1/+1
In order to make the debug print in file_fat_read_at() a tad more useful, show the offset the file is being read at alongside the filename. Suggested-by: Tero Kristo <t-kristo@ti.com> Signed-off-by: Andreas Dannenberg <dannenberg@ti.com> Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
2018-07-25fs: fat: cannot write to subdirectoriesHeinrich Schuchardt1-1/+15
fs_fat_write() is not able to write to subdirectories. Currently if a filepath with a leading slash is passed, the slash is treated as part of the filename to be created in the root directory. Strip leading (back-)slashes. Check that the remaining filename does not contain any illegal characters (<>:"/\|?*). This way we will throw an error when trying to write to a subdirectory. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Signed-off-by: Alexander Graf <agraf@suse.de>
2018-05-07SPDX: Convert all of our single license tags to Linux Kernel styleTom Rini3-7/+3
When U-Boot started using SPDX tags we were among the early adopters and there weren't a lot of other examples to borrow from. So we picked the area of the file that usually had a full license text and replaced it with an appropriate SPDX-License-Identifier: entry. Since then, the Linux Kernel has adopted SPDX tags and they place it as the very first line in a file (except where shebangs are used, then it's second line) and with slightly different comment styles than us. In part due to community overlap, in part due to better tag visibility and in part for other minor reasons, switch over to that style. This commit changes all instances where we have a single declared license in the tag as both the before and after are identical in tag contents. There's also a few places where I found we did not have a tag and have introduced one. Signed-off-by: Tom Rini <trini@konsulko.com>
2018-01-23fs: fat: Drop CONFIG_SUPPORT_VFATTuomas Tynkkynen2-13/+2
fat.h unconditionally defines CONFIG_SUPPORT_VFAT (and has done since 2003), so as a result VFAT support is always enabled regardless of whether a board config defines it or not. Drop this unnecessary option. Signed-off-by: Tuomas Tynkkynen <tuomas@tuxera.com>
2018-01-23fs: FAT: Fix typo in FS_FAT_MAX_CLUSTSIZE descriptionTuomas Tynkkynen1-1/+1
Signed-off-by: Tuomas Tynkkynen <tuomas@tuxera.com>
2018-01-19fs/fat: remove distractive message in file_fat_read_at()Heinrich Schuchardt1-1/+1
The message "reading %s\n" may be interesting when debugging but otherwise it is superfluous. Only output the message when debugging. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2018-01-10fat write: Fixed a problem with the case of file names when writing filesJean-Jacques Hiblot1-2/+2
commit 21a24c3bf35b ("fs/fat: fix case for FAT shortnames") made it possible that get_name() returns file names with some upper cases. find_directory_entry() must be updated to take this account, and use case-insensitive functions to compare file names. Signed-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>
2017-11-30fat: Use cache aligned buffers for fat_opendirNeil Armstrong1-1/+3
Before this patch one could receive following errors when executing "fatls" command on machine with cache enabled (ex i.MX6Q) : => fatls mmc 0:1 CACHE: Misaligned operation at range [4f59dfc8, 4f59e7c8] CACHE: Misaligned operation at range [4f59dfc8, 4f59e7c8] ERROR: v7_outer_cache_inval_range - start address is not aligned - 0x4f59dfc8 ERROR: v7_outer_cache_inval_range - stop address is not aligned - 0x4f59e7c8 CACHE: Misaligned operation at range [4f59dfc8, 4f59e7c8] CACHE: Misaligned operation at range [4f59dfc8, 4f59e7c8] ERROR: v7_outer_cache_inval_range - start address is not aligned - 0x4f59dfc8 ERROR: v7_outer_cache_inval_range - stop address is not aligned - 0x4f59e7c8 To alleviate this problem - the calloc()s have been replaced with malloc_cache_aligned() and memset(). After those changes the buffers are properly aligned (with both start address and size) to SoC cache line. Fixes: 09fa964bba80 ("fs/fat: Fix 'CACHE: Misaligned operation at range' warnings") Suggested-by: Lukasz Majewski <lukma@denx.de> Signed-off-by: Neil Armstrong <narmstrong@baylibre.com> Reviewed-by: Lukasz Majewski <lukma@denx.de> Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
2017-10-08fs/fat: Check malloc return values and fix memory leaksTuomas Tynkkynen1-10/+20
Check malloc() return values and properly unwind on errors so memory allocated for fat_itr structures get freed properly. Also fixes a leak of fsdata.fatbuf in fat_size(). Fixes: 2460098cffacd1 ("fs/fat: Reduce stack usage") Reported-by: Coverity (CID: 167225, 167233, 167234) Signed-off-by: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> Reviewed-by: Tom Rini <trini@konsulko.com>
2017-10-08fs/fat: Fix 'CACHE: Misaligned operation at range' warningsTuomas Tynkkynen1-5/+5
The 'block' field of fat_itr needs to be properly aligned for DMA and while it does have '__aligned(ARCH_DMA_MINALIGN)', the fat_itr structure itself needs to be properly aligned as well. While at it use malloc_cache_aligned() for the other aligned allocations in the file as well. Fixes: 2460098cffacd1 ("fs/fat: Reduce stack usage") Signed-off-by: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> Reviewed-by: Tom Rini <trini@konsulko.com>
2017-10-06fs/fat: Fix pathnames using '..' that lead to the root directoryTuomas Tynkkynen1-1/+2
If we end up back in the root directory via a '..' directory entry, set itr->is_root accordingly. Failing to do that gives spews like "Invalid FAT entry" and being unable to access directory entries located past the first cluster of the root directory. Fixes: 8eafae209c35 ("fat/fs: convert to directory iterators") Reviewed-by: Tom Rini <trini@konsulko.com> Signed-off-by: Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
2017-09-22fs/fat: Reduce stack usageTom Rini1-4/+10
We have limited stack in SPL builds. Drop itrblock and move to malloc/free of itr to move this off of the stack. As part of this fix a double-free issue in fat_size(). Signed-off-by: Tom Rini <trini@konsulko.com> --- Rework to use malloc/free as moving this to a global overflows some SH targets.
2017-09-15fs/fat: fix fatbuf leakRob Clark1-7/+16
A new fatbuf was allocated by get_fs_info() (called by fat_itr_root()), but not freed, resulting in eventually running out of memory. Spotted by running 'ls -r' in a large FAT filesystem from Shell.efi. fatbuf is mainly used to cache FAT entry lookups (get_fatent()).. possibly once fat_write.c it can move into the iterator to simplify this. Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Simon Glass <sjg@chromium.org> Reviewed-by: Łukasz Majewski <lukma@denx.de>
2017-09-15fs/fat: Clean up open-coded sector <-> cluster conversionsRob Clark2-10/+6
Use the clust_to_sect() helper that was introduced earlier, and add an inverse sect_to_clust(), plus update the various spots that open-coded this conversion previously. Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-09-15fs/fat: fix case for FAT shortnamesRob Clark2-8/+12
Noticed when comparing our output to linux. There are some lcase bits which control whether filename and/or extension should be downcase'd. Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Łukasz Majewski <lukma@denx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-09-15fat/fs: move ls to generic implementationRob Clark1-32/+0
Add a generic implementation of 'ls' using opendir/readdir/closedir, and replace fat's custom implementation. Other filesystems should move to the generic implementation after they add opendir/readdir/closedir support. Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Łukasz Majewski <lukma@denx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-09-15fat/fs: remove a bunch of dead codeRob Clark2-187/+0
Spotted by chance, when trying to remove file_fat_ls(), I noticed there were some dead users of the API. Signed-off-by: Rob Clark <robdclark@gmail.com> Acked-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-09-15fs/fat: implement opendir/readdir/closedirRob Clark1-0/+61
Implement the readdir interface using the directory iterators. Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Łukasz Majewski <lukma@denx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-09-15fat/fs: convert to directory iteratorsRob Clark1-646/+76
And drop a whole lot of ugly code! Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Łukasz Majewski <lukma@denx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-09-15fs/fat: introduce new director iteratorsRob Clark1-3/+353
Untangle directory traversal into a simple iterator, to replace the existing multi-purpose do_fat_read_at() + get_dentfromdir(). Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Łukasz Majewski <lukma@denx.de>
2017-09-15fs/fat: split out helper to init fsdataRob Clark1-30/+43
Want to re-use this in fat dirent iterator in next patch. Signed-off-by: Rob Clark <robdclark@gmail.com> Reviewed-by: Łukasz Majewski <lukma@denx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-08-26fs: fat: fix fatwrite overflow calculationReno Farnesi1-1/+1
The overflow calculation was incorrect. Adding the start block of the partition is not needed because the sectors are already relative to the beginning of the partition. If you attempted to write a file smaller than cur_part_info.start blocks on a full partition the old calculation fails to catch the overflow. This would cause an infinite loop in the determine_fatent function. Old, incorrect calculation: ending sector of new file = start sector + file size (in sectors) last sector = partition start + total sectors on the partition Adding the partition start block number is not needed because sectors are already relative to the start of the partition. New calculation: ending sector of new file = start sector + file size (in sectors) last sector = total sectors on the partition Signed-off-by: Reno Farnesi <nfarnesi4@gmail.com>
2017-08-20fs/fat: Correct blk_dread() return value checkTom Rini1-1/+1
The function blk_dread will return -ENOSYS on failure or on success the number of blocks read, which must be the number asked to read (otherwise it failed somewhere). Correct this check. Signed-off-by: Tom Rini <trini@konsulko.com>
2017-07-11Kconfig: Add CONFIG_SATA to enable SATASimon Glass1-1/+1
At present CONFIG_CMD_SATA enables the 'sata' command which also brings in SATA support. Some boards may wish to enable SATA without the command. Add a separate CONFIG to permit this. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
2017-06-04fs: fat: add kbuild configuration supportSekhar Nori1-0/+24
Add Kconfig symbols for various configurations supported by FAT filesystem support code. CONFIG_SUPPORT_VFAT has been left out since its force enabled in include/fat.h and probably should get removed at some point. Signed-off-by: Sekhar Nori <nsekhar@ti.com> [trini: add select FS_FAT for CMD_FAT and SPL_FAT_SUPPORT] Signed-off-by: Tom Rini <trini@konsulko.com>