Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:32

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2012, 2018 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include "tmacros.h"
0033 
0034 #include <sys/stat.h>
0035 #include <assert.h>
0036 #include <errno.h>
0037 #include <fcntl.h>
0038 #include <string.h>
0039 #include <unistd.h>
0040 
0041 #include <rtems/blkdev.h>
0042 #include <rtems/bdbuf.h>
0043 
0044 const char rtems_test_name[] = "BLOCK 12";
0045 
0046 #define BLOCK_COUNT 15
0047 
0048 #define DISK_PATH "/disk"
0049 
0050 static int block_access_counts [BLOCK_COUNT];
0051 
0052 static int expected_block_access_counts [BLOCK_COUNT] = {
0053   2, 0, 2, 0, 2, 0, 2, 2, 2, 0, 2, 0, 2, 0, 2
0054 };
0055 
0056 static const rtems_blkdev_bnum read_sequence [] = {
0057   0, 2, 4, 6, 8, 10, 12, 14, 7
0058 };
0059 
0060 static int test_disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg)
0061 {
0062   int rv = 0;
0063 
0064   if (req == RTEMS_BLKIO_REQUEST) {
0065     rtems_blkdev_request *breq = arg;
0066     rtems_blkdev_sg_buffer *sg = breq->bufs;
0067     uint32_t i;
0068 
0069     rtems_test_assert(breq->req == RTEMS_BLKDEV_REQ_READ);
0070 
0071     for (i = 0; i < breq->bufnum; ++i) {
0072       rtems_blkdev_bnum block = sg [i].block;
0073 
0074       rtems_test_assert(block < BLOCK_COUNT);
0075 
0076       ++block_access_counts [block];
0077     }
0078 
0079     rtems_blkdev_request_done(breq, RTEMS_SUCCESSFUL);
0080   } else {
0081     rv = rtems_blkdev_ioctl(dd, req, arg);
0082   }
0083 
0084   return rv;
0085 }
0086 
0087 static void do_read_sequence(rtems_disk_device *dd)
0088 {
0089   size_t i;
0090 
0091   for (i = 0; i < sizeof(read_sequence) / sizeof(read_sequence [0]); ++i) {
0092     rtems_status_code sc;
0093     rtems_bdbuf_buffer *bd;
0094 
0095     sc = rtems_bdbuf_read(dd, read_sequence [i], &bd);
0096     rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0097 
0098     sc = rtems_bdbuf_release(bd);
0099     rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0100   }
0101 }
0102 
0103 static void test(void)
0104 {
0105   rtems_status_code sc;
0106   int fd;
0107   int rv;
0108   rtems_disk_device *dd;
0109 
0110   sc = rtems_blkdev_create(
0111     DISK_PATH,
0112     1,
0113     BLOCK_COUNT,
0114     test_disk_ioctl,
0115     NULL
0116   );
0117   rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0118 
0119   fd = open(DISK_PATH, O_RDWR);
0120   rtems_test_assert(fd >= 0);
0121 
0122   rv = rtems_disk_fd_get_disk_device(fd, &dd);
0123   rtems_test_assert(rv == 0);
0124 
0125   rv = close(fd);
0126   rtems_test_assert(rv == 0);
0127 
0128   do_read_sequence(dd);
0129   rtems_bdbuf_purge_dev(dd);
0130   do_read_sequence(dd);
0131 
0132   rtems_test_assert(
0133     memcmp(
0134       block_access_counts,
0135       expected_block_access_counts,
0136       sizeof(block_access_counts)
0137     ) == 0
0138   );
0139 
0140   rv = unlink(DISK_PATH);
0141   rtems_test_assert(rv == 0);
0142 }
0143 
0144 static void Init(rtems_task_argument arg)
0145 {
0146   TEST_BEGIN();
0147 
0148   test();
0149 
0150   TEST_END();
0151 
0152   rtems_test_exit(0);
0153 }
0154 
0155 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0156 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0157 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0158 
0159 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0160 
0161 #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE 1
0162 #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE 1
0163 #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE BLOCK_COUNT
0164 
0165 #define CONFIGURE_MAXIMUM_TASKS 1
0166 
0167 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0168 
0169 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0170 
0171 #define CONFIGURE_INIT
0172 
0173 #include <rtems/confdefs.h>