1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup tests |
---|
7 | * |
---|
8 | * @brief Block device tests. |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * Copyright (C) 2009, 2018 embedded brains GmbH & Co. KG |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifdef HAVE_CONFIG_H |
---|
37 | #include "config.h" |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <fcntl.h> |
---|
43 | #include "tmacros.h" |
---|
44 | |
---|
45 | #include <rtems.h> |
---|
46 | #include <rtems/ramdisk.h> |
---|
47 | #include <rtems/diskdevs.h> |
---|
48 | |
---|
49 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
---|
50 | |
---|
51 | const char rtems_test_name[] = "BLOCK 1"; |
---|
52 | |
---|
53 | #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL) |
---|
54 | |
---|
55 | #define ASSERT_SC_EQ(sc, sc_expected) rtems_test_assert((sc) == (sc_expected)) |
---|
56 | |
---|
57 | #define BLOCK_SIZE 512U |
---|
58 | |
---|
59 | #define BLOCK_COUNT 16U |
---|
60 | |
---|
61 | static const rtems_driver_address_table ramdisk_ops = { |
---|
62 | .initialization_entry = NULL, |
---|
63 | RTEMS_GENERIC_BLOCK_DEVICE_DRIVER_ENTRIES |
---|
64 | }; |
---|
65 | |
---|
66 | static void test_block_io_control_api(dev_t dev, ramdisk *rd) |
---|
67 | { |
---|
68 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
69 | rtems_disk_device *dd = NULL; |
---|
70 | rtems_disk_device *fd_dd = NULL; |
---|
71 | int fd = -1; |
---|
72 | int rv = -1; |
---|
73 | uint32_t value = 0; |
---|
74 | rtems_blkdev_bnum block_count = 0; |
---|
75 | |
---|
76 | sc = rtems_disk_create_phys(dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda"); |
---|
77 | ASSERT_SC(sc); |
---|
78 | |
---|
79 | dd = rtems_disk_obtain(dev); |
---|
80 | rtems_test_assert(dd != NULL); |
---|
81 | |
---|
82 | fd = open("/dev/rda", O_RDWR); |
---|
83 | rtems_test_assert(fd >= 0); |
---|
84 | |
---|
85 | value = 0; |
---|
86 | rv = rtems_disk_fd_get_media_block_size(fd, &value); |
---|
87 | rtems_test_assert(rv == 0); |
---|
88 | rtems_test_assert(value == BLOCK_SIZE); |
---|
89 | |
---|
90 | value = 0; |
---|
91 | rv = rtems_disk_fd_get_block_size(fd, &value); |
---|
92 | rtems_test_assert(rv == 0); |
---|
93 | rtems_test_assert(value == BLOCK_SIZE); |
---|
94 | |
---|
95 | value = 1024; |
---|
96 | rv = rtems_disk_fd_set_block_size(fd, value); |
---|
97 | rtems_test_assert(rv == 0); |
---|
98 | |
---|
99 | value = 0; |
---|
100 | rv = rtems_disk_fd_get_block_size(fd, &value); |
---|
101 | rtems_test_assert(rv == 0); |
---|
102 | rtems_test_assert(value == 1024); |
---|
103 | |
---|
104 | block_count = 0; |
---|
105 | rv = rtems_disk_fd_get_block_count(fd, &block_count); |
---|
106 | rtems_test_assert(rv == 0); |
---|
107 | rtems_test_assert(block_count == BLOCK_COUNT); |
---|
108 | |
---|
109 | rv = rtems_disk_fd_get_disk_device(fd, &fd_dd); |
---|
110 | rtems_test_assert(rv == 0); |
---|
111 | rtems_test_assert(fd_dd == dd); |
---|
112 | |
---|
113 | rv = rtems_disk_fd_sync(fd); |
---|
114 | rtems_test_assert(rv == 0); |
---|
115 | |
---|
116 | rv = close(fd); |
---|
117 | rtems_test_assert(rv == 0); |
---|
118 | |
---|
119 | sc = rtems_disk_release(dd); |
---|
120 | ASSERT_SC(sc); |
---|
121 | |
---|
122 | sc = rtems_disk_delete(dev); |
---|
123 | ASSERT_SC(sc); |
---|
124 | } |
---|
125 | |
---|
126 | static void test_diskdevs(void) |
---|
127 | { |
---|
128 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
129 | rtems_device_major_number major = 0; |
---|
130 | rtems_device_minor_number minor = 0; |
---|
131 | rtems_disk_device *physical_dd = NULL; |
---|
132 | rtems_disk_device *logical_dd = NULL; |
---|
133 | rtems_disk_device *dd = NULL; |
---|
134 | dev_t physical_dev = 0; |
---|
135 | dev_t logical_dev = 0; |
---|
136 | dev_t logical_2_dev = 0; |
---|
137 | dev_t const big_major_dev = rtems_filesystem_make_dev_t((rtems_device_major_number) -2, 0); |
---|
138 | dev_t const big_minor_dev = rtems_filesystem_make_dev_t(0, (rtems_device_minor_number) -2); |
---|
139 | ramdisk *const rd = ramdisk_allocate(NULL, BLOCK_SIZE, BLOCK_COUNT, false); |
---|
140 | |
---|
141 | rtems_test_assert(rd != NULL); |
---|
142 | |
---|
143 | sc = rtems_disk_io_initialize(); |
---|
144 | ASSERT_SC(sc); |
---|
145 | |
---|
146 | sc = rtems_io_register_driver(0, &ramdisk_ops, &major); |
---|
147 | ASSERT_SC(sc); |
---|
148 | |
---|
149 | physical_dev = rtems_filesystem_make_dev_t(major, minor); |
---|
150 | logical_dev = rtems_filesystem_make_dev_t(major, minor + 1); |
---|
151 | logical_2_dev = rtems_filesystem_make_dev_t(major, minor + 2); |
---|
152 | |
---|
153 | /* Consistency checks for physical disks creation */ |
---|
154 | |
---|
155 | sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, NULL, rd, "/dev/rda"); |
---|
156 | ASSERT_SC_EQ(sc, RTEMS_INVALID_ADDRESS); |
---|
157 | |
---|
158 | sc = rtems_disk_create_phys(physical_dev, 0, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda"); |
---|
159 | ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER); |
---|
160 | |
---|
161 | sc = rtems_disk_create_phys(big_major_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda"); |
---|
162 | ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY); |
---|
163 | |
---|
164 | sc = rtems_disk_create_phys(big_minor_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda"); |
---|
165 | ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY); |
---|
166 | |
---|
167 | sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, NULL); |
---|
168 | ASSERT_SC(sc); |
---|
169 | |
---|
170 | sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, NULL); |
---|
171 | ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE); |
---|
172 | |
---|
173 | sc = rtems_disk_delete(physical_dev); |
---|
174 | ASSERT_SC(sc); |
---|
175 | |
---|
176 | /* Consistency checks for logical disks creation */ |
---|
177 | |
---|
178 | sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1"); |
---|
179 | ASSERT_SC_EQ(sc, RTEMS_INVALID_ID); |
---|
180 | |
---|
181 | sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda"); |
---|
182 | ASSERT_SC(sc); |
---|
183 | |
---|
184 | sc = rtems_disk_create_log(big_major_dev, physical_dev, 0, 1, "/dev/rda1"); |
---|
185 | ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY); |
---|
186 | |
---|
187 | sc = rtems_disk_create_log(big_minor_dev, physical_dev, 0, 1, "/dev/rda1"); |
---|
188 | ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY); |
---|
189 | |
---|
190 | sc = rtems_disk_create_log(logical_dev, physical_dev, BLOCK_COUNT, 0, "/dev/rda1"); |
---|
191 | ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER); |
---|
192 | |
---|
193 | sc = rtems_disk_create_log(logical_dev, physical_dev, 0, BLOCK_COUNT + 1, "/dev/rda1"); |
---|
194 | ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER); |
---|
195 | |
---|
196 | sc = rtems_disk_create_log(logical_dev, physical_dev, 1, BLOCK_COUNT, "/dev/rda1"); |
---|
197 | ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER); |
---|
198 | |
---|
199 | sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1"); |
---|
200 | ASSERT_SC(sc); |
---|
201 | |
---|
202 | sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1"); |
---|
203 | ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE); |
---|
204 | |
---|
205 | sc = rtems_disk_create_log(logical_2_dev, logical_dev, 0, 1, "/dev/rda2"); |
---|
206 | ASSERT_SC_EQ(sc, RTEMS_INVALID_ID); |
---|
207 | |
---|
208 | sc = rtems_disk_delete(logical_dev); |
---|
209 | ASSERT_SC(sc); |
---|
210 | |
---|
211 | /* Consistency checks delete */ |
---|
212 | |
---|
213 | sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1"); |
---|
214 | ASSERT_SC(sc); |
---|
215 | |
---|
216 | physical_dd = rtems_disk_obtain(physical_dev); |
---|
217 | rtems_test_assert(physical_dd != NULL && physical_dd->uses == 2); |
---|
218 | |
---|
219 | sc = rtems_disk_release(physical_dd); |
---|
220 | ASSERT_SC(sc); |
---|
221 | |
---|
222 | logical_dd = rtems_disk_obtain(logical_dev); |
---|
223 | rtems_test_assert(logical_dd != NULL && logical_dd->uses == 1); |
---|
224 | |
---|
225 | sc = rtems_disk_delete(physical_dev); |
---|
226 | ASSERT_SC(sc); |
---|
227 | |
---|
228 | sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda"); |
---|
229 | ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE); |
---|
230 | |
---|
231 | dd = rtems_disk_obtain(physical_dev); |
---|
232 | rtems_test_assert(dd == NULL); |
---|
233 | |
---|
234 | dd = rtems_disk_obtain(logical_dev); |
---|
235 | rtems_test_assert(dd == NULL); |
---|
236 | |
---|
237 | sc = rtems_disk_release(logical_dd); |
---|
238 | ASSERT_SC(sc); |
---|
239 | |
---|
240 | /* Test block IO control API */ |
---|
241 | |
---|
242 | test_block_io_control_api(physical_dev, rd); |
---|
243 | |
---|
244 | /* Cleanup */ |
---|
245 | |
---|
246 | sc = rtems_io_unregister_driver(major); |
---|
247 | ASSERT_SC(sc); |
---|
248 | |
---|
249 | ramdisk_free(rd); |
---|
250 | |
---|
251 | sc = rtems_disk_io_done(); |
---|
252 | ASSERT_SC(sc); |
---|
253 | } |
---|
254 | |
---|
255 | static rtems_task Init(rtems_task_argument argument) |
---|
256 | { |
---|
257 | TEST_BEGIN(); |
---|
258 | |
---|
259 | test_diskdevs(); |
---|
260 | |
---|
261 | TEST_END(); |
---|
262 | |
---|
263 | exit(0); |
---|
264 | } |
---|
265 | |
---|
266 | #define CONFIGURE_INIT |
---|
267 | |
---|
268 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
269 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
270 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
271 | |
---|
272 | #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
273 | |
---|
274 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
275 | #define CONFIGURE_MAXIMUM_DRIVERS 2 |
---|
276 | |
---|
277 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
278 | |
---|
279 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
280 | |
---|
281 | #include <rtems/confdefs.h> |
---|