Rev 2989 | Rev 3101 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2989 | Rev 3004 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup loader |
29 | /** @addtogroup loader |
30 | * @brief Loads and runs programs from VFS. |
30 | * @brief Loads and runs programs from VFS. |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <stdio.h> |
37 | #include <stdio.h> |
- | 38 | #include <stdlib.h> |
|
38 | #include <unistd.h> |
39 | #include <unistd.h> |
39 | #include <fcntl.h> |
40 | #include <fcntl.h> |
40 | #include <sys/types.h> |
41 | #include <sys/types.h> |
- | 42 | #include <ipc/ipc.h> |
|
- | 43 | #include <errno.h> |
|
41 | #include <as.h> |
44 | #include <as.h> |
42 | 45 | ||
43 | #include <elf.h> |
46 | #include <elf.h> |
44 | #include <elf_load.h> |
47 | #include <elf_load.h> |
45 | #include <pcb.h> |
48 | #include <pcb.h> |
46 | 49 | ||
47 | #define RTLD_BIAS 0x80000 |
50 | #define RTLD_BIAS 0x80000 |
48 | 51 | ||
- | 52 | static char *pathname = NULL; |
|
- | 53 | ||
- | 54 | void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request) |
|
- | 55 | { |
|
- | 56 | // ipc_callid_t callid; |
|
- | 57 | size_t len; |
|
- | 58 | char *name_buf; |
|
- | 59 | ||
49 | int main(int argc, char *argv[]) |
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) |
|
50 | { |
90 | { |
51 | elf_info_t prog_info; |
- | |
52 | elf_info_t interp_info; |
- | |
53 | char *file_name; |
- | |
54 | pcb_t *pcb; |
- | |
55 | int rc; |
91 | int rc; |
- | 92 | pcb_t *pcb; |
|
56 | 93 | ||
57 | printf("This is loader\n"); |
94 | elf_info_t prog_info; |
58 | getchar(); |
95 | elf_info_t interp_info; |
59 | 96 | ||
60 | printf("Load program\n"); |
97 | printf("Load program '%s'\n", pathname); |
61 | 98 | ||
62 | rc = elf_load_file("/dltest", 0, &prog_info); |
99 | rc = elf_load_file(pathname, 0, &prog_info); |
63 | // rc = elf_load_file("/tetris", 0, &prog_info); |
- | |
64 | if (rc < 0) { |
100 | if (rc < 0) { |
65 | printf("failed to load program\n"); |
101 | printf("failed to load program\n"); |
66 | return 1; |
102 | return 1; |
67 | } |
103 | } |
68 | 104 | ||
69 | printf("Create PCB\n"); |
105 | printf("Create PCB\n"); |
70 | if (elf_create_pcb(&prog_info) < 0) return 1; |
106 | if (elf_create_pcb(&prog_info) < 0) return 1; |
71 | 107 | ||
- | 108 | if (prog_info.interp == NULL) { |
|
- | 109 | /* Statically linked program */ |
|
- | 110 | printf("Run statically linked program\n"); |
|
72 | // elf_run(&prog_info); |
111 | elf_run(&prog_info); |
73 | - | ||
74 | // getchar(); |
112 | return 0; |
- | 113 | } |
|
75 | 114 | ||
76 | printf("Load dynamic linker\n"); |
115 | printf("Load dynamic linker '%s'\n", prog_info.interp); |
77 | file_name = "/rtld.so"; |
- | |
78 | printf("open and read '%s'...\n", file_name); |
- | |
79 | rc = elf_load_file(file_name, RTLD_BIAS, &interp_info); |
116 | rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info); |
80 | if (rc < 0) { |
117 | if (rc < 0) { |
81 | printf("failed to load dynamic linker\n"); |
118 | printf("failed to load dynamic linker\n"); |
82 | return 1; |
119 | return 1; |
83 | } |
120 | } |
84 | 121 | ||
85 | /* |
122 | /* |
86 | * Provide rtld with some useful data |
123 | * Provide dynamic linker with some useful data |
87 | */ |
124 | */ |
88 | pcb = (pcb_t *)PCB_ADDRESS; |
125 | pcb = (pcb_t *)PCB_ADDRESS; |
89 | pcb->rtld_dynamic = interp_info.dynamic; |
126 | pcb->rtld_dynamic = interp_info.dynamic; |
90 | pcb->rtld_bias = RTLD_BIAS; |
127 | pcb->rtld_bias = RTLD_BIAS; |
91 | 128 | ||
92 | printf("run dynamic linker\n"); |
129 | printf("run dynamic linker\n"); |
93 | elf_run(&interp_info); |
130 | elf_run(&interp_info); |
94 | 131 | ||
- | 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 | ||
95 | /* not reached */ |
161 | /* not reached */ |
96 | return 0; |
162 | return 0; |
97 | } |
163 | } |
98 | 164 | ||
99 | /** @} |
165 | /** @} |
100 | */ |
166 | */ |
101 | 167 |