Subversion Repositories freemyipod

Rev

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