Subversion Repositories HelenOS

Rev

Rev 2932 | Rev 2961 | 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
 
50
int elf_load_file(char *file_name, elf_header_t *header)
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
 
71
void elf_run(elf_header_t *header)
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
 
2959 svoboda 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
 
2928 svoboda 97
int main(int argc, char *argv[])
98
{
2959 svoboda 99
    elf_header_t prog_header;
100
    elf_header_t interp_header;
2928 svoboda 101
    char *file_name;
102
    int rc;
103
 
104
    printf("This is loader\n");
105
    getchar();
106
 
2959 svoboda 107
    printf("Load program\n");
2928 svoboda 108
 
2959 svoboda 109
    rc = elf_load_file("/tetris", &prog_header);
2928 svoboda 110
    if (rc < 0) {
111
        printf("failed to load program\n");
112
        return 1;
113
    }
114
 
2959 svoboda 115
    printf("Create PCB\n");
116
    elf_create_pcb(&prog_header);  
2928 svoboda 117
 
2959 svoboda 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");
128
    elf_run(&interp_header);
129
 
2928 svoboda 130
    /* not reached */
131
    return 0;
132
}
133
 
134
/** @}
135
 */