Subversion Repositories HelenOS

Rev

Rev 2959 | Rev 2962 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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