Johan Jonker | c9a97f6 | 2023-10-18 16:01:59 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Test derived from: |
| 4 | * /test/dm/host.c |
| 5 | * Copyright 2022 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | * |
| 8 | * Copyright (C) 2023 Johan Jonker <jbx6244@gmail.com> |
| 9 | */ |
| 10 | |
Johan Jonker | c9a97f6 | 2023-10-18 16:01:59 +0200 | [diff] [blame] | 11 | #include <blk.h> |
| 12 | #include <dm.h> |
| 13 | #include <fs.h> |
| 14 | #include <rkmtd.h> |
| 15 | #include <asm/test.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/test.h> |
| 18 | #include <test/test.h> |
| 19 | #include <test/ut.h> |
| 20 | |
| 21 | #define RW_BUF_SIZE 12 * 512 |
| 22 | |
| 23 | /* Basic test of the RKMTD interface */ |
| 24 | static int dm_test_rkmtd(struct unit_test_state *uts) |
| 25 | { |
| 26 | struct udevice *dev, *part, *chk, *blk; |
| 27 | char write[RW_BUF_SIZE], read[RW_BUF_SIZE]; |
| 28 | static const char label[] = "test"; |
| 29 | struct rkmtd_dev *plat; |
| 30 | struct blk_desc *desc; |
| 31 | struct sector0 *sec0; |
| 32 | int i; |
| 33 | |
| 34 | ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_RKMTD, &dev)); |
| 35 | ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part)); |
| 36 | |
| 37 | ut_assertok(rkmtd_create_device(label, &dev)); |
| 38 | |
| 39 | /* Check that the plat data has been allocated */ |
| 40 | plat = dev_get_plat(dev); |
| 41 | ut_asserteq_str("test", plat->label); |
| 42 | ut_assert(label != plat->label); |
| 43 | |
| 44 | /* Attach RKMTD driver */ |
| 45 | ut_assertok(rkmtd_attach(dev)); |
| 46 | ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk)); |
| 47 | ut_asserteq_ptr(chk, dev); |
| 48 | |
| 49 | /* Get RKMTD block device */ |
| 50 | ut_assertok(blk_get_from_parent(dev, &blk)); |
| 51 | ut_assertok(device_probe(blk)); |
| 52 | |
| 53 | /* There should be a GPT partition table in this device */ |
| 54 | ut_asserteq(0, uclass_first_device_err(UCLASS_PARTITION, &part)); |
| 55 | |
| 56 | /* Write a boot block and verify that we get the same data back */ |
| 57 | desc = dev_get_uclass_plat(blk); |
| 58 | ut_asserteq(true, desc->removable); |
| 59 | ut_asserteq(LBA, desc->lba); |
| 60 | |
| 61 | memset(write, '\0', BLK_SIZE); |
| 62 | |
| 63 | for (i = BLK_SIZE; i < sizeof(write); i++) |
| 64 | write[i] = i; |
| 65 | |
| 66 | sec0 = (struct sector0 *)write; |
| 67 | sec0->magic = 0x0FF0AA55; |
| 68 | sec0->rc4_flag = 0; |
| 69 | sec0->boot_code1_offset = 4; |
| 70 | sec0->boot_code2_offset = 4; |
| 71 | sec0->flash_data_size = 4; |
| 72 | sec0->flash_boot_size = 8; |
| 73 | |
| 74 | rkmtd_rc4(write, 512); |
| 75 | ut_asserteq(RK_TAG, sec0->magic); |
| 76 | |
| 77 | ut_asserteq(12, blk_dwrite(desc, 64, 12, write)); |
| 78 | ut_asserteq(12, blk_dread(desc, 64, 12, read)); |
| 79 | ut_asserteq_mem(write, read, RW_BUF_SIZE); |
| 80 | |
| 81 | ut_assertok(rkmtd_detach(dev)); |
| 82 | |
| 83 | ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk)); |
| 84 | ut_assertok(device_unbind(dev)); |
| 85 | |
| 86 | return 0; |
| 87 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 88 | DM_TEST(dm_test_rkmtd, UTF_SCAN_FDT); |
Johan Jonker | c9a97f6 | 2023-10-18 16:01:59 +0200 | [diff] [blame] | 89 | |
| 90 | /* Reusing the same label should work */ |
| 91 | static int dm_test_rkmtd_dup(struct unit_test_state *uts) |
| 92 | { |
| 93 | static const char label[] = "test"; |
| 94 | struct udevice *dev, *chk; |
| 95 | |
| 96 | /* Create a RKMTD device with label "test" */ |
| 97 | ut_asserteq(0, uclass_id_count(UCLASS_RKMTD)); |
| 98 | ut_assertok(rkmtd_create_device(label, &dev)); |
| 99 | ut_assertok(rkmtd_attach(dev)); |
| 100 | ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk)); |
| 101 | ut_asserteq_ptr(chk, dev); |
| 102 | ut_asserteq(1, uclass_id_count(UCLASS_RKMTD)); |
| 103 | |
| 104 | /* Create another device with the same label (should remove old one) */ |
| 105 | ut_assertok(rkmtd_create_device(label, &dev)); |
| 106 | ut_assertok(rkmtd_attach(dev)); |
| 107 | ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &chk)); |
| 108 | ut_asserteq_ptr(chk, dev); |
| 109 | |
| 110 | /* Make sure there is still only one device */ |
| 111 | ut_asserteq(1, uclass_id_count(UCLASS_RKMTD)); |
| 112 | |
| 113 | return 0; |
| 114 | } |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 115 | DM_TEST(dm_test_rkmtd_dup, UTF_SCAN_FDT); |
Johan Jonker | c9a97f6 | 2023-10-18 16:01:59 +0200 | [diff] [blame] | 116 | |
| 117 | /* Basic test of the 'rkmtd' command */ |
| 118 | static int dm_test_rkmtd_cmd(struct unit_test_state *uts) |
| 119 | { |
| 120 | struct udevice *dev, *blk; |
| 121 | struct blk_desc *desc; |
| 122 | |
| 123 | /* First check 'rkmtd info' with binding */ |
| 124 | ut_assertok(run_command("rkmtd info", 0)); |
| 125 | ut_assert_nextline("dev blocks label "); |
| 126 | ut_assert_console_end(); |
| 127 | |
| 128 | /* Bind device 1 */ |
| 129 | ut_assertok(run_commandf("rkmtd bind test1")); |
| 130 | ut_assertok(uclass_first_device_err(UCLASS_RKMTD, &dev)); |
| 131 | ut_assertok(blk_get_from_parent(dev, &blk)); |
| 132 | desc = dev_get_uclass_plat(blk); |
| 133 | |
| 134 | ut_assertok(run_command("rkmtd info", 0)); |
| 135 | ut_assert_nextline("dev blocks label "); |
| 136 | ut_assert_nextline(" 0 609 test1 "); |
| 137 | ut_assert_console_end(); |
| 138 | |
| 139 | /* Bind device 2 */ |
| 140 | ut_assertok(run_commandf("rkmtd bind test2")); |
| 141 | ut_assertok(uclass_next_device_err(&dev)); |
| 142 | ut_assertok(blk_get_from_parent(dev, &blk)); |
| 143 | desc = dev_get_uclass_plat(blk); |
| 144 | |
| 145 | ut_assertok(run_command("rkmtd info", 0)); |
| 146 | ut_assert_nextline("dev blocks label "); |
| 147 | ut_assert_nextline(" 0 609 test1 "); |
| 148 | ut_assert_nextline(" 1 609 test2 "); |
| 149 | ut_assert_console_end(); |
| 150 | |
| 151 | ut_asserteq(1, run_command("rkmtd info test", 0)); |
| 152 | ut_assert_nextline("No such device 'test'"); |
| 153 | ut_assert_console_end(); |
| 154 | |
| 155 | ut_assertok(run_command("rkmtd info test2", 0)); |
| 156 | ut_assert_nextline("dev blocks label "); |
| 157 | ut_assert_nextline(" 1 609 test2 "); |
| 158 | ut_assert_console_end(); |
| 159 | |
| 160 | /* Check 'rkmtd dev' */ |
| 161 | ut_asserteq(1, run_command("rkmtd dev", 0)); |
| 162 | ut_assert_nextline("No current rkmtd device"); |
| 163 | ut_assert_console_end(); |
| 164 | |
| 165 | ut_asserteq(1, run_command("rkmtd dev missing", 0)); |
| 166 | ut_assert_nextline("No such device 'missing'"); |
| 167 | ut_assert_console_end(); |
| 168 | |
| 169 | ut_assertok(run_command("rkmtd dev test2", 0)); |
| 170 | ut_assert_console_end(); |
| 171 | |
| 172 | ut_assertok(run_command("rkmtd dev", 0)); |
| 173 | ut_assert_nextline("Current rkmtd device: 1: test2"); |
| 174 | ut_assert_console_end(); |
| 175 | |
| 176 | /* Try a numerical label */ |
| 177 | ut_assertok(run_command("rkmtd dev 0", 0)); |
| 178 | ut_assert_console_end(); |
| 179 | |
| 180 | ut_assertok(run_command("rkmtd dev", 0)); |
| 181 | ut_assert_nextline("Current rkmtd device: 0: test1"); |
| 182 | ut_assert_console_end(); |
| 183 | |
| 184 | /* Remove one of the bindings */ |
| 185 | ut_assertok(run_commandf("rkmtd unbind test1")); |
| 186 | |
| 187 | /* There should now be no current device */ |
| 188 | ut_asserteq(1, run_command("rkmtd dev", 0)); |
| 189 | ut_assert_nextline("No current rkmtd device"); |
| 190 | ut_assert_console_end(); |
| 191 | |
| 192 | ut_assertok(run_command("rkmtd info", 0)); |
| 193 | ut_assert_nextline("dev blocks label "); |
| 194 | ut_assert_nextline(" 1 609 test2 "); |
| 195 | ut_assert_console_end(); |
| 196 | |
| 197 | return 0; |
| 198 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 199 | DM_TEST(dm_test_rkmtd_cmd, UTF_SCAN_FDT | UTF_CONSOLE); |