source: mainline/uspace/srv/bd/rd/rd.c@ 9eb1ff5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9eb1ff5 was 1558d85, checked in by Jakub Jermar <jakub@…>, 9 years ago

Remove duplicate includes

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