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
    {
626 theseven 66
        void* alloc = memalign(CACHEALIGN_SIZE, tempsize);
453 theseven 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
    }
626 theseven 84
    else if (copy || (((uint32_t)image) & (CACHEALIGN_SIZE - 1)))
453 theseven 85
    {
626 theseven 86
        void* alloc = memalign(CACHEALIGN_SIZE, tempsize);
453 theseven 87
        if (!alloc)
88
        {
89
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
626 theseven 90
            if (!copy) free(image);
453 theseven 91
            return NULL;
92
        }
93
        memcpy(alloc, image + offset, datasize);
626 theseven 94
        if (!copy) free(image);
453 theseven 95
        image = alloc;
96
    }
97
    else
98
    {
99
        memcpy(image, image + offset, datasize);
626 theseven 100
        void* newimage = realign(image, CACHEALIGN_SIZE, tempsize);
465 theseven 101
        if (!newimage)
453 theseven 102
        {
465 theseven 103
            free(image);
453 theseven 104
            cprintf(CONSOLE_BOOT, "execimage: Out of memory!\n");
105
            return NULL;
106
        }
465 theseven 107
        else image = newimage;
453 theseven 108
    }
109
    for (; reloccount; reloccount--, relocstart += 4)
110
    {
111
        off_t reloc = *((off_t*)(image + relocstart));
112
        uint32_t data = *((uint32_t*)(image + reloc));
113
        *((void**)(image + reloc)) = image + data;
114
    }
115
    if (tempsize != finalsize) realloc(image, finalsize); /* Can only shrink => safe */
617 theseven 116
    memset(image + textsize, 0, bsssize);
92 theseven 117
    clean_dcache();
118
    invalidate_icache();
465 theseven 119
    struct scheduler_thread* thread;
120
    if (lib) thread = (struct scheduler_thread*)library_register(image, image + entrypoint);
121
    else
122
    {
123
        thread = thread_create(NULL, NULL, image + entrypoint, image + textsize + bsssize,
124
                               stacksize, USER_THREAD, 127, false);
125
        if (thread)
126
        {
127
            reownalloc(image, thread);
128
            thread_resume(thread);
129
        }
130
        else free(image);
131
    }
453 theseven 132
    return thread;
90 theseven 133
}