Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com> |
| 4 | */ |
| 5 | |
| 6 | #ifndef TEST_SPL_H |
| 7 | #define TEST_SPL_H |
| 8 | |
| 9 | struct unit_test_state; |
| 10 | struct spl_image_info; |
| 11 | |
| 12 | /* Declare a new SPL test */ |
| 13 | #define SPL_TEST(_name, _flags) UNIT_TEST(_name, _flags, spl_test) |
| 14 | |
| 15 | /** |
| 16 | * generate_data() - Generate some test payload data |
| 17 | * @data: The location to fill |
| 18 | * @size: The size of @data |
| 19 | * @test_name: The seed for the data |
| 20 | * |
| 21 | * Fill @data with data. The upper nibbles will be an incrementing counter |
| 22 | * (0x00, 0x10, 0x20...) to make the data identifiable in a hex dump. The lower |
| 23 | * nibbles are random bits seeded with @test_name. |
| 24 | */ |
| 25 | void generate_data(char *data, size_t size, const char *test_name); |
| 26 | |
| 27 | /** |
| 28 | * enum spl_test_image - Image types for testing |
| 29 | * @LEGACY: "Legacy" uImages |
Sean Anderson | 430e4aa | 2023-10-14 16:48:04 -0400 | [diff] [blame] | 30 | * @LEGACY_LZMA: "Legacy" uImages, LZMA compressed |
Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 31 | * @IMX8: i.MX8 Container images |
| 32 | * @FIT_INTERNAL: FITs with internal data |
| 33 | * @FIT_EXTERNAL: FITs with external data |
| 34 | */ |
| 35 | enum spl_test_image { |
| 36 | LEGACY, |
Sean Anderson | 430e4aa | 2023-10-14 16:48:04 -0400 | [diff] [blame] | 37 | LEGACY_LZMA, |
Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 38 | IMX8, |
| 39 | FIT_INTERNAL, |
| 40 | FIT_EXTERNAL, |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * create_image() - Create an image for testing |
| 45 | * @dst: The location to create the image at |
| 46 | * @type: The type of image to create |
| 47 | * @info: Image parameters |
| 48 | * @data_offset: Offset of payload data within the image |
| 49 | * |
| 50 | * Create a new image at @dst. @dst must be initialized to all zeros. @info |
| 51 | * should already have name and size filled in. All other parameters will be |
| 52 | * filled in by this function. @info can later be passed to check_image_info(). |
| 53 | * |
| 54 | * If @dst is %NULL, then no data is written. Otherwise, @dst must be |
| 55 | * initialized to zeros, except payload data which must already be present at |
| 56 | * @data_offset. @data_offset may be %NULL if unnecessary. |
| 57 | * |
| 58 | * Typically, this function will be called as follows: |
| 59 | * |
| 60 | * size = create_image(NULL, type, &info, &off); |
| 61 | * img = calloc(size, 1); |
| 62 | * generate_data(img + off, ...); |
| 63 | * create_image(img, type, &info, NULL); |
| 64 | * |
| 65 | * Return: The size of the image, or 0 on error |
| 66 | */ |
| 67 | size_t create_image(void *dst, enum spl_test_image type, |
| 68 | struct spl_image_info *info, size_t *data_offset); |
| 69 | |
| 70 | /** |
| 71 | * check_image_info() - Check image info after loading |
| 72 | * @uts: Current unit test state |
| 73 | * @info1: The base, known good info |
| 74 | * @info2: The info to check |
| 75 | * |
| 76 | * Check @info2 against @info1. This function is typically called after calling |
| 77 | * a function to load/parse an image. Image data is not checked. |
| 78 | * |
| 79 | * Return: 0 on success, or 1 on failure |
| 80 | */ |
| 81 | int check_image_info(struct unit_test_state *uts, struct spl_image_info *info1, |
| 82 | struct spl_image_info *info2); |
| 83 | |
| 84 | /** |
Sean Anderson | 576295d | 2023-10-14 16:48:02 -0400 | [diff] [blame] | 85 | * typedef write_image_t - Callback for writing an image |
| 86 | * @uts: Current unit test state |
| 87 | * @img: Image to write |
| 88 | * @size: Size of @img |
| 89 | * |
| 90 | * Write @img to a location which will be read by a &struct spl_image_loader. |
| 91 | * |
| 92 | * Return: 0 on success or 1 on failure |
| 93 | */ |
| 94 | typedef int write_image_t(struct unit_test_state *its, void *img, size_t size); |
| 95 | |
| 96 | /** |
| 97 | * do_spl_test_load() - Test loading with an SPL image loader |
| 98 | * @uts: Current unit test state |
| 99 | * @test_name: Name of the current test |
| 100 | * @type: Type of image to try loading |
| 101 | * @loader: The loader to test |
| 102 | * @write_image: Callback to write the image to the backing storage |
| 103 | * |
| 104 | * Test @loader, performing the common tasks of setting up the image and |
| 105 | * checking it was loaded correctly. The caller must supply a @write_image |
| 106 | * callback to write the image to a location which will be read by @loader. |
| 107 | * |
| 108 | * Return: 0 on success or 1 on failure |
| 109 | */ |
| 110 | int do_spl_test_load(struct unit_test_state *uts, const char *test_name, |
| 111 | enum spl_test_image type, struct spl_image_loader *loader, |
| 112 | write_image_t write_image); |
| 113 | |
| 114 | /** |
Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 115 | * image_supported() - Determine whether an image type is supported |
| 116 | * @type: The image type to check |
| 117 | * |
| 118 | * Return: %true if supported and %false otherwise |
| 119 | */ |
| 120 | static inline bool image_supported(enum spl_test_image type) |
| 121 | { |
| 122 | switch (type) { |
Sean Anderson | 430e4aa | 2023-10-14 16:48:04 -0400 | [diff] [blame] | 123 | case LEGACY_LZMA: |
| 124 | if (!IS_ENABLED(CONFIG_SPL_LZMA)) |
| 125 | return false; |
Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 126 | case LEGACY: |
| 127 | return IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT); |
| 128 | case IMX8: |
| 129 | return IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER); |
| 130 | case FIT_INTERNAL: |
| 131 | case FIT_EXTERNAL: |
| 132 | return IS_ENABLED(CONFIG_SPL_LOAD_FIT) || |
| 133 | IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL); |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /* Declare an image test (skipped if the image type is unsupported) */ |
| 140 | #define SPL_IMG_TEST(func, type, flags) \ |
| 141 | static int func##_##type(struct unit_test_state *uts) \ |
| 142 | { \ |
| 143 | if (!image_supported(type)) \ |
| 144 | return -EAGAIN; \ |
| 145 | return func(uts, __func__, type); \ |
| 146 | } \ |
| 147 | SPL_TEST(func##_##type, flags) |
| 148 | |
| 149 | /* More than a couple blocks, and will not be aligned to anything */ |
| 150 | #define SPL_TEST_DATA_SIZE 4099 |
| 151 | |
Sean Anderson | 2d3ffa3 | 2023-10-14 16:48:00 -0400 | [diff] [blame] | 152 | /* Flags necessary for accessing DM devices */ |
| 153 | #define DM_FLAGS (UT_TESTF_DM | UT_TESTF_SCAN_FDT) |
| 154 | |
Sean Anderson | f65dcc0 | 2023-10-14 16:47:59 -0400 | [diff] [blame] | 155 | #endif /* TEST_SPL_H */ |