Subversion Repositories freemyipod

Rev

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

Rev 414 Rev 551
Line 1... Line 1...
1
//
1
//
2
//
2
//
3
//    Copyright 2010 TheSeven
3
//    Copyright 2010 TheSeven
4
//
4
//
5
//
5
//
6
//    This file is part of emBIOS.
6
//    This file is part of emCORE.
7
//
7
//
8
//    emBIOS is free software: you can redistribute it and/or
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
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
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
11
//    License, or (at your option) any later version.
12
//
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
16
//    See the GNU General Public License for more details.
17
//
17
//
18
//    You should have received a copy of the GNU General Public License along
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/>.
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
20
//
21
//
21
//
22
 
22
 
23
 
23
 
24
#include "embiosapp.h"
24
#include "emcoreapp.h"
-
 
25
#include "libpng.h"
-
 
26
#include "libui.h"
25
 
27
 
26
 
28
 
27
void main();
29
#define STRINGIFY(x) #x
28
EMBIOS_APP_HEADER("Installer thread", 0x10000, main, 127)
30
#define BOOTNOTE_FILENAME "/Notes/" STRINGIFY(BASENAME) ".bootnote"
29
 
31
 
30
 
32
 
31
uint16_t lcdbuffer[176 * 132];
-
 
32
uint16_t backdrop[176 * 132];
-
 
33
#define BMPIDX_BACKDROP 0
-
 
34
#define BMPIDX_WELCOME 1
33
void main();
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
34
EMCORE_APP_HEADER("emCORE installer", main, 127)
41
#define BMPIDX_INSTALLFILES 8
-
 
42
#define BMPIDX_FLASHING 9
-
 
43
 
35
 
44
struct wakeup eventwakeup;
-
 
45
volatile int button;
-
 
46
 
36
 
-
 
37
extern char background_png[];
47
char mallocbuf[0xec0000] __attribute__((aligned(16)));
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;
48
tlsf_pool mallocpool;
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[];
-
 
52
 
49
 
53
 
50
uint32_t fat32_ok;
54
uint32_t fat32_ok;
51
uint32_t fat32_startsector;
55
uint32_t fat32_startsector;
52
uint32_t fat32_secperclus;
56
uint32_t fat32_secperclus;
53
uint32_t fat32_database;
57
uint32_t fat32_database;
Line 56... Line 60...
56
uint32_t fat32_fatcount;
60
uint32_t fat32_fatcount;
57
uint32_t fat32_sectorcount;
61
uint32_t fat32_sectorcount;
58
uint32_t fat32_clustercount;
62
uint32_t fat32_clustercount;
59
uint32_t fat32_rootdirclus;
63
uint32_t fat32_rootdirclus;
60
 
64
 
-
 
65
struct wakeup eventwakeup;
-
 
66
volatile int button;
-
 
67
volatile int scrollpos;
-
 
68
 
-
 
69
 
61
#define nor ((uint8_t*)0x24000000)
70
#define nor ((uint8_t*)0x24000000)
62
#define norword ((uint32_t*)0x24000000)
71
#define norword ((uint32_t*)0x24000000)
63
 
72
 
64
extern uint32_t _scriptstart;
-
 
65
 
73
 
66
 
-
 
67
void handler(enum button_event eventtype, int which, int value)
74
void handler(void* user, enum button_event eventtype, int which, int value)
68
{
75
{
69
    if (eventtype == BUTTON_PRESS) button |= 1 << which;
76
    if (eventtype == BUTTON_PRESS) button |= 1 << which;
-
 
77
    if (eventtype == WHEEL_MOVED_ACCEL)
-
 
78
        scrollpos = MAX(0, MIN(309, scrollpos + value / 8));
70
    wakeup_signal(&eventwakeup);
79
    wakeup_signal(&eventwakeup);
71
}
80
}
72
 
81
 
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)
82
uint32_t freeret(uint32_t rc, void* ptr)
100
{
83
{
101
    tlsf_free(mallocpool, ptr);
84
    free(ptr);
102
    return rc;
85
    return rc;
103
}
86
}
104
 
87
 
105
int decryptfw(void* image, uint32_t offset)
88
int decryptfw(void* image, uint32_t offset)
106
{
89
{
107
    uint32_t size = ((uint32_t*)image)[5];
90
    uint32_t size = ((uint32_t*)image)[5];
108
    if (size > 0x800000) return 0;
91
    if (size > 0x800000) return 0;
109
    hwkeyaes(HWKEYAES_DECRYPT, ((uint32_t*)image)[2], &((uint8_t*)image)[offset], size);
92
    hwkeyaes(HWKEYAES_DECRYPT, ((uint32_t*)image)[2], &((uint8_t*)image)[offset], size);
110
    memcpy(image, &((uint8_t*)image)[offset], size);
93
    memcpy(image, &((uint8_t*)image)[offset], size);
-
 
94
    cputc(3, '.');
111
    return size;
95
    return size;
112
}
96
}
113
 
97
 
114
uint32_t getfw(const char* filename, uint32_t* sector, int* size)
98
uint32_t getfw(const char* filename, uint32_t* sector, int* size)
115
{
99
{
116
    uint32_t i;
100
    uint32_t i;
117
    uint32_t* buffer = tlsf_memalign(mallocpool, 0x10, 0x800);
101
    uint32_t* buffer = memalign(0x10, 0x800);
118
    if (storage_read_sectors_md(0, 0, 1, buffer) != 0) return freeret(1, buffer);
102
    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);
103
    if (*((uint16_t*)((uint32_t)buffer + 0x1FE)) != 0xAA55) return freeret(1, buffer);
120
    uint32_t startsector = 0;
104
    uint32_t startsector = 0;
121
    for (i = 0x1C2; i < 0x200; i += 0x10)
105
    for (i = 0x1C2; i < 0x200; i += 0x10)
122
        if (((uint8_t*)buffer)[i] == 0)
106
        if (((uint8_t*)buffer)[i] == 0)
123
        {
107
        {
124
            startsector = *((uint16_t*)((uint32_t)buffer + i + 4))
108
            startsector = *((uint16_t*)((uint32_t)buffer + i + 4))
125
                        | (*((uint16_t*)((uint32_t)buffer + i + 6)) << 16);
109
                        | (*((uint16_t*)((uint32_t)buffer + i + 6)) << 16);
126
            break;
110
            break;
127
        }
111
        }
-
 
112
    cputc(3, '.');
128
    if (startsector == 0) return freeret(1, buffer);
113
    if (startsector == 0) return freeret(1, buffer);
129
    if (storage_read_sectors_md(0, startsector, 1, buffer) != 0) return freeret(1, buffer);
114
    if (storage_read_sectors_md(0, startsector, 1, buffer) != 0) return freeret(1, buffer);
-
 
115
    cputc(3, '.');
130
    if (buffer[0x40] != 0x5B68695D) return freeret(1, buffer);
116
    if (buffer[0x40] != 0x5B68695D) return freeret(1, buffer);
131
    if (storage_read_sectors_md(0, startsector + 1 + (buffer[0x41] >> 11), 1, buffer) != 0)
117
    if (storage_read_sectors_md(0, startsector + 1 + (buffer[0x41] >> 11), 1, buffer) != 0)
132
        return freeret(1, buffer);
118
        return freeret(1, buffer);
-
 
119
    cputc(3, '.');
133
    for (i = 0; i < 0x1FE; i += 10)
120
    for (i = 0; i < 0x1FE; i += 10)
134
        if (memcmp(&buffer[i], filename, 8) == 0)
121
        if (memcmp(&buffer[i], filename, 8) == 0)
135
        {
122
        {
136
            *sector = startsector + (buffer[i + 3] >> 11);
123
            *sector = startsector + (buffer[i + 3] >> 11);
137
            *size = buffer[i + 4] + 0x800;
124
            *size = buffer[i + 4] + 0x800;
138
            tlsf_free(mallocpool, buffer);
125
            free(buffer);
-
 
126
            cputc(3, '.');
139
            return 0;
127
            return 0;
140
        }
128
        }
141
    return freeret(2, buffer);
129
    return freeret(2, buffer);
142
}
130
}
143
 
131
 
144
uint32_t readfw(const char* filename, void** address, int* size)
132
uint32_t readfw(const char* filename, void** address, int* size)
145
{
133
{
146
    uint32_t sector;
134
    uint32_t sector;
147
    uint32_t rc = getfw(filename, &sector, size);
135
    uint32_t rc = getfw(filename, &sector, size);
148
    if (rc) return rc;
136
    if (rc) return rc;
149
    *address = tlsf_memalign(mallocpool, 0x10, *size);
137
    *address = memalign(0x10, *size);
150
    if (storage_read_sectors_md(0, sector, ((*size + 0x7FF) >> 11), *address) != 0)
138
    if (storage_read_sectors_md(0, sector, ((*size + 0x7FF) >> 11), *address) != 0)
151
        return freeret(1, *address);
139
        return freeret(1, *address);
-
 
140
    cputc(3, '.');
152
    *size = decryptfw(*address, 0x800);
141
    *size = decryptfw(*address, 0x800);
153
    tlsf_realloc(mallocpool, *address, *size);
142
    realloc(*address, *size);
154
    return 0;
143
    return 0;
155
}
144
}
156
 
145
 
157
uint32_t getapplenor(const char* filename, void** address, int* size)
146
uint32_t getapplenor(const char* filename, void** address, int* size)
158
{
147
{
Line 172... Line 161...
172
    void* noraddr;
161
    void* noraddr;
173
    uint32_t rc = getapplenor(filename, &noraddr, size);
162
    uint32_t rc = getapplenor(filename, &noraddr, size);
174
    if (rc) return rc;
163
    if (rc) return rc;
175
    *address = malloc(*size);
164
    *address = malloc(*size);
176
    memcpy(*address, noraddr, *size);
165
    memcpy(*address, noraddr, *size);
-
 
166
    cputc(3, '.');
177
    *size = decryptfw(*address, 0x200);
167
    *size = decryptfw(*address, 0x200);
178
    realloc(*address, *size);
168
    realloc(*address, *size);
179
    return 0;
169
    return 0;
180
}
170
}
181
 
171
 
Line 483... Line 473...
483
}
473
}
484
 
474
 
485
void main(void)
475
void main(void)
486
{
476
{
487
    uint32_t i, j, k, rc;
477
    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;
478
    uint32_t dummy;
493
    int deleterc = 1;
479
    int deleterc = 1;
494
    struct progressbar_state progressbar;
480
    struct progressbar_state progressbar;
495
    bool repartition = false;
481
    bool repartition = false;
496
    bool appleflash;
482
    bool appleflash;
Line 502... Line 488...
502
    int diskuclsize = 0;
488
    int diskuclsize = 0;
503
    void* diskuclptr;
489
    void* diskuclptr;
504
    uint8_t* norbuf;
490
    uint8_t* norbuf;
505
#define norbufword ((uint32_t*)norbuf)
491
#define norbufword ((uint32_t*)norbuf)
506
 
492
 
507
    button = 0;
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;
508
    wakeup_init(&eventwakeup);
497
    cputc(3, '.');
-
 
498
    struct emcorelib_header* libui = get_library(0x49554365, LIBUI_API_VERSION, LIBSOURCE_RAM_NEEDCOPY, f_ui_emcorelib);
509
    button_register_handler(handler);
499
    if (!libui) panicf(PANIC_KILLTHREAD, "Could not load user interface library!");
510
    mallocpool = tlsf_create(mallocbuf, sizeof(mallocbuf));
500
    struct libui_api* ui = (struct libui_api*)libui->api;
-
 
501
    cputc(3, '.');
511
 
502
 
-
 
503
    struct png_info* handle = png->png_open(background_png, background_png_size);
-
 
504
    if (!handle) panicf(PANIC_KILLTHREAD, "Could not parse background image!");
512
    script = &_scriptstart;
505
    cputc(3, '.');
-
 
506
    struct png_rgb* bg = png->png_decode_rgb(handle);
-
 
507
    if (!bg) panicf(PANIC_KILLTHREAD, "Could not decode background image!");
513
    for (i = 0; i < 10; i++)
508
    png->png_destroy(handle);
514
    {
509
    cputc(3, '.');
515
        bitmapsize[i] = *script;
510
    void* darkened = malloc(176 * 132 * 3);
-
 
511
    if (!darkened) panicf(PANIC_KILLTHREAD, "Could not allocate darkened image!");
516
        bitmapdata[i] = &script[1];
512
    cputc(3, '.');
517
        script = &script[1 + (bitmapsize[i] >> 2)];
513
    handle = png->png_open(darkener_png, darkener_png_size);
-
 
514
    if (!handle) panicf(PANIC_KILLTHREAD, "Could not parse darkener image!");
518
    }
515
    cputc(3, '.');
519
 
-
 
-
 
516
    struct png_rgba* darkener = png->png_decode_rgba(handle);
-
 
517
    if (!darkener) panicf(PANIC_KILLTHREAD, "Could not decode darkener image!");
520
    void* bmpbuffer = malloc(0xb600);
518
    png->png_destroy(handle);
-
 
519
    cputc(3, '.');
521
    ucl_decompress(bitmapdata[BMPIDX_BACKDROP], bitmapsize[BMPIDX_BACKDROP], bmpbuffer, &dummy);
520
    ui->blenda(176, 132, 255, darkened, 0, 0, 176, bg, 0, 0, 176, darkener, 0, 0, 176);
-
 
521
    free(darkener);
-
 
522
    cputc(3, '.');
522
    renderbmp(backdrop, bmpbuffer, 176);
523
    handle = png->png_open(disclaimer_png, disclaimer_png_size);
-
 
524
    if (!handle) panicf(PANIC_KILLTHREAD, "Could not parse disclaimer image!");
-
 
525
    cputc(3, '.');
523
    memcpy(lcdbuffer, backdrop, 0xb580);
526
    struct png_rgba* disclaimer = png->png_decode_rgba(handle);
524
    ucl_decompress(bitmapdata[BMPIDX_WELCOME], bitmapsize[BMPIDX_WELCOME], bmpbuffer, &dummy);
527
    if (!disclaimer) panicf(PANIC_KILLTHREAD, "Could not decode disclaimer image!");
-
 
528
    png->png_destroy(handle);
-
 
529
    cputc(3, '.');
525
    renderbmp(&lcdbuffer[176 * 25 + 25], bmpbuffer, 176);
530
    handle = png->png_open(actions_png, actions_png_size);
-
 
531
    if (!handle) panicf(PANIC_KILLTHREAD, "Could not parse actions image!");
-
 
532
    cputc(3, '.');
526
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
533
    struct png_rgba* actions = png->png_decode_rgba(handle);
-
 
534
    if (!actions) panicf(PANIC_KILLTHREAD, "Could not decode actions image!");
527
    backlight_set_fade(32);
535
    png->png_destroy(handle);
-
 
536
    cputc(3, '.');
528
    backlight_set_brightness(177);
537
    void* framebuf = malloc(160 * 91 * 3);
-
 
538
    if (!framebuf) panicf(PANIC_KILLTHREAD, "Could not allocate frame buffer!");
529
    backlight_on(true);
539
    cputc(3, '.');
530
 
540
 
531
    if (*script) deleterc = remove((char*)&script[1]);
541
    deleterc = remove(BOOTNOTE_FILENAME);
-
 
542
    cputc(3, '.');
-
 
543
    disk_unmount(0);
532
    script = &script[1 + *script];
544
    rc = fat32_init();
-
 
545
    if (rc == 2) panic(PANIC_KILLTHREAD, "Data flash I/O error!");
-
 
546
    cputc(3, '.');
533
 
547
 
534
    if (norword[0x400] == 0x53436667) appleflash = false;
548
    if (norword[0x400] == 0x53436667) appleflash = false;
535
    else if (norword[0x1000] == 0x53436667) appleflash = true;
549
    else if (norword[0x1000] == 0x53436667) appleflash = true;
536
    else panic(PANIC_KILLTHREAD, "Boot flash contents are damaged! "
550
    else panic(PANIC_KILLTHREAD, "Boot flash contents are damaged! "
537
                                 "(No SYSCFG found)\n\nPlease ask for help.\n");
551
                                 "(No SYSCFG found)\n\nPlease ask for help.\n");
538
    disk_unmount(0);
-
 
539
    rc = fat32_init();
-
 
540
    if (rc == 2) panic(PANIC_KILLTHREAD, "Data flash I/O error!");
-
 
541
    sleep(5000000);
-
 
542
    if (rc)
-
 
543
    {
-
 
544
        ucl_decompress(bitmapdata[BMPIDX_BADPARTITION], bitmapsize[BMPIDX_BADPARTITION],
-
 
545
                       bmpbuffer, &dummy);
-
 
546
        memcpy(lcdbuffer, backdrop, 0xb580);
-
 
547
        renderbmp(lcdbuffer, bmpbuffer, 176);
-
 
548
        displaylcd(0, 175, 0, 131, lcdbuffer, 0);
-
 
549
        while (true)
-
 
550
        {
-
 
551
            wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
-
 
552
            if (button == 2)
-
 
553
            {
-
 
554
                repartition = true;
-
 
555
                break;
-
 
556
            }
-
 
557
            else if (button == 4)
-
 
558
            {
-
 
559
                if (deleterc)
-
 
560
                {
-
 
561
                    ucl_decompress(bitmapdata[BMPIDX_CANCELLED], bitmapsize[BMPIDX_CANCELLED],
-
 
562
                                   bmpbuffer, &dummy);
-
 
563
                    memcpy(lcdbuffer, backdrop, 0xb580);
-
 
564
                    renderbmp(lcdbuffer, bmpbuffer, 176);
-
 
565
                    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
-
 
566
                    sleep(500000);
-
 
567
                    button = 0;
-
 
568
                    while (!button) wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
-
 
569
                    memcpy((void*)0x2202bf00, "diskmodehotstuff\1\0\0", 20);
-
 
570
                }
-
 
571
                shutdown(false);
-
 
572
                reset();
-
 
573
            }
-
 
574
            button = 0;
-
 
575
        }
-
 
576
    }
-
 
577
    else if (fat32_startsector != 0)
-
 
578
    {
-
 
579
        ucl_decompress(bitmapdata[BMPIDX_REPARTITION], bitmapsize[BMPIDX_REPARTITION],
-
 
580
                       bmpbuffer, &dummy);
-
 
581
        memcpy(lcdbuffer, backdrop, 0xb580);
-
 
582
        renderbmp(lcdbuffer, bmpbuffer, 176);
-
 
583
        displaylcd(0, 175, 0, 131, lcdbuffer, 0);
-
 
584
        while (true)
-
 
585
        {
-
 
586
            wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
-
 
587
            if (button == 2)
-
 
588
            {
-
 
589
                repartition = true;
-
 
590
                break;
-
 
591
            }
-
 
592
            else if (button == 4) break;
-
 
593
            button = 0;
-
 
594
        }
-
 
595
    }
-
 
596
    ucl_decompress(bitmapdata[BMPIDX_INSTALLING], bitmapsize[BMPIDX_INSTALLING],
-
 
597
                   bmpbuffer, &dummy);
-
 
598
    renderbmp(backdrop, bmpbuffer, 176);
-
 
599
    ucl_decompress(bitmapdata[BMPIDX_PREPARING], bitmapsize[BMPIDX_PREPARING],
-
 
600
                    bmpbuffer, &dummy);
-
 
601
    memcpy(lcdbuffer, backdrop, 0xb580);
-
 
602
    renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
-
 
603
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
-
 
604
    free(bmpbuffer);
-
 
605
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 100);
-
 
606
 
552
 
607
    syscfgptr = malloc(0x1000);
-
 
-
 
553
 
608
    if (appleflash)
554
    if (appleflash)
609
    {
555
    {
610
        memcpy(syscfgptr, &nor[0x4000], 0x1000);
556
        syscfgptr = &nor[0x4000];
611
        if (readapplenor("hslfksid", &diskuclptr, &diskuclsize)) diskuclsize = 0;
557
        if (readapplenor("hslfksid", &diskuclptr, &diskuclsize)) diskuclsize = 0;
612
        else
558
        else
613
        {
559
        {
614
            progressbar_setpos(&progressbar, 5, false);
560
            cputc(3, '.');
615
            void* newptr = malloc(diskuclsize + (diskuclsize >> 3) + 256);
561
            void* newptr = malloc(diskuclsize + (diskuclsize >> 3) + 256);
616
            if (ucl_nrv2e_99_compress(diskuclptr, diskuclsize, newptr,
562
            if (ucl_nrv2e_99_compress(diskuclptr, diskuclsize, newptr,
617
                                      (uint32_t*)&diskuclsize, 0, 10, 0, 0))
563
                                      (uint32_t*)&diskuclsize, 0, 10, 0, 0))
618
            {
564
            {
619
                free(newptr);
565
                free(newptr);
620
                diskuclsize = 0;
566
                diskuclsize = 0;
621
            }
567
            }
-
 
568
            cputc(3, '.');
622
            free(diskuclptr);
569
            free(diskuclptr);
623
            realloc(newptr, diskuclsize);
570
            realloc(newptr, diskuclsize);
624
            diskuclptr = newptr;
571
            diskuclptr = newptr;
625
        }
572
        }
626
        progressbar_setpos(&progressbar, 35, false);
573
        cputc(3, '.');
627
        if (readapplenor("hslfgaid", &diaguclptr, &diaguclsize)) diaguclsize = 0;
574
        if (readapplenor("hslfgaid", &diaguclptr, &diaguclsize)) diaguclsize = 0;
628
        else
575
        else
629
        {
576
        {
630
            progressbar_setpos(&progressbar, 40, false);
577
            cputc(3, '.');
631
            void* newptr = malloc(diaguclsize + (diaguclsize >> 3) + 256);
578
            void* newptr = malloc(diaguclsize + (diaguclsize >> 3) + 256);
632
            if (ucl_nrv2e_99_compress(diaguclptr, diaguclsize, newptr,
579
            if (ucl_nrv2e_99_compress(diaguclptr, diaguclsize, newptr,
633
                                      (uint32_t*)&diaguclsize, 0, 10, 0, 0))
580
                                      (uint32_t*)&diaguclsize, 0, 10, 0, 0))
634
            {
581
            {
635
                free(newptr);
582
                free(newptr);
636
                diaguclsize = 0;
583
                diaguclsize = 0;
637
            }
584
            }
-
 
585
            cputc(3, '.');
638
            free(diaguclptr);
586
            free(diaguclptr);
639
            realloc(newptr, diaguclsize);
587
            realloc(newptr, diaguclsize);
640
            diaguclptr = newptr;
588
            diaguclptr = newptr;
641
        }
589
        }
642
        progressbar_setpos(&progressbar, 70, false);
590
        cputc(3, '.');
643
        if (readfw(deleterc ? "DNANkbso" : "DNANsoso", &ososptr, &osossize)) osossize = 0;
591
        if (readfw(deleterc ? "DNANkbso" : "DNANsoso", &ososptr, &osossize)) osossize = 0;
644
        if (osossize)
592
        if (osossize)
645
        {
593
        {
-
 
594
            cputc(3, '.');
646
            if (((uint8_t*)ososptr)[0x64d48] == 0x2b && ((uint8_t*)ososptr)[0x64d54] == 0x34)
595
            if (((uint8_t*)ososptr)[0x64d48] == 0x2b && ((uint8_t*)ososptr)[0x64d54] == 0x34)
647
            {
596
            {
648
                ((uint8_t*)ososptr)[0x64d48] = 0x43;
597
                ((uint8_t*)ososptr)[0x64d48] = 0x43;
649
                ((uint8_t*)ososptr)[0x64d54] = 0x52;
598
                ((uint8_t*)ososptr)[0x64d54] = 0x52;
650
            }
599
            }
651
            if (((uint8_t*)ososptr)[0x3acd8] == 0x01)
600
            if (((uint8_t*)ososptr)[0x3acd8] == 0x01)
652
                ((uint8_t*)ososptr)[0x3acd8] = 0x00;
601
                ((uint8_t*)ososptr)[0x3acd8] = 0x00;
653
        }
602
        }
654
        progressbar_setpos(&progressbar, 90, false);
-
 
655
    }
603
    }
656
    else
604
    else
657
    {
605
    {
658
        memcpy(syscfgptr, &nor[0x1000], 0x1000);
606
        syscfgptr = &nor[0x1000];
659
        diskuclsize = bootflash_filesize("diskmode");
607
        diskuclsize = bootflash_filesize("diskmode");
660
        if (diskuclsize > 0)
608
        if (diskuclsize > 0)
661
        {
609
        {
662
            diskuclptr = bootflash_getaddr("diskmode");
610
            diskuclptr = bootflash_getaddr("diskmode");
663
            if (!(bootflash_attributes("diskmode") & 0x800))
611
            if (!(bootflash_attributes("diskmode") & 0x800))
Line 667... Line 615...
667
                                          (uint32_t*)&diskuclsize, 0, 10, 0, 0))
615
                                          (uint32_t*)&diskuclsize, 0, 10, 0, 0))
668
                {
616
                {
669
                    free(newptr);
617
                    free(newptr);
670
                    diskuclsize = 0;
618
                    diskuclsize = 0;
671
                }
619
                }
-
 
620
                cputc(3, '.');
672
                realloc(newptr, diskuclsize);
621
                realloc(newptr, diskuclsize);
673
                diskuclptr = newptr;
622
                diskuclptr = newptr;
674
            }
623
            }
675
        }
624
        }
676
        progressbar_setpos(&progressbar, 45, false);
625
        cputc(3, '.');
677
        diaguclsize = bootflash_filesize("diagmode");
626
        diaguclsize = bootflash_filesize("diagmode");
678
        if (diaguclsize > 0)
627
        if (diaguclsize > 0)
679
        {
628
        {
680
            diaguclptr = bootflash_getaddr("diagmode");
629
            diaguclptr = bootflash_getaddr("diagmode");
681
            if (!(bootflash_attributes("diagmode") & 0x800))
630
            if (!(bootflash_attributes("diagmode") & 0x800))
Line 685... Line 634...
685
                                          (uint32_t*)&diaguclsize, 0, 10, 0, 0))
634
                                          (uint32_t*)&diaguclsize, 0, 10, 0, 0))
686
                {
635
                {
687
                    free(newptr);
636
                    free(newptr);
688
                    diaguclsize = 0;
637
                    diaguclsize = 0;
689
                }
638
                }
-
 
639
                cputc(3, '.');
690
                realloc(newptr, diaguclsize);
640
                realloc(newptr, diaguclsize);
691
                diaguclptr = newptr;
641
                diaguclptr = newptr;
692
            }
642
            }
693
        }
643
        }
694
        progressbar_setpos(&progressbar, 90, false);
-
 
695
    }
644
    }
-
 
645
    cputc(3, '.');
696
    norbuf = malloc(0x100000);
646
    norbuf = malloc(0x100000);
697
    memset(norbuf, 0xff, 0x100000);
647
    memset(norbuf, 0xff, 0x100000);
698
    memcpy(&norbuf[0x1000], syscfgptr, 0x1000);
648
    memcpy(&norbuf[0x1000], syscfgptr, 0x1000);
699
    free(syscfgptr);
649
    free(syscfgptr);
-
 
650
    cputc(3, '.');
-
 
651
 
-
 
652
    uint32_t* script = flashscript;
700
    uint32_t sp = 0;
653
    uint32_t sp = 0;
701
    uint32_t beginptr = 0x2000;
654
    uint32_t beginptr = 0x2000;
702
    uint32_t endptr = 0x100000;
655
    uint32_t endptr = 0x100000;
703
    uint32_t dirptr = 0;
656
    uint32_t dirptr = 0;
704
    while (script[sp])
657
    while (script[sp])
Line 720... Line 673...
720
                data = diaguclptr;
673
                data = diaguclptr;
721
                size = diaguclsize;
674
                size = diaguclsize;
722
                flags |= 2;
675
                flags |= 2;
723
                break;
676
                break;
724
            default:
677
            default:
725
                data = &scriptb[script[sp++]];
678
                data = (void*)(script[sp++]);
726
                size = script[sp++];
679
                size = script[sp++];
727
        }
680
        }
728
        if (size)
681
        if (size)
729
        {
682
        {
730
            if (align && !(flags & 1))
683
            if (align && !(flags & 1))
Line 759... Line 712...
759
                dirptr += 0x10;
712
                dirptr += 0x10;
760
                sp += 2;
713
                sp += 2;
761
            }
714
            }
762
        }
715
        }
763
        else if (!(flags & 4)) sp += 2;
716
        else if (!(flags & 4)) sp += 2;
-
 
717
        cputc(3, '.');
764
    }
718
    }
765
    progressbar_setpos(&progressbar, 100, false);
-
 
766
    if (diskuclptr && (uint32_t)diskuclptr < 0x24000000) free(diskuclptr);
719
    if (diskuclptr && (uint32_t)diskuclptr < 0x24000000) free(diskuclptr);
767
    if (diaguclptr && (uint32_t)diaguclptr < 0x24000000) free(diaguclptr);
720
    if (diaguclptr && (uint32_t)diaguclptr < 0x24000000) free(diaguclptr);
-
 
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!");
-
 
728
 
-
 
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!");
768
 
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
            
769
    if (repartition)
830
    if (repartition)
770
    {
831
    {
771
        bmpbuffer = malloc(0xb600);
-
 
772
        memcpy(lcdbuffer, backdrop, 0xb580);
-
 
773
        ucl_decompress(bitmapdata[BMPIDX_REPARTITIONING], bitmapsize[BMPIDX_REPARTITIONING],
832
        ui->blenda(110, 20, 255, framebuf, 0, 0, 110, bg, 33, 55, 176, actions, 0, 0, 110);
774
                       bmpbuffer, &dummy);
-
 
775
        renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
833
        displaylcd(0, 0, 176, 132, bg, 0, 0, 176);
776
        displaylcd(0, 175, 0, 131, lcdbuffer, 0);
834
        displaylcd(33, 55, 110, 20, framebuf, 0, 0, 110);
777
        free(bmpbuffer);
-
 
778
        progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 100);
835
        progressbar_init(&progressbar, 10, 165, 74, 83, 0x77ff, 0xe8, 0x125f, 0, 100);
779
        if (fat32_resize_fulldisk(&progressbar))
836
        if (fat32_resize_fulldisk(&progressbar))
780
            panic(PANIC_KILLTHREAD, "Data flash I/O error!");
837
            panic(PANIC_KILLTHREAD, "Data flash I/O error!");
781
    }
838
    }
782
 
839
 
783
    bmpbuffer = malloc(0xb600);
840
    ui->blenda(110, 20, 255, framebuf, 0, 0, 110, bg, 33, 55, 176, actions, 0, 20, 110);
784
    memcpy(lcdbuffer, backdrop, 0xb580);
841
    displaylcd(0, 0, 176, 132, bg, 0, 0, 176);
785
    ucl_decompress(bitmapdata[BMPIDX_INSTALLFILES], bitmapsize[BMPIDX_INSTALLFILES],
842
    displaylcd(33, 55, 110, 20, framebuf, 0, 0, 110);
786
                   bmpbuffer, &dummy);
843
    backlight_set_fade(32);
787
    renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
844
    backlight_set_brightness(177);
788
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
845
    backlight_on(true);
789
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 100);
-
 
-
 
846
 
790
    disk_mount(0);
847
    disk_mount(0);
791
    int updating = mkdir("/iLoader");
848
    int updating = !(appleflash || rc);
792
    int status;
849
    int cost;
793
    if (updating)
850
    if (updating)
794
    {
851
    {
795
        status = script[sp + 3];
852
        cost = commoncost;
796
        sp = script[sp + 1] >> 2;
853
        script = commonscript;
797
    }
854
    }
798
    else
855
    else
799
    {
856
    {
800
        status = script[sp + 2] + script[sp + 3];
857
        cost = firstinstcost + commoncost;
801
        sp += 4;
858
        script = firstinstscript;
802
    }
859
    }
803
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, status);
860
    progressbar_init(&progressbar, 10, 165, 74, 83, 0x77ff, 0xe8, 0x125f, 0, cost);
-
 
861
    sp = 0;
804
    status = 0;
862
    cost = 0;
805
    while (script[sp])
863
    while (script[sp])
806
    {
864
    {
807
        int fd;
865
        int fd;
808
        void* data;
866
        void* data;
809
        switch (script[sp])
867
        switch (script[sp])
810
        {
868
        {
811
            case 1:
869
            case 1:
812
                mkdir(&scriptb[script[sp + 1]]);
870
                mkdir((char*)(script[sp + 1]));
813
                sp += 2;
871
                sp += 2;
814
                break;
872
                break;
815
            case 2:
873
            case 2:
816
                if (script[sp + 2] == 0xffffffff)
874
                if (script[sp + 2] == 0xffffffff)
817
                {
875
                {
818
                    data = ososptr;
876
                    data = ososptr;
819
                    script[sp + 3] = osossize;
877
                    script[sp + 3] = osossize;
820
                }
878
                }
821
                else if (script[sp + 2] == 0xfffffffe)
879
                else if (script[sp + 2] == 0xfffffffe && appleflash)
822
                {
880
                {
823
                    data = nor;
881
                    data = nor;
824
                    script[sp + 3] = 0x100000;
882
                    script[sp + 3] = 0x100000;
825
                }
883
                }
-
 
884
                else if (script[sp + 2] == 0xfffffffe) script[sp + 3] = 0;
826
                if (!script[sp + 3])
885
                if (!script[sp + 3])
827
                {
886
                {
828
                    sp += 4;
887
                    sp += 4;
829
                    break;
888
                    break;
830
                }
889
                }
831
            case 3:
890
            case 3:
832
                fd = file_open(&scriptb[script[sp + 1]], O_RDONLY);
891
                fd = file_open((char*)(script[sp + 1]), O_RDONLY);
833
                if (fd >= 0)
892
                if (fd >= 0)
834
                {
893
                {
835
                    close(fd);
894
                    close(fd);
836
                    sp += 4;
895
                    sp += 4;
837
                    break;
896
                    break;
838
                }
897
                }
839
            case 4:
898
            case 4:
840
                if (script[sp + 2] < 0xfffffffe) data = &scriptb[script[sp + 2]];
899
                if (script[sp + 2] < 0xfffffffe) data = (void*)(script[sp + 2]);
841
                fd = file_creat(&scriptb[script[sp + 1]]);
900
                fd = file_creat((char*)(script[sp + 1]));
842
                if (fd >= 0)
901
                if (fd >= 0)
843
                {
902
                {
844
                    write(fd, data, script[sp + 3]);
903
                    write(fd, data, script[sp + 3]);
845
                    close(fd);
904
                    close(fd);
846
                }
905
                }
847
                sp += 4;
906
                sp += 4;
848
                break;
907
                break;
849
            default:
908
            default:
850
                panic(PANIC_KILLTHREAD, "Bad installation script!");
909
                panic(PANIC_KILLTHREAD, "Bad installation script!");
851
        }
910
        }
852
        status += script[sp++];
911
        cost += script[sp++];
853
        progressbar_setpos(&progressbar, status, false);
912
        progressbar_setpos(&progressbar, cost, false);
854
    }
913
    }
855
 
914
 
856
    bmpbuffer = malloc(0xb600);
-
 
857
    memcpy(lcdbuffer, backdrop, 0xb580);
-
 
858
    ucl_decompress(bitmapdata[BMPIDX_FLASHING], bitmapsize[BMPIDX_FLASHING], bmpbuffer, &dummy);
915
    ui->blenda(110, 20, 255, framebuf, 0, 0, 110, bg, 33, 55, 176, actions, 0, 40, 110);
859
    renderbmp(&lcdbuffer[176 * 36], bmpbuffer, 176);
-
 
860
    displaylcd(0, 175, 0, 131, lcdbuffer, 0);
916
    displaylcd(33, 55, 110, 20, framebuf, 0, 0, 110);
861
    progressbar_init(&progressbar, 15, 160, 50, 60, 0xce79, 0x18e3, 0x7bf9, 0, 256);
917
    progressbar_init(&progressbar, 10, 165, 74, 83, 0x77ff, 0xe8, 0x125f, 0, 256);
862
    for (i = 0; i < 256; i++)
918
    for (i = 0; i < 256; i++)
863
    {
919
    {
864
        bootflash_writeraw(&norbuf[i << 12], i << 12, 1 << 12);
920
        bootflash_writeraw(&norbuf[i << 12], i << 12, 1 << 12);
865
        progressbar_setpos(&progressbar, i, false);
921
        progressbar_setpos(&progressbar, i, false);
866
    }
922
    }
867
 
923
 
-
 
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
 
868
    shutdown(false);
934
    shutdown(false);
869
    reset();
935
    reset();
870
}
936
}