blob: 7d5fb9b07e05faebf4693d66122c471e9f629eef [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
Sean Andersondcd91b12023-10-14 16:47:57 -04007#include <image.h>
8#include <os.h>
9#include <spl.h>
Sean Andersonf65dcc02023-10-14 16:47:59 -040010#include <test/spl.h>
Sean Andersondcd91b12023-10-14 16:47:57 -040011#include <test/ut.h>
12
Sean Andersondcd91b12023-10-14 16:47:57 -040013/* Context used for this test */
14struct text_ctx {
15 int fd;
16};
17
Sean Anderson7d8d6132023-11-08 11:48:40 -050018static ulong read_fit_image(struct spl_load_info *load, ulong offset,
19 ulong size, void *buf)
Sean Andersondcd91b12023-10-14 16:47:57 -040020{
21 struct text_ctx *text_ctx = load->priv;
Sean Anderson7d8d6132023-11-08 11:48:40 -050022 off_t ret;
Sean Andersondcd91b12023-10-14 16:47:57 -040023 ssize_t res;
24
Sean Andersondcd91b12023-10-14 16:47:57 -040025 ret = os_lseek(text_ctx->fd, offset, OS_SEEK_SET);
26 if (ret != offset) {
27 printf("Failed to seek to %zx, got %zx (errno=%d)\n", offset,
28 ret, errno);
29 return 0;
30 }
31
Sean Anderson7d8d6132023-11-08 11:48:40 -050032 res = os_read(text_ctx->fd, buf, size);
Sean Andersondcd91b12023-10-14 16:47:57 -040033 if (res == -1) {
34 printf("Failed to read %lx bytes, got %ld (errno=%d)\n",
Sean Anderson7d8d6132023-11-08 11:48:40 -050035 size, res, errno);
Sean Andersondcd91b12023-10-14 16:47:57 -040036 return 0;
37 }
38
Sean Anderson7d8d6132023-11-08 11:48:40 -050039 return size;
Sean Andersondcd91b12023-10-14 16:47:57 -040040}
41
42static int spl_test_load(struct unit_test_state *uts)
43{
44 struct spl_image_info image;
45 struct legacy_img_hdr *header;
46 struct text_ctx text_ctx;
47 struct spl_load_info load;
48 char fname[256];
49 int ret;
50 int fd;
51
52 memset(&load, '\0', sizeof(load));
Sean Anderson35f15fe2023-11-08 11:48:43 -050053 spl_set_bl_len(&load, 512);
Sean Andersondcd91b12023-10-14 16:47:57 -040054 load.read = read_fit_image;
55
56 ret = sandbox_find_next_phase(fname, sizeof(fname), true);
Sean Anderson20d95f82023-10-14 16:47:58 -040057 if (ret)
58 ut_assertf(0, "%s not found, error %d\n", fname, ret);
Sean Andersondcd91b12023-10-14 16:47:57 -040059
60 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
61
62 fd = os_open(fname, OS_O_RDONLY);
63 ut_assert(fd >= 0);
64 ut_asserteq(512, os_read(fd, header, 512));
65 text_ctx.fd = fd;
66
67 load.priv = &text_ctx;
68
69 ut_assertok(spl_load_simple_fit(&image, &load, 0, header));
70
71 return 0;
72}
73SPL_TEST(spl_test_load, 0);