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/ioctl.h>
0035 #include <sys/types.h>
0036 #include <sys/stat.h>
0037 #include <fcntl.h>
0038 #include <unistd.h>
0039 #include <string.h>
0040 
0041 #include <rtems/ramdisk.h>
0042 #include <rtems/bdbuf.h>
0043 
0044 const char rtems_test_name[] = "BLOCK 16";
0045 
0046 #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL)
0047 
0048 static void test(void)
0049 {
0050   static const char device [] = "/dev/rda";
0051   rtems_status_code sc;
0052   int fd;
0053   int rv;
0054   rtems_disk_device *dd;
0055   ramdisk *rd;
0056   unsigned char buf [8];
0057   uint32_t media_block_size = sizeof(buf [0]);
0058   rtems_blkdev_bnum media_block_count = sizeof(buf) / sizeof(buf [0]);
0059   rtems_blkdev_bnum media_size = media_block_size * media_block_count;
0060   rtems_bdbuf_buffer *media_bd [media_block_count];
0061   rtems_bdbuf_buffer *large_bd;
0062   rtems_blkdev_bnum i;
0063 
0064   for (i = 0; i < media_block_count; ++i) {
0065     buf [i] = i;
0066   }
0067 
0068   rd = ramdisk_allocate(buf, media_block_size, media_block_count, false);
0069   rtems_test_assert(rd != NULL);
0070 
0071   sc = rtems_blkdev_create(
0072     device,
0073     media_block_size,
0074     media_block_count,
0075     ramdisk_ioctl,
0076     rd
0077   );
0078   ASSERT_SC(sc);
0079 
0080   fd = open(device, O_RDWR);
0081   rtems_test_assert(fd >= 0);
0082 
0083   rv = rtems_disk_fd_get_disk_device(fd, &dd);
0084   rtems_test_assert(rv == 0);
0085 
0086   for (i = 0; i < media_block_count; ++i) {
0087     sc = rtems_bdbuf_read(dd, i, &media_bd [i]);
0088     ASSERT_SC(sc);
0089 
0090     rtems_test_assert(media_bd [i]->buffer [0] == buf [i]);
0091   }
0092 
0093   for (i = 0; i < media_block_count; ++i) {
0094     media_bd [i]->buffer [0] += media_block_count;
0095 
0096     sc = rtems_bdbuf_release_modified(media_bd [i]);
0097     ASSERT_SC(sc);
0098   }
0099 
0100   for (i = 0; i < media_block_count; i += 2) {
0101     sc = rtems_bdbuf_read(dd, i, &media_bd [i]);
0102     ASSERT_SC(sc);
0103 
0104     rtems_test_assert(media_bd [i]->buffer [0] == buf [i] + media_block_count);
0105   }
0106 
0107   sc = rtems_bdbuf_set_block_size(dd, media_size, true);
0108   ASSERT_SC(sc);
0109 
0110   for (i = 0; i < media_block_count; i += 2) {
0111     rtems_test_assert(media_bd [i]->state == RTEMS_BDBUF_STATE_ACCESS_PURGED);
0112     rtems_test_assert(media_bd [i + 1]->state == RTEMS_BDBUF_STATE_FREE);
0113 
0114     rtems_test_assert(media_bd [i]->buffer [0] == buf [i] + media_block_count);
0115     rtems_test_assert(media_bd [i + 1]->buffer [0] == buf [i + 1]);
0116 
0117     sc = rtems_bdbuf_release(media_bd [i]);
0118     ASSERT_SC(sc);
0119   }
0120 
0121   sc = rtems_bdbuf_read(dd, 0, &large_bd);
0122   ASSERT_SC(sc);
0123 
0124   rtems_test_assert(memcmp(buf, large_bd->buffer, media_size) == 0);
0125 
0126   sc = rtems_bdbuf_release(large_bd);
0127   ASSERT_SC(sc);
0128 
0129   rv = close(fd);
0130   rtems_test_assert(rv == 0);
0131 
0132   rv = unlink(device);
0133   rtems_test_assert(rv == 0);
0134 }
0135 
0136 static void Init(rtems_task_argument arg)
0137 {
0138   TEST_BEGIN();
0139 
0140   test();
0141 
0142   TEST_END();
0143 
0144   rtems_test_exit(0);
0145 }
0146 
0147 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0148 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0149 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0150 
0151 #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE 1
0152 #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE 8
0153 #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE 8
0154 
0155 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0156 
0157 #define CONFIGURE_MAXIMUM_TASKS 1
0158 
0159 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0160 
0161 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0162 
0163 #define CONFIGURE_INIT
0164 
0165 #include <rtems/confdefs.h>