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
 
453 theseven 31
struct scheduler_thread* execimage(void* image, bool copy)
90 theseven 32
{
453 theseven 33
    struct emcoreapp_header* header = (struct emcoreapp_header*)image;
34
    if (memcmp(header, "emCOexec", 8))
90 theseven 35
    {
453 theseven 36
        cprintf(CONSOLE_BOOT, "execimage: Bad signature: "
90 theseven 37
                              "%02X %02X %02X %02X %02X %02X %02X %02X\n",
38
                header->signature[0], header->signature[1], header->signature[2],
39
                header->signature[3], header->signature[4], header->signature[5],
40
                header->signature[6], header->signature[7]);
465 theseven 41
        if (!copy) free(image);
453 theseven 42
        return NULL;
90 theseven 43
    }
453 theseven 44
    if (header->version != EMCOREAPP_HEADER_VERSION)
90 theseven 45
    {
46
        cprintf(CONSOLE_BOOT, "execimage: Unsupported version! (%08X)\n", header->version);
465 theseven 47
        if (!copy) free(image);
453 theseven 48
        return NULL;
90 theseven 49
    }
453 theseven 50
    off_t offset = header->textstart;
51
    size_t textsize = header->textsize;
52
    size_t bsssize = header->bsssize;
53
    size_t stacksize = header->stacksize;
54
	off_t entrypoint = header->entrypoint;
55
	off_t relocstart = header->relocstart - offset;
56
	uint32_t reloccount = header->reloccount;
57
	bool compressed = header->flags & EMCOREAPP_FLAG_COMPRESSED;
465 theseven 58
	bool lib = header->flags & EMCOREAPP_FLAG_LIBRARY;
59
    size_t finalsize;
60
    if (lib) finalsize = textsize + bsssize;
61
    else finalsize = textsize + bsssize + stacksize;
453 theseven 62
    size_t datasize = relocstart + reloccount * 4;
63
    size_t tempsize = MAX(finalsize, datasize);
64
    if (compressed)
90 theseven 65
    {
453 theseven 66
        void* alloc = malloc(tempsize);
67
        if (!alloc)
68
        {
69
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
465 theseven 70
            if (!copy) free(image);
453 theseven 71
            return NULL;
72
        }
73
        uint32_t decompsize;
466 theseven 74
        ucl_decompress(image + offset, datasize, alloc, &decompsize);
465 theseven 75
        if (!copy) free(image);
453 theseven 76
        if (datasize != decompsize)
77
        {
466 theseven 78
            cprintf(CONSOLE_BOOT, "execimage: Decompression failed!\n");
453 theseven 79
            free(alloc);
80
            return NULL;
81
        }
82
        image = alloc;
90 theseven 83
    }
453 theseven 84
    else if (copy)
85
    {
86
        void* alloc = malloc(tempsize);
87
        if (!alloc)
88
        {
89
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
90
            return NULL;
91
        }
92
        memcpy(alloc, image + offset, datasize);
93
        image = alloc;
94
    }
95
    else
96
    {
97
        memcpy(image, image + offset, datasize);
465 theseven 98
        void* newimage = realloc(image, tempsize);
99
        if (!newimage)
453 theseven 100
        {
465 theseven 101
            free(image);
453 theseven 102
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
103
            return NULL;
104
        }
465 theseven 105
        else image = newimage;
453 theseven 106
    }
107
    for (; reloccount; reloccount--, relocstart += 4)
108
    {
109
        off_t reloc = *((off_t*)(image + relocstart));
110
        uint32_t data = *((uint32_t*)(image + reloc));
111
        *((void**)(image + reloc)) = image + data;
112
    }
113
    if (tempsize != finalsize) realloc(image, finalsize); /* Can only shrink => safe */
92 theseven 114
    clean_dcache();
115
    invalidate_icache();
465 theseven 116
    struct scheduler_thread* thread;
117
    if (lib) thread = (struct scheduler_thread*)library_register(image, image + entrypoint);
118
    else
119
    {
120
        thread = thread_create(NULL, NULL, image + entrypoint, image + textsize + bsssize,
121
                               stacksize, USER_THREAD, 127, false);
122
        if (thread)
123
        {
124
            reownalloc(image, thread);
125
            thread_resume(thread);
126
        }
127
        else free(image);
128
    }
453 theseven 129
    return thread;
90 theseven 130
}