Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Barry Wardell |
| 11 | * |
| 12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing |
| 13 | * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach |
| 14 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 19 | * |
| 20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 21 | * KIND, either express or implied. |
| 22 | * |
| 23 | ****************************************************************************/ |
Jonathan Gordon | ceb1e7a | 2007-03-22 12:55:51 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Björn Stenberg | b69be10 | 2008-11-23 22:07:48 +0000 | [diff] [blame] | 26 | |
| 27 | #include "config.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 28 | #include "common.h" |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 29 | #include "cpu.h" |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 30 | #include "file.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 31 | #include "system.h" |
Michael Sevakis | 4ea4cdf | 2014-08-08 02:28:11 -0400 | [diff] [blame] | 32 | #include "../kernel-internal.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 33 | #include "lcd.h" |
| 34 | #include "font.h" |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 35 | #include "storage.h" |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 36 | #include "file_internal.h" |
Barry Wardell | 1ac260c | 2008-08-07 11:23:40 +0000 | [diff] [blame] | 37 | #include "adc.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 38 | #include "button.h" |
| 39 | #include "disk.h" |
Barry Wardell | 54c73a2 | 2007-06-04 13:48:21 +0000 | [diff] [blame] | 40 | #include "crc32-mi4.h" |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 41 | #include "mi4-loader.h" |
| 42 | #include "loader_strerror.h" |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 43 | #include <string.h> |
Jonathan Gordon | fc8e09d | 2007-11-19 02:07:40 +0000 | [diff] [blame] | 44 | #include "power.h" |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 45 | #include "version.h" |
Mark Arigo | b4275d4 | 2008-05-21 03:55:17 +0000 | [diff] [blame] | 46 | #if defined(SANSA_E200) || defined(PHILIPS_SA9200) |
Barry Wardell | f1fead0 | 2007-10-14 18:20:23 +0000 | [diff] [blame] | 47 | #include "i2c.h" |
| 48 | #include "backlight-target.h" |
| 49 | #endif |
Jonathan Gordon | ceb1e7a | 2007-03-22 12:55:51 +0000 | [diff] [blame] | 50 | #include "usb.h" |
Michael Sevakis | 71651ca | 2011-01-15 08:52:59 +0000 | [diff] [blame] | 51 | #if defined(SANSA_E200) || defined(SANSA_C200) || defined(PHILIPS_SA9200) |
Björn Stenberg | 7af22e1 | 2007-11-22 22:17:45 +0000 | [diff] [blame] | 52 | #include "usb_drv.h" |
Jonathan Gordon | ceb1e7a | 2007-03-22 12:55:51 +0000 | [diff] [blame] | 53 | #endif |
Thomas Martitz | e2ccabf | 2009-09-08 13:57:13 +0000 | [diff] [blame] | 54 | #if defined(SAMSUNG_YH925) |
| 55 | /* this function (in lcd-yh925.c) resets the screen orientation for the OF |
| 56 | * for use with dualbooting */ |
| 57 | void lcd_reset(void); |
| 58 | #endif |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 59 | |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 60 | /* Show the Rockbox logo - in show_logo.c */ |
Bertrik Sikken | 7e3a3f4 | 2011-09-08 18:31:15 +0000 | [diff] [blame] | 61 | extern void show_logo(void); |
Jonathan Gordon | ceb1e7a | 2007-03-22 12:55:51 +0000 | [diff] [blame] | 62 | |
| 63 | /* Button definitions */ |
| 64 | #if CONFIG_KEYPAD == IRIVER_H10_PAD |
| 65 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 66 | |
| 67 | #elif CONFIG_KEYPAD == SANSA_E200_PAD |
| 68 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 69 | |
Mark Arigo | dbc6b4e | 2007-09-06 03:28:58 +0000 | [diff] [blame] | 70 | #elif CONFIG_KEYPAD == SANSA_C200_PAD |
| 71 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 72 | |
Mark Arigo | e66ddd7 | 2008-01-09 07:24:43 +0000 | [diff] [blame] | 73 | #elif CONFIG_KEYPAD == MROBE100_PAD |
Robert Kukla | 6ef63a5 | 2008-03-05 00:17:21 +0000 | [diff] [blame] | 74 | #define BOOTLOADER_BOOT_OF BUTTON_POWER |
Mark Arigo | e66ddd7 | 2008-01-09 07:24:43 +0000 | [diff] [blame] | 75 | |
Mark Arigo | b4275d4 | 2008-05-21 03:55:17 +0000 | [diff] [blame] | 76 | #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD |
| 77 | #define BOOTLOADER_BOOT_OF BUTTON_VOL_UP |
Mark Arigo | 22e7bf3 | 2008-06-27 18:40:25 +0000 | [diff] [blame] | 78 | |
| 79 | #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD |
| 80 | #define BOOTLOADER_BOOT_OF BUTTON_MENU |
| 81 | |
Mark Arigo | 6908cc5 | 2009-12-25 04:05:01 +0000 | [diff] [blame] | 82 | #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD |
| 83 | #define BOOTLOADER_BOOT_OF BUTTON_VOL_UP |
| 84 | |
Sebastian Leonhardt | 7f7aee3 | 2014-05-22 23:34:52 +0200 | [diff] [blame] | 85 | #elif (CONFIG_KEYPAD == SAMSUNG_YH820_PAD) || \ |
Sebastian Leonhardt | e6cd53a | 2016-01-23 15:54:08 +0100 | [diff] [blame] | 86 | (CONFIG_KEYPAD == SAMSUNG_YH92X_PAD) |
Mark Arigo | 758bb3b | 2009-05-25 21:10:45 +0000 | [diff] [blame] | 87 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 88 | |
Robert Keevil | eea149b | 2009-07-13 21:09:39 +0000 | [diff] [blame] | 89 | #elif CONFIG_KEYPAD == SANSA_FUZE_PAD |
| 90 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 91 | |
Szymon Dziok | 492fafe | 2010-03-23 21:28:25 +0000 | [diff] [blame] | 92 | #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD |
| 93 | #define BOOTLOADER_BOOT_OF BUTTON_OK |
| 94 | |
Jonathan Gordon | ceb1e7a | 2007-03-22 12:55:51 +0000 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
| 98 | #define MAX_LOADSIZE (10*1024*1024) |
| 99 | |
| 100 | /* A buffer to load the original firmware or Rockbox into */ |
| 101 | unsigned char *loadbuffer = (unsigned char *)DRAM_START; |
| 102 | |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 103 | /* Locations and sizes in hidden partition on Sansa */ |
Frank Gevaerts | 19d1cac | 2008-10-31 21:25:04 +0000 | [diff] [blame] | 104 | #if (CONFIG_STORAGE & STORAGE_SD) |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 105 | #define PPMI_SECTOR_OFFSET 1024 |
| 106 | #define PPMI_SECTORS 1 |
| 107 | #define MI4_HEADER_SECTORS 1 |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 108 | #define NUM_PARTITIONS 2 |
| 109 | |
| 110 | #else |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 111 | #define NUM_PARTITIONS 1 |
| 112 | |
| 113 | #endif |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 114 | |
Barry Wardell | 7e03b0a | 2007-03-20 22:56:51 +0000 | [diff] [blame] | 115 | #define MI4_HEADER_SIZE 0x200 |
| 116 | |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 117 | /* PPMI header structure */ |
| 118 | struct ppmi_header_t { |
| 119 | unsigned char magic[4]; |
| 120 | uint32_t length; |
| 121 | uint32_t pad[126]; |
| 122 | }; |
| 123 | |
Frank Gevaerts | 19d1cac | 2008-10-31 21:25:04 +0000 | [diff] [blame] | 124 | #if (CONFIG_STORAGE & STORAGE_SD) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 125 | /* Load mi4 firmware from a hidden disk partition */ |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame] | 126 | int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, |
| 127 | unsigned int buffer_size, bool disable_rebuild) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 128 | { |
| 129 | struct mi4header_t mi4header; |
| 130 | struct ppmi_header_t ppmi_header; |
| 131 | unsigned long sum; |
| 132 | |
| 133 | /* Read header to find out how long the mi4 file is. */ |
Frank Gevaerts | 2dbafc1 | 2010-04-03 22:18:16 +0000 | [diff] [blame] | 134 | storage_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET, |
| 135 | PPMI_SECTORS, &ppmi_header); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 136 | |
| 137 | /* The first four characters at 0x80000 (sector 1024) should be PPMI*/ |
| 138 | if( memcmp(ppmi_header.magic, "PPMI", 4) ) |
| 139 | return EFILE_NOT_FOUND; |
| 140 | |
| 141 | printf("BL mi4 size: %x", ppmi_header.length); |
| 142 | |
| 143 | /* Read mi4 header of the OF */ |
Frank Gevaerts | 2dbafc1 | 2010-04-03 22:18:16 +0000 | [diff] [blame] | 144 | storage_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 145 | + (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 146 | |
| 147 | /* We don't support encrypted mi4 files yet */ |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 148 | if( (mi4header.plaintext) != (mi4header.mi4size-MI4_HEADER_SIZE)) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 149 | return EINVALID_FORMAT; |
| 150 | |
| 151 | /* MI4 file size */ |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 152 | printf("OF mi4 size: %x", mi4header.mi4size); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 153 | |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 154 | if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 155 | return EFILE_TOO_BIG; |
| 156 | |
| 157 | /* CRC32 */ |
| 158 | printf("CRC32: %x", mi4header.crc32); |
| 159 | |
| 160 | /* Rockbox model id */ |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 161 | printf("Model id: %.4s", mi4header.model); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 162 | |
| 163 | /* Read binary type (RBOS, RBBL) */ |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 164 | printf("Binary type: %.4s", mi4header.type); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 165 | |
| 166 | /* Load firmware */ |
Frank Gevaerts | 2dbafc1 | 2010-04-03 22:18:16 +0000 | [diff] [blame] | 167 | storage_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 168 | + (ppmi_header.length/512) + MI4_HEADER_SECTORS, |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 169 | (mi4header.mi4size-MI4_HEADER_SIZE)/512, buf); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 170 | |
| 171 | /* Check CRC32 to see if we have a valid file */ |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 172 | sum = chksum_crc32 (buf,mi4header.mi4size-MI4_HEADER_SIZE); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 173 | |
| 174 | printf("Calculated CRC32: %x", sum); |
| 175 | |
| 176 | if(sum != mi4header.crc32) |
| 177 | return EBAD_CHKSUM; |
| 178 | |
Barry Wardell | d46cf97 | 2007-10-20 17:08:41 +0000 | [diff] [blame] | 179 | #ifdef SANSA_E200 |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame] | 180 | if (disable_rebuild) |
| 181 | { |
| 182 | char block[512]; |
Barry Wardell | d46cf97 | 2007-10-20 17:08:41 +0000 | [diff] [blame] | 183 | |
| 184 | printf("Disabling database rebuild"); |
| 185 | |
Frank Gevaerts | 2dbafc1 | 2010-04-03 22:18:16 +0000 | [diff] [blame] | 186 | storage_read_sectors(pinfo->start + 0x3c08, 1, block); |
Barry Wardell | d46cf97 | 2007-10-20 17:08:41 +0000 | [diff] [blame] | 187 | block[0xe1] = 0; |
Frank Gevaerts | 2dbafc1 | 2010-04-03 22:18:16 +0000 | [diff] [blame] | 188 | storage_write_sectors(pinfo->start + 0x3c08, 1, block); |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame] | 189 | } |
Barry Wardell | 8b6bf8f | 2007-10-20 18:30:41 +0000 | [diff] [blame] | 190 | #else |
| 191 | (void) disable_rebuild; |
Barry Wardell | d46cf97 | 2007-10-20 17:08:41 +0000 | [diff] [blame] | 192 | #endif |
| 193 | |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 194 | return mi4header.mi4size-MI4_HEADER_SIZE; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 195 | } |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 196 | #endif /* (CONFIG_STORAGE & STORAGE_SD) */ |
| 197 | |
| 198 | #ifdef HAVE_BOOTLOADER_USB_MODE |
| 199 | /* Return USB_HANDLED if session took place else return USB_EXTRACTED */ |
| 200 | static int handle_usb(int connect_timeout) |
| 201 | { |
| 202 | static struct event_queue q SHAREDBSS_ATTR; |
| 203 | struct queue_event ev; |
| 204 | int usb = USB_EXTRACTED; |
| 205 | long end_tick = 0; |
| 206 | |
| 207 | if (!usb_plugged()) |
| 208 | return USB_EXTRACTED; |
| 209 | |
| 210 | queue_init(&q, true); |
| 211 | usb_init(); |
| 212 | usb_start_monitoring(); |
| 213 | |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 214 | printf("USB: Connecting"); |
| 215 | |
| 216 | if (connect_timeout != TIMEOUT_BLOCK) |
| 217 | end_tick = current_tick + connect_timeout; |
| 218 | |
| 219 | while (1) |
| 220 | { |
| 221 | /* Sleep no longer than 1/2s */ |
| 222 | queue_wait_w_tmo(&q, &ev, HZ/2); |
| 223 | |
| 224 | if (ev.id == SYS_USB_CONNECTED) |
| 225 | { |
Michael Sevakis | d0178ba | 2011-01-16 00:21:54 +0000 | [diff] [blame] | 226 | /* Switch to verbose mode if not in it so that the status updates |
| 227 | * are shown */ |
| 228 | verbose = true; |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 229 | /* Got the message - wait for disconnect */ |
| 230 | printf("Bootloader USB mode"); |
| 231 | |
| 232 | usb = USB_HANDLED; |
| 233 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 234 | usb_wait_for_disconnect(&q); |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | if (connect_timeout != TIMEOUT_BLOCK && |
| 239 | TIME_AFTER(current_tick, end_tick)) |
| 240 | { |
Michael Sevakis | d0178ba | 2011-01-16 00:21:54 +0000 | [diff] [blame] | 241 | /* Timed out waiting for the connect - will happen when connected |
| 242 | * to a charger instead of a host port and the charging pin is |
| 243 | * the same as the USB pin */ |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 244 | printf("USB: Timed out"); |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | if (!usb_plugged()) |
| 249 | break; /* Cable pulled */ |
| 250 | } |
| 251 | |
| 252 | usb_close(); |
| 253 | queue_delete(&q); |
| 254 | |
| 255 | return usb; |
| 256 | } |
Michael Sevakis | 71651ca | 2011-01-15 08:52:59 +0000 | [diff] [blame] | 257 | #elif (defined(SANSA_E200) || defined(SANSA_C200) || defined(PHILIPS_SA9200) \ |
Michael Sevakis | 5bf25bb | 2011-01-16 00:35:22 +0000 | [diff] [blame] | 258 | || defined (SANSA_VIEW)) && !defined(USE_ROCKBOX_USB) |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 259 | /* Return USB_INSERTED if cable present */ |
| 260 | static int handle_usb(int connect_timeout) |
| 261 | { |
| 262 | int usb_retry = 0; |
| 263 | int usb = USB_EXTRACTED; |
| 264 | |
| 265 | usb_init(); |
| 266 | while (usb_drv_powered() && usb_retry < 5 && usb != USB_INSERTED) |
| 267 | { |
| 268 | usb_retry++; |
| 269 | sleep(HZ/4); |
| 270 | usb = usb_detect(); |
| 271 | } |
| 272 | |
| 273 | if (usb != USB_INSERTED) |
| 274 | usb = USB_EXTRACTED; |
| 275 | |
| 276 | return usb; |
| 277 | (void)connect_timeout; |
| 278 | } |
Michael Sevakis | 71651ca | 2011-01-15 08:52:59 +0000 | [diff] [blame] | 279 | #else |
| 280 | /* Ignore cable state */ |
| 281 | static int handle_usb(int connect_timeout) |
| 282 | { |
| 283 | return USB_EXTRACTED; |
| 284 | (void)connect_timeout; |
| 285 | } |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 286 | #endif /* HAVE_BOOTLOADER_USB_MODE */ |
| 287 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 288 | void* main(void) |
| 289 | { |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 290 | char filename[MAX_PATH]; |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 291 | int i; |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 292 | int btn; |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 293 | int rc; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 294 | int num_partitions; |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 295 | struct partinfo pinfo; |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 296 | #if !(CONFIG_STORAGE & STORAGE_SD) |
Mark Arigo | dbc6b4e | 2007-09-06 03:28:58 +0000 | [diff] [blame] | 297 | char buf[256]; |
| 298 | unsigned short* identify_info; |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame] | 299 | #endif |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 300 | int usb = USB_EXTRACTED; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 301 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 302 | system_init(); |
| 303 | kernel_init(); |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 304 | |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 305 | #ifdef HAVE_BOOTLOADER_USB_MODE |
| 306 | /* loader must service interrupts */ |
| 307 | enable_interrupt(IRQ_FIQ_STATUS); |
| 308 | #endif |
| 309 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 310 | lcd_init(); |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 311 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 312 | font_init(); |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 313 | show_logo(); |
| 314 | |
Barry Wardell | 1ac260c | 2008-08-07 11:23:40 +0000 | [diff] [blame] | 315 | adc_init(); |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 316 | #ifdef HAVE_BOOTLOADER_USB_MODE |
| 317 | button_init_device(); |
| 318 | #else |
Barry Wardell | 2f16d4f | 2006-12-19 11:33:53 +0000 | [diff] [blame] | 319 | button_init(); |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 320 | #endif |
Mark Arigo | b4275d4 | 2008-05-21 03:55:17 +0000 | [diff] [blame] | 321 | #if defined(SANSA_E200) || defined(PHILIPS_SA9200) |
Barry Wardell | f1fead0 | 2007-10-14 18:20:23 +0000 | [diff] [blame] | 322 | i2c_init(); |
Marcin Bukat | 89ba7e8 | 2015-01-09 00:22:40 +0100 | [diff] [blame] | 323 | backlight_hw_on(); |
Barry Wardell | f1fead0 | 2007-10-14 18:20:23 +0000 | [diff] [blame] | 324 | #endif |
Mark Arigo | e66ddd7 | 2008-01-09 07:24:43 +0000 | [diff] [blame] | 325 | |
Jonathan Gordon | fc8e09d | 2007-11-19 02:07:40 +0000 | [diff] [blame] | 326 | if (button_hold()) |
| 327 | { |
| 328 | verbose = true; |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 329 | lcd_clear_display(); |
Jonathan Gordon | fc8e09d | 2007-11-19 02:07:40 +0000 | [diff] [blame] | 330 | printf("Hold switch on"); |
| 331 | printf("Shutting down..."); |
| 332 | sleep(HZ); |
| 333 | power_off(); |
| 334 | } |
Mark Arigo | b4275d4 | 2008-05-21 03:55:17 +0000 | [diff] [blame] | 335 | |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 336 | btn = button_read_device(); |
Dave Chapman | f0cde2d | 2008-03-27 00:31:51 +0000 | [diff] [blame] | 337 | |
| 338 | /* Enable bootloader messages if any button is pressed */ |
Michael Sevakis | d0178ba | 2011-01-16 00:21:54 +0000 | [diff] [blame] | 339 | #ifdef HAVE_BOOTLOADER_USB_MODE |
| 340 | lcd_clear_display(); |
| 341 | if (btn) |
| 342 | verbose = true; |
| 343 | #else |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 344 | if (btn) { |
| 345 | lcd_clear_display(); |
Dave Chapman | f0cde2d | 2008-03-27 00:31:51 +0000 | [diff] [blame] | 346 | verbose = true; |
Barry Wardell | 940091a | 2008-05-05 12:05:00 +0000 | [diff] [blame] | 347 | } |
Michael Sevakis | d0178ba | 2011-01-16 00:21:54 +0000 | [diff] [blame] | 348 | #endif |
Dave Chapman | f0cde2d | 2008-03-27 00:31:51 +0000 | [diff] [blame] | 349 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 350 | lcd_setfont(FONT_SYSFIXED); |
| 351 | |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 352 | printf("Rockbox boot loader"); |
Michael Sevakis | 95a4c3a | 2014-08-28 10:26:45 -0400 | [diff] [blame] | 353 | printf("Version: %s", rbversion); |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 354 | printf(MODEL_NAME); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 355 | |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 356 | i=storage_init(); |
Frank Gevaerts | 19d1cac | 2008-10-31 21:25:04 +0000 | [diff] [blame] | 357 | #if !(CONFIG_STORAGE & STORAGE_SD) |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 358 | if (i==0) { |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 359 | identify_info=ata_get_identify(); |
| 360 | /* Show model */ |
| 361 | for (i=0; i < 20; i++) { |
| 362 | ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]); |
| 363 | } |
| 364 | buf[40]=0; |
| 365 | for (i=39; i && buf[i]==' '; i--) { |
| 366 | buf[i]=0; |
| 367 | } |
| 368 | printf(buf); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 369 | } else { |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 370 | error(EATA, i, true); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 371 | } |
Michael Sevakis | 1167e3c | 2007-06-30 02:08:27 +0000 | [diff] [blame] | 372 | #endif |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 373 | |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 374 | filesystem_init(); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 375 | num_partitions = disk_mount_all(); |
| 376 | if (num_partitions<=0) |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 377 | { |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 378 | error(EDISK,num_partitions, true); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 381 | /* Just list the first 2 partitions since we don't have any devices yet |
| 382 | that have more than that */ |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 383 | for(i=0; i<NUM_PARTITIONS; i++) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 384 | { |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 385 | disk_partinfo(i, &pinfo); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 386 | printf("Partition %d: 0x%02x %ld MB", |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 387 | i, pinfo.type, pinfo.size / 2048); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 388 | } |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 389 | |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 390 | /* Now that storage is initialized, check for USB connection */ |
| 391 | if ((btn & BOOTLOADER_BOOT_OF) == 0) |
| 392 | { |
| 393 | usb_pin_init(); |
| 394 | usb = handle_usb(HZ*2); |
| 395 | if (usb == USB_INSERTED) |
| 396 | btn |= BOOTLOADER_BOOT_OF; |
| 397 | } |
| 398 | |
Nils Wallménius | 6ce98bd | 2009-03-17 20:05:03 +0000 | [diff] [blame] | 399 | /* Try loading Rockbox, if that fails, fall back to the OF */ |
| 400 | if((btn & BOOTLOADER_BOOT_OF) == 0) |
| 401 | { |
| 402 | printf("Loading Rockbox..."); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 403 | snprintf(filename,sizeof(filename), BOOTDIR "/%s", BOOTFILE); |
| 404 | |
| 405 | rc = load_mi4(loadbuffer, filename, MAX_LOADSIZE); |
| 406 | if (rc <= EFILE_EMPTY) |
Nils Wallménius | 6ce98bd | 2009-03-17 20:05:03 +0000 | [diff] [blame] | 407 | { |
| 408 | bool old_verbose = verbose; |
| 409 | verbose = true; |
| 410 | printf("Can't load " BOOTFILE ": "); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 411 | printf(loader_strerror(rc)); |
Nils Wallménius | 6ce98bd | 2009-03-17 20:05:03 +0000 | [diff] [blame] | 412 | verbose = old_verbose; |
| 413 | btn |= BOOTLOADER_BOOT_OF; |
| 414 | sleep(5*HZ); |
| 415 | } |
| 416 | else |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 417 | goto main_exit; |
Nils Wallménius | 6ce98bd | 2009-03-17 20:05:03 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Barry Wardell | a42070d | 2007-03-15 22:32:58 +0000 | [diff] [blame] | 420 | if(btn & BOOTLOADER_BOOT_OF) |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 421 | { |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 422 | /* Load original mi4 firmware in to a memory buffer called loadbuffer. |
| 423 | The rest of the loading is done in crt0.S. |
| 424 | 1) First try reading from the hidden partition (on Sansa only). |
| 425 | 2) Next try a decrypted mi4 file in /System/OF.mi4 |
Thomas Martitz | e2ccabf | 2009-09-08 13:57:13 +0000 | [diff] [blame] | 426 | 3) Finally, try a raw firmware binary in /System/OF.bin. It should be |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 427 | a mi4 firmware decrypted and header stripped using mi4code. |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 428 | */ |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 429 | printf("Loading original firmware..."); |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 430 | |
Frank Gevaerts | 19d1cac | 2008-10-31 21:25:04 +0000 | [diff] [blame] | 431 | #if (CONFIG_STORAGE & STORAGE_SD) |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 432 | /* First try a (hidden) firmware partition */ |
| 433 | printf("Trying firmware partition"); |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 434 | disk_partinfo(1, &pinfo); |
| 435 | if(pinfo.type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 436 | { |
Michael Sevakis | 7d1a47c | 2013-08-05 22:02:45 -0400 | [diff] [blame] | 437 | rc = load_mi4_part(loadbuffer, &pinfo, MAX_LOADSIZE, |
Michael Sevakis | 71651ca | 2011-01-15 08:52:59 +0000 | [diff] [blame] | 438 | usb == USB_INSERTED); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 439 | if (rc <= EFILE_EMPTY) { |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 440 | printf("Can't load from partition"); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 441 | printf(loader_strerror(rc)); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 442 | } else { |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 443 | goto main_exit; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 444 | } |
| 445 | } else { |
| 446 | printf("No hidden partition found."); |
| 447 | } |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 448 | #endif |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 449 | |
Michael Sevakis | 0afad89 | 2011-01-09 19:31:41 +0000 | [diff] [blame] | 450 | #if defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || defined(PHILIPS_SA9200) |
Mark Arigo | 3482a55 | 2009-01-10 03:21:07 +0000 | [diff] [blame] | 451 | printf("Trying /System/OF.ebn"); |
| 452 | rc=load_mi4(loadbuffer, "/System/OF.ebn", MAX_LOADSIZE); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 453 | if (rc <= EFILE_EMPTY) { |
Mark Arigo | 3482a55 | 2009-01-10 03:21:07 +0000 | [diff] [blame] | 454 | printf("Can't load /System/OF.ebn"); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 455 | printf(loader_strerror(rc)); |
Mark Arigo | 3482a55 | 2009-01-10 03:21:07 +0000 | [diff] [blame] | 456 | } else { |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 457 | goto main_exit; |
Mark Arigo | 3482a55 | 2009-01-10 03:21:07 +0000 | [diff] [blame] | 458 | } |
| 459 | #endif |
| 460 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 461 | printf("Trying /System/OF.mi4"); |
| 462 | rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 463 | if (rc <= EFILE_EMPTY) { |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 464 | printf("Can't load /System/OF.mi4"); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 465 | printf(loader_strerror(rc)); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 466 | } else { |
Thomas Martitz | e2ccabf | 2009-09-08 13:57:13 +0000 | [diff] [blame] | 467 | #if defined(SAMSUNG_YH925) |
| 468 | lcd_reset(); |
| 469 | #endif |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 470 | goto main_exit; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | printf("Trying /System/OF.bin"); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 474 | rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 475 | if (rc <= EFILE_EMPTY) { |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 476 | printf("Can't load /System/OF.bin"); |
Marcin Bukat | 0b29691 | 2012-03-04 15:34:29 +0100 | [diff] [blame] | 477 | printf(loader_strerror(rc)); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 478 | } else { |
Thomas Martitz | e2ccabf | 2009-09-08 13:57:13 +0000 | [diff] [blame] | 479 | #if defined(SAMSUNG_YH925) |
| 480 | lcd_reset(); |
| 481 | #endif |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 482 | goto main_exit; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 483 | } |
Barry Wardell | 0c1a304 | 2007-03-20 10:36:26 +0000 | [diff] [blame] | 484 | |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 485 | error(0, 0, true); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 486 | } |
Michael Sevakis | 3a11277 | 2011-01-15 08:19:30 +0000 | [diff] [blame] | 487 | |
| 488 | main_exit: |
| 489 | #ifdef HAVE_BOOTLOADER_USB_MODE |
| 490 | storage_close(); |
| 491 | system_prepare_fw_start(); |
| 492 | #endif |
| 493 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 494 | return (void*)loadbuffer; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 495 | } |