Sean Anderson | dcd91b1 | 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 | |
| 7 | #include <common.h> |
| 8 | #include <image.h> |
| 9 | #include <os.h> |
| 10 | #include <spl.h> |
Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 11 | #include <test/spl.h> |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 12 | #include <test/ut.h> |
| 13 | |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 14 | /* Context used for this test */ |
| 15 | struct text_ctx { |
| 16 | int fd; |
| 17 | }; |
| 18 | |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 19 | static ulong read_fit_image(struct spl_load_info *load, ulong offset, |
| 20 | ulong size, void *buf) |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 21 | { |
| 22 | struct text_ctx *text_ctx = load->priv; |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 23 | off_t ret; |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 24 | ssize_t res; |
| 25 | |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 26 | 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 Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 33 | res = os_read(text_ctx->fd, buf, size); |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 34 | if (res == -1) { |
| 35 | printf("Failed to read %lx bytes, got %ld (errno=%d)\n", |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 36 | size, res, errno); |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
Sean Anderson | 7d8d613 | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 40 | return size; |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static 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 Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 54 | spl_set_bl_len(&load, 512); |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 55 | load.read = read_fit_image; |
| 56 | |
| 57 | ret = sandbox_find_next_phase(fname, sizeof(fname), true); |
Sean Anderson | 20d95f8 | 2023-10-14 16:47:58 -0400 | [diff] [blame] | 58 | if (ret) |
| 59 | ut_assertf(0, "%s not found, error %d\n", fname, ret); |
Sean Anderson | dcd91b1 | 2023-10-14 16:47:57 -0400 | [diff] [blame] | 60 | |
| 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 | } |
| 74 | SPL_TEST(spl_test_load, 0); |