Subversion Repositories freemyipod

Rev

Go to most recent revision | 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"
126 theseven 32
#include "targetinit.h"
85 theseven 33
#ifdef HAVE_LCD
15 theseven 34
#include "lcd.h"
2 theseven 35
#include "lcdconsole.h"
85 theseven 36
#endif
37
#ifdef HAVE_I2C
15 theseven 38
#include "i2c.h"
85 theseven 39
#endif
40
#ifdef HAVE_USB
41
#include "usb/usb.h"
42
#endif
43
#ifdef HAVE_STORAGE
58 theseven 44
#include "storage.h"
45
#include "disk.h"
89 theseven 46
#include "file.h"
85 theseven 47
#endif
95 theseven 48
#ifdef HAVE_BOOTFLASH
49
#include "bootflash.h"
50
#endif
2 theseven 51
 
87 theseven 52
 
89 theseven 53
struct bootinfo_t
54
{
55
    char signature[8];
56
    int version;
57
    bool trydataflash;
58
    char dataflashpath[256];
59
    bool dataflashflags;
60
    bool trybootflash;
61
    char bootimagename[8];
62
    bool bootflashflags;
63
    bool trymemmapped;
64
    void* memmappedaddr;
95 theseven 65
    int memmappedsize;
89 theseven 66
    bool memmappedflags;
67
};
68
 
69
 
32 theseven 70
static const char welcomestring[] INITCONST_ATTR = "emBIOS v" VERSION " r" VERSION_SVN "\n\n";
110 theseven 71
static const char initthreadname[] INITCONST_ATTR = "Initialization thread";
89 theseven 72
static uint32_t initstack[0x400] INITSTACK_ATTR;
73
extern int _loadspaceend;
74
extern int _initstart;
98 theseven 75
struct bootinfo_t bootinfo_src INITHEAD_ATTR =
89 theseven 76
{
77
    .signature = "emBIboot",
78
    .version = 0,
79
    .trydataflash = false,
80
    .trybootflash = false,
81
    .trymemmapped = false
82
};
15 theseven 83
 
87 theseven 84
 
89 theseven 85
void boot()
86
{
87
    struct bootinfo_t bootinfo = bootinfo_src;
88
#ifdef HAVE_STORAGE
89
    if (bootinfo.trydataflash)
90
    {
91
        int fd = file_open(bootinfo.dataflashpath, O_RDONLY);
92
        if (fd < 0) goto dataflashfailed;
95 theseven 93
        int size = filesize(fd);
94
        if (size < 0) goto dataflashfailed;
89 theseven 95
        if (bootinfo.dataflashflags & 1)
96
        {
97
            void* addr = (void*)((((uint32_t)&_loadspaceend) - size) & ~(CACHEALIGN_SIZE - 1));
98
            if (read(fd, addr, size) != size) goto dataflashfailed;
95 theseven 99
            if (ucl_decompress(addr, size, &_initstart, (uint32_t*)&size)) goto dataflashfailed;
89 theseven 100
        }
101
        else if (read(fd, &_initstart, size) != size) goto dataflashfailed;
133 theseven 102
        if (execimage(&_initstart) >= 0) return;
89 theseven 103
    }
104
dataflashfailed:
105
#endif
106
#ifdef HAVE_BOOTFLASH
107
    if (bootinfo.trybootflash)
108
    {
95 theseven 109
        int size = bootflash_filesize(bootinfo.bootimagename);
89 theseven 110
        if (size < 0) goto bootflashfailed;
111
#ifdef BOOTFLASH_IS_MEMMAPPED
112
        void* addr = bootflash_getaddr(bootinfo.bootimagename);
95 theseven 113
        if (!addr) goto bootflashfailed;
89 theseven 114
        if (bootinfo.bootflashflags & 1)
115
        {
95 theseven 116
            if (ucl_decompress(addr, size, &_initstart, (uint32_t*)&size)) goto bootflashfailed;
133 theseven 117
            if (execimage(&_initstart) >= 0) return;
89 theseven 118
        }
119
        else if (bootinfo.bootflashflags & 2)
120
        {
121
            memcpy(&_initstart, addr, size);
133 theseven 122
            if (execimage(&_initstart) >= 0) return;
89 theseven 123
        }
133 theseven 124
        else if (execimage(addr) >= 0) return;
89 theseven 125
#else
126
        if (bootinfo.bootflashflags & 1)
127
        {
128
            void* addr = (void*)((((uint32_t)&_loadspaceend) - size) & ~(CACHEALIGN_SIZE - 1));
95 theseven 129
            if (bootflash_read(bootinfo.bootimagename, addr, 0, size) != size)
130
                goto bootflashfailed;
131
            if (ucl_decompress(addr, size, &_initstart, (uint32_t*)&size)) goto bootflashfailed;
89 theseven 132
        }
95 theseven 133
        else if (bootflash_read(bootinfo.bootimagename, &_initstart, 0, size) != size)
134
            goto bootflashfailed;
133 theseven 135
        if (execimage(&_initstart) >= 0) return;
89 theseven 136
#endif
137
    }
138
bootflashfailed:
139
#endif
140
    if (bootinfo.trymemmapped)
141
    {
95 theseven 142
        int size = bootinfo.memmappedsize;
133 theseven 143
        if (bootinfo.memmappedflags & 1)
89 theseven 144
        {
95 theseven 145
            if (ucl_decompress(bootinfo.memmappedaddr, size, &_initstart, (uint32_t*)&size))
89 theseven 146
                goto memmappedfailed;
133 theseven 147
            if (execimage(&_initstart) >= 0) return;
89 theseven 148
        }
133 theseven 149
        else if (bootinfo.memmappedflags & 2)
89 theseven 150
        {
151
            memcpy(&_initstart, bootinfo.memmappedaddr, size);
133 theseven 152
            if (execimage(&_initstart) >= 0) return;
89 theseven 153
        }
133 theseven 154
        else if (execimage(bootinfo.memmappedaddr) >= 0) return;
89 theseven 155
    }
156
memmappedfailed:
157
    if (bootinfo.trydataflash || bootinfo.trybootflash || bootinfo.trymemmapped)
158
        cputs(CONSOLE_BOOT, "Could not find a usable boot image!\n");
159
    cputs(CONSOLE_BOOT, "Waiting for USB commands\n\n");
160
}
161
 
66 theseven 162
void initthread() INITCODE_ATTR;
163
void initthread()
2 theseven 164
{
126 theseven 165
#ifdef HAVE_I2C
15 theseven 166
    i2c_init();
126 theseven 167
#endif
54 theseven 168
    power_init();
126 theseven 169
#ifdef HAVE_USB
15 theseven 170
    usb_init();
126 theseven 171
#endif
138 theseven 172
    cputs(CONSOLE_BOOT, welcomestring);
130 theseven 173
#ifdef HAVE_BUTTON
174
    button_init();
175
#endif
126 theseven 176
#ifdef HAVE_STORAGE
66 theseven 177
    DEBUGF("Initializing storage drivers...");
58 theseven 178
    storage_init();
66 theseven 179
    DEBUGF("Initializing storage subsystem...");
58 theseven 180
    disk_init_subsystem();
66 theseven 181
    DEBUGF("Reading partition tables...");
58 theseven 182
    disk_init();
66 theseven 183
    DEBUGF("Mounting partitions...");
58 theseven 184
    disk_mount_all();
126 theseven 185
#endif
186
#ifdef HAVE_TARGETINIT_LATE
187
    targetinit_late();
188
#endif
66 theseven 189
    DEBUGF("Finished initialisation sequence");
89 theseven 190
    boot();
66 theseven 191
}
192
 
193
void init() INITCODE_ATTR;
194
void init()
195
{
126 theseven 196
#ifdef HAVE_TARGETINIT_VERYEARLY
197
    targetinit_veryearly();
198
#endif
66 theseven 199
    scheduler_init();
200
    console_init();
126 theseven 201
#ifdef HAVE_LCD
66 theseven 202
    lcd_init();
203
    lcdconsole_init();
126 theseven 204
#endif
205
#ifdef HAVE_TARGETINIT_EARLY
206
    targetinit_early();
207
#endif
66 theseven 208
    interrupt_init();
209
    thread_create(initthreadname, initthread, initstack,
210
                  sizeof(initstack), USER_THREAD, 127, true);
211
}