[e51bd96] | 1 | /* ramdisk.c -- RAM disk block device implementation |
---|
| 2 | * |
---|
| 3 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
| 4 | * Author: Victor V. Vengerov <vvv@oktet.ru> |
---|
| 5 | * |
---|
| 6 | * @(#) $Id$ |
---|
| 7 | */ |
---|
| 8 | |
---|
[006fa1ef] | 9 | #if HAVE_CONFIG_H |
---|
| 10 | #include "config.h" |
---|
| 11 | #endif |
---|
| 12 | |
---|
[e51bd96] | 13 | #include <rtems.h> |
---|
| 14 | #include <rtems/libio.h> |
---|
| 15 | #include <errno.h> |
---|
| 16 | #include <stdlib.h> |
---|
| 17 | #include <stdio.h> |
---|
[228587bb] | 18 | #include <string.h> |
---|
[f6a8433d] | 19 | #include <inttypes.h> |
---|
[e51bd96] | 20 | |
---|
| 21 | #include "rtems/blkdev.h" |
---|
| 22 | #include "rtems/diskdevs.h" |
---|
| 23 | #include "rtems/ramdisk.h" |
---|
| 24 | |
---|
[3899a537] | 25 | /** |
---|
| 26 | * Control tracing. It can be compiled out of the code for small |
---|
| 27 | * footprint targets. Leave in by default. |
---|
| 28 | */ |
---|
| 29 | #if !defined (RTEMS_RAMDISK_TRACE) |
---|
| 30 | #define RTEMS_RAMDISK_TRACE 0 |
---|
| 31 | #endif |
---|
| 32 | |
---|
[e51bd96] | 33 | #define RAMDISK_DEVICE_BASE_NAME "/dev/ramdisk" |
---|
| 34 | |
---|
| 35 | /* Internal RAM disk descriptor */ |
---|
| 36 | struct ramdisk { |
---|
| 37 | int block_size; /* RAM disk block size */ |
---|
| 38 | int block_num; /* Number of blocks on this RAM disk */ |
---|
| 39 | void *area; /* RAM disk memory area */ |
---|
| 40 | rtems_boolean initialized;/* RAM disk is initialized */ |
---|
[8b96149] | 41 | rtems_boolean malloced; /* != 0, if memory allocated by malloc for this |
---|
[e51bd96] | 42 | RAM disk */ |
---|
[3899a537] | 43 | #if RTEMS_RAMDISK_TRACE |
---|
| 44 | int info_level; /* Trace level */ |
---|
| 45 | #endif |
---|
[e51bd96] | 46 | }; |
---|
| 47 | |
---|
| 48 | static struct ramdisk *ramdisk; |
---|
[cec5c069] | 49 | static uint32_t nramdisks; |
---|
[e51bd96] | 50 | |
---|
[3899a537] | 51 | #if RTEMS_RAMDISK_TRACE |
---|
| 52 | /** |
---|
| 53 | * Print a message to the ramdisk output and flush it. |
---|
| 54 | * |
---|
| 55 | * @param rd The ramdisk control structure. |
---|
| 56 | * @param format The format string. See printf for details. |
---|
| 57 | * @param ... The arguments for the format text. |
---|
| 58 | * @return int The number of bytes written to the output. |
---|
| 59 | */ |
---|
| 60 | static int |
---|
| 61 | rtems_ramdisk_printf (struct ramdisk *rd, const char *format, ...) |
---|
| 62 | { |
---|
| 63 | int ret = 0; |
---|
| 64 | if (rd->info_level >= 1) |
---|
| 65 | { |
---|
| 66 | va_list args; |
---|
| 67 | va_start (args, format); |
---|
| 68 | fprintf (stdout, "ramdisk:"); |
---|
| 69 | ret = vfprintf (stdout, format, args); |
---|
| 70 | fprintf (stdout, "\n"); |
---|
| 71 | fflush (stdout); |
---|
| 72 | } |
---|
| 73 | return ret; |
---|
| 74 | } |
---|
| 75 | #endif |
---|
| 76 | |
---|
[e51bd96] | 77 | /* ramdisk_read -- |
---|
| 78 | * RAM disk READ request handler. This primitive copies data from RAM |
---|
| 79 | * disk to supplied buffer and invoke the callout function to inform |
---|
| 80 | * upper layer that reading is completed. |
---|
[8b96149] | 81 | * |
---|
[e51bd96] | 82 | * PARAMETERS: |
---|
| 83 | * req - pointer to the READ block device request info |
---|
| 84 | * |
---|
| 85 | * RETURNS: |
---|
| 86 | * ioctl return value |
---|
| 87 | */ |
---|
| 88 | static int |
---|
[3899a537] | 89 | ramdisk_read(struct ramdisk *rd, rtems_blkdev_request *req) |
---|
[e51bd96] | 90 | { |
---|
| 91 | char *from; |
---|
[c01d95b7] | 92 | uint32_t i; |
---|
[3899a537] | 93 | rtems_blkdev_sg_buffer *sg; |
---|
[c01d95b7] | 94 | uint32_t remains; |
---|
[048dcd2b] | 95 | |
---|
[3899a537] | 96 | #if RTEMS_RAMDISK_TRACE |
---|
| 97 | rtems_ramdisk_printf (rd, "ramdisk read: start=%d, blocks=%d remains=%d", |
---|
| 98 | req->bufs[0].block, req->bufnum, |
---|
| 99 | rd->block_size * req->count); |
---|
| 100 | #endif |
---|
| 101 | |
---|
[2eb89ad] | 102 | remains = rd->block_size * req->bufnum; |
---|
[e51bd96] | 103 | sg = req->bufs; |
---|
| 104 | for (i = 0; (remains > 0) && (i < req->bufnum); i++, sg++) |
---|
| 105 | { |
---|
[cec5c069] | 106 | uint32_t count = sg->length; |
---|
[3899a537] | 107 | from = ((char *)rd->area + (sg->block * rd->block_size)); |
---|
[e51bd96] | 108 | if (count > remains) |
---|
| 109 | count = remains; |
---|
| 110 | memcpy(sg->buffer, from, count); |
---|
| 111 | remains -= count; |
---|
[25d42183] | 112 | from += count; |
---|
[e51bd96] | 113 | } |
---|
| 114 | req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0); |
---|
| 115 | return 0; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /* ramdisk_write -- |
---|
| 119 | * RAM disk WRITE request handler. This primitive copies data from |
---|
| 120 | * supplied buffer to RAM disk and invoke the callout function to inform |
---|
| 121 | * upper layer that writing is completed. |
---|
[8b96149] | 122 | * |
---|
[e51bd96] | 123 | * PARAMETERS: |
---|
| 124 | * req - pointer to the WRITE block device request info |
---|
| 125 | * |
---|
| 126 | * RETURNS: |
---|
| 127 | * ioctl return value |
---|
| 128 | */ |
---|
| 129 | static int |
---|
[3899a537] | 130 | ramdisk_write(struct ramdisk *rd, rtems_blkdev_request *req) |
---|
[e51bd96] | 131 | { |
---|
| 132 | char *to; |
---|
[c01d95b7] | 133 | uint32_t i; |
---|
[3899a537] | 134 | rtems_blkdev_sg_buffer *sg; |
---|
[c01d95b7] | 135 | uint32_t remains; |
---|
[048dcd2b] | 136 | |
---|
[3899a537] | 137 | #if RTEMS_RAMDISK_TRACE |
---|
| 138 | rtems_ramdisk_printf (rd, "ramdisk write: start=%d, blocks=%d remains=%d", |
---|
| 139 | req->bufs[0].block, req->bufnum, |
---|
[2eb89ad] | 140 | rd->block_size * req->bufnum); |
---|
[3899a537] | 141 | #endif |
---|
[2eb89ad] | 142 | remains = rd->block_size * req->bufnum; |
---|
[e51bd96] | 143 | sg = req->bufs; |
---|
| 144 | for (i = 0; (remains > 0) && (i < req->bufnum); i++, sg++) |
---|
| 145 | { |
---|
[cec5c069] | 146 | uint32_t count = sg->length; |
---|
[3899a537] | 147 | to = ((char *)rd->area + (sg->block * rd->block_size)); |
---|
[e51bd96] | 148 | if (count > remains) |
---|
| 149 | count = remains; |
---|
| 150 | memcpy(to, sg->buffer, count); |
---|
| 151 | remains -= count; |
---|
[25d42183] | 152 | to += count; |
---|
[e51bd96] | 153 | } |
---|
| 154 | req->req_done(req->done_arg, RTEMS_SUCCESSFUL, 0); |
---|
| 155 | return 0; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /* ramdisk_ioctl -- |
---|
| 159 | * IOCTL handler for RAM disk device. |
---|
| 160 | * |
---|
| 161 | * PARAMETERS: |
---|
| 162 | * dev - device number (major, minor number) |
---|
| 163 | * req - IOCTL request code |
---|
| 164 | * argp - IOCTL argument |
---|
| 165 | * |
---|
| 166 | * RETURNS: |
---|
| 167 | * IOCTL return value |
---|
| 168 | */ |
---|
| 169 | static int |
---|
[f6a8433d] | 170 | ramdisk_ioctl(dev_t dev, uint32_t req, void *argp) |
---|
[e51bd96] | 171 | { |
---|
| 172 | switch (req) |
---|
| 173 | { |
---|
[3899a537] | 174 | case RTEMS_BLKIO_REQUEST: |
---|
[e51bd96] | 175 | { |
---|
| 176 | rtems_device_minor_number minor; |
---|
[3899a537] | 177 | rtems_blkdev_request *r = argp; |
---|
[e51bd96] | 178 | struct ramdisk *rd; |
---|
| 179 | |
---|
| 180 | minor = rtems_filesystem_dev_minor_t(dev); |
---|
| 181 | if ((minor >= nramdisks) || !ramdisk[minor].initialized) |
---|
| 182 | { |
---|
| 183 | errno = ENODEV; |
---|
| 184 | return -1; |
---|
| 185 | } |
---|
[048dcd2b] | 186 | |
---|
[e51bd96] | 187 | rd = ramdisk + minor; |
---|
[048dcd2b] | 188 | |
---|
[e51bd96] | 189 | switch (r->req) |
---|
| 190 | { |
---|
[3899a537] | 191 | case RTEMS_BLKDEV_REQ_READ: |
---|
[e51bd96] | 192 | return ramdisk_read(rd, r); |
---|
| 193 | |
---|
[3899a537] | 194 | case RTEMS_BLKDEV_REQ_WRITE: |
---|
[e51bd96] | 195 | return ramdisk_write(rd, r); |
---|
[048dcd2b] | 196 | |
---|
[e51bd96] | 197 | default: |
---|
| 198 | errno = EBADRQC; |
---|
| 199 | return -1; |
---|
| 200 | } |
---|
| 201 | break; |
---|
| 202 | } |
---|
[048dcd2b] | 203 | |
---|
[e51bd96] | 204 | default: |
---|
| 205 | errno = EBADRQC; |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /* ramdisk_initialize -- |
---|
[8b96149] | 211 | * RAM disk device driver initialization. Run through RAM disk |
---|
[e51bd96] | 212 | * configuration information and configure appropriate RAM disks. |
---|
| 213 | * |
---|
| 214 | * PARAMETERS: |
---|
| 215 | * major - RAM disk major device number |
---|
| 216 | * minor - minor device number, not applicable |
---|
| 217 | * arg - initialization argument, not applicable |
---|
| 218 | * |
---|
| 219 | * RETURNS: |
---|
| 220 | * none |
---|
| 221 | */ |
---|
| 222 | rtems_device_driver |
---|
| 223 | ramdisk_initialize( |
---|
| 224 | rtems_device_major_number major, |
---|
| 225 | rtems_device_minor_number minor, |
---|
| 226 | void *arg) |
---|
| 227 | { |
---|
| 228 | rtems_device_minor_number i; |
---|
| 229 | rtems_ramdisk_config *c = rtems_ramdisk_configuration; |
---|
| 230 | struct ramdisk *r; |
---|
| 231 | rtems_status_code rc; |
---|
| 232 | |
---|
| 233 | rc = rtems_disk_io_initialize(); |
---|
| 234 | if (rc != RTEMS_SUCCESSFUL) |
---|
| 235 | return rc; |
---|
[048dcd2b] | 236 | |
---|
[8b96149] | 237 | r = ramdisk = calloc(rtems_ramdisk_configuration_size, |
---|
[e51bd96] | 238 | sizeof(struct ramdisk)); |
---|
[3899a537] | 239 | #if RTEMS_RAMDISK_TRACE |
---|
| 240 | r->info_level = 1; |
---|
| 241 | #endif |
---|
[e51bd96] | 242 | for (i = 0; i < rtems_ramdisk_configuration_size; i++, c++, r++) |
---|
| 243 | { |
---|
| 244 | dev_t dev = rtems_filesystem_make_dev_t(major, i); |
---|
| 245 | char name[sizeof(RAMDISK_DEVICE_BASE_NAME "0123456789")]; |
---|
[f6a8433d] | 246 | snprintf(name, sizeof(name), RAMDISK_DEVICE_BASE_NAME "%" PRIu32, i); |
---|
[e51bd96] | 247 | r->block_size = c->block_size; |
---|
| 248 | r->block_num = c->block_num; |
---|
| 249 | if (c->location == NULL) |
---|
| 250 | { |
---|
| 251 | r->malloced = TRUE; |
---|
| 252 | r->area = malloc(r->block_size * r->block_num); |
---|
| 253 | if (r->area == NULL) /* No enough memory for this disk */ |
---|
| 254 | { |
---|
| 255 | r->initialized = FALSE; |
---|
| 256 | continue; |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | r->initialized = TRUE; |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | r->malloced = FALSE; |
---|
| 266 | r->initialized = TRUE; |
---|
| 267 | r->area = c->location; |
---|
| 268 | } |
---|
| 269 | rc = rtems_disk_create_phys(dev, c->block_size, c->block_num, |
---|
| 270 | ramdisk_ioctl, name); |
---|
| 271 | if (rc != RTEMS_SUCCESSFUL) |
---|
| 272 | { |
---|
| 273 | if (r->malloced) |
---|
| 274 | { |
---|
| 275 | free(r->area); |
---|
| 276 | } |
---|
| 277 | r->initialized = FALSE; |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | nramdisks = rtems_ramdisk_configuration_size; |
---|
| 281 | return RTEMS_SUCCESSFUL; |
---|
| 282 | } |
---|