OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [loader/] [main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
#include <elf/elf.h>
2
#include <elf/elf32.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include "arch.h"
7
 
8
/* These symbols are defined by the linker script. */
9
extern char _start_kernel[];
10
extern char _end_kernel[];
11
extern char _start_containers[];
12
extern char _end_containers[];
13
 
14
/* This is a kernel symbol exported to loader's linker script from kernel build */
15
extern char bkpt_phys_to_virt[];
16
 
17
int load_elf_image(unsigned long **entry, void *filebuf);
18
 
19
/*
20
 * Given a section that is a valid elf file, look for sections
21
 * and recognise special .img.[0-9] section name and run
22
 * load_elf_image on it.
23
 */
24
int load_container_image(void *cont_section)
25
{
26
        struct Elf32_Header *elf_header = (struct Elf32_Header *)cont_section;
27
        struct Elf32_Shdr *sect_header;
28
        int nsect;
29
        int nimgs = 0;
30
        unsigned long *image_entry;
31
 
32
        if (elf32_checkFile(elf_header) < 0) {
33
                printf("%s: Not a valid elf image.\n", __FUNCTION__);
34
                return -1;
35
        }
36
 
37
        sect_header = elf32_getSectionTable(elf_header);
38
        nsect = elf32_getNumSections(elf_header);
39
 
40
        for (int i = 0; i < nsect; i++) {
41
                char *sectname = elf32_getSectionName(elf_header, i);
42
                if (!strncmp(sectname, ".img.", strlen(".img."))) {
43
                        printf("Loading %s section image...\n", sectname);
44
                        load_elf_image(&image_entry, elf32_getSection(elf_header, i));
45
                        nimgs++;
46
                }
47
        }
48
        printf("Total of %d images in this container.\n", nimgs);
49
        return 0;
50
 
51
}
52
 
53
/*
54
 * From a given offset, recognise special .cont.[0-9] section name
55
 * and run load_container_image on it.
56
 */
57
int load_container_images(unsigned long start, unsigned long end)
58
{
59
        struct Elf32_Header *elf_header = (struct Elf32_Header *)start;
60
        struct Elf32_Shdr *sect_header;
61
        int nsect = 0;
62
        int nconts = 0;
63
 
64
        if (elf32_checkFile(elf_header) < 0) {
65
                printf("Not a valid elf image.\n");
66
                return -1;
67
        }
68
 
69
        sect_header = elf32_getSectionTable(elf_header);
70
        nsect = elf32_getNumSections(elf_header);
71
 
72
        for (int i = 0; i < nsect; i++) {
73
                char *sectname = elf32_getSectionName(elf_header, i);
74
                if (!strncmp(sectname, ".cont.", strlen(".cont."))) {
75
                        nconts++;
76
                        printf("\nLoading section %s from top-level elf file.\n", sectname);
77
                        load_container_image(elf32_getSection(elf_header, i));
78
                }
79
        }
80
        printf("Total of %d container images.\n", nconts);
81
        return 0;
82
}
83
 
84
 
85
int load_elf_image(unsigned long **entry, void *filebuf)
86
{
87
        if (!elf32_checkFile((struct Elf32_Header *)filebuf)) {
88
                **entry = (unsigned long)elf32_getEntryPoint((struct Elf32_Header *)filebuf);
89
                printf("Entry point: 0x%lx\n", **entry);
90
        } else {
91
                printf("Not a valid elf image.\n");
92
                return -1;
93
        }
94
        if (!elf_loadFile(filebuf, 1)) {
95
                printf("Elf image seems valid, but unable to load.\n");
96
                return -1;
97
        }
98
        return 0;
99
}
100
 
101
void arch_start_kernel(void *entry)
102
{
103
        printf("elf-loader:\tStarting kernel\n\r");
104
        void (*func)(unsigned long) = (void (*)(unsigned long)) (*(unsigned long*)entry);
105
        func(0);
106
}
107
 
108
int main(void)
109
{
110
        unsigned long *kernel_entry;
111
 
112
        printf("ELF Loader: Started.\n");
113
 
114
        printf("Loading the kernel...\n");
115
        load_elf_image(&kernel_entry, (void *)_start_kernel);
116
 
117
        printf("Loading containers...\n");
118
        load_container_images((unsigned long)_start_containers,
119
                              (unsigned long)_end_containers);
120
 
121
        printf("elf-loader:\tkernel entry point is 0x%lx\n", *kernel_entry);
122
        arch_start_kernel(kernel_entry);
123
 
124
        printf("elf-loader:\tKernel start failed! Looping endless.\n");
125
        while (1)
126
                ;
127
 
128
        return -1;
129
}
130
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.