u-boot/fs/exfat/repair.c
Marek Vasut b86a651b64 fs: exfat: Add U-Boot porting layer
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>
2025-04-02 20:00:59 -06:00

116 lines
2.7 KiB
C

/*
repair.c (09.03.17)
exFAT file system implementation library.
Free exFAT implementation.
Copyright (C) 2010-2023 Andrew Nayenko
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "exfat.h"
#ifndef __UBOOT__
#include <strings.h>
#endif
int exfat_errors_fixed;
bool exfat_ask_to_fix(const struct exfat* ef)
{
const char* question = "Fix (Y/N)?";
#ifndef __UBOOT__
char answer[8];
bool yeah, nope;
#endif
switch (ef->repair)
{
case EXFAT_REPAIR_NO:
return false;
case EXFAT_REPAIR_YES:
printf("%s %s", question, "Y\n");
return true;
case EXFAT_REPAIR_ASK:
#ifndef __UBOOT__
do
{
printf("%s ", question);
fflush(stdout);
if (fgets(answer, sizeof(answer), stdin))
{
yeah = strcasecmp(answer, "Y\n") == 0;
nope = strcasecmp(answer, "N\n") == 0;
}
else
{
yeah = false;
nope = true;
}
}
while (!yeah && !nope);
return yeah;
#else
default:
/* Do not attempt to repair FS in U-Boot. */
return false;
#endif
}
exfat_bug("invalid repair option value: %d", ef->repair);
#ifdef __UBOOT__
return false;
#endif
}
bool exfat_fix_invalid_vbr_checksum(const struct exfat* ef, void* sector,
uint32_t vbr_checksum)
{
size_t i;
off_t sector_size = SECTOR_SIZE(*ef->sb);
for (i = 0; i < sector_size / sizeof(vbr_checksum); i++)
((le32_t*) sector)[i] = cpu_to_le32(vbr_checksum);
if (exfat_pwrite(ef->dev, sector, sector_size, 11 * sector_size) < 0)
{
exfat_error("failed to write correct VBR checksum");
return false;
}
exfat_errors_fixed++;
return true;
}
bool exfat_fix_invalid_node_checksum(UNUSED const struct exfat* ef,
struct exfat_node* node)
{
/* checksum will be rewritten by exfat_flush_node() */
node->is_dirty = true;
exfat_errors_fixed++;
return true;
}
bool exfat_fix_unknown_entry(struct exfat* ef, struct exfat_node* dir,
const struct exfat_entry* entry, off_t offset)
{
struct exfat_entry deleted = *entry;
deleted.type &= ~EXFAT_ENTRY_VALID;
if (exfat_generic_pwrite(ef, dir, &deleted, sizeof(struct exfat_entry),
offset) != sizeof(struct exfat_entry))
return false;
exfat_errors_fixed++;
return true;
}