Subversion Repositories HelenOS

Rev

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

Rev 2929 Rev 2932
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
 
41
 
42
#include "elf.h"
42
#include "elf.h"
43
 
43
 
44
unsigned int elf_load(int fd, elf_header_t *header);
44
unsigned int elf_load(int fd, elf_header_t *header);
45
 
45
 
46
typedef void (*entry_point_t)(void);
46
typedef void (*entry_point_t)(void);
47
 
47
 
48
int elf_load_file(char *file_name, elf_header_t *header)
48
int elf_load_file(char *file_name, elf_header_t *header)
49
{
49
{
50
    int fd;
50
    int fd;
51
    int rc;
51
    int rc;
52
 
52
 
53
    printf("open and read '%s'...\n", file_name);
53
    printf("open and read '%s'...\n", file_name);
54
 
54
 
55
    fd = open(file_name, 0);
55
    fd = open(file_name, 0);
56
    if (fd < 0) {
56
    if (fd < 0) {
57
        printf("failed opening file\n");
57
        printf("failed opening file\n");
58
        return -1;
58
        return -1;
59
    }
59
    }
60
 
60
 
61
    rc = elf_load(fd, header);
61
    rc = elf_load(fd, header);
62
    printf("elf_load() -> %d\n", rc);
62
    printf("elf_load() -> %d\n", rc);
63
 
63
 
64
    close(fd);
64
    close(fd);
65
 
65
 
66
    return rc;
66
    return rc;
67
}
67
}
68
 
68
 
69
void elf_run(elf_header_t *header)
69
void elf_run(elf_header_t *header)
70
{
70
{
71
    entry_point_t entry_point;
71
    entry_point_t entry_point;
72
 
72
 
73
    entry_point = (entry_point_t)header->e_entry;
73
    entry_point = (entry_point_t)header->e_entry;
74
    (*entry_point)();
74
    (*entry_point)();
75
 
75
 
76
    /* not reached */
76
    /* not reached */
77
}
77
}
78
 
78
 
79
int main(int argc, char *argv[])
79
int main(int argc, char *argv[])
80
{
80
{
81
    elf_header_t header;
81
    elf_header_t header;
82
    char *file_name;
82
    char *file_name;
83
    int rc;
83
    int rc;
84
 
84
 
85
    printf("This is loader\n");
85
    printf("This is loader\n");
86
    getchar();
86
    getchar();
87
 
87
 
88
    file_name = "/tetris";
88
    file_name = "/test";
89
    rc = elf_load_file(file_name, &header);
89
    rc = elf_load_file(file_name, &header);
90
    printf("open and read '%s'...\n", file_name);
90
    printf("open and read '%s'...\n", file_name);
91
 
91
 
92
    if (rc < 0) {
92
    if (rc < 0) {
93
        printf("failed to load program\n");
93
        printf("failed to load program\n");
94
        return 1;
94
        return 1;
95
    }
95
    }
96
 
96
 
97
    printf("run program\n");
97
    printf("run program\n");
98
    elf_run(&header);
98
    elf_run(&header);
99
 
99
 
100
    /* not reached */
100
    /* not reached */
101
    return 0;
101
    return 0;
102
}
102
}
103
 
103
 
104
/** @}
104
/** @}
105
 */
105
 */
106
 
106