summaryrefslogtreecommitdiffstats
path: root/bootloader/ipod.c
blob: 840b95be9ea41a159722a69fe0a2cb5848ff8a9f (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Dave Chapman
 *
 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
 * 
 * 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 "cpu.h"
#include "system.h"
#include "lcd.h"
#include "kernel.h"
#include "thread.h"
#include "ata.h"
#include "fat.h"
#include "disk.h"
#include "font.h"
#include "adc.h"
#include "backlight.h"
#include "button.h"
#include "panic.h"
#include "power.h"
#include "file.h"

#define DRAM_START             0x10000000
#define IPOD_PP5020_RTC        0x60005010

#define IPOD_HW_REVISION (*((volatile unsigned long*)(0x00002084)))

char version[] = APPSVERSION;

#include "rockbox-16bit.h"
#include "ipodlinux-16bit.h"

typedef struct _image {
    unsigned type;              /* '' */
    unsigned id;                /* */
    unsigned pad1;              /* 0000 0000 */
    unsigned devOffset;         /* byte offset of start of image code */
    unsigned len;               /* length in bytes of image */
    void    *addr;              /* load address */
    unsigned entryOffset;       /* execution start within image */
    unsigned chksum;            /* checksum for image */
    unsigned vers;              /* image version */
    unsigned loadAddr;          /* load address for image */
} image_t;

extern image_t boot_table[];

int line=0;

static void memmove16(void *dest, const void *src, unsigned count)
{
    struct bufstr {
        unsigned _buf[4];
    } *d, *s;

    if (src >= dest) {
        count = (count + 15) >> 4;
        d = (struct bufstr *) dest;
        s = (struct bufstr *) src;
        while (count--)
            *d++ = *s++;
    } else {
        count = (count + 15) >> 4;
        d = (struct bufstr *)(dest + (count <<4));
        s = (struct bufstr *)(src + (count <<4));
        while (count--)
            *--d = *--s;
    }
}

/* get current usec counter */
int timer_get_current(void)
{
    return inl(IPOD_PP5020_RTC);
}

/* check if number of seconds has past */
int timer_check(int clock_start, unsigned int usecs)
{
    if ((inl(IPOD_PP5020_RTC) - clock_start) >= usecs) {
        return 1;
    } else {
        return 0;
    }
}

/* This isn't a sleep, but let's call it that. */
int usleep(unsigned int usecs)
{
    unsigned int start = inl(IPOD_PP5020_RTC);

    while ((inl(IPOD_PP5020_RTC) - start) < usecs) {
       // empty
    }

    return 0;
}

int load_firmware(void)
{
    int fd;
    int rc;
    int len;
    unsigned long chksum;
    char model[5];
    unsigned long sum;
    int i;
    unsigned char *buf = (unsigned char *)DRAM_START;
    char str[80];
    
    fd = open("/rockbox.ipod", O_RDONLY);
    if(fd < 0)
    return -1;

    len = filesize(fd) - 8;

    snprintf(str, 80, "Length: %x", len);
    lcd_puts(0, line++, str);
    lcd_update();

    lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
    
    rc = read(fd, &chksum, 4);
    if(rc < 4)
    return -2;

    snprintf(str, 80, "Checksum: %x", chksum);
    lcd_puts(0, line++, str);
    lcd_update();

    rc = read(fd, model, 4);
    if(rc < 4)
    return -3;

    model[4] = 0;
    
    snprintf(str, 80, "Model name: %s", model);
    lcd_puts(0, line++, str);
    lcd_update();

    lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);

    rc = read(fd, buf, len);
    if(rc < len)
    return -4;

    close(fd);

    sum = MODEL_NUMBER;
    
    for(i = 0;i < len;i++) {
    sum += buf[i];
    }

    snprintf(str, 80, "Sum: %x", sum);
    lcd_puts(0, line++, str);
    lcd_update();

    if(sum != chksum)
    return -5;

    return 0;
}

void* main(void)
{
    char buf[256];
    int imageno=0;
    int i;
    int rc;
    int padding = 0x4400;
    image_t *tblp = boot_table;
    void* entry;
    struct partinfo* pinfo;
    unsigned short* identify_info;

    /* Turn on the backlight */

#if CONFIG_BACKLIGHT==BL_IPOD4G

    /* brightness full */
    outl(0x80000000 | (0xff << 16), 0x7000a010);

    /* set port B03 on */
    outl(((0x100 | 1) << 3), 0x6000d824);

#elif CONFIG_BACKLIGHT==BL_IPODNANO

    /* set port B03 on */
    outl(((0x100 | 1) << 3), 0x6000d824);

    /* set port L07 on */
    outl(((0x100 | 1) << 7), 0x6000d12c);

#endif

    system_init();
    kernel_init();
    lcd_init();
    font_init();

#if 0
    /* ADC and button drivers are not yet implemented */
    adc_init();
    button_init();
#endif

    /* Notes: iPod Color/Photo LCD is 220x176, Nano is 176x138 */

    /* Display the 42x47 pixel iPodLinux logo */
    lcd_bitmap((unsigned char*)ipllogo, 20,6, 42,47);

    /* Display the 100x31 pixel Rockbox logo */
    lcd_bitmap((unsigned char*)rockboxlogo, 74,16, 100,31);

    line=7;

    lcd_setfont(FONT_SYSFIXED);

    lcd_puts(0, line++, "iPodLinux/Rockbox boot loader");
    snprintf(buf, sizeof(buf), "Version: 20%s", version);
    lcd_puts(0, line++, buf);
    snprintf(buf, sizeof(buf), "IPOD version: 0x%08x", IPOD_HW_REVISION);
    lcd_puts(0, line++, buf);
    lcd_update();

    i=ata_init();
    if (i==0) {
      identify_info=ata_get_identify();
      /* Show model */
      for (i=0; i < 20; i++) {
        ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
      }
      buf[40]=0;
      for (i=39; i && buf[i]==' '; i--) {
        buf[i]=0;
      }
      lcd_puts(0, line++, buf);
      lcd_update();
    } else {
      snprintf(buf, sizeof(buf), "ATA: %d", i);
      lcd_puts(0, line++, buf);
      lcd_update();
    }

    disk_init();
    rc = disk_mount_all();
    if (rc<=0)
    {
        lcd_puts(0, line++, "No partition found");
        lcd_update();
//        while(button_get(true) != SYS_USB_CONNECTED) {};
    }

    pinfo = disk_partinfo(1);
    snprintf(buf, sizeof(buf), "Partition 1: 0x%02x %ld MB", 
                  pinfo->type, pinfo->size / 2048);
    lcd_puts(0, line++, buf);
    lcd_update();

#if 0
    /* The following code will load and run an ipodlinux kernel - we will
       enable it once the button driver is written and we can detect key
       presses */
    int fd=open("/linux.bin",O_RDONLY);
    if (fd >= 0) {
      i=filesize(fd);
      int n=read(fd,(void*)DRAM_START,i);
      if (n==i) {
          /* We return the entry point for the loaded kernel */
          return DRAM_START;
      } else {
          /* What do we do now?  We may have overwritten the copy of the 
             original firmware with our incomplete copy of the Linux 
             kernel... */
      }
    }
#endif

    /* Pause for 5 seconds so we can see what's happened*/
    usleep(5000000);

    /* If everything else failed, try the original firmware */
    lcd_puts(0, line, "Loading original firmware...");
    lcd_update();

    entry = tblp->addr + tblp->entryOffset;
    if (imageno || ((int)tblp->addr & 0xffffff) != 0) {
        memmove16(tblp->addr, tblp->addr + tblp->devOffset - padding,
                  tblp->len);
    }

    /* Return the start address in loaded image */
    return entry;
}

/* These functions are present in the firmware library, but we reimplement
   them here because the originals do a lot more than we want */

void reset_poweroff_timer(void)
{
}

void screen_dump(void)
{
}

int dbg_ports(void)
{
   return 0;
}

void mpeg_stop(void)
{
}

void usb_acknowledge(void)
{
}

void usb_wait_for_disconnect(void)
{
}

void sys_poweroff(void)
{
}