blob: 4c6b789e478b79b16f7d851b84a07b8e153539b4 [file] [log] [blame]
Sean Andersonb93cc1e2023-10-14 16:47:59 -04001/* 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 * @IMX8: i.MX8 Container images
31 * @FIT_INTERNAL: FITs with internal data
32 * @FIT_EXTERNAL: FITs with external data
33 */
34enum spl_test_image {
35 LEGACY,
36 IMX8,
37 FIT_INTERNAL,
38 FIT_EXTERNAL,
39};
40
41/**
42 * create_image() - Create an image for testing
43 * @dst: The location to create the image at
44 * @type: The type of image to create
45 * @info: Image parameters
46 * @data_offset: Offset of payload data within the image
47 *
48 * Create a new image at @dst. @dst must be initialized to all zeros. @info
49 * should already have name and size filled in. All other parameters will be
50 * filled in by this function. @info can later be passed to check_image_info().
51 *
52 * If @dst is %NULL, then no data is written. Otherwise, @dst must be
53 * initialized to zeros, except payload data which must already be present at
54 * @data_offset. @data_offset may be %NULL if unnecessary.
55 *
56 * Typically, this function will be called as follows:
57 *
58 * size = create_image(NULL, type, &info, &off);
59 * img = calloc(size, 1);
60 * generate_data(img + off, ...);
61 * create_image(img, type, &info, NULL);
62 *
63 * Return: The size of the image, or 0 on error
64 */
65size_t create_image(void *dst, enum spl_test_image type,
66 struct spl_image_info *info, size_t *data_offset);
67
68/**
69 * check_image_info() - Check image info after loading
70 * @uts: Current unit test state
71 * @info1: The base, known good info
72 * @info2: The info to check
73 *
74 * Check @info2 against @info1. This function is typically called after calling
75 * a function to load/parse an image. Image data is not checked.
76 *
77 * Return: 0 on success, or 1 on failure
78 */
79int check_image_info(struct unit_test_state *uts, struct spl_image_info *info1,
80 struct spl_image_info *info2);
81
82/**
83 * image_supported() - Determine whether an image type is supported
84 * @type: The image type to check
85 *
86 * Return: %true if supported and %false otherwise
87 */
88static inline bool image_supported(enum spl_test_image type)
89{
90 switch (type) {
91 case LEGACY:
92 return IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT);
93 case IMX8:
94 return IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER);
95 case FIT_INTERNAL:
96 case FIT_EXTERNAL:
97 return IS_ENABLED(CONFIG_SPL_LOAD_FIT) ||
98 IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL);
99 }
100
101 return false;
102}
103
104/* Declare an image test (skipped if the image type is unsupported) */
105#define SPL_IMG_TEST(func, type, flags) \
106static int func##_##type(struct unit_test_state *uts) \
107{ \
108 if (!image_supported(type)) \
109 return -EAGAIN; \
110 return func(uts, __func__, type); \
111} \
112SPL_TEST(func##_##type, flags)
113
114/* More than a couple blocks, and will not be aligned to anything */
115#define SPL_TEST_DATA_SIZE 4099
116
117#endif /* TEST_SPL_H */