Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 7 | #include <image.h> |
| 8 | #include <os.h> |
| 9 | #include <spl.h> |
Sean Anderson | b93cc1e | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 10 | #include <test/spl.h> |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 11 | #include <test/ut.h> |
| 12 | |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 13 | /* Context used for this test */ |
| 14 | struct text_ctx { |
| 15 | int fd; |
| 16 | }; |
| 17 | |
Sean Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 18 | static ulong read_fit_image(struct spl_load_info *load, ulong offset, |
| 19 | ulong size, void *buf) |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 20 | { |
| 21 | struct text_ctx *text_ctx = load->priv; |
Sean Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 22 | off_t ret; |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 23 | ssize_t res; |
| 24 | |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 25 | 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 Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 32 | res = os_read(text_ctx->fd, buf, size); |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 33 | if (res == -1) { |
| 34 | printf("Failed to read %lx bytes, got %ld (errno=%d)\n", |
Sean Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 35 | size, res, errno); |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
Sean Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 39 | return size; |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static 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 Anderson | 5271e35 | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 53 | spl_set_bl_len(&load, 512); |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 54 | load.read = read_fit_image; |
| 55 | |
| 56 | ret = sandbox_find_next_phase(fname, sizeof(fname), true); |
Sean Anderson | b5bf83b | 2023-10-14 16:47:58 -0400 | [diff] [blame] | 57 | if (ret) |
| 58 | ut_assertf(0, "%s not found, error %d\n", fname, ret); |
Sean Anderson | c56468a6 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 59 | |
| 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 | } |
| 73 | SPL_TEST(spl_test_load, 0); |