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...");
123
    storage_init();
124
    DEBUGF("Initializing storage subsystem...");
125
    disk_init_subsystem();
126
    DEBUGF("Reading partition tables...");
127
    disk_init();
128
    DEBUGF("Mounting partitions...");
129
    disk_mount_all();
130
    DEBUGF("Storage init finished.");
436 theseven 131
    wakeup_signal(&(ib->storagewakeup));
249 theseven 132
}
133
#endif
134
 
835 theseven 135
void initthread(void* arg0, void* arg1, void* arg2, void* arg3) INITCODE_ATTR;
842 theseven 136
void initthread(void* arg0, void* arg1, void* arg2, void* arg3)
2 theseven 137
{
835 theseven 138
    struct initbss* ib = (struct initbss*)arg0;
126 theseven 139
#ifdef HAVE_I2C
15 theseven 140
    i2c_init();
126 theseven 141
#endif
54 theseven 142
    power_init();
249 theseven 143
    cputs(CONSOLE_BOOT, welcomestring);
144
#ifdef HAVE_STORAGE
436 theseven 145
    wakeup_init(&(ib->storagewakeup));
835 theseven 146
    thread_create(&(ib->storagethread), storagethreadname, storageinitthread, ib->storagestack,
147
                  sizeof(ib->storagestack), USER_THREAD, 127, true, ib, NULL, NULL, NULL);
249 theseven 148
#endif
126 theseven 149
#ifdef HAVE_USB
891 theseven 150
    usbmanager_init();
126 theseven 151
#endif
190 theseven 152
#ifdef HAVE_BACKLIGHT
153
    backlight_init();
154
#endif
130 theseven 155
#ifdef HAVE_BUTTON
156
    button_init();
157
#endif
126 theseven 158
#ifdef HAVE_TARGETINIT_LATE
159
    targetinit_late();
160
#endif
249 theseven 161
#ifdef HAVE_STORAGE
162
    while (true)
163
    {
436 theseven 164
        if (wakeup_wait(&(ib->storagewakeup), 100000) == THREAD_OK) break;
165
        enum thread_state state = thread_get_state(&(ib->storagethread));
166
        if (state == THREAD_DEFUNCT_ACK)
249 theseven 167
        {
436 theseven 168
            if (wakeup_wait(&(ib->storagewakeup), 0) == THREAD_OK) break;
169
            thread_terminate(&(ib->storagethread));
249 theseven 170
            break;
171
        }
172
    }
173
#endif
174
#ifdef HAVE_TARGETINIT_VERYLATE
175
    targetinit_verylate();
176
#endif
66 theseven 177
    DEBUGF("Finished initialisation sequence");
436 theseven 178
 
456 theseven 179
    struct bootoption* option = bootinfo.options;
180
    bool success = false;
181
    while (option)
182
    {
183
        success = false;
436 theseven 184
        switch (option->type)
185
        {
186
        case BOOTTYPE_PIGGYBACKED:
835 theseven 187
            success = execimage(option->source, true, 0, NULL) != NULL;
436 theseven 188
            break;
189
 
190
#ifdef HAVE_BOOTFLASH
191
        case BOOTTYPE_BOOTFLASH:
192
        {
193
            int size = bootflash_filesize(option->source);
194
            if (size <= 0) break;
195
            void* buffer = memalign(0x10, size);
196
            if (!buffer) break;
197
            if (bootflash_read(option->source, buffer, 0, size) != size)
198
            {
199
                free(buffer);
200
                break;
201
            }
835 theseven 202
            success = execimage(buffer, false, 0, NULL) != NULL;
436 theseven 203
            break;
204
        }
205
#endif
206
 
207
#ifdef HAVE_STORAGE
208
        case BOOTTYPE_FILESYSTEM:
209
        {
210
            int fd = file_open(option->source, O_RDONLY);
211
            if (fd <= 0) break;
212
            int size = filesize(fd);
213
            if (size <= 0)
214
            {
215
                close(fd);
216
                break;
217
            }
218
            void* buffer = memalign(0x10, size);
219
            if (!buffer)
220
            {
221
                close(fd);
222
                break;
223
            }
224
            if (read(fd, buffer, size) != size)
225
            {
226
                free(buffer);
227
                close(fd);
228
                break;
229
            }
230
            close(fd);
835 theseven 231
            success = execimage(buffer, false, 0, NULL) != NULL;
436 theseven 232
            break;
233
        }
234
#endif
235
 
456 theseven 236
        case BOOTTYPE_FAKESUCCESS:
237
            success = true;
238
            break;
239
 
436 theseven 240
        default:
241
            cprintf(CONSOLE_BOOT, unknownboottypestr, option->type);
242
        }
456 theseven 243
        if (success) option = option->success_next;
244
        else option = option->fail_next;
245
    }
246
    if (!success) cputs(CONSOLE_BOOT, nobootoptionsstr);
66 theseven 247
}
248
 
249
void init() INITCODE_ATTR;
250
void init()
251
{
126 theseven 252
#ifdef HAVE_TARGETINIT_VERYEARLY
253
    targetinit_veryearly();
254
#endif
66 theseven 255
    scheduler_init();
256
    console_init();
126 theseven 257
#ifdef HAVE_LCD
66 theseven 258
    lcd_init();
259
    lcdconsole_init();
126 theseven 260
#endif
261
#ifdef HAVE_TARGETINIT_EARLY
262
    targetinit_early();
263
#endif
436 theseven 264
    malloc_init();
265
    size_t size = (size_t)(&bootinfo) - (size_t)(&_poolstart) + bootinfo.totalsize;
266
    void* bootalloc = malloc(size);
267
    size -= (size_t)(bootalloc) - (size_t)(&_poolstart);
268
    realloc(bootalloc, size);
835 theseven 269
    struct initbss* ib = (struct initbss*)malloc(sizeof(struct initbss));
814 theseven 270
    reownalloc(ib, OWNER_TYPE(OWNER_THREAD, &(ib->initthread)));
271
    reownalloc(bootalloc, OWNER_TYPE(OWNER_THREAD, &(ib->initthread)));
436 theseven 272
    thread_create(&(ib->initthread), initthreadname, initthread, ib->initstack,
842 theseven 273
                  sizeof(ib->initstack), OS_THREAD, 127, true, ib, NULL, NULL, NULL);
597 theseven 274
    timer_init();
437 theseven 275
    interrupt_init();
66 theseven 276
}