blob: c12027ce68033672ce24e17d8dbb1e9f840ce2c2 [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
7#include <common.h>
8#include <console.h>
Stefan Roese5be8f372022-09-02 13:57:54 +02009#include <cyclic.h>
Simon Glassd18f7392021-03-07 17:34:50 -070010#include <dm.h>
Simon Glassa128b1b2022-03-04 08:43:01 -070011#include <event.h>
Simon Glass3ba76752022-09-06 20:27:05 -060012#include <os.h>
Simon Glassd18f7392021-03-07 17:34:50 -070013#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070014#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070015#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070016#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070017#include <test/ut.h>
Simon Glass3ba76752022-09-06 20:27:05 -060018#include <u-boot/crc.h>
Simon Glass5722fb22021-03-07 17:34:47 -070019
Simon Glass0f8f6772021-03-07 17:34:49 -070020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass3ba76752022-09-06 20:27:05 -060022/**
23 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
24 *
25 * This affects what happens with the device tree before and after a test
26 *
27 * @FDTCHK_NONE: Do nothing
28 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
29 * compare it afterwards to detect any changes
30 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
31 */
32enum fdtchk_t {
33 FDTCHK_NONE,
34 FDTCHK_CHECKSUM,
35 FDTCHK_COPY,
36};
37
38/**
39 * fdt_action() - get the required action for the FDT
40 *
41 * @return the action that should be taken for this build
42 */
43static enum fdtchk_t fdt_action(void)
44{
45 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
46 if (CONFIG_IS_ENABLED(SANDBOX))
47 return FDTCHK_COPY;
48
49 /* For sandbox SPL builds, do nothing */
50 if (IS_ENABLED(CONFIG_SANDBOX))
51 return FDTCHK_NONE;
52
53 /* For all other boards, do a checksum */
54 return FDTCHK_CHECKSUM;
55}
56
Simon Glass4066d8d2021-03-07 17:35:04 -070057/* This is valid when a test is running, NULL otherwise */
58static struct unit_test_state *cur_test_state;
59
60struct unit_test_state *test_get_state(void)
61{
62 return cur_test_state;
63}
64
65void test_set_state(struct unit_test_state *uts)
66{
67 cur_test_state = uts;
68}
69
Simon Glass2dba4762021-03-07 17:34:58 -070070/**
71 * dm_test_pre_run() - Get ready to run a driver model test
72 *
73 * This clears out the driver model data structures. For sandbox it resets the
74 * state structure
75 *
76 * @uts: Test state
77 */
78static int dm_test_pre_run(struct unit_test_state *uts)
79{
80 bool of_live = uts->of_live;
81
Simon Glass60f08992022-09-06 20:27:06 -060082 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
83 printf("Cannot run live tree test as device tree changed\n");
84 return -EFAULT;
85 }
Simon Glass2dba4762021-03-07 17:34:58 -070086 uts->root = NULL;
87 uts->testdev = NULL;
88 uts->force_fail_alloc = false;
89 uts->skip_post_probe = false;
Simon Glass3ba76752022-09-06 20:27:05 -060090 if (fdt_action() == FDTCHK_CHECKSUM)
91 uts->fdt_chksum = crc8(0, gd->fdt_blob,
92 fdt_totalsize(gd->fdt_blob));
Simon Glass2dba4762021-03-07 17:34:58 -070093 gd->dm_root = NULL;
Simon Glass1a3e39b2022-09-06 20:27:00 -060094 malloc_disable_testing();
Simon Glassf7005042021-07-05 16:32:43 -060095 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glass2dba4762021-03-07 17:34:58 -070096 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glass45b807d2021-03-25 10:44:33 +130097 arch_reset_for_test();
Simon Glass2dba4762021-03-07 17:34:58 -070098
99 /* Determine whether to make the live tree available */
100 gd_set_of_root(of_live ? uts->of_root : NULL);
101 ut_assertok(dm_init(of_live));
102 uts->root = dm_root();
103
104 return 0;
105}
106
Simon Glass5b7e55b2021-03-07 17:34:59 -0700107static int dm_test_post_run(struct unit_test_state *uts)
108{
109 int id;
110
Simon Glass3ba76752022-09-06 20:27:05 -0600111 if (gd->fdt_blob) {
112 switch (fdt_action()) {
113 case FDTCHK_COPY:
114 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
115 break;
116 case FDTCHK_CHECKSUM: {
117 uint chksum;
118
119 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
Simon Glass60f08992022-09-06 20:27:06 -0600120 if (chksum != uts->fdt_chksum) {
121 /*
122 * We cannot run any more tests that need the
123 * live tree, since its strings point into the
124 * flat tree, which has changed. This likely
125 * means that at least some of the pointers from
126 * the live tree point to different things
127 */
Simon Glass3ba76752022-09-06 20:27:05 -0600128 printf("Device tree changed: cannot run live tree tests\n");
Simon Glass60f08992022-09-06 20:27:06 -0600129 gd->flags |= GD_FLG_FDT_CHANGED;
130 }
Simon Glass3ba76752022-09-06 20:27:05 -0600131 break;
132 }
133 case FDTCHK_NONE:
134 break;
135 }
136 }
137
Simon Glass96113c12021-03-15 17:25:21 +1300138 /*
139 * With of-platdata-inst the uclasses are created at build time. If we
140 * destroy them we cannot get them back since uclass_add() is not
141 * supported. So skip this.
142 */
143 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
144 for (id = 0; id < UCLASS_COUNT; id++) {
145 struct uclass *uc;
Simon Glass5b7e55b2021-03-07 17:34:59 -0700146
Simon Glass96113c12021-03-15 17:25:21 +1300147 /*
148 * If the uclass doesn't exist we don't want to create
149 * it. So check that here before we call
150 * uclass_find_device().
151 */
152 uc = uclass_find(id);
153 if (!uc)
154 continue;
155 ut_assertok(uclass_destroy(uc));
156 }
Simon Glass5b7e55b2021-03-07 17:34:59 -0700157 }
158
159 return 0;
160}
161
Simon Glass242357c2021-03-07 17:34:51 -0700162/* Ensure all the test devices are probed */
163static int do_autoprobe(struct unit_test_state *uts)
164{
165 struct udevice *dev;
166 int ret;
167
168 /* Scanning the uclass is enough to probe all the devices */
169 for (ret = uclass_first_device(UCLASS_TEST, &dev);
170 dev;
171 ret = uclass_next_device(&dev))
172 ;
173
174 return ret;
175}
176
Simon Glass0d32ec22021-03-07 17:35:03 -0700177/*
178 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
179 *
180 * This skips long/slow tests where there is not much value in running a flat
181 * DT test in addition to a live DT test.
182 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100183 * Return: true to run the given test on the flat device tree
Simon Glass0d32ec22021-03-07 17:35:03 -0700184 */
185static bool ut_test_run_on_flattree(struct unit_test *test)
186{
187 const char *fname = strrchr(test->file, '/') + 1;
188
189 if (!(test->flags & UT_TESTF_DM))
190 return false;
191
192 return !strstr(fname, "video") || strstr(test->name, "video_base");
193}
194
Simon Glass436687e2021-03-07 17:35:01 -0700195/**
Simon Glassbb2b1732021-03-07 17:35:05 -0700196 * test_matches() - Check if a test should be run
197 *
198 * This checks if the a test should be run. In the normal case of running all
199 * tests, @select_name is NULL.
200 *
201 * @prefix: String prefix for the tests. Any tests that have this prefix will be
202 * printed without the prefix, so that it is easier to see the unique part
Simon Glass1ef74ab2021-03-07 17:35:12 -0700203 * of the test name. If NULL, any suite name (xxx_test) is considered to be
204 * a prefix.
Simon Glassbb2b1732021-03-07 17:35:05 -0700205 * @test_name: Name of current test
206 * @select_name: Name of test to run (or NULL for all)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100207 * Return: true to run this test, false to skip it
Simon Glassbb2b1732021-03-07 17:35:05 -0700208 */
209static bool test_matches(const char *prefix, const char *test_name,
210 const char *select_name)
211{
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200212 size_t len;
213
Simon Glassbb2b1732021-03-07 17:35:05 -0700214 if (!select_name)
215 return true;
216
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200217 /* Allow glob expansion in the test name */
218 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
219 if (len-- == 1)
220 return true;
221
222 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700223 return true;
224
Andy Shevchenko00d42902021-02-11 16:40:11 +0200225 if (prefix) {
226 /* All tests have this prefix */
227 if (!strncmp(test_name, prefix, strlen(prefix)))
228 test_name += strlen(prefix);
229 } else {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700230 const char *p = strstr(test_name, "_test_");
231
232 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
233 if (p)
Andy Shevchenko00d42902021-02-11 16:40:11 +0200234 test_name = p + strlen("_test_");
Simon Glass1ef74ab2021-03-07 17:35:12 -0700235 }
Simon Glassbb2b1732021-03-07 17:35:05 -0700236
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200237 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700238 return true;
239
240 return false;
241}
242
Simon Glass53d1b192021-03-07 17:35:08 -0700243/**
Simon Glass1899e132021-03-07 17:35:07 -0700244 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
245 *
246 * @tests: List of tests to run
247 * @count: Number of tests to ru
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100248 * Return: true if any of the tests have the UT_TESTF_DM flag
Simon Glass1899e132021-03-07 17:35:07 -0700249 */
250static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
251{
252 struct unit_test *test;
253
254 for (test = tests; test < tests + count; test++) {
255 if (test->flags & UT_TESTF_DM)
256 return true;
257 }
258
259 return false;
260}
261
Simon Glassbb2b1732021-03-07 17:35:05 -0700262/**
Simon Glass53d1b192021-03-07 17:35:08 -0700263 * dm_test_restore() Put things back to normal so sandbox works as expected
264 *
265 * @of_root: Value to set for of_root
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100266 * Return: 0 if OK, -ve on error
Simon Glass53d1b192021-03-07 17:35:08 -0700267 */
268static int dm_test_restore(struct device_node *of_root)
269{
270 int ret;
271
272 gd_set_of_root(of_root);
273 gd->dm_root = NULL;
274 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
275 if (ret)
276 return ret;
277 dm_scan_plat(false);
278 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
279 dm_scan_fdt(false);
280
281 return 0;
282}
283
284/**
Simon Glass436687e2021-03-07 17:35:01 -0700285 * test_pre_run() - Handle any preparation needed to run a test
286 *
287 * @uts: Test state
288 * @test: Test to prepare for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100289 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
Simon Glass436687e2021-03-07 17:35:01 -0700290 * available, other -ve on error (meaning that testing cannot likely
291 * continue)
292 */
293static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700294{
Simon Glassa128b1b2022-03-04 08:43:01 -0700295 ut_assertok(event_init());
Stefan Roese5be8f372022-09-02 13:57:54 +0200296 ut_assertok(cyclic_init());
Simon Glassa128b1b2022-03-04 08:43:01 -0700297
Simon Glassb2890a12021-03-07 17:34:56 -0700298 if (test->flags & UT_TESTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700299 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700300
Simon Glass59cad962021-03-07 17:34:55 -0700301 ut_set_skip_delays(uts, false);
302
Simon Glass86a5bd02021-03-07 17:34:53 -0700303 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700304
Simon Glass9c80e542022-07-30 15:52:26 -0600305 if (test->flags & UT_TESTF_SCAN_PDATA) {
Simon Glass177e0fd2021-03-07 17:34:52 -0700306 ut_assertok(dm_scan_plat(false));
Simon Glass9c80e542022-07-30 15:52:26 -0600307 ut_assertok(dm_scan_other(false));
308 }
Simon Glass177e0fd2021-03-07 17:34:52 -0700309
Simon Glass242357c2021-03-07 17:34:51 -0700310 if (test->flags & UT_TESTF_PROBE_TEST)
311 ut_assertok(do_autoprobe(uts));
312
Simon Glassd18f7392021-03-07 17:34:50 -0700313 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
314 (test->flags & UT_TESTF_SCAN_FDT))
315 ut_assertok(dm_extended_scan(false));
316
Simon Glassd93dc7d2021-03-07 17:34:48 -0700317 if (test->flags & UT_TESTF_CONSOLE_REC) {
318 int ret = console_record_reset_enable();
319
320 if (ret) {
321 printf("Skipping: Console recording disabled\n");
322 return -EAGAIN;
323 }
324 }
Simon Glass2b566b92021-03-07 17:34:54 -0700325 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700326
327 return 0;
328}
329
Simon Glass436687e2021-03-07 17:35:01 -0700330/**
331 * test_post_run() - Handle cleaning up after a test
332 *
333 * @uts: Test state
334 * @test: Test to clean up after
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100335 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
Simon Glass436687e2021-03-07 17:35:01 -0700336 */
337static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700338{
Simon Glass2b566b92021-03-07 17:34:54 -0700339 ut_unsilence_console(uts);
Simon Glass5b7e55b2021-03-07 17:34:59 -0700340 if (test->flags & UT_TESTF_DM)
341 ut_assertok(dm_test_post_run(uts));
Stefan Roese5be8f372022-09-02 13:57:54 +0200342 ut_assertok(cyclic_uninit());
Simon Glassa128b1b2022-03-04 08:43:01 -0700343 ut_assertok(event_uninit());
Simon Glass0f8f6772021-03-07 17:34:49 -0700344
Simon Glassd93dc7d2021-03-07 17:34:48 -0700345 return 0;
346}
347
Simon Glass0d32ec22021-03-07 17:35:03 -0700348/**
349 * ut_run_test() - Run a single test
350 *
351 * This runs the test, handling any preparation and clean-up needed. It prints
352 * the name of each test before running it.
353 *
354 * @uts: Test state to update. The caller should ensure that this is zeroed for
355 * the first call to this function. On exit, @uts->fail_count is
356 * incremented by the number of failures (0, one hopes)
357 * @test_name: Test to run
358 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100359 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glass0d32ec22021-03-07 17:35:03 -0700360 * any failed
361 */
362static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
363 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700364{
Simon Glass436687e2021-03-07 17:35:01 -0700365 const char *fname = strrchr(test->file, '/') + 1;
366 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700367 int ret;
368
Simon Glass436687e2021-03-07 17:35:01 -0700369 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
370 note = " (flat tree)";
371 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700372
Simon Glass4066d8d2021-03-07 17:35:04 -0700373 /* Allow access to test state from drivers */
374 test_set_state(uts);
375
Simon Glass5517edb2021-03-07 17:35:00 -0700376 ret = test_pre_run(uts, test);
377 if (ret == -EAGAIN)
378 return -EAGAIN;
379 if (ret)
380 return ret;
381
382 test->func(uts);
383
384 ret = test_post_run(uts, test);
385 if (ret)
386 return ret;
387
Simon Glass4066d8d2021-03-07 17:35:04 -0700388 test_set_state( NULL);
389
Simon Glass5517edb2021-03-07 17:35:00 -0700390 return 0;
391}
392
Simon Glassbb2b1732021-03-07 17:35:05 -0700393/**
394 * ut_run_test_live_flat() - Run a test with both live and flat tree
395 *
396 * This calls ut_run_test() with livetree enabled, which is the standard setup
397 * for runnig tests. Then, for driver model test, it calls it again with
398 * livetree disabled. This allows checking of flattree being used when OF_LIVE
399 * is enabled, as is the case in U-Boot proper before relocation, as well as in
400 * SPL.
401 *
402 * @uts: Test state to update. The caller should ensure that this is zeroed for
403 * the first call to this function. On exit, @uts->fail_count is
404 * incremented by the number of failures (0, one hopes)
405 * @test: Test to run
406 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100407 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassbb2b1732021-03-07 17:35:05 -0700408 * any failed
409 */
410static int ut_run_test_live_flat(struct unit_test_state *uts,
411 struct unit_test *test, const char *name)
Simon Glass0d32ec22021-03-07 17:35:03 -0700412{
413 int runs;
414
415 /* Run with the live tree if possible */
416 runs = 0;
417 if (CONFIG_IS_ENABLED(OF_LIVE)) {
Simon Glass7f9b5802022-09-06 20:26:59 -0600418 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700419 uts->of_live = true;
420 ut_assertok(ut_run_test(uts, test, test->name));
421 runs++;
422 }
423 }
424
425 /*
426 * Run with the flat tree if we couldn't run it with live tree,
427 * or it is a core test.
428 */
429 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
Simon Glass60f08992022-09-06 20:27:06 -0600430 (!runs || ut_test_run_on_flattree(test)) &&
431 !(gd->flags & GD_FLG_FDT_CHANGED)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700432 uts->of_live = false;
433 ut_assertok(ut_run_test(uts, test, test->name));
434 runs++;
435 }
436
437 return 0;
438}
439
Simon Glassbb2b1732021-03-07 17:35:05 -0700440/**
441 * ut_run_tests() - Run a set of tests
442 *
443 * This runs the tests, handling any preparation and clean-up needed. It prints
444 * the name of each test before running it.
445 *
446 * @uts: Test state to update. The caller should ensure that this is zeroed for
447 * the first call to this function. On exit, @uts->fail_count is
448 * incremented by the number of failures (0, one hopes)
449 * @prefix: String prefix for the tests. Any tests that have this prefix will be
450 * printed without the prefix, so that it is easier to see the unique part
451 * of the test name. If NULL, no prefix processing is done
452 * @tests: List of tests to run
453 * @count: Number of tests to run
454 * @select_name: Name of a single test to run (from the list provided). If NULL
455 * then all tests are run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100456 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
Simon Glassbb2b1732021-03-07 17:35:05 -0700457 * -EBADF if any failed
458 */
459static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
460 struct unit_test *tests, int count,
461 const char *select_name)
Simon Glass5722fb22021-03-07 17:34:47 -0700462{
463 struct unit_test *test;
Simon Glass5722fb22021-03-07 17:34:47 -0700464 int found = 0;
465
466 for (test = tests; test < tests + count; test++) {
467 const char *test_name = test->name;
Simon Glass91a187b2022-08-01 07:58:45 -0600468 int ret, i, old_fail_count;
Simon Glass5722fb22021-03-07 17:34:47 -0700469
Simon Glassbb2b1732021-03-07 17:35:05 -0700470 if (!test_matches(prefix, test_name, select_name))
Simon Glass5722fb22021-03-07 17:34:47 -0700471 continue;
Simon Glass91a187b2022-08-01 07:58:45 -0600472 old_fail_count = uts->fail_count;
473 for (i = 0; i < uts->runs_per_test; i++)
474 ret = ut_run_test_live_flat(uts, test, select_name);
475 if (uts->fail_count != old_fail_count) {
476 printf("Test %s failed %d times\n", select_name,
477 uts->fail_count - old_fail_count);
478 }
Simon Glass5722fb22021-03-07 17:34:47 -0700479 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700480 if (ret == -EAGAIN)
481 continue;
482 if (ret)
483 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700484 }
485 if (select_name && !found)
486 return -ENOENT;
487
488 return uts->fail_count ? -EBADF : 0;
489}
490
491int ut_run_list(const char *category, const char *prefix,
Simon Glass91a187b2022-08-01 07:58:45 -0600492 struct unit_test *tests, int count, const char *select_name,
493 int runs_per_test)
Simon Glass5722fb22021-03-07 17:34:47 -0700494{
495 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass53d1b192021-03-07 17:35:08 -0700496 bool has_dm_tests = false;
Simon Glass5722fb22021-03-07 17:34:47 -0700497 int ret;
498
Simon Glass1899e132021-03-07 17:35:07 -0700499 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
500 ut_list_has_dm_tests(tests, count)) {
Simon Glass53d1b192021-03-07 17:35:08 -0700501 has_dm_tests = true;
Simon Glass1899e132021-03-07 17:35:07 -0700502 /*
503 * If we have no device tree, or it only has a root node, then
504 * these * tests clearly aren't going to work...
505 */
506 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
507 puts("Please run with test device tree:\n"
508 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
509 return CMD_RET_FAILURE;
510 }
511 }
512
Simon Glass5722fb22021-03-07 17:34:47 -0700513 if (!select_name)
514 printf("Running %d %s tests\n", count, category);
515
Simon Glassbb2b1732021-03-07 17:35:05 -0700516 uts.of_root = gd_of_root();
Simon Glass91a187b2022-08-01 07:58:45 -0600517 uts.runs_per_test = runs_per_test;
Simon Glass3ba76752022-09-06 20:27:05 -0600518 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
519 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
520 uts.fdt_copy = os_malloc(uts.fdt_size);
521 if (!uts.fdt_copy) {
522 printf("Out of memory for device tree copy\n");
523 return -ENOMEM;
524 }
525 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
526 }
Simon Glass5722fb22021-03-07 17:34:47 -0700527 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
528
Simon Glass3ba76752022-09-06 20:27:05 -0600529 /* Best efforts only...ignore errors */
530 if (has_dm_tests)
531 dm_test_restore(uts.of_root);
532 if (IS_ENABLED(CONFIG_SANDBOX))
533 os_free(uts.fdt_copy);
534
Simon Glass5722fb22021-03-07 17:34:47 -0700535 if (ret == -ENOENT)
536 printf("Test '%s' not found\n", select_name);
537 else
538 printf("Failures: %d\n", uts.fail_count);
539
Simon Glass53d1b192021-03-07 17:35:08 -0700540 /* Best efforts only...ignore errors */
541 if (has_dm_tests)
542 dm_test_restore(uts.of_root);
543
Simon Glass5722fb22021-03-07 17:34:47 -0700544 return ret;
545}