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); |
| 58 | ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0)); |
| 59 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
| 60 | ut_assertok(ut_check_console_end(uts)); |
| 61 | |
| 62 | /* Set up a working FDT and try again */ |
| 63 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt))); |
| 64 | addr = map_to_sysmem(fdt); |
| 65 | set_working_fdt_addr(addr); |
| 66 | ut_assertok(run_command("fdt addr", 0)); |
| 67 | ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt)); |
| 68 | ut_assertok(ut_check_console_end(uts)); |
| 69 | |
| 70 | /* Set the working FDT */ |
| 71 | set_working_fdt_addr(0); |
| 72 | ut_assertok(run_commandf("fdt addr %08x", addr)); |
| 73 | ut_asserteq(addr, map_to_sysmem(working_fdt)); |
| 74 | ut_assertok(ut_check_console_end(uts)); |
| 75 | set_working_fdt_addr(0); |
| 76 | |
| 77 | /* Set the working FDT */ |
| 78 | fdt_blob = gd->fdt_blob; |
| 79 | gd->fdt_blob = NULL; |
| 80 | ret = run_commandf("fdt addr -c %08x", addr); |
| 81 | new_fdt = gd->fdt_blob; |
| 82 | gd->fdt_blob = fdt_blob; |
| 83 | ut_assertok(ret); |
| 84 | ut_asserteq(addr, map_to_sysmem(new_fdt)); |
| 85 | ut_assertok(ut_check_console_end(uts)); |
| 86 | |
| 87 | /* Test setting an invalid FDT */ |
| 88 | fdt[0] = 123; |
| 89 | ut_asserteq(1, run_commandf("fdt addr %08x", addr)); |
| 90 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
| 91 | ut_assertok(ut_check_console_end(uts)); |
| 92 | |
| 93 | /* Test detecting an invalid FDT */ |
| 94 | fdt[0] = 123; |
| 95 | set_working_fdt_addr(addr); |
| 96 | ut_asserteq(1, run_commandf("fdt addr")); |
| 97 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
| 98 | ut_assertok(ut_check_console_end(uts)); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC); |
| 103 | |
| 104 | /* Test 'fdt addr' resizing an fdt */ |
| 105 | static int fdt_test_resize(struct unit_test_state *uts) |
| 106 | { |
| 107 | char fdt[256]; |
| 108 | const int newsize = sizeof(fdt) / 2; |
| 109 | ulong addr; |
| 110 | |
| 111 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt))); |
| 112 | addr = map_to_sysmem(fdt); |
| 113 | set_working_fdt_addr(addr); |
| 114 | |
| 115 | /* Test setting and resizing the working FDT to a larger size */ |
| 116 | ut_assertok(console_record_reset_enable()); |
| 117 | ut_assertok(run_commandf("fdt addr %08x %x", addr, newsize)); |
| 118 | ut_assertok(ut_check_console_end(uts)); |
| 119 | |
| 120 | /* Try shrinking it */ |
| 121 | ut_assertok(run_commandf("fdt addr %08x %x", addr, sizeof(fdt) / 4)); |
| 122 | ut_assert_nextline("New length %d < existing length %d, ignoring", |
| 123 | (int)sizeof(fdt) / 4, newsize); |
| 124 | ut_assertok(ut_check_console_end(uts)); |
| 125 | |
| 126 | /* ...quietly */ |
| 127 | ut_assertok(run_commandf("fdt addr -q %08x %x", addr, sizeof(fdt) / 4)); |
| 128 | ut_assertok(ut_check_console_end(uts)); |
| 129 | |
| 130 | /* We cannot easily provoke errors in fdt_open_into(), so ignore that */ |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC); |
| 135 | |
| 136 | int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 137 | { |
| 138 | struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test); |
| 139 | const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test); |
| 140 | |
| 141 | return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv); |
| 142 | } |