test: Detect a change in the device tree
[platform/kernel/u-boot.git] / test / test-main.c
1 // 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>
9 #include <cyclic.h>
10 #include <dm.h>
11 #include <event.h>
12 #include <os.h>
13 #include <dm/root.h>
14 #include <dm/test.h>
15 #include <dm/uclass-internal.h>
16 #include <test/test.h>
17 #include <test/ut.h>
18 #include <u-boot/crc.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 /**
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  */
32 enum 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  */
43 static 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
57 /* This is valid when a test is running, NULL otherwise */
58 static struct unit_test_state *cur_test_state;
59
60 struct unit_test_state *test_get_state(void)
61 {
62         return cur_test_state;
63 }
64
65 void test_set_state(struct unit_test_state *uts)
66 {
67         cur_test_state = uts;
68 }
69
70 /**
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  */
78 static int dm_test_pre_run(struct unit_test_state *uts)
79 {
80         bool of_live = uts->of_live;
81
82         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         }
86         uts->root = NULL;
87         uts->testdev = NULL;
88         uts->force_fail_alloc = false;
89         uts->skip_post_probe = false;
90         if (fdt_action() == FDTCHK_CHECKSUM)
91                 uts->fdt_chksum = crc8(0, gd->fdt_blob,
92                                        fdt_totalsize(gd->fdt_blob));
93         gd->dm_root = NULL;
94         malloc_disable_testing();
95         if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
96                 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
97         arch_reset_for_test();
98
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
107 static int dm_test_post_run(struct unit_test_state *uts)
108 {
109         int id;
110
111         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));
120                         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                                  */
128                                 printf("Device tree changed: cannot run live tree tests\n");
129                                 gd->flags |= GD_FLG_FDT_CHANGED;
130                         }
131                         break;
132                 }
133                 case FDTCHK_NONE:
134                         break;
135                 }
136         }
137
138         /*
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;
146
147                         /*
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                 }
157         }
158
159         return 0;
160 }
161
162 /* Ensure all the test devices are probed */
163 static 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
177 /*
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  *
183  * Return: true to run the given test on the flat device tree
184  */
185 static 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
195 /**
196  * 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
203  *      of the test name. If NULL, any suite name (xxx_test) is considered to be
204  *      a prefix.
205  * @test_name: Name of current test
206  * @select_name: Name of test to run (or NULL for all)
207  * Return: true to run this test, false to skip it
208  */
209 static bool test_matches(const char *prefix, const char *test_name,
210                          const char *select_name)
211 {
212         size_t len;
213
214         if (!select_name)
215                 return true;
216
217         /* 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))
223                 return true;
224
225         if (prefix) {
226                 /* All tests have this prefix */
227                 if (!strncmp(test_name, prefix, strlen(prefix)))
228                         test_name += strlen(prefix);
229         } else {
230                 const char *p = strstr(test_name, "_test_");
231
232                 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
233                 if (p)
234                         test_name = p + strlen("_test_");
235         }
236
237         if (!strncmp(test_name, select_name, len))
238                 return true;
239
240         return false;
241 }
242
243 /**
244  * 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
248  * Return: true if any of the tests have the UT_TESTF_DM flag
249  */
250 static 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
262 /**
263  * dm_test_restore() Put things back to normal so sandbox works as expected
264  *
265  * @of_root: Value to set for of_root
266  * Return: 0 if OK, -ve on error
267  */
268 static 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 /**
285  * test_pre_run() - Handle any preparation needed to run a test
286  *
287  * @uts: Test state
288  * @test: Test to prepare for
289  * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
290  *      available, other -ve on error (meaning that testing cannot likely
291  *      continue)
292  */
293 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
294 {
295         ut_assertok(event_init());
296         ut_assertok(cyclic_init());
297
298         if (test->flags & UT_TESTF_DM)
299                 ut_assertok(dm_test_pre_run(uts));
300
301         ut_set_skip_delays(uts, false);
302
303         uts->start = mallinfo();
304
305         if (test->flags & UT_TESTF_SCAN_PDATA) {
306                 ut_assertok(dm_scan_plat(false));
307                 ut_assertok(dm_scan_other(false));
308         }
309
310         if (test->flags & UT_TESTF_PROBE_TEST)
311                 ut_assertok(do_autoprobe(uts));
312
313         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
314             (test->flags & UT_TESTF_SCAN_FDT))
315                 ut_assertok(dm_extended_scan(false));
316
317         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         }
325         ut_silence_console(uts);
326
327         return 0;
328 }
329
330 /**
331  * test_post_run() - Handle cleaning up after a test
332  *
333  * @uts: Test state
334  * @test: Test to clean up after
335  * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
336  */
337 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
338 {
339         ut_unsilence_console(uts);
340         if (test->flags & UT_TESTF_DM)
341                 ut_assertok(dm_test_post_run(uts));
342         ut_assertok(cyclic_uninit());
343         ut_assertok(event_uninit());
344
345         return 0;
346 }
347
348 /**
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
359  * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
360  *      any failed
361  */
362 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
363                        const char *test_name)
364 {
365         const char *fname = strrchr(test->file, '/') + 1;
366         const char *note = "";
367         int ret;
368
369         if ((test->flags & UT_TESTF_DM) && !uts->of_live)
370                 note = " (flat tree)";
371         printf("Test: %s: %s%s\n", test_name, fname, note);
372
373         /* Allow access to test state from drivers */
374         test_set_state(uts);
375
376         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
388         test_set_state( NULL);
389
390         return 0;
391 }
392
393 /**
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
407  * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
408  *      any failed
409  */
410 static int ut_run_test_live_flat(struct unit_test_state *uts,
411                                  struct unit_test *test, const char *name)
412 {
413         int runs;
414
415         /* Run with the live tree if possible */
416         runs = 0;
417         if (CONFIG_IS_ENABLED(OF_LIVE)) {
418                 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
419                         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) &&
430             (!runs || ut_test_run_on_flattree(test)) &&
431             !(gd->flags & GD_FLG_FDT_CHANGED)) {
432                 uts->of_live = false;
433                 ut_assertok(ut_run_test(uts, test, test->name));
434                 runs++;
435         }
436
437         return 0;
438 }
439
440 /**
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
456  * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
457  *      -EBADF if any failed
458  */
459 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
460                         struct unit_test *tests, int count,
461                         const char *select_name)
462 {
463         struct unit_test *test;
464         int found = 0;
465
466         for (test = tests; test < tests + count; test++) {
467                 const char *test_name = test->name;
468                 int ret, i, old_fail_count;
469
470                 if (!test_matches(prefix, test_name, select_name))
471                         continue;
472                 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                 }
479                 found++;
480                 if (ret == -EAGAIN)
481                         continue;
482                 if (ret)
483                         return ret;
484         }
485         if (select_name && !found)
486                 return -ENOENT;
487
488         return uts->fail_count ? -EBADF : 0;
489 }
490
491 int ut_run_list(const char *category, const char *prefix,
492                 struct unit_test *tests, int count, const char *select_name,
493                 int runs_per_test)
494 {
495         struct unit_test_state uts = { .fail_count = 0 };
496         bool has_dm_tests = false;
497         int ret;
498
499         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
500             ut_list_has_dm_tests(tests, count)) {
501                 has_dm_tests = true;
502                 /*
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
513         if (!select_name)
514                 printf("Running %d %s tests\n", count, category);
515
516         uts.of_root = gd_of_root();
517         uts.runs_per_test = runs_per_test;
518         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         }
527         ret = ut_run_tests(&uts, prefix, tests, count, select_name);
528
529         /* 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
535         if (ret == -ENOENT)
536                 printf("Test '%s' not found\n", select_name);
537         else
538                 printf("Failures: %d\n", uts.fail_count);
539
540         /* Best efforts only...ignore errors */
541         if (has_dm_tests)
542                 dm_test_restore(uts.of_root);
543
544         return ret;
545 }