Oops, source in deutsch.lang should match the english source.
[kugel-rb/myfork.git] / bootloader / ondavx747.c
blob496363c4b1be442cafa652c1873f21ad27be0fe8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "jz4740.h"
24 #include "backlight.h"
25 #include "font.h"
26 #include "lcd.h"
27 #include "file.h"
28 #include "usb.h"
29 #include "system.h"
30 #include "button.h"
31 #include "common.h"
32 #include "storage.h"
33 #include "disk.h"
34 #include "string.h"
35 #include "adc.h"
37 extern int show_logo(void);
38 extern void power_off(void);
40 static void show_splash(int timeout, const char *msg)
42 reset_screen();
43 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
44 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
45 lcd_update();
47 sleep(timeout);
50 static void usb_mode(void)
52 int button;
54 /* Init USB */
55 usb_init();
56 usb_start_monitoring();
58 /* Wait for threads to connect */
59 show_splash(HZ/2, "Waiting for USB");
61 while (1)
63 button = button_get_w_tmo(HZ/2);
65 if (button == SYS_USB_CONNECTED)
66 break; /* Hit */
69 if (button == SYS_USB_CONNECTED)
71 /* Got the message - wait for disconnect */
72 show_splash(0, "Bootloader USB mode");
74 usb_acknowledge(SYS_USB_CONNECTED_ACK);
76 while (1)
78 button = button_get(true);
79 if (button == SYS_USB_DISCONNECTED)
81 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
82 break;
87 reset_screen();
90 static int boot_of(void)
92 int fd, rc, len, i, checksum = 0;
93 void (*kernel_entry)(int, void*, void*);
95 /* TODO: get this from the NAND flash instead of SD */
96 fd = open("/ccpmp.bin", O_RDONLY);
97 if(fd < 0)
98 return EFILE_NOT_FOUND;
100 lseek(fd, 4, SEEK_SET);
101 rc = read(fd, (char*)&len, 4); /* CPU is LE */
102 if(rc < 4)
103 return EREAD_IMAGE_FAILED;
105 len += 8;
106 printf("Reading %d bytes...", len);
108 lseek(fd, 0, SEEK_SET);
109 rc = read(fd, (void*)0x80004000, len);
110 if(rc < len)
111 return EREAD_IMAGE_FAILED;
113 close(fd);
115 for(i=0; i<len; i++)
116 checksum += ((unsigned char*)0x80004000)[i];
118 *((unsigned int*)0x80004000) = checksum;
120 printf("Starting the OF...");
122 /* OF requires all clocks on */
123 __cpm_start_all();
125 disable_interrupt();
126 __dcache_writeback_all();
127 __icache_invalidate_all();
129 for(i=8000; i>0; i--)
130 asm volatile("nop\n");
132 kernel_entry = (void*) 0x80004008;
133 kernel_entry(0, "Jan 10 2008", "15:34:42"); /* Reversed from the SPL */
135 return 0; /* Shouldn't happen */
138 static int boot_rockbox(void)
140 int rc;
141 void (*kernel_entry)(void);
143 printf("Loading firmware");
144 rc = load_firmware((unsigned char *)CONFIG_SDRAM_START, BOOTFILE, 0x400000);
145 if(rc < 0)
146 return rc;
147 else
149 printf("Starting Rockbox...");
150 adc_close(); /* Disable SADC, seems to fix the re-init Rockbox does */
152 disable_interrupt();
153 kernel_entry = (void*) CONFIG_SDRAM_START;
154 kernel_entry();
156 return 0; /* Shouldn't happen */
160 #define RECT_X (LCD_WIDTH/8)
161 #define RECT_Y(i) (LCD_HEIGHT/20 + LCD_HEIGHT/10*i + RECT_HEIGHT*i)
162 #define RECT_WIDTH (LCD_WIDTH*3/4)
163 #define RECT_HEIGHT (LCD_HEIGHT/ARRAYLEN(strings) - LCD_HEIGHT/10)
164 #define TEXT_X(i) (RECT_X + RECT_WIDTH/2 - strlen(strings[i])*SYSFONT_WIDTH/2)
165 #define TEXT_Y(i) (RECT_Y(i) + RECT_HEIGHT/2 - SYSFONT_HEIGHT/2)
166 static int boot_menu(void)
168 const char* strings[] = {"Boot Rockbox", "Boot OF", "USB mode"};
169 int button, touch;
170 unsigned int i;
172 adc_init();
174 redraw:
175 lcd_clear_display();
176 for(i=0; i<ARRAYLEN(strings); i++)
178 lcd_drawrect(RECT_X, RECT_Y(i), RECT_WIDTH, RECT_HEIGHT);
179 lcd_putsxy(TEXT_X(i), TEXT_Y(i), strings[i]);
181 lcd_update();
183 while(1)
185 button = button_get_w_tmo(HZ/4);
186 if(button & (BUTTON_TOUCHSCREEN|BUTTON_REPEAT))
188 touch = button_get_data();
189 unsigned int x = touch & 0xFFFF, y = touch >> 16;
190 int found = -1;
191 for(i=0; i<ARRAYLEN(strings); i++)
193 if(x > RECT_X && x < RECT_X+RECT_WIDTH &&
194 y > RECT_Y(i) && y < RECT_Y(i)+RECT_HEIGHT)
196 found = i;
197 break;
201 switch(found)
203 case 0:
204 return boot_rockbox();
205 case 1:
206 return boot_of();
207 case 2:
208 usb_mode();
209 break;
212 if(found != -1)
213 goto redraw;
215 else if(button & (BUTTON_POWER|BUTTON_REPEAT))
216 power_off();
220 int main(void)
222 int rc;
223 #ifdef HAVE_TOUCHSCREEN
224 int dummy;
225 #endif
227 kernel_init();
228 lcd_init();
229 font_init();
230 lcd_setfont(FONT_SYSFIXED);
231 button_init();
232 backlight_init();
234 show_logo();
236 rc = storage_init();
237 if(rc)
238 error(EATA, rc);
240 #ifdef HAVE_TOUCHSCREEN
241 rc = button_read_device(&dummy);
242 #else
243 rc = button_read_device();
244 #endif
246 if(rc)
247 verbose = true;
249 #ifdef BUTTON_VOL_UP
250 if(rc & BUTTON_VOL_UP ||
251 #endif
253 rc = boot_menu();
255 if(verbose)
256 reset_screen();
257 printf(MODEL_NAME" Rockbox Bootloader");
258 printf("Version "APPSVERSION);
260 rc = disk_mount_all();
261 if (rc <= 0)
262 error(EDISK,rc);
264 #ifdef HAS_BUTTON_HOLD
265 if(button_hold())
266 rc = boot_of();
267 else
268 #endif
269 rc = boot_rockbox();
271 if(rc < 0)
272 printf("Error: %s", strerror(rc));
274 /* Halt */
275 while (1)
276 core_idle();
278 return 0;