Accept both 0x0b and 0x0c as valid FAT32 partition types in the partition table
[Rockbox.git] / bootloader / ipod.c
blobd308e3e9c5cea2e9400a249b9b2e8adbd8de3b45
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 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include "cpu.h"
29 #include "system.h"
30 #include "lcd.h"
31 #include "kernel.h"
32 #include "thread.h"
33 #include "ata.h"
34 #include "fat.h"
35 #include "disk.h"
36 #include "font.h"
37 #include "adc.h"
38 #include "backlight-target.h"
39 #include "panic.h"
40 #include "power.h"
41 #include "file.h"
42 #include "common.h"
43 #include "hwcompat.h"
45 #define XSC(X) #X
46 #define SC(X) XSC(X)
48 /* Maximum allowed firmware image size. The largest known current
49 (December 2006) firmware is about 7.5MB (Apple's firmware for the ipod video)
50 so we set this to 8MB. */
51 #define MAX_LOADSIZE (8*1024*1024)
53 /* A buffer to load the Linux kernel or Rockbox into */
54 unsigned char *loadbuffer = (unsigned char *)DRAM_START;
56 /* Bootloader version */
57 char version[] = APPSVERSION;
59 #define BUTTON_LEFT 1
60 #define BUTTON_MENU 2
61 #define BUTTON_RIGHT 3
62 #define BUTTON_PLAY 4
63 #define BUTTON_HOLD 5
65 #if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_MINI)
66 /* check if number of seconds has past */
67 int timer_check(int clock_start, unsigned int usecs)
69 if ((USEC_TIMER - clock_start) >= usecs) {
70 return 1;
71 } else {
72 return 0;
76 static void ser_opto_keypad_cfg(int val)
78 int start_time;
80 GPIOB_ENABLE &=~ 0x80;
82 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
83 outl(val, 0x7000c120);
84 outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
86 GPIOB_OUTPUT_VAL &=~ 0x10;
87 GPIOB_OUTPUT_EN |= 0x10;
89 start_time = USEC_TIMER;
90 do {
91 if ((inl(0x7000c104) & 0x80000000) == 0) {
92 break;
94 } while (timer_check(start_time, 1500) != 0);
96 outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
98 GPIOB_ENABLE |= 0x80;
99 GPIOB_OUTPUT_VAL |= 0x10;
100 GPIOB_OUTPUT_EN &=~0x10;
102 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
103 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
106 int opto_keypad_read(void)
108 int loop_cnt, had_io = 0;
110 for (loop_cnt = 5; loop_cnt != 0;)
112 int key_pressed = 0;
113 int start_time;
114 unsigned int key_pad_val;
116 ser_opto_keypad_cfg(0x8000023a);
118 start_time = USEC_TIMER;
119 do {
120 if (inl(0x7000c104) & 0x4000000) {
121 had_io = 1;
122 break;
125 if (had_io != 0) {
126 break;
128 } while (timer_check(start_time, 1500) != 0);
130 key_pad_val = inl(0x7000c140);
131 if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
132 loop_cnt--;
133 } else {
134 key_pad_val = (key_pad_val << 11) >> 27;
135 key_pressed = 1;
138 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
139 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
141 if (key_pressed != 0) {
142 return key_pad_val ^ 0x1f;
146 return 0;
148 #endif
150 static int key_pressed(void)
152 unsigned char state;
154 #if CONFIG_KEYPAD == IPOD_4G_PAD
155 #ifdef IPOD_MINI /* mini 1G only */
156 state = GPIOA_INPUT_VAL & 0x3f;
157 if ((state & 0x10) == 0) return BUTTON_LEFT;
158 if ((state & 0x2) == 0) return BUTTON_MENU;
159 if ((state & 0x4) == 0) return BUTTON_PLAY;
160 if ((state & 0x8) == 0) return BUTTON_RIGHT;
161 #else
162 state = opto_keypad_read();
163 if ((state & 0x4) == 0) return BUTTON_LEFT;
164 if ((state & 0x10) == 0) return BUTTON_MENU;
165 if ((state & 0x8) == 0) return BUTTON_PLAY;
166 if ((state & 0x2) == 0) return BUTTON_RIGHT;
167 #endif
168 #elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_1G2G_PAD)
169 state = GPIOA_INPUT_VAL;
170 if ((state & 0x08) == 0) return BUTTON_LEFT;
171 if ((state & 0x10) == 0) return BUTTON_MENU;
172 if ((state & 0x04) == 0) return BUTTON_PLAY;
173 if ((state & 0x01) == 0) return BUTTON_RIGHT;
174 #endif
175 return 0;
178 bool button_hold(void)
180 #if CONFIG_KEYPAD == IPOD_1G2G_PAD
181 return (GPIOA_INPUT_VAL & 0x20);
182 #else
183 return !(GPIOA_INPUT_VAL & 0x20);
184 #endif
187 void fatal_error(void)
189 extern int line;
190 bool holdstatus=false;
192 /* System font is 6 pixels wide */
193 #if defined(IPOD_1G2G) || defined(IPOD_3G)
194 printf("Hold MENU+PLAY to");
195 printf("reboot then REW+FF");
196 printf("for disk mode");
197 #elif LCD_WIDTH >= (30*6)
198 printf("Hold MENU+SELECT to reboot");
199 printf("then SELECT+PLAY for disk mode");
200 #else
201 printf("Hold MENU+SELECT to");
202 printf("reboot then SELECT+PLAY");
203 printf("for disk mode");
204 #endif
205 lcd_update();
207 while (1) {
208 if (button_hold() != holdstatus) {
209 if (button_hold()) {
210 holdstatus=true;
211 lcd_puts(0, line, "Hold switch on!");
212 } else {
213 holdstatus=false;
214 lcd_puts(0, line, " ");
216 lcd_update();
218 udelay(100000); /* 100ms */
224 void* main(void)
226 char buf[256];
227 int i;
228 int btn;
229 int rc;
230 bool haveretailos;
231 bool button_was_held;
232 struct partinfo* pinfo;
233 unsigned short* identify_info;
235 /* Check the button hold status as soon as possible - to
236 give the user maximum chance to turn it off in order to
237 reset the settings in rockbox. */
238 button_was_held = button_hold();
240 /* Turn on the backlight */
242 __backlight_on();
244 system_init();
245 kernel_init();
246 lcd_init();
247 font_init();
249 #ifdef HAVE_LCD_COLOR
250 lcd_set_foreground(LCD_WHITE);
251 lcd_set_background(LCD_BLACK);
252 lcd_clear_display();
253 #endif
255 #if 0
256 /* ADC and button drivers are not yet implemented */
257 adc_init();
258 button_init();
259 #endif
261 btn=key_pressed();
263 /* Enable bootloader messages */
264 if (btn==BUTTON_RIGHT)
265 verbose = true;
267 lcd_setfont(FONT_SYSFIXED);
269 printf("Rockbox boot loader");
270 printf("Version: %s", version);
271 printf("IPOD version: 0x%08x", IPOD_HW_REVISION);
273 i=ata_init();
274 if (i==0) {
275 identify_info=ata_get_identify();
276 /* Show model */
277 for (i=0; i < 20; i++) {
278 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
280 buf[40]=0;
281 for (i=39; i && buf[i]==' '; i--) {
282 buf[i]=0;
284 printf(buf);
285 } else {
286 printf("ATA: %d", i);
289 disk_init();
290 rc = disk_mount_all();
291 if (rc<=0)
293 printf("No partition found");
294 fatal_error();
297 pinfo = disk_partinfo(1);
298 printf("Partition 1: 0x%02x %ld MB",
299 pinfo->type, pinfo->size / 2048);
301 if (button_was_held || (btn==BUTTON_MENU)) {
302 /* If either the hold switch was on, or the Menu button was held, then
303 try the Apple firmware */
305 printf("Loading original firmware...");
307 /* First try an apple_os.ipod file on the FAT32 partition
308 (either in .rockbox or the root)
311 rc=load_firmware(loadbuffer, "apple_os.ipod", MAX_LOADSIZE);
313 if (rc == EOK) {
314 printf("apple_os.ipod loaded.");
315 return (void*)DRAM_START;
316 } else if (rc == EFILE_NOT_FOUND) {
317 /* If apple_os.ipod doesn't exist, then check if there is an Apple
318 firmware image in RAM */
319 haveretailos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
320 if (haveretailos) {
321 /* We have a copy of the retailos in RAM, lets just run it. */
322 return (void*)DRAM_START;
324 } else if (rc < EFILE_NOT_FOUND) {
325 printf("Error!");
326 printf("Can't load apple_os.ipod:");
327 printf(strerror(rc));
330 /* Everything failed - just loop forever */
331 printf("No RetailOS detected");
333 } else if (btn==BUTTON_PLAY) {
334 printf("Loading Linux...");
335 rc=load_raw_firmware(loadbuffer, "/linux.bin", MAX_LOADSIZE);
336 if (rc < EOK) {
337 printf("Error!");
338 printf("Can't load linux.bin:");
339 printf(strerror(rc));
340 } else {
341 return (void*)DRAM_START;
343 } else {
344 printf("Loading Rockbox...");
345 rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
346 if (rc < EOK) {
347 printf("Error!");
348 printf("Can't load rockbox.ipod:");
349 printf(strerror(rc));
350 } else {
351 printf("Rockbox loaded.");
352 return (void*)DRAM_START;
356 /* If we get to here, then we haven't been able to load any firmware */
357 fatal_error();
359 /* We never get here, but keep gcc happy */
360 return (void*)0;
363 /* These functions are present in the firmware library, but we reimplement
364 them here because the originals do a lot more than we want */
365 void usb_acknowledge(void)
369 void usb_wait_for_disconnect(void)