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
//
427 farthen 6
//    This file is part of emCORE.
2 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
2 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,
2 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/>.
2 theseven 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"
436 theseven 33
#include "malloc.h"
85 theseven 34
#ifdef HAVE_LCD
15 theseven 35
#include "lcd.h"
2 theseven 36
#include "lcdconsole.h"
85 theseven 37
#endif
38
#ifdef HAVE_I2C
15 theseven 39
#include "i2c.h"
85 theseven 40
#endif
41
#ifdef HAVE_USB
891 theseven 42
#include "usb/usbglue.h"
85 theseven 43
#endif
44
#ifdef HAVE_STORAGE
58 theseven 45
#include "storage.h"
46
#include "disk.h"
89 theseven 47
#include "file.h"
85 theseven 48
#endif
95 theseven 49
#ifdef HAVE_BOOTFLASH
50
#include "bootflash.h"
51
#endif
191 theseven 52
#ifdef HAVE_BACKLIGHT
190 theseven 53
#include "backlight.h"
54
#endif
2 theseven 55
 
87 theseven 56
 
436 theseven 57
extern int _poolstart;   // Not an int at all, but gcc complains about void types being
58
                         // used here, and we only need the address, so just make it happy...
59
 
60
 
61
enum boottype
89 theseven 62
{
436 theseven 63
    BOOTTYPE_PIGGYBACKED = 1,
64
    BOOTTYPE_BOOTFLASH = 2,
456 theseven 65
    BOOTTYPE_FILESYSTEM = 3,
66
    BOOTTYPE_FAKESUCCESS = 4
436 theseven 67
};
68
 
69
struct bootoption
70
{
456 theseven 71
    struct bootoption* success_next;
72
    struct bootoption* fail_next;
512 theseven 73
    int type;
436 theseven 74
    char* source;
75
};
76
 
77
struct bootinfo
78
{
89 theseven 79
    char signature[8];
80
    int version;
436 theseven 81
    void* baseaddr;
82
    size_t totalsize;
83
    struct bootoption* options;
89 theseven 84
};
85
 
86
 
436 theseven 87
struct initbss
88
{
89
    struct scheduler_thread initthread;
90
    uint32_t initstack[0x400];
91
#ifdef HAVE_STORAGE
92
    struct scheduler_thread storagethread;
93
    uint32_t storagestack[0x400];
94
    struct wakeup storagewakeup;
95
#endif
96
};
97
 
98
 
427 farthen 99
static const char welcomestring[] INITCONST_ATTR = "emCORE v" VERSION " r" VERSION_SVN "\n\n";
110 theseven 100
static const char initthreadname[] INITCONST_ATTR = "Initialization thread";
436 theseven 101
static const char unknownboottypestr[] INITCONST_ATTR = "Skipping boot option with unknown type %d\n";
102
static const char nobootoptionsstr[] INITCONST_ATTR = "No usable boot options, waiting for USB commands\n\n";
249 theseven 103
#ifdef HAVE_STORAGE
436 theseven 104
static const char storagethreadname[] INITCONST_ATTR = "Storage init thread";
249 theseven 105
#endif
436 theseven 106
 
107
struct bootinfo bootinfo INITTAIL_ATTR =
89 theseven 108
{
429 theseven 109
    .signature = "emCOboot",
436 theseven 110
    .version = 1,
111
    .baseaddr = &bootinfo,
112
    .totalsize = sizeof(struct bootinfo),
113
    .options = NULL
89 theseven 114
};
15 theseven 115
 
87 theseven 116
 
89 theseven 117
#ifdef HAVE_STORAGE
835 theseven 118
void storageinitthread(void* arg0, void* arg1, void* arg2, void* arg3) INITCODE_ATTR;
119
void storageinitthread(void* arg0, void* arg1, void* arg2, void* arg3)
249 theseven 120
{
835 theseven 121
    struct initbss* ib = (struct initbss*)arg0;
249 theseven 122
    DEBUGF("Initializing storage drivers...");
921 theseven 123
    int rc = storage_init();
124
    if (IS_ERR(rc))
125
    {
126
        DEBUGF("Storage init error: %08X\n", rc);
127
        cprintf(CONSOLE_BOOT, "Storage init error: %08X\n", rc);
128
    }
249 theseven 129
    DEBUGF("Initializing storage subsystem...");
130
    disk_init_subsystem();
131
    DEBUGF("Reading partition tables...");
132
    disk_init();
133
    DEBUGF("Mounting partitions...");
134
    disk_mount_all();
135
    DEBUGF("Storage init finished.");
436 theseven 136
    wakeup_signal(&(ib->storagewakeup));
249 theseven 137
}
138
#endif
139
 
835 theseven 140
void initthread(void* arg0, void* arg1, void* arg2, void* arg3) INITCODE_ATTR;
842 theseven 141
void initthread(void* arg0, void* arg1, void* arg2, void* arg3)
2 theseven 142
{
835 theseven 143
    struct initbss* ib = (struct initbss*)arg0;
126 theseven 144
#ifdef HAVE_I2C
15 theseven 145
    i2c_init();
126 theseven 146
#endif
54 theseven 147
    power_init();
249 theseven 148
    cputs(CONSOLE_BOOT, welcomestring);
149
#ifdef HAVE_STORAGE
436 theseven 150
    wakeup_init(&(ib->storagewakeup));
835 theseven 151
    thread_create(&(ib->storagethread), storagethreadname, storageinitthread, ib->storagestack,
152
                  sizeof(ib->storagestack), USER_THREAD, 127, true, ib, NULL, NULL, NULL);
249 theseven 153
#endif
126 theseven 154
#ifdef HAVE_USB
891 theseven 155
    usbmanager_init();
126 theseven 156
#endif
190 theseven 157
#ifdef HAVE_BACKLIGHT
158
    backlight_init();
159
#endif
130 theseven 160
#ifdef HAVE_BUTTON
161
    button_init();
162
#endif
126 theseven 163
#ifdef HAVE_TARGETINIT_LATE
164
    targetinit_late();
165
#endif
249 theseven 166
#ifdef HAVE_STORAGE
167
    while (true)
168
    {
436 theseven 169
        if (wakeup_wait(&(ib->storagewakeup), 100000) == THREAD_OK) break;
170
        enum thread_state state = thread_get_state(&(ib->storagethread));
171
        if (state == THREAD_DEFUNCT_ACK)
249 theseven 172
        {
436 theseven 173
            if (wakeup_wait(&(ib->storagewakeup), 0) == THREAD_OK) break;
174
            thread_terminate(&(ib->storagethread));
249 theseven 175
            break;
176
        }
177
    }
178
#endif
179
#ifdef HAVE_TARGETINIT_VERYLATE
180
    targetinit_verylate();
181
#endif
66 theseven 182
    DEBUGF("Finished initialisation sequence");
436 theseven 183
 
456 theseven 184
    struct bootoption* option = bootinfo.options;
185
    bool success = false;
186
    while (option)
187
    {
188
        success = false;
436 theseven 189
        switch (option->type)
190
        {
191
        case BOOTTYPE_PIGGYBACKED:
835 theseven 192
            success = execimage(option->source, true, 0, NULL) != NULL;
436 theseven 193
            break;
194
 
195
#ifdef HAVE_BOOTFLASH
196
        case BOOTTYPE_BOOTFLASH:
197
        {
198
            int size = bootflash_filesize(option->source);
199
            if (size <= 0) break;
200
            void* buffer = memalign(0x10, size);
201
            if (!buffer) break;
202
            if (bootflash_read(option->source, buffer, 0, size) != size)
203
            {
204
                free(buffer);
205
                break;
206
            }
835 theseven 207
            success = execimage(buffer, false, 0, NULL) != NULL;
436 theseven 208
            break;
209
        }
210
#endif
211
 
212
#ifdef HAVE_STORAGE
213
        case BOOTTYPE_FILESYSTEM:
214
        {
215
            int fd = file_open(option->source, O_RDONLY);
216
            if (fd <= 0) break;
217
            int size = filesize(fd);
218
            if (size <= 0)
219
            {
220
                close(fd);
221
                break;
222
            }
223
            void* buffer = memalign(0x10, size);
224
            if (!buffer)
225
            {
226
                close(fd);
227
                break;
228
            }
229
            if (read(fd, buffer, size) != size)
230
            {
231
                free(buffer);
232
                close(fd);
233
                break;
234
            }
235
            close(fd);
835 theseven 236
            success = execimage(buffer, false, 0, NULL) != NULL;
436 theseven 237
            break;
238
        }
239
#endif
240
 
456 theseven 241
        case BOOTTYPE_FAKESUCCESS:
242
            success = true;
243
            break;
244
 
436 theseven 245
        default:
246
            cprintf(CONSOLE_BOOT, unknownboottypestr, option->type);
247
        }
456 theseven 248
        if (success) option = option->success_next;
249
        else option = option->fail_next;
250
    }
251
    if (!success) cputs(CONSOLE_BOOT, nobootoptionsstr);
66 theseven 252
}
253
 
254
void init() INITCODE_ATTR;
255
void init()
256
{
126 theseven 257
#ifdef HAVE_TARGETINIT_VERYEARLY
258
    targetinit_veryearly();
259
#endif
66 theseven 260
    scheduler_init();
261
    console_init();
126 theseven 262
#ifdef HAVE_LCD
66 theseven 263
    lcd_init();
264
    lcdconsole_init();
126 theseven 265
#endif
266
#ifdef HAVE_TARGETINIT_EARLY
267
    targetinit_early();
268
#endif
436 theseven 269
    malloc_init();
270
    size_t size = (size_t)(&bootinfo) - (size_t)(&_poolstart) + bootinfo.totalsize;
271
    void* bootalloc = malloc(size);
272
    size -= (size_t)(bootalloc) - (size_t)(&_poolstart);
273
    realloc(bootalloc, size);
835 theseven 274
    struct initbss* ib = (struct initbss*)malloc(sizeof(struct initbss));
814 theseven 275
    reownalloc(ib, OWNER_TYPE(OWNER_THREAD, &(ib->initthread)));
276
    reownalloc(bootalloc, OWNER_TYPE(OWNER_THREAD, &(ib->initthread)));
436 theseven 277
    thread_create(&(ib->initthread), initthreadname, initthread, ib->initstack,
842 theseven 278
                  sizeof(ib->initstack), OS_THREAD, 127, true, ib, NULL, NULL, NULL);
597 theseven 279
    timer_init();
437 theseven 280
    interrupt_init();
66 theseven 281
}