1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2023 Sean Anderson <seanga2@gmail.com>
9 struct unit_test_state;
10 struct spl_image_info;
12 /* Declare a new SPL test */
13 #define SPL_TEST(_name, _flags) UNIT_TEST(_name, _flags, spl_test)
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
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.
25 void generate_data(char *data, size_t size, const char *test_name);
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
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
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().
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.
58 * Typically, this function will be called as follows:
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);
65 * Return: The size of the image, or 0 on error
67 size_t create_image(void *dst, enum spl_test_image type,
68 struct spl_image_info *info, size_t *data_offset);
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
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.
79 * Return: 0 on success, or 1 on failure
81 int check_image_info(struct unit_test_state *uts, struct spl_image_info *info1,
82 struct spl_image_info *info2);
84 /* Some compressed data and it size */
85 extern const char lzma_compressed[];
86 extern const size_t lzma_compressed_size;
89 * typedef write_image_t - Callback for writing an image
90 * @uts: Current unit test state
91 * @img: Image to write
94 * Write @img to a location which will be read by a &struct spl_image_loader.
96 * Return: 0 on success or 1 on failure
98 typedef int write_image_t(struct unit_test_state *its, void *img, size_t size);
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
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.
112 * Return: 0 on success or 1 on failure
114 int 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);
119 * image_supported() - Determine whether an image type is supported
120 * @type: The image type to check
122 * Return: %true if supported and %false otherwise
124 static inline bool image_supported(enum spl_test_image type)
128 if (!IS_ENABLED(CONFIG_SPL_LZMA))
131 return IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT);
133 return IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER);
136 return IS_ENABLED(CONFIG_SPL_LOAD_FIT) ||
137 IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL);
143 /* Declare an image test (skipped if the image type is unsupported) */
144 #define SPL_IMG_TEST(func, type, flags) \
145 static int func##_##type(struct unit_test_state *uts) \
147 if (!image_supported(type)) \
149 return func(uts, __func__, type); \
151 SPL_TEST(func##_##type, flags)
153 /* More than a couple blocks, and will not be aligned to anything */
154 #define SPL_TEST_DATA_SIZE 4099
156 /* Flags necessary for accessing DM devices */
157 #define DM_FLAGS (UTF_DM | UTF_SCAN_FDT)
159 #endif /* TEST_SPL_H */