[PATCH v3 3/8] spl: fit: Pass FIT context via a structure pointer

Alexandru Gagniuc
2020-12-23T14:44:07Z

Several loose arguments describe the FIT image. They are thus related,
and it makes sense to pass them together, in a structure. Examples
include the FIT blob pointer, offset to FDT nodes, and the offset to
external data.

Use a spl_fit_info structure to group these parameters.

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
common/spl/spl_fit.c | 101 ++++++++++++++++++-------------------------
1 file changed, 43 insertions(+), 58 deletions(-)

diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c
index bcc943b0b8..5a06e8a9db 100644
--- a/common/spl/spl_fit.c
+++ b/common/spl/spl_fit.c
@@ -76,7 +76,7 @@ static int find_node_from_desc(const void *fit, int node, const char *str)
*
* Return: 0 on success, or a negative error number
*/
-static int spl_fit_get_image_name(const void *fit, int images,
+static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
const char *type, int index,
const char **outname)
{
@@ -87,21 +87,21 @@ static int spl_fit_get_image_name(const void *fit, int images,
int len, i;
bool found = true;

- conf_node = fit_find_config_node(fit);
+ conf_node = fit_find_config_node(ctx->fit);
if (conf_node < 0) {
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
printf("No matching DT out of these options:\n");
- for (node = fdt_first_subnode(fit, conf_node);
+ for (node = fdt_first_subnode(ctx->fit, conf_node);
node >= 0;
- node = fdt_next_subnode(fit, node)) {
- name = fdt_getprop(fit, node, "description", &len);
+ node = fdt_next_subnode(ctx->fit, node)) {
+ name = fdt_getprop(ctx->fit, node, "description", &len);
printf(" %s\n", name);
}
#endif
return conf_node;
}

- name = fdt_getprop(fit, conf_node, type, &len);
+ name = fdt_getprop(ctx->fit, conf_node, type, &len);
if (!name) {
debug("cannot find property '%s': %d\n", type, len);
return -EINVAL;
@@ -135,11 +135,11 @@ static int spl_fit_get_image_name(const void *fit, int images,
* node name.
*/
int node;
- int images = fdt_path_offset(fit, FIT_IMAGES_PATH);
+ int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);

- node = find_node_from_desc(fit, images, str);
+ node = find_node_from_desc(ctx->fit, images, str);
if (node > 0)
- str = fdt_get_name(fit, node, NULL);
+ str = fdt_get_name(ctx->fit, node, NULL);

found = true;
}
@@ -166,20 +166,20 @@ static int spl_fit_get_image_name(const void *fit, int images,
* Return: the node offset of the respective image node or a negative
* error number.
*/
-static int spl_fit_get_image_node(const void *fit, int images,
+static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
const char *type, int index)
{
const char *str;
int err;
int node;

- err = spl_fit_get_image_name(fit, images, type, index, &str);
+ err = spl_fit_get_image_name(ctx, type, index, &str);
if (err)
return err;

debug("%s: '%s'\n", type, str);

- node = fdt_subnode_offset(fit, images, str);
+ node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
if (node < 0) {
pr_err("cannot find image node '%s': %d\n", str, node);
return -EINVAL;
@@ -230,10 +230,7 @@ static int get_aligned_image_size(struct spl_load_info *info, int data_size,
* spl_load_fit_image(): load the image described in a certain FIT node
* @info: points to information about the device to load data from
* @sector: the start sector of the FIT image on the device
- * @fit: points to the flattened device tree blob describing the FIT
- * image
- * @base_offset: the beginning of the data area containing the actual
- * image data, relative to the beginning of the FIT
+ * @ctx: points to the FIT context structure
* @node: offset of the DT node describing the image to load (relative
* to @fit)
* @image_info: will be filled with information about the loaded image
@@ -244,7 +241,7 @@ static int get_aligned_image_size(struct spl_load_info *info, int data_size,
* Return: 0 on success or a negative error number.
*/
static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
- void *fit, ulong base_offset, int node,
+ const struct spl_fit_info *ctx, int node,
struct spl_image_info *image_info)
{
int offset;
@@ -258,6 +255,7 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
int align_len = ARCH_DMA_MINALIGN - 1;
uint8_t image_comp = -1, type = -1;
const void *data;
+ const void *fit = ctx->fit;
bool external_data = false;

if (IS_ENABLED(CONFIG_SPL_FPGA) ||
@@ -279,7 +277,7 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
if (!fit_image_get_data_position(fit, node, &offset)) {
external_data = true;
} else if (!fit_image_get_data_offset(fit, node, &offset)) {
- offset += base_offset;
+ offset += ctx->ext_data_offset;
external_data = true;
}

@@ -355,7 +353,7 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector,

static int spl_fit_append_fdt(struct spl_image_info *spl_image,
struct spl_load_info *info, ulong sector,
- void *fit, int images, ulong base_offset)
+ const struct spl_fit_info *ctx)
{
struct spl_image_info image_info;
int node, ret = 0, index = 0;
@@ -367,7 +365,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
image_info.load_addr = spl_image->load_addr + spl_image->size;

/* Figure out which device tree the board wants to use */
- node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, index++);
+ node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
if (node < 0) {
debug("%s: cannot find FDT node\n", __func__);

@@ -381,7 +379,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
else
return node;
} else {
- ret = spl_load_fit_image(info, sector, fit, base_offset, node,
+ ret = spl_load_fit_image(info, sector, ctx, node,
&image_info);
if (ret < 0)
return ret;
@@ -394,8 +392,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
void *tmpbuffer = NULL;

for (; ; index++) {
- node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP,
- index);
+ node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index);
if (node == -E2BIG) {
debug("%s: No additional FDT node\n", __func__);
break;
@@ -418,7 +415,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
__func__);
}
image_info.load_addr = (ulong)tmpbuffer;
- ret = spl_load_fit_image(info, sector, fit, base_offset,
+ ret = spl_load_fit_image(info, sector, ctx,
node, &image_info);
if (ret < 0)
break;
@@ -433,12 +430,12 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
(void *)image_info.load_addr);
if (ret) {
pr_err("failed to apply DT overlay %s\n",
- fit_get_name(fit, node, NULL));
+ fit_get_name(ctx->fit, node, NULL));
break;
}

debug("%s: DT overlay %s applied\n", __func__,
- fit_get_name(fit, node, NULL));
+ fit_get_name(ctx->fit, node, NULL));
}
free(tmpbuffer);
if (ret)
@@ -453,7 +450,7 @@ static int spl_fit_append_fdt(struct spl_image_info *spl_image,
return ret;
}

-static int spl_fit_record_loadable(const void *fit, int images, int index,
+static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
void *blob, struct spl_image_info *image)
{
int ret = 0;
@@ -461,17 +458,16 @@ static int spl_fit_record_loadable(const void *fit, int images, int index,
const char *name;
int node;

- ret = spl_fit_get_image_name(fit, images, "loadables",
- index, &name);
+ ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
if (ret < 0)
return ret;

- node = spl_fit_get_image_node(fit, images, "loadables", index);
+ node = spl_fit_get_image_node(ctx, "loadables", index);

ret = fdt_record_loadable(blob, index, name, image->load_addr,
image->size, image->entry_point,
- fdt_getprop(fit, node, "type", NULL),
- fdt_getprop(fit, node, "os", NULL));
+ fdt_getprop(ctx->fit, node, "type", NULL),
+ fdt_getprop(ctx->fit, node, "os", NULL));
#endif
return ret;
}
@@ -591,8 +587,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
struct spl_image_info image_info;
struct spl_fit_info ctx;
int node = -1;
- int images, ret;
- int base_offset;
+ int ret;
int index = 0;
int firmware_node;

@@ -608,16 +603,11 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
if (ret < 0)
return ret;

- images = ctx.images_node;
- fit = (void *)ctx.fit;
- base_offset = ctx.ext_data_offset;
-
#ifdef CONFIG_SPL_FPGA
- node = spl_fit_get_image_node(fit, images, "fpga", 0);
+ node = spl_fit_get_image_node(&ctx, "fpga", 0);
if (node >= 0) {
/* Load the image and set up the spl_image structure */
- ret = spl_load_fit_image(info, sector, fit, base_offset, node,
- spl_image);
+ ret = spl_load_fit_image(info, sector, ctx, node, spl_image);
if (ret) {
printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
return ret;
@@ -646,15 +636,14 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
* - fall back to using the first 'loadables' entry
*/
if (node < 0)
- node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
- 0);
+ node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
#ifdef CONFIG_SPL_OS_BOOT
if (node < 0)
- node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
+ node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
#endif
if (node < 0) {
debug("could not find firmware image, trying loadables...\n");
- node = spl_fit_get_image_node(fit, images, "loadables", 0);
+ node = spl_fit_get_image_node(&ctx, "loadables", 0);
/*
* If we pick the U-Boot image from "loadables", start at
* the second image when later loading additional images.
@@ -668,8 +657,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
}

/* Load the image and set up the spl_image structure */
- ret = spl_load_fit_image(info, sector, fit, base_offset, node,
- spl_image);
+ ret = spl_load_fit_image(info, sector, &ctx, node, spl_image);
if (ret)
return ret;

@@ -677,7 +665,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
* For backward compatibility, we treat the first node that is
* as a U-Boot image, if no OS-type has been declared.
*/
- if (!spl_fit_image_get_os(fit, node, &spl_image->os))
+ if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
#if !defined(CONFIG_SPL_OS_BOOT)
else
@@ -689,8 +677,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
* We allow this to fail, as the U-Boot image might embed its FDT.
*/
if (spl_image->os == IH_OS_U_BOOT) {
- ret = spl_fit_append_fdt(spl_image, info, sector, fit,
- images, base_offset);
+ ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
if (!IS_ENABLED(CONFIG_OF_EMBED) && ret < 0)
return ret;
}
@@ -700,7 +687,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
for (; ; index++) {
uint8_t os_type = IH_OS_INVALID;

- node = spl_fit_get_image_node(fit, images, "loadables", index);
+ node = spl_fit_get_image_node(&ctx, "loadables", index);
if (node < 0)
break;

@@ -712,17 +699,15 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
if (firmware_node == node)
continue;

- ret = spl_load_fit_image(info, sector, fit, base_offset, node,
- &image_info);
+ ret = spl_load_fit_image(info, sector, &ctx, node, &image_info);
if (ret < 0)
continue;

- if (!spl_fit_image_get_os(fit, node, &os_type))
+ if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
debug("Loadable is %s\n", genimg_get_os_name(os_type));

if (os_type == IH_OS_U_BOOT) {
- spl_fit_append_fdt(&image_info, info, sector,
- fit, images, base_offset);
+ spl_fit_append_fdt(&image_info, info, sector, &ctx);
spl_image->fdt_addr = image_info.fdt_addr;
}

@@ -736,7 +721,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,

/* Record our loadables into the FDT */
if (spl_image->fdt_addr)
- spl_fit_record_loadable(fit, images, index,
+ spl_fit_record_loadable(&ctx, index,
spl_image->fdt_addr,
&image_info);
}
@@ -752,7 +737,7 @@ int spl_load_simple_fit(struct spl_image_info *spl_image,
spl_image->flags |= SPL_FIT_FOUND;

#ifdef CONFIG_IMX_HAB
- board_spl_fit_post_load(fit);
+ board_spl_fit_post_load(ctx.fit);
#endif

return 0;
--
2.26.2