blob: 49edf152d78171cc18f2ab778b7c665fe3496b14 [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
19static ulong read_fit_image(struct spl_load_info *load, ulong sector,
20 ulong count, void *buf)
21{
22 struct text_ctx *text_ctx = load->priv;
23 off_t offset, ret;
24 ssize_t res;
25
26 offset = sector * load->bl_len;
27 ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
28 if (ret != offset) {
29 printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
30 ret, errno);
31 return 0;
32 }
33
34 res = os_read(text_ctx->fd, buf, count * load->bl_len);
35 if (res == -1) {
36 printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
37 count * load->bl_len, res, errno);
38 return 0;
39 }
40
41 return count;
42}
43
44static int spl_test_load(struct unit_test_state *uts)
45{
46 struct spl_image_info image;
47 struct legacy_img_hdr *header;
48 struct text_ctx text_ctx;
49 struct spl_load_info load;
50 char fname[256];
51 int ret;
52 int fd;
53
54 memset(&load, '\0', sizeof(load));
55 load.bl_len = 512;
56 load.read = read_fit_image;
57
58 ret = sandbox_find_next_phase(fname, sizeof(fname), true);
Sean Anderson20d95f82023-10-14 16:47:58 -040059 if (ret)
60 ut_assertf(0, "%s not found, error %d\n", fname, ret);
Sean Andersondcd91b12023-10-14 16:47:57 -040061 load.filename = fname;
62
63 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
64
65 fd = os_open(fname, OS_O_RDONLY);
66 ut_assert(fd >= 0);
67 ut_asserteq(512, os_read(fd, header, 512));
68 text_ctx.fd = fd;
69
70 load.priv = &text_ctx;
71
72 ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
73
74 return 0;
75}
76SPL_TEST(spl_test_load, 0);