Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
90 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
90 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
90 theseven 9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
90 theseven 14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
90 theseven 20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "console.h"
26
#include "execimage.h"
92 theseven 27
#include "mmu.h"
465 theseven 28
#include "malloc.h"
90 theseven 29
 
30
 
835 theseven 31
struct scheduler_thread* execimage(void* image, bool copy, int argc, const char** argv)
90 theseven 32
{
835 theseven 33
    int i;
453 theseven 34
    struct emcoreapp_header* header = (struct emcoreapp_header*)image;
35
    if (memcmp(header, "emCOexec", 8))
90 theseven 36
    {
453 theseven 37
        cprintf(CONSOLE_BOOT, "execimage: Bad signature: "
90 theseven 38
                              "%02X %02X %02X %02X %02X %02X %02X %02X\n",
39
                header->signature[0], header->signature[1], header->signature[2],
40
                header->signature[3], header->signature[4], header->signature[5],
41
                header->signature[6], header->signature[7]);
465 theseven 42
        if (!copy) free(image);
453 theseven 43
        return NULL;
90 theseven 44
    }
453 theseven 45
    if (header->version != EMCOREAPP_HEADER_VERSION)
90 theseven 46
    {
47
        cprintf(CONSOLE_BOOT, "execimage: Unsupported version! (%08X)\n", header->version);
465 theseven 48
        if (!copy) free(image);
453 theseven 49
        return NULL;
90 theseven 50
    }
453 theseven 51
    off_t offset = header->textstart;
52
    size_t textsize = header->textsize;
53
    size_t bsssize = header->bsssize;
54
    size_t stacksize = header->stacksize;
55
	off_t entrypoint = header->entrypoint;
56
	off_t relocstart = header->relocstart - offset;
57
	uint32_t reloccount = header->reloccount;
58
	bool compressed = header->flags & EMCOREAPP_FLAG_COMPRESSED;
465 theseven 59
	bool lib = header->flags & EMCOREAPP_FLAG_LIBRARY;
835 theseven 60
    uint32_t argsize = 0;
61
    if (argv)
62
        for (i = 0; i < argc; i++)
63
            argsize += 5 + strlen(argv[i]);
64
    else argc = 0;
465 theseven 65
    size_t finalsize;
66
    if (lib) finalsize = textsize + bsssize;
835 theseven 67
    else finalsize = textsize + bsssize + argsize + stacksize;
453 theseven 68
    size_t datasize = relocstart + reloccount * 4;
69
    size_t tempsize = MAX(finalsize, datasize);
70
    if (compressed)
90 theseven 71
    {
626 theseven 72
        void* alloc = memalign(CACHEALIGN_SIZE, tempsize);
453 theseven 73
        if (!alloc)
74
        {
75
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
465 theseven 76
            if (!copy) free(image);
453 theseven 77
            return NULL;
78
        }
79
        uint32_t decompsize;
466 theseven 80
        ucl_decompress(image + offset, datasize, alloc, &decompsize);
465 theseven 81
        if (!copy) free(image);
453 theseven 82
        if (datasize != decompsize)
83
        {
466 theseven 84
            cprintf(CONSOLE_BOOT, "execimage: Decompression failed!\n");
453 theseven 85
            free(alloc);
86
            return NULL;
87
        }
88
        image = alloc;
90 theseven 89
    }
626 theseven 90
    else if (copy || (((uint32_t)image) & (CACHEALIGN_SIZE - 1)))
453 theseven 91
    {
626 theseven 92
        void* alloc = memalign(CACHEALIGN_SIZE, tempsize);
453 theseven 93
        if (!alloc)
94
        {
95
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
626 theseven 96
            if (!copy) free(image);
453 theseven 97
            return NULL;
98
        }
99
        memcpy(alloc, image + offset, datasize);
626 theseven 100
        if (!copy) free(image);
453 theseven 101
        image = alloc;
102
    }
103
    else
104
    {
105
        memcpy(image, image + offset, datasize);
626 theseven 106
        void* newimage = realign(image, CACHEALIGN_SIZE, tempsize);
465 theseven 107
        if (!newimage)
453 theseven 108
        {
465 theseven 109
            free(image);
453 theseven 110
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
111
            return NULL;
112
        }
465 theseven 113
        else image = newimage;
453 theseven 114
    }
115
    for (; reloccount; reloccount--, relocstart += 4)
116
    {
117
        off_t reloc = *((off_t*)(image + relocstart));
118
        uint32_t data = *((uint32_t*)(image + reloc));
119
        *((void**)(image + reloc)) = image + data;
120
    }
121
    if (tempsize != finalsize) realloc(image, finalsize); /* Can only shrink => safe */
835 theseven 122
    void* ptr = image + textsize;
123
    memset(ptr, 0, bsssize);
124
    ptr += bsssize;
125
    if (argv)
126
    {
127
        memcpy(image + textsize + bsssize, argv, argc * 4);
128
        ptr += argc * 4;
129
        for (i = 0; i < argc; i++)
130
        {
131
            uint32_t len = strlen(argv[i]);
132
            memcpy(ptr, argv[i], len);
133
            ptr += len;
134
        }
135
    }
92 theseven 136
    clean_dcache();
137
    invalidate_icache();
465 theseven 138
    struct scheduler_thread* thread;
835 theseven 139
    if (lib)
140
        thread = (struct scheduler_thread*)library_register(image, image + entrypoint, argc, argv);
465 theseven 141
    else
142
    {
835 theseven 143
        thread = thread_create(NULL, NULL, image + entrypoint, ptr, stacksize,
144
                               USER_THREAD, 127, false, (void*)argc, argv, NULL, NULL);
465 theseven 145
        if (thread)
146
        {
814 theseven 147
            reownalloc(image, OWNER_TYPE(OWNER_THREAD, thread));
465 theseven 148
            thread_resume(thread);
149
        }
150
        else free(image);
151
    }
453 theseven 152
    return thread;
90 theseven 153
}