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