1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Dave Chapman
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
40 #include "backlight.h"
50 /* Maximum allowed firmware image size. The largest known current
51 (December 2006) firmware is about 7.5MB (Apple's firmware for the ipod video)
52 so we set this to 8MB. */
53 #define MAX_LOADSIZE (8*1024*1024)
55 /* A buffer to load the Linux kernel or Rockbox into */
56 unsigned char *loadbuffer
= (unsigned char *)DRAM_START
;
58 /* Bootloader version */
59 char version
[] = APPSVERSION
;
63 #define BUTTON_RIGHT 3
67 #if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_MINI)
68 /* check if number of seconds has past */
69 int timer_check(int clock_start
, unsigned int usecs
)
71 if ((USEC_TIMER
- clock_start
) >= usecs
) {
78 static void ser_opto_keypad_cfg(int val
)
82 GPIOB_ENABLE
&=~ 0x80;
84 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
85 outl(val
, 0x7000c120);
86 outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
88 GPIOB_OUTPUT_VAL
&=~ 0x10;
89 GPIOB_OUTPUT_EN
|= 0x10;
91 start_time
= USEC_TIMER
;
93 if ((inl(0x7000c104) & 0x80000000) == 0) {
96 } while (timer_check(start_time
, 1500) != 0);
98 outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
100 GPIOB_ENABLE
|= 0x80;
101 GPIOB_OUTPUT_VAL
|= 0x10;
102 GPIOB_OUTPUT_EN
&=~0x10;
104 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
105 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
108 int opto_keypad_read(void)
110 int loop_cnt
, had_io
= 0;
112 for (loop_cnt
= 5; loop_cnt
!= 0;)
116 unsigned int key_pad_val
;
118 ser_opto_keypad_cfg(0x8000023a);
120 start_time
= USEC_TIMER
;
122 if (inl(0x7000c104) & 0x4000000) {
130 } while (timer_check(start_time
, 1500) != 0);
132 key_pad_val
= inl(0x7000c140);
133 if ((key_pad_val
& ~0x7fff0000) != 0x8000023a) {
136 key_pad_val
= (key_pad_val
<< 11) >> 27;
140 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
141 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
143 if (key_pressed
!= 0) {
144 return key_pad_val
^ 0x1f;
152 static int key_pressed(void)
156 #if CONFIG_KEYPAD == IPOD_4G_PAD
157 #ifdef IPOD_MINI /* mini 1G only */
158 state
= GPIOA_INPUT_VAL
& 0x3f;
159 if ((state
& 0x10) == 0) return BUTTON_LEFT
;
160 if ((state
& 0x2) == 0) return BUTTON_MENU
;
161 if ((state
& 0x4) == 0) return BUTTON_PLAY
;
162 if ((state
& 0x8) == 0) return BUTTON_RIGHT
;
164 state
= opto_keypad_read();
165 if ((state
& 0x4) == 0) return BUTTON_LEFT
;
166 if ((state
& 0x10) == 0) return BUTTON_MENU
;
167 if ((state
& 0x8) == 0) return BUTTON_PLAY
;
168 if ((state
& 0x2) == 0) return BUTTON_RIGHT
;
170 #elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_1G2G_PAD)
171 state
= GPIOA_INPUT_VAL
;
172 if ((state
& 0x08) == 0) return BUTTON_LEFT
;
173 if ((state
& 0x10) == 0) return BUTTON_MENU
;
174 if ((state
& 0x04) == 0) return BUTTON_PLAY
;
175 if ((state
& 0x01) == 0) return BUTTON_RIGHT
;
180 bool button_hold(void)
182 #if CONFIG_KEYPAD == IPOD_1G2G_PAD
183 return (GPIOA_INPUT_VAL
& 0x20);
185 return !(GPIOA_INPUT_VAL
& 0x20);
189 void fatal_error(void)
192 bool holdstatus
=false;
194 /* System font is 6 pixels wide */
195 #if defined(IPOD_1G2G) || defined(IPOD_3G)
196 printf("Hold MENU+PLAY to");
197 printf("reboot then REW+FF");
198 printf("for disk mode");
199 #elif LCD_WIDTH >= (30*6)
200 printf("Hold MENU+SELECT to reboot");
201 printf("then SELECT+PLAY for disk mode");
203 printf("Hold MENU+SELECT to");
204 printf("reboot then SELECT+PLAY");
205 printf("for disk mode");
210 if (button_hold() != holdstatus
) {
213 lcd_puts(0, line
, "Hold switch on!");
216 lcd_puts(0, line
, " ");
220 udelay(100000); /* 100ms */
233 bool button_was_held
;
234 struct partinfo
* pinfo
;
235 unsigned short* identify_info
;
237 /* Check the button hold status as soon as possible - to
238 give the user maximum chance to turn it off in order to
239 reset the settings in rockbox. */
240 button_was_held
= button_hold();
245 #ifndef HAVE_BACKLIGHT_INVERSION
246 backlight_init(); /* Turns on the backlight */
252 #ifdef HAVE_LCD_COLOR
253 lcd_set_foreground(LCD_WHITE
);
254 lcd_set_background(LCD_BLACK
);
259 /* ADC and button drivers are not yet implemented */
266 /* Enable bootloader messages */
267 if (btn
==BUTTON_RIGHT
)
270 lcd_setfont(FONT_SYSFIXED
);
272 printf("Rockbox boot loader");
273 printf("Version: %s", version
);
274 printf("IPOD version: 0x%08x", IPOD_HW_REVISION
);
278 identify_info
=ata_get_identify();
280 for (i
=0; i
< 20; i
++) {
281 ((unsigned short*)buf
)[i
]=htobe16(identify_info
[i
+27]);
284 for (i
=39; i
&& buf
[i
]==' '; i
--) {
289 printf("ATA: %d", i
);
293 rc
= disk_mount_all();
296 printf("No partition found");
300 pinfo
= disk_partinfo(1);
301 printf("Partition 1: 0x%02x %ld sectors",
302 pinfo
->type
, pinfo
->size
);
304 if (button_was_held
|| (btn
==BUTTON_MENU
)) {
305 /* If either the hold switch was on, or the Menu button was held, then
306 try the Apple firmware */
308 printf("Loading original firmware...");
310 /* First try an apple_os.ipod file on the FAT32 partition
311 (either in .rockbox or the root)
314 rc
=load_firmware(loadbuffer
, "apple_os.ipod", MAX_LOADSIZE
);
317 printf("apple_os.ipod loaded.");
318 return (void*)DRAM_START
;
319 } else if (rc
== EFILE_NOT_FOUND
) {
320 /* If apple_os.ipod doesn't exist, then check if there is an Apple
321 firmware image in RAM */
322 haveretailos
= (memcmp((void*)(DRAM_START
+0x20),"portalplayer",12)==0);
324 /* We have a copy of the retailos in RAM, lets just run it. */
325 return (void*)DRAM_START
;
327 } else if (rc
< EFILE_NOT_FOUND
) {
329 printf("Can't load apple_os.ipod:");
330 printf(strerror(rc
));
333 /* Everything failed - just loop forever */
334 printf("No RetailOS detected");
336 } else if (btn
==BUTTON_PLAY
) {
337 printf("Loading Linux...");
338 rc
=load_raw_firmware(loadbuffer
, "/linux.bin", MAX_LOADSIZE
);
341 printf("Can't load linux.bin:");
342 printf(strerror(rc
));
344 return (void*)DRAM_START
;
347 printf("Loading Rockbox...");
348 rc
=load_firmware(loadbuffer
, BOOTFILE
, MAX_LOADSIZE
);
351 printf("Can't load " BOOTFILE
": ");
352 printf(strerror(rc
));
354 printf("Rockbox loaded.");
355 return (void*)DRAM_START
;
359 /* If we get to here, then we haven't been able to load any firmware */
362 /* We never get here, but keep gcc happy */
366 /* These functions are present in the firmware library, but we reimplement
367 them here because the originals do a lot more than we want */
368 void usb_acknowledge(void)
372 void usb_wait_for_disconnect(void)