Subversion Repositories freemyipod

Rev

Rev 630 | Rev 672 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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