spl: Create a function to init spl_load_info

Rather than having every caller set this up individually, create a
common init function. This allows new fields to be added without the
risk of them being left uninited.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Michael Trimarchi <michael@amarulasolutions.com>
This commit is contained in:
Simon Glass
2024-08-22 07:55:02 -06:00
committed by Tom Rini
parent 50a1ed4335
commit 3fd11278ff
18 changed files with 57 additions and 76 deletions

View File

@@ -108,18 +108,13 @@ static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) { if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && image_get_magic(header) == FDT_MAGIC) {
struct spl_load_info load; struct spl_load_info load;
memset(&load, 0, sizeof(load)); spl_load_init(&load, spl_romapi_read_seekable, NULL, pagesize);
spl_set_bl_len(&load, pagesize);
load.read = spl_romapi_read_seekable;
return spl_load_simple_fit(spl_image, &load, offset, header); return spl_load_simple_fit(spl_image, &load, offset, header);
} else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) && } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
valid_container_hdr((void *)header)) { valid_container_hdr((void *)header)) {
struct spl_load_info load; struct spl_load_info load;
memset(&load, 0, sizeof(load)); spl_load_init(&load, spl_romapi_read_seekable, NULL, pagesize);
spl_set_bl_len(&load, pagesize);
load.read = spl_romapi_read_seekable;
ret = spl_load_imx_container(spl_image, &load, offset); ret = spl_load_imx_container(spl_image, &load, offset);
} else { } else {
/* TODO */ /* TODO */
@@ -341,10 +336,7 @@ static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
ss.end = p; ss.end = p;
ss.pagesize = pagesize; ss.pagesize = pagesize;
memset(&load, 0, sizeof(load)); spl_load_init(&load, spl_romapi_read_stream, &ss, 1);
spl_set_bl_len(&load, 1);
load.read = spl_romapi_read_stream;
load.priv = &ss;
return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr); return spl_load_simple_fit(spl_image, &load, (ulong)phdr, phdr);
} }

View File

@@ -390,8 +390,7 @@ static int spl_spi_load_image(struct spl_image_info *spl_image,
struct spl_load_info load; struct spl_load_info load;
debug("Found FIT image\n"); debug("Found FIT image\n");
spl_set_bl_len(&load, 1); spl_load_init(&load, spi_load_read, NULL, 1);
load.read = spi_load_read;
ret = spl_load_simple_fit(spl_image, &load, ret = spl_load_simple_fit(spl_image, &load,
load_offset, header); load_offset, header);
} else { } else {

View File

@@ -221,9 +221,8 @@ int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
int ret; int ret;
int fd; int fd;
memset(&load, '\0', sizeof(load)); spl_load_init(&load, read_fit_image, &load_ctx,
spl_set_bl_len(&load, IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1); IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1);
load.read = read_fit_image;
ret = sandbox_find_next_phase(fname, maxlen, true); ret = sandbox_find_next_phase(fname, maxlen, true);
if (ret) { if (ret) {

View File

@@ -80,11 +80,8 @@ int spl_blk_load_image(struct spl_image_info *spl_image,
return ret; return ret;
} }
load.read = spl_fit_read; spl_load_init(&load, spl_fit_read, &dev,
if (IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN)) IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN) ?
spl_set_bl_len(&load, ARCH_DMA_MINALIGN); ARCH_DMA_MINALIGN : 1);
else
spl_set_bl_len(&load, 1);
load.priv = &dev;
return spl_load(spl_image, bootdev, &load, filesize, 0); return spl_load(spl_image, bootdev, &load, filesize, 0);
} }

View File

@@ -51,8 +51,7 @@ int spl_load_image_ext(struct spl_image_info *spl_image,
goto end; goto end;
} }
spl_set_bl_len(&load, 1); spl_load_init(&load, spl_fit_read, NULL, 1);
load.read = spl_fit_read;
err = spl_load(spl_image, bootdev, &load, filelen, 0); err = spl_load(spl_image, bootdev, &load, filelen, 0);
end: end:

View File

@@ -83,12 +83,10 @@ int spl_load_image_fat(struct spl_image_info *spl_image,
size = 0; size = 0;
} }
load.read = spl_fit_read; spl_load_init(&load, spl_fit_read, (void *)filename,
if (IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN)) IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN) ?
spl_set_bl_len(&load, ARCH_DMA_MINALIGN); ARCH_DMA_MINALIGN : 1);
else
spl_set_bl_len(&load, 1);
load.priv = (void *)filename;
err = spl_load(spl_image, bootdev, &load, size, 0); err = spl_load(spl_image, bootdev, &load, size, 0);
end: end:

View File

@@ -46,9 +46,7 @@ int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
struct blk_desc *bd = mmc_get_blk_desc(mmc); struct blk_desc *bd = mmc_get_blk_desc(mmc);
struct spl_load_info load; struct spl_load_info load;
load.priv = bd; spl_load_init(&load, h_spl_load_read, bd, bd->blksz);
spl_set_bl_len(&load, bd->blksz);
load.read = h_spl_load_read;
ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz); ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
if (ret) { if (ret) {
puts("mmc_load_image_raw_sector: mmc block read error\n"); puts("mmc_load_image_raw_sector: mmc block read error\n");

View File

@@ -71,9 +71,7 @@ static int spl_nand_load_element(struct spl_image_info *spl_image,
{ {
struct spl_load_info load; struct spl_load_info load;
load.priv = &offset; spl_load_init(&load, spl_nand_read, &offset, 1);
spl_set_bl_len(&load, 1);
load.read = spl_nand_read;
return spl_load(spl_image, bootdev, &load, 0, offset); return spl_load(spl_image, bootdev, &load, 0, offset);
} }

View File

@@ -47,8 +47,7 @@ static int spl_net_load_image(struct spl_image_info *spl_image,
return rv; return rv;
} }
spl_set_bl_len(&load, 1); spl_load_init(&load, spl_net_load_read, NULL, 1);
load.read = spl_net_load_read;
return spl_load(spl_image, bootdev, &load, 0, 0); return spl_load(spl_image, bootdev, &load, 0, 0);
} }
#endif #endif

View File

@@ -49,8 +49,7 @@ static int spl_nor_load_image(struct spl_image_info *spl_image,
int ret; int ret;
debug("Found FIT\n"); debug("Found FIT\n");
spl_set_bl_len(&load, 1); spl_load_init(&load, spl_nor_load_read, NULL, 1);
load.read = spl_nor_load_read;
ret = spl_load_simple_fit(spl_image, &load, ret = spl_load_simple_fit(spl_image, &load,
CONFIG_SYS_OS_BASE, CONFIG_SYS_OS_BASE,
@@ -93,8 +92,7 @@ static int spl_nor_load_image(struct spl_image_info *spl_image,
* Load real U-Boot from its location in NOR flash to its * Load real U-Boot from its location in NOR flash to its
* defined location in SDRAM * defined location in SDRAM
*/ */
spl_set_bl_len(&load, 1); spl_load_init(&load, spl_nor_load_read, NULL, 1);
load.read = spl_nor_load_read;
return spl_load(spl_image, bootdev, &load, 0, spl_nor_get_uboot_base()); return spl_load(spl_image, bootdev, &load, 0, spl_nor_get_uboot_base());
} }
SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image); SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);

View File

@@ -69,8 +69,7 @@ static int spl_ram_load_image(struct spl_image_info *spl_image,
struct spl_load_info load; struct spl_load_info load;
debug("Found FIT\n"); debug("Found FIT\n");
spl_set_bl_len(&load, 1); spl_load_init(&load, spl_ram_load_read, NULL, 1);
load.read = spl_ram_load_read;
ret = spl_load_simple_fit(spl_image, &load, 0, header); ret = spl_load_simple_fit(spl_image, &load, 0, header);
} else { } else {
ulong u_boot_pos = spl_get_image_pos(); ulong u_boot_pos = spl_get_image_pos();

View File

@@ -43,9 +43,7 @@ static int spl_smh_load_image(struct spl_image_info *spl_image,
} }
len = ret; len = ret;
load.read = smh_fit_read; spl_load_init(&load, smh_fit_read, &fd, 1);
spl_set_bl_len(&load, 1);
load.priv = &fd;
ret = spl_load(spl_image, bootdev, &load, len, 0); ret = spl_load(spl_image, bootdev, &load, len, 0);
if (ret) if (ret)
log_debug("could not read %s: %d\n", filename, ret); log_debug("could not read %s: %d\n", filename, ret);

View File

@@ -77,9 +77,7 @@ static int spl_spi_load_image(struct spl_image_info *spl_image,
return -ENODEV; return -ENODEV;
} }
load.priv = flash; spl_load_init(&load, spl_spi_fit_read, flash, 1);
spl_set_bl_len(&load, 1);
load.read = spl_spi_fit_read;
#if CONFIG_IS_ENABLED(OS_BOOT) #if CONFIG_IS_ENABLED(OS_BOOT)
if (spl_start_uboot()) { if (spl_start_uboot()) {

View File

@@ -132,11 +132,9 @@ int spl_ymodem_load_image(struct spl_image_info *spl_image,
struct ymodem_fit_info info; struct ymodem_fit_info info;
debug("Found FIT\n"); debug("Found FIT\n");
load.priv = (void *)&info; spl_load_init(&load, ymodem_read_fit, (void *)&info, 1);
spl_set_bl_len(&load, 1);
info.buf = buf; info.buf = buf;
info.image_read = BUF_SIZE; info.image_read = BUF_SIZE;
load.read = ymodem_read_fit;
ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
size = info.image_read; size = info.image_read;

View File

@@ -842,9 +842,7 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,
struct spl_load_info load; struct spl_load_info load;
debug("Found FIT\n"); debug("Found FIT\n");
load.priv = header; spl_load_init(&load, sdp_load_read, header, 1);
spl_set_bl_len(&load, 1);
load.read = sdp_load_read;
spl_load_simple_fit(spl_image, &load, 0, spl_load_simple_fit(spl_image, &load, 0,
header); header);
@@ -855,9 +853,7 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,
valid_container_hdr((void *)header)) { valid_container_hdr((void *)header)) {
struct spl_load_info load; struct spl_load_info load;
load.priv = header; spl_load_init(&load, sdp_load_read, header, 1);
spl_set_bl_len(&load, 1);
load.read = sdp_load_read;
spl_load_imx_container(spl_image, &load, 0); spl_load_imx_container(spl_image, &load, 0);
return SDP_EXIT; return SDP_EXIT;
} }

View File

@@ -282,17 +282,10 @@ static inline void *spl_image_fdt_addr(struct spl_image_info *info)
#endif #endif
} }
struct spl_load_info;
/** /**
* Information required to load data from a device * spl_load_reader() - Read from device
*
* @priv: Private data for the device
* @bl_len: Block length for reading in bytes
* @read: Function to call to read from the device
*/
struct spl_load_info {
void *priv;
/**
* read() - Read from device
* *
* @load: Information about the load state * @load: Information about the load state
* @offset: Offset to read from in bytes. This must be a multiple of * @offset: Offset to read from in bytes. This must be a multiple of
@@ -302,8 +295,19 @@ struct spl_load_info {
* @buf: Buffer to read into * @buf: Buffer to read into
* @return number of bytes read, 0 on error * @return number of bytes read, 0 on error
*/ */
ulong (*read)(struct spl_load_info *load, ulong sector, ulong count, typedef ulong (*spl_load_reader)(struct spl_load_info *load, ulong sector,
void *buf); ulong count, void *buf);
/**
* Information required to load data from a device
*
* @read: Function to call to read from the device
* @priv: Private data for the device
* @bl_len: Block length for reading in bytes
*/
struct spl_load_info {
spl_load_reader read;
void *priv;
#if IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) #if IS_ENABLED(CONFIG_SPL_LOAD_BLOCK)
int bl_len; int bl_len;
#endif #endif
@@ -328,6 +332,18 @@ static inline void spl_set_bl_len(struct spl_load_info *info, int bl_len)
#endif #endif
} }
/**
* spl_load_init() - Set up a new spl_load_info structure
*/
static inline void spl_load_init(struct spl_load_info *load,
spl_load_reader h_read, void *priv,
uint bl_len)
{
load->read = h_read;
load->priv = priv;
spl_set_bl_len(load, bl_len);
}
/* /*
* We need to know the position of U-Boot in memory so we can jump to it. We * We need to know the position of U-Boot in memory so we can jump to it. We
* allow any U-Boot binary to be used (u-boot.bin, u-boot-nodtb.bin, * allow any U-Boot binary to be used (u-boot.bin, u-boot-nodtb.bin,

View File

@@ -343,9 +343,7 @@ static int spl_test_image(struct unit_test_state *uts, const char *test_name,
} else { } else {
struct spl_load_info load; struct spl_load_info load;
spl_set_bl_len(&load, 1); spl_load_init(&load, spl_test_read, img, 1);
load.priv = img;
load.read = spl_test_read;
if (type == IMX8) if (type == IMX8)
ut_assertok(spl_load_imx_container(&info_read, &load, ut_assertok(spl_load_imx_container(&info_read, &load,
0)); 0));

View File

@@ -20,3 +20,4 @@ static int spl_test_load(struct unit_test_state *uts)
return 0; return 0;
} }
SPL_TEST(spl_test_load, 0); SPL_TEST(spl_test_load, 0);