blob: 8ca44e8c47b6db62c7325d0782e27c0f7e14fbc6 [file] [log] [blame]
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
21#include <stdlib.h>
22#include <stdio.h>
23#include "cpu.h"
24#include "system.h"
25#include "lcd.h"
26#include "kernel.h"
27#include "thread.h"
28#include "ata.h"
29#include "disk.h"
30#include "font.h"
31#include "adc.h"
32#include "backlight.h"
33#include "button.h"
34#include "panic.h"
35#include "power.h"
36#include "file.h"
37
38int line = 0;
39
40int usb_screen(void)
41{
42 return 0;
43}
44
45void start_iriver_fw(void)
46{
47 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000048 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000049 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000050 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000051 asm(" movec.l %d0,%vbr");
52 asm(" move.l 0,%sp");
53 asm(" lea.l 8,%a0");
54 asm(" jmp (%a0)");
55}
56
57int load_firmware(void)
58{
59 int fd;
60 int rc;
61 int len;
62 unsigned long chksum;
63 unsigned long sum;
64 int i;
65 unsigned char *buf = (unsigned char *)0x30000000;
66 char str[80];
67
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +000068 fd = open("/rockbox.iriver", O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000069 if(fd < 0)
70 return -1;
71
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +000072 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000073
74 snprintf(str, 80, "Length: %x", len);
75 lcd_puts(0, line++, str);
76 lcd_update();
77
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +000078 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000079
80 rc = read(fd, &chksum, 4);
81 if(rc < 4)
82 return -2;
83
84 snprintf(str, 80, "Checksum: %x", chksum);
85 lcd_puts(0, line++, str);
86 lcd_update();
87
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +000088 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000089
90 rc = read(fd, buf, len);
91 if(rc < len)
92 return -4;
93
94 close(fd);
95
96 sum = 0;
97
98 for(i = 0;i < len;i++) {
99 sum += buf[i];
100 }
101
102 snprintf(str, 80, "Sum: %x", sum);
103 lcd_puts(0, line++, str);
104 lcd_update();
105
106 if(sum != chksum)
107 return -5;
108
109 return 0;
110}
111
112void start_firmware(void)
113{
114 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000115 /* Reset the cookie for the crt0 crash check */
116 asm(" move.l #0,%d0");
117 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000118 asm(" move.l #0x30000000,%d0");
119 asm(" movec.l %d0,%vbr");
120 asm(" move.l 0x30000000,%sp");
121 asm(" move.l 0x30000004,%a0");
122 asm(" jmp (%a0)");
123}
124
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000125void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000126{
127 int i;
128 int rc;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000129 char buf[256];
130
131 power_init();
132 system_init();
133 kernel_init();
134 backlight_init();
135 set_irq_level(0);
136 lcd_init();
137 font_init();
138 adc_init();
139 button_init();
140
141 lcd_setfont(FONT_SYSFIXED);
142
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000143 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000144
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000145 if(button_status() & BUTTON_REC ||
146 button_status() & BUTTON_RC_ON) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000147 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000148 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000149 start_iriver_fw();
150 }
151
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000152 if((button_status() & BUTTON_ON) & button_hold()) {
153 lcd_puts(0, 8, "HOLD switch on, power off...");
154 lcd_update();
155 sleep(HZ/2);
156 power_off();
157 }
158#if 0
159 if((button_status() & BUTTON_RC_ON) & remote_button_hold()) {
160 lcd_puts(0, 8, "HOLD switch on, power off...");
161 lcd_update();
162 sleep(HZ/2);
163 power_off();
164 }
165#endif
166
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000167 rc = ata_init();
168 if(rc)
169 {
170#ifdef HAVE_LCD_BITMAP
171 char str[32];
172 lcd_clear_display();
173 snprintf(str, 31, "ATA error: %d", rc);
174 lcd_puts(0, 1, str);
175 lcd_puts(0, 3, "Press ON to debug");
176 lcd_update();
177 while(!(button_get(true) & BUTTON_REL));
178#endif
179 panicf("ata: %d", rc);
180 }
181
182 disk_init();
183
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000184 rc = disk_mount_all();
185 if (rc<=0)
186 {
187 lcd_clear_display();
188 lcd_puts(0, 0, "No partition");
189 lcd_puts(0, 1, "found.");
190 while(button_get(true) != SYS_USB_CONNECTED) {};
191 }
192
193 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000194 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000195 i = load_firmware();
196 snprintf(buf, 256, "Result: %d", i);
197 lcd_puts(0, line++, buf);
198 lcd_update();
199
200 if(i == 0)
201 start_firmware();
202
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000203 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000204}
205
206/* These functions are present in the firmware library, but we reimplement
207 them here because the originals do a lot more than we want */
208
209void reset_poweroff_timer(void)
210{
211}
212
213void screen_dump(void)
214{
215}
216
217int dbg_ports(void)
218{
219 return 0;
220}
221
222void mpeg_stop(void)
223{
224}
225
226void usb_acknowledge(void)
227{
228}
229
230void usb_wait_for_disconnect(void)
231{
232}