summaryrefslogtreecommitdiffstats
path: root/bootloader/h10.c
blob: d7942c958314ba09a7a78aa395a9d1201d9c2371 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Barry Wardell
 *
 * 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 <string.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"

/* Size of the buffer to store the loaded Rockbox/iriver image */
#define MAX_LOADSIZE (5*1024*1024)

/* This is identical to the function used in the iPod bootloader */
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;
    }
}


/* Load original iriver firmware. This function expects a file called
   "H10_20GC.mi4" in the root directory of the player. It should be decrypted
   and have the header stripped using mi4code. It reads the file in to a memory
   buffer called buf. The rest of the loading is done in main()
*/
int load_iriver(unsigned char* buf)
{
    int fd;
    int rc;
    int len;
    
    fd = open("/H10_20GC.mi4", O_RDONLY);

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

    close(fd);
    return len;
}

/* A buffer to load the iriver firmware or Rockbox into */
unsigned char loadbuffer[MAX_LOADSIZE];

void main(void)
{
    /* Attempt to load original iriver firmware. Successfully starts loading the
       iriver firmware but then locks up once the "System Initialising" screen
       is displayed.
       
       The iriver firmware was decrypted and the header removed. It was then
       appended to the end of bootloader.bin and an mi4 file was created from
       the resulting file. 
       
       The original firmware starts at 0xd800 in the file and is of length
       0x47da00.
       
       The whole file (bootloader.bin + decrypted mi4) are loaded to memory by
       the original iriver bootloader on startup. This copies the mi4 part to
       the start of DRAM and passes execution to there.
    
    memmove16((void*)DRAMORIG, (void*)(DRAMORIG + 0xd800), 0x47da00);
    asm volatile(
        "mov   r0, #" SC(DRAMORIG) "\n"
        "mov   pc, r0                 \n"
    );
    */

    int i;
    int rc;
    int btn;
    int fd;
    char buffer[24];

    i=ata_init();
    disk_init();
    rc = disk_mount_all();

    /* Load original iriver firmware. Uses load_iriver(buf) to load the
       decrypted mi4 file from disk to DRAM. This then copies that part of DRAM
       to the start of DRAM and passes 
       execution to there.
    
    rc=load_iriver(loadbuffer);
    memcpy((void*)DRAMORIG,loadbuffer,rc);
    asm volatile(
        "mov   r0, #" SC(DRAMORIG) "\n"
        "mov   pc, r0                 \n"
    );*/
    
    
    /* This assumes that /test.txt exists */
    fd=open("/test.txt",O_RDWR);
    
    
    /*
    for(i=0;i<100;i++){
        btn = button_read_device();
        switch(btn){
        case BUTTON_LEFT:
            snprintf(buffer, sizeof(buffer), "Left");
            write(fd,buffer,sizeof(buffer));
            break;
        case BUTTON_RIGHT:
        break;
        case BUTTON_REW:
        break;
        case BUTTON_FF:
        break;
        case BUTTON_PLAY:
        break;
        default:
        break;
        }
        

    }
    */
    
    
    
    /* Investigate gpio
    
    unsigned int gpio_a, gpio_b, gpio_c, gpio_d;
    unsigned int gpio_e, gpio_f, gpio_g, gpio_h;
    unsigned int gpio_i, gpio_j, gpio_k, gpio_l;

    gpio_a = GPIOA_INPUT_VAL;
    gpio_b = GPIOB_INPUT_VAL;
    gpio_c = GPIOC_INPUT_VAL;

    gpio_g = GPIOG_INPUT_VAL;
    gpio_h = GPIOH_INPUT_VAL;
    gpio_i = GPIOI_INPUT_VAL;

    snprintf(buffer,sizeof(buffer),"GPIO_A: %02x GPIO_G: %02x\n",gpio_a,gpio_g);
    write(fd,buffer,sizeof(buffer));
    snprintf(buffer,sizeof(buffer),"GPIO_B: %02x GPIO_H: %02x\n",gpio_b,gpio_h);
    write(fd,buffer,sizeof(buffer));
    snprintf(buffer,sizeof(buffer),"GPIO_C: %02x GPIO_I: %02x\n",gpio_c,gpio_i);
    write(fd,buffer,sizeof(buffer));
    
    gpio_d = GPIOD_INPUT_VAL;
    gpio_e = GPIOE_INPUT_VAL;
    gpio_f = GPIOF_INPUT_VAL;

    gpio_j = GPIOJ_INPUT_VAL;
    gpio_k = GPIOK_INPUT_VAL;
    gpio_l = GPIOL_INPUT_VAL;

    snprintf(buffer,sizeof(buffer),"GPIO_D: %02x GPIO_J: %02x\n",gpio_d,gpio_j);
    write(fd,buffer,sizeof(buffer));
    snprintf(buffer,sizeof(buffer),"GPIO_E: %02x GPIO_K: %02x\n",gpio_e,gpio_k);
    write(fd,buffer,sizeof(buffer));
    snprintf(buffer,sizeof(buffer),"GPIO_F: %02x GPIO_L: %02x\n",gpio_f,gpio_l);
    write(fd,buffer,sizeof(buffer));
    */
    
    
    
    /* Detect the scroller being touched
    
    int j = 0;
    for(j=0;j<1000;j++){
        if(gpio_c!=0xF7){
            snprintf(buffer, sizeof(buffer), "GPIO_C: %02x\n", gpio_c);
            write(fd,buffer,sizeof(buffer));
        }
        if(gpio_d!=0xDD){
            snprintf(buffer, sizeof(buffer), "GPIO_D: %02x\n", gpio_d);
            write(fd,buffer,sizeof(buffer));
        }
    }*/
    
    
    /* Apparently necessary for the data to be actually written to file */
    fsync(fd);
    udelay(1000000);
    
    /* This causes the device to shut off instantly
    
    GPIOF_OUTPUT_VAL = 0;
    */
    
    close(fd);
    udelay(1000000);
}

/* 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)
{
}

int dbg_ports(void)
{
   return 0;
}

void mpeg_stop(void)
{
}

void usb_acknowledge(void)
{
}

void usb_wait_for_disconnect(void)
{
}

void sys_poweroff(void)
{
}