source: mainline/uspace/srv/bd/rd/rd.c@ ca48672

Last change on this file since ca48672 was ca48672, checked in by Jiri Svoboda <jiri@…>, 8 days ago

loc_service_register() needs to take a port ID argument.

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