Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
2928 svoboda 1
/*
2
 * Copyright (c) 2008 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 loader
30
 * @brief   Loads and runs programs from VFS.
31
 * @{
32
 */
33
/**
34
 * @file
35
 */
36
 
37
#include <stdio.h>
3004 svoboda 38
#include <stdlib.h>
2928 svoboda 39
#include <unistd.h>
40
#include <fcntl.h>
41
#include <sys/types.h>
3004 svoboda 42
#include <ipc/ipc.h>
43
#include <errno.h>
2959 svoboda 44
#include <as.h>
2928 svoboda 45
 
2969 svoboda 46
#include <elf.h>
47
#include <elf_load.h>
2989 svoboda 48
#include <pcb.h>
2928 svoboda 49
 
2972 svoboda 50
#define RTLD_BIAS 0x80000
51
 
3004 svoboda 52
static char *pathname = NULL;
53
 
54
void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
2928 svoboda 55
{
3004 svoboda 56
//  ipc_callid_t callid;
57
    size_t len;
58
    char *name_buf;
59
 
60
/*  printf("iloader_set_pathname\n");
61
    if (!ipc_data_write_receive(&callid, &len)) {
62
        ipc_answer_0(callid, EINVAL);
63
        ipc_answer_0(rid, EINVAL);
64
        return;
65
    }
66
*/
67
    len = IPC_GET_ARG2(*request);
68
    printf("alloc %d bytes\n", len+1);
69
 
70
    name_buf = malloc(len + 1);
71
    if (!name_buf) {
72
//      ipc_answer_0(callid, ENOMEM);
73
        ipc_answer_0(rid, ENOMEM);
74
        return;
75
    }
76
 
77
    printf("write_finalize\n");
78
    ipc_data_write_finalize(rid, name_buf, len);
79
//  ipc_answer_0(rid, EOK);
80
 
81
    if (pathname != NULL) {
82
        free(pathname);
83
        pathname = NULL;
84
    }
85
 
86
    pathname = name_buf;
87
}
88
 
89
int iloader_run(ipc_callid_t rid, ipc_call_t *request)
90
{
91
    int rc;
92
    pcb_t *pcb;
93
 
2964 svoboda 94
    elf_info_t prog_info;
95
    elf_info_t interp_info;
2928 svoboda 96
 
3004 svoboda 97
    printf("Load program '%s'\n", pathname);
2928 svoboda 98
 
3004 svoboda 99
    rc = elf_load_file(pathname, 0, &prog_info);
2928 svoboda 100
    if (rc < 0) {
101
        printf("failed to load program\n");
102
        return 1;
103
    }
104
 
2959 svoboda 105
    printf("Create PCB\n");
2964 svoboda 106
    if (elf_create_pcb(&prog_info) < 0) return 1;
2928 svoboda 107
 
3004 svoboda 108
    if (prog_info.interp == NULL) {
109
        /* Statically linked program */
110
        printf("Run statically linked program\n");
111
        elf_run(&prog_info);
112
        return 0;
113
    }
2988 svoboda 114
 
3004 svoboda 115
    printf("Load dynamic linker '%s'\n", prog_info.interp);
116
    rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
2959 svoboda 117
    if (rc < 0) {
118
        printf("failed to load dynamic linker\n");
119
        return 1;
120
    }
121
 
2989 svoboda 122
    /*
3004 svoboda 123
     * Provide dynamic linker with some useful data
2989 svoboda 124
     */
125
    pcb = (pcb_t *)PCB_ADDRESS;
126
    pcb->rtld_dynamic = interp_info.dynamic;
127
    pcb->rtld_bias = RTLD_BIAS;
128
 
2959 svoboda 129
    printf("run dynamic linker\n");
2964 svoboda 130
    elf_run(&interp_info);
2989 svoboda 131
 
3004 svoboda 132
    return 0;
133
}
134
 
135
int main(int argc, char *argv[])
136
{
137
    ipc_callid_t callid;
138
    ipc_call_t call;
139
    int retval;
140
    int len;
141
 
142
    while (1) {
143
        callid = ipc_wait_for_call(&call);
144
        printf("received call, method=%d\n", IPC_GET_METHOD(call));
145
        switch (IPC_GET_METHOD(call)) {
146
        case IPC_M_DATA_WRITE:
147
            iloader_set_pathname(callid, &call);
148
            iloader_run(callid, &call);
149
            exit(0);
150
            continue;
151
        default:
152
            retval = ENOENT;
153
            break;
154
        }
155
        if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
156
            printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
157
            ipc_answer_0(callid, EINVAL);
158
        }
159
    }
160
 
2928 svoboda 161
    /* not reached */
162
    return 0;
163
}
164
 
165
/** @}
166
 */