PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/bootloader/ipod.c

https://github.com/jdgordon/rockbox
C | 435 lines | 277 code | 54 blank | 104 comment | 88 complexity | 18cee29e295d60438293889b8b899627 MD5 | raw file
  1. /***************************************************************************
  2. * __________ __ ___.
  3. * Open \______ \ ____ ____ | | _\_ |__ _______ ___
  4. * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
  5. * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
  6. * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
  7. * \/ \/ \/ \/ \/
  8. * $Id$
  9. *
  10. * Copyright (C) 2005 by Dave Chapman
  11. *
  12. * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
  13. * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
  14. *
  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.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. ****************************************************************************/
  24. #include "config.h"
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include "cpu.h"
  30. #include "system.h"
  31. #include "lcd.h"
  32. #include "kernel.h"
  33. #include "thread.h"
  34. #include "ata.h"
  35. #include "fat.h"
  36. #include "disk.h"
  37. #include "font.h"
  38. #include "adc.h"
  39. #include "backlight.h"
  40. #include "panic.h"
  41. #include "power.h"
  42. #include "file.h"
  43. #include "common.h"
  44. #include "hwcompat.h"
  45. #include "usb.h"
  46. #include "version.h"
  47. #define XSC(X) #X
  48. #define SC(X) XSC(X)
  49. /* Maximum allowed firmware image size. The largest known current
  50. (December 2006) firmware is about 7.5MB (Apple's firmware for the ipod video)
  51. so we set this to 8MB. */
  52. #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;
  55. #if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_MINI)
  56. /* check if number of seconds has past */
  57. int timer_check(int clock_start, unsigned int usecs)
  58. {
  59. if ((USEC_TIMER - clock_start) >= usecs) {
  60. return 1;
  61. } else {
  62. return 0;
  63. }
  64. }
  65. static void ser_opto_keypad_cfg(int val)
  66. {
  67. int start_time;
  68. GPIOB_ENABLE &=~ 0x80;
  69. outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
  70. outl(val, 0x7000c120);
  71. outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
  72. GPIOB_OUTPUT_VAL &=~ 0x10;
  73. GPIOB_OUTPUT_EN |= 0x10;
  74. start_time = USEC_TIMER;
  75. do {
  76. if ((inl(0x7000c104) & 0x80000000) == 0) {
  77. break;
  78. }
  79. } while (timer_check(start_time, 1500) != 0);
  80. outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
  81. GPIOB_ENABLE |= 0x80;
  82. GPIOB_OUTPUT_VAL |= 0x10;
  83. GPIOB_OUTPUT_EN &=~0x10;
  84. outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
  85. outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
  86. }
  87. int opto_keypad_read(void)
  88. {
  89. int loop_cnt, had_io = 0;
  90. for (loop_cnt = 5; loop_cnt != 0;)
  91. {
  92. int key_pressed = 0;
  93. int start_time;
  94. unsigned int key_pad_val;
  95. ser_opto_keypad_cfg(0x8000023a);
  96. start_time = USEC_TIMER;
  97. do {
  98. if (inl(0x7000c104) & 0x4000000) {
  99. had_io = 1;
  100. break;
  101. }
  102. if (had_io != 0) {
  103. break;
  104. }
  105. } while (timer_check(start_time, 1500) != 0);
  106. key_pad_val = inl(0x7000c140);
  107. if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
  108. loop_cnt--;
  109. } else {
  110. key_pad_val = (key_pad_val << 11) >> 27;
  111. key_pressed = 1;
  112. }
  113. outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
  114. outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
  115. if (key_pressed != 0) {
  116. return key_pad_val ^ 0x1f;
  117. }
  118. }
  119. return 0;
  120. }
  121. #endif
  122. static int key_pressed(void)
  123. {
  124. unsigned char state;
  125. #if CONFIG_KEYPAD == IPOD_4G_PAD
  126. #ifdef IPOD_MINI /* mini 1G only */
  127. state = GPIOA_INPUT_VAL & 0x3f;
  128. if ((state & 0x10) == 0) return BUTTON_LEFT;
  129. if ((state & 0x2) == 0) return BUTTON_MENU;
  130. if ((state & 0x4) == 0) return BUTTON_PLAY;
  131. if ((state & 0x8) == 0) return BUTTON_RIGHT;
  132. #else
  133. state = opto_keypad_read();
  134. if ((state & 0x4) == 0) return BUTTON_LEFT;
  135. if ((state & 0x10) == 0) return BUTTON_MENU;
  136. if ((state & 0x8) == 0) return BUTTON_PLAY;
  137. if ((state & 0x2) == 0) return BUTTON_RIGHT;
  138. #endif
  139. #elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_1G2G_PAD)
  140. state = GPIOA_INPUT_VAL;
  141. if ((state & 0x08) == 0) return BUTTON_LEFT;
  142. if ((state & 0x10) == 0) return BUTTON_MENU;
  143. if ((state & 0x04) == 0) return BUTTON_PLAY;
  144. if ((state & 0x01) == 0) return BUTTON_RIGHT;
  145. #endif
  146. return 0;
  147. }
  148. bool button_hold(void)
  149. {
  150. #if CONFIG_KEYPAD == IPOD_1G2G_PAD
  151. return (GPIOA_INPUT_VAL & 0x20);
  152. #else
  153. return !(GPIOA_INPUT_VAL & 0x20);
  154. #endif
  155. }
  156. void fatal_error(void)
  157. {
  158. extern int line;
  159. bool holdstatus=false;
  160. /* System font is 6 pixels wide */
  161. #if defined(IPOD_1G2G) || defined(IPOD_3G)
  162. printf("Insert Firewire cable, or");
  163. printf("hold MENU+PLAY to reboot");
  164. printf("then REW+FF for disk mode");
  165. #elif LCD_WIDTH >= (30*6)
  166. printf("Insert USB cable, or");
  167. printf("hold MENU+SELECT to reboot");
  168. printf("then SELECT+PLAY for disk mode");
  169. #else
  170. printf("Insert USB cable, or");
  171. printf("hold MENU+SELECT to");
  172. printf("reboot then SELECT+PLAY");
  173. printf("for disk mode");
  174. #endif
  175. lcd_update();
  176. usb_init();
  177. while (1) {
  178. if (button_hold() != holdstatus) {
  179. if (button_hold()) {
  180. holdstatus=true;
  181. lcd_puts(0, line, "Hold switch on!");
  182. } else {
  183. holdstatus=false;
  184. lcd_puts(0, line, " ");
  185. }
  186. lcd_update();
  187. }
  188. if (usb_detect() == USB_INSERTED) {
  189. ata_sleepnow(); /* Immediately spindown the disk. */
  190. sleep(HZ*2);
  191. #if CONFIG_CPU == PP5020
  192. memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
  193. #elif CONFIG_CPU == PP5022
  194. memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
  195. #elif CONFIG_CPU == PP5002
  196. memcpy((void *)0x40017f00, "diskmodehotstuff\1", 17);
  197. #endif /* CONFIG_CPU */
  198. system_reboot(); /* Reboot */
  199. }
  200. udelay(100000); /* 100ms */
  201. }
  202. }
  203. /* The bootloader is started from the OSOS image on the firmware
  204. * partition. There are several ways it can be installed there:
  205. * appended to the Apple firmware, on its own, or appended to
  206. * Rockbox itself. The Apple ROM loader loads the entire OSOS
  207. * image to DRAM_START, whatever it contains. If the bootloader
  208. * is appended to another image then it will've modified the
  209. * entry point in the OSOS header such that the ROM will call the
  210. * bootloader rather than the main image.
  211. *
  212. * So, once the bootloader has control:
  213. *
  214. * 1) If the hold switch is on, or the menu button is being held,
  215. * try to boot the Apple firmware.
  216. * 1a) First, it looks for apple_os.ipod on the FAT32 partition,
  217. * in .rockbox or the root directory. If found it loads that
  218. * without further checking and runs it.
  219. * 1b) Next, it checks to see if the OSOS image already loaded
  220. * into RAM is in fact the Apple firmware with the bootloader
  221. * appended. It looks at DRAM_START+0x20 for the string
  222. * "portalplayer", and if it's there, just jumps back to
  223. * DRAM_START where the entry point was before the bootloader
  224. * was appended.
  225. * 1c) If neither of those worked, it displays an error and dies.
  226. *
  227. * 2) If the play button is being held, try to boot Linux. It looks
  228. * for linux.bin in the root directory, and if it's not there,
  229. * it displays an error and dies.
  230. *
  231. * 3) Otherwise, try to boot Rockbox.
  232. * 3a) First, it looks for rockbox.ipod on the FAT32 partition,
  233. * in .rockbox or the root directory. If found it loads that
  234. * without further checking and runs it.
  235. * 3b) Next, it checks to see if the OSOS image already loaded
  236. * into RAM is in fact Rockbox with the bootloader appended.
  237. * It looks at DRAM_START+0x20 for the string "Rockbox\1"
  238. * (which is inserted there in crt0-pp.S), and if it's there,
  239. * just humps back to DRAM_START where the entry point was
  240. * before the bootloader was appended.
  241. * 3c) If neither of those worked, it displays an error and dies.
  242. *
  243. * The result is that any of the three install configurations work,
  244. * and that images of apple_os.ipod or rockbox.ipod on the FAT32
  245. * partition take priority over the contents of OSOS (this avoids
  246. * upgrades failing to work if OSOS is not updated).
  247. *
  248. * Loading from OSOS is somewhat faster than loading from FAT32,
  249. * because the Apple ROM doesn't have to deal with filesystems or
  250. * fragmentation, and is already loading from OSOS anyway. Thus,
  251. * the fastest boot configuration that still allows dual booting
  252. * is to install Rockbox into OSOS with the bootloader appended
  253. * (and delete/rename rockbox.ipod from the FAT32 partition).
  254. *
  255. * It is of course faster to just install Rockbox to OSOS alone,
  256. * but then it's impossible to boot the Apple firmware.
  257. */
  258. void* main(void)
  259. {
  260. char buf[256];
  261. int i;
  262. int btn;
  263. int rc;
  264. bool haveramos;
  265. bool button_was_held;
  266. struct partinfo* pinfo;
  267. unsigned short* identify_info;
  268. /* Check the button hold status as soon as possible - to
  269. give the user maximum chance to turn it off in order to
  270. reset the settings in rockbox. */
  271. button_was_held = button_hold();
  272. system_init();
  273. kernel_init();
  274. #ifndef HAVE_BACKLIGHT_INVERSION
  275. backlight_init(); /* Turns on the backlight */
  276. #endif
  277. lcd_init();
  278. font_init();
  279. #ifdef HAVE_LCD_COLOR
  280. lcd_set_foreground(LCD_WHITE);
  281. lcd_set_background(LCD_BLACK);
  282. lcd_clear_display();
  283. #endif
  284. #if 0
  285. /* ADC and button drivers are not yet implemented */
  286. adc_init();
  287. button_init();
  288. #endif
  289. btn=key_pressed();
  290. /* Enable bootloader messages */
  291. if (btn==BUTTON_RIGHT)
  292. verbose = true;
  293. lcd_setfont(FONT_SYSFIXED);
  294. printf("Rockbox boot loader");
  295. printf("Version: " RBVERSION);
  296. printf("IPOD version: 0x%08x", IPOD_HW_REVISION);
  297. i=ata_init();
  298. if (i==0) {
  299. identify_info=ata_get_identify();
  300. /* Show model */
  301. for (i=0; i < 20; i++) {
  302. ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
  303. }
  304. buf[40]=0;
  305. for (i=39; i && buf[i]==' '; i--) {
  306. buf[i]=0;
  307. }
  308. printf(buf);
  309. } else {
  310. printf("ATA: %d", i);
  311. }
  312. disk_init();
  313. rc = disk_mount_all();
  314. if (rc<=0)
  315. {
  316. printf("No partition found");
  317. fatal_error();
  318. }
  319. pinfo = disk_partinfo(1);
  320. printf("Partition 1: 0x%02x %ld sectors",
  321. pinfo->type, pinfo->size);
  322. if (button_was_held || (btn==BUTTON_MENU)) {
  323. /* If either the hold switch was on, or the Menu button was held, then
  324. try the Apple firmware */
  325. printf("Loading original firmware...");
  326. /* First try an apple_os.ipod file on the FAT32 partition
  327. (either in .rockbox or the root)
  328. */
  329. rc=load_firmware(loadbuffer, "apple_os.ipod", MAX_LOADSIZE);
  330. if (rc == EOK) {
  331. printf("apple_os.ipod loaded.");
  332. return (void*)DRAM_START;
  333. } else if (rc == EFILE_NOT_FOUND) {
  334. /* If apple_os.ipod doesn't exist, then check if there is an Apple
  335. firmware image in RAM */
  336. haveramos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
  337. if (haveramos) {
  338. /* We have a copy of the retailos in RAM, lets just run it. */
  339. return (void*)DRAM_START;
  340. }
  341. } else if (rc < EFILE_NOT_FOUND) {
  342. printf("Error!");
  343. printf("Can't load apple_os.ipod:");
  344. printf(strerror(rc));
  345. }
  346. /* Everything failed - just loop forever */
  347. printf("No RetailOS detected");
  348. } else if (btn==BUTTON_PLAY) {
  349. printf("Loading Linux...");
  350. rc=load_raw_firmware(loadbuffer, "/linux.bin", MAX_LOADSIZE);
  351. if (rc < EOK) {
  352. printf("Error!");
  353. printf("Can't load linux.bin:");
  354. printf(strerror(rc));
  355. } else {
  356. return (void*)DRAM_START;
  357. }
  358. } else {
  359. printf("Loading Rockbox...");
  360. rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
  361. if (rc == EOK) {
  362. printf("Rockbox loaded.");
  363. return (void*)DRAM_START;
  364. } else if (rc == EFILE_NOT_FOUND) {
  365. /* if rockbox.ipod doesn't exist, then check if there is a Rockbox
  366. image in RAM */
  367. haveramos = (memcmp((void*)(DRAM_START+0x20),"Rockbox\1",8)==0);
  368. if (haveramos) {
  369. /* We have a copy of Rockbox in RAM, lets just run it. */
  370. return (void*)DRAM_START;
  371. }
  372. }
  373. printf("Error!");
  374. printf("Can't load " BOOTFILE ": ");
  375. printf(strerror(rc));
  376. }
  377. /* If we get to here, then we haven't been able to load any firmware */
  378. fatal_error();
  379. /* We never get here, but keep gcc happy */
  380. return (void*)0;
  381. }