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
42
#include "usb/usb.h"
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];
437 theseven 91
    void* bootalloc;
436 theseven 92
#ifdef HAVE_STORAGE
93
    struct scheduler_thread storagethread;
94
    uint32_t storagestack[0x400];
95
    struct wakeup storagewakeup;
96
#endif
97
};
98
 
99
 
100
static struct initbss* ib INITDATA_ATTR = NULL;
427 farthen 101
static const char welcomestring[] INITCONST_ATTR = "emCORE v" VERSION " r" VERSION_SVN "\n\n";
110 theseven 102
static const char initthreadname[] INITCONST_ATTR = "Initialization thread";
436 theseven 103
static const char unknownboottypestr[] INITCONST_ATTR = "Skipping boot option with unknown type %d\n";
104
static const char nobootoptionsstr[] INITCONST_ATTR = "No usable boot options, waiting for USB commands\n\n";
249 theseven 105
#ifdef HAVE_STORAGE
436 theseven 106
static const char storagethreadname[] INITCONST_ATTR = "Storage init thread";
249 theseven 107
#endif
436 theseven 108
 
109
struct bootinfo bootinfo INITTAIL_ATTR =
89 theseven 110
{
429 theseven 111
    .signature = "emCOboot",
436 theseven 112
    .version = 1,
113
    .baseaddr = &bootinfo,
114
    .totalsize = sizeof(struct bootinfo),
115
    .options = NULL
89 theseven 116
};
15 theseven 117
 
87 theseven 118
 
89 theseven 119
#ifdef HAVE_STORAGE
249 theseven 120
void storageinitthread() INITCODE_ATTR;
121
void storageinitthread()
122
{
123
    DEBUGF("Initializing storage drivers...");
124
    storage_init();
125
    DEBUGF("Initializing storage subsystem...");
126
    disk_init_subsystem();
127
    DEBUGF("Reading partition tables...");
128
    disk_init();
129
    DEBUGF("Mounting partitions...");
130
    disk_mount_all();
131
    DEBUGF("Storage init finished.");
436 theseven 132
    wakeup_signal(&(ib->storagewakeup));
249 theseven 133
}
134
#endif
135
 
66 theseven 136
void initthread() INITCODE_ATTR;
137
void initthread()
2 theseven 138
{
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));
146
    thread_create(&(ib->storagethread), storagethreadname, storageinitthread,
147
                  ib->storagestack, sizeof(ib->storagestack), USER_THREAD, 127, true);
249 theseven 148
#endif
126 theseven 149
#ifdef HAVE_USB
15 theseven 150
    usb_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:
456 theseven 187
            success = execimage(option->source, true) != 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
            }
456 theseven 202
            success = execimage(buffer, false) != 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);
456 theseven 231
            success = execimage(buffer, false) != 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);
437 theseven 247
    free(ib->bootalloc);
66 theseven 248
}
249
 
250
void init() INITCODE_ATTR;
251
void init()
252
{
126 theseven 253
#ifdef HAVE_TARGETINIT_VERYEARLY
254
    targetinit_veryearly();
255
#endif
66 theseven 256
    scheduler_init();
257
    console_init();
126 theseven 258
#ifdef HAVE_LCD
66 theseven 259
    lcd_init();
260
    lcdconsole_init();
126 theseven 261
#endif
262
#ifdef HAVE_TARGETINIT_EARLY
263
    targetinit_early();
264
#endif
436 theseven 265
    malloc_init();
266
    size_t size = (size_t)(&bootinfo) - (size_t)(&_poolstart) + bootinfo.totalsize;
267
    void* bootalloc = malloc(size);
268
    size -= (size_t)(bootalloc) - (size_t)(&_poolstart);
269
    realloc(bootalloc, size);
270
    ib = (struct initbss*)malloc(sizeof(struct initbss));
814 theseven 271
    reownalloc(ib, OWNER_TYPE(OWNER_THREAD, &(ib->initthread)));
272
    reownalloc(bootalloc, OWNER_TYPE(OWNER_THREAD, &(ib->initthread)));
437 theseven 273
    ib->bootalloc = bootalloc;
436 theseven 274
    thread_create(&(ib->initthread), initthreadname, initthread, ib->initstack,
542 theseven 275
                  sizeof(ib->initstack), OS_THREAD, 127, true);
597 theseven 276
    timer_init();
437 theseven 277
    interrupt_init();
66 theseven 278
}