blob: e12c5a95e45d5728684570e38151f9440682b404 [file] [log] [blame]
Simon Glass5722fb22021-03-07 17:34:47 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glassbe277ee2024-10-28 13:47:53 +01007#define LOG_CATEGORY LOGC_TEST
8
Simon Glass76c62692022-10-29 19:47:08 -06009#include <blk.h>
Simon Glass5722fb22021-03-07 17:34:47 -070010#include <console.h>
Stefan Roese5be8f372022-09-02 13:57:54 +020011#include <cyclic.h>
Simon Glassd18f7392021-03-07 17:34:50 -070012#include <dm.h>
Simon Glassa128b1b2022-03-04 08:43:01 -070013#include <event.h>
Simon Glassb490f5f2023-01-17 10:47:28 -070014#include <net.h>
Simon Glassb9958072022-09-06 20:27:11 -060015#include <of_live.h>
Simon Glass3ba76752022-09-06 20:27:05 -060016#include <os.h>
Simon Glass1880b8a2024-09-01 16:26:20 -060017#include <usb.h>
Simon Glassba8457b2022-09-06 20:27:19 -060018#include <dm/ofnode.h>
Simon Glassd18f7392021-03-07 17:34:50 -070019#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070020#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070021#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070022#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070023#include <test/ut.h>
Simon Glass3ba76752022-09-06 20:27:05 -060024#include <u-boot/crc.h>
Simon Glass5722fb22021-03-07 17:34:47 -070025
Simon Glass0f8f6772021-03-07 17:34:49 -070026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass3ba76752022-09-06 20:27:05 -060028/**
29 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
30 *
31 * This affects what happens with the device tree before and after a test
32 *
33 * @FDTCHK_NONE: Do nothing
34 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
35 * compare it afterwards to detect any changes
36 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
37 */
38enum fdtchk_t {
39 FDTCHK_NONE,
40 FDTCHK_CHECKSUM,
41 FDTCHK_COPY,
42};
43
44/**
45 * fdt_action() - get the required action for the FDT
46 *
47 * @return the action that should be taken for this build
48 */
49static enum fdtchk_t fdt_action(void)
50{
Simon Glass3ba76752022-09-06 20:27:05 -060051 /* For sandbox SPL builds, do nothing */
Simon Glass0e84d962024-09-29 19:49:50 -060052 if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_XPL_BUILD))
Simon Glass3ba76752022-09-06 20:27:05 -060053 return FDTCHK_NONE;
54
Simon Glass234f05b2023-02-22 09:34:12 -070055 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
56 if (IS_ENABLED(CONFIG_SANDBOX))
57 return FDTCHK_COPY;
58
Simon Glass3ba76752022-09-06 20:27:05 -060059 /* For all other boards, do a checksum */
60 return FDTCHK_CHECKSUM;
61}
62
Simon Glass4066d8d2021-03-07 17:35:04 -070063/* This is valid when a test is running, NULL otherwise */
64static struct unit_test_state *cur_test_state;
65
Simon Glasse6411372025-01-20 14:25:24 -070066struct unit_test_state *ut_get_state(void)
Simon Glass4066d8d2021-03-07 17:35:04 -070067{
68 return cur_test_state;
69}
70
Simon Glasse6411372025-01-20 14:25:24 -070071void ut_set_state(struct unit_test_state *uts)
Simon Glass4066d8d2021-03-07 17:35:04 -070072{
73 cur_test_state = uts;
74}
75
Simon Glass2d989e82025-01-20 14:25:25 -070076void ut_init_state(struct unit_test_state *uts)
77{
78 memset(uts, '\0', sizeof(*uts));
79}
80
81void ut_uninit_state(struct unit_test_state *uts)
82{
83 if (IS_ENABLED(CONFIG_SANDBOX)) {
84 os_free(uts->fdt_copy);
85 os_free(uts->other_fdt);
86 }
87}
88
Simon Glass2dba4762021-03-07 17:34:58 -070089/**
90 * dm_test_pre_run() - Get ready to run a driver model test
91 *
92 * This clears out the driver model data structures. For sandbox it resets the
93 * state structure
94 *
95 * @uts: Test state
96 */
97static int dm_test_pre_run(struct unit_test_state *uts)
98{
99 bool of_live = uts->of_live;
100
Simon Glass60f08992022-09-06 20:27:06 -0600101 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
102 printf("Cannot run live tree test as device tree changed\n");
103 return -EFAULT;
104 }
Simon Glass2dba4762021-03-07 17:34:58 -0700105 uts->root = NULL;
106 uts->testdev = NULL;
107 uts->force_fail_alloc = false;
108 uts->skip_post_probe = false;
Simon Glass3ba76752022-09-06 20:27:05 -0600109 if (fdt_action() == FDTCHK_CHECKSUM)
110 uts->fdt_chksum = crc8(0, gd->fdt_blob,
111 fdt_totalsize(gd->fdt_blob));
Simon Glass2dba4762021-03-07 17:34:58 -0700112 gd->dm_root = NULL;
Simon Glass1a3e39b2022-09-06 20:27:00 -0600113 malloc_disable_testing();
Simon Glassf7005042021-07-05 16:32:43 -0600114 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glass2dba4762021-03-07 17:34:58 -0700115 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glass45b807d2021-03-25 10:44:33 +1300116 arch_reset_for_test();
Simon Glass2dba4762021-03-07 17:34:58 -0700117
118 /* Determine whether to make the live tree available */
119 gd_set_of_root(of_live ? uts->of_root : NULL);
Simon Glassba8457b2022-09-06 20:27:19 -0600120 oftree_reset();
Simon Glass2dba4762021-03-07 17:34:58 -0700121 ut_assertok(dm_init(of_live));
122 uts->root = dm_root();
123
124 return 0;
125}
126
Simon Glass5b7e55b2021-03-07 17:34:59 -0700127static int dm_test_post_run(struct unit_test_state *uts)
128{
129 int id;
130
Simon Glass3ba76752022-09-06 20:27:05 -0600131 if (gd->fdt_blob) {
132 switch (fdt_action()) {
133 case FDTCHK_COPY:
134 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
135 break;
136 case FDTCHK_CHECKSUM: {
137 uint chksum;
138
139 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
Simon Glass60f08992022-09-06 20:27:06 -0600140 if (chksum != uts->fdt_chksum) {
141 /*
142 * We cannot run any more tests that need the
143 * live tree, since its strings point into the
144 * flat tree, which has changed. This likely
145 * means that at least some of the pointers from
146 * the live tree point to different things
147 */
Simon Glass3ba76752022-09-06 20:27:05 -0600148 printf("Device tree changed: cannot run live tree tests\n");
Simon Glass60f08992022-09-06 20:27:06 -0600149 gd->flags |= GD_FLG_FDT_CHANGED;
150 }
Simon Glass3ba76752022-09-06 20:27:05 -0600151 break;
152 }
153 case FDTCHK_NONE:
154 break;
155 }
156 }
157
Simon Glass96113c12021-03-15 17:25:21 +1300158 /*
159 * With of-platdata-inst the uclasses are created at build time. If we
160 * destroy them we cannot get them back since uclass_add() is not
161 * supported. So skip this.
162 */
163 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
164 for (id = 0; id < UCLASS_COUNT; id++) {
165 struct uclass *uc;
Simon Glass5b7e55b2021-03-07 17:34:59 -0700166
Simon Glass96113c12021-03-15 17:25:21 +1300167 /*
168 * If the uclass doesn't exist we don't want to create
169 * it. So check that here before we call
170 * uclass_find_device().
171 */
172 uc = uclass_find(id);
173 if (!uc)
174 continue;
175 ut_assertok(uclass_destroy(uc));
176 }
Simon Glass5b7e55b2021-03-07 17:34:59 -0700177 }
178
179 return 0;
180}
181
Simon Glass242357c2021-03-07 17:34:51 -0700182/* Ensure all the test devices are probed */
183static int do_autoprobe(struct unit_test_state *uts)
184{
Michal Suchanek44322b52022-10-12 21:57:51 +0200185 return uclass_probe_all(UCLASS_TEST);
Simon Glass242357c2021-03-07 17:34:51 -0700186}
187
Simon Glass0d32ec22021-03-07 17:35:03 -0700188/*
189 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
190 *
191 * This skips long/slow tests where there is not much value in running a flat
192 * DT test in addition to a live DT test.
193 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100194 * Return: true to run the given test on the flat device tree
Simon Glass0d32ec22021-03-07 17:35:03 -0700195 */
196static bool ut_test_run_on_flattree(struct unit_test *test)
197{
198 const char *fname = strrchr(test->file, '/') + 1;
199
Simon Glass1a92f832024-08-22 07:57:48 -0600200 if (!(test->flags & UTF_DM))
Simon Glass0d32ec22021-03-07 17:35:03 -0700201 return false;
202
203 return !strstr(fname, "video") || strstr(test->name, "video_base");
204}
205
Simon Glass436687e2021-03-07 17:35:01 -0700206/**
Simon Glassbb2b1732021-03-07 17:35:05 -0700207 * test_matches() - Check if a test should be run
208 *
209 * This checks if the a test should be run. In the normal case of running all
210 * tests, @select_name is NULL.
211 *
212 * @prefix: String prefix for the tests. Any tests that have this prefix will be
213 * printed without the prefix, so that it is easier to see the unique part
Simon Glass1ef74ab2021-03-07 17:35:12 -0700214 * of the test name. If NULL, any suite name (xxx_test) is considered to be
215 * a prefix.
Simon Glassbb2b1732021-03-07 17:35:05 -0700216 * @test_name: Name of current test
217 * @select_name: Name of test to run (or NULL for all)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100218 * Return: true to run this test, false to skip it
Simon Glassbb2b1732021-03-07 17:35:05 -0700219 */
220static bool test_matches(const char *prefix, const char *test_name,
221 const char *select_name)
222{
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200223 size_t len;
224
Simon Glassbb2b1732021-03-07 17:35:05 -0700225 if (!select_name)
226 return true;
227
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200228 /* Allow glob expansion in the test name */
229 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
230 if (len-- == 1)
231 return true;
232
233 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700234 return true;
235
Andy Shevchenko00d42902021-02-11 16:40:11 +0200236 if (prefix) {
237 /* All tests have this prefix */
238 if (!strncmp(test_name, prefix, strlen(prefix)))
239 test_name += strlen(prefix);
240 } else {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700241 const char *p = strstr(test_name, "_test_");
242
243 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
244 if (p)
Andy Shevchenko00d42902021-02-11 16:40:11 +0200245 test_name = p + strlen("_test_");
Simon Glass1ef74ab2021-03-07 17:35:12 -0700246 }
Simon Glassbb2b1732021-03-07 17:35:05 -0700247
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200248 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700249 return true;
250
251 return false;
252}
253
Simon Glass53d1b192021-03-07 17:35:08 -0700254/**
Simon Glass1899e132021-03-07 17:35:07 -0700255 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
256 *
257 * @tests: List of tests to run
Simon Glass798b5c52024-10-19 09:21:56 -0600258 * @count: Number of tests to run
259 * @prefix: String prefix for the tests. Any tests that have this prefix will be
260 * printed without the prefix, so that it is easier to see the unique part
261 * of the test name. If NULL, no prefix processing is done
262 * @select_name: Name of a single test being run (from the list provided). If
263 * NULL all tests are being run
Simon Glass1a92f832024-08-22 07:57:48 -0600264 * Return: true if any of the tests have the UTF_DM flag
Simon Glass1899e132021-03-07 17:35:07 -0700265 */
Simon Glass798b5c52024-10-19 09:21:56 -0600266static bool ut_list_has_dm_tests(struct unit_test *tests, int count,
267 const char *prefix, const char *select_name)
Simon Glass1899e132021-03-07 17:35:07 -0700268{
269 struct unit_test *test;
270
271 for (test = tests; test < tests + count; test++) {
Simon Glass798b5c52024-10-19 09:21:56 -0600272 if (test_matches(prefix, test->name, select_name) &&
273 (test->flags & UTF_DM))
Simon Glass1899e132021-03-07 17:35:07 -0700274 return true;
275 }
276
277 return false;
278}
279
Simon Glassbb2b1732021-03-07 17:35:05 -0700280/**
Simon Glass53d1b192021-03-07 17:35:08 -0700281 * dm_test_restore() Put things back to normal so sandbox works as expected
282 *
283 * @of_root: Value to set for of_root
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100284 * Return: 0 if OK, -ve on error
Simon Glass53d1b192021-03-07 17:35:08 -0700285 */
286static int dm_test_restore(struct device_node *of_root)
287{
288 int ret;
289
290 gd_set_of_root(of_root);
291 gd->dm_root = NULL;
292 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
293 if (ret)
294 return ret;
295 dm_scan_plat(false);
296 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
AKASHI Takahiro16f51472023-06-08 09:55:59 +0900297 dm_extended_scan(false);
Simon Glass53d1b192021-03-07 17:35:08 -0700298
299 return 0;
300}
301
302/**
Simon Glass436687e2021-03-07 17:35:01 -0700303 * test_pre_run() - Handle any preparation needed to run a test
304 *
305 * @uts: Test state
306 * @test: Test to prepare for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100307 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
Simon Glass436687e2021-03-07 17:35:01 -0700308 * available, other -ve on error (meaning that testing cannot likely
309 * continue)
310 */
311static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700312{
Simon Glassa128b1b2022-03-04 08:43:01 -0700313 ut_assertok(event_init());
314
Simon Glass1880b8a2024-09-01 16:26:20 -0600315 /*
316 * Remove any USB keyboard, so that we can add and remove USB devices
317 * in tests.
318 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600319 * For UTF_DM tests, the old driver model state is saved and
Simon Glass1880b8a2024-09-01 16:26:20 -0600320 * restored across each test. Within in each test there is therefore a
321 * new driver model state, which means that any USB keyboard device in
322 * stdio points to the old state.
323 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600324 * This is fine in most cases. But if a non-UTF_DM test starts up
Simon Glass1880b8a2024-09-01 16:26:20 -0600325 * USB (thus creating a stdio record pointing to the USB keyboard
326 * device) then when the test finishes, the new driver model state is
327 * freed, meaning that there is now a stale pointer in stdio.
328 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600329 * This means that any future UTF_DM test which uses stdin will
Simon Glass1880b8a2024-09-01 16:26:20 -0600330 * cause the console system to call tstc() on the stale device pointer,
331 * causing a crash.
332 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600333 * We don't want to fix this by enabling UTF_DM for all tests as
Simon Glass1880b8a2024-09-01 16:26:20 -0600334 * this causes other problems. For example, bootflow_efi relies on
335 * U-Boot going through a proper init - without that we don't have the
336 * TCG measurement working and get an error
337 * 'tcg2 measurement fails(0x8000000000000007)'. Once we tidy up how EFI
338 * runs tests (e.g. get rid of all the restarting of U-Boot) we could
Simon Glassb0fd7a22024-10-14 14:17:53 -0600339 * potentially make the bootstd tests set UTF_DM, but other tests
Simon Glass1880b8a2024-09-01 16:26:20 -0600340 * might do the same thing.
341 *
342 * We could add a test flag to declare that USB is being used, but that
343 * seems unnecessary, at least for now. We could detect USB being used
344 * in a test, but there is no obvious drawback to clearing out stale
345 * pointers always.
346 *
347 * So just remove any USB keyboards from the console tables. This allows
Simon Glassb0fd7a22024-10-14 14:17:53 -0600348 * UTF_DM and non-UTF_DM tests to coexist happily.
Simon Glass1880b8a2024-09-01 16:26:20 -0600349 */
350 usb_kbd_remove_for_test();
351
Simon Glass1a92f832024-08-22 07:57:48 -0600352 if (test->flags & UTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700353 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700354
Simon Glass59cad962021-03-07 17:34:55 -0700355 ut_set_skip_delays(uts, false);
356
Simon Glass86a5bd02021-03-07 17:34:53 -0700357 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700358
Simon Glass1a92f832024-08-22 07:57:48 -0600359 if (test->flags & UTF_SCAN_PDATA)
Simon Glass177e0fd2021-03-07 17:34:52 -0700360 ut_assertok(dm_scan_plat(false));
361
Simon Glass1a92f832024-08-22 07:57:48 -0600362 if (test->flags & UTF_PROBE_TEST)
Simon Glass242357c2021-03-07 17:34:51 -0700363 ut_assertok(do_autoprobe(uts));
364
Sean Anderson7bf2f8c2023-10-14 16:47:46 -0400365 if (CONFIG_IS_ENABLED(OF_REAL) &&
Simon Glass1a92f832024-08-22 07:57:48 -0600366 (test->flags & UTF_SCAN_FDT)) {
Simon Glassb490f5f2023-01-17 10:47:28 -0700367 /*
368 * only set this if we know the ethernet uclass will be created
369 */
Simon Glass1a92f832024-08-22 07:57:48 -0600370 eth_set_enable_bootdevs(test->flags & UTF_ETH_BOOTDEV);
371 test_sf_set_enable_bootdevs(test->flags & UTF_SF_BOOTDEV);
Simon Glassd18f7392021-03-07 17:34:50 -0700372 ut_assertok(dm_extended_scan(false));
Simon Glassb490f5f2023-01-17 10:47:28 -0700373 }
Simon Glassd18f7392021-03-07 17:34:50 -0700374
Simon Glassb9ff6482023-01-17 10:47:23 -0700375 /*
376 * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c
377 * checks for the existence of bootstd
378 */
Simon Glass1a92f832024-08-22 07:57:48 -0600379 if (test->flags & UTF_SCAN_PDATA)
Simon Glassb9ff6482023-01-17 10:47:23 -0700380 ut_assertok(dm_scan_other(false));
381
Simon Glass1a92f832024-08-22 07:57:48 -0600382 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UTF_OTHER_FDT)) {
Simon Glassb9958072022-09-06 20:27:11 -0600383 /* make sure the other FDT is available */
384 ut_assertok(test_load_other_fdt(uts));
385
386 /*
387 * create a new live tree with it for every test, in case a
388 * test modifies the tree
389 */
390 if (of_live_active()) {
391 ut_assertok(unflatten_device_tree(uts->other_fdt,
392 &uts->of_other));
393 }
394 }
395
Simon Glass11fcfa32024-08-22 07:57:50 -0600396 if (test->flags & UTF_CONSOLE) {
Simon Glassd93dc7d2021-03-07 17:34:48 -0700397 int ret = console_record_reset_enable();
398
399 if (ret) {
400 printf("Skipping: Console recording disabled\n");
401 return -EAGAIN;
402 }
403 }
Simon Glassbe277ee2024-10-28 13:47:53 +0100404 if (test->flags & UFT_BLOBLIST) {
405 log_debug("save bloblist %p\n", gd_bloblist());
406 uts->old_bloblist = gd_bloblist();
407 gd_set_bloblist(NULL);
408 }
409
Simon Glass2b566b92021-03-07 17:34:54 -0700410 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700411
412 return 0;
413}
414
Simon Glass436687e2021-03-07 17:35:01 -0700415/**
416 * test_post_run() - Handle cleaning up after a test
417 *
418 * @uts: Test state
419 * @test: Test to clean up after
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100420 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
Simon Glass436687e2021-03-07 17:35:01 -0700421 */
422static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700423{
Simon Glass2b566b92021-03-07 17:34:54 -0700424 ut_unsilence_console(uts);
Simon Glass1a92f832024-08-22 07:57:48 -0600425 if (test->flags & UTF_DM)
Simon Glass5b7e55b2021-03-07 17:34:59 -0700426 ut_assertok(dm_test_post_run(uts));
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200427 ut_assertok(cyclic_unregister_all());
Simon Glassa128b1b2022-03-04 08:43:01 -0700428 ut_assertok(event_uninit());
Simon Glass0f8f6772021-03-07 17:34:49 -0700429
Simon Glassb9958072022-09-06 20:27:11 -0600430 free(uts->of_other);
431 uts->of_other = NULL;
432
Simon Glassbe277ee2024-10-28 13:47:53 +0100433 if (test->flags & UFT_BLOBLIST) {
434 gd_set_bloblist(uts->old_bloblist);
435 log_debug("restore bloblist %p\n", gd_bloblist());
436 }
437
Simon Glass76c62692022-10-29 19:47:08 -0600438 blkcache_free();
439
Simon Glassd93dc7d2021-03-07 17:34:48 -0700440 return 0;
441}
442
Simon Glass0d32ec22021-03-07 17:35:03 -0700443/**
Simon Glass816fe6c2022-10-20 18:22:48 -0600444 * skip_test() - Handle skipping a test
445 *
446 * @uts: Test state to update
447 * @return -EAGAIN (always)
448 */
449static int skip_test(struct unit_test_state *uts)
450{
451 uts->skip_count++;
452
453 return -EAGAIN;
454}
455
456/**
Simon Glass0d32ec22021-03-07 17:35:03 -0700457 * ut_run_test() - Run a single test
458 *
459 * This runs the test, handling any preparation and clean-up needed. It prints
460 * the name of each test before running it.
461 *
462 * @uts: Test state to update. The caller should ensure that this is zeroed for
463 * the first call to this function. On exit, @uts->fail_count is
464 * incremented by the number of failures (0, one hopes)
465 * @test_name: Test to run
466 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100467 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glass0d32ec22021-03-07 17:35:03 -0700468 * any failed
469 */
470static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
471 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700472{
Simon Glass436687e2021-03-07 17:35:01 -0700473 const char *fname = strrchr(test->file, '/') + 1;
474 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700475 int ret;
476
Simon Glass1a92f832024-08-22 07:57:48 -0600477 if ((test->flags & UTF_DM) && !uts->of_live)
Simon Glass436687e2021-03-07 17:35:01 -0700478 note = " (flat tree)";
479 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700480
Simon Glass4066d8d2021-03-07 17:35:04 -0700481 /* Allow access to test state from drivers */
Simon Glasse6411372025-01-20 14:25:24 -0700482 ut_set_state(uts);
Simon Glass4066d8d2021-03-07 17:35:04 -0700483
Simon Glass5517edb2021-03-07 17:35:00 -0700484 ret = test_pre_run(uts, test);
485 if (ret == -EAGAIN)
Simon Glass816fe6c2022-10-20 18:22:48 -0600486 return skip_test(uts);
Simon Glass5517edb2021-03-07 17:35:00 -0700487 if (ret)
488 return ret;
489
Simon Glass816fe6c2022-10-20 18:22:48 -0600490 ret = test->func(uts);
491 if (ret == -EAGAIN)
492 skip_test(uts);
Simon Glass5517edb2021-03-07 17:35:00 -0700493
494 ret = test_post_run(uts, test);
495 if (ret)
496 return ret;
497
Simon Glasse6411372025-01-20 14:25:24 -0700498 ut_set_state(NULL);
Simon Glass4066d8d2021-03-07 17:35:04 -0700499
Simon Glass5517edb2021-03-07 17:35:00 -0700500 return 0;
501}
502
Simon Glassbb2b1732021-03-07 17:35:05 -0700503/**
504 * ut_run_test_live_flat() - Run a test with both live and flat tree
505 *
506 * This calls ut_run_test() with livetree enabled, which is the standard setup
507 * for runnig tests. Then, for driver model test, it calls it again with
508 * livetree disabled. This allows checking of flattree being used when OF_LIVE
509 * is enabled, as is the case in U-Boot proper before relocation, as well as in
510 * SPL.
511 *
512 * @uts: Test state to update. The caller should ensure that this is zeroed for
513 * the first call to this function. On exit, @uts->fail_count is
514 * incremented by the number of failures (0, one hopes)
515 * @test: Test to run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100516 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassbb2b1732021-03-07 17:35:05 -0700517 * any failed
518 */
519static int ut_run_test_live_flat(struct unit_test_state *uts,
Simon Glass0f7eb632022-10-29 19:47:09 -0600520 struct unit_test *test)
Simon Glass0d32ec22021-03-07 17:35:03 -0700521{
Simon Glass94b35832024-10-09 18:28:59 -0600522 int runs, ret;
Simon Glass0d32ec22021-03-07 17:35:03 -0700523
Simon Glass1a92f832024-08-22 07:57:48 -0600524 if ((test->flags & UTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
Simon Glass816fe6c2022-10-20 18:22:48 -0600525 return skip_test(uts);
Simon Glassb9958072022-09-06 20:27:11 -0600526
Simon Glass0d32ec22021-03-07 17:35:03 -0700527 /* Run with the live tree if possible */
528 runs = 0;
529 if (CONFIG_IS_ENABLED(OF_LIVE)) {
Simon Glass1a92f832024-08-22 07:57:48 -0600530 if (!(test->flags & UTF_FLAT_TREE)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700531 uts->of_live = true;
Simon Glass94b35832024-10-09 18:28:59 -0600532 ret = ut_run_test(uts, test, test->name);
533 if (ret != -EAGAIN) {
534 ut_assertok(ret);
535 runs++;
536 }
Simon Glass0d32ec22021-03-07 17:35:03 -0700537 }
538 }
539
540 /*
Simon Glassb9958072022-09-06 20:27:11 -0600541 * Run with the flat tree if:
542 * - it is not marked for live tree only
543 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
544 * not enabled (since flat tree can only support a single FDT in that
545 * case
546 * - we couldn't run it with live tree,
547 * - it is a core test (dm tests except video)
548 * - the FDT is still valid and has not been updated by an earlier test
549 * (for sandbox we handle this by copying the tree, but not for other
550 * boards)
Simon Glass0d32ec22021-03-07 17:35:03 -0700551 */
Sean Anderson5765fb12023-09-29 12:06:54 -0400552 if ((!CONFIG_IS_ENABLED(OF_LIVE) ||
Simon Glass1a92f832024-08-22 07:57:48 -0600553 (test->flags & UTF_SCAN_FDT)) &&
554 !(test->flags & UTF_LIVE_TREE) &&
Simon Glassb9958072022-09-06 20:27:11 -0600555 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
Simon Glass1a92f832024-08-22 07:57:48 -0600556 !(test->flags & UTF_OTHER_FDT)) &&
Simon Glass60f08992022-09-06 20:27:06 -0600557 (!runs || ut_test_run_on_flattree(test)) &&
558 !(gd->flags & GD_FLG_FDT_CHANGED)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700559 uts->of_live = false;
Simon Glass94b35832024-10-09 18:28:59 -0600560 ret = ut_run_test(uts, test, test->name);
561 if (ret != -EAGAIN) {
562 ut_assertok(ret);
563 runs++;
564 }
Simon Glass0d32ec22021-03-07 17:35:03 -0700565 }
566
567 return 0;
568}
569
Simon Glassbb2b1732021-03-07 17:35:05 -0700570/**
571 * ut_run_tests() - Run a set of tests
572 *
573 * This runs the tests, handling any preparation and clean-up needed. It prints
574 * the name of each test before running it.
575 *
576 * @uts: Test state to update. The caller should ensure that this is zeroed for
577 * the first call to this function. On exit, @uts->fail_count is
578 * incremented by the number of failures (0, one hopes)
579 * @prefix: String prefix for the tests. Any tests that have this prefix will be
580 * printed without the prefix, so that it is easier to see the unique part
581 * of the test name. If NULL, no prefix processing is done
582 * @tests: List of tests to run
583 * @count: Number of tests to run
584 * @select_name: Name of a single test to run (from the list provided). If NULL
585 * then all tests are run
Simon Glass798b5c52024-10-19 09:21:56 -0600586 * @test_insert: String describing a test to run after n other tests run, in the
587 * format n:name where n is the number of tests to run before this one and
588 * name is the name of the test to run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100589 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
Simon Glassbb2b1732021-03-07 17:35:05 -0700590 * -EBADF if any failed
591 */
592static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
593 struct unit_test *tests, int count,
Simon Glass85ba7c32022-10-29 19:47:13 -0600594 const char *select_name, const char *test_insert)
Simon Glass5722fb22021-03-07 17:34:47 -0700595{
Simon Glass85ba7c32022-10-29 19:47:13 -0600596 struct unit_test *test, *one;
Simon Glass5722fb22021-03-07 17:34:47 -0700597 int found = 0;
Simon Glass85ba7c32022-10-29 19:47:13 -0600598 int pos = 0;
599 int upto;
Simon Glass5722fb22021-03-07 17:34:47 -0700600
Simon Glass85ba7c32022-10-29 19:47:13 -0600601 one = NULL;
602 if (test_insert) {
603 char *p;
604
605 pos = dectoul(test_insert, NULL);
606 p = strchr(test_insert, ':');
607 if (p)
608 p++;
609
610 for (test = tests; test < tests + count; test++) {
611 if (!strcmp(p, test->name))
612 one = test;
613 }
614 }
615
616 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
Simon Glass5722fb22021-03-07 17:34:47 -0700617 const char *test_name = test->name;
Simon Glass91a187b2022-08-01 07:58:45 -0600618 int ret, i, old_fail_count;
Simon Glass5722fb22021-03-07 17:34:47 -0700619
Simon Glassbb2b1732021-03-07 17:35:05 -0700620 if (!test_matches(prefix, test_name, select_name))
Simon Glass5722fb22021-03-07 17:34:47 -0700621 continue;
Simon Glass1f1614b2022-10-20 18:22:50 -0600622
Simon Glass1a92f832024-08-22 07:57:48 -0600623 if (test->flags & UTF_MANUAL) {
Simon Glass1f1614b2022-10-20 18:22:50 -0600624 int len;
625
626 /*
627 * manual tests must have a name ending "_norun" as this
628 * is how pytest knows to skip them. See
629 * generate_ut_subtest() for this check.
630 */
631 len = strlen(test_name);
632 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600633 printf("Test '%s' is manual so must have a name ending in _norun\n",
Simon Glass1f1614b2022-10-20 18:22:50 -0600634 test_name);
635 uts->fail_count++;
636 return -EBADF;
637 }
638 if (!uts->force_run) {
639 if (select_name) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600640 printf("Test '%s' skipped as it is manual (use -f to run it)\n",
Simon Glass1f1614b2022-10-20 18:22:50 -0600641 test_name);
642 }
643 continue;
644 }
645 }
Simon Glass91a187b2022-08-01 07:58:45 -0600646 old_fail_count = uts->fail_count;
Simon Glass85ba7c32022-10-29 19:47:13 -0600647
648 if (one && upto == pos) {
649 ret = ut_run_test_live_flat(uts, one);
650 if (uts->fail_count != old_fail_count) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600651 printf("Test '%s' failed %d times (position %d)\n",
Simon Glass85ba7c32022-10-29 19:47:13 -0600652 one->name,
653 uts->fail_count - old_fail_count, pos);
654 }
655 return -EBADF;
656 }
657
Simon Glass91a187b2022-08-01 07:58:45 -0600658 for (i = 0; i < uts->runs_per_test; i++)
Simon Glass0f7eb632022-10-29 19:47:09 -0600659 ret = ut_run_test_live_flat(uts, test);
Simon Glass91a187b2022-08-01 07:58:45 -0600660 if (uts->fail_count != old_fail_count) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600661 printf("Test '%s' failed %d times\n", test_name,
Simon Glass91a187b2022-08-01 07:58:45 -0600662 uts->fail_count - old_fail_count);
663 }
Simon Glass5722fb22021-03-07 17:34:47 -0700664 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700665 if (ret == -EAGAIN)
666 continue;
667 if (ret)
668 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700669 }
670 if (select_name && !found)
671 return -ENOENT;
672
673 return uts->fail_count ? -EBADF : 0;
674}
675
Simon Glass3869eb52025-01-20 14:25:26 -0700676int ut_run_list(struct unit_test_state *uts, const char *category,
677 const char *prefix, struct unit_test *tests, int count,
678 const char *select_name, int runs_per_test, bool force_run,
679 const char *test_insert)
Simon Glass5722fb22021-03-07 17:34:47 -0700680{
Simon Glass3869eb52025-01-20 14:25:26 -0700681 ;
Simon Glass53d1b192021-03-07 17:35:08 -0700682 bool has_dm_tests = false;
Simon Glass5722fb22021-03-07 17:34:47 -0700683 int ret;
684
Simon Glass1899e132021-03-07 17:35:07 -0700685 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
Simon Glass798b5c52024-10-19 09:21:56 -0600686 ut_list_has_dm_tests(tests, count, prefix, select_name)) {
Simon Glass53d1b192021-03-07 17:35:08 -0700687 has_dm_tests = true;
Simon Glass1899e132021-03-07 17:35:07 -0700688 /*
689 * If we have no device tree, or it only has a root node, then
690 * these * tests clearly aren't going to work...
691 */
692 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
693 puts("Please run with test device tree:\n"
694 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
695 return CMD_RET_FAILURE;
696 }
697 }
698
Simon Glass5722fb22021-03-07 17:34:47 -0700699 if (!select_name)
700 printf("Running %d %s tests\n", count, category);
701
Simon Glass3869eb52025-01-20 14:25:26 -0700702 uts->of_root = gd_of_root();
703 uts->runs_per_test = runs_per_test;
Simon Glass3ba76752022-09-06 20:27:05 -0600704 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
Simon Glass3869eb52025-01-20 14:25:26 -0700705 uts->fdt_size = fdt_totalsize(gd->fdt_blob);
706 uts->fdt_copy = os_malloc(uts->fdt_size);
707 if (!uts->fdt_copy) {
Simon Glass3ba76752022-09-06 20:27:05 -0600708 printf("Out of memory for device tree copy\n");
709 return -ENOMEM;
710 }
Simon Glass3869eb52025-01-20 14:25:26 -0700711 memcpy(uts->fdt_copy, gd->fdt_blob, uts->fdt_size);
Simon Glass3ba76752022-09-06 20:27:05 -0600712 }
Simon Glass3869eb52025-01-20 14:25:26 -0700713 uts->force_run = force_run;
714 ret = ut_run_tests(uts, prefix, tests, count, select_name,
Simon Glass85ba7c32022-10-29 19:47:13 -0600715 test_insert);
Simon Glass5722fb22021-03-07 17:34:47 -0700716
Simon Glass3ba76752022-09-06 20:27:05 -0600717 /* Best efforts only...ignore errors */
718 if (has_dm_tests)
Simon Glass3869eb52025-01-20 14:25:26 -0700719 dm_test_restore(uts->of_root);
Simon Glass3ba76752022-09-06 20:27:05 -0600720
Simon Glass3869eb52025-01-20 14:25:26 -0700721 if (uts->skip_count)
722 printf("Skipped: %d, ", uts->skip_count);
Simon Glass5722fb22021-03-07 17:34:47 -0700723 if (ret == -ENOENT)
724 printf("Test '%s' not found\n", select_name);
725 else
Simon Glass3869eb52025-01-20 14:25:26 -0700726 printf("Failures: %d\n", uts->fail_count);
Simon Glass5722fb22021-03-07 17:34:47 -0700727
Simon Glass5722fb22021-03-07 17:34:47 -0700728 return ret;
729}