blob: ca6a073a8cbea72422c53aa950b09ac81bf97b98 [file] [log] [blame]
Simon Glass1c721752021-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 Glass5ea894a2022-10-29 19:47:08 -06007#include <blk.h>
Simon Glass1c721752021-03-07 17:34:47 -07008#include <console.h>
Stefan Roeseaf042c22022-09-02 13:57:54 +02009#include <cyclic.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070010#include <dm.h>
Simon Glass7d026452022-03-04 08:43:01 -070011#include <event.h>
Simon Glass70dd8862023-01-17 10:47:28 -070012#include <net.h>
Simon Glass8d468a12022-09-06 20:27:11 -060013#include <of_live.h>
Simon Glass0e4b6972022-09-06 20:27:05 -060014#include <os.h>
Simon Glassbc624322024-09-01 16:26:20 -060015#include <usb.h>
Simon Glassee88ba72022-09-06 20:27:19 -060016#include <dm/ofnode.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070017#include <dm/root.h>
Simon Glassc79705e2021-03-07 17:34:58 -070018#include <dm/test.h>
Simon Glasse77615d2021-03-07 17:34:59 -070019#include <dm/uclass-internal.h>
Simon Glass1c721752021-03-07 17:34:47 -070020#include <test/test.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070021#include <test/ut.h>
Simon Glass0e4b6972022-09-06 20:27:05 -060022#include <u-boot/crc.h>
Simon Glass1c721752021-03-07 17:34:47 -070023
Simon Glass30a0d202021-03-07 17:34:49 -070024DECLARE_GLOBAL_DATA_PTR;
25
Simon Glass0e4b6972022-09-06 20:27:05 -060026/**
27 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
28 *
29 * This affects what happens with the device tree before and after a test
30 *
31 * @FDTCHK_NONE: Do nothing
32 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
33 * compare it afterwards to detect any changes
34 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
35 */
36enum fdtchk_t {
37 FDTCHK_NONE,
38 FDTCHK_CHECKSUM,
39 FDTCHK_COPY,
40};
41
42/**
43 * fdt_action() - get the required action for the FDT
44 *
45 * @return the action that should be taken for this build
46 */
47static enum fdtchk_t fdt_action(void)
48{
Simon Glass0e4b6972022-09-06 20:27:05 -060049 /* For sandbox SPL builds, do nothing */
Simon Glass1d6132e2024-09-29 19:49:50 -060050 if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_XPL_BUILD))
Simon Glass0e4b6972022-09-06 20:27:05 -060051 return FDTCHK_NONE;
52
Simon Glassd5774592023-02-22 09:34:12 -070053 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
54 if (IS_ENABLED(CONFIG_SANDBOX))
55 return FDTCHK_COPY;
56
Simon Glass0e4b6972022-09-06 20:27:05 -060057 /* For all other boards, do a checksum */
58 return FDTCHK_CHECKSUM;
59}
60
Simon Glassfe806862021-03-07 17:35:04 -070061/* This is valid when a test is running, NULL otherwise */
62static struct unit_test_state *cur_test_state;
63
64struct unit_test_state *test_get_state(void)
65{
66 return cur_test_state;
67}
68
69void test_set_state(struct unit_test_state *uts)
70{
71 cur_test_state = uts;
72}
73
Simon Glassc79705e2021-03-07 17:34:58 -070074/**
75 * dm_test_pre_run() - Get ready to run a driver model test
76 *
77 * This clears out the driver model data structures. For sandbox it resets the
78 * state structure
79 *
80 * @uts: Test state
81 */
82static int dm_test_pre_run(struct unit_test_state *uts)
83{
84 bool of_live = uts->of_live;
85
Simon Glasseb6e9032022-09-06 20:27:06 -060086 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
87 printf("Cannot run live tree test as device tree changed\n");
88 return -EFAULT;
89 }
Simon Glassc79705e2021-03-07 17:34:58 -070090 uts->root = NULL;
91 uts->testdev = NULL;
92 uts->force_fail_alloc = false;
93 uts->skip_post_probe = false;
Simon Glass0e4b6972022-09-06 20:27:05 -060094 if (fdt_action() == FDTCHK_CHECKSUM)
95 uts->fdt_chksum = crc8(0, gd->fdt_blob,
96 fdt_totalsize(gd->fdt_blob));
Simon Glassc79705e2021-03-07 17:34:58 -070097 gd->dm_root = NULL;
Simon Glass62d63832022-09-06 20:27:00 -060098 malloc_disable_testing();
Simon Glass719d2862021-07-05 16:32:43 -060099 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glassc79705e2021-03-07 17:34:58 -0700100 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glassd4a15922021-03-25 10:44:33 +1300101 arch_reset_for_test();
Simon Glassc79705e2021-03-07 17:34:58 -0700102
103 /* Determine whether to make the live tree available */
104 gd_set_of_root(of_live ? uts->of_root : NULL);
Simon Glassee88ba72022-09-06 20:27:19 -0600105 oftree_reset();
Simon Glassc79705e2021-03-07 17:34:58 -0700106 ut_assertok(dm_init(of_live));
107 uts->root = dm_root();
108
109 return 0;
110}
111
Simon Glasse77615d2021-03-07 17:34:59 -0700112static int dm_test_post_run(struct unit_test_state *uts)
113{
114 int id;
115
Simon Glass0e4b6972022-09-06 20:27:05 -0600116 if (gd->fdt_blob) {
117 switch (fdt_action()) {
118 case FDTCHK_COPY:
119 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
120 break;
121 case FDTCHK_CHECKSUM: {
122 uint chksum;
123
124 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
Simon Glasseb6e9032022-09-06 20:27:06 -0600125 if (chksum != uts->fdt_chksum) {
126 /*
127 * We cannot run any more tests that need the
128 * live tree, since its strings point into the
129 * flat tree, which has changed. This likely
130 * means that at least some of the pointers from
131 * the live tree point to different things
132 */
Simon Glass0e4b6972022-09-06 20:27:05 -0600133 printf("Device tree changed: cannot run live tree tests\n");
Simon Glasseb6e9032022-09-06 20:27:06 -0600134 gd->flags |= GD_FLG_FDT_CHANGED;
135 }
Simon Glass0e4b6972022-09-06 20:27:05 -0600136 break;
137 }
138 case FDTCHK_NONE:
139 break;
140 }
141 }
142
Simon Glasse8c023c2021-03-15 17:25:21 +1300143 /*
144 * With of-platdata-inst the uclasses are created at build time. If we
145 * destroy them we cannot get them back since uclass_add() is not
146 * supported. So skip this.
147 */
148 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
149 for (id = 0; id < UCLASS_COUNT; id++) {
150 struct uclass *uc;
Simon Glasse77615d2021-03-07 17:34:59 -0700151
Simon Glasse8c023c2021-03-15 17:25:21 +1300152 /*
153 * If the uclass doesn't exist we don't want to create
154 * it. So check that here before we call
155 * uclass_find_device().
156 */
157 uc = uclass_find(id);
158 if (!uc)
159 continue;
160 ut_assertok(uclass_destroy(uc));
161 }
Simon Glasse77615d2021-03-07 17:34:59 -0700162 }
163
164 return 0;
165}
166
Simon Glass4b8b27e2021-03-07 17:34:51 -0700167/* Ensure all the test devices are probed */
168static int do_autoprobe(struct unit_test_state *uts)
169{
Michal Suchanekc0648b72022-10-12 21:57:51 +0200170 return uclass_probe_all(UCLASS_TEST);
Simon Glass4b8b27e2021-03-07 17:34:51 -0700171}
172
Simon Glassd2281bb2021-03-07 17:35:03 -0700173/*
174 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
175 *
176 * This skips long/slow tests where there is not much value in running a flat
177 * DT test in addition to a live DT test.
178 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100179 * Return: true to run the given test on the flat device tree
Simon Glassd2281bb2021-03-07 17:35:03 -0700180 */
181static bool ut_test_run_on_flattree(struct unit_test *test)
182{
183 const char *fname = strrchr(test->file, '/') + 1;
184
Simon Glass725c4382024-08-22 07:57:48 -0600185 if (!(test->flags & UTF_DM))
Simon Glassd2281bb2021-03-07 17:35:03 -0700186 return false;
187
188 return !strstr(fname, "video") || strstr(test->name, "video_base");
189}
190
Simon Glassca44ca02021-03-07 17:35:01 -0700191/**
Simon Glassf97f85e2021-03-07 17:35:05 -0700192 * test_matches() - Check if a test should be run
193 *
194 * This checks if the a test should be run. In the normal case of running all
195 * tests, @select_name is NULL.
196 *
197 * @prefix: String prefix for the tests. Any tests that have this prefix will be
198 * printed without the prefix, so that it is easier to see the unique part
Simon Glass84823562021-03-07 17:35:12 -0700199 * of the test name. If NULL, any suite name (xxx_test) is considered to be
200 * a prefix.
Simon Glassf97f85e2021-03-07 17:35:05 -0700201 * @test_name: Name of current test
202 * @select_name: Name of test to run (or NULL for all)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100203 * Return: true to run this test, false to skip it
Simon Glassf97f85e2021-03-07 17:35:05 -0700204 */
205static bool test_matches(const char *prefix, const char *test_name,
206 const char *select_name)
207{
Andy Shevchenkoff232a72021-02-11 16:40:10 +0200208 size_t len;
209
Simon Glassf97f85e2021-03-07 17:35:05 -0700210 if (!select_name)
211 return true;
212
Andy Shevchenkoff232a72021-02-11 16:40:10 +0200213 /* Allow glob expansion in the test name */
214 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
215 if (len-- == 1)
216 return true;
217
218 if (!strncmp(test_name, select_name, len))
Simon Glassf97f85e2021-03-07 17:35:05 -0700219 return true;
220
Andy Shevchenko494a5e12021-02-11 16:40:11 +0200221 if (prefix) {
222 /* All tests have this prefix */
223 if (!strncmp(test_name, prefix, strlen(prefix)))
224 test_name += strlen(prefix);
225 } else {
Simon Glass84823562021-03-07 17:35:12 -0700226 const char *p = strstr(test_name, "_test_");
227
228 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
229 if (p)
Andy Shevchenko494a5e12021-02-11 16:40:11 +0200230 test_name = p + strlen("_test_");
Simon Glass84823562021-03-07 17:35:12 -0700231 }
Simon Glassf97f85e2021-03-07 17:35:05 -0700232
Andy Shevchenkoff232a72021-02-11 16:40:10 +0200233 if (!strncmp(test_name, select_name, len))
Simon Glassf97f85e2021-03-07 17:35:05 -0700234 return true;
235
236 return false;
237}
238
Simon Glass664277f2021-03-07 17:35:08 -0700239/**
Simon Glass1fc9c122021-03-07 17:35:07 -0700240 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
241 *
242 * @tests: List of tests to run
Simon Glassfbdac812024-10-19 09:21:56 -0600243 * @count: Number of tests to run
244 * @prefix: String prefix for the tests. Any tests that have this prefix will be
245 * printed without the prefix, so that it is easier to see the unique part
246 * of the test name. If NULL, no prefix processing is done
247 * @select_name: Name of a single test being run (from the list provided). If
248 * NULL all tests are being run
Simon Glass725c4382024-08-22 07:57:48 -0600249 * Return: true if any of the tests have the UTF_DM flag
Simon Glass1fc9c122021-03-07 17:35:07 -0700250 */
Simon Glassfbdac812024-10-19 09:21:56 -0600251static bool ut_list_has_dm_tests(struct unit_test *tests, int count,
252 const char *prefix, const char *select_name)
Simon Glass1fc9c122021-03-07 17:35:07 -0700253{
254 struct unit_test *test;
255
256 for (test = tests; test < tests + count; test++) {
Simon Glassfbdac812024-10-19 09:21:56 -0600257 if (test_matches(prefix, test->name, select_name) &&
258 (test->flags & UTF_DM))
Simon Glass1fc9c122021-03-07 17:35:07 -0700259 return true;
260 }
261
262 return false;
263}
264
Simon Glassf97f85e2021-03-07 17:35:05 -0700265/**
Simon Glass664277f2021-03-07 17:35:08 -0700266 * dm_test_restore() Put things back to normal so sandbox works as expected
267 *
268 * @of_root: Value to set for of_root
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100269 * Return: 0 if OK, -ve on error
Simon Glass664277f2021-03-07 17:35:08 -0700270 */
271static int dm_test_restore(struct device_node *of_root)
272{
273 int ret;
274
275 gd_set_of_root(of_root);
276 gd->dm_root = NULL;
277 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
278 if (ret)
279 return ret;
280 dm_scan_plat(false);
281 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
AKASHI Takahiro230038f2023-06-08 09:55:59 +0900282 dm_extended_scan(false);
Simon Glass664277f2021-03-07 17:35:08 -0700283
284 return 0;
285}
286
287/**
Simon Glassca44ca02021-03-07 17:35:01 -0700288 * test_pre_run() - Handle any preparation needed to run a test
289 *
290 * @uts: Test state
291 * @test: Test to prepare for
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100292 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
Simon Glassca44ca02021-03-07 17:35:01 -0700293 * available, other -ve on error (meaning that testing cannot likely
294 * continue)
295 */
296static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700297{
Simon Glass7d026452022-03-04 08:43:01 -0700298 ut_assertok(event_init());
299
Simon Glassbc624322024-09-01 16:26:20 -0600300 /*
301 * Remove any USB keyboard, so that we can add and remove USB devices
302 * in tests.
303 *
Simon Glass29502f62024-10-14 14:17:53 -0600304 * For UTF_DM tests, the old driver model state is saved and
Simon Glassbc624322024-09-01 16:26:20 -0600305 * restored across each test. Within in each test there is therefore a
306 * new driver model state, which means that any USB keyboard device in
307 * stdio points to the old state.
308 *
Simon Glass29502f62024-10-14 14:17:53 -0600309 * This is fine in most cases. But if a non-UTF_DM test starts up
Simon Glassbc624322024-09-01 16:26:20 -0600310 * USB (thus creating a stdio record pointing to the USB keyboard
311 * device) then when the test finishes, the new driver model state is
312 * freed, meaning that there is now a stale pointer in stdio.
313 *
Simon Glass29502f62024-10-14 14:17:53 -0600314 * This means that any future UTF_DM test which uses stdin will
Simon Glassbc624322024-09-01 16:26:20 -0600315 * cause the console system to call tstc() on the stale device pointer,
316 * causing a crash.
317 *
Simon Glass29502f62024-10-14 14:17:53 -0600318 * We don't want to fix this by enabling UTF_DM for all tests as
Simon Glassbc624322024-09-01 16:26:20 -0600319 * this causes other problems. For example, bootflow_efi relies on
320 * U-Boot going through a proper init - without that we don't have the
321 * TCG measurement working and get an error
322 * 'tcg2 measurement fails(0x8000000000000007)'. Once we tidy up how EFI
323 * runs tests (e.g. get rid of all the restarting of U-Boot) we could
Simon Glass29502f62024-10-14 14:17:53 -0600324 * potentially make the bootstd tests set UTF_DM, but other tests
Simon Glassbc624322024-09-01 16:26:20 -0600325 * might do the same thing.
326 *
327 * We could add a test flag to declare that USB is being used, but that
328 * seems unnecessary, at least for now. We could detect USB being used
329 * in a test, but there is no obvious drawback to clearing out stale
330 * pointers always.
331 *
332 * So just remove any USB keyboards from the console tables. This allows
Simon Glass29502f62024-10-14 14:17:53 -0600333 * UTF_DM and non-UTF_DM tests to coexist happily.
Simon Glassbc624322024-09-01 16:26:20 -0600334 */
335 usb_kbd_remove_for_test();
336
Simon Glass725c4382024-08-22 07:57:48 -0600337 if (test->flags & UTF_DM)
Simon Glassc79705e2021-03-07 17:34:58 -0700338 ut_assertok(dm_test_pre_run(uts));
Simon Glass72b524c2021-03-07 17:34:56 -0700339
Simon Glass47ec3ed2021-03-07 17:34:55 -0700340 ut_set_skip_delays(uts, false);
341
Simon Glass19fb3db2021-03-07 17:34:53 -0700342 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -0700343
Simon Glass725c4382024-08-22 07:57:48 -0600344 if (test->flags & UTF_SCAN_PDATA)
Simon Glass5a986f32021-03-07 17:34:52 -0700345 ut_assertok(dm_scan_plat(false));
346
Simon Glass725c4382024-08-22 07:57:48 -0600347 if (test->flags & UTF_PROBE_TEST)
Simon Glass4b8b27e2021-03-07 17:34:51 -0700348 ut_assertok(do_autoprobe(uts));
349
Sean Andersoneaf73852023-10-14 16:47:46 -0400350 if (CONFIG_IS_ENABLED(OF_REAL) &&
Simon Glass725c4382024-08-22 07:57:48 -0600351 (test->flags & UTF_SCAN_FDT)) {
Simon Glass70dd8862023-01-17 10:47:28 -0700352 /*
353 * only set this if we know the ethernet uclass will be created
354 */
Simon Glass725c4382024-08-22 07:57:48 -0600355 eth_set_enable_bootdevs(test->flags & UTF_ETH_BOOTDEV);
356 test_sf_set_enable_bootdevs(test->flags & UTF_SF_BOOTDEV);
Simon Glassd8ed2342021-03-07 17:34:50 -0700357 ut_assertok(dm_extended_scan(false));
Simon Glass70dd8862023-01-17 10:47:28 -0700358 }
Simon Glassd8ed2342021-03-07 17:34:50 -0700359
Simon Glassecb274c2023-01-17 10:47:23 -0700360 /*
361 * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c
362 * checks for the existence of bootstd
363 */
Simon Glass725c4382024-08-22 07:57:48 -0600364 if (test->flags & UTF_SCAN_PDATA)
Simon Glassecb274c2023-01-17 10:47:23 -0700365 ut_assertok(dm_scan_other(false));
366
Simon Glass725c4382024-08-22 07:57:48 -0600367 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UTF_OTHER_FDT)) {
Simon Glass8d468a12022-09-06 20:27:11 -0600368 /* make sure the other FDT is available */
369 ut_assertok(test_load_other_fdt(uts));
370
371 /*
372 * create a new live tree with it for every test, in case a
373 * test modifies the tree
374 */
375 if (of_live_active()) {
376 ut_assertok(unflatten_device_tree(uts->other_fdt,
377 &uts->of_other));
378 }
379 }
380
Simon Glass9b997622024-08-22 07:57:50 -0600381 if (test->flags & UTF_CONSOLE) {
Simon Glassd002a272021-03-07 17:34:48 -0700382 int ret = console_record_reset_enable();
383
384 if (ret) {
385 printf("Skipping: Console recording disabled\n");
386 return -EAGAIN;
387 }
388 }
Simon Glass74524712021-03-07 17:34:54 -0700389 ut_silence_console(uts);
Simon Glassd002a272021-03-07 17:34:48 -0700390
391 return 0;
392}
393
Simon Glassca44ca02021-03-07 17:35:01 -0700394/**
395 * test_post_run() - Handle cleaning up after a test
396 *
397 * @uts: Test state
398 * @test: Test to clean up after
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100399 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
Simon Glassca44ca02021-03-07 17:35:01 -0700400 */
401static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700402{
Simon Glass74524712021-03-07 17:34:54 -0700403 ut_unsilence_console(uts);
Simon Glass725c4382024-08-22 07:57:48 -0600404 if (test->flags & UTF_DM)
Simon Glasse77615d2021-03-07 17:34:59 -0700405 ut_assertok(dm_test_post_run(uts));
Rasmus Villemoes50128ae2022-10-28 13:50:54 +0200406 ut_assertok(cyclic_unregister_all());
Simon Glass7d026452022-03-04 08:43:01 -0700407 ut_assertok(event_uninit());
Simon Glass30a0d202021-03-07 17:34:49 -0700408
Simon Glass8d468a12022-09-06 20:27:11 -0600409 free(uts->of_other);
410 uts->of_other = NULL;
411
Simon Glass5ea894a2022-10-29 19:47:08 -0600412 blkcache_free();
413
Simon Glassd002a272021-03-07 17:34:48 -0700414 return 0;
415}
416
Simon Glassd2281bb2021-03-07 17:35:03 -0700417/**
Simon Glass1facaad2022-10-20 18:22:48 -0600418 * skip_test() - Handle skipping a test
419 *
420 * @uts: Test state to update
421 * @return -EAGAIN (always)
422 */
423static int skip_test(struct unit_test_state *uts)
424{
425 uts->skip_count++;
426
427 return -EAGAIN;
428}
429
430/**
Simon Glassd2281bb2021-03-07 17:35:03 -0700431 * ut_run_test() - Run a single test
432 *
433 * This runs the test, handling any preparation and clean-up needed. It prints
434 * the name of each test before running it.
435 *
436 * @uts: Test state to update. The caller should ensure that this is zeroed for
437 * the first call to this function. On exit, @uts->fail_count is
438 * incremented by the number of failures (0, one hopes)
439 * @test_name: Test to run
440 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100441 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassd2281bb2021-03-07 17:35:03 -0700442 * any failed
443 */
444static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
445 const char *test_name)
Simon Glass99a88fe2021-03-07 17:35:00 -0700446{
Simon Glassca44ca02021-03-07 17:35:01 -0700447 const char *fname = strrchr(test->file, '/') + 1;
448 const char *note = "";
Simon Glass99a88fe2021-03-07 17:35:00 -0700449 int ret;
450
Simon Glass725c4382024-08-22 07:57:48 -0600451 if ((test->flags & UTF_DM) && !uts->of_live)
Simon Glassca44ca02021-03-07 17:35:01 -0700452 note = " (flat tree)";
453 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass99a88fe2021-03-07 17:35:00 -0700454
Simon Glassfe806862021-03-07 17:35:04 -0700455 /* Allow access to test state from drivers */
456 test_set_state(uts);
457
Simon Glass99a88fe2021-03-07 17:35:00 -0700458 ret = test_pre_run(uts, test);
459 if (ret == -EAGAIN)
Simon Glass1facaad2022-10-20 18:22:48 -0600460 return skip_test(uts);
Simon Glass99a88fe2021-03-07 17:35:00 -0700461 if (ret)
462 return ret;
463
Simon Glass1facaad2022-10-20 18:22:48 -0600464 ret = test->func(uts);
465 if (ret == -EAGAIN)
466 skip_test(uts);
Simon Glass99a88fe2021-03-07 17:35:00 -0700467
468 ret = test_post_run(uts, test);
469 if (ret)
470 return ret;
471
Simon Glassfe806862021-03-07 17:35:04 -0700472 test_set_state( NULL);
473
Simon Glass99a88fe2021-03-07 17:35:00 -0700474 return 0;
475}
476
Simon Glassf97f85e2021-03-07 17:35:05 -0700477/**
478 * ut_run_test_live_flat() - Run a test with both live and flat tree
479 *
480 * This calls ut_run_test() with livetree enabled, which is the standard setup
481 * for runnig tests. Then, for driver model test, it calls it again with
482 * livetree disabled. This allows checking of flattree being used when OF_LIVE
483 * is enabled, as is the case in U-Boot proper before relocation, as well as in
484 * SPL.
485 *
486 * @uts: Test state to update. The caller should ensure that this is zeroed for
487 * the first call to this function. On exit, @uts->fail_count is
488 * incremented by the number of failures (0, one hopes)
489 * @test: Test to run
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100490 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassf97f85e2021-03-07 17:35:05 -0700491 * any failed
492 */
493static int ut_run_test_live_flat(struct unit_test_state *uts,
Simon Glassf7a68d22022-10-29 19:47:09 -0600494 struct unit_test *test)
Simon Glassd2281bb2021-03-07 17:35:03 -0700495{
Simon Glass4018a082024-10-09 18:28:59 -0600496 int runs, ret;
Simon Glassd2281bb2021-03-07 17:35:03 -0700497
Simon Glass725c4382024-08-22 07:57:48 -0600498 if ((test->flags & UTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
Simon Glass1facaad2022-10-20 18:22:48 -0600499 return skip_test(uts);
Simon Glass8d468a12022-09-06 20:27:11 -0600500
Simon Glassd2281bb2021-03-07 17:35:03 -0700501 /* Run with the live tree if possible */
502 runs = 0;
503 if (CONFIG_IS_ENABLED(OF_LIVE)) {
Simon Glass725c4382024-08-22 07:57:48 -0600504 if (!(test->flags & UTF_FLAT_TREE)) {
Simon Glassd2281bb2021-03-07 17:35:03 -0700505 uts->of_live = true;
Simon Glass4018a082024-10-09 18:28:59 -0600506 ret = ut_run_test(uts, test, test->name);
507 if (ret != -EAGAIN) {
508 ut_assertok(ret);
509 runs++;
510 }
Simon Glassd2281bb2021-03-07 17:35:03 -0700511 }
512 }
513
514 /*
Simon Glass8d468a12022-09-06 20:27:11 -0600515 * Run with the flat tree if:
516 * - it is not marked for live tree only
517 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
518 * not enabled (since flat tree can only support a single FDT in that
519 * case
520 * - we couldn't run it with live tree,
521 * - it is a core test (dm tests except video)
522 * - the FDT is still valid and has not been updated by an earlier test
523 * (for sandbox we handle this by copying the tree, but not for other
524 * boards)
Simon Glassd2281bb2021-03-07 17:35:03 -0700525 */
Sean Anderson3f876cb2023-09-29 12:06:54 -0400526 if ((!CONFIG_IS_ENABLED(OF_LIVE) ||
Simon Glass725c4382024-08-22 07:57:48 -0600527 (test->flags & UTF_SCAN_FDT)) &&
528 !(test->flags & UTF_LIVE_TREE) &&
Simon Glass8d468a12022-09-06 20:27:11 -0600529 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
Simon Glass725c4382024-08-22 07:57:48 -0600530 !(test->flags & UTF_OTHER_FDT)) &&
Simon Glasseb6e9032022-09-06 20:27:06 -0600531 (!runs || ut_test_run_on_flattree(test)) &&
532 !(gd->flags & GD_FLG_FDT_CHANGED)) {
Simon Glassd2281bb2021-03-07 17:35:03 -0700533 uts->of_live = false;
Simon Glass4018a082024-10-09 18:28:59 -0600534 ret = ut_run_test(uts, test, test->name);
535 if (ret != -EAGAIN) {
536 ut_assertok(ret);
537 runs++;
538 }
Simon Glassd2281bb2021-03-07 17:35:03 -0700539 }
540
541 return 0;
542}
543
Simon Glassf97f85e2021-03-07 17:35:05 -0700544/**
545 * ut_run_tests() - Run a set of tests
546 *
547 * This runs the tests, handling any preparation and clean-up needed. It prints
548 * the name of each test before running it.
549 *
550 * @uts: Test state to update. The caller should ensure that this is zeroed for
551 * the first call to this function. On exit, @uts->fail_count is
552 * incremented by the number of failures (0, one hopes)
553 * @prefix: String prefix for the tests. Any tests that have this prefix will be
554 * printed without the prefix, so that it is easier to see the unique part
555 * of the test name. If NULL, no prefix processing is done
556 * @tests: List of tests to run
557 * @count: Number of tests to run
558 * @select_name: Name of a single test to run (from the list provided). If NULL
559 * then all tests are run
Simon Glassfbdac812024-10-19 09:21:56 -0600560 * @test_insert: String describing a test to run after n other tests run, in the
561 * format n:name where n is the number of tests to run before this one and
562 * name is the name of the test to run
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100563 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
Simon Glassf97f85e2021-03-07 17:35:05 -0700564 * -EBADF if any failed
565 */
566static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
567 struct unit_test *tests, int count,
Simon Glassd1b46592022-10-29 19:47:13 -0600568 const char *select_name, const char *test_insert)
Simon Glass1c721752021-03-07 17:34:47 -0700569{
Simon Glassd1b46592022-10-29 19:47:13 -0600570 struct unit_test *test, *one;
Simon Glass1c721752021-03-07 17:34:47 -0700571 int found = 0;
Simon Glassd1b46592022-10-29 19:47:13 -0600572 int pos = 0;
573 int upto;
Simon Glass1c721752021-03-07 17:34:47 -0700574
Simon Glassd1b46592022-10-29 19:47:13 -0600575 one = NULL;
576 if (test_insert) {
577 char *p;
578
579 pos = dectoul(test_insert, NULL);
580 p = strchr(test_insert, ':');
581 if (p)
582 p++;
583
584 for (test = tests; test < tests + count; test++) {
585 if (!strcmp(p, test->name))
586 one = test;
587 }
588 }
589
590 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
Simon Glass1c721752021-03-07 17:34:47 -0700591 const char *test_name = test->name;
Simon Glassea94d052022-08-01 07:58:45 -0600592 int ret, i, old_fail_count;
Simon Glass1c721752021-03-07 17:34:47 -0700593
Simon Glassf97f85e2021-03-07 17:35:05 -0700594 if (!test_matches(prefix, test_name, select_name))
Simon Glass1c721752021-03-07 17:34:47 -0700595 continue;
Simon Glasscbd71fa2022-10-20 18:22:50 -0600596
Simon Glass725c4382024-08-22 07:57:48 -0600597 if (test->flags & UTF_MANUAL) {
Simon Glasscbd71fa2022-10-20 18:22:50 -0600598 int len;
599
600 /*
601 * manual tests must have a name ending "_norun" as this
602 * is how pytest knows to skip them. See
603 * generate_ut_subtest() for this check.
604 */
605 len = strlen(test_name);
606 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
607 printf("Test %s is manual so must have a name ending in _norun\n",
608 test_name);
609 uts->fail_count++;
610 return -EBADF;
611 }
612 if (!uts->force_run) {
613 if (select_name) {
614 printf("Test %s skipped as it is manual (use -f to run it)\n",
615 test_name);
616 }
617 continue;
618 }
619 }
Simon Glassea94d052022-08-01 07:58:45 -0600620 old_fail_count = uts->fail_count;
Simon Glassd1b46592022-10-29 19:47:13 -0600621
622 if (one && upto == pos) {
623 ret = ut_run_test_live_flat(uts, one);
624 if (uts->fail_count != old_fail_count) {
625 printf("Test %s failed %d times (position %d)\n",
626 one->name,
627 uts->fail_count - old_fail_count, pos);
628 }
629 return -EBADF;
630 }
631
Simon Glassea94d052022-08-01 07:58:45 -0600632 for (i = 0; i < uts->runs_per_test; i++)
Simon Glassf7a68d22022-10-29 19:47:09 -0600633 ret = ut_run_test_live_flat(uts, test);
Simon Glassea94d052022-08-01 07:58:45 -0600634 if (uts->fail_count != old_fail_count) {
635 printf("Test %s failed %d times\n", select_name,
636 uts->fail_count - old_fail_count);
637 }
Simon Glass1c721752021-03-07 17:34:47 -0700638 found++;
Simon Glassd002a272021-03-07 17:34:48 -0700639 if (ret == -EAGAIN)
640 continue;
641 if (ret)
642 return ret;
Simon Glass1c721752021-03-07 17:34:47 -0700643 }
644 if (select_name && !found)
645 return -ENOENT;
646
647 return uts->fail_count ? -EBADF : 0;
648}
649
650int ut_run_list(const char *category, const char *prefix,
Simon Glassea94d052022-08-01 07:58:45 -0600651 struct unit_test *tests, int count, const char *select_name,
Simon Glassd1b46592022-10-29 19:47:13 -0600652 int runs_per_test, bool force_run, const char *test_insert)
Simon Glass1c721752021-03-07 17:34:47 -0700653{
654 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass664277f2021-03-07 17:35:08 -0700655 bool has_dm_tests = false;
Simon Glass1c721752021-03-07 17:34:47 -0700656 int ret;
657
Simon Glass1fc9c122021-03-07 17:35:07 -0700658 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
Simon Glassfbdac812024-10-19 09:21:56 -0600659 ut_list_has_dm_tests(tests, count, prefix, select_name)) {
Simon Glass664277f2021-03-07 17:35:08 -0700660 has_dm_tests = true;
Simon Glass1fc9c122021-03-07 17:35:07 -0700661 /*
662 * If we have no device tree, or it only has a root node, then
663 * these * tests clearly aren't going to work...
664 */
665 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
666 puts("Please run with test device tree:\n"
667 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
668 return CMD_RET_FAILURE;
669 }
670 }
671
Simon Glass1c721752021-03-07 17:34:47 -0700672 if (!select_name)
673 printf("Running %d %s tests\n", count, category);
674
Simon Glassf97f85e2021-03-07 17:35:05 -0700675 uts.of_root = gd_of_root();
Simon Glassea94d052022-08-01 07:58:45 -0600676 uts.runs_per_test = runs_per_test;
Simon Glass0e4b6972022-09-06 20:27:05 -0600677 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
678 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
679 uts.fdt_copy = os_malloc(uts.fdt_size);
680 if (!uts.fdt_copy) {
681 printf("Out of memory for device tree copy\n");
682 return -ENOMEM;
683 }
684 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
685 }
Simon Glasscbd71fa2022-10-20 18:22:50 -0600686 uts.force_run = force_run;
Simon Glassd1b46592022-10-29 19:47:13 -0600687 ret = ut_run_tests(&uts, prefix, tests, count, select_name,
688 test_insert);
Simon Glass1c721752021-03-07 17:34:47 -0700689
Simon Glass0e4b6972022-09-06 20:27:05 -0600690 /* Best efforts only...ignore errors */
691 if (has_dm_tests)
692 dm_test_restore(uts.of_root);
Simon Glass8d468a12022-09-06 20:27:11 -0600693 if (IS_ENABLED(CONFIG_SANDBOX)) {
Simon Glass0e4b6972022-09-06 20:27:05 -0600694 os_free(uts.fdt_copy);
Simon Glass8d468a12022-09-06 20:27:11 -0600695 os_free(uts.other_fdt);
696 }
Simon Glass0e4b6972022-09-06 20:27:05 -0600697
Simon Glass1facaad2022-10-20 18:22:48 -0600698 if (uts.skip_count)
699 printf("Skipped: %d, ", uts.skip_count);
Simon Glass1c721752021-03-07 17:34:47 -0700700 if (ret == -ENOENT)
701 printf("Test '%s' not found\n", select_name);
702 else
703 printf("Failures: %d\n", uts.fail_count);
704
Simon Glass1c721752021-03-07 17:34:47 -0700705 return ret;
706}