| [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>
|
|---|
| 48 | #include <bool.h>
|
|---|
| [228b135] | 49 | #include <errno.h>
|
|---|
| 50 | #include <async.h>
|
|---|
| [ff3a34b] | 51 | #include <align.h>
|
|---|
| 52 | #include <async.h>
|
|---|
| [1e4cada] | 53 | #include <fibril_synch.h>
|
|---|
| [2f65fb0] | 54 | #include <stdio.h>
|
|---|
| [15f3c3f] | 55 | #include <loc.h>
|
|---|
| [1ee00b7] | 56 | #include <macros.h>
|
|---|
| [b5daa89] | 57 | #include <inttypes.h>
|
|---|
| [228b135] | 58 |
|
|---|
| [b5daa89] | 59 | #define NAME "rd"
|
|---|
| [2f65fb0] | 60 |
|
|---|
| [1ee00b7] | 61 | /** Pointer to the ramdisk's image */
|
|---|
| [ff3a34b] | 62 | static void *rd_addr;
|
|---|
| [d9fae235] | 63 |
|
|---|
| [1ee00b7] | 64 | /** Size of the ramdisk */
|
|---|
| [3ae470a] | 65 | static size_t rd_size;
|
|---|
| [228b135] | 66 |
|
|---|
| [1ee00b7] | 67 | /** Block size */
|
|---|
| 68 | static const size_t block_size = 512;
|
|---|
| 69 |
|
|---|
| [135486d] | 70 | static int rd_open(bd_srvs_t *, bd_srv_t *);
|
|---|
| [4802dd7] | 71 | static int rd_close(bd_srv_t *);
|
|---|
| 72 | static int rd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
|
|---|
| 73 | static int rd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
|
|---|
| 74 | static int rd_get_block_size(bd_srv_t *, size_t *);
|
|---|
| 75 | static int rd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
|---|
| [1ee00b7] | 76 |
|
|---|
| [d9fae235] | 77 | /** This rwlock protects the ramdisk's data.
|
|---|
| 78 | *
|
|---|
| [84947a4] | 79 | * If we were to serve multiple requests (read + write or several writes)
|
|---|
| [d9fae235] | 80 | * concurrently (i.e. from two or more threads), each read and write needs to
|
|---|
| 81 | * be protected by this rwlock.
|
|---|
| 82 | *
|
|---|
| 83 | */
|
|---|
| [4802dd7] | 84 | static fibril_rwlock_t rd_lock;
|
|---|
| [84947a4] | 85 |
|
|---|
| [4802dd7] | 86 | static bd_ops_t rd_bd_ops = {
|
|---|
| 87 | .open = rd_open,
|
|---|
| 88 | .close = rd_close,
|
|---|
| 89 | .read_blocks = rd_read_blocks,
|
|---|
| 90 | .write_blocks = rd_write_blocks,
|
|---|
| 91 | .get_block_size = rd_get_block_size,
|
|---|
| 92 | .get_num_blocks = rd_get_num_blocks
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| [135486d] | 95 | static bd_srvs_t bd_srvs;
|
|---|
| [4802dd7] | 96 |
|
|---|
| 97 | static void rd_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| [228b135] | 98 | {
|
|---|
| [135486d] | 99 | bd_conn(iid, icall, &bd_srvs);
|
|---|
| [4802dd7] | 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Open device. */
|
|---|
| [135486d] | 103 | static int rd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
|---|
| [4802dd7] | 104 | {
|
|---|
| 105 | return EOK;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Close device. */
|
|---|
| 109 | static int rd_close(bd_srv_t *bd)
|
|---|
| 110 | {
|
|---|
| 111 | return EOK;
|
|---|
| [228b135] | 112 | }
|
|---|
| 113 |
|
|---|
| [1ee00b7] | 114 | /** Read blocks from the device. */
|
|---|
| [4802dd7] | 115 | static int rd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
|
|---|
| 116 | size_t size)
|
|---|
| [1ee00b7] | 117 | {
|
|---|
| 118 | if ((ba + cnt) * block_size > rd_size) {
|
|---|
| 119 | /* Reading past the end of the device. */
|
|---|
| 120 | return ELIMIT;
|
|---|
| 121 | }
|
|---|
| [d9fae235] | 122 |
|
|---|
| [1ee00b7] | 123 | fibril_rwlock_read_lock(&rd_lock);
|
|---|
| [4802dd7] | 124 | memcpy(buf, rd_addr + ba * block_size, min(block_size * cnt, size));
|
|---|
| [1ee00b7] | 125 | fibril_rwlock_read_unlock(&rd_lock);
|
|---|
| [d9fae235] | 126 |
|
|---|
| [1ee00b7] | 127 | return EOK;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** Write blocks to the device. */
|
|---|
| [4802dd7] | 131 | static int rd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 132 | const void *buf, size_t size)
|
|---|
| [1ee00b7] | 133 | {
|
|---|
| 134 | if ((ba + cnt) * block_size > rd_size) {
|
|---|
| 135 | /* Writing past the end of the device. */
|
|---|
| 136 | return ELIMIT;
|
|---|
| 137 | }
|
|---|
| [d9fae235] | 138 |
|
|---|
| [1ee00b7] | 139 | fibril_rwlock_write_lock(&rd_lock);
|
|---|
| [4802dd7] | 140 | memcpy(rd_addr + ba * block_size, buf, min(block_size * cnt, size));
|
|---|
| [1ee00b7] | 141 | fibril_rwlock_write_unlock(&rd_lock);
|
|---|
| [d9fae235] | 142 |
|
|---|
| [1ee00b7] | 143 | return EOK;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| [84947a4] | 146 | /** Prepare the ramdisk image for operation. */
|
|---|
| [7c34822e] | 147 | static bool rd_init(void)
|
|---|
| [228b135] | 148 | {
|
|---|
| [b5daa89] | 149 | sysarg_t size;
|
|---|
| 150 | int ret = sysinfo_get_value("rd.size", &size);
|
|---|
| 151 | if ((ret != EOK) || (size == 0)) {
|
|---|
| [d9fae235] | 152 | printf("%s: No RAM disk found\n", NAME);
|
|---|
| 153 | return false;
|
|---|
| 154 | }
|
|---|
| [7c34822e] | 155 |
|
|---|
| [b5daa89] | 156 | sysarg_t addr_phys;
|
|---|
| 157 | ret = sysinfo_get_value("rd.address.physical", &addr_phys);
|
|---|
| 158 | if ((ret != EOK) || (addr_phys == 0)) {
|
|---|
| [d9fae235] | 159 | printf("%s: Invalid RAM disk physical address\n", NAME);
|
|---|
| [7c34822e] | 160 | return false;
|
|---|
| [2f65fb0] | 161 | }
|
|---|
| [228b135] | 162 |
|
|---|
| [b5daa89] | 163 | rd_size = ALIGN_UP(size, block_size);
|
|---|
| 164 | unsigned int flags =
|
|---|
| 165 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
|
|---|
| [fbcdeb8] | 166 |
|
|---|
| 167 | ret = physmem_map((void *) addr_phys,
|
|---|
| 168 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags, &rd_addr);
|
|---|
| 169 | if (ret != EOK) {
|
|---|
| [d9fae235] | 170 | printf("%s: Error mapping RAM disk\n", NAME);
|
|---|
| [2f65fb0] | 171 | return false;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| [b5daa89] | 174 | printf("%s: Found RAM disk at %p, %" PRIun " bytes\n", NAME,
|
|---|
| 175 | (void *) addr_phys, size);
|
|---|
| [2f65fb0] | 176 |
|
|---|
| [135486d] | 177 | bd_srvs_init(&bd_srvs);
|
|---|
| 178 | bd_srvs.ops = &rd_bd_ops;
|
|---|
| [4802dd7] | 179 |
|
|---|
| 180 | async_set_client_connection(rd_client_conn);
|
|---|
| [f302586] | 181 | ret = loc_server_register(NAME);
|
|---|
| [b16e77d] | 182 | if (ret != EOK) {
|
|---|
| [b5daa89] | 183 | printf("%s: Unable to register driver (%d)\n", NAME, ret);
|
|---|
| [2f65fb0] | 184 | return false;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| [15f3c3f] | 187 | service_id_t service_id;
|
|---|
| [b5daa89] | 188 | ret = loc_service_register("bd/initrd", &service_id);
|
|---|
| 189 | if (ret != EOK) {
|
|---|
| [15f3c3f] | 190 | printf("%s: Unable to register device service\n", NAME);
|
|---|
| [537611cc] | 191 | return false;
|
|---|
| 192 | }
|
|---|
| [79ae36dd] | 193 |
|
|---|
| [52e4f526] | 194 | fibril_rwlock_initialize(&rd_lock);
|
|---|
| [2f65fb0] | 195 |
|
|---|
| [7c34822e] | 196 | return true;
|
|---|
| 197 | }
|
|---|
| [228b135] | 198 |
|
|---|
| [4802dd7] | 199 | /** Get device block size. */
|
|---|
| 200 | static int rd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
|---|
| 201 | {
|
|---|
| 202 | *rsize = block_size;
|
|---|
| 203 | return EOK;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Get number of blocks on device. */
|
|---|
| 207 | static int rd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
|---|
| 208 | {
|
|---|
| 209 | *rnb = rd_size / block_size;
|
|---|
| 210 | return EOK;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| [7c34822e] | 213 | int main(int argc, char **argv)
|
|---|
| 214 | {
|
|---|
| [d9fae235] | 215 | printf("%s: HelenOS RAM disk server\n", NAME);
|
|---|
| [7c34822e] | 216 |
|
|---|
| [2f65fb0] | 217 | if (!rd_init())
|
|---|
| 218 | return -1;
|
|---|
| 219 |
|
|---|
| [d9fae235] | 220 | printf("%s: Accepting connections\n", NAME);
|
|---|
| [2f65fb0] | 221 | async_manager();
|
|---|
| [79ae36dd] | 222 |
|
|---|
| [2f65fb0] | 223 | /* Never reached */
|
|---|
| 224 | return 0;
|
|---|
| [228b135] | 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * @}
|
|---|
| [cb41a5e] | 229 | */
|
|---|