[228b135] | 1 | /*
|
---|
[ff3a34b] | 2 | * Copyright (c) 2007 Michal Konopa
|
---|
| 3 | * Copyright (c) 2007 Martin Jelen
|
---|
| 4 | * Copyright (c) 2007 Peter Majer
|
---|
[84947a4] | 5 | * Copyright (c) 2007 Jakub Jermar
|
---|
[228b135] | 6 | * All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
---|
| 9 | * modification, are permitted provided that the following conditions
|
---|
| 10 | * are met:
|
---|
| 11 | *
|
---|
| 12 | * - Redistributions of source code must retain the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer.
|
---|
| 14 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 16 | * documentation and/or other materials provided with the distribution.
|
---|
| 17 | * - The name of the author may not be used to endorse or promote products
|
---|
| 18 | * derived from this software without specific prior written permission.
|
---|
| 19 | *
|
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | /** @addtogroup rd
|
---|
| 33 | * @{
|
---|
[d9fae235] | 34 | */
|
---|
[228b135] | 35 |
|
---|
| 36 | /**
|
---|
[d9fae235] | 37 | * @file rd.c
|
---|
| 38 | * @brief Initial RAM disk for HelenOS.
|
---|
[228b135] | 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <ipc/services.h>
|
---|
| 42 | #include <ipc/ns.h>
|
---|
[7c34822e] | 43 | #include <sysinfo.h>
|
---|
| 44 | #include <as.h>
|
---|
[4802dd7] | 45 | #include <bd_srv.h>
|
---|
[7c34822e] | 46 | #include <ddi.h>
|
---|
| 47 | #include <align.h>
|
---|
[3e6a98c5] | 48 | #include <stdbool.h>
|
---|
[228b135] | 49 | #include <errno.h>
|
---|
[c1694b6b] | 50 | #include <str_error.h>
|
---|
[228b135] | 51 | #include <async.h>
|
---|
[1e4cada] | 52 | #include <fibril_synch.h>
|
---|
[2f65fb0] | 53 | #include <stdio.h>
|
---|
[15f3c3f] | 54 | #include <loc.h>
|
---|
[1ee00b7] | 55 | #include <macros.h>
|
---|
[b5daa89] | 56 | #include <inttypes.h>
|
---|
[228b135] | 57 |
|
---|
[b5daa89] | 58 | #define NAME "rd"
|
---|
[2f65fb0] | 59 |
|
---|
[1ee00b7] | 60 | /** Pointer to the ramdisk's image */
|
---|
[bf9cb2f] | 61 | static void *rd_addr = AS_AREA_ANY;
|
---|
[d9fae235] | 62 |
|
---|
[1ee00b7] | 63 | /** Size of the ramdisk */
|
---|
[3ae470a] | 64 | static size_t rd_size;
|
---|
[228b135] | 65 |
|
---|
[1ee00b7] | 66 | /** Block size */
|
---|
| 67 | static const size_t block_size = 512;
|
---|
| 68 |
|
---|
[b7fd2a0] | 69 | static errno_t rd_open(bd_srvs_t *, bd_srv_t *);
|
---|
| 70 | static errno_t rd_close(bd_srv_t *);
|
---|
| 71 | static errno_t rd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
|
---|
| 72 | static errno_t rd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
|
---|
| 73 | static errno_t rd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 74 | static errno_t rd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
[1ee00b7] | 75 |
|
---|
[d9fae235] | 76 | /** This rwlock protects the ramdisk's data.
|
---|
| 77 | *
|
---|
[84947a4] | 78 | * If we were to serve multiple requests (read + write or several writes)
|
---|
[d9fae235] | 79 | * concurrently (i.e. from two or more threads), each read and write needs to
|
---|
| 80 | * be protected by this rwlock.
|
---|
| 81 | *
|
---|
| 82 | */
|
---|
[4802dd7] | 83 | static fibril_rwlock_t rd_lock;
|
---|
[84947a4] | 84 |
|
---|
[4802dd7] | 85 | static bd_ops_t rd_bd_ops = {
|
---|
| 86 | .open = rd_open,
|
---|
| 87 | .close = rd_close,
|
---|
| 88 | .read_blocks = rd_read_blocks,
|
---|
| 89 | .write_blocks = rd_write_blocks,
|
---|
| 90 | .get_block_size = rd_get_block_size,
|
---|
| 91 | .get_num_blocks = rd_get_num_blocks
|
---|
| 92 | };
|
---|
| 93 |
|
---|
[135486d] | 94 | static bd_srvs_t bd_srvs;
|
---|
[4802dd7] | 95 |
|
---|
| 96 | static void rd_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[228b135] | 97 | {
|
---|
[135486d] | 98 | bd_conn(iid, icall, &bd_srvs);
|
---|
[4802dd7] | 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Open device. */
|
---|
[b7fd2a0] | 102 | static errno_t rd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
[4802dd7] | 103 | {
|
---|
| 104 | return EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /** Close device. */
|
---|
[b7fd2a0] | 108 | static errno_t rd_close(bd_srv_t *bd)
|
---|
[4802dd7] | 109 | {
|
---|
| 110 | return EOK;
|
---|
[228b135] | 111 | }
|
---|
| 112 |
|
---|
[1ee00b7] | 113 | /** Read blocks from the device. */
|
---|
[b7fd2a0] | 114 | static errno_t rd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
|
---|
[4802dd7] | 115 | size_t size)
|
---|
[1ee00b7] | 116 | {
|
---|
| 117 | if ((ba + cnt) * block_size > rd_size) {
|
---|
| 118 | /* Reading past the end of the device. */
|
---|
| 119 | return ELIMIT;
|
---|
| 120 | }
|
---|
[a35b458] | 121 |
|
---|
[1ee00b7] | 122 | fibril_rwlock_read_lock(&rd_lock);
|
---|
[4802dd7] | 123 | memcpy(buf, rd_addr + ba * block_size, min(block_size * cnt, size));
|
---|
[1ee00b7] | 124 | fibril_rwlock_read_unlock(&rd_lock);
|
---|
[a35b458] | 125 |
|
---|
[1ee00b7] | 126 | return EOK;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /** Write blocks to the device. */
|
---|
[b7fd2a0] | 130 | static errno_t rd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
[4802dd7] | 131 | const void *buf, size_t size)
|
---|
[1ee00b7] | 132 | {
|
---|
| 133 | if ((ba + cnt) * block_size > rd_size) {
|
---|
| 134 | /* Writing past the end of the device. */
|
---|
| 135 | return ELIMIT;
|
---|
| 136 | }
|
---|
[a35b458] | 137 |
|
---|
[1ee00b7] | 138 | fibril_rwlock_write_lock(&rd_lock);
|
---|
[4802dd7] | 139 | memcpy(rd_addr + ba * block_size, buf, min(block_size * cnt, size));
|
---|
[1ee00b7] | 140 | fibril_rwlock_write_unlock(&rd_lock);
|
---|
[a35b458] | 141 |
|
---|
[1ee00b7] | 142 | return EOK;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[84947a4] | 145 | /** Prepare the ramdisk image for operation. */
|
---|
[7c34822e] | 146 | static bool rd_init(void)
|
---|
[228b135] | 147 | {
|
---|
[b5daa89] | 148 | sysarg_t size;
|
---|
[b7fd2a0] | 149 | errno_t ret = sysinfo_get_value("rd.size", &size);
|
---|
[b5daa89] | 150 | if ((ret != EOK) || (size == 0)) {
|
---|
[d9fae235] | 151 | printf("%s: No RAM disk found\n", NAME);
|
---|
| 152 | return false;
|
---|
| 153 | }
|
---|
[a35b458] | 154 |
|
---|
[b5daa89] | 155 | sysarg_t addr_phys;
|
---|
| 156 | ret = sysinfo_get_value("rd.address.physical", &addr_phys);
|
---|
| 157 | if ((ret != EOK) || (addr_phys == 0)) {
|
---|
[d9fae235] | 158 | printf("%s: Invalid RAM disk physical address\n", NAME);
|
---|
[7c34822e] | 159 | return false;
|
---|
[2f65fb0] | 160 | }
|
---|
[a35b458] | 161 |
|
---|
[b5daa89] | 162 | rd_size = ALIGN_UP(size, block_size);
|
---|
| 163 | unsigned int flags =
|
---|
| 164 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
|
---|
[a35b458] | 165 |
|
---|
[8442d10] | 166 | ret = physmem_map(addr_phys,
|
---|
[fbcdeb8] | 167 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags, &rd_addr);
|
---|
| 168 | if (ret != EOK) {
|
---|
[d9fae235] | 169 | printf("%s: Error mapping RAM disk\n", NAME);
|
---|
[2f65fb0] | 170 | return false;
|
---|
| 171 | }
|
---|
[a35b458] | 172 |
|
---|
[b5daa89] | 173 | printf("%s: Found RAM disk at %p, %" PRIun " bytes\n", NAME,
|
---|
| 174 | (void *) addr_phys, size);
|
---|
[a35b458] | 175 |
|
---|
[135486d] | 176 | bd_srvs_init(&bd_srvs);
|
---|
| 177 | bd_srvs.ops = &rd_bd_ops;
|
---|
[a35b458] | 178 |
|
---|
[b688fd8] | 179 | async_set_fallback_port_handler(rd_client_conn, NULL);
|
---|
[f302586] | 180 | ret = loc_server_register(NAME);
|
---|
[b16e77d] | 181 | if (ret != EOK) {
|
---|
[c1694b6b] | 182 | printf("%s: Unable to register driver: %s\n", NAME, str_error(ret));
|
---|
[2f65fb0] | 183 | return false;
|
---|
| 184 | }
|
---|
[a35b458] | 185 |
|
---|
[15f3c3f] | 186 | service_id_t service_id;
|
---|
[b5daa89] | 187 | ret = loc_service_register("bd/initrd", &service_id);
|
---|
| 188 | if (ret != EOK) {
|
---|
[15f3c3f] | 189 | printf("%s: Unable to register device service\n", NAME);
|
---|
[537611cc] | 190 | return false;
|
---|
| 191 | }
|
---|
[a35b458] | 192 |
|
---|
[52e4f526] | 193 | fibril_rwlock_initialize(&rd_lock);
|
---|
[a35b458] | 194 |
|
---|
[7c34822e] | 195 | return true;
|
---|
| 196 | }
|
---|
[228b135] | 197 |
|
---|
[4802dd7] | 198 | /** Get device block size. */
|
---|
[b7fd2a0] | 199 | static errno_t rd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
[4802dd7] | 200 | {
|
---|
| 201 | *rsize = block_size;
|
---|
| 202 | return EOK;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Get number of blocks on device. */
|
---|
[b7fd2a0] | 206 | static errno_t rd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
[4802dd7] | 207 | {
|
---|
| 208 | *rnb = rd_size / block_size;
|
---|
| 209 | return EOK;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[7c34822e] | 212 | int main(int argc, char **argv)
|
---|
| 213 | {
|
---|
[d9fae235] | 214 | printf("%s: HelenOS RAM disk server\n", NAME);
|
---|
[a35b458] | 215 |
|
---|
[2f65fb0] | 216 | if (!rd_init())
|
---|
| 217 | return -1;
|
---|
[a35b458] | 218 |
|
---|
[d9fae235] | 219 | printf("%s: Accepting connections\n", NAME);
|
---|
[2f65fb0] | 220 | async_manager();
|
---|
[a35b458] | 221 |
|
---|
[2f65fb0] | 222 | /* Never reached */
|
---|
| 223 | return 0;
|
---|
[228b135] | 224 | }
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
| 227 | * @}
|
---|
[cb41a5e] | 228 | */
|
---|