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

4.11
Last change on this file since faa4e4c was faa4e4c, checked in by Joel Sherrill <joel.sherrill@…>, on Apr 21, 2014 at 2:08:34 PM

flashdisk01: Honor BSP_SMALL_MEMORY

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