sandbox: test: Tidy up spl_test_load() calls
[platform/kernel/u-boot.git] / test / image / spl_load.c
1 // 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 <mapmem.h>
10 #include <os.h>
11 #include <spl.h>
12 #include <test/ut.h>
13
14 /* Declare a new SPL test */
15 #define SPL_TEST(_name, _flags)         UNIT_TEST(_name, _flags, spl_test)
16
17 /* Context used for this test */
18 struct text_ctx {
19         int fd;
20 };
21
22 static ulong read_fit_image(struct spl_load_info *load, ulong sector,
23                             ulong count, void *buf)
24 {
25         struct text_ctx *text_ctx = load->priv;
26         off_t offset, ret;
27         ssize_t res;
28
29         offset = sector * load->bl_len;
30         ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
31         if (ret != offset) {
32                 printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
33                        ret, errno);
34                 return 0;
35         }
36
37         res = os_read(text_ctx->fd, buf, count * load->bl_len);
38         if (res == -1) {
39                 printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
40                        count * load->bl_len, res, errno);
41                 return 0;
42         }
43
44         return count;
45 }
46
47 int board_fit_config_name_match(const char *name)
48 {
49         return 0;
50 }
51
52 struct image_header *spl_get_load_buffer(ssize_t offset, size_t size)
53 {
54         return map_sysmem(0x100000, 0);
55 }
56
57 static int spl_test_load(struct unit_test_state *uts)
58 {
59         struct spl_image_info image;
60         struct image_header *header;
61         struct text_ctx text_ctx;
62         struct spl_load_info load;
63         char fname[256];
64         int ret;
65         int fd;
66
67         memset(&load, '\0', sizeof(load));
68         load.bl_len = 512;
69         load.read = read_fit_image;
70
71         ret = sandbox_find_next_phase(fname, sizeof(fname), true);
72         if (ret) {
73                 printf("(%s not found, error %d)\n", fname, ret);
74                 return ret;
75         }
76         load.filename = fname;
77
78         header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
79
80         fd = os_open(fname, OS_O_RDONLY);
81         ut_assert(fd >= 0);
82         ut_asserteq(512, os_read(fd, header, 512));
83         text_ctx.fd = fd;
84
85         load.priv = &text_ctx;
86
87         ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
88
89         return 0;
90 }
91 SPL_TEST(spl_test_load, 0);