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