blob: 794cfad4e70e4507e3362d8e370917e47ddc3932 [file] [log] [blame]
Sean Andersondcd91b12023-10-14 16:47:57 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#include <common.h>
8#include <image.h>
9#include <os.h>
10#include <spl.h>
Sean Andersonf65dcc02023-10-14 16:47:59 -040011#include <test/spl.h>
Sean Andersondcd91b12023-10-14 16:47:57 -040012#include <test/ut.h>
13
Sean Andersondcd91b12023-10-14 16:47:57 -040014/* Context used for this test */
15struct text_ctx {
16 int fd;
17};
18
Sean Anderson7d8d6132023-11-08 11:48:40 -050019static ulong read_fit_image(struct spl_load_info *load, ulong offset,
20 ulong size, void *buf)
Sean Andersondcd91b12023-10-14 16:47:57 -040021{
22 struct text_ctx *text_ctx = load->priv;
Sean Anderson7d8d6132023-11-08 11:48:40 -050023 off_t ret;
Sean Andersondcd91b12023-10-14 16:47:57 -040024 ssize_t res;
25
Sean Andersondcd91b12023-10-14 16:47:57 -040026 ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
27 if (ret != offset) {
28 printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
29 ret, errno);
30 return 0;
31 }
32
Sean Anderson7d8d6132023-11-08 11:48:40 -050033 res = os_read(text_ctx->fd, buf, size);
Sean Andersondcd91b12023-10-14 16:47:57 -040034 if (res == -1) {
35 printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
Sean Anderson7d8d6132023-11-08 11:48:40 -050036 size, res, errno);
Sean Andersondcd91b12023-10-14 16:47:57 -040037 return 0;
38 }
39
Sean Anderson7d8d6132023-11-08 11:48:40 -050040 return size;
Sean Andersondcd91b12023-10-14 16:47:57 -040041}
42
43static int spl_test_load(struct unit_test_state *uts)
44{
45 struct spl_image_info image;
46 struct legacy_img_hdr *header;
47 struct text_ctx text_ctx;
48 struct spl_load_info load;
49 char fname[256];
50 int ret;
51 int fd;
52
53 memset(&load, '\0', sizeof(load));
54 load.bl_len = 512;
55 load.read = read_fit_image;
56
57 ret = sandbox_find_next_phase(fname, sizeof(fname), true);
Sean Anderson20d95f82023-10-14 16:47:58 -040058 if (ret)
59 ut_assertf(0, "%s not found, error %d\n", fname, ret);
Sean Andersondcd91b12023-10-14 16:47:57 -040060 load.filename = fname;
61
62 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
63
64 fd = os_open(fname, OS_O_RDONLY);
65 ut_assert(fd >= 0);
66 ut_asserteq(512, os_read(fd, header, 512));
67 text_ctx.fd = fd;
68
69 load.priv = &text_ctx;
70
71 ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
72
73 return 0;
74}
75SPL_TEST(spl_test_load, 0);