Rev 2959 | Rev 2962 | 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> |
||
38 | #include <unistd.h> |
||
39 | #include <fcntl.h> |
||
40 | #include <sys/types.h> |
||
2959 | svoboda | 41 | #include <as.h> |
2928 | svoboda | 42 | |
43 | #include "elf.h" |
||
2959 | svoboda | 44 | #include "pcb.h" |
2928 | svoboda | 45 | |
46 | unsigned int elf_load(int fd, elf_header_t *header); |
||
47 | |||
48 | typedef void (*entry_point_t)(void); |
||
49 | |||
2961 | svoboda | 50 | static int elf_load_file(char *file_name, elf_header_t *header) |
2928 | svoboda | 51 | { |
52 | int fd; |
||
53 | int rc; |
||
54 | |||
55 | printf("open and read '%s'...\n", file_name); |
||
56 | |||
57 | fd = open(file_name, 0); |
||
58 | if (fd < 0) { |
||
59 | printf("failed opening file\n"); |
||
60 | return -1; |
||
61 | } |
||
62 | |||
63 | rc = elf_load(fd, header); |
||
64 | printf("elf_load() -> %d\n", rc); |
||
65 | |||
66 | close(fd); |
||
67 | |||
68 | return rc; |
||
69 | } |
||
70 | |||
2961 | svoboda | 71 | static void elf_run(elf_header_t *header) |
2928 | svoboda | 72 | { |
73 | entry_point_t entry_point; |
||
74 | |||
75 | entry_point = (entry_point_t)header->e_entry; |
||
76 | (*entry_point)(); |
||
77 | |||
78 | /* not reached */ |
||
79 | } |
||
80 | |||
2961 | svoboda | 81 | static int elf_create_pcb(elf_header_t *header) |
2959 | svoboda | 82 | { |
83 | pcb_t *pcb; |
||
84 | void *a; |
||
85 | |||
86 | pcb = (pcb_t *)PCB_ADDRESS; |
||
87 | |||
88 | a = as_area_create(pcb, sizeof(pcb_t), AS_AREA_READ | AS_AREA_WRITE); |
||
89 | if (a == (void *)(-1)) { |
||
90 | printf("elf_create_pcb: memory mapping failed\n"); |
||
91 | return EE_MEMORY; |
||
92 | } |
||
93 | |||
94 | pcb->entry = (entry_point_t)header->e_entry; |
||
2961 | svoboda | 95 | |
96 | return 0; |
||
2959 | svoboda | 97 | } |
98 | |||
2928 | svoboda | 99 | int main(int argc, char *argv[]) |
100 | { |
||
2959 | svoboda | 101 | elf_header_t prog_header; |
102 | elf_header_t interp_header; |
||
2928 | svoboda | 103 | char *file_name; |
104 | int rc; |
||
105 | |||
106 | printf("This is loader\n"); |
||
107 | getchar(); |
||
108 | |||
2959 | svoboda | 109 | printf("Load program\n"); |
2928 | svoboda | 110 | |
2959 | svoboda | 111 | rc = elf_load_file("/tetris", &prog_header); |
2928 | svoboda | 112 | if (rc < 0) { |
113 | printf("failed to load program\n"); |
||
114 | return 1; |
||
115 | } |
||
116 | |||
2959 | svoboda | 117 | printf("Create PCB\n"); |
2961 | svoboda | 118 | if (elf_create_pcb(&prog_header) < 0) return 1; |
2928 | svoboda | 119 | |
2959 | svoboda | 120 | printf("Load dynamic linker\n"); |
121 | file_name = "/rtld.so"; |
||
122 | printf("open and read '%s'...\n", file_name); |
||
123 | rc = elf_load_file(file_name, &interp_header); |
||
124 | if (rc < 0) { |
||
125 | printf("failed to load dynamic linker\n"); |
||
126 | return 1; |
||
127 | } |
||
128 | |||
129 | printf("run dynamic linker\n"); |
||
130 | elf_run(&interp_header); |
||
131 | |||
2928 | svoboda | 132 | /* not reached */ |
133 | return 0; |
||
134 | } |
||
135 | |||
136 | /** @} |
||
137 | */ |