test: Move some SPL-loading test-code into sandbox common

This code is useful for loading an image in sandbox_spl so move it into
a place where it can be called as needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2024-08-07 16:47:21 -06:00
committed by Tom Rini
parent d9f1b00bc7
commit 615d84b6ce
3 changed files with 88 additions and 52 deletions

View File

@@ -6,6 +6,7 @@
#include <dm.h>
#include <hang.h>
#include <handoff.h>
#include <image.h>
#include <init.h>
#include <log.h>
#include <os.h>
@@ -179,3 +180,75 @@ int handoff_arch_save(struct spl_handoff *ho)
return 0;
}
/* Context used to hold file descriptor */
struct load_ctx {
int fd;
};
static ulong read_fit_image(struct spl_load_info *load, ulong offset,
ulong size, void *buf)
{
struct load_ctx *load_ctx = load->priv;
off_t ret;
ssize_t res;
ret = os_lseek(load_ctx->fd, offset, OS_SEEK_SET);
if (ret < 0) {
printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
ret, errno);
return log_msg_ret("lse", -errno);
}
res = os_read(load_ctx->fd, buf, size);
if (res < 0) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
size, res, errno);
return log_msg_ret("osr", -errno);
}
return size;
}
int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image)
{
struct legacy_img_hdr *header;
struct load_ctx load_ctx;
struct spl_load_info load;
int ret;
int fd;
memset(&load, '\0', sizeof(load));
spl_set_bl_len(&load, IS_ENABLED(CONFIG_SPL_LOAD_BLOCK) ? 512 : 1);
load.read = read_fit_image;
ret = sandbox_find_next_phase(fname, maxlen, true);
if (ret) {
printf("%s not found, error %d\n", fname, ret);
return log_msg_ret("nph", ret);
}
header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
log_debug("reading from %s\n", fname);
fd = os_open(fname, OS_O_RDONLY);
if (fd < 0) {
printf("Failed to open '%s'\n", fname);
return log_msg_ret("ope", -errno);
}
ret = os_read(fd, header, sizeof(*header));
if (ret != sizeof(*header)) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
sizeof(*header), ret, -errno);
return log_msg_ret("rea", -errno);
}
load_ctx.fd = fd;
load.priv = &load_ctx;
ret = spl_load_simple_fit(image, &load, 0, header);
if (ret)
return log_msg_ret("slf", ret);
return 0;
}

View File

@@ -6,6 +6,8 @@
#ifndef __asm_spl_h
#define __asm_spl_h
struct spl_image_info;
enum {
BOOT_DEVICE_MMC1,
BOOT_DEVICE_MMC2,
@@ -31,4 +33,16 @@ enum {
*/
int sandbox_find_next_phase(char *fname, int maxlen, bool use_img);
/**
* sandbox_spl_load_fit() - Load the next phase from a FIT
*
* Loads a FIT containing the next phase and sets it up for booting
*
* @fname: Returns filename loaded
* @maxlen: Maximum length for @fname including \0
* @image: Place to put SPL-image information
* Return: 0 if OK, -ve on error
*/
int sandbox_spl_load_fit(char *fname, int maxlen, struct spl_image_info *image);
#endif

View File

@@ -10,63 +10,12 @@
#include <test/spl.h>
#include <test/ut.h>
/* Context used for this test */
struct text_ctx {
int fd;
};
static ulong read_fit_image(struct spl_load_info *load, ulong offset,
ulong size, void *buf)
{
struct text_ctx *text_ctx = load->priv;
off_t ret;
ssize_t res;
ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
if (ret != offset) {
printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
ret, errno);
return 0;
}
res = os_read(text_ctx->fd, buf, size);
if (res == -1) {
printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
size, res, errno);
return 0;
}
return size;
}
static int spl_test_load(struct unit_test_state *uts)
{
struct spl_image_info image;
struct legacy_img_hdr *header;
struct text_ctx text_ctx;
struct spl_load_info load;
char fname[256];
int ret;
int fd;
memset(&load, '\0', sizeof(load));
spl_set_bl_len(&load, 512);
load.read = read_fit_image;
ret = sandbox_find_next_phase(fname, sizeof(fname), true);
if (ret)
ut_assertf(0, "%s not found, error %d\n", fname, ret);
header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
fd = os_open(fname, OS_O_RDONLY);
ut_assert(fd >= 0);
ut_asserteq(512, os_read(fd, header, 512));
text_ctx.fd = fd;
load.priv = &text_ctx;
ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
ut_assertok(sandbox_spl_load_fit(fname, sizeof(fname), &image));
return 0;
}