Support "eject" on OS X.
[maemo-rb.git] / bootloader / ipod.c
blobaf4c1576bb9c86ed1cffb95cf652c2f93f8ff839
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"
46 #include "usb.h"
47 #include "version.h"
49 #define XSC(X) #X
50 #define SC(X) XSC(X)
52 /* Maximum allowed firmware image size. The largest known current
53 (December 2006) firmware is about 7.5MB (Apple's firmware for the ipod video)
54 so we set this to 8MB. */
55 #define MAX_LOADSIZE (8*1024*1024)
57 /* A buffer to load the Linux kernel or Rockbox into */
58 unsigned char *loadbuffer = (unsigned char *)DRAM_START;
60 #if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_MINI)
61 /* check if number of seconds has past */
62 int timer_check(int clock_start, unsigned int usecs)
64 if ((USEC_TIMER - clock_start) >= usecs) {
65 return 1;
66 } else {
67 return 0;
71 static void ser_opto_keypad_cfg(int val)
73 int start_time;
75 GPIOB_ENABLE &=~ 0x80;
77 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
78 outl(val, 0x7000c120);
79 outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
81 GPIOB_OUTPUT_VAL &=~ 0x10;
82 GPIOB_OUTPUT_EN |= 0x10;
84 start_time = USEC_TIMER;
85 do {
86 if ((inl(0x7000c104) & 0x80000000) == 0) {
87 break;
89 } while (timer_check(start_time, 1500) != 0);
91 outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
93 GPIOB_ENABLE |= 0x80;
94 GPIOB_OUTPUT_VAL |= 0x10;
95 GPIOB_OUTPUT_EN &=~0x10;
97 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
98 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
101 int opto_keypad_read(void)
103 int loop_cnt, had_io = 0;
105 for (loop_cnt = 5; loop_cnt != 0;)
107 int key_pressed = 0;
108 int start_time;
109 unsigned int key_pad_val;
111 ser_opto_keypad_cfg(0x8000023a);
113 start_time = USEC_TIMER;
114 do {
115 if (inl(0x7000c104) & 0x4000000) {
116 had_io = 1;
117 break;
120 if (had_io != 0) {
121 break;
123 } while (timer_check(start_time, 1500) != 0);
125 key_pad_val = inl(0x7000c140);
126 if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
127 loop_cnt--;
128 } else {
129 key_pad_val = (key_pad_val << 11) >> 27;
130 key_pressed = 1;
133 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
134 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
136 if (key_pressed != 0) {
137 return key_pad_val ^ 0x1f;
141 return 0;
143 #endif
145 static int key_pressed(void)
147 unsigned char state;
149 #if CONFIG_KEYPAD == IPOD_4G_PAD
150 #ifdef IPOD_MINI /* mini 1G only */
151 state = GPIOA_INPUT_VAL & 0x3f;
152 if ((state & 0x10) == 0) return BUTTON_LEFT;
153 if ((state & 0x2) == 0) return BUTTON_MENU;
154 if ((state & 0x4) == 0) return BUTTON_PLAY;
155 if ((state & 0x8) == 0) return BUTTON_RIGHT;
156 #else
157 state = opto_keypad_read();
158 if ((state & 0x4) == 0) return BUTTON_LEFT;
159 if ((state & 0x10) == 0) return BUTTON_MENU;
160 if ((state & 0x8) == 0) return BUTTON_PLAY;
161 if ((state & 0x2) == 0) return BUTTON_RIGHT;
162 #endif
163 #elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_1G2G_PAD)
164 state = GPIOA_INPUT_VAL;
165 if ((state & 0x08) == 0) return BUTTON_LEFT;
166 if ((state & 0x10) == 0) return BUTTON_MENU;
167 if ((state & 0x04) == 0) return BUTTON_PLAY;
168 if ((state & 0x01) == 0) return BUTTON_RIGHT;
169 #endif
170 return 0;
173 bool button_hold(void)
175 #if CONFIG_KEYPAD == IPOD_1G2G_PAD
176 return (GPIOA_INPUT_VAL & 0x20);
177 #else
178 return !(GPIOA_INPUT_VAL & 0x20);
179 #endif
182 void fatal_error(void)
184 extern int line;
185 bool holdstatus=false;
187 /* System font is 6 pixels wide */
188 #if defined(IPOD_1G2G) || defined(IPOD_3G)
189 printf("Insert Firewire cable, or");
190 printf("hold MENU+PLAY to reboot");
191 printf("then REW+FF for disk mode");
192 #elif LCD_WIDTH >= (30*6)
193 printf("Insert USB cable, or");
194 printf("hold MENU+SELECT to reboot");
195 printf("then SELECT+PLAY for disk mode");
196 #else
197 printf("Insert USB cable, or");
198 printf("hold MENU+SELECT to");
199 printf("reboot then SELECT+PLAY");
200 printf("for disk mode");
201 #endif
202 lcd_update();
204 usb_init();
205 while (1) {
206 if (button_hold() != holdstatus) {
207 if (button_hold()) {
208 holdstatus=true;
209 lcd_puts(0, line, "Hold switch on!");
210 } else {
211 holdstatus=false;
212 lcd_puts(0, line, " ");
214 lcd_update();
216 if (usb_detect() == USB_INSERTED) {
217 ata_sleepnow(); /* Immediately spindown the disk. */
218 sleep(HZ*2);
219 #if CONFIG_CPU == PP5020
220 memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
221 #elif CONFIG_CPU == PP5022
222 memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
223 #elif CONFIG_CPU == PP5002
224 memcpy((void *)0x40017f00, "diskmodehotstuff\1", 17);
225 #endif /* CONFIG_CPU */
226 system_reboot(); /* Reboot */
228 udelay(100000); /* 100ms */
233 /* The bootloader is started from the OSOS image on the firmware
234 * partition. There are several ways it can be installed there:
235 * appended to the Apple firmware, on its own, or appended to
236 * Rockbox itself. The Apple ROM loader loads the entire OSOS
237 * image to DRAM_START, whatever it contains. If the bootloader
238 * is appended to another image then it will've modified the
239 * entry point in the OSOS header such that the ROM will call the
240 * bootloader rather than the main image.
242 * So, once the bootloader has control:
244 * 1) If the hold switch is on, or the menu button is being held,
245 * try to boot the Apple firmware.
246 * 1a) First, it looks for apple_os.ipod on the FAT32 partition,
247 * in .rockbox or the root directory. If found it loads that
248 * without further checking and runs it.
249 * 1b) Next, it checks to see if the OSOS image already loaded
250 * into RAM is in fact the Apple firmware with the bootloader
251 * appended. It looks at DRAM_START+0x20 for the string
252 * "portalplayer", and if it's there, just jumps back to
253 * DRAM_START where the entry point was before the bootloader
254 * was appended.
255 * 1c) If neither of those worked, it displays an error and dies.
257 * 2) If the play button is being held, try to boot Linux. It looks
258 * for linux.bin in the root directory, and if it's not there,
259 * it displays an error and dies.
261 * 3) Otherwise, try to boot Rockbox.
262 * 3a) First, it looks for rockbox.ipod on the FAT32 partition,
263 * in .rockbox or the root directory. If found it loads that
264 * without further checking and runs it.
265 * 3b) Next, it checks to see if the OSOS image already loaded
266 * into RAM is in fact Rockbox with the bootloader appended.
267 * It looks at DRAM_START+0x20 for the string "Rockbox\1"
268 * (which is inserted there in crt0-pp.S), and if it's there,
269 * just humps back to DRAM_START where the entry point was
270 * before the bootloader was appended.
271 * 3c) If neither of those worked, it displays an error and dies.
273 * The result is that any of the three install configurations work,
274 * and that images of apple_os.ipod or rockbox.ipod on the FAT32
275 * partition take priority over the contents of OSOS (this avoids
276 * upgrades failing to work if OSOS is not updated).
278 * Loading from OSOS is somewhat faster than loading from FAT32,
279 * because the Apple ROM doesn't have to deal with filesystems or
280 * fragmentation, and is already loading from OSOS anyway. Thus,
281 * the fastest boot configuration that still allows dual booting
282 * is to install Rockbox into OSOS with the bootloader appended
283 * (and delete/rename rockbox.ipod from the FAT32 partition).
285 * It is of course faster to just install Rockbox to OSOS alone,
286 * but then it's impossible to boot the Apple firmware.
289 void* main(void)
291 char buf[256];
292 int i;
293 int btn;
294 int rc;
295 bool haveramos;
296 bool button_was_held;
297 struct partinfo* pinfo;
298 unsigned short* identify_info;
300 /* Check the button hold status as soon as possible - to
301 give the user maximum chance to turn it off in order to
302 reset the settings in rockbox. */
303 button_was_held = button_hold();
305 system_init();
306 kernel_init();
308 #ifndef HAVE_BACKLIGHT_INVERSION
309 backlight_init(); /* Turns on the backlight */
310 #endif
312 lcd_init();
313 font_init();
315 #ifdef HAVE_LCD_COLOR
316 lcd_set_foreground(LCD_WHITE);
317 lcd_set_background(LCD_BLACK);
318 lcd_clear_display();
319 #endif
321 #if 0
322 /* ADC and button drivers are not yet implemented */
323 adc_init();
324 button_init();
325 #endif
327 btn=key_pressed();
329 /* Enable bootloader messages */
330 if (btn==BUTTON_RIGHT)
331 verbose = true;
333 lcd_setfont(FONT_SYSFIXED);
335 printf("Rockbox boot loader");
336 printf("Version: " RBVERSION);
337 printf("IPOD version: 0x%08x", IPOD_HW_REVISION);
339 i=ata_init();
340 if (i==0) {
341 identify_info=ata_get_identify();
342 /* Show model */
343 for (i=0; i < 20; i++) {
344 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
346 buf[40]=0;
347 for (i=39; i && buf[i]==' '; i--) {
348 buf[i]=0;
350 printf(buf);
351 } else {
352 printf("ATA: %d", i);
355 disk_init();
356 rc = disk_mount_all();
357 if (rc<=0)
359 printf("No partition found");
360 fatal_error();
363 pinfo = disk_partinfo(1);
364 printf("Partition 1: 0x%02x %ld sectors",
365 pinfo->type, pinfo->size);
367 if (button_was_held || (btn==BUTTON_MENU)) {
368 /* If either the hold switch was on, or the Menu button was held, then
369 try the Apple firmware */
371 printf("Loading original firmware...");
373 /* First try an apple_os.ipod file on the FAT32 partition
374 (either in .rockbox or the root)
377 rc=load_firmware(loadbuffer, "apple_os.ipod", MAX_LOADSIZE);
379 if (rc == EOK) {
380 printf("apple_os.ipod loaded.");
381 return (void*)DRAM_START;
382 } else if (rc == EFILE_NOT_FOUND) {
383 /* If apple_os.ipod doesn't exist, then check if there is an Apple
384 firmware image in RAM */
385 haveramos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
386 if (haveramos) {
387 /* We have a copy of the retailos in RAM, lets just run it. */
388 return (void*)DRAM_START;
390 } else if (rc < EFILE_NOT_FOUND) {
391 printf("Error!");
392 printf("Can't load apple_os.ipod:");
393 printf(strerror(rc));
396 /* Everything failed - just loop forever */
397 printf("No RetailOS detected");
399 } else if (btn==BUTTON_PLAY) {
400 printf("Loading Linux...");
401 rc=load_raw_firmware(loadbuffer, "/linux.bin", MAX_LOADSIZE);
402 if (rc < EOK) {
403 printf("Error!");
404 printf("Can't load linux.bin:");
405 printf(strerror(rc));
406 } else {
407 return (void*)DRAM_START;
409 } else {
410 printf("Loading Rockbox...");
411 rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
412 if (rc == EOK) {
413 printf("Rockbox loaded.");
414 return (void*)DRAM_START;
415 } else if (rc == EFILE_NOT_FOUND) {
416 /* if rockbox.ipod doesn't exist, then check if there is a Rockbox
417 image in RAM */
418 haveramos = (memcmp((void*)(DRAM_START+0x20),"Rockbox\1",8)==0);
419 if (haveramos) {
420 /* We have a copy of Rockbox in RAM, lets just run it. */
421 return (void*)DRAM_START;
425 printf("Error!");
426 printf("Can't load " BOOTFILE ": ");
427 printf(strerror(rc));
430 /* If we get to here, then we haven't been able to load any firmware */
431 fatal_error();
433 /* We never get here, but keep gcc happy */
434 return (void*)0;