Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
160 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
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
//
13
//    emBIOS is distributed in the hope that it will be useful,
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
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "embiosapp.h"
25
 
26
 
27
void main();
28
EMBIOS_APP_HEADER("Installer thread", 0x10000, main, 127)
29
 
30
 
31
uint16_t lcdbuffer[176 * 132];
32
uint16_t backdrop[176 * 132];
33
#define BMPIDX_BACKDROP 0
34
#define BMPIDX_WELCOME 1
35
#define BMPIDX_BADPARTITION 2
36
#define BMPIDX_CANCELLED 3
37
#define BMPIDX_REPARTITION 4
38
#define BMPIDX_INSTALLING 5
39
#define BMPIDX_PREPARING 6
40
#define BMPIDX_REPARTITIONING 7
41
#define BMPIDX_INSTALLFILES 8
42
#define BMPIDX_FLASHING 9
43
 
44
struct wakeup eventwakeup;
45
volatile int button;
46
 
47
char mallocbuf[0xec0000] __attribute__((aligned(16)));
48
tlsf_pool mallocpool;
49
 
50
uint32_t fat32_ok;
51
uint32_t fat32_startsector;
52
uint32_t fat32_secperclus;
53
uint32_t fat32_database;
54
uint32_t fat32_fatbase;
55
uint32_t fat32_fatsize;
56
uint32_t fat32_fatcount;
57
uint32_t fat32_sectorcount;
58
uint32_t fat32_clustercount;
59
uint32_t fat32_rootdirclus;
60
 
61
#define nor ((uint8_t*)0x24000000)
62
#define norword ((uint32_t*)0x24000000)
63
 
64
extern uint32_t _scriptstart;
65
 
66
 
67
void handler(enum button_event eventtype, int which, int value)
68
{
69
    if (eventtype == BUTTON_PRESS) button |= 1 << which;
70
    wakeup_signal(&eventwakeup);
71
}
72
 
73
void* malloc(size_t size)
74
{
75
    void* result = tlsf_malloc(mallocpool, size);
76
    if (!result && size) panic(PANIC_KILLTHREAD, "Out of memory!");
77
    return result;
78
}
79
 
80
void* memalign(size_t align, size_t size)
81
{
82
    void* result = tlsf_memalign(mallocpool, align, size);
83
    if (!result && size) panic(PANIC_KILLTHREAD, "Out of memory!");
84
    return result;
85
}
86
 
87
void* realloc(void* ptr, size_t size)
88
{
89
    void* result = tlsf_realloc(mallocpool, ptr, size);
90
    if (!result && size) panic(PANIC_KILLTHREAD, "Out of memory!");
91
    return result;
92
}
93
 
94
void free(void* ptr)
95
{
96
    tlsf_free(mallocpool, ptr);
97
}
98
 
99
uint32_t freeret(uint32_t rc, void* ptr)
100
{
101
    tlsf_free(mallocpool, ptr);
102
    return rc;
103
}
104
 
105
int decryptfw(void* image, uint32_t offset)
106
{
107
    uint32_t size = ((uint32_t*)image)[5];
108
    if (size > 0x800000) return 0;
109
    hwkeyaes(HWKEYAES_DECRYPT, ((uint32_t*)image)[2], &((uint8_t*)image)[offset], size);
110
    memcpy(image, &((uint8_t*)image)[offset], size);
111
    return size;
112
}
113
 
114
uint32_t getfw(const char* filename, uint32_t* sector, int* size)
115
{
116
    uint32_t i;
117
    uint32_t* buffer = tlsf_memalign(mallocpool, 0x10, 0x800);
118
    if (storage_read_sectors_md(0, 0, 1, buffer) != 0) return freeret(1, buffer);
119
    if (*((uint16_t*)((uint32_t)buffer + 0x1FE)) != 0xAA55) return freeret(1, buffer);
120
    uint32_t startsector = 0;
121
    for (i = 0x1C2; i < 0x200; i += 0x10)
122
        if (((uint8_t*)buffer)[i] == 0)
123
        {
124
            startsector = *((uint16_t*)((uint32_t)buffer + i + 4))
125
                        | (*((uint16_t*)((uint32_t)buffer + i + 6)) << 16);
126
            break;
127
        }
128
    if (startsector == 0) return freeret(1, buffer);
129
    if (storage_read_sectors_md(0, startsector, 1, buffer) != 0) return freeret(1, buffer);
130
    if (buffer[0x40] != 0x5B68695D) return freeret(1, buffer);
131
    if (storage_read_sectors_md(0, startsector + 1 + (buffer[0x41] >> 11), 1, buffer) != 0)
132
        return freeret(1, buffer);
133
    for (i = 0; i < 0x1FE; i += 10)
134
        if (memcmp(&buffer[i], filename, 8) == 0)
135
        {
136
            *sector = startsector + (buffer[i + 3] >> 11);
137
            *size = buffer[i + 4] + 0x800;
138
            tlsf_free(mallocpool, buffer);
139
            return 0;
140
        }
141
    return freeret(2, buffer);
142
}
143
 
144
uint32_t readfw(const char* filename, void** address, int* size)
145
{
146
    uint32_t sector;
147
    uint32_t rc = getfw(filename, &sector, size);
148
    if (rc) return rc;
149
    *address = tlsf_memalign(mallocpool, 0x10, *size);
150
    if (storage_read_sectors_md(0, sector, ((*size + 0x7FF) >> 11), *address) != 0)
151
        return freeret(1, *address);
152
    *size = decryptfw(*address, 0x800);
153
    tlsf_realloc(mallocpool, *address, *size);
154
    return 0;
155
}
156
 
157
uint32_t getapplenor(const char* filename, void** address, int* size)
158
{
159
    uint32_t i;
160
    for (i = 0xffe00; i < 0x100000; i += 0x28)
161
        if (memcmp(&nor[i], filename, 8) == 0)
162
        {
163
            *size = norword[(i + 0x10) >> 2] + 0x200;
164
            *address = &nor[norword[(i + 0xc) >> 2]];
165
            return 0;
166
        }
167
    return 1;
168
}
169
 
170
uint32_t readapplenor(const char* filename, void** address, int* size)
171
{
172
    void* noraddr;
173
    uint32_t rc = getapplenor(filename, &noraddr, size);
174
    if (rc) return rc;
175
    *address = malloc(*size);
176
    memcpy(*address, noraddr, *size);
177
    *size = decryptfw(*address, 0x200);
178
    realloc(*address, *size);
179
    return 0;
180
}
181
 
182
uint32_t fat32_resize_patchdirs(uint32_t clusterchain, uint32_t clustoffset,
183
                                struct progressbar_state* progressbar, int min, int len)
184
{
185
    uint32_t i, j,  rc;
186
    uint32_t* buffer = (uint32_t*)memalign(0x10, 0x800);
187
    int pos = min, newlen = len / 15;
188
    while (clusterchain < 0x0ffffff0)
189
    {
190
        uint32_t sectorbase = (clusterchain - 2) * fat32_secperclus + fat32_database;
191
        for (i = 0; i < fat32_secperclus; i++)
192
        {
193
            if (storage_read_sectors_md(0, sectorbase + i, 1, buffer))
194
            {
195
                free(buffer);
196
                return 2;
197
            }
198
            for (j = 0; j < 64; j++)
199
                if (!((uint8_t*)buffer)[i << 2])
200
                {
201
                    free(buffer);
202
                    return 0;
203
                }
204
                else if (((uint8_t*)buffer)[j << 5] == 0xe5) continue;
205
                else if (((uint8_t*)buffer)[(j << 5) + 11] & 8) continue;
206
                else
207
                {
208
                    uint32_t clust = (((uint16_t*)buffer)[(j << 4) + 0xa] << 16)
209
                                    | ((uint16_t*)buffer)[(j << 4) + 0xd];
210
                    if (clust > 1 && clust < 0xffffff0)
211
                    {
212
                        clust += clustoffset;
213
                        ((uint16_t*)buffer)[(j << 4) + 0xa] = clust >> 16;
214
                        ((uint16_t*)buffer)[(j << 4) + 0xd] = clust & 0xffff;
215
                        if ((((uint8_t*)buffer)[(j << 5) + 0xb] & 0x10)
216
                         && memcmp(&((uint8_t*)buffer)[j << 5], ".          ", 11)
217
                         && memcmp(&((uint8_t*)buffer)[j << 5], "..         ", 11))
218
                            if ((rc = fat32_resize_patchdirs(clust, clustoffset,
219
                                                             progressbar, pos, newlen)))
220
                            {
221
                                free(buffer);
222
                                return rc;
223
                            }
224
                        pos += newlen;
225
                        newlen = 15 * newlen / 16;
226
                    }
227
                }
228
            if (storage_write_sectors_md(0, sectorbase + i, 1, buffer))
229
            {
230
                free(buffer);
231
                return 2;
232
            }
233
        }
234
        uint32_t fatsector = fat32_fatbase + (clusterchain >> 9);
235
        if (storage_read_sectors_md(0, fatsector, 1, buffer))
236
        {
237
            free(buffer);
238
            return 2;
239
        }
240
        clusterchain = buffer[(i << 9) + (clusterchain & 0x1FF)];
241
    }
242
    free(buffer);
243
    if (len) progressbar_setpos(progressbar, min + len, false);
244
    return 0;
245
}
246
 
247
uint32_t fat32_resize_fulldisk(struct progressbar_state* progressbar)
248
{
249
    uint32_t i, j, rc;
250
    uint32_t fatsectors = 1;
251
    uint32_t oldfatsectors = 0;
252
    uint32_t clustercount;
253
    uint32_t reserved;
254
    struct storage_info storageinfo;
255
    storage_get_info(0, &storageinfo);
256
    uint32_t totalsectors = storageinfo.num_sectors;
257
    uint32_t* buf1 = (uint32_t*)memalign(0x10, 0x800);
258
    uint32_t* buf2 = (uint32_t*)memalign(0x10, 0x800);
259
    if (!fat32_ok)
260
    {
261
        fat32_secperclus = 4;
262
        fat32_rootdirclus = 2;
263
    }
264
    while (fatsectors != oldfatsectors)
265
    {
266
        oldfatsectors = fatsectors;
267
        if (!fat32_ok) reserved = 2;
268
        else reserved = (fat32_database - fatsectors - 2) % fat32_secperclus + 2;
269
        clustercount = (totalsectors - fatsectors - reserved) / fat32_secperclus;
270
        fatsectors = (clustercount + 513) >> 9;
271
    }
272
    uint32_t database = fatsectors + reserved;
273
    uint32_t clusoffset;
274
    if (!fat32_ok) clusoffset = 0;
275
    else clusoffset = (fat32_database - database) / fat32_secperclus;
276
    memset(buf1, 0, 0x800);
277
    if (fat32_ok)
278
        if (storage_read_sectors_md(0, fat32_startsector, 1, buf2))
279
        {
280
            fat32_ok = 0;
281
            free(buf1);
282
            free(buf2);
283
            return 2;
284
        }
285
    memcpy(buf1, "\xeb\x58\x00MSWIN5.0\0\x08", 0xd);
286
    ((uint8_t*)buf1)[0xd] = fat32_secperclus;
287
    ((uint16_t*)buf1)[7] = reserved;
288
    memcpy(&((uint8_t*)buf1)[0x10], "\x01\0\0\0\0\xf8\0\0\x3f\0\xff", 0xb);
289
    buf1[8] = totalsectors;
290
    buf1[9] = fatsectors;
291
    buf1[0xb] = fat32_rootdirclus + clusoffset;
292
    ((uint16_t*)buf1)[0x18] = 1;
293
    ((uint8_t*)buf1)[0x40] = 0x80;
294
    ((uint8_t*)buf1)[0x42] = 0x29;
295
    if (!fat32_ok) memcpy(&((uint8_t*)buf1)[0x43], "\0\0\0\0iPod Nano  ", 0xf);
296
    else memcpy(&((uint8_t*)buf1)[0x43], &((uint8_t*)buf2)[0x43], 0xf);
297
    memcpy(&((uint8_t*)buf1)[0x52], "FAT32   ", 8);
298
    ((uint16_t*)buf1)[0xff] = 0xaa55;
299
    if (storage_write_sectors_md(0, 0, 1, buf1))
300
    {
301
        fat32_ok = 0;
302
        free(buf1);
303
        free(buf2);
304
        return 2;
305
    }
306
    if (fat32_ok)
307
    {
308
        if (storage_read_sectors_md(0, fat32_startsector + ((uint16_t*)buf2)[0x18], 1, buf1))
309
        {
310
            fat32_ok = 0;
311
            free(buf1);
312
            free(buf2);
313
            return 2;
314
        }
315
        buf1[0x7a] += clustercount - fat32_clustercount;
316
    }
317
    else
318
    {
319
        memset(buf1, 0, 0x800);
320
        buf1[0] = 0x41615252;
321
        buf1[0x79] = 0x61417272;
322
        buf1[0x7a] = clustercount - 1;
323
        buf1[0x7b] = 2;
324
        buf1[0x7f] = 0xaa550000;
325
    }
326
    if (storage_write_sectors_md(0, 1, 1, buf1))
327
    {
328
        fat32_ok = 0;
329
        free(buf1);
330
        free(buf2);
331
        return 2;
332
    }
333
    progressbar_setpos(progressbar, 5, false);
334
    uint32_t cursect = 0;
335
    if (!fat32_ok)
336
    {
337
        for (i = 0; i < fatsectors; i++)
338
        {
339
            memset(buf1, 0, 0x800);
340
            if (!i) memcpy(buf1, "\xf8\xff\xff\x0f\xff\xff\xff\xff\xff\xff\xff\x0f", 12);
341
            if (storage_write_sectors_md(0, reserved + i, 1, buf1))
342
            {
343
                free(buf1);
344
                free(buf2);
345
                return 2;
346
            }
347
            progressbar_setpos(progressbar, 5 + i * 90 / fatsectors, false);
348
        }
349
    }
350
    else
351
    {
352
        for (i = 0; i < fatsectors; i++)
353
        {
354
            memset(buf1, 0, 0x800);
355
            for (j = 0; j < 512; j++)
356
            {
357
                if (!i && !j) buf1[j] = 0x0fffffff;
358
                else if (!i && j == 1) buf1[j] = 0xffffffff;
359
                else if (((i << 9) | j) < clusoffset + 2);
360
                else if (((i << 9) | j) >= clusoffset + fat32_clustercount + 2);
361
                else
362
                {
363
                    uint32_t oldclust = (((i << 9) | j) - clusoffset);
364
                    if (((oldclust >> 9) + fat32_fatbase) != cursect)
365
                    {
366
                        cursect = (oldclust >> 9) + fat32_fatbase;
367
                        if (storage_read_sectors_md(0, cursect, 1, buf2))
368
                        {
369
                            fat32_ok = 0;
370
                            free(buf1);
371
                            free(buf2);
372
                            return 2;
373
                        }
374
                    }
375
                    buf1[j] = buf2[oldclust & 0x1ff];
376
                    if (buf1[j] > 1 && buf1[j] < 0xffffff0)
377
                        buf1[j] += clusoffset;
378
                }
379
            }
380
            if (storage_write_sectors_md(0, reserved + i, 1, buf1))
381
            {
382
                fat32_ok = 0;
383
                free(buf1);
384
                free(buf2);
385
                return 2;
386
            }
387
            progressbar_setpos(progressbar, 5 + i * 20 / fatsectors, false);
388
        }
389
    }
390
    fat32_startsector = 0;
391
    fat32_database = database;
392
    fat32_fatbase = reserved;
393
    fat32_fatsize = fatsectors;
394
    fat32_fatcount = 1;
395
    fat32_sectorcount = totalsectors;
396
    fat32_clustercount = clustercount;
397
    fat32_rootdirclus = fat32_rootdirclus + clusoffset;
398
    if (!fat32_ok)
399
    {
400
        for (i = 0; i < fat32_secperclus; i++)
401
        {
402
            memset(buf1, 0, 0x800);
403
            if (!i) memcpy(buf1, "iPod Nano  \x08", 12);
404
            if (storage_write_sectors_md(0, database + i, 1, buf1))
405
            {
406
                free(buf1);
407
                free(buf2);
408
                return 2;
409
            }
410
        }
411
        free(buf1);
412
        free(buf2);
413
    }
414
    else
415
    {
416
        free(buf1);
417
        free(buf2);
418
        if ((rc = fat32_resize_patchdirs(fat32_rootdirclus, clusoffset, progressbar, 25, 75)))
419
        {
420
            fat32_ok = 0;
421
            return rc;
422
        }
423
    }
424
    progressbar_setpos(progressbar, 100, false);
425
    fat32_ok = 1;
426
    return 0;
427
}
428
 
429
uint32_t fat32_init()
430
{
431
    uint32_t i;
432
    fat32_ok = 0;
433
    fat32_startsector = 0xFFFFFFFF;
434
    uint32_t* buf = (uint32_t*)memalign(0x10, 0x800);
435
 
436
    if (storage_read_sectors_md(0, 0, 1, buf)) return freeret(2, buf);
437
 
438
    if (*((uint16_t*)((uint32_t)buf + 0x1FE)) != 0xAA55) return 1;
439
 
440
    for (i = 0x1C2; i < 0x200; i += 0x10)
441
        if (((uint8_t*)buf)[i] == 0xB)
442
        {
443
            fat32_startsector = *((uint16_t*)((uint32_t)buf + i + 4))
444
                              | (*((uint16_t*)((uint32_t)buf + i + 6)) << 16);
445
            break;
446
        }
447
 
448
    if (fat32_startsector == 0xFFFFFFFF
449
     && *((uint16_t*)((uint32_t)buf + 0x52)) == 0x4146
450
     && *((uint8_t*)((uint32_t)buf + 0x54)) == 0x54)
451
        fat32_startsector = 0;
452
 
453
    if (fat32_startsector == 0xFFFFFFFF) return freeret(1, buf);
454
 
455
    if (storage_read_sectors_md(0, fat32_startsector, 1, buf)) return freeret(2, buf);
456
 
457
    if (*((uint16_t*)((uint32_t)buf + 0x1FE)) != 0xAA55) return freeret(1, buf);
458
 
459
    if (((uint8_t*)buf)[0xB] != 0 || ((uint8_t*)buf)[0xC] != 8) return freeret(1, buf);
460
 
461
    fat32_secperclus = ((uint8_t*)buf)[0xD];
462
    uint32_t reserved = ((uint16_t*)buf)[0x7];
463
    fat32_fatcount = ((uint8_t*)buf)[0x10];
464
 
465
    if (((uint8_t*)buf)[0x11] != 0) return freeret(1, buf);
466
 
467
    fat32_sectorcount = buf[8];
468
    fat32_fatsize = buf[9];
469
 
470
    if (((uint16_t*)buf)[0x15] != 0) return freeret(1, buf);
471
 
472
    fat32_rootdirclus = buf[0xB];
473
    free(buf);
474
 
475
    fat32_clustercount = (fat32_sectorcount - reserved
476
                        - fat32_fatcount * fat32_fatsize) / fat32_secperclus;
477
 
478
    fat32_fatbase = fat32_startsector + reserved;
479
    fat32_database = fat32_fatbase + fat32_fatcount * fat32_fatsize;
480
 
481
    fat32_ok = 1;
482
    return 0;
483
}
484
 
485
void main(void)
486
{
487
    uint32_t i, j, k, rc;
488
    void* bitmapdata[10];
489
    uint32_t bitmapsize[10];
490
    uint32_t* script;
491
#define scriptb ((uint8_t*)script)
492
    uint32_t dummy;
493
    struct progressbar_state progressbar;
494
    bool repartition = false;
495
    bool appleflash;
496
    void* syscfgptr;
497
    int osossize = 0;
498
    void* ososptr;
499
    int diaguclsize = 0;
500
    void* diaguclptr;
501
    int diskuclsize = 0;
502
    void* diskuclptr;
503
    uint8_t* norbuf;
504
#define norbufword ((uint32_t*)norbuf)
505
 
506
    button = 0;
507
    wakeup_init(&eventwakeup);
508
    button_register_handler(handler);
509
    mallocpool = tlsf_create(mallocbuf, sizeof(mallocbuf));
510
 
511
    script = &_scriptstart;
512
    for (i = 0; i < 10; i++)
513
    {
514
        bitmapsize[i] = *script;
515
        bitmapdata[i] = &script[1];
516
        script = &script[1 + (bitmapsize[i] >> 2)];
517
    }
518
 
519
    void* bmpbuffer = malloc(0xb600);
520
    ucl_decompress(bitmapdata[BMPIDX_BACKDROP], bitmapsize[BMPIDX_BACKDROP], bmpbuffer, &dummy);
521
    renderbmp(backdrop, bmpbuffer, 176);
522
    memcpy(lcdbuffer, backdrop, 0xb580);
523
    ucl_decompress(bitmapdata[BMPIDX_WELCOME], bitmapsize[BMPIDX_WELCOME], bmpbuffer, &dummy);
524
    renderbmp(&lcdbuffer[176 * 25 + 25], bmpbuffer, 176);
525
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
526
    if (norword[0x400] == 0x53436667) appleflash = false;
527
    else if (norword[0x1000] == 0x53436667) appleflash = true;
528
    else panic(PANIC_KILLTHREAD, "Boot flash contents are damaged! "
529
                                 "(No SYSCFG found)\n\nPlease ask for help.\n");
530
    disk_unmount(0);
531
    rc = fat32_init();
532
    if (rc == 2) panic(PANIC_KILLTHREAD, "Data flash I/O error!");
533
    sleep(5000000);
534
    if (rc)
535
    {
536
        ucl_decompress(bitmapdata[BMPIDX_BADPARTITION], bitmapsize[BMPIDX_BADPARTITION],
537
                       bmpbuffer, &dummy);
538
        memcpy(lcdbuffer, backdrop, 0xb580);
539
        renderbmp(lcdbuffer, bmpbuffer, 176);
540
        displaylcd(0, 175, 0, 131, lcdbuffer, 0);
541
        while (true)
542
        {
543
            wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
544
            if (button == 2)
545
            {
546
                repartition = true;
547
                break;
548
            }
549
            else if (button == 4)
550
            {
551
                ucl_decompress(bitmapdata[BMPIDX_CANCELLED], bitmapsize[BMPIDX_CANCELLED],
552
                               bmpbuffer, &dummy);
553
                memcpy(lcdbuffer, backdrop, 0xb580);
554
                renderbmp(lcdbuffer, bmpbuffer, 176);
555
                displaylcd(0, 175, 0, 131, lcdbuffer, 0);
556
                sleep(500000);
557
                button = 0;
558
                while (!button) wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
559
                memcpy((void*)0x2202bf00, "diskmodehotstuff\1\0\0", 20);
560
                shutdown(false);
561
                reset();
562
            }
563
            button = 0;
564
        }
565
    }
566
    else if (fat32_startsector != 0)
567
    {
568
        ucl_decompress(bitmapdata[BMPIDX_REPARTITION], bitmapsize[BMPIDX_REPARTITION],
569
                       bmpbuffer, &dummy);
570
        memcpy(lcdbuffer, backdrop, 0xb580);
571
        renderbmp(lcdbuffer, bmpbuffer, 176);
572
        displaylcd(0, 175, 0, 131, lcdbuffer, 0);
573
        while (true)
574
        {
575
            wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
576
            if (button == 2)
577
            {
578
                repartition = true;
579
                break;
580
            }
581
            else if (button == 4) break;
582
            button = 0;
583
        }
584
    }
585
    ucl_decompress(bitmapdata[BMPIDX_INSTALLING], bitmapsize[BMPIDX_INSTALLING],
586
                   bmpbuffer, &dummy);
587
    renderbmp(backdrop, bmpbuffer, 176);
588
    ucl_decompress(bitmapdata[BMPIDX_PREPARING], bitmapsize[BMPIDX_PREPARING],
589
                    bmpbuffer, &dummy);
590
    memcpy(lcdbuffer, backdrop, 0xb580);
591
    renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
592
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
593
    free(bmpbuffer);
594
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 100);
595
 
596
    syscfgptr = malloc(0x1000);
597
    if (appleflash)
598
    {
599
        memcpy(syscfgptr, &nor[0x4000], 0x1000);
600
        if (readapplenor("hslfksid", &diskuclptr, &diskuclsize)) diskuclsize = 0;
601
        else
602
        {
603
            progressbar_setpos(&progressbar, 5, false);
604
            void* newptr = malloc(diskuclsize + (diskuclsize >> 3) + 256);
605
            if (ucl_nrv2e_99_compress(diskuclptr, diskuclsize, newptr,
606
                                      (uint32_t*)&diskuclsize, 0, 10, 0, 0))
607
            {
608
                free(newptr);
609
                diskuclsize = 0;
610
            }
611
            free(diskuclptr);
612
            realloc(newptr, diskuclsize);
613
            diskuclptr = newptr;
614
        }
615
        progressbar_setpos(&progressbar, 35, false);
616
        if (readapplenor("hslfgaid", &diaguclptr, &diaguclsize)) diaguclsize = 0;
617
        else
618
        {
619
            progressbar_setpos(&progressbar, 40, false);
620
            void* newptr = malloc(diaguclsize + (diaguclsize >> 3) + 256);
621
            if (ucl_nrv2e_99_compress(diaguclptr, diaguclsize, newptr,
622
                                      (uint32_t*)&diaguclsize, 0, 10, 0, 0))
623
            {
624
                free(newptr);
625
                diaguclsize = 0;
626
            }
627
            free(diaguclptr);
628
            realloc(newptr, diaguclsize);
629
            diaguclptr = newptr;
630
        }
631
        progressbar_setpos(&progressbar, 70, false);
632
        if (readfw("DNANkbso", &ososptr, &osossize)) osossize = 0;
633
        if (osossize)
634
        {
635
            if (((uint8_t*)ososptr)[0x64d48] == 0x2b && ((uint8_t*)ososptr)[0x64d54] == 0x34)
636
            {
637
                ((uint8_t*)ososptr)[0x64d48] = 0x43;
638
                ((uint8_t*)ososptr)[0x64d54] = 0x52;
639
            }
640
            if (((uint8_t*)ososptr)[0x3acd8] == 0x01)
641
                ((uint8_t*)ososptr)[0x3acd8] = 0x00;
642
        }
643
        progressbar_setpos(&progressbar, 90, false);
644
    }
645
    else
646
    {
647
        memcpy(syscfgptr, &nor[0x1000], 0x1000);
648
        diskuclsize = bootflash_filesize("diskmode");
649
        if (diskuclsize > 0)
650
        {
651
            diskuclptr = bootflash_getaddr("diskmode");
652
            if (!(bootflash_attributes("diskmode") & 0x800))
653
            {
654
                void* newptr = malloc(diskuclsize + (diskuclsize >> 3) + 256);
655
                if (ucl_nrv2e_99_compress(diskuclptr, diskuclsize, newptr,
656
                                          (uint32_t*)&diskuclsize, 0, 10, 0, 0))
657
                {
658
                    free(newptr);
659
                    diskuclsize = 0;
660
                }
661
                realloc(newptr, diskuclsize);
662
                diskuclptr = newptr;
663
            }
664
        }
665
        progressbar_setpos(&progressbar, 45, false);
666
        diaguclsize = bootflash_filesize("diagmode");
667
        if (diaguclsize > 0)
668
        {
669
            diaguclptr = bootflash_getaddr("diagmode");
670
            if (!(bootflash_attributes("diagmode") & 0x800))
671
            {
672
                void* newptr = malloc(diaguclsize + (diaguclsize >> 3) + 256);
673
                if (ucl_nrv2e_99_compress(diaguclptr, diaguclsize, newptr,
674
                                          (uint32_t*)&diaguclsize, 0, 10, 0, 0))
675
                {
676
                    free(newptr);
677
                    diaguclsize = 0;
678
                }
679
                realloc(newptr, diaguclsize);
680
                diaguclptr = newptr;
681
            }
682
        }
683
        progressbar_setpos(&progressbar, 90, false);
684
    }
685
    norbuf = malloc(0x100000);
686
    memset(norbuf, 0xff, 0x100000);
687
    memcpy(&norbuf[0x1000], syscfgptr, 0x1000);
688
    free(syscfgptr);
689
    uint32_t sp = 0;
690
    uint32_t beginptr = 0x2000;
691
    uint32_t endptr = 0x100000;
692
    uint32_t dirptr = 0;
693
    while (script[sp])
694
    {
695
        uint32_t file = script[sp] & 0xff;
696
        uint32_t flags = (script[sp] >> 8) & 0xff;
697
        uint32_t align = (script[sp] >> 16) & 0xff;
698
        void* data;
699
        uint32_t size;
700
        sp++;
701
        switch (file)
702
        {
703
            case 1:
704
                data = diskuclptr;
705
                size = diskuclsize;
706
                flags |= 2;
707
                break;
708
            case 2:
709
                data = diaguclptr;
710
                size = diaguclsize;
711
                flags |= 2;
712
                break;
713
            default:
714
                data = &scriptb[script[sp++]];
715
                size = script[sp++];
716
        }
717
        if (size)
718
        {
719
            if (align && !(flags & 1))
720
            {
721
                if ((align << 12) < beginptr)
722
                    panicf(PANIC_KILLTHREAD, "Error: Align failed! (%02X)", align);
723
                beginptr = align << 12;
724
            }
725
            if (endptr - beginptr < size)
726
                panicf(PANIC_KILLTHREAD, "Error: Flash is full!");
727
            uint32_t storesize = size;
728
            if (flags & 2) storesize |= 0x80000000;
729
            if (flags & 1)
730
            {
731
                endptr -= ((size + 0xfff) & ~0xfff);
732
                memcpy(&norbuf[endptr], data, size);
733
                file = endptr;
734
            }
735
            else
736
            {
737
                memcpy(&norbuf[beginptr], data, size);
738
                file = beginptr;
739
                beginptr += ((size + 0xfff) & ~0xfff);
740
            }
741
            if (!(flags & 4))
742
            {
743
                if (dirptr >= 0x1000)
744
                    panicf(PANIC_KILLTHREAD, "Error: Directory is full!");
745
                memcpy(&norbuf[dirptr], &script[sp], 8);
746
                norbufword[(dirptr >> 2) + 2] = file;
747
                norbufword[(dirptr >> 2) + 3] = storesize;
748
                dirptr += 0x10;
749
                sp += 2;
750
            }
751
        }
752
        else if (!(flags & 4)) sp += 2;
753
    }
754
    progressbar_setpos(&progressbar, 100, false);
755
    if (diskuclptr && (uint32_t)diskuclptr < 0x24000000) free(diskuclptr);
756
    if (diaguclptr && (uint32_t)diaguclptr < 0x24000000) free(diaguclptr);
757
 
758
    if (repartition)
759
    {
760
        bmpbuffer = malloc(0xb600);
761
        memcpy(lcdbuffer, backdrop, 0xb580);
762
        ucl_decompress(bitmapdata[BMPIDX_REPARTITIONING], bitmapsize[BMPIDX_REPARTITIONING],
763
                       bmpbuffer, &dummy);
764
        renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
765
        displaylcd(0, 175, 0, 131, lcdbuffer, 0);
766
        free(bmpbuffer);
767
        progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 100);
768
        if (fat32_resize_fulldisk(&progressbar))
769
            panic(PANIC_KILLTHREAD, "Data flash I/O error!");
770
    }
771
 
772
    bmpbuffer = malloc(0xb600);
773
    memcpy(lcdbuffer, backdrop, 0xb580);
774
    ucl_decompress(bitmapdata[BMPIDX_INSTALLFILES], bitmapsize[BMPIDX_INSTALLFILES],
775
                   bmpbuffer, &dummy);
776
    renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
777
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
778
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 100);
779
    disk_mount(0);
780
    int updating = mkdir("/iLoader");
781
    int status;
782
    if (updating)
783
    {
784
        status = script[sp + 3];
785
        sp = script[sp + 1] >> 2;
786
    }
787
    else
788
    {
789
        status = script[sp + 2] + script[sp + 3];
790
        sp += 4;
791
    }
792
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, status);
793
    status = 0;
794
    while (script[sp])
795
    {
796
        int fd;
797
        void* data;
798
        switch (script[sp])
799
        {
800
            case 1:
801
                mkdir(&scriptb[script[sp + 1]]);
802
                sp += 2;
803
                break;
804
            case 2:
805
                if (script[sp + 2] == 0xffffffff)
806
                {
807
                    data = ososptr;
808
                    script[sp + 3] = osossize;
809
                }
810
                else if (script[sp + 2] == 0xfffffffe)
811
                {
812
                    data = nor;
813
                    script[sp + 3] = 0x100000;
814
                }
815
                if (!script[sp + 3])
816
                {
817
                    sp += 4;
818
                    break;
819
                }
820
            case 3:
821
                fd = file_open(&scriptb[script[sp + 1]], O_RDONLY);
822
                if (fd >= 0)
823
                {
824
                    close(fd);
825
                    sp += 4;
826
                    break;
827
                }
828
            case 4:
829
                if (script[sp + 2] < 0xfffffffe) data = &scriptb[script[sp + 2]];
830
                fd = file_creat(&scriptb[script[sp + 1]]);
831
                if (fd >= 0)
832
                {
833
                    write(fd, data, script[sp + 3]);
834
                    close(fd);
835
                }
836
                sp += 4;
837
                break;
838
            default:
839
                panic(PANIC_KILLTHREAD, "Bad installation script!");
840
        }
841
        status += script[sp++];
842
        progressbar_setpos(&progressbar, status, false);
843
    }
844
 
845
    bmpbuffer = malloc(0xb600);
846
    memcpy(lcdbuffer, backdrop, 0xb580);
847
    ucl_decompress(bitmapdata[BMPIDX_FLASHING], bitmapsize[BMPIDX_FLASHING], bmpbuffer, &dummy);
848
    renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
849
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
850
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 256);
851
    for (i = 0; i < 256; i++)
852
    {
853
        bootflash_writeraw(&norbuf[i << 12], i << 12, 1 << 12);
854
        progressbar_setpos(&progressbar, i, false);
855
    }
856
 
857
    shutdown(false);
858
    reset();
859
}