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