Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +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 | * |
| 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. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ****************************************************************************/ |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 22 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include "common.h" |
| 26 | #include "cpu.h" |
| 27 | #include "file.h" |
| 28 | #include "system.h" |
| 29 | #include "kernel.h" |
| 30 | #include "lcd.h" |
| 31 | #include "font.h" |
| 32 | #include "ata.h" |
| 33 | #include "button.h" |
| 34 | #include "disk.h" |
| 35 | #include "crc32-mi4.h" |
| 36 | #include <string.h> |
| 37 | #include "i2c.h" |
| 38 | #include "backlight-target.h" |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 39 | #include "power.h" |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 40 | |
| 41 | /* Bootloader version */ |
| 42 | char version[] = APPSVERSION; |
| 43 | |
| 44 | #define START_SECTOR_OF_ROM 1 |
| 45 | #define ROMSECTOR_TO_HACK 63 |
| 46 | #define HACK_OFFSET 498 |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 47 | #define KNOWN_CRC32 0x5a09c266 /* E200R CRC before patching */ |
| 48 | #define PATCHED_CRC32 0x0a162b34 /* E200R CRC after patching */ |
| 49 | |
| 50 | static unsigned char knownBytes[] = {0x00, 0x24, 0x07, 0xe1}; |
| 51 | static unsigned char changedBytes[] = {0xc0, 0x46, 0xc0, 0x46 }; |
| 52 | |
| 53 | /* |
| 54 | CRC32s of sector 63 from E200 bootloaders - so we can tell users if they're |
| 55 | trying to use e200rpatcher with a vanilla e200. |
| 56 | |
| 57 | These are all known E200 bootloaders as of 8 November 2007. |
| 58 | |
| 59 | */ |
| 60 | |
| 61 | static uint32_t e200_crcs[] = |
| 62 | { |
| 63 | 0xbeceba58, |
| 64 | 0x4e6b038f, |
| 65 | 0x5e4f4219, |
| 66 | 0xae087742, |
| 67 | 0x3dd94852, |
| 68 | 0x72fa69f3, |
| 69 | 0x4ce0d10b |
| 70 | }; |
| 71 | |
| 72 | #define NUM_E200_CRCS ((int)((sizeof(e200_crcs) / sizeof(uint32_t)))) |
| 73 | |
| 74 | static bool is_e200(uint32_t crc) |
| 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0 ; i < NUM_E200_CRCS ; i++) |
| 79 | { |
| 80 | if (crc == e200_crcs[i]) |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 88 | void* main(void) |
| 89 | { |
| 90 | int i; |
| 91 | int btn; |
| 92 | int num_partitions; |
| 93 | int crc32; |
| 94 | char sector[512]; |
| 95 | struct partinfo* pinfo; |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 96 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 97 | chksum_crc32gentab (); |
| 98 | |
| 99 | system_init(); |
| 100 | kernel_init(); |
| 101 | lcd_init(); |
| 102 | font_init(); |
| 103 | button_init(); |
| 104 | i2c_init(); |
Jens Arnold | ef12b3b | 2007-11-12 18:49:53 +0000 | [diff] [blame] | 105 | _backlight_on(); |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 106 | |
| 107 | lcd_set_foreground(LCD_WHITE); |
| 108 | lcd_set_background(LCD_BLACK); |
| 109 | lcd_clear_display(); |
| 110 | |
| 111 | btn = button_read_device(); |
| 112 | verbose = true; |
| 113 | |
| 114 | lcd_setfont(FONT_SYSFIXED); |
| 115 | |
| 116 | printf("Rockbox e200R installer"); |
| 117 | printf("Version: %s", version); |
| 118 | printf(MODEL_NAME); |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 119 | printf(""); |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 120 | |
| 121 | i=ata_init(); |
| 122 | disk_init(IF_MV(0)); |
| 123 | num_partitions = disk_mount_all(); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 124 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 125 | if (num_partitions<=0) |
| 126 | { |
| 127 | error(EDISK,num_partitions); |
| 128 | } |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 129 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 130 | pinfo = disk_partinfo(1); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 131 | |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 132 | #if 0 /* not needed in release builds */ |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 133 | printf("--- Partition info ---"); |
| 134 | printf("start: %x", pinfo->start); |
| 135 | printf("size: %x", pinfo->size); |
| 136 | printf("type: %x", pinfo->type); |
| 137 | printf("reading: %x", (START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK)*512); |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 138 | #endif |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 139 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 140 | ata_read_sectors(IF_MV2(0,) |
| 141 | pinfo->start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK, |
| 142 | 1 , sector); |
| 143 | crc32 = chksum_crc32 (sector, 512); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 144 | |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 145 | #if 0 /* not needed in release builds */ |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 146 | printf("--- Hack Status ---"); |
| 147 | printf("Sector checksum: %x", crc32); |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 148 | #endif |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 149 | |
| 150 | if (crc32 == PATCHED_CRC32) |
| 151 | { |
| 152 | /* Bootloader already patched */ |
Dave Chapman | eaed785 | 2007-11-09 08:37:14 +0000 | [diff] [blame] | 153 | printf("Already unlocked"); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 154 | printf("Proceed to Step 2"); |
| 155 | } else if ((crc32 == KNOWN_CRC32) && |
| 156 | !memcmp(§or[HACK_OFFSET], knownBytes, |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 157 | sizeof(knownBytes)/sizeof(*knownBytes))) |
| 158 | { |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 159 | /* E200R bootloader detected - patch it */ |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 160 | memcpy(§or[HACK_OFFSET], changedBytes, |
| 161 | sizeof(changedBytes)/sizeof(*changedBytes)); |
| 162 | ata_write_sectors(IF_MV2(0,) |
| 163 | pinfo->start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK, |
| 164 | 1 , sector); |
Dave Chapman | eaed785 | 2007-11-09 08:37:14 +0000 | [diff] [blame] | 165 | printf("Firmware unlocked"); |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 166 | printf("Proceed to Step 2"); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 167 | } else if (is_e200(crc32)) |
| 168 | { |
| 169 | printf("Vanilla E200 detected!"); |
| 170 | printf("Please install using"); |
| 171 | printf("Sansapatcher"); |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 172 | } |
| 173 | else |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 174 | { |
| 175 | printf("Unknown bootloader"); |
| 176 | printf("Rockbox installer cannot"); |
| 177 | printf("continue"); |
| 178 | } |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 179 | |
| 180 | /* Turn button lights off */ |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 181 | GPIOG_OUTPUT_VAL &=~0x80; |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 182 | |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 183 | printf(""); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 184 | |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 185 | if (button_hold()) |
| 186 | printf("Release Hold and"); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 187 | |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 188 | printf("Press any key to shutdown"); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 189 | |
| 190 | while(button_read_device() == BUTTON_NONE); |
| 191 | |
Jonathan Gordon | d777a36 | 2007-10-14 11:16:20 +0000 | [diff] [blame] | 192 | power_off(); |
Dave Chapman | 82a1f8e | 2007-11-08 20:37:16 +0000 | [diff] [blame] | 193 | |
Jonathan Gordon | 9db22ef | 2007-09-09 11:20:20 +0000 | [diff] [blame] | 194 | return NULL; |
| 195 | } |