1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #define LOG_CATEGORY LOGC_TEST
8
9 #include <blk.h>
10 #include <console.h>
11 #include <cyclic.h>
12 #include <dm.h>
13 #include <event.h>
14 #include <net.h>
15 #include <of_live.h>
16 #include <os.h>
17 #include <spl.h>
18 #include <usb.h>
19 #include <dm/ofnode.h>
20 #include <dm/root.h>
21 #include <dm/test.h>
22 #include <dm/uclass-internal.h>
23 #include <test/test.h>
24 #include <test/ut.h>
25 #include <u-boot/crc.h>
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 /**
30 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
31 *
32 * This affects what happens with the device tree before and after a test
33 *
34 * @FDTCHK_NONE: Do nothing
35 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
36 * compare it afterwards to detect any changes
37 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
38 */
39 enum fdtchk_t {
40 FDTCHK_NONE,
41 FDTCHK_CHECKSUM,
42 FDTCHK_COPY,
43 };
44
45 /**
46 * fdt_action() - get the required action for the FDT
47 *
48 * @return the action that should be taken for this build
49 */
fdt_action(void)50 static enum fdtchk_t fdt_action(void)
51 {
52 /* For sandbox SPL builds, do nothing */
53 if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_XPL_BUILD))
54 return FDTCHK_NONE;
55
56 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
57 if (IS_ENABLED(CONFIG_SANDBOX))
58 return FDTCHK_COPY;
59
60 /* For all other boards, do a checksum */
61 return FDTCHK_CHECKSUM;
62 }
63
64 /* This is valid when a test is running, NULL otherwise */
65 static struct unit_test_state *cur_test_state;
66
ut_get_state(void)67 struct unit_test_state *ut_get_state(void)
68 {
69 return cur_test_state;
70 }
71
ut_set_state(struct unit_test_state * uts)72 void ut_set_state(struct unit_test_state *uts)
73 {
74 cur_test_state = uts;
75 }
76
ut_init_state(struct unit_test_state * uts)77 void ut_init_state(struct unit_test_state *uts)
78 {
79 memset(uts, '\0', sizeof(*uts));
80 }
81
ut_uninit_state(struct unit_test_state * uts)82 void ut_uninit_state(struct unit_test_state *uts)
83 {
84 if (IS_ENABLED(CONFIG_SANDBOX)) {
85 os_free(uts->fdt_copy);
86 os_free(uts->other_fdt);
87 }
88 }
89
90 /**
91 * dm_test_pre_run() - Get ready to run a driver model test
92 *
93 * This clears out the driver model data structures. For sandbox it resets the
94 * state structure
95 *
96 * @uts: Test state
97 */
dm_test_pre_run(struct unit_test_state * uts)98 static int dm_test_pre_run(struct unit_test_state *uts)
99 {
100 bool of_live = uts->of_live;
101
102 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
103 printf("Cannot run live tree test as device tree changed\n");
104 return -EFAULT;
105 }
106 uts->root = NULL;
107 uts->testdev = NULL;
108 uts->force_fail_alloc = false;
109 uts->skip_post_probe = false;
110 if (fdt_action() == FDTCHK_CHECKSUM)
111 uts->fdt_chksum = crc8(0, gd->fdt_blob,
112 fdt_totalsize(gd->fdt_blob));
113 gd->dm_root = NULL;
114 malloc_disable_testing();
115 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
116 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
117 arch_reset_for_test();
118
119 /* Determine whether to make the live tree available */
120 gd_set_of_root(of_live ? uts->of_root : NULL);
121 oftree_reset();
122 ut_assertok(dm_init(of_live));
123 uts->root = dm_root();
124
125 return 0;
126 }
127
dm_test_post_run(struct unit_test_state * uts)128 static int dm_test_post_run(struct unit_test_state *uts)
129 {
130 int id;
131
132 if (gd->fdt_blob) {
133 switch (fdt_action()) {
134 case FDTCHK_COPY:
135 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
136 break;
137 case FDTCHK_CHECKSUM: {
138 uint chksum;
139
140 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
141 if (chksum != uts->fdt_chksum) {
142 /*
143 * We cannot run any more tests that need the
144 * live tree, since its strings point into the
145 * flat tree, which has changed. This likely
146 * means that at least some of the pointers from
147 * the live tree point to different things
148 */
149 printf("Device tree changed: cannot run live tree tests\n");
150 gd->flags |= GD_FLG_FDT_CHANGED;
151 }
152 break;
153 }
154 case FDTCHK_NONE:
155 break;
156 }
157 }
158
159 /*
160 * With of-platdata-inst the uclasses are created at build time. If we
161 * destroy them we cannot get them back since uclass_add() is not
162 * supported. So skip this.
163 */
164 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
165 for (id = 0; id < UCLASS_COUNT; id++) {
166 struct uclass *uc;
167
168 /*
169 * If the uclass doesn't exist we don't want to create
170 * it. So check that here before we call
171 * uclass_find_device().
172 */
173 uc = uclass_find(id);
174 if (!uc)
175 continue;
176 ut_assertok(uclass_destroy(uc));
177 }
178 }
179
180 return 0;
181 }
182
183 /* Ensure all the test devices are probed */
do_autoprobe(struct unit_test_state * uts)184 static int do_autoprobe(struct unit_test_state *uts)
185 {
186 return uclass_probe_all(UCLASS_TEST);
187 }
188
189 /*
190 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
191 *
192 * This skips long/slow tests where there is not much value in running a flat
193 * DT test in addition to a live DT test.
194 *
195 * Return: true to run the given test on the flat device tree
196 */
ut_test_run_on_flattree(struct unit_test * test)197 static bool ut_test_run_on_flattree(struct unit_test *test)
198 {
199 const char *fname = strrchr(test->file, '/') + 1;
200
201 if (!(test->flags & UTF_DM))
202 return false;
203
204 return !strstr(fname, "video") || strstr(test->name, "video_base");
205 }
206
207 /**
208 * test_matches() - Check if a test should be run
209 *
210 * This checks if the a test should be run. In the normal case of running all
211 * tests, @select_name is NULL.
212 *
213 * @prefix: String prefix for the tests. Any tests that have this prefix will be
214 * printed without the prefix, so that it is easier to see the unique part
215 * of the test name. If NULL, any suite name (xxx_test) is considered to be
216 * a prefix.
217 * @test_name: Name of current test
218 * @select_name: Name of test to run (or NULL for all)
219 * Return: true to run this test, false to skip it
220 */
test_matches(const char * prefix,const char * test_name,const char * select_name)221 static bool test_matches(const char *prefix, const char *test_name,
222 const char *select_name)
223 {
224 size_t len;
225
226 if (!select_name)
227 return true;
228
229 /* Allow glob expansion in the test name */
230 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
231 if (len-- == 1)
232 return true;
233
234 if (!strncmp(test_name, select_name, len))
235 return true;
236
237 if (prefix) {
238 /* All tests have this prefix */
239 if (!strncmp(test_name, prefix, strlen(prefix)))
240 test_name += strlen(prefix);
241 } else {
242 const char *p = strstr(test_name, "_test_");
243
244 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
245 if (p)
246 test_name = p + strlen("_test_");
247 }
248
249 if (!strncmp(test_name, select_name, len))
250 return true;
251
252 return false;
253 }
254
255 /**
256 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
257 *
258 * @tests: List of tests to run
259 * @count: Number of tests to run
260 * @prefix: String prefix for the tests. Any tests that have this prefix will be
261 * printed without the prefix, so that it is easier to see the unique part
262 * of the test name. If NULL, no prefix processing is done
263 * @select_name: Name of a single test being run (from the list provided). If
264 * NULL all tests are being run
265 * Return: true if any of the tests have the UTF_DM flag
266 */
ut_list_has_dm_tests(struct unit_test * tests,int count,const char * prefix,const char * select_name)267 static bool ut_list_has_dm_tests(struct unit_test *tests, int count,
268 const char *prefix, const char *select_name)
269 {
270 struct unit_test *test;
271
272 for (test = tests; test < tests + count; test++) {
273 if (test_matches(prefix, test->name, select_name) &&
274 (test->flags & UTF_DM))
275 return true;
276 }
277
278 return false;
279 }
280
281 /**
282 * dm_test_restore() Put things back to normal so sandbox works as expected
283 *
284 * @of_root: Value to set for of_root
285 * Return: 0 if OK, -ve on error
286 */
dm_test_restore(struct device_node * of_root)287 static int dm_test_restore(struct device_node *of_root)
288 {
289 int ret;
290
291 gd_set_of_root(of_root);
292 gd->dm_root = NULL;
293 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
294 if (ret)
295 return ret;
296 dm_scan_plat(false);
297 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
298 dm_extended_scan(false);
299
300 return 0;
301 }
302
303 /**
304 * test_pre_run() - Handle any preparation needed to run a test
305 *
306 * @uts: Test state
307 * @test: Test to prepare for
308 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
309 * available, other -ve on error (meaning that testing cannot likely
310 * continue)
311 */
test_pre_run(struct unit_test_state * uts,struct unit_test * test)312 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
313 {
314 ut_assertok(event_init());
315
316 /*
317 * Remove any USB keyboard, so that we can add and remove USB devices
318 * in tests.
319 *
320 * For UTF_DM tests, the old driver model state is saved and
321 * restored across each test. Within in each test there is therefore a
322 * new driver model state, which means that any USB keyboard device in
323 * stdio points to the old state.
324 *
325 * This is fine in most cases. But if a non-UTF_DM test starts up
326 * USB (thus creating a stdio record pointing to the USB keyboard
327 * device) then when the test finishes, the new driver model state is
328 * freed, meaning that there is now a stale pointer in stdio.
329 *
330 * This means that any future UTF_DM test which uses stdin will
331 * cause the console system to call tstc() on the stale device pointer,
332 * causing a crash.
333 *
334 * We don't want to fix this by enabling UTF_DM for all tests as
335 * this causes other problems. For example, bootflow_efi relies on
336 * U-Boot going through a proper init - without that we don't have the
337 * TCG measurement working and get an error
338 * 'tcg2 measurement fails(0x8000000000000007)'. Once we tidy up how EFI
339 * runs tests (e.g. get rid of all the restarting of U-Boot) we could
340 * potentially make the bootstd tests set UTF_DM, but other tests
341 * might do the same thing.
342 *
343 * We could add a test flag to declare that USB is being used, but that
344 * seems unnecessary, at least for now. We could detect USB being used
345 * in a test, but there is no obvious drawback to clearing out stale
346 * pointers always.
347 *
348 * So just remove any USB keyboards from the console tables. This allows
349 * UTF_DM and non-UTF_DM tests to coexist happily.
350 */
351 usb_kbd_remove_for_test();
352
353 if (test->flags & UTF_DM)
354 ut_assertok(dm_test_pre_run(uts));
355
356 ut_set_skip_delays(uts, false);
357
358 uts->start = mallinfo();
359
360 if (test->flags & UTF_SCAN_PDATA)
361 ut_assertok(dm_scan_plat(false));
362
363 if (test->flags & UTF_PROBE_TEST)
364 ut_assertok(do_autoprobe(uts));
365
366 if (CONFIG_IS_ENABLED(OF_REAL) &&
367 (test->flags & UTF_SCAN_FDT)) {
368 /*
369 * only set this if we know the ethernet uclass will be created
370 */
371 eth_set_enable_bootdevs(test->flags & UTF_ETH_BOOTDEV);
372 test_sf_set_enable_bootdevs(test->flags & UTF_SF_BOOTDEV);
373 ut_assertok(dm_extended_scan(false));
374 }
375
376 /*
377 * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c
378 * checks for the existence of bootstd
379 */
380 if (test->flags & UTF_SCAN_PDATA)
381 ut_assertok(dm_scan_other(false));
382
383 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UTF_OTHER_FDT)) {
384 /* make sure the other FDT is available */
385 ut_assertok(test_load_other_fdt(uts));
386
387 /*
388 * create a new live tree with it for every test, in case a
389 * test modifies the tree
390 */
391 if (of_live_active()) {
392 ut_assertok(unflatten_device_tree(uts->other_fdt,
393 &uts->of_other));
394 }
395 }
396
397 if (test->flags & UTF_CONSOLE) {
398 int ret = console_record_reset_enable();
399
400 if (ret) {
401 printf("Skipping: Console recording disabled\n");
402 return -EAGAIN;
403 }
404 }
405 if (test->flags & UFT_BLOBLIST) {
406 log_debug("save bloblist %p\n", gd_bloblist());
407 uts->old_bloblist = gd_bloblist();
408 gd_set_bloblist(NULL);
409 }
410
411 ut_silence_console(uts);
412
413 return 0;
414 }
415
416 /**
417 * test_post_run() - Handle cleaning up after a test
418 *
419 * @uts: Test state
420 * @test: Test to clean up after
421 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
422 */
test_post_run(struct unit_test_state * uts,struct unit_test * test)423 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
424 {
425 ut_unsilence_console(uts);
426 if (test->flags & UTF_DM)
427 ut_assertok(dm_test_post_run(uts));
428 ut_assertok(cyclic_unregister_all());
429 ut_assertok(event_uninit());
430
431 free(uts->of_other);
432 uts->of_other = NULL;
433
434 if (test->flags & UFT_BLOBLIST) {
435 gd_set_bloblist(uts->old_bloblist);
436 log_debug("restore bloblist %p\n", gd_bloblist());
437 }
438
439 blkcache_free();
440
441 return 0;
442 }
443
444 /**
445 * skip_test() - Handle skipping a test
446 *
447 * @uts: Test state to update
448 * @return -EAGAIN (always)
449 */
skip_test(struct unit_test_state * uts)450 static int skip_test(struct unit_test_state *uts)
451 {
452 uts->cur.skip_count++;
453
454 return -EAGAIN;
455 }
456
457 /**
458 * ut_run_test() - Run a single test
459 *
460 * This runs the test, handling any preparation and clean-up needed. It prints
461 * the name of each test before running it.
462 *
463 * @uts: Test state to update. The caller should ensure that this is zeroed for
464 * the first call to this function. On exit, @uts->cur.fail_count is
465 * incremented by the number of failures (0, one hopes)
466 * @test_name: Test to run
467 * @name: Name of test, possibly skipping a prefix that should not be displayed
468 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
469 * any failed
470 */
ut_run_test(struct unit_test_state * uts,struct unit_test * test,const char * test_name)471 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
472 const char *test_name)
473 {
474 const char *fname = strrchr(test->file, '/') + 1;
475 const char *note = "";
476 int ret;
477
478 if ((test->flags & UTF_DM) && !uts->of_live)
479 note = " (flat tree)";
480 printf("Test: %s: %s%s\n", test_name, fname, note);
481
482 /* Allow access to test state from drivers */
483 ut_set_state(uts);
484
485 ret = test_pre_run(uts, test);
486 if (ret == -EAGAIN)
487 return skip_test(uts);
488 if (ret)
489 return ret;
490
491 ret = test->func(uts);
492 if (ret == -EAGAIN)
493 skip_test(uts);
494
495 ret = test_post_run(uts, test);
496 if (ret)
497 return ret;
498
499 ut_set_state(NULL);
500
501 return 0;
502 }
503
504 /**
505 * ut_run_test_live_flat() - Run a test with both live and flat tree
506 *
507 * This calls ut_run_test() with livetree enabled, which is the standard setup
508 * for runnig tests. Then, for driver model test, it calls it again with
509 * livetree disabled. This allows checking of flattree being used when OF_LIVE
510 * is enabled, as is the case in U-Boot proper before relocation, as well as in
511 * SPL.
512 *
513 * @uts: Test state to update. The caller should ensure that this is zeroed for
514 * the first call to this function. On exit, @uts->cur.fail_count is
515 * incremented by the number of failures (0, one hopes)
516 * @test: Test to run
517 * @leaf: Part of the name to show, or NULL to use test->name
518 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
519 * any failed
520 */
ut_run_test_live_flat(struct unit_test_state * uts,struct unit_test * test,const char * leaf)521 static int ut_run_test_live_flat(struct unit_test_state *uts,
522 struct unit_test *test, const char *leaf)
523 {
524 int runs, ret;
525
526 if ((test->flags & UTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
527 return skip_test(uts);
528
529 /* Run with the live tree if possible */
530 runs = 0;
531 if (CONFIG_IS_ENABLED(OF_LIVE)) {
532 if (!(test->flags & UTF_FLAT_TREE)) {
533 uts->of_live = true;
534 ret = ut_run_test(uts, test, leaf ?: test->name);
535 if (ret != -EAGAIN) {
536 ut_assertok(ret);
537 runs++;
538 }
539 }
540 }
541
542 /*
543 * Run with the flat tree if:
544 * - it is not marked for live tree only
545 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
546 * not enabled (since flat tree can only support a single FDT in that
547 * case
548 * - we couldn't run it with live tree,
549 * - it is a core test (dm tests except video)
550 * - the FDT is still valid and has not been updated by an earlier test
551 * (for sandbox we handle this by copying the tree, but not for other
552 * boards)
553 */
554 if ((!CONFIG_IS_ENABLED(OF_LIVE) ||
555 (test->flags & UTF_SCAN_FDT)) &&
556 !(test->flags & UTF_LIVE_TREE) &&
557 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
558 !(test->flags & UTF_OTHER_FDT)) &&
559 (!runs || ut_test_run_on_flattree(test)) &&
560 !(gd->flags & GD_FLG_FDT_CHANGED)) {
561 uts->of_live = false;
562 ret = ut_run_test(uts, test, leaf ?: test->name);
563 if (ret != -EAGAIN) {
564 ut_assertok(ret);
565 runs++;
566 }
567 }
568
569 return 0;
570 }
571
572 /**
573 * ut_run_tests() - Run a set of tests
574 *
575 * This runs the tests, handling any preparation and clean-up needed. It prints
576 * the name of each test before running it.
577 *
578 * @uts: Test state to update. The caller should ensure that this is zeroed for
579 * the first call to this function. On exit, @uts->cur.fail_count is
580 * incremented by the number of failures (0, one hopes)
581 * @prefix: String prefix for the tests. Any tests that have this prefix will be
582 * printed without the prefix, so that it is easier to see the unique part
583 * of the test name. If NULL, no prefix processing is done
584 * @tests: List of tests to run
585 * @count: Number of tests to run
586 * @select_name: Name of a single test to run (from the list provided). If NULL
587 * then all tests are run
588 * @test_insert: String describing a test to run after n other tests run, in the
589 * format n:name where n is the number of tests to run before this one and
590 * name is the name of the test to run
591 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
592 * -EBADF if any failed
593 */
ut_run_tests(struct unit_test_state * uts,const char * prefix,struct unit_test * tests,int count,const char * select_name,const char * test_insert)594 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
595 struct unit_test *tests, int count,
596 const char *select_name, const char *test_insert)
597 {
598 int prefix_len = prefix ? strlen(prefix) : 0;
599 struct unit_test *test, *one;
600 int found = 0;
601 int pos = 0;
602 int upto;
603
604 one = NULL;
605 if (test_insert) {
606 char *p;
607
608 pos = dectoul(test_insert, NULL);
609 p = strchr(test_insert, ':');
610 if (p)
611 p++;
612
613 for (test = tests; test < tests + count; test++) {
614 if (!strcmp(p, test->name))
615 one = test;
616 }
617 }
618
619 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
620 const char *test_name = test->name;
621 int ret, i, old_fail_count;
622
623 if (!(test->flags & (UTF_INIT | UTF_UNINIT)) &&
624 !test_matches(prefix, test_name, select_name))
625 continue;
626
627 if (test->flags & UTF_MANUAL) {
628 int len;
629
630 /*
631 * manual tests must have a name ending "_norun" as this
632 * is how pytest knows to skip them. See
633 * generate_ut_subtest() for this check.
634 */
635 len = strlen(test_name);
636 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
637 printf("Test '%s' is manual so must have a name ending in _norun\n",
638 test_name);
639 uts->cur.fail_count++;
640 return -EBADF;
641 }
642 if (!uts->force_run) {
643 printf("Test: %s: skipped as it is manual (use -f to run it)\n",
644 test_name);
645 continue;
646 }
647 }
648 old_fail_count = uts->cur.fail_count;
649
650 uts->cur.test_count++;
651 if (one && upto == pos) {
652 ret = ut_run_test_live_flat(uts, one, NULL);
653 if (uts->cur.fail_count != old_fail_count) {
654 printf("Test '%s' failed %d times (position %d)\n",
655 one->name,
656 uts->cur.fail_count - old_fail_count,
657 pos);
658 }
659 return -EBADF;
660 }
661
662 if (prefix_len && !strncmp(test_name, prefix, prefix_len))
663 test_name = test_name + prefix_len;
664
665 for (i = 0; i < uts->runs_per_test; i++)
666 ret = ut_run_test_live_flat(uts, test, test_name);
667 if (uts->cur.fail_count != old_fail_count) {
668 printf("Test '%s' failed %d times\n", test_name,
669 uts->cur.fail_count - old_fail_count);
670 }
671 found++;
672 if (ret == -EAGAIN)
673 continue;
674 if (ret)
675 return ret;
676 }
677 if (select_name && !found)
678 return -ENOENT;
679
680 return uts->cur.fail_count ? -EBADF : 0;
681 }
682
ut_report(struct ut_stats * stats,int run_count)683 void ut_report(struct ut_stats *stats, int run_count)
684 {
685 if (run_count > 1)
686 printf("Suites run: %d, total tests", run_count);
687 else
688 printf("Tests");
689 printf(" run: %d, ", stats->test_count);
690 if (stats && stats->test_count) {
691 ulong dur = stats->duration_ms;
692
693 printf("%ld ms, average: %ld ms, ", dur,
694 dur ? dur / stats->test_count : 0);
695 }
696 if (stats->skip_count)
697 printf("skipped: %d, ", stats->skip_count);
698 printf("failures: %d\n", stats->fail_count);
699 }
700
ut_run_list(struct unit_test_state * uts,const char * category,const char * prefix,struct unit_test * tests,int count,const char * select_name,int runs_per_test,bool force_run,const char * test_insert)701 int ut_run_list(struct unit_test_state *uts, const char *category,
702 const char *prefix, struct unit_test *tests, int count,
703 const char *select_name, int runs_per_test, bool force_run,
704 const char *test_insert)
705 {
706 ;
707 bool has_dm_tests = false;
708 ulong start_offset = 0;
709 ulong test_offset = 0;
710 int ret;
711
712 memset(&uts->cur, '\0', sizeof(struct ut_stats));
713 if (CONFIG_IS_ENABLED(UNIT_TEST_DURATION)) {
714 uts->cur.start = get_timer(0);
715 start_offset = timer_test_get_offset();
716 }
717
718 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
719 ut_list_has_dm_tests(tests, count, prefix, select_name)) {
720 has_dm_tests = true;
721 /*
722 * If we have no device tree, or it only has a root node, then
723 * these tests clearly aren't going to work...
724 */
725 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
726 puts("Please run with test device tree:\n"
727 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
728 return CMD_RET_FAILURE;
729 }
730 }
731
732 if (!select_name)
733 printf("Running %d %s tests\n", count, category);
734
735 uts->of_root = gd_of_root();
736 uts->runs_per_test = runs_per_test;
737 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
738 uts->fdt_size = fdt_totalsize(gd->fdt_blob);
739 uts->fdt_copy = os_malloc(uts->fdt_size);
740 if (!uts->fdt_copy) {
741 printf("Out of memory for device tree copy\n");
742 return -ENOMEM;
743 }
744 memcpy(uts->fdt_copy, gd->fdt_blob, uts->fdt_size);
745 }
746 uts->force_run = force_run;
747 ret = ut_run_tests(uts, prefix, tests, count, select_name,
748 test_insert);
749
750 /* Best efforts only...ignore errors */
751 if (has_dm_tests)
752 dm_test_restore(uts->of_root);
753
754 if (ret == -ENOENT)
755 printf("Test '%s' not found\n", select_name);
756 if (CONFIG_IS_ENABLED(UNIT_TEST_DURATION)) {
757 test_offset = timer_test_get_offset() - start_offset;
758
759 uts->cur.duration_ms = get_timer(uts->cur.start) - test_offset;
760 }
761 ut_report(&uts->cur, 1);
762
763 uts->total.skip_count += uts->cur.skip_count;
764 uts->total.fail_count += uts->cur.fail_count;
765 uts->total.test_count += uts->cur.test_count;
766 uts->total.duration_ms += uts->cur.duration_ms;
767 uts->run_count++;
768
769 return ret;
770 }
771