Back to home page

LXR

 
 

    


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

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 <fcntl.h>
0036 
0037 #include <rtems/bdpart.h>
0038 #include <rtems/blkdev.h>
0039 #include <rtems/ide_part_table.h>
0040 #include <rtems/ramdisk.h>
0041 #include <rtems/libcsupport.h>
0042 
0043 const char rtems_test_name[] = "FSBDPART 1";
0044 
0045 #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL)
0046 
0047 #define PARTITION_COUNT 9
0048 
0049 static const char rda [] = "/dev/rda";
0050 
0051 static const char *const bdpart_rdax [PARTITION_COUNT] = {
0052   "/dev/rda1",
0053   "/dev/rda2",
0054   "/dev/rda3",
0055   "/dev/rda4",
0056   "/dev/rda5",
0057   "/dev/rda6",
0058   "/dev/rda7",
0059   "/dev/rda8",
0060   "/dev/rda9"
0061 };
0062 
0063 static const char *const ide_part_table_rdax [PARTITION_COUNT] = {
0064   "/dev/rda1",
0065   "/dev/rda2",
0066   "/dev/rda3",
0067   "/dev/rda5",
0068   "/dev/rda6",
0069   "/dev/rda7",
0070   "/dev/rda8",
0071   "/dev/rda9",
0072   "/dev/rda10"
0073 };
0074 
0075 static const rtems_blkdev_bnum starts [PARTITION_COUNT] = {
0076   63, 126, 189, 315, 441, 567, 693, 819, 945
0077 };
0078 
0079 static const rtems_bdpart_format format = {
0080   .mbr = {
0081     .type = RTEMS_BDPART_FORMAT_MBR,
0082     .disk_id = 0xdeadbeef,
0083     .dos_compatibility = true
0084   }
0085 };
0086 
0087 static const unsigned distribution [PARTITION_COUNT] = {
0088   1, 1, 1, 1, 1, 1, 1, 1, 1
0089 };
0090 
0091 static void test_logical_disks(const char *const *rdax, bool exists)
0092 {
0093   size_t i = 0;
0094 
0095   for (i = 0; i < PARTITION_COUNT; ++i) {
0096     int fd = open(rdax [i], O_RDONLY);
0097 
0098     if (exists) {
0099       rtems_disk_device *dd = NULL;
0100       int rv = 0;
0101 
0102       rtems_test_assert(fd >= 0);
0103 
0104       rv = rtems_disk_fd_get_disk_device(fd, &dd);
0105       rtems_test_assert(rv == 0);
0106 
0107       rtems_test_assert(dd->start == starts [i]);
0108       rtems_test_assert(dd->size == 63);
0109 
0110       rv = close(fd);
0111       rtems_test_assert(rv == 0);
0112     } else {
0113       rtems_test_assert(fd == -1);
0114     }
0115   }
0116 }
0117 
0118 static void test_bdpart(void)
0119 {
0120   rtems_status_code sc = RTEMS_SUCCESSFUL;
0121   rtems_bdpart_partition created_partitions [PARTITION_COUNT];
0122   rtems_bdpart_format actual_format;
0123   rtems_bdpart_partition actual_partitions [PARTITION_COUNT];
0124   rtems_resource_snapshot before;
0125   size_t actual_count = PARTITION_COUNT;
0126   size_t i = 0;
0127 
0128   memset(&created_partitions [0], 0, sizeof(created_partitions));
0129   memset(&actual_format, 0, sizeof(actual_format));
0130   memset(&actual_partitions [0], 0, sizeof(actual_partitions));
0131 
0132   rtems_resource_snapshot_take(&before);
0133 
0134   for (i = 0; i < PARTITION_COUNT; ++i) {
0135     rtems_bdpart_to_partition_type(
0136       RTEMS_BDPART_MBR_FAT_32,
0137       created_partitions [i].type
0138     );
0139   }
0140 
0141   sc = rtems_bdpart_create(
0142     rda,
0143     &format,
0144     &created_partitions [0],
0145     &distribution [0],
0146     PARTITION_COUNT
0147   );
0148   ASSERT_SC(sc);
0149 
0150   sc = rtems_bdpart_write(
0151     rda,
0152     &format,
0153     &created_partitions [0],
0154     PARTITION_COUNT
0155   );
0156   ASSERT_SC(sc);
0157 
0158   sc = rtems_bdpart_read(
0159     rda,
0160     &actual_format,
0161     &actual_partitions [0],
0162     &actual_count
0163   );
0164   ASSERT_SC(sc);
0165   rtems_test_assert(actual_format.mbr.disk_id == format.mbr.disk_id);
0166   rtems_test_assert(
0167     memcmp(
0168       &actual_partitions [0],
0169       &created_partitions [0],
0170       PARTITION_COUNT
0171     ) == 0
0172   );
0173 
0174   sc = rtems_bdpart_register(
0175     rda,
0176     actual_partitions,
0177     actual_count
0178   );
0179   ASSERT_SC(sc);
0180   test_logical_disks(&bdpart_rdax [0], true);
0181 
0182   sc = rtems_bdpart_unregister(
0183     rda,
0184     actual_partitions,
0185     actual_count
0186   );
0187   ASSERT_SC(sc);
0188   test_logical_disks(&bdpart_rdax [0], false);
0189 
0190   rtems_test_assert(rtems_resource_snapshot_check(&before));
0191 
0192   sc = rtems_bdpart_register_from_disk(rda);
0193   ASSERT_SC(sc);
0194   test_logical_disks(&bdpart_rdax [0], true);
0195 
0196   sc = rtems_bdpart_unregister(
0197     rda,
0198     actual_partitions,
0199     actual_count
0200   );
0201   ASSERT_SC(sc);
0202   test_logical_disks(&bdpart_rdax [0], false);
0203 
0204   rtems_test_assert(rtems_resource_snapshot_check(&before));
0205 
0206   rtems_bdpart_dump(&actual_partitions [0], actual_count);
0207 }
0208 
0209 static void test_ide_part_table(void)
0210 {
0211   rtems_status_code sc = RTEMS_SUCCESSFUL;
0212 
0213   test_logical_disks(&ide_part_table_rdax [0], false);
0214 #pragma GCC diagnostic push
0215 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
0216   sc = rtems_ide_part_table_initialize(rda);
0217 #pragma GCC diagnostic pop
0218   ASSERT_SC(sc);
0219   test_logical_disks(&ide_part_table_rdax [0], true);
0220 }
0221 
0222 static void Init(rtems_task_argument arg)
0223 {
0224   TEST_BEGIN();
0225 
0226   test_bdpart();
0227   test_ide_part_table();
0228 
0229   TEST_END();
0230   rtems_test_exit(0);
0231 }
0232 
0233 rtems_ramdisk_config rtems_ramdisk_configuration [] = {
0234   { .block_size = 512, .block_num = 1024 }
0235 };
0236 
0237 size_t rtems_ramdisk_configuration_size = 1;
0238 
0239 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0240 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0241 #define CONFIGURE_APPLICATION_EXTRA_DRIVERS RAMDISK_DRIVER_TABLE_ENTRY
0242 #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
0243 
0244 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 13
0245 
0246 #define CONFIGURE_MAXIMUM_TASKS 2
0247 
0248 #define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
0249 
0250 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0251 
0252 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0253 
0254 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0255 
0256 #define CONFIGURE_INIT
0257 
0258 #include <rtems/confdefs.h>