Subversion Repositories HelenOS

Rev

Rev 2396 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1999 decky 1
/*
2435 jelen 2
 * Copyright (c) 2007 Konopa-Jelen-Majer
1999 decky 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
 
2435 jelen 29
/** @addtogroup RamDiskTask
1999 decky 30
 * @{
31
 */
32
 
33
/**
34
 * @file    rd.c
35
 * @brief   Initial RAM disk for HelenOS.
36
 */
37
 
38
#include <ipc/ipc.h>
39
#include <ipc/services.h>
40
#include <ipc/ns.h>
2005 decky 41
#include <sysinfo.h>
42
#include <as.h>
43
#include <ddi.h>
44
#include <align.h>
45
#include <bool.h>
1999 decky 46
#include <errno.h>
47
#include <async.h>
2248 jelen 48
#include <stdlib.h>
49
#include <unistd.h>
50
#include <align.h>
51
#include <async.h>
52
#include <ddi.h>
53
#include <libarch/ddi.h>
2396 konopa 54
#include <stdio.h> 
2248 jelen 55
#include "rd.h"
2396 konopa 56
#include "../console/console.h"
57
#include "../share/base_const.h"
1999 decky 58
 
2248 jelen 59
 
2396 konopa 60
#define CON_CONN_ATTEMPTS   1000
2248 jelen 61
 
2396 konopa 62
static int con_phone;
63
static void *rd_addr;
64
static void *fs_addr;
65
 
66
static int connect_to_con(int *phone, int attempts);
67
 
1999 decky 68
static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
69
{
70
    ipc_callid_t callid;
71
    ipc_call_t call;
72
    int retval;
73
 
74
    ipc_answer_fast(iid, 0, 0, 0);
2248 jelen 75
    ipcarg_t dst, offset;
1999 decky 76
 
77
    while (1) {
78
        callid = async_get_call(&call);
79
        switch (IPC_GET_METHOD(call)) {
2396 konopa 80
            case IPC_M_PHONE_HUNGUP:
81
                ipc_answer_fast(callid, 0,0,0);
82
                return;
83
            case IPC_M_AS_AREA_SEND:
84
                //print_console("Receiving shared area from FS...");
85
                ipc_answer_fast(callid, 0, (uintptr_t)fs_addr, 0);
86
                //print_console("OK\n");
87
                continue;
88
            case RD_READ_BLOCK:        
89
                offset = IPC_GET_ARG1(call);
90
                memcpy((void *)fs_addr, rd_addr+offset, BLOCK_SIZE);
91
                retval = OK;
92
                break;
93
            default: retval = EINVAL;
1999 decky 94
        }
95
        ipc_answer_fast(callid, retval, 0, 0);
96
    }  
97
}
98
 
99
 
2005 decky 100
static bool rd_init(void)
1999 decky 101
{
2396 konopa 102
 
103
    int retval, flags;
104
 
2005 decky 105
    size_t rd_size = sysinfo_value("rd.size");
106
    void * rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
2015 jermar 107
    int rd_color = (int) sysinfo_value("rd.address.color");
1999 decky 108
 
2005 decky 109
    if (rd_size == 0)
110
        return false;
1999 decky 111
 
2248 jelen 112
    rd_addr = as_get_mappable_page(rd_size, rd_color);
2005 decky 113
 
2396 konopa 114
    //print_console("Physmem mapping...");
115
    flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
116
    retval = physmem_map(rd_ph_addr, rd_addr, ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
117
 
118
    if (retval < 0) {
119
        //print_console_int("%d\n", retval);
120
        return FALSE;
121
    }  
122
    //print_console("OK\n");
123
 
124
    size_t fs_size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
125
    fs_addr = as_get_mappable_page(fs_size, PAGE_COLOR((uintptr_t)fs_addr));
126
 
127
    return TRUE;
128
}
129
 
130
int connect_to_con(int *phone, int attempts)
131
{
2005 decky 132
 
2396 konopa 133
    int i;
134
 
135
    if (attempts <= 0)
136
        return FALSE;
137
 
138
    for (i = 1; i <= attempts; i++) {
139
        *phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0);
140
 
141
        if (*phone > 0)
142
            return TRUE;
143
 
144
        usleep(10000);
145
    }
146
 
147
    if (*phone >= 0)
148
        return TRUE;
149
 
150
    return FALSE;
2005 decky 151
}
1999 decky 152
 
2005 decky 153
int main(int argc, char **argv)
154
{
2396 konopa 155
 
156
    /* Connection to SERVICE_CONSOLE service. */
157
    if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS)) {
158
        return -1;
159
    }
160
 
161
    //print_console("RAMDISK task\n");
162
 
2005 decky 163
    if (rd_init()) {
164
        ipcarg_t phonead;
165
 
166
        async_set_client_connection(rd_connection);
167
 
168
        /* Register service at nameserver */
169
        if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, &phonead) != 0)
170
            return -1;
171
 
172
        async_manager();
173
 
174
        /* Never reached */
175
        return 0;
176
    }
177
 
178
    return -1;
1999 decky 179
}
180
 
181
/**
182
 * @}
183
 */