blob: 26228a8a4a9ce851235757e19fd162891a640792 [file] [log] [blame]
Sean Andersonc56468a62023-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 Andersonb93cc1e2023-10-14 16:47:59 -040011#include <test/spl.h>
Sean Andersonc56468a62023-10-14 16:47:57 -040012#include <test/ut.h>
13
Sean Andersonc56468a62023-10-14 16:47:57 -040014/* Context used for this test */
15struct text_ctx {
16 int fd;
17};
18
Sean Anderson73c40fc2023-11-08 11:48:40 -050019static ulong read_fit_image(struct spl_load_info *load, ulong offset,
20 ulong size, void *buf)
Sean Andersonc56468a62023-10-14 16:47:57 -040021{
22 struct text_ctx *text_ctx = load->priv;
Sean Anderson73c40fc2023-11-08 11:48:40 -050023 off_t ret;
Sean Andersonc56468a62023-10-14 16:47:57 -040024 ssize_t res;
25
Sean Andersonc56468a62023-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 Anderson73c40fc2023-11-08 11:48:40 -050033 res = os_read(text_ctx->fd, buf, size);
Sean Andersonc56468a62023-10-14 16:47:57 -040034 if (res == -1) {
35 printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
Sean Anderson73c40fc2023-11-08 11:48:40 -050036 size, res, errno);
Sean Andersonc56468a62023-10-14 16:47:57 -040037 return 0;
38 }
39
Sean Anderson73c40fc2023-11-08 11:48:40 -050040 return size;
Sean Andersonc56468a62023-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));
Sean Anderson5271e352023-11-08 11:48:43 -050054 spl_set_bl_len(&load, 512);
Sean Andersonc56468a62023-10-14 16:47:57 -040055 load.read = read_fit_image;
56
57 ret = sandbox_find_next_phase(fname, sizeof(fname), true);
Sean Andersonb5bf83b2023-10-14 16:47:58 -040058 if (ret)
59 ut_assertf(0, "%s not found, error %d\n", fname, ret);
Sean Andersonc56468a62023-10-14 16:47:57 -040060
61 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
62
63 fd = os_open(fname, OS_O_RDONLY);
64 ut_assert(fd >= 0);
65 ut_asserteq(512, os_read(fd, header, 512));
66 text_ctx.fd = fd;
67
68 load.priv = &text_ctx;
69
70 ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
71
72 return 0;
73}
74SPL_TEST(spl_test_load, 0);