Convert all __u8, __u16, and __u32 types to their u8, u16, u32
equivalents throughout the FAT filesystem code.
Series-to: u-boot
Series-cc: heinrich
Cover-letter:
fat: Some code improvements
This is an attempt to improve the structure of the FAT code, since it
doesn't fully follow the U-Boot conventions:
- fat_write.c includes fat.c which is odd
- use of __u32 and its ilk
- use of typedef
- old-style struct comments
This series resolves these problems, making it easier to take on other
improvements in future.
END
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
Convert the struct fat_itr documentation from the older style with
separate @field comments to the standard kernel-doc style where field
descriptions are listed in the header comment block.
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
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 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
As a first step towards separating fat.c from fat_write.c, create a
header file for the definitions.
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
Add a Kconfig optiion to enable this library and add it to the lib/
Makefile, being careful to avoid a conflict with the existing blake2b
implementation.
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This function is used by the EFI loader but we want to avoid it for the
app, since it records global state.
We don't intend to support running arbitrary EFI apps outside of a
bootstd context, so drop this call for the EFI app.
Signed-off-by: Simon Glass <sjg@chromium.org>
At present a new directory device is created for every access to a
directory. This means that a new device is always created when 'ls' is
used.
Where the directory being accessed matches an existing device, this is
not necessary.
Check that path against existing directories and reuse the device if
possible.
Signed-off-by: Simon Glass <sjg@chromium.org>
It is easier to use an empty string when the root directory is intended.
Adjust the code to drop use of "/" and NULL and just use and empty
string.
Signed-off-by: Simon Glass <sjg@chromium.org>
This name matches the ARM name and 'emulation' is not really a vendor.
Use ARCH_QEMU_X86 instead, moving that option from its existing location.
Signed-off-by: Simon Glass <sjg@chromium.org>
Add compatibility function to support a single virtio-fs via the normal
'ls' and 'load' commands. Other commands are not supported for now.
Add a workaround for the fact that virtiofs does not use a block device.
The existing filesystem layer does not support non-block filesystems -
sandbox's hostfs mostly bypasses this code.
Make sure that sandbox does not try to mount a virtio-fs as this does
not work at present. It will require either a fake driver for virtio-fs
which connects to host files, or possibly something involving FUSE.
Series-to: concept
Cover-letter:
virtio: Support virtio-fs
This series introduces support for virtio-fs, a filesystem which
provides access to host files from within a QEMU guest OS.
A new filesystem driver is created with support for the three uclasses
(FS, DIR, FILE). A compatibility layer is added as well, so that the
existing cmdline work as expected.
Only listing directories and reading files are supported so far.
Since sandbox works by using a NULL blk_desc, a workaround is added for
now. Once we switch commands (and bootstd!) over to the new filesystem
approach, this will go away.
It is possible to test this using something like:
./scripts/build-qemu -a x86 -rs -D .
then within U-Boot:
ls virtio 0
END
Signed-off-by: Simon Glass <sjg@chromium.org>
Using DM_DRIVER_GET() forces alignment which in the case of the
tools-only board results in an invalid linker list. It is not actually
used in this case (since only the tools are ever executed), but does
cause a build failure.
Fix it by switching to DM_DRIVER_REF()
Signed-off-by: Simon Glass <sjg@chromium.org>
Create a test which looks for a file reads from it.
Series-to: concept
Cover-letter:
Introduce a filesystem layer
U-Boot's filesystems work reasonably well but could be improved:
- There is no equivalent of Linux's VFS, meaning that the device type
must be provided with filesystem commands like 'ls' and 'load'
- There is no caching, nor really anything to hang caching on to, so
each access causes the filesystem to be remounted
- There is no real 'driver model' access to filesystems, in the sense
of opening a file, reading it and then later closing it
- It is quite hard to support non-block filesystems, since many
functions pass around a struct blk_desc for want of a better
structure.
As a first step towards improving things, this series provides a simple
filesystem uclass, which can be implemented to provide access to
filesystems via driver model. Only one filesystem is currently
supported: sandbox's hostfs
The existing filesystem layer is renamed to 'legacy', to indicate that
it is on the way out. New filesystems should try to build on this new
series. The rename is only partial, since there is no attempt here to
replace the existing filesystem commands.
Basic versions of a directory and file are also added. The directory
uclass only supports listing directories. The file uclass only supports
reading files. There is a small test.
From this small start, we can perhaps build on more features.
END
Signed-off-by: Simon Glass <sjg@chromium.org>
Directories can have a number of files within them. U-Boot only needs to
create a file object for those that have been opened for read/write. Add
the concept of a file, with its directory as parent.
Signed-off-by: Simon Glass <sjg@chromium.org>
Filesystems can have a number of directories within them. U-Boot only
worries about directories that have been accessed. Add the concept of
a directory, with the filesystem as parent.
Note that this is not a hierarchical device structure, so UCLASS_DIR
devices always have a UCLASS_FS as the parent.
Signed-off-by: Simon Glass <sjg@chromium.org>
The existing filesystem support is fairly basic. It supports access to
only one filesystem at a time, does not allow caching, does not have an
equivalent of Linux's VFS and uses global variables to keep track of
which one is in use.
As a starting point to improving this, provide a filesystem uclass. For
now it only includes operations to mount and unmount.
Provide a bootdev so that it is possible to locate bootflows on a
filesystem.
Signed-off-by: Simon Glass <sjg@chromium.org>
The existing disk and partition code assumes a block device.
Sandbox provides access to host files and does not operate through a
block device. This is currently handled with special-case logic.
We want to add virtio-fs support, which is also not a block device. It
is not easy to have these two co-exist, so add a new type field to
struct disk_partition to indicate the type.
This is a work-around to deal with the fact that the type needs to be
set by blk_get_device_part_str() in such a way that it is visible to
fs_readdir()
There is a global fs_type in disk/part.c but this is not available in
the fs/ code.
Signed-off-by: Simon Glass <sjg@chromium.org>
This existing function does not have any context associated with it.
Rename it so that fs_read() can be used for the new FS uclass.
Signed-off-by: Simon Glass <sjg@chromium.org>
Add a new SPL_FS_LEGACY which mirrors the recently added FS_LEGACY
option.
Select this new option when filesystems are required, either due to
SPL_FS_LOADER or one of the filesystems.
Signed-off-by: Simon Glass <sjg@chromium.org>
There is currently no Kconfig used and the filesystem code is always
included in U-Boot proper. Add an option to control this. For now it is
always on.
Signed-off-by: Simon Glass <sjg@chromium.org>
This filename is confusing since it is actually for the sandbox hostfs,
i.e. access to files on the host filesystem, not the host device which
can be used to bind disk images.
Rename it to hostfs_bootdev.c to make this clearer. Update the driver
name as well.
Signed-off-by: Simon Glass <sjg@chromium.org>
Using an abuf for this function simplifies returning the size and also
makes it easier to free memory afterwards. Update the API and callers.
Signed-off-by: Simon Glass <sjg@chromium.org>
Using an abuf for this function simplifies returning the size and also
makes it easier to free memory afterwards. Update the API and callers.
Signed-off-by: Simon Glass <sjg@chromium.org>
It is confusing to have both "$(PHASE_)" and "$(XPL_)" be used in our
Makefiles as part of the macros to determine when to do something in our
Makefiles based on what phase of the build we are in. For consistency,
bring this down to a single macro and use "$(PHASE_)" only.
Signed-off-by: Tom Rini <trini@konsulko.com>
Implement exfat_fs_rename() to rename or move files. This is used
by the 'mv' generic FS interface command. The rename implementation
for other filesystems was added recently and was not part of exfat
porting layer due to merge issue, which made 'mv' command crash,
fix this by adding the missing implementation.
Fixes: b86a651b64 ("fs: exfat: Add U-Boot porting layer")
Signed-off-by: Marek Vasut <marex@denx.de>
The exfat_fs_exists() should return 0 in case the path does not exist,
and 1 in case the path does exist. Fix the inverted return value. This
fixes 'test -e' command with exfat.
Fixes: b86a651b64 ("fs: exfat: Add U-Boot porting layer")
Signed-off-by: Marek Vasut <marex@denx.de>
The exfat_fs_readdir() depends on state created in exfat_fs_opendir(),
but that state may be disrupted by fs_close() called by the FS layer
in fs_opendir(), because exfat porting layer unmounts the filesystem
in ->close() callback.
To avoid this disruption, avoid creating state in exfat_fs_opendir(),
cache only the directory name to list there, and rework exfat_fs_readdir()
to work in a similar way to exfat_fs_ls(). That is, make exfat_fs_readdir()
open the directory, look up specific entry, extract its properties to be
reported to FS layer, and close the directory. This is slow, but avoids
the disruption. The slowness does not affect regular 'ls' command, which
uses exfat_fs_ls() fast path.
Fixes: b86a651b64 ("fs: exfat: Add U-Boot porting layer")
Signed-off-by: Marek Vasut <marex@denx.de>
Write into a bogus file, like '/.', triggers an "impossible"
print from the exfat core code. That should not be printed
in U-Boot, because U-Boot prints its own error message sooner.
Inhibit this error message.
The following command triggers the bogus print:
"
=> save host 0:0 1000008 /. 0x10
"
Fixes: b86a651b64 ("fs: exfat: Add U-Boot porting layer")
Signed-off-by: Marek Vasut <marex@denx.de>
Make sure the node is never dirty before being released, flush
the node first using exfat_flush_node() and only then release
the node using exfat_put_node(). This now matches the behavior
of exfat_fs_write() too.
Fixes: b86a651b64 ("fs: exfat: Add U-Boot porting layer")
Signed-off-by: Marek Vasut <marex@denx.de>
Demote "exFAT file system is not found" message to debug(). This is
printed when U-Boot attempts to auto-detect the filesystem via generic
filesystem API by attempting to mount the device, and fails to do so
because there is another filesystem in place. The libexfat-fuse code
prints this an error, which interferes with 'test_gpt' test. Demote
the message to debug().
Reviewed-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Marek Vasut <marex@denx.de>
Fix the following conversion overflow errors. The UTF8-to-UTF16
conversion is done through 32bit wchar_t, but U-Boot codebase is
built with -fshort-wchar which limits wchar_t to 16bit. Replace
the built-in wchar_t with u32 to assure the intermediate type is
32bit.
"
fs/exfat/utf.c: In function ‘utf8_to_wchar’:
fs/exfat/utf.c:165:23: warning: overflow in conversion from ‘int’ to ‘wchar_t’ {aka ‘short unsigned int’} changes value from ‘(int)(short unsigned int)*input << 18 & 1835008’ to ‘0’ [-Woverflow]
165 | *wc = ((wchar_t) input[0] & 0x07) << 18;
| ^
fs/exfat/utf.c:170:23: warning: overflow in conversion from ‘int’ to ‘wchar_t’ {aka ‘short unsigned int’} changes value from ‘(int)(short unsigned int)*input << 24 & 50331648’ to ‘0’ [-Woverflow]
170 | *wc = ((wchar_t) input[0] & 0x03) << 24;
| ^
fs/exfat/utf.c:175:23: warning: overflow in conversion from ‘int’ to ‘wchar_t’ {aka ‘short unsigned int’} changes value from ‘(int)(short unsigned int)*input << 30 & 1073741824’ to ‘0’ [-Woverflow]
175 | *wc = ((wchar_t) input[0] & 0x01) << 30;
| ^
"
Signed-off-by: Marek Vasut <marex@denx.de>
Add U-Boot adjustments to the libexfat code and integrate
the result into U-Boot filesystem layer. This provides full
read-write exfat support for U-Boot available via generic
filesystem interface.
FS_DIRENT_NAME_LEN is increased to 1024 in case exfat is
enabled, because EXFAT can use UTF16 names, which do not
fit into current FS_DIRENT_NAME_LEN. To avoid affecting
every configuration, increase FS_DIRENT_NAME_LEN only in
case EXFAT is enabled.
Example usage via sandbox, assuming disk.img with one exfat partition:
Drive info:
$ ./u-boot -Tc 'host bind 0 ../disk.img ; host info 0'
dev blocks blksz label path
0 262144 512 0 ../disk.img
List files:
$ ./u-boot -Tc 'host bind 0 ../disk.img ; ls host 0:1 /api'
475 Kconfig
230 Makefile
1873 README
...
10 file(s), 0 dir(s)
Load and checksum a file:
$ ./u-boot -Tc 'host bind 0 ../disk.img ; load host 0:1 $loadaddr .config ; \
crc32 $loadaddr $filesize'
56724 bytes read in 1 ms (54.1 MiB/s)
crc32 for 00000000 ... 0000dd93 ==> b2e847c9
$ crc32 .config
b2e847c9
Load .config file to RAM, store the file into FS as /newconfig,
load the /newconfig into RAM and checksum the file:
$ ./u-boot -Tc 'host bind 0 ../disk.img ; load host 0:1 $loadaddr .config ; \
save host 0:1 $loadaddr /newconfig $filesize ; \
load host 0:1 0x10000 /newconfig ; \
crc32 0x10000 $filesize'
56724 bytes read in 1 ms (54.1 MiB/s)
56724 bytes written in 0 ms
56724 bytes read in 0 ms
crc32 for 00010000 ... 0001dd93 ==> b2e847c9
Remove file 3.txt and create new directory /newdir:
$ ./u-boot -Tc 'host bind 0 ../disk.img ; ls host 0:1 / ; \
rm host 0:1 3.txt ; mkdir host 0:1 /newdir ; \
ls host 0:1 /'
...
0 1.txt
0 2.txt
0 3.txt
0 4.txt
0 5.txt
7 file(s), 4 dir(s)
...
0 1.txt
0 2.txt
newdir/
0 4.txt
0 5.txt
6 file(s), 5 dir(s)
Acked-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Marek Vasut <marex@denx.de>
Import most of libexfat from [1] except for log.c verbatim. The code
does not even compile and further adjustments and integration into
U-Boot filesystem code is in the next patch.
[1] https://github.com/relan/exfat
0b41c6d3560d ("CI: bump FreeBSD to 13.1.")
Acked-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Marek Vasut <marex@denx.de>
Add generic implementation of write into a block device to be used
by filesystem implementations. This is a pair function for already
existing fs_devread().
Signed-off-by: Marek Vasut <marex@denx.de>
POSIX filesystem functions that create or remove directory entries contain
text along the lines of "[function] shall mark for update the last data
modification and last file status change timestamps of the parent
directory of each file." [1][2][3] The common theme is these timestamp
updates occur when a directory entry is added or removed. The
create_link() and delete_dentry_link() functions have been changed to
update the modification timestamp on the directory where the direntry
change occurs. This differs slightly from Linux in the case of rename(),
where Linux will not update `new_path`'s parent directory's timestamp if
it is replacing an existing file. (via `vfat_add_entry` [4])
The timestamps are not updated if the build configuration does not support
RTCs. This is an effort to minimize introducing erratic timestamps where
they would go from [current date] -> 2000-01-01 (error timestamp in the
FAT driver). I would assume an unchanged timestamp would be more valuable
than a default timestamp in these cases.
[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html
[2] https://pubs.opengroup.org/onlinepubs/9799919799/functions/unlink.html
[3] https://pubs.opengroup.org/onlinepubs/9799919799/functions/open.html
[4] https://elixir.bootlin.com/linux/v6.12.6/source/fs/fat/namei_vfat.c#L682
Signed-off-by: Gabriel Dalimonte <gabriel.dalimonte@gmail.com>
The implementation roughly follows the POSIX specification for
rename() [1]. The ordering of operations attempting to minimize the chance
for data loss in unexpected circumstances.
The 'mv' command was implemented as a front end for the rename operation
as that is what most users are likely familiar with in terms of behavior.
The 'FAT_RENAME' Kconfig option was added to prevent code size increase on
size-oriented builds like SPL.
[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html
Signed-off-by: Gabriel Dalimonte <gabriel.dalimonte@gmail.com>
The create_link() code was previously duplicated in two existing functions.
The two functions will be used in a future commit to achieve renaming.
Signed-off-by: Gabriel Dalimonte <gabriel.dalimonte@gmail.com>
This makes a start on dealing with images loaded outside the context of
bootstd. For now, it just records these images. They can be listed using
the 'bootstd images' command.
Often, very little is known about these images, but future work could
perhaps use the filename or contents to detect the type.
Signed-off-by: Simon Glass <sjg@chromium.org>
The comment above fs_read_alloc() explains:
@align: Alignment to use for memory allocation (0 for default)
However, in the actual implementation, there is no alignment when @align is
zero.
This current default is probably fine for most cases. But for some block
devices which transfer data via DMA, ARCH_DMA_MINALIGN is needed.
Change the default alignment to ARCH_DMA_MINALIGN.
Fixes: de7b5a8a1a ("fs: Create functions to load and allocate a file")
Signed-off-by: Nam Cao <namcao@linutronix.de>
Tested-by: Javier Fernandez Pastrana <javier.pastrana@linutronix.de>
Running commands such as 'load mmc 2:1 $addr $path' when path does not
exists will print an error twice if the file does not exist, e.g.:
```
Cannot lookup file boot/boot.scr
Failed to load 'boot/boot.scr'
```
(where the first line is printed by btrfs and the second by common fs
code)
Historically other filesystems such as ext4 or fat have not been
printing a message here, so do the same here to avoid duplicate.
The other error messages in this function are also somewhat redundant,
but bring useful diagnostics if they happen somewhere, so have been left
as printf.
Note that if a user wants no message to be printed for optional file
loads, they have to check for file existence first with other commands
such as 'size'.
Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Now that opendir, readir, closedir are implemented for ext4 we can use
fs_ls_generic() for implementing the ls command.
Adjust the unit tests:
* fs_ls_generic() produces more spaces between file size and name.
* The ext4 specific message "** Can not find directory. **\n" is not
written anymore.
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
For accessing directories from the EFI sub-system a file system must
implement opendir, readdir, closedir. Provide the missing implementation.
With this patch the eficonfig command can be used to define load options
for the ext4 file system.
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>