summaryrefslogtreecommitdiffstats
path: root/bootloader/gigabeat-s.c
blob: e884e062198f4c834206e9166497dfeb135b70c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Greg White
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include "inttypes.h"
#include "string.h"
#include "cpu.h"
#include "system.h"
#include "lcd.h"
#include "kernel.h"
#include "thread.h"
#include "ata.h"
#include "dir.h"
#include "fat.h"
#include "disk.h"
#include "font.h"
#include "adc.h"
#include "backlight.h"
#include "backlight-target.h"
#include "button.h"
#include "panic.h"
#include "power.h"
#include "file.h"
#include "common.h"
#include "rbunicode.h"
#include "usb.h"
#include "mmu-imx31.h"
#include "lcd-target.h"
#include "avic-imx31.h"
#include <stdarg.h>

#define TAR_CHUNK 512
#define TAR_HEADER_SIZE 157

char version[] = APPSVERSION;
char basedir[] = "/Content/0b00/00/"; /* Where files sent via MTP are stored */
int (*kernel_entry)(void);
extern void reference_system_c(void);

/* Dummy stub that creates C references for C functions only used by
   assembly - never called */
void reference_files(void)
{
    reference_system_c();
}

void untar(int tar_fd)
{
    char header[TAR_HEADER_SIZE];
    char copybuf[TAR_CHUNK];
    char path[102];
    int fd, i, size = 0, pos = 0;

    while (1)
    {
        read(tar_fd, header, TAR_HEADER_SIZE);

        if (*header == '\0')  /* Check for EOF */
            break;

        /* Parse the size field */
        size = 0;
        for (i = 124 ; i < 124 + 11 ; i++) {
            size = (8 * size) + header[i] - '0';
        }

        /* Skip rest of header */
        pos = lseek(tar_fd, TAR_CHUNK - TAR_HEADER_SIZE, SEEK_CUR);

        /* Make the path absolute */
        strcpy(path, "/");
        strcat(path, header);

        if (header[156] == '0')  /* file */
        {
            int rc, wc, total = 0;

            fd = creat(path);
            if (fd < 0)
            {
                printf("failed to create file (%d)", fd);
                /* Skip the file */
                lseek(tar_fd, (size + 511) & (~511), SEEK_CUR);
            }
            else
            {
                /* Copy the file over 512 bytes at a time */
                while (total < size)
                {
                    rc = read(tar_fd, copybuf, TAR_CHUNK);
                    pos += rc;

                    wc = write(fd, copybuf, MIN(rc, size - total));
                    if (wc < 0)
                    {
                        printf("write failed (%d)", wc);
                        break;
                    }
                    total += wc;
                }
                close(fd);
            }
        }
        else if (header[156] == '5')  /* directory */
        {
            int ret;

            /* Remove the trailing slash */
            if (path[strlen(path) - 1] == '/')
                path[strlen(path) - 1] = '\0';

            /* Create the dir */
            ret = mkdir(path);
            if (ret < 0 && ret != -4)
            {
                printf("failed to create dir (%d)", ret);
            }
        }
    }
}

void main(void)
{
    char buf[MAX_PATH];
    char tarstring[6];
    char model[5];

    lcd_clear_display();
    printf("Hello world!");
    printf("Gigabeat S Rockbox Bootloader v.00000005");
    system_init();
    kernel_init();
    printf("kernel init done");
    int rc;

    enable_interrupt(IRQ_FIQ_STATUS);

    rc = ata_init();
    if(rc)
    {
        reset_screen();
        error(EATA, rc);
    }
    printf("ata init done");

    disk_init();
    printf("disk init done");

    rc = disk_mount_all();
    if (rc<=0)
    {
        error(EDISK,rc);
    }

    /* Look for a tar file */
    struct dirent_uncached* entry;
    DIR_UNCACHED* dir;
    int fd;
    dir = opendir_uncached(basedir);
    while ((entry = readdir_uncached(dir)))
    {
        if (*entry->d_name != '.')
        {
            snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
            fd = open(buf, O_RDONLY);
            if (fd >= 0)
            {
                /* Check whether the file is a rockbox binary. */
                lseek(fd, 4, SEEK_SET);
                rc = read(fd, model, 4);
                if (rc == 4)
                {
                    model[4] = 0;
                    if (strcmp(model, "gigs") == 0)
                    {
                        printf("Found rockbox binary. Moving...");
                        close(fd);
                        remove("/.rockbox/rockbox.gigabeat");
                        int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
                        printf("returned %d", ret);
                        sleep(100);
                        break;
                    }
                }

                /* Check whether the file is a tar file. */
                lseek(fd, 257, SEEK_SET);
                rc = read(fd, tarstring, 5);
                if (rc == 5)
                {
                    tarstring[5] = 0;
                    if (strcmp(tarstring, "ustar") == 0)
                    {
                        printf("Found tar file. Unarchiving...");
                        lseek(fd, 0, SEEK_SET);
                        untar(fd);
                        close(fd);
                        printf("Removing tar file");
                        remove(buf);
                        break;
                    }
                }
                close(fd);
            }
        }
    }

    unsigned char *loadbuffer = (unsigned char *)0x0;
    int buffer_size = 31*1024*1024;

    rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size);
    if(rc < 0)
        error((int)buf, rc);

    system_prepare_fw_start();

    if (rc == EOK)
    {
        kernel_entry = (void*) loadbuffer;
        invalidate_icache();
        rc = kernel_entry();
    }

    while (1);
}