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