source: rtems/testsuites/libtests/flashdisk01/init.c @ 721fe34

4.11
Last change on this file since 721fe34 was 721fe34, checked in by Joel Sherrill <joel.sherrill@…>, on May 31, 2012 at 8:34:36 PM

Fix C files which had two semi-colons at EOL

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