source: mainline/uspace/srv/bd/rd/rd.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

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