u-boot/common/spl/spl_fat.c
Prasad Kale c488907f91 Invalidate cached FAT device upon boot error
When spl boot device list has multiple FAT devices, any previousely
registered FAT device should be deregistered before registering
next FAT boot device, otherwise the function may not attempt boot
from next FAT device.One of the situations where this issue can be
observed is when the boot device list has two FAT partitions of a
memory device and if booting fails on first partition (because of
file or partition related errors), boot from next partition actually
gets attempted on previous boot device only, as the previous device
has remained marked as registered. Call the function that invalidates
cached boot device in case of failure in booting from current FAT
boot device.

Signed-off-by: Prasad Kale <prasad.kale@live.com>
Cc: Dan Murphy <dmurphy@ti.com>
Cc: Sean Anderson <seanga2@gmail.com>
2025-11-06 08:31:34 -06:00

178 lines
3.7 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2014
* Texas Instruments, <www.ti.com>
*
* Dan Murphy <dmurphy@ti.com>
*
* FAT Image Functions copied from spl_mmc.c
*/
#include <env.h>
#include <log.h>
#include <spl.h>
#include <spl_load.h>
#include <fat.h>
#include <errno.h>
#include <image.h>
#include <linux/libfdt.h>
#include <asm/cache.h>
static int fat_registered;
void spl_fat_force_reregister(void)
{
fat_registered = 0;
}
static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
{
int err = 0;
if (fat_registered)
return err;
err = fat_register_device(block_dev, partition);
if (err) {
printf("%s: fat register err - %d\n", __func__, err);
return err;
}
fat_registered = 1;
return err;
}
static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
ulong size, void *buf)
{
struct legacy_img_hdr *header;
loff_t actread;
int ret;
char *filename = load->priv;
ret = fat_read_file(filename, buf, file_offset, size, &actread);
if (ret)
return ret;
if (CONFIG_IS_ENABLED(OS_BOOT)) {
header = (struct legacy_img_hdr *)buf;
if (image_get_magic(header) != FDT_MAGIC)
return size;
}
return actread;
}
int spl_load_image_fat(struct spl_image_info *spl_image,
struct spl_boot_device *bootdev,
struct blk_desc *block_dev, int partition,
const char *filename)
{
int err;
loff_t size;
struct spl_load_info load;
err = spl_register_fat_device(block_dev, partition);
if (err)
goto end;
/*
* Avoid pulling in this function for other image types since we are
* very short on space on some boards.
*/
if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL)) {
err = fat_size(filename, &size);
if (err)
goto end;
} else {
size = 0;
}
spl_load_init(&load, spl_fit_read, (void *)filename,
IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN) ?
ARCH_DMA_MINALIGN : 1);
err = spl_load(spl_image, bootdev, &load, size, 0);
end:
if (err < 0) {
spl_fat_force_reregister();
printf("%s: error reading image %s, err - %d\n",
__func__, filename, err);
}
return err;
}
#if CONFIG_IS_ENABLED(OS_BOOT)
int spl_load_image_fat_os(struct spl_image_info *spl_image,
struct spl_boot_device *bootdev,
struct blk_desc *block_dev, int partition)
{
int err;
__maybe_unused char *file;
err = spl_register_fat_device(block_dev, partition);
if (err)
return err;
if (!CONFIG_IS_ENABLED(ENV_SUPPORT))
goto defaults;
file = env_get("falcon_image_file");
if (file) {
err = spl_load_image_fat(spl_image, bootdev, block_dev,
partition, file);
if (err != 0) {
puts("spl: falling back to default\n");
goto defaults;
}
#if IS_ENABLED(CONFIG_SPL_OS_BOOT_ARGS)
file = env_get("falcon_args_file");
if (file) {
err = file_fat_read(
file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
if (err <= 0) {
printf("spl: error reading args %s, err - %d, falling back to default\n",
file, err);
goto defaults;
}
return 0;
} else
puts("spl: falcon_args_file not set in environment, falling back to default\n");
#endif
} else
puts("spl: falcon_image_file not set in environment, falling back to default\n");
defaults:
err = spl_load_image_fat(spl_image, bootdev, block_dev, partition,
CONFIG_SPL_FS_LOAD_KERNEL_NAME);
if (err)
return err;
#if IS_ENABLED(CONFIG_SPL_OS_BOOT_ARGS)
err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
(void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
if (err <= 0) {
printf("%s: error reading image %s, err - %d\n",
__func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
return -1;
}
#endif
return 0;
}
#else
int spl_load_image_fat_os(struct spl_image_info *spl_image,
struct spl_boot_device *bootdev,
struct blk_desc *block_dev, int partition)
{
return -ENOSYS;
}
#endif