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