Subversion Repositories freemyipod

Rev

Rev 651 | Rev 692 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 651 Rev 660
Line 22... Line 22...
22
 
22
 
23
 
23
 
24
#include "emcoreapp.h"
24
#include "emcoreapp.h"
25
#include "libpng.h"
25
#include "libpng.h"
26
#include "libui.h"
26
#include "libui.h"
-
 
27
#include "libmkfat32.h"
27
 
28
 
28
 
29
 
29
void main();
30
void main();
30
EMCORE_APP_HEADER("emCORE installer", main, 127)
31
EMCORE_APP_HEADER("emCORE installer", main, 127)
31
 
32
 
Line 38... Line 39...
38
extern uint32_t disclaimer_png_size;
39
extern uint32_t disclaimer_png_size;
39
extern char actions_png[];
40
extern char actions_png[];
40
extern uint32_t actions_png_size;
41
extern uint32_t actions_png_size;
41
extern char f_png_emcorelib[];
42
extern char f_png_emcorelib[];
42
extern char f_ui_emcorelib[];
43
extern char f_ui_emcorelib[];
-
 
44
extern char f_mkfat32_emcorelib[];
43
extern uint32_t flashscript[];
45
extern uint32_t flashscript[];
44
extern uint32_t firstinstcost;
46
extern uint32_t firstinstcost;
45
extern uint32_t firstinstscript[];
47
extern uint32_t firstinstscript[];
46
extern uint32_t commoncost;
48
extern uint32_t commoncost;
47
extern uint32_t commonscript[];
49
extern uint32_t commonscript[];
Line 110... Line 112...
110
    if (eventtype == WHEEL_MOVED_ACCEL)
112
    if (eventtype == WHEEL_MOVED_ACCEL)
111
        scrollpos = MAX(0, MIN(295, scrollpos + value / 8));
113
        scrollpos = MAX(0, MIN(295, scrollpos + value / 8));
112
    wakeup_signal(&eventwakeup);
114
    wakeup_signal(&eventwakeup);
113
}
115
}
114
 
116
 
115
void mkfat32(struct progressbar_state* progressbar)
117
void fat32_progressbar_init(void* user, int max)
116
{
118
{
117
    uint32_t i, j, rc;
-
 
118
    uint32_t rootdirclus = 2;
-
 
119
    uint32_t secperclus = 1;
-
 
120
    uint32_t fatsectors = 1;
-
 
121
    uint32_t oldfatsectors = 0;
-
 
122
    uint32_t clustercount;
-
 
123
    uint32_t reserved = 2;
-
 
124
    struct storage_info storageinfo;
-
 
125
    storage_get_info(0, &storageinfo);
-
 
126
    uint32_t totalsectors = storageinfo.num_sectors;
119
    progressbar_init((struct progressbar_state*)user,
127
    disk_unmount(0);
-
 
128
    while (fatsectors != oldfatsectors)
-
 
129
    {
-
 
130
        oldfatsectors = fatsectors;
-
 
131
        clustercount = (totalsectors - fatsectors - reserved) / secperclus;
120
                     15, 304, 135, 159, 0x77ff, 0xe8, 0x125f, 0, max);
132
        fatsectors = (clustercount + 1025) >> 10;
-
 
133
    }
121
}
134
    uint32_t database = fatsectors + reserved;
-
 
135
    uint32_t clusoffset = 0;
-
 
136
    uint32_t* buf = memalign(0x10, 0x20000);
-
 
137
    memset(buf, 0, 0x1000);
-
 
138
    memcpy(buf, "\xeb\x58\x00MSWIN5.0\0\x10", 0xd);
-
 
139
    ((uint8_t*)buf)[0xd] = secperclus;
-
 
140
    ((uint16_t*)buf)[7] = reserved;
-
 
141
    memcpy(&((uint8_t*)buf)[0x10], "\x01\0\0\0\0\xf8\0\0\x3f\0\xff", 0xb);
-
 
142
    buf[8] = totalsectors;
-
 
143
    buf[9] = fatsectors;
-
 
144
    buf[0xb] = rootdirclus + clusoffset;
-
 
145
    ((uint16_t*)buf)[0x18] = 1;
-
 
146
    ((uint8_t*)buf)[0x40] = 0x80;
-
 
147
    ((uint8_t*)buf)[0x42] = 0x29;
-
 
148
    memcpy(&((uint8_t*)buf)[0x43], "\0\0\0\0iPodClassic", 0xf);
-
 
149
    memcpy(&((uint8_t*)buf)[0x52], "FAT32   ", 8);
-
 
150
    ((uint16_t*)buf)[0xff] = 0xaa55;
-
 
151
    if (rc = storage_write_sectors_md(0, 0, 1, buf))
-
 
152
        panicf(PANIC_KILLTHREAD, "Error writing MBR: %08X", rc);
-
 
153
    memset(buf, 0, 0x1000);
-
 
154
    buf[0] = 0x41615252;
-
 
155
    buf[0x79] = 0x61417272;
-
 
156
    buf[0x7a] = clustercount - 1;
-
 
157
    buf[0x7b] = 2;
-
 
158
    buf[0x7f] = 0xaa550000;
-
 
159
    if (rc = storage_write_sectors_md(0, 1, 1, buf))
-
 
160
        panicf(PANIC_KILLTHREAD, "Error writing FSINFO: %08X", rc);
-
 
161
    progressbar_init(progressbar, 15, 304, 135, 159, 0x77ff, 0xe8, 0x125f, 0, fatsectors);
-
 
162
    uint32_t cursect = 0;
-
 
163
    for (i = 0; i < fatsectors; i += 32)
-
 
164
    {
122
 
165
        memset(buf, 0, 0x20000);
-
 
166
        if (!i) memcpy(buf, "\xf8\xff\xff\x0f\xff\xff\xff\xff\xff\xff\xff\x0f", 12);
-
 
167
        if (rc = storage_write_sectors_md(0, reserved + i, MIN(fatsectors - i, 32), buf))
-
 
168
            panicf(PANIC_KILLTHREAD, "Error writing FAT sectors %d-%d: %08X",
-
 
169
                   i, MIN(fatsectors - 1, i + 31), rc);
-
 
170
        progressbar_setpos(progressbar, i, false);
123
void fat32_progressbar_update(void* user, int current)
171
    }
124
{
172
    memset(buf, 0, secperclus * 0x1000);
-
 
173
    memcpy(buf, "iPodClassic\x08", 12);
-
 
174
    if (rc = storage_write_sectors_md(0, database, secperclus, buf))
125
    progressbar_setpos((struct progressbar_state*)user, current, false);
175
        panicf(PANIC_KILLTHREAD, "Error writing root directory sectors: %08X", i, rc);
-
 
176
    free(buf);
-
 
177
    disk_mount(0);
-
 
178
}
126
}
179
 
127
 
180
void main(void)
128
void main(void)
181
{
129
{
182
    uint32_t i, j, k, rc;
130
    uint32_t i, j, k, rc;
183
    uint32_t dummy;
131
    uint32_t dummy;
184
    struct progressbar_state progressbar;
132
    struct progressbar_state progressbar;
-
 
133
    bool appleflash = false;
185
    bool appleflash;
134
    bool oldflash;
186
    void* syscfgptr;
135
    void* syscfgptr;
187
    uint8_t* norbuf;
136
    uint8_t* norbuf;
188
#define norbufword ((uint32_t*)norbuf)
137
#define norbufword ((uint32_t*)norbuf)
189
    uint8_t* oldnor;
138
    uint8_t* oldnor;
190
#define oldnorword ((uint32_t*)oldnor)
139
#define oldnorword ((uint32_t*)oldnor)
Line 196... Line 145...
196
    cputc(3, '.');
145
    cputc(3, '.');
197
    struct emcorelib_header* libui = get_library(LIBUI_IDENTIFIER, LIBUI_API_VERSION, LIBSOURCE_RAM_NEEDCOPY, f_ui_emcorelib);
146
    struct emcorelib_header* libui = get_library(LIBUI_IDENTIFIER, LIBUI_API_VERSION, LIBSOURCE_RAM_NEEDCOPY, f_ui_emcorelib);
198
    if (!libui) panicf(PANIC_KILLTHREAD, "Could not load user interface library!");
147
    if (!libui) panicf(PANIC_KILLTHREAD, "Could not load user interface library!");
199
    struct libui_api* ui = (struct libui_api*)libui->api;
148
    struct libui_api* ui = (struct libui_api*)libui->api;
200
    cputc(3, '.');
149
    cputc(3, '.');
-
 
150
    struct emcorelib_header* libmkfat32 = get_library(LIBMKFAT32_IDENTIFIER, LIBMKFAT32_API_VERSION, LIBSOURCE_RAM_NEEDCOPY, f_mkfat32_emcorelib);
-
 
151
    if (!libmkfat32) panicf(PANIC_KILLTHREAD, "Could not load mkfat32 library!");
-
 
152
    struct libmkfat32_api* mf32 = (struct libmkfat32_api*)libmkfat32->api;
-
 
153
    cputc(3, '.');
201
 
154
 
202
    struct png_info* handle = png->png_open(background_png, background_png_size);
155
    struct png_info* handle = png->png_open(background_png, background_png_size);
203
    if (!handle) panicf(PANIC_KILLTHREAD, "Could not parse background image!");
156
    if (!handle) panicf(PANIC_KILLTHREAD, "Could not parse background image!");
204
    cputc(3, '.');
157
    cputc(3, '.');
205
    struct png_rgb* bg = png->png_decode_rgb(handle);
158
    struct png_rgb* bg = png->png_decode_rgb(handle);
Line 225... Line 178...
225
    oldnor = memalign(0x10, 0x100000);
178
    oldnor = memalign(0x10, 0x100000);
226
    memset(norbuf, 0xff, 0x100000);
179
    memset(norbuf, 0xff, 0x100000);
227
    cputc(3, '.');
180
    cputc(3, '.');
228
    bootflash_readraw(oldnor, 0, 0x100000);
181
    bootflash_readraw(oldnor, 0, 0x100000);
229
    cputc(3, '.');
182
    cputc(3, '.');
230
    if (oldnorword[0x400] == 0x53436667) appleflash = false;
183
    if (oldnorword[0x400] == 0x53436667) oldflash = true;
231
    else
184
    else
232
    {
185
    {
233
        updating = false;
-
 
234
        if (oldnorword[0] == 0x53436667) appleflash = true;
186
        if (oldnorword[0] == 0x53436667) oldflash = false;
235
        else panic(PANIC_KILLTHREAD, "Boot flash contents are damaged! "
187
        else panic(PANIC_KILLTHREAD, "Boot flash contents are damaged! "
236
                                     "(No SYSCFG found)\n\nPlease ask for help.\n");
188
                                     "(No SYSCFG found)\n\nPlease ask for help.\n");
-
 
189
        if (oldnorword[0x400] == 0xffffffff && oldnorword[0x3ff80] == 0x666c7368)
-
 
190
        {
-
 
191
            appleflash = true;
-
 
192
            updating = false;
-
 
193
        }
237
    }
194
    }
238
    memcpy(&norbuf[0x1000], &oldnor[appleflash ? 0 : 0x1000], 0x1000);
195
    memcpy(norbuf, &oldnor[oldflash ? 0x1000 : 0], 0x1000);
239
    cputc(3, '.');
196
    cputc(3, '.');
240
 
197
 
241
    uint32_t* script = flashscript;
198
    uint32_t* script = flashscript;
242
    uint32_t sp = 0;
199
    uint32_t sp = 0;
243
    uint32_t beginptr = 0x2000;
200
    uint32_t beginptr = 0x2000;
244
    uint32_t endptr = 0x100000;
201
    uint32_t endptr = 0x100000;
245
    uint32_t dirptr = 0;
202
    uint32_t dirptr = 0x1000;
246
    while (script[sp])
203
    while (script[sp])
247
    {
204
    {
248
        uint32_t file = script[sp] & 0xff;
205
        uint32_t file = script[sp] & 0xff;
249
        uint32_t flags = (script[sp] >> 8) & 0xff;
206
        uint32_t flags = (script[sp] >> 8) & 0xff;
250
        uint32_t align = (script[sp] >> 16) & 0xff;
207
        uint32_t align = (script[sp] >> 16) & 0xff;
Line 287... Line 244...
287
                file = beginptr;
244
                file = beginptr;
288
                beginptr += ((size + 0xfff) & ~0xfff);
245
                beginptr += ((size + 0xfff) & ~0xfff);
289
            }
246
            }
290
            if (!(flags & 4))
247
            if (!(flags & 4))
291
            {
248
            {
292
                if (dirptr >= 0x1000)
249
                if (dirptr >= 0x2000)
293
                    panicf(PANIC_KILLTHREAD, "Error: Directory is full!");
250
                    panicf(PANIC_KILLTHREAD, "Error: Directory is full!");
294
                memcpy(&norbuf[dirptr], &script[sp], 8);
251
                memcpy(&norbuf[dirptr], &script[sp], 8);
295
                norbufword[(dirptr >> 2) + 2] = file;
252
                norbufword[(dirptr >> 2) + 2] = file;
296
                norbufword[(dirptr >> 2) + 3] = storesize;
253
                norbufword[(dirptr >> 2) + 3] = storesize;
297
                dirptr += 0x10;
254
                dirptr += 0x10;
Line 371... Line 328...
371
                
328
                
372
        ui->blenda(165, 36, 255, framebuf, 0, 0, 165, bg, 77, 100, 320, actions, 0, 0, 165);
329
        ui->blenda(165, 36, 255, framebuf, 0, 0, 165, bg, 77, 100, 320, actions, 0, 0, 165);
373
        displaylcd(0, 0, 320, 240, bg, 0, 0, 320);
330
        displaylcd(0, 0, 320, 240, bg, 0, 0, 320);
374
        displaylcd(77, 100, 165, 36, framebuf, 0, 0, 165);
331
        displaylcd(77, 100, 165, 36, framebuf, 0, 0, 165);
375
 
332
 
-
 
333
        struct storage_info storageinfo;
376
        mkfat32(&progressbar);
334
        storage_get_info(0, &storageinfo);
-
 
335
        int rc = mf32->mkfat32(0, 0, storageinfo.num_sectors, 4096, 1, "iPodClassic", &progressbar,
-
 
336
                              fat32_progressbar_init, fat32_progressbar_update);
-
 
337
        if (rc < 0) panicf(PANIC_KILLTHREAD, "Error formatting hard drive: %08X", rc);
377
    }
338
    }
378
 
339
 
379
    ui->blenda(165, 36, 255, framebuf, 0, 0, 165, bg, 77, 100, 320, actions, 0, 36, 165);
340
    ui->blenda(165, 36, 255, framebuf, 0, 0, 165, bg, 77, 100, 320, actions, 0, 36, 165);
380
    displaylcd(0, 0, 320, 240, bg, 0, 0, 320);
341
    displaylcd(0, 0, 320, 240, bg, 0, 0, 320);
381
    displaylcd(77, 100, 165, 36, framebuf, 0, 0, 165);
342
    displaylcd(77, 100, 165, 36, framebuf, 0, 0, 165);
Line 458... Line 419...
458
    free(norbuf);
419
    free(norbuf);
459
    free(framebuf);
420
    free(framebuf);
460
    free(actions);
421
    free(actions);
461
    free(bg);
422
    free(bg);
462
 
423
 
-
 
424
    release_library(libmkfat32);
463
    release_library(libui);
425
    release_library(libui);
464
    release_library(libpng);
426
    release_library(libpng);
-
 
427
    library_unload(libmkfat32);
465
    library_unload(libui);
428
    library_unload(libui);
466
    library_unload(libpng);
429
    library_unload(libpng);
467
 
430
 
468
    shutdown(false);
431
    shutdown(false);
469
    reset();
432
    reset();