source: rtems/testsuites/libtests/block09/init.c @ c4b8b147

5
Last change on this file since c4b8b147 was c4b8b147, checked in by Sebastian Huber <sebastian.huber@…>, on 11/03/17 at 07:35:38

tests: Use simple console driver

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[64734fc]1/**
2 * @file
3 *
4 * @ingroup test_bdbuf
5 *
6 * @brief Bdbuf test for disk driver error handling..
7 */
8
9/*
[796967c]10 * Copyright (c) 2009-2012 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
[64734fc]17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
[c499856]20 * http://www.rtems.org/license/LICENSE.
[64734fc]21 */
22
[7d3f9c6]23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
[24d0ee57]27#include "tmacros.h"
28
[64734fc]29#include <assert.h>
30#include <errno.h>
[4cf41fd]31#include <stdlib.h>
[64734fc]32
33#include <rtems.h>
34#include <rtems/bdbuf.h>
35#include <rtems/diskdevs.h>
[f8b2eb03]36#include <rtems/test.h>
[64734fc]37
[dc0eed7]38#include <bsp.h>
39
[f8b2eb03]40const char rtems_test_name[] = "BLOCK 9";
41
[4c86e3d]42/* forward declarations to avoid warnings */
43static rtems_task Init(rtems_task_argument argument);
44
[64734fc]45#define ASSERT_SC(sc) assert((sc) == RTEMS_SUCCESSFUL)
46
47#define BLOCK_SIZE sizeof(char)
48
49#define BLOCK_COUNT 4
50
51#define BLOCK_READ_IO_ERROR 0
52#define BLOCK_READ_UNSATISFIED 1
53#define BLOCK_READ_SUCCESSFUL 2
54#define BLOCK_WRITE_IO_ERROR 3
55
56static char disk_data [BLOCK_COUNT];
57
58static const rtems_driver_address_table disk_ops = {
59  .initialization_entry = NULL,
60  RTEMS_GENERIC_BLOCK_DEVICE_DRIVER_ENTRIES
61};
62
63static int disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg)
64{
65  rtems_status_code sc = RTEMS_SUCCESSFUL;
66
67  if (req == RTEMS_BLKIO_REQUEST) {
68    rtems_blkdev_request *r = arg;
69    uint32_t i = 0;
70
71    for (i = 0; i < r->bufnum; ++i) {
72      rtems_blkdev_sg_buffer *sg = &r->bufs [i];
73      char *buf = sg->buffer;
74
[9f0a68c]75      if (sg->length == 1) {
76        switch (r->req) {
77          case RTEMS_BLKDEV_REQ_READ:
78            switch (sg->block) {
79              case BLOCK_READ_IO_ERROR:
80                sc = RTEMS_IO_ERROR;
81                break;
82              case BLOCK_READ_UNSATISFIED:
83                sc = RTEMS_UNSATISFIED;
84                break;
85              case BLOCK_READ_SUCCESSFUL:
86              case BLOCK_WRITE_IO_ERROR:
87                *buf = disk_data [sg->block];
88                break;
89              default:
90                sc = RTEMS_IO_ERROR;
91                break;
92            }
93            break;
94          case RTEMS_BLKDEV_REQ_WRITE:
95            switch (sg->block) {
96              case BLOCK_READ_IO_ERROR:
97              case BLOCK_READ_UNSATISFIED:
98              case BLOCK_READ_SUCCESSFUL:
99                disk_data [sg->block] = *buf;
100                break;
101              case BLOCK_WRITE_IO_ERROR:
102                sc = RTEMS_IO_ERROR;
103                break;
104              default:
105                sc = RTEMS_IO_ERROR;
106                break;
107            }
108            break;
109          default:
110            sc = RTEMS_IO_ERROR;
111            break;
112        }
113      } else {
114        sc = RTEMS_IO_ERROR;
[64734fc]115      }
116    }
117
[9f0a68c]118    rtems_blkdev_request_done(r, sc);
[64734fc]119
120    return 0;
121  } else {
122    return rtems_blkdev_ioctl(dd, req, arg);
123  }
124}
125
[796967c]126static rtems_status_code disk_register(
[64734fc]127  uint32_t block_size,
128  rtems_blkdev_bnum block_count,
129  dev_t *dev_ptr
130)
131{
132  rtems_status_code sc = RTEMS_SUCCESSFUL;
133  rtems_device_major_number major = 0;
134  dev_t dev = 0;
135
136  sc = rtems_io_register_driver(0, &disk_ops, &major);
137  ASSERT_SC(sc);
138
139  dev = rtems_filesystem_make_dev_t(major, 0);
140
141  sc = rtems_disk_create_phys(
142    dev,
143    block_size,
144    block_count,
145    disk_ioctl,
146    NULL,
147    NULL
148  );
149  ASSERT_SC(sc);
150
151  *dev_ptr = dev;
152
153  return RTEMS_SUCCESSFUL;
154}
155
156static void check_read(
[40284de]157  rtems_disk_device *dd,
[64734fc]158  rtems_blkdev_bnum block,
159  rtems_status_code expected_sc
160)
161{
162  rtems_status_code sc = RTEMS_SUCCESSFUL;
163  rtems_bdbuf_buffer *bd = NULL;
164
[796967c]165  sc = rtems_bdbuf_read(dd, block, &bd);
[64734fc]166  assert(sc == expected_sc);
167
168  if (sc == RTEMS_SUCCESSFUL) {
169    sc = rtems_bdbuf_release(bd);
170    ASSERT_SC(sc);
171  }
172}
173
174static rtems_task Init(rtems_task_argument argument)
175{
176  rtems_status_code sc = RTEMS_SUCCESSFUL;
177  rtems_bdbuf_buffer *bd = NULL;
178  dev_t dev = 0;
[796967c]179  rtems_disk_device *dd = NULL;
[64734fc]180
[24d0ee57]181  TEST_BEGIN();
[64734fc]182
183  sc = rtems_disk_io_initialize();
184  ASSERT_SC(sc);
185
186  sc = disk_register(BLOCK_SIZE, BLOCK_COUNT, &dev);
187  ASSERT_SC(sc);
188
[796967c]189  dd = rtems_disk_obtain(dev);
190  assert(dd != NULL);
191
192  check_read(dd, BLOCK_READ_IO_ERROR, RTEMS_IO_ERROR);
193  check_read(dd, BLOCK_READ_UNSATISFIED, RTEMS_UNSATISFIED);
194  check_read(dd, BLOCK_READ_SUCCESSFUL, RTEMS_SUCCESSFUL);
195  check_read(dd, BLOCK_WRITE_IO_ERROR, RTEMS_SUCCESSFUL);
[64734fc]196
197  /* Check write IO error */
198
[796967c]199  sc = rtems_bdbuf_read(dd, BLOCK_WRITE_IO_ERROR, &bd);
[64734fc]200  ASSERT_SC(sc);
201
202  bd->buffer [0] = 1;
203
204  sc = rtems_bdbuf_sync(bd);
205  ASSERT_SC(sc);
206
[796967c]207  sc = rtems_bdbuf_read(dd, BLOCK_WRITE_IO_ERROR, &bd);
[64734fc]208  ASSERT_SC(sc);
209
210  assert(bd->buffer [0] == 0);
211
212  sc = rtems_bdbuf_release(bd);
213  ASSERT_SC(sc);
214
215  /* Check write to deleted disk */
216
[796967c]217  sc = rtems_bdbuf_read(dd, BLOCK_READ_SUCCESSFUL, &bd);
[64734fc]218  ASSERT_SC(sc);
219
220  sc = rtems_disk_delete(dev);
221  ASSERT_SC(sc);
222
223  sc = rtems_bdbuf_sync(bd);
224  ASSERT_SC(sc);
225
[796967c]226  sc = rtems_disk_release(dd);
227  ASSERT_SC(sc);
228
[24d0ee57]229  TEST_END();
[64734fc]230
231  exit(0);
232}
233
234#define CONFIGURE_INIT
235
236#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
[c4b8b147]237#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
[64734fc]238#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
239
[b6911069]240#define CONFIGURE_MAXIMUM_TASKS 1
[64734fc]241#define CONFIGURE_MAXIMUM_DRIVERS 4
242
[f8b2eb03]243#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
244
[64734fc]245#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
246
247#define CONFIGURE_BDBUF_BUFFER_MIN_SIZE BLOCK_SIZE
248#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE BLOCK_SIZE
249#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (BLOCK_SIZE * BLOCK_COUNT)
250
251#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.