Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Tests for fdt command |
| 4 | * |
| 5 | * Copyright 2022 Google LLCmap_to_sysmem(fdt)); |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <console.h> |
| 10 | #include <fdt_support.h> |
| 11 | #include <mapmem.h> |
| 12 | #include <asm/global_data.h> |
| 13 | #include <linux/libfdt.h> |
| 14 | #include <test/suites.h> |
| 15 | #include <test/ut.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | /* Declare a new fdt test */ |
| 20 | #define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test) |
| 21 | |
| 22 | /** |
| 23 | * make_test_fdt() - Create an FDT with just a root node |
| 24 | * |
| 25 | * The size is set to the minimum needed |
| 26 | * |
| 27 | * @uts: Test state |
| 28 | * @fdt: Place to write FDT |
| 29 | * @size: Maximum size of space for fdt |
| 30 | */ |
| 31 | static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size) |
| 32 | { |
| 33 | ut_assertok(fdt_create(fdt, size)); |
| 34 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 35 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 36 | ut_assertok(fdt_end_node(fdt)); |
| 37 | ut_assertok(fdt_finish(fdt)); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /* Test 'fdt addr' getting/setting address */ |
| 43 | static int fdt_test_addr(struct unit_test_state *uts) |
| 44 | { |
| 45 | const void *fdt_blob, *new_fdt; |
| 46 | char fdt[256]; |
| 47 | ulong addr; |
| 48 | int ret; |
| 49 | |
| 50 | ut_assertok(console_record_reset_enable()); |
| 51 | ut_assertok(run_command("fdt addr -c", 0)); |
| 52 | ut_assert_nextline("Control fdt: %08lx", |
| 53 | (ulong)map_to_sysmem(gd->fdt_blob)); |
| 54 | ut_assertok(ut_check_console_end(uts)); |
| 55 | |
| 56 | /* The working fdt is not set, so this should fail */ |
| 57 | set_working_fdt_addr(0); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 58 | ut_assert_nextline("Working FDT set to 0"); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 59 | ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0)); |
| 60 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
| 61 | ut_assertok(ut_check_console_end(uts)); |
| 62 | |
| 63 | /* Set up a working FDT and try again */ |
| 64 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt))); |
| 65 | addr = map_to_sysmem(fdt); |
| 66 | set_working_fdt_addr(addr); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 67 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 68 | ut_assertok(run_command("fdt addr", 0)); |
| 69 | ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt)); |
| 70 | ut_assertok(ut_check_console_end(uts)); |
| 71 | |
| 72 | /* Set the working FDT */ |
| 73 | set_working_fdt_addr(0); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 74 | ut_assert_nextline("Working FDT set to 0"); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 75 | ut_assertok(run_commandf("fdt addr %08x", addr)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 76 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 77 | ut_asserteq(addr, map_to_sysmem(working_fdt)); |
| 78 | ut_assertok(ut_check_console_end(uts)); |
| 79 | set_working_fdt_addr(0); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 80 | ut_assert_nextline("Working FDT set to 0"); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 81 | |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 82 | /* Set the control FDT */ |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 83 | fdt_blob = gd->fdt_blob; |
| 84 | gd->fdt_blob = NULL; |
| 85 | ret = run_commandf("fdt addr -c %08x", addr); |
| 86 | new_fdt = gd->fdt_blob; |
| 87 | gd->fdt_blob = fdt_blob; |
| 88 | ut_assertok(ret); |
| 89 | ut_asserteq(addr, map_to_sysmem(new_fdt)); |
| 90 | ut_assertok(ut_check_console_end(uts)); |
| 91 | |
| 92 | /* Test setting an invalid FDT */ |
| 93 | fdt[0] = 123; |
| 94 | ut_asserteq(1, run_commandf("fdt addr %08x", addr)); |
| 95 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
| 96 | ut_assertok(ut_check_console_end(uts)); |
| 97 | |
| 98 | /* Test detecting an invalid FDT */ |
| 99 | fdt[0] = 123; |
| 100 | set_working_fdt_addr(addr); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 101 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 102 | ut_asserteq(1, run_commandf("fdt addr")); |
| 103 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
| 104 | ut_assertok(ut_check_console_end(uts)); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC); |
| 109 | |
| 110 | /* Test 'fdt addr' resizing an fdt */ |
| 111 | static int fdt_test_resize(struct unit_test_state *uts) |
| 112 | { |
| 113 | char fdt[256]; |
| 114 | const int newsize = sizeof(fdt) / 2; |
| 115 | ulong addr; |
| 116 | |
| 117 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt))); |
| 118 | addr = map_to_sysmem(fdt); |
| 119 | set_working_fdt_addr(addr); |
| 120 | |
| 121 | /* Test setting and resizing the working FDT to a larger size */ |
| 122 | ut_assertok(console_record_reset_enable()); |
| 123 | ut_assertok(run_commandf("fdt addr %08x %x", addr, newsize)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 124 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 125 | ut_assertok(ut_check_console_end(uts)); |
| 126 | |
| 127 | /* Try shrinking it */ |
| 128 | ut_assertok(run_commandf("fdt addr %08x %x", addr, sizeof(fdt) / 4)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 129 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 130 | ut_assert_nextline("New length %d < existing length %d, ignoring", |
| 131 | (int)sizeof(fdt) / 4, newsize); |
| 132 | ut_assertok(ut_check_console_end(uts)); |
| 133 | |
| 134 | /* ...quietly */ |
| 135 | ut_assertok(run_commandf("fdt addr -q %08x %x", addr, sizeof(fdt) / 4)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 136 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 137 | ut_assertok(ut_check_console_end(uts)); |
| 138 | |
| 139 | /* We cannot easily provoke errors in fdt_open_into(), so ignore that */ |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC); |
| 144 | |
| 145 | int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 146 | { |
| 147 | struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test); |
| 148 | const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test); |
| 149 | |
| 150 | return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv); |
| 151 | } |