blob: 0cfd64a96e05ca6cf7ef83fb9e0e26961b720628 [file] [log] [blame]
Hristo Kovachev9dc0e622006-08-11 08:35:27 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Barry Wardell
11 *
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
14 *
15 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22#include "config.h"
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include "cpu.h"
28#include "system.h"
29#include "lcd.h"
30#include "kernel.h"
31#include "thread.h"
32#include "ata.h"
33#include "fat.h"
34#include "disk.h"
35#include "font.h"
36#include "adc.h"
37#include "backlight.h"
38#include "button.h"
39#include "panic.h"
40#include "power.h"
41#include "file.h"
42
43/* Size of the buffer to store the loaded Rockbox/iriver image */
44#define MAX_LOADSIZE (5*1024*1024)
45
Barry Wardell1920df32006-08-28 08:11:32 +000046/* A buffer to load the iriver firmware or Rockbox into */
47unsigned char loadbuffer[MAX_LOADSIZE];
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000048
Barry Wardell1920df32006-08-28 08:11:32 +000049char version[] = APPSVERSION;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000050
Barry Wardell1920df32006-08-28 08:11:32 +000051#define DRAM_START 0x10000000
52
53int line=0;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000054
Hristo Kovachev12041362006-08-11 09:51:04 +000055/* Load original iriver firmware. This function expects a file called
Barry Wardell1920df32006-08-28 08:11:32 +000056 "/System/Original.mi4" on the player. It should be decrypted
Hristo Kovachev12041362006-08-11 09:51:04 +000057 and have the header stripped using mi4code. It reads the file in to a memory
Barry Wardell1920df32006-08-28 08:11:32 +000058 buffer called buf. The rest of the loading is done in main() and crt0.S
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000059*/
60int load_iriver(unsigned char* buf)
61{
62 int fd;
63 int rc;
64 int len;
65
Barry Wardell1920df32006-08-28 08:11:32 +000066 fd = open("/System/Original.mi4", O_RDONLY);
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000067
68 len = filesize(fd);
Barry Wardell1920df32006-08-28 08:11:32 +000069
70 if (len > MAX_LOADSIZE)
71 return -6;
72
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000073 rc = read(fd, buf, len);
74 if(rc < len)
75 return -4;
76
77 close(fd);
78 return len;
79}
80
Barry Wardell1920df32006-08-28 08:11:32 +000081/* Load Rockbox firmware (rockbox.h10) */
82int load_rockbox(unsigned char* buf)
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000083{
Barry Wardell1920df32006-08-28 08:11:32 +000084 int fd;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000085 int rc;
Barry Wardell1920df32006-08-28 08:11:32 +000086 int len;
87 unsigned long chksum;
88 char model[5];
89 unsigned long sum;
90 int i;
91 char str[80];
Barry Wardell9272dfd2006-08-20 10:18:47 +000092
Barry Wardell1920df32006-08-28 08:11:32 +000093 fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
94 if(fd < 0)
95 {
96 fd = open("/" BOOTFILE, O_RDONLY);
97 if(fd < 0)
98 return -1;
99 }
100
101 len = filesize(fd) - 8;
102
103 snprintf(str, sizeof(str), "Length: %x", len);
104 lcd_puts(0, line++ ,str);
105 lcd_update();
106
107 if (len > MAX_LOADSIZE)
108 return -6;
109
110 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
111
112 rc = read(fd, &chksum, 4);
113 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
114 if(rc < 4)
115 return -2;
116
117 snprintf(str, sizeof(str), "Checksum: %x", chksum);
118 lcd_puts(0, line++ ,str);
Barry Wardell9272dfd2006-08-20 10:18:47 +0000119 lcd_update();
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000120
Barry Wardell1920df32006-08-28 08:11:32 +0000121 rc = read(fd, model, 4);
122 if(rc < 4)
123 return -3;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000124
Barry Wardell1920df32006-08-28 08:11:32 +0000125 model[4] = 0;
Hristo Kovachev12041362006-08-11 09:51:04 +0000126
Barry Wardell1920df32006-08-28 08:11:32 +0000127 snprintf(str, sizeof(str), "Model name: %s", model);
128 lcd_puts(0, line++ ,str);
129 lcd_update();
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000130
Barry Wardell1920df32006-08-28 08:11:32 +0000131 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
132
133 rc = read(fd, buf, len);
134 if(rc < len)
135 return -4;
136
137 close(fd);
138
139 sum = MODEL_NUMBER;
140
141 for(i = 0;i < len;i++) {
142 sum += buf[i];
Hristo Kovachev12041362006-08-11 09:51:04 +0000143 }
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000144
Barry Wardell1920df32006-08-28 08:11:32 +0000145 snprintf(str, sizeof(str), "Sum: %x", sum);
146 lcd_puts(0, line++ ,str);
147 lcd_update();
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000148
Barry Wardell1920df32006-08-28 08:11:32 +0000149 if(sum != chksum)
150 return -5;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000151
Barry Wardell1920df32006-08-28 08:11:32 +0000152 return len;
153}
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000154
Barry Wardell1920df32006-08-28 08:11:32 +0000155void* main(void)
156{
157 char buf[256];
158 int i;
159 int rc;
160 unsigned short* identify_info;
161 struct partinfo* pinfo;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000162
Barry Wardell1920df32006-08-28 08:11:32 +0000163 system_init();
164 kernel_init();
165 lcd_init();
166 font_init();
167
168 line=0;
169
170 lcd_setfont(FONT_SYSFIXED);
171
172 lcd_puts(0, line++, "Rockbox boot loader");
173 snprintf(buf, sizeof(buf), "Version: 20%s", version);
174 lcd_puts(0, line++, buf);
175 snprintf(buf, sizeof(buf), "iriver H10");
176 lcd_puts(0, line++, buf);
177 lcd_update();
178
179 i=ata_init();
180 if (i==0) {
181 identify_info=ata_get_identify();
182 /* Show model */
183 for (i=0; i < 20; i++) {
184 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
185 }
186 buf[40]=0;
187 for (i=39; i && buf[i]==' '; i--) {
188 buf[i]=0;
189 }
190 lcd_puts(0, line++, buf);
191 lcd_update();
192 } else {
193 snprintf(buf, sizeof(buf), "ATA: %d", i);
194 lcd_puts(0, line++, buf);
195 lcd_update();
196 }
197
198 disk_init();
199 rc = disk_mount_all();
200 if (rc<=0)
201 {
202 lcd_puts(0, line++, "No partition found");
203 lcd_update();
204 }
205
206 pinfo = disk_partinfo(0);
207 snprintf(buf, sizeof(buf), "Partition 0: 0x%02x %ld MB",
208 pinfo->type, pinfo->size / 2048);
209 lcd_puts(0, line++, buf);
210 lcd_update();
211
212 i=button_read_device();
213 if(i==BUTTON_LEFT)
214 {
215 lcd_puts(0, line, "Loading iriver firmware...");
216 lcd_update();
217 rc=load_iriver(loadbuffer);
218 } else {
219 lcd_puts(0, line, "Loading Rockbox...");
220 lcd_update();
221 rc=load_rockbox(loadbuffer);
222 }
223
224 if (rc < 0) {
225 snprintf(buf, sizeof(buf), "Rockbox error: %d",rc);
226 lcd_puts(0, line++, buf);
227 lcd_update();
228 }
Hristo Kovachev12041362006-08-11 09:51:04 +0000229
Barry Wardell1920df32006-08-28 08:11:32 +0000230 memcpy((void*)DRAM_START,loadbuffer,rc);
Hristo Kovachev12041362006-08-11 09:51:04 +0000231
Barry Wardell1920df32006-08-28 08:11:32 +0000232 return (void*)DRAM_START;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000233}
234
235/* These functions are present in the firmware library, but we reimplement
236 them here because the originals do a lot more than we want */
237
238void reset_poweroff_timer(void)
239{
240}
241
242int dbg_ports(void)
243{
244 return 0;
245}
246
247void mpeg_stop(void)
248{
249}
250
251void usb_acknowledge(void)
252{
253}
254
255void usb_wait_for_disconnect(void)
256{
257}
258
259void sys_poweroff(void)
260{
261}