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;
436 theseven 73
    enum boottype type;
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;
203
            if (!success) free(buffer);
436 theseven 204
            break;
205
        }
206
#endif
207
 
208
#ifdef HAVE_STORAGE
209
        case BOOTTYPE_FILESYSTEM:
210
        {
211
            int fd = file_open(option->source, O_RDONLY);
212
            if (fd <= 0) break;
213
            int size = filesize(fd);
214
            if (size <= 0)
215
            {
216
                close(fd);
217
                break;
218
            }
219
            void* buffer = memalign(0x10, size);
220
            if (!buffer)
221
            {
222
                close(fd);
223
                break;
224
            }
225
            if (read(fd, buffer, size) != size)
226
            {
227
                free(buffer);
228
                close(fd);
229
                break;
230
            }
231
            close(fd);
456 theseven 232
            success = execimage(buffer, false) != NULL;
233
            if (!success) free(buffer);
436 theseven 234
            break;
235
        }
236
#endif
237
 
456 theseven 238
        case BOOTTYPE_FAKESUCCESS:
239
            success = true;
240
            break;
241
 
436 theseven 242
        default:
243
            cprintf(CONSOLE_BOOT, unknownboottypestr, option->type);
244
        }
456 theseven 245
        if (success) option = option->success_next;
246
        else option = option->fail_next;
247
    }
248
    if (!success) cputs(CONSOLE_BOOT, nobootoptionsstr);
437 theseven 249
    free(ib->bootalloc);
66 theseven 250
}
251
 
252
void init() INITCODE_ATTR;
253
void init()
254
{
126 theseven 255
#ifdef HAVE_TARGETINIT_VERYEARLY
256
    targetinit_veryearly();
257
#endif
66 theseven 258
    scheduler_init();
259
    console_init();
126 theseven 260
#ifdef HAVE_LCD
66 theseven 261
    lcd_init();
262
    lcdconsole_init();
126 theseven 263
#endif
264
#ifdef HAVE_TARGETINIT_EARLY
265
    targetinit_early();
266
#endif
436 theseven 267
    malloc_init();
268
    size_t size = (size_t)(&bootinfo) - (size_t)(&_poolstart) + bootinfo.totalsize;
269
    void* bootalloc = malloc(size);
270
    size -= (size_t)(bootalloc) - (size_t)(&_poolstart);
271
    realloc(bootalloc, size);
272
    ib = (struct initbss*)malloc(sizeof(struct initbss));
273
    reownalloc(ib, &(ib->initthread));
274
    reownalloc(bootalloc, &(ib->initthread));
437 theseven 275
    ib->bootalloc = bootalloc;
436 theseven 276
    thread_create(&(ib->initthread), initthreadname, initthread, ib->initstack,
277
                  sizeof(ib->initstack), USER_THREAD, 127, true);
437 theseven 278
    interrupt_init();
66 theseven 279
}