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 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 <inttypes.h>
0039 #include <stdlib.h>
0040 #include <unistd.h>
0041 
0042 #include <rtems/bdbuf.h>
0043 
0044 const char rtems_test_name[] = "BLOCK 15";
0045 
0046 #define BLOCK_COUNT 20
0047 
0048 #define ACTION_COUNT 8
0049 
0050 #define REQUEST_COUNT 4
0051 
0052 #define WRITE_COUNT 16
0053 
0054 #define MAX_WRITE_BLOCKS 3
0055 
0056 #define MEDIA_BLOCK_SIZE 2
0057 
0058 #define BLOCK_SIZE 4
0059 
0060 #define DISK_PATH "/disk"
0061 
0062 static const rtems_blkdev_bnum action_sequence [ACTION_COUNT] = {
0063   2, 1, 0, 4, 5, 6, 7, 9
0064 };
0065 
0066 static size_t request_index;
0067 
0068 static const uint32_t expected_request_bufnums [REQUEST_COUNT] = {
0069   3, 3, 1, 1
0070 };
0071 
0072 static size_t write_index;
0073 
0074 static const rtems_blkdev_bnum expected_write_blocks [WRITE_COUNT] = {
0075   0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 18, 19
0076 };
0077 
0078 static int test_disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg)
0079 {
0080   int rv = 0;
0081 
0082   if (req == RTEMS_BLKIO_REQUEST) {
0083     rtems_blkdev_request *breq = arg;
0084     uint32_t i;
0085 
0086     printf("REQ %" PRIu32 "\n", breq->bufnum);
0087 
0088     rtems_test_assert(breq->req == RTEMS_BLKDEV_REQ_WRITE);
0089     rtems_test_assert(breq->bufnum == expected_request_bufnums [request_index]);
0090     ++request_index;
0091 
0092     for (i = 0; i < breq->bufnum; ++i) {
0093       rtems_blkdev_sg_buffer *sg = &breq->bufs [i];
0094       rtems_blkdev_bnum j;
0095 
0096       printf("W %" PRIu32 "\n", sg->block);
0097 
0098       rtems_test_assert(sg->block < BLOCK_COUNT);
0099       rtems_test_assert(sg->length == BLOCK_SIZE);
0100 
0101       for (j = 0; j < BLOCK_SIZE / MEDIA_BLOCK_SIZE; ++j) {
0102         rtems_test_assert(expected_write_blocks [write_index] == sg->block + j);
0103         ++write_index;
0104       }
0105     }
0106 
0107     rtems_blkdev_request_done(breq, RTEMS_SUCCESSFUL);
0108   } else if (req == RTEMS_BLKIO_CAPABILITIES) {
0109     *(uint32_t *) arg = RTEMS_BLKDEV_CAP_MULTISECTOR_CONT;
0110   } else {
0111     rv = rtems_blkdev_ioctl(dd, req, arg);
0112   }
0113 
0114   return rv;
0115 }
0116 
0117 static void test_write_requests(rtems_disk_device *dd)
0118 {
0119   rtems_status_code sc;
0120   int i;
0121 
0122   sc = rtems_bdbuf_set_block_size(dd, BLOCK_SIZE, true);
0123   rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0124 
0125   for (i = 0; i < ACTION_COUNT; ++i) {
0126     rtems_blkdev_bnum block = action_sequence [i];
0127     rtems_bdbuf_buffer *bd;
0128 
0129     sc = rtems_bdbuf_get(dd, block, &bd);
0130     rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0131 
0132     sc = rtems_bdbuf_release_modified(bd);
0133     rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0134   }
0135 
0136   sc = rtems_bdbuf_syncdev(dd);
0137   rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0138 
0139   rtems_test_assert(request_index == REQUEST_COUNT);
0140   rtems_test_assert(write_index == WRITE_COUNT);
0141 }
0142 
0143 static void test(void)
0144 {
0145   rtems_status_code sc;
0146   rtems_disk_device *dd;
0147   int fd;
0148   int rv;
0149 
0150   sc = rtems_blkdev_create(
0151     DISK_PATH,
0152     MEDIA_BLOCK_SIZE,
0153     BLOCK_COUNT,
0154     test_disk_ioctl,
0155     NULL
0156   );
0157   rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0158 
0159   fd = open(DISK_PATH, O_RDWR);
0160   rtems_test_assert(fd >= 0);
0161 
0162   rv = rtems_disk_fd_get_disk_device(fd, &dd);
0163   rtems_test_assert(rv == 0);
0164 
0165   rv = close(fd);
0166   rtems_test_assert(rv == 0);
0167 
0168   test_write_requests(dd);
0169 
0170   rv = unlink(DISK_PATH);
0171   rtems_test_assert(rv == 0);
0172 }
0173 
0174 static void Init(rtems_task_argument arg)
0175 {
0176   TEST_BEGIN();
0177 
0178   test();
0179 
0180   TEST_END();
0181 
0182   rtems_test_exit(0);
0183 }
0184 
0185 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0186 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0187 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0188 
0189 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0190 
0191 #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE 1
0192 #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE 4
0193 #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (BLOCK_COUNT * BLOCK_SIZE)
0194 #define CONFIGURE_BDBUF_MAX_WRITE_BLOCKS MAX_WRITE_BLOCKS
0195 
0196 #define CONFIGURE_MAXIMUM_TASKS 1
0197 
0198 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0199 
0200 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0201 
0202 #define CONFIGURE_INIT
0203 
0204 #include <rtems/confdefs.h>