Fix remote backdrop crashes
[kugel-rb.git] / bootloader / ipod.c
blobe5538b280ccd00cef3de61f8d1a7a8dd012179bf
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include "cpu.h"
31 #include "system.h"
32 #include "lcd.h"
33 #include "kernel.h"
34 #include "thread.h"
35 #include "ata.h"
36 #include "fat.h"
37 #include "disk.h"
38 #include "font.h"
39 #include "adc.h"
40 #include "backlight.h"
41 #include "panic.h"
42 #include "power.h"
43 #include "file.h"
44 #include "common.h"
45 #include "hwcompat.h"
47 #define XSC(X) #X
48 #define SC(X) XSC(X)
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;
61 #define BUTTON_LEFT 1
62 #define BUTTON_MENU 2
63 #define BUTTON_RIGHT 3
64 #define BUTTON_PLAY 4
65 #define BUTTON_HOLD 5
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) {
72 return 1;
73 } else {
74 return 0;
78 static void ser_opto_keypad_cfg(int val)
80 int start_time;
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;
92 do {
93 if ((inl(0x7000c104) & 0x80000000) == 0) {
94 break;
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;)
114 int key_pressed = 0;
115 int start_time;
116 unsigned int key_pad_val;
118 ser_opto_keypad_cfg(0x8000023a);
120 start_time = USEC_TIMER;
121 do {
122 if (inl(0x7000c104) & 0x4000000) {
123 had_io = 1;
124 break;
127 if (had_io != 0) {
128 break;
130 } while (timer_check(start_time, 1500) != 0);
132 key_pad_val = inl(0x7000c140);
133 if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
134 loop_cnt--;
135 } else {
136 key_pad_val = (key_pad_val << 11) >> 27;
137 key_pressed = 1;
140 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
141 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
143 if (key_pressed != 0) {
144 return key_pad_val ^ 0x1f;
148 return 0;
150 #endif
152 static int key_pressed(void)
154 unsigned char state;
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;
163 #else
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;
169 #endif
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;
176 #endif
177 return 0;
180 bool button_hold(void)
182 #if CONFIG_KEYPAD == IPOD_1G2G_PAD
183 return (GPIOA_INPUT_VAL & 0x20);
184 #else
185 return !(GPIOA_INPUT_VAL & 0x20);
186 #endif
189 void fatal_error(void)
191 extern int line;
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");
202 #else
203 printf("Hold MENU+SELECT to");
204 printf("reboot then SELECT+PLAY");
205 printf("for disk mode");
206 #endif
207 lcd_update();
209 while (1) {
210 if (button_hold() != holdstatus) {
211 if (button_hold()) {
212 holdstatus=true;
213 lcd_puts(0, line, "Hold switch on!");
214 } else {
215 holdstatus=false;
216 lcd_puts(0, line, " ");
218 lcd_update();
220 udelay(100000); /* 100ms */
225 /* The bootloader is started from the OSOS image on the firmware
226 * partition. There are several ways it can be installed there:
227 * appended to the Apple firmware, on its own, or appended to
228 * Rockbox itself. The Apple ROM loader loads the entire OSOS
229 * image to DRAM_START, whatever it contains. If the bootloader
230 * is appended to another image then it will've modified the
231 * entry point in the OSOS header such that the ROM will call the
232 * bootloader rather than the main image.
234 * So, once the bootloader has control:
236 * 1) If the hold switch is on, or the menu button is being held,
237 * try to boot the Apple firmware.
238 * 1a) First, it looks for apple_os.ipod on the FAT32 partition,
239 * in .rockbox or the root directory. If found it loads that
240 * without further checking and runs it.
241 * 1b) Next, it checks to see if the OSOS image already loaded
242 * into RAM is in fact the Apple firmware with the bootloader
243 * appended. It looks at DRAM_START+0x20 for the string
244 * "portalplayer", and if it's there, just jumps back to
245 * DRAM_START where the entry point was before the bootloader
246 * was appended.
247 * 1c) If neither of those worked, it displays an error and dies.
249 * 2) If the play button is being held, try to boot Linux. It looks
250 * for linux.bin in the root directory, and if it's not there,
251 * it displays an error and dies.
253 * 3) Otherwise, try to boot Rockbox.
254 * 3a) First, it looks for rockbox.ipod on the FAT32 partition,
255 * in .rockbox or the root directory. If found it loads that
256 * without further checking and runs it.
257 * 3b) Next, it checks to see if the OSOS image already loaded
258 * into RAM is in fact Rockbox with the bootloader appended.
259 * It looks at DRAM_START+0x20 for the string "Rockbox\1"
260 * (which is inserted there in crt0-pp.S), and if it's there,
261 * just humps back to DRAM_START where the entry point was
262 * before the bootloader was appended.
263 * 3c) If neither of those worked, it displays an error and dies.
265 * The result is that any of the three install configurations work,
266 * and that images of apple_os.ipod or rockbox.ipod on the FAT32
267 * partition take priority over the contents of OSOS (this avoids
268 * upgrades failing to work if OSOS is not updated).
270 * Loading from OSOS is somewhat faster than loading from FAT32,
271 * because the Apple ROM doesn't have to deal with filesystems or
272 * fragmentation, and is already loading from OSOS anyway. Thus,
273 * the fastest boot configuration that still allows dual booting
274 * is to install Rockbox into OSOS with the bootloader appended
275 * (and delete/rename rockbox.ipod from the FAT32 partition).
277 * It is of course faster to just install Rockbox to OSOS alone,
278 * but then it's impossible to boot the Apple firmware.
281 void* main(void)
283 char buf[256];
284 int i;
285 int btn;
286 int rc;
287 bool haveramos;
288 bool button_was_held;
289 struct partinfo* pinfo;
290 unsigned short* identify_info;
292 /* Check the button hold status as soon as possible - to
293 give the user maximum chance to turn it off in order to
294 reset the settings in rockbox. */
295 button_was_held = button_hold();
297 system_init();
298 kernel_init();
300 #ifndef HAVE_BACKLIGHT_INVERSION
301 backlight_init(); /* Turns on the backlight */
302 #endif
304 lcd_init();
305 font_init();
307 #ifdef HAVE_LCD_COLOR
308 lcd_set_foreground(LCD_WHITE);
309 lcd_set_background(LCD_BLACK);
310 lcd_clear_display();
311 #endif
313 #if 0
314 /* ADC and button drivers are not yet implemented */
315 adc_init();
316 button_init();
317 #endif
319 btn=key_pressed();
321 /* Enable bootloader messages */
322 if (btn==BUTTON_RIGHT)
323 verbose = true;
325 lcd_setfont(FONT_SYSFIXED);
327 printf("Rockbox boot loader");
328 printf("Version: %s", version);
329 printf("IPOD version: 0x%08x", IPOD_HW_REVISION);
331 i=ata_init();
332 if (i==0) {
333 identify_info=ata_get_identify();
334 /* Show model */
335 for (i=0; i < 20; i++) {
336 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
338 buf[40]=0;
339 for (i=39; i && buf[i]==' '; i--) {
340 buf[i]=0;
342 printf(buf);
343 } else {
344 printf("ATA: %d", i);
347 disk_init();
348 rc = disk_mount_all();
349 if (rc<=0)
351 printf("No partition found");
352 fatal_error();
355 pinfo = disk_partinfo(1);
356 printf("Partition 1: 0x%02x %ld sectors",
357 pinfo->type, pinfo->size);
359 if (button_was_held || (btn==BUTTON_MENU)) {
360 /* If either the hold switch was on, or the Menu button was held, then
361 try the Apple firmware */
363 printf("Loading original firmware...");
365 /* First try an apple_os.ipod file on the FAT32 partition
366 (either in .rockbox or the root)
369 rc=load_firmware(loadbuffer, "apple_os.ipod", MAX_LOADSIZE);
371 if (rc == EOK) {
372 printf("apple_os.ipod loaded.");
373 return (void*)DRAM_START;
374 } else if (rc == EFILE_NOT_FOUND) {
375 /* If apple_os.ipod doesn't exist, then check if there is an Apple
376 firmware image in RAM */
377 haveramos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
378 if (haveramos) {
379 /* We have a copy of the retailos in RAM, lets just run it. */
380 return (void*)DRAM_START;
382 } else if (rc < EFILE_NOT_FOUND) {
383 printf("Error!");
384 printf("Can't load apple_os.ipod:");
385 printf(strerror(rc));
388 /* Everything failed - just loop forever */
389 printf("No RetailOS detected");
391 } else if (btn==BUTTON_PLAY) {
392 printf("Loading Linux...");
393 rc=load_raw_firmware(loadbuffer, "/linux.bin", MAX_LOADSIZE);
394 if (rc < EOK) {
395 printf("Error!");
396 printf("Can't load linux.bin:");
397 printf(strerror(rc));
398 } else {
399 return (void*)DRAM_START;
401 } else {
402 printf("Loading Rockbox...");
403 rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
404 if (rc == EOK) {
405 printf("Rockbox loaded.");
406 return (void*)DRAM_START;
407 } else if (rc == EFILE_NOT_FOUND) {
408 /* if rockbox.ipod doesn't exist, then check if there is a Rockbox
409 image in RAM */
410 haveramos = (memcmp((void*)(DRAM_START+0x20),"Rockbox\1",8)==0);
411 if (haveramos) {
412 /* We have a copy of Rockbox in RAM, lets just run it. */
413 return (void*)DRAM_START;
417 printf("Error!");
418 printf("Can't load " BOOTFILE ": ");
419 printf(strerror(rc));
422 /* If we get to here, then we haven't been able to load any firmware */
423 fatal_error();
425 /* We never get here, but keep gcc happy */
426 return (void*)0;