Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
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
//
13
//    emBIOS is distributed in the hope that it will be useful,
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
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
14 theseven 25
#include "thread.h"
26
#include "console.h"
85 theseven 27
#include "power.h"
28
#include "interrupt.h"
89 theseven 29
#include "ucl.h"
30
#include "util.h"
31
#include "execimage.h"
85 theseven 32
#ifdef HAVE_LCD
15 theseven 33
#include "lcd.h"
2 theseven 34
#include "lcdconsole.h"
85 theseven 35
#endif
36
#ifdef HAVE_I2C
15 theseven 37
#include "i2c.h"
85 theseven 38
#endif
39
#ifdef HAVE_USB
40
#include "usb/usb.h"
41
#endif
42
#ifdef HAVE_STORAGE
58 theseven 43
#include "storage.h"
44
#include "disk.h"
89 theseven 45
#include "file.h"
85 theseven 46
#endif
95 theseven 47
#ifdef HAVE_BOOTFLASH
48
#include "bootflash.h"
49
#endif
2 theseven 50
 
87 theseven 51
 
89 theseven 52
struct bootinfo_t
53
{
54
    char signature[8];
55
    int version;
56
    bool trydataflash;
57
    char dataflashpath[256];
58
    bool dataflashflags;
59
    bool trybootflash;
60
    char bootimagename[8];
61
    bool bootflashflags;
62
    bool trymemmapped;
63
    void* memmappedaddr;
95 theseven 64
    int memmappedsize;
89 theseven 65
    bool memmappedflags;
66
};
67
 
68
 
32 theseven 69
static const char welcomestring[] INITCONST_ATTR = "emBIOS v" VERSION " r" VERSION_SVN "\n\n";
110 theseven 70
static const char initthreadname[] INITCONST_ATTR = "Initialization thread";
89 theseven 71
static uint32_t initstack[0x400] INITSTACK_ATTR;
72
extern int _loadspaceend;
73
extern int _initstart;
98 theseven 74
struct bootinfo_t bootinfo_src INITHEAD_ATTR =
89 theseven 75
{
76
    .signature = "emBIboot",
77
    .version = 0,
78
    .trydataflash = false,
79
    .trybootflash = false,
80
    .trymemmapped = false
81
};
15 theseven 82
 
87 theseven 83
 
89 theseven 84
void boot()
85
{
86
    struct bootinfo_t bootinfo = bootinfo_src;
87
#ifdef HAVE_STORAGE
88
    if (bootinfo.trydataflash)
89
    {
90
        int fd = file_open(bootinfo.dataflashpath, O_RDONLY);
91
        if (fd < 0) goto dataflashfailed;
95 theseven 92
        int size = filesize(fd);
93
        if (size < 0) goto dataflashfailed;
89 theseven 94
        if (bootinfo.dataflashflags & 1)
95
        {
96
            void* addr = (void*)((((uint32_t)&_loadspaceend) - size) & ~(CACHEALIGN_SIZE - 1));
97
            if (read(fd, addr, size) != size) goto dataflashfailed;
95 theseven 98
            if (ucl_decompress(addr, size, &_initstart, (uint32_t*)&size)) goto dataflashfailed;
89 theseven 99
        }
100
        else if (read(fd, &_initstart, size) != size) goto dataflashfailed;
101
        if (execimage(&_initstart) < 0) return;
102
    }
103
dataflashfailed:
104
#endif
105
#ifdef HAVE_BOOTFLASH
106
    if (bootinfo.trybootflash)
107
    {
95 theseven 108
        int size = bootflash_filesize(bootinfo.bootimagename);
89 theseven 109
        if (size < 0) goto bootflashfailed;
110
#ifdef BOOTFLASH_IS_MEMMAPPED
111
        void* addr = bootflash_getaddr(bootinfo.bootimagename);
95 theseven 112
        if (!addr) goto bootflashfailed;
89 theseven 113
        if (bootinfo.bootflashflags & 1)
114
        {
95 theseven 115
            if (ucl_decompress(addr, size, &_initstart, (uint32_t*)&size)) goto bootflashfailed;
89 theseven 116
            if (execimage(&_initstart) < 0) return;
117
        }
118
        else if (bootinfo.bootflashflags & 2)
119
        {
120
            memcpy(&_initstart, addr, size);
121
            if (execimage(&_initstart) < 0) return;
122
        }
123
        else execimage(addr);
124
#else
125
        if (bootinfo.bootflashflags & 1)
126
        {
127
            void* addr = (void*)((((uint32_t)&_loadspaceend) - size) & ~(CACHEALIGN_SIZE - 1));
95 theseven 128
            if (bootflash_read(bootinfo.bootimagename, addr, 0, size) != size)
129
                goto bootflashfailed;
130
            if (ucl_decompress(addr, size, &_initstart, (uint32_t*)&size)) goto bootflashfailed;
89 theseven 131
        }
95 theseven 132
        else if (bootflash_read(bootinfo.bootimagename, &_initstart, 0, size) != size)
133
            goto bootflashfailed;
89 theseven 134
        if (execimage(&_initstart) < 0) return;
135
#endif
136
    }
137
bootflashfailed:
138
#endif
139
    if (bootinfo.trymemmapped)
140
    {
95 theseven 141
        int size = bootinfo.memmappedsize;
89 theseven 142
        if (bootinfo.bootflashflags & 1)
143
        {
95 theseven 144
            if (ucl_decompress(bootinfo.memmappedaddr, size, &_initstart, (uint32_t*)&size))
89 theseven 145
                goto memmappedfailed;
146
            if (execimage(&_initstart) < 0) return;
147
        }
148
        else if (bootinfo.bootflashflags & 2)
149
        {
150
            memcpy(&_initstart, bootinfo.memmappedaddr, size);
151
            if (execimage(&_initstart) < 0) return;
152
        }
153
        else if (execimage(bootinfo.memmappedaddr) < 0) return;
154
    }
155
memmappedfailed:
156
    if (bootinfo.trydataflash || bootinfo.trybootflash || bootinfo.trymemmapped)
157
        cputs(CONSOLE_BOOT, "Could not find a usable boot image!\n");
158
    cputs(CONSOLE_BOOT, "Waiting for USB commands\n\n");
159
}
160
 
66 theseven 161
void initthread() INITCODE_ATTR;
162
void initthread()
2 theseven 163
{
85 theseven 164
    cputs(CONSOLE_BOOT, welcomestring);
165
	#ifdef HAVE_I2C
15 theseven 166
    i2c_init();
85 theseven 167
	#endif
54 theseven 168
    power_init();
85 theseven 169
	#ifdef HAVE_USB
15 theseven 170
    usb_init();
85 theseven 171
	#endif
172
	#ifdef HAVE_STORAGE
66 theseven 173
    DEBUGF("Initializing storage drivers...");
58 theseven 174
    storage_init();
66 theseven 175
    DEBUGF("Initializing storage subsystem...");
58 theseven 176
    disk_init_subsystem();
66 theseven 177
    DEBUGF("Reading partition tables...");
58 theseven 178
    disk_init();
66 theseven 179
    DEBUGF("Mounting partitions...");
58 theseven 180
    disk_mount_all();
85 theseven 181
	#endif
66 theseven 182
    DEBUGF("Finished initialisation sequence");
89 theseven 183
    boot();
66 theseven 184
}
185
 
186
void init() INITCODE_ATTR;
187
void init()
188
{
189
    scheduler_init();
190
    console_init();
85 theseven 191
	#ifdef HAVE_LCD
66 theseven 192
    lcd_init();
193
    lcdconsole_init();
85 theseven 194
	#endif
66 theseven 195
    interrupt_init();
196
    thread_create(initthreadname, initthread, initstack,
197
                  sizeof(initstack), USER_THREAD, 127, true);
198
}