source: rtems/testsuites/libtests/block01/init.c @ 24b94c4

Last change on this file since 24b94c4 was 24b94c4, checked in by Sebastian Huber <sebastian.huber@…>, on Jul 30, 2018 at 4:39:09 AM

ramdisk: Use rtems_blkdev_create()

Update #3358.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup tests
5 *
6 * @brief Block device tests.
7 */
8
9/*
10 * Copyright (c) 2009, 2018 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <sys/stat.h>
28#include <stdio.h>
29#include <fcntl.h>
30#include "tmacros.h"
31
32#include <rtems.h>
33#include <rtems/ramdisk.h>
34#include <rtems/diskdevs.h>
35
36const char rtems_test_name[] = "BLOCK 1";
37
38#define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL)
39
40#define ASSERT_SC_EQ(sc, sc_expected) rtems_test_assert((sc) == (sc_expected))
41
42#define BLOCK_SIZE 512U
43
44#define BLOCK_COUNT 16U
45
46static const rtems_driver_address_table ramdisk_ops = {
47  .initialization_entry = NULL,
48  RTEMS_GENERIC_BLOCK_DEVICE_DRIVER_ENTRIES
49};
50
51static void test_block_io_control_api(dev_t dev, ramdisk *rd)
52{
53  rtems_status_code sc = RTEMS_SUCCESSFUL;
54  rtems_disk_device *dd = NULL;
55  rtems_disk_device *fd_dd = NULL;
56  int fd = -1;
57  int rv = -1;
58  uint32_t value = 0;
59  rtems_blkdev_bnum block_count = 0;
60
61  sc = rtems_disk_create_phys(dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
62  ASSERT_SC(sc);
63
64  dd = rtems_disk_obtain(dev);
65  rtems_test_assert(dd != NULL);
66
67  fd = open("/dev/rda", O_RDWR);
68  rtems_test_assert(fd >= 0);
69
70  value = 0;
71  rv = rtems_disk_fd_get_media_block_size(fd, &value);
72  rtems_test_assert(rv == 0);
73  rtems_test_assert(value == BLOCK_SIZE);
74
75  value = 0;
76  rv = rtems_disk_fd_get_block_size(fd, &value);
77  rtems_test_assert(rv == 0);
78  rtems_test_assert(value == BLOCK_SIZE);
79
80  value = 1024;
81  rv = rtems_disk_fd_set_block_size(fd, value);
82  rtems_test_assert(rv == 0);
83
84  value = 0;
85  rv = rtems_disk_fd_get_block_size(fd, &value);
86  rtems_test_assert(rv == 0);
87  rtems_test_assert(value == 1024);
88
89  block_count = 0;
90  rv = rtems_disk_fd_get_block_count(fd, &block_count);
91  rtems_test_assert(rv == 0);
92  rtems_test_assert(block_count == BLOCK_COUNT);
93
94  rv = rtems_disk_fd_get_disk_device(fd, &fd_dd);
95  rtems_test_assert(rv == 0);
96  rtems_test_assert(fd_dd == dd);
97
98  rv = rtems_disk_fd_sync(fd);
99  rtems_test_assert(rv == 0);
100
101  rv = close(fd);
102  rtems_test_assert(rv == 0);
103
104  sc = rtems_disk_release(dd);
105  ASSERT_SC(sc);
106
107  sc = rtems_disk_delete(dev);
108  ASSERT_SC(sc);
109}
110
111static void test_diskdevs(void)
112{
113  rtems_status_code sc = RTEMS_SUCCESSFUL;
114  rtems_device_major_number major = 0;
115  rtems_device_minor_number minor = 0;
116  rtems_disk_device *physical_dd = NULL;
117  rtems_disk_device *logical_dd = NULL;
118  rtems_disk_device *dd = NULL;
119  dev_t physical_dev = 0;
120  dev_t logical_dev = 0;
121  dev_t logical_2_dev = 0;
122  dev_t const big_major_dev = rtems_filesystem_make_dev_t((rtems_device_major_number) -2, 0);
123  dev_t const big_minor_dev = rtems_filesystem_make_dev_t(0, (rtems_device_minor_number) -2);
124  ramdisk *const rd = ramdisk_allocate(NULL, BLOCK_SIZE, BLOCK_COUNT, false);
125
126  rtems_test_assert(rd != NULL);
127
128  sc = rtems_disk_io_initialize();
129  ASSERT_SC(sc);
130
131  sc = rtems_io_register_driver(0, &ramdisk_ops, &major);
132  ASSERT_SC(sc);
133
134  physical_dev = rtems_filesystem_make_dev_t(major, minor);
135  logical_dev = rtems_filesystem_make_dev_t(major, minor + 1);
136  logical_2_dev = rtems_filesystem_make_dev_t(major, minor + 2);
137
138  /* Consistency checks for physical disks creation */
139
140  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, NULL, rd, "/dev/rda");
141  ASSERT_SC_EQ(sc, RTEMS_INVALID_ADDRESS);
142
143  sc = rtems_disk_create_phys(physical_dev, 0, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
144  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
145
146  sc = rtems_disk_create_phys(big_major_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
147  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
148
149  sc = rtems_disk_create_phys(big_minor_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
150  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
151
152  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, NULL);
153  ASSERT_SC(sc);
154
155  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, NULL);
156  ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE);
157
158  sc = rtems_disk_delete(physical_dev);
159  ASSERT_SC(sc);
160
161  /* Consistency checks for logical disks creation */
162
163  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
164  ASSERT_SC_EQ(sc, RTEMS_INVALID_ID);
165
166  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
167  ASSERT_SC(sc);
168
169  sc = rtems_disk_create_log(big_major_dev, physical_dev, 0, 1, "/dev/rda1");
170  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
171
172  sc = rtems_disk_create_log(big_minor_dev, physical_dev, 0, 1, "/dev/rda1");
173  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
174
175  sc = rtems_disk_create_log(logical_dev, physical_dev, BLOCK_COUNT, 0, "/dev/rda1");
176  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
177
178  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, BLOCK_COUNT + 1, "/dev/rda1");
179  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
180
181  sc = rtems_disk_create_log(logical_dev, physical_dev, 1, BLOCK_COUNT, "/dev/rda1");
182  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
183
184  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
185  ASSERT_SC(sc);
186
187  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
188  ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE);
189
190  sc = rtems_disk_create_log(logical_2_dev, logical_dev, 0, 1, "/dev/rda2");
191  ASSERT_SC_EQ(sc, RTEMS_INVALID_ID);
192
193  sc = rtems_disk_delete(logical_dev);
194  ASSERT_SC(sc);
195
196  /* Consistency checks delete */
197
198  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
199  ASSERT_SC(sc);
200
201  physical_dd = rtems_disk_obtain(physical_dev);
202  rtems_test_assert(physical_dd != NULL && physical_dd->uses == 2);
203
204  sc = rtems_disk_release(physical_dd);
205  ASSERT_SC(sc);
206
207  logical_dd = rtems_disk_obtain(logical_dev);
208  rtems_test_assert(logical_dd != NULL && logical_dd->uses == 1);
209
210  sc = rtems_disk_delete(physical_dev);
211  ASSERT_SC(sc);
212
213  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
214  ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE);
215
216  dd = rtems_disk_obtain(physical_dev);
217  rtems_test_assert(dd == NULL);
218
219  dd = rtems_disk_obtain(logical_dev);
220  rtems_test_assert(dd == NULL);
221
222  sc = rtems_disk_release(logical_dd);
223  ASSERT_SC(sc);
224
225  /* Test block IO control API */
226
227  test_block_io_control_api(physical_dev, rd);
228
229  /* Cleanup */
230
231  sc = rtems_io_unregister_driver(major);
232  ASSERT_SC(sc);
233
234  ramdisk_free(rd);
235
236  sc = rtems_disk_io_done();
237  ASSERT_SC(sc);
238}
239
240static rtems_task Init(rtems_task_argument argument)
241{
242  TEST_BEGIN();
243
244  test_diskdevs();
245
246  TEST_END();
247
248  exit(0);
249}
250
251#define CONFIGURE_INIT
252
253#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
254#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
255#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
256
257#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
258
259#define CONFIGURE_MAXIMUM_TASKS 1
260#define CONFIGURE_MAXIMUM_DRIVERS 2
261
262#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
263
264#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
265
266#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.