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 9struct unit_test_state; 10struct 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 */ 25void 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 30 * @LEGACY_LZMA: "Legacy" uImages, LZMA compressed 31 * @IMX8: i.MX8 Container images 32 * @FIT_INTERNAL: FITs with internal data 33 * @FIT_EXTERNAL: FITs with external data 34 */ 35enum spl_test_image { 36 LEGACY, 37 LEGACY_LZMA, 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 */ 67size_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 */ 81int check_image_info(struct unit_test_state *uts, struct spl_image_info *info1, 82 struct spl_image_info *info2); 83 84/* Some compressed data and it size */ 85extern const char lzma_compressed[]; 86extern const size_t lzma_compressed_size; 87 88/** 89 * typedef write_image_t - Callback for writing an image 90 * @uts: Current unit test state 91 * @img: Image to write 92 * @size: Size of @img 93 * 94 * Write @img to a location which will be read by a &struct spl_image_loader. 95 * 96 * Return: 0 on success or 1 on failure 97 */ 98typedef int write_image_t(struct unit_test_state *its, void *img, size_t size); 99 100/** 101 * do_spl_test_load() - Test loading with an SPL image loader 102 * @uts: Current unit test state 103 * @test_name: Name of the current test 104 * @type: Type of image to try loading 105 * @loader: The loader to test 106 * @write_image: Callback to write the image to the backing storage 107 * 108 * Test @loader, performing the common tasks of setting up the image and 109 * checking it was loaded correctly. The caller must supply a @write_image 110 * callback to write the image to a location which will be read by @loader. 111 * 112 * Return: 0 on success or 1 on failure 113 */ 114int do_spl_test_load(struct unit_test_state *uts, const char *test_name, 115 enum spl_test_image type, struct spl_image_loader *loader, 116 write_image_t write_image); 117 118/** 119 * image_supported() - Determine whether an image type is supported 120 * @type: The image type to check 121 * 122 * Return: %true if supported and %false otherwise 123 */ 124static inline bool image_supported(enum spl_test_image type) 125{ 126 switch (type) { 127 case LEGACY_LZMA: 128 if (!IS_ENABLED(CONFIG_SPL_LZMA)) 129 return false; 130 case LEGACY: 131 return IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT); 132 case IMX8: 133 return IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER); 134 case FIT_INTERNAL: 135 case FIT_EXTERNAL: 136 return IS_ENABLED(CONFIG_SPL_LOAD_FIT) || 137 IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL); 138 } 139 140 return false; 141} 142 143/* Declare an image test (skipped if the image type is unsupported) */ 144#define SPL_IMG_TEST(func, type, flags) \ 145static int func##_##type(struct unit_test_state *uts) \ 146{ \ 147 if (!image_supported(type)) \ 148 return -EAGAIN; \ 149 return func(uts, __func__, type); \ 150} \ 151SPL_TEST(func##_##type, flags) 152 153/* More than a couple blocks, and will not be aligned to anything */ 154#define SPL_TEST_DATA_SIZE 4099 155 156/* Flags necessary for accessing DM devices */ 157#define DM_FLAGS (UT_TESTF_DM | UT_TESTF_SCAN_FDT) 158 159#endif /* TEST_SPL_H */ 160