1 | /*
|
---|
2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libdevice
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Block device server stub
|
---|
35 | */
|
---|
36 | #include <errno.h>
|
---|
37 | #include <ipc/bd.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <stddef.h>
|
---|
41 | #include <stdint.h>
|
---|
42 |
|
---|
43 | #include <bd_srv.h>
|
---|
44 |
|
---|
45 | static void bd_read_blocks_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
46 | {
|
---|
47 | aoff64_t ba;
|
---|
48 | size_t cnt;
|
---|
49 | void *buf;
|
---|
50 | size_t size;
|
---|
51 | errno_t rc;
|
---|
52 |
|
---|
53 | ba = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
|
---|
54 | cnt = ipc_get_arg3(call);
|
---|
55 |
|
---|
56 | ipc_call_t rcall;
|
---|
57 | if (!async_data_read_receive(&rcall, &size)) {
|
---|
58 | async_answer_0(&rcall, EINVAL);
|
---|
59 | async_answer_0(call, EINVAL);
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | buf = malloc(size);
|
---|
64 | if (buf == NULL) {
|
---|
65 | async_answer_0(&rcall, ENOMEM);
|
---|
66 | async_answer_0(call, ENOMEM);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (srv->srvs->ops->read_blocks == NULL) {
|
---|
71 | async_answer_0(&rcall, ENOTSUP);
|
---|
72 | async_answer_0(call, ENOTSUP);
|
---|
73 | free(buf);
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | rc = srv->srvs->ops->read_blocks(srv, ba, cnt, buf, size);
|
---|
78 | if (rc != EOK) {
|
---|
79 | async_answer_0(&rcall, ENOMEM);
|
---|
80 | async_answer_0(call, ENOMEM);
|
---|
81 | free(buf);
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | async_data_read_finalize(&rcall, buf, size);
|
---|
86 |
|
---|
87 | free(buf);
|
---|
88 | async_answer_0(call, EOK);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void bd_read_toc_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
92 | {
|
---|
93 | uint8_t session;
|
---|
94 | void *buf;
|
---|
95 | size_t size;
|
---|
96 | errno_t rc;
|
---|
97 |
|
---|
98 | session = ipc_get_arg1(call);
|
---|
99 |
|
---|
100 | ipc_call_t rcall;
|
---|
101 | if (!async_data_read_receive(&rcall, &size)) {
|
---|
102 | async_answer_0(&rcall, EINVAL);
|
---|
103 | async_answer_0(call, EINVAL);
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | buf = malloc(size);
|
---|
108 | if (buf == NULL) {
|
---|
109 | async_answer_0(&rcall, ENOMEM);
|
---|
110 | async_answer_0(call, ENOMEM);
|
---|
111 | return;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (srv->srvs->ops->read_toc == NULL) {
|
---|
115 | async_answer_0(&rcall, ENOTSUP);
|
---|
116 | async_answer_0(call, ENOTSUP);
|
---|
117 | free(buf);
|
---|
118 | return;
|
---|
119 | }
|
---|
120 |
|
---|
121 | rc = srv->srvs->ops->read_toc(srv, session, buf, size);
|
---|
122 | if (rc != EOK) {
|
---|
123 | async_answer_0(&rcall, ENOMEM);
|
---|
124 | async_answer_0(call, ENOMEM);
|
---|
125 | free(buf);
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | async_data_read_finalize(&rcall, buf, size);
|
---|
130 |
|
---|
131 | free(buf);
|
---|
132 | async_answer_0(call, EOK);
|
---|
133 | }
|
---|
134 |
|
---|
135 | static void bd_sync_cache_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
136 | {
|
---|
137 | aoff64_t ba;
|
---|
138 | size_t cnt;
|
---|
139 | errno_t rc;
|
---|
140 |
|
---|
141 | ba = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
|
---|
142 | cnt = ipc_get_arg3(call);
|
---|
143 |
|
---|
144 | if (srv->srvs->ops->sync_cache == NULL) {
|
---|
145 | async_answer_0(call, ENOTSUP);
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | rc = srv->srvs->ops->sync_cache(srv, ba, cnt);
|
---|
150 | async_answer_0(call, rc);
|
---|
151 | }
|
---|
152 |
|
---|
153 | static void bd_write_blocks_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
154 | {
|
---|
155 | aoff64_t ba;
|
---|
156 | size_t cnt;
|
---|
157 | void *data;
|
---|
158 | size_t size;
|
---|
159 | errno_t rc;
|
---|
160 |
|
---|
161 | ba = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
|
---|
162 | cnt = ipc_get_arg3(call);
|
---|
163 |
|
---|
164 | rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
|
---|
165 | if (rc != EOK) {
|
---|
166 | async_answer_0(call, rc);
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (srv->srvs->ops->write_blocks == NULL) {
|
---|
171 | async_answer_0(call, ENOTSUP);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 |
|
---|
175 | rc = srv->srvs->ops->write_blocks(srv, ba, cnt, data, size);
|
---|
176 | free(data);
|
---|
177 | async_answer_0(call, rc);
|
---|
178 | }
|
---|
179 |
|
---|
180 | static void bd_get_block_size_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
181 | {
|
---|
182 | errno_t rc;
|
---|
183 | size_t block_size;
|
---|
184 |
|
---|
185 | if (srv->srvs->ops->get_block_size == NULL) {
|
---|
186 | async_answer_0(call, ENOTSUP);
|
---|
187 | return;
|
---|
188 | }
|
---|
189 |
|
---|
190 | rc = srv->srvs->ops->get_block_size(srv, &block_size);
|
---|
191 | async_answer_1(call, rc, block_size);
|
---|
192 | }
|
---|
193 |
|
---|
194 | static void bd_get_num_blocks_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
195 | {
|
---|
196 | errno_t rc;
|
---|
197 | aoff64_t num_blocks;
|
---|
198 |
|
---|
199 | if (srv->srvs->ops->get_num_blocks == NULL) {
|
---|
200 | async_answer_0(call, ENOTSUP);
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | rc = srv->srvs->ops->get_num_blocks(srv, &num_blocks);
|
---|
205 | async_answer_2(call, rc, LOWER32(num_blocks), UPPER32(num_blocks));
|
---|
206 | }
|
---|
207 |
|
---|
208 | static bd_srv_t *bd_srv_create(bd_srvs_t *srvs)
|
---|
209 | {
|
---|
210 | bd_srv_t *srv;
|
---|
211 |
|
---|
212 | srv = calloc(1, sizeof(bd_srv_t));
|
---|
213 | if (srv == NULL)
|
---|
214 | return NULL;
|
---|
215 |
|
---|
216 | srv->srvs = srvs;
|
---|
217 | return srv;
|
---|
218 | }
|
---|
219 |
|
---|
220 | void bd_srvs_init(bd_srvs_t *srvs)
|
---|
221 | {
|
---|
222 | srvs->ops = NULL;
|
---|
223 | srvs->sarg = NULL;
|
---|
224 | }
|
---|
225 |
|
---|
226 | errno_t bd_conn(ipc_call_t *icall, bd_srvs_t *srvs)
|
---|
227 | {
|
---|
228 | bd_srv_t *srv;
|
---|
229 | errno_t rc;
|
---|
230 |
|
---|
231 | /* Accept the connection */
|
---|
232 | async_accept_0(icall);
|
---|
233 |
|
---|
234 | srv = bd_srv_create(srvs);
|
---|
235 | if (srv == NULL)
|
---|
236 | return ENOMEM;
|
---|
237 |
|
---|
238 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
239 | if (sess == NULL)
|
---|
240 | return ENOMEM;
|
---|
241 |
|
---|
242 | srv->client_sess = sess;
|
---|
243 |
|
---|
244 | rc = srvs->ops->open(srvs, srv);
|
---|
245 | if (rc != EOK)
|
---|
246 | return rc;
|
---|
247 |
|
---|
248 | while (true) {
|
---|
249 | ipc_call_t call;
|
---|
250 | async_get_call(&call);
|
---|
251 | sysarg_t method = ipc_get_imethod(&call);
|
---|
252 |
|
---|
253 | if (!method) {
|
---|
254 | /* The other side has hung up */
|
---|
255 | async_answer_0(&call, EOK);
|
---|
256 | break;
|
---|
257 | }
|
---|
258 |
|
---|
259 | switch (method) {
|
---|
260 | case BD_READ_BLOCKS:
|
---|
261 | bd_read_blocks_srv(srv, &call);
|
---|
262 | break;
|
---|
263 | case BD_READ_TOC:
|
---|
264 | bd_read_toc_srv(srv, &call);
|
---|
265 | break;
|
---|
266 | case BD_SYNC_CACHE:
|
---|
267 | bd_sync_cache_srv(srv, &call);
|
---|
268 | break;
|
---|
269 | case BD_WRITE_BLOCKS:
|
---|
270 | bd_write_blocks_srv(srv, &call);
|
---|
271 | break;
|
---|
272 | case BD_GET_BLOCK_SIZE:
|
---|
273 | bd_get_block_size_srv(srv, &call);
|
---|
274 | break;
|
---|
275 | case BD_GET_NUM_BLOCKS:
|
---|
276 | bd_get_num_blocks_srv(srv, &call);
|
---|
277 | break;
|
---|
278 | default:
|
---|
279 | async_answer_0(&call, EINVAL);
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | rc = srvs->ops->close(srv);
|
---|
284 | free(srv);
|
---|
285 |
|
---|
286 | return rc;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** @}
|
---|
290 | */
|
---|