Subversion Repositories HelenOS

Rev

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

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