source: rtems/testsuites/libtests/flashdisk01/init.c @ 3324383c

4.11
Last change on this file since 3324383c was 3324383c, checked in by Joel Sherrill <joel.sherrill@…>, on May 5, 2014 at 2:47:30 PM

testsuites: Remove BSP_SMALL_MEMORY

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include "tmacros.h"
20
21#include <sys/stat.h>
22#include <errno.h>
23#include <string.h>
24#include <stdint.h>
25#include <unistd.h>
26#include <fcntl.h>
27
28#include <rtems/flashdisk.h>
29#include <rtems/libio.h>
30#include <rtems/blkdev.h>
31#include <rtems/rtems-rfs-format.h>
32
33#include "test-file-system.h"
34
35const char rtems_test_name[] = "FLASHDISK 1";
36
37/* forward declarations to avoid warnings */
38static rtems_task Init(rtems_task_argument argument);
39
40#define FLASHDISK_CONFIG_COUNT 1
41
42#define FLASHDISK_DEVICE_COUNT 1
43
44#define FLASHDISK_SEGMENT_COUNT 4U
45
46#define FLASHDISK_SEGMENT_SIZE (16 * 1024)
47
48#define FLASHDISK_BLOCK_SIZE 512U
49
50#define FLASHDISK_BLOCKS_PER_SEGMENT \
51  (FLASHDISK_SEGMENT_SIZE / FLASHDISK_BLOCK_SIZE)
52
53#define FLASHDISK_SIZE \
54  (FLASHDISK_SEGMENT_COUNT * FLASHDISK_SEGMENT_SIZE)
55
56static const rtems_rfs_format_config rfs_config;
57
58static const char device [] = "/dev/fdda";
59
60static const char mnt [] = "/mnt";
61
62static const char file [] = "/mnt/file";
63
64static uint8_t flashdisk_data [FLASHDISK_SIZE];
65
66static void flashdisk_print_status(const char *disk_path)
67{
68  int rv;
69  int fd = open(disk_path, O_RDWR);
70  rtems_test_assert(fd >= 0);
71
72  rv = ioctl(fd, RTEMS_FDISK_IOCTL_PRINT_STATUS);
73  rtems_test_assert(rv == 0);
74
75  rv = close(fd);
76  rtems_test_assert(rv == 0);
77}
78
79static int test_rfs_mount_handler(
80  const char *disk_path,
81  const char *mount_path,
82  void *arg
83)
84{
85  return mount_and_make_target_path(
86    disk_path,
87    mount_path,
88    RTEMS_FILESYSTEM_TYPE_RFS,
89    RTEMS_FILESYSTEM_READ_WRITE,
90    NULL
91  );
92}
93
94static int test_rfs_format_handler(
95  const char *disk_path,
96  void *arg
97)
98{
99  flashdisk_print_status(disk_path);
100
101  rtems_test_assert(0);
102
103  errno = EIO;
104
105  return -1;
106}
107
108static const test_file_system_handler test_rfs_handler = {
109  .mount = test_rfs_mount_handler,
110  .format = test_rfs_format_handler
111};
112
113static void test(void)
114{
115  int rv;
116  const void *data = NULL;
117
118  rv = mkdir(mnt, S_IRWXU | S_IRWXG | S_IRWXO);
119  rtems_test_assert(rv == 0);
120
121  rv = rtems_rfs_format(device, &rfs_config);
122  rtems_test_assert(rv == 0);
123
124  rv = mount(
125    device,
126    mnt,
127    RTEMS_FILESYSTEM_TYPE_RFS,
128    RTEMS_FILESYSTEM_READ_WRITE,
129    data
130  );
131  rtems_test_assert(rv == 0);
132
133  rv = mknod(file, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0);
134  rtems_test_assert(rv == 0);
135
136  rv = unmount(mnt);
137  rtems_test_assert(rv == 0);
138
139  test_file_system_with_handler(
140    0,
141    device,
142    mnt,
143    &test_rfs_handler,
144    NULL
145  );
146
147  flashdisk_print_status(device);
148}
149
150static void Init(rtems_task_argument arg)
151{
152  TEST_BEGIN();
153
154  test();
155
156  TEST_END();
157
158  rtems_test_exit(0);
159}
160
161static void erase_device(void)
162{
163  memset(&flashdisk_data [0], 0xff, FLASHDISK_SIZE);
164}
165
166static rtems_device_driver flashdisk_initialize(
167  rtems_device_major_number major,
168  rtems_device_minor_number minor,
169  void *arg
170)
171{
172  erase_device();
173
174  return rtems_fdisk_initialize(major, minor, arg);
175}
176
177static uint8_t *get_data_pointer(
178  const rtems_fdisk_segment_desc *sd,
179  uint32_t segment,
180  uint32_t offset
181)
182{
183  offset += sd->offset + (segment - sd->segment) * sd->size;
184
185  return &flashdisk_data [offset];
186}
187
188static int flashdisk_read(
189  const rtems_fdisk_segment_desc *sd,
190  uint32_t device,
191  uint32_t segment,
192  uint32_t offset,
193  void *buffer,
194  uint32_t size
195)
196{
197  int eno = 0;
198  const uint8_t *data = get_data_pointer(sd, segment, offset);
199
200  memcpy(buffer, data, size);
201
202  return eno;
203}
204
205static int flashdisk_write(
206  const rtems_fdisk_segment_desc *sd,
207  uint32_t device,
208  uint32_t segment,
209  uint32_t offset,
210  const void *buffer,
211  uint32_t size
212)
213{
214  int eno = 0;
215  uint8_t *data = get_data_pointer(sd, segment, offset);
216
217  memcpy(data, buffer, size);
218
219  return eno;
220}
221
222static int flashdisk_blank(
223  const rtems_fdisk_segment_desc *sd,
224  uint32_t device,
225  uint32_t segment,
226  uint32_t offset,
227  uint32_t size
228)
229{
230  int eno = 0;
231  const uint8_t *current = get_data_pointer(sd, segment, offset);
232  const uint8_t *end = current + size;
233
234  while (eno == 0 && current != end) {
235    if (*current != 0xff) {
236      eno = EIO;
237    }
238    ++current;
239  }
240
241  return eno;
242}
243
244static int flashdisk_verify(
245  const rtems_fdisk_segment_desc *sd,
246  uint32_t device,
247  uint32_t segment,
248  uint32_t offset,
249  const void *buffer,
250  uint32_t size
251)
252{
253  int eno = 0;
254  uint8_t *data = get_data_pointer(sd, segment, offset);
255
256  if (memcmp(data, buffer, size) != 0) {
257    eno = EIO;
258  }
259
260  return eno;
261}
262
263static int flashdisk_erase(
264  const rtems_fdisk_segment_desc *sd,
265  uint32_t device,
266  uint32_t segment
267)
268{
269  int eno = 0;
270  uint8_t *data = get_data_pointer(sd, segment, 0);
271
272  memset(data, 0xff, sd->size);
273
274  return eno;
275}
276
277static int flashdisk_erase_device(
278  const rtems_fdisk_device_desc *sd,
279  uint32_t device
280)
281{
282  int eno = 0;
283
284  erase_device();
285
286  return eno;
287}
288
289static const rtems_fdisk_segment_desc flashdisk_segment_desc = {
290  .count = FLASHDISK_SEGMENT_COUNT,
291  .segment = 0,
292  .offset = 0,
293  .size = FLASHDISK_SEGMENT_SIZE
294};
295
296static const rtems_fdisk_driver_handlers flashdisk_ops = {
297  .read = flashdisk_read,
298  .write = flashdisk_write,
299  .blank = flashdisk_blank,
300  .verify = flashdisk_verify,
301  .erase = flashdisk_erase,
302  .erase_device = flashdisk_erase_device
303};
304
305static const rtems_fdisk_device_desc flashdisk_device = {
306  .segment_count = 1,
307  .segments = &flashdisk_segment_desc,
308  .flash_ops = &flashdisk_ops
309};
310
311const rtems_flashdisk_config
312rtems_flashdisk_configuration [FLASHDISK_CONFIG_COUNT] = {
313  {
314    .block_size = FLASHDISK_BLOCK_SIZE,
315    .device_count = FLASHDISK_DEVICE_COUNT,
316    .devices = &flashdisk_device,
317    .flags = RTEMS_FDISK_CHECK_PAGES
318      | RTEMS_FDISK_BLANK_CHECK_BEFORE_WRITE,
319    .unavail_blocks = FLASHDISK_BLOCKS_PER_SEGMENT,
320    .compact_segs = 2,
321    .avail_compact_segs = 1,
322    .info_level = 0
323  }
324};
325
326uint32_t rtems_flashdisk_configuration_size = FLASHDISK_CONFIG_COUNT;
327
328#define FLASHDISK_DRIVER { \
329  .initialization_entry = flashdisk_initialize, \
330  .open_entry = rtems_blkdev_generic_open, \
331  .close_entry = rtems_blkdev_generic_close, \
332  .read_entry = rtems_blkdev_generic_read, \
333  .write_entry = rtems_blkdev_generic_write, \
334  .control_entry = rtems_blkdev_generic_ioctl \
335}
336
337#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
338#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
339#define CONFIGURE_APPLICATION_EXTRA_DRIVERS FLASHDISK_DRIVER
340#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
341
342#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 6
343
344#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
345#define CONFIGURE_FILESYSTEM_RFS
346
347#define CONFIGURE_MAXIMUM_TASKS 2
348#define CONFIGURE_MAXIMUM_SEMAPHORES 1
349
350#define CONFIGURE_MINIMUM_TASK_STACK_SIZE (32U * 1024U)
351
352#define CONFIGURE_EXTRA_TASK_STACKS (8 * 1024)
353
354#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
355
356#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
357
358#define CONFIGURE_INIT
359
360#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.