blob: 5d1bff7b371e98767071d52f1913e6b0751f7cc6 [file] [log] [blame]
Jonathan Gordon9db22ef2007-09-09 11:20:20 +00001/***************************************************************************
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 Stenberg2acc0ac2008-06-28 18:10:04 +000015 * 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 Gordon9db22ef2007-09-09 11:20:20 +000019 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
Dave Chapman82a1f8e2007-11-08 20:37:16 +000024
Jonathan Gordon9db22ef2007-09-09 11:20:20 +000025#include <stdio.h>
26#include <stdlib.h>
27#include "common.h"
28#include "cpu.h"
Jonathan Gordon9db22ef2007-09-09 11:20:20 +000029#include "system.h"
Michael Sevakis4ea4cdf2014-08-08 02:28:11 -040030#include "../kernel-internal.h"
Jonathan Gordon9db22ef2007-09-09 11:20:20 +000031#include "lcd.h"
32#include "font.h"
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000033#include "storage.h"
Michael Sevakis7d1a47c2013-08-05 22:02:45 -040034#include "file_internal.h"
35#include "file.h"
Jonathan Gordon9db22ef2007-09-09 11:20:20 +000036#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 Gordond777a362007-10-14 11:16:20 +000042#include "power.h"
Rafaël Carré5d236b22010-05-27 09:41:46 +000043#include "version.h"
Jonathan Gordon9db22ef2007-09-09 11:20:20 +000044
45#define START_SECTOR_OF_ROM 1
46#define ROMSECTOR_TO_HACK 63
47#define HACK_OFFSET 498
Dave Chapman82a1f8e2007-11-08 20:37:16 +000048#define KNOWN_CRC32 0x5a09c266 /* E200R CRC before patching */
49#define PATCHED_CRC32 0x0a162b34 /* E200R CRC after patching */
50
51static unsigned char knownBytes[] = {0x00, 0x24, 0x07, 0xe1};
52static 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
62static 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
75static 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 Gordon9db22ef2007-09-09 11:20:20 +000089void* main(void)
90{
91 int i;
92 int btn;
93 int num_partitions;
94 int crc32;
95 char sector[512];
Michael Sevakis7d1a47c2013-08-05 22:02:45 -040096 struct partinfo pinfo;
Dave Chapman82a1f8e2007-11-08 20:37:16 +000097
Jonathan Gordon9db22ef2007-09-09 11:20:20 +000098 system_init();
99 kernel_init();
100 lcd_init();
101 font_init();
102 button_init();
103 i2c_init();
Marcin Bukat89ba7e82015-01-09 00:22:40 +0100104 backlight_hw_on();
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000105
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 Sevakis95a4c3a2014-08-28 10:26:45 -0400116 printf("Version: %s", rbversion);
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000117 printf(MODEL_NAME);
Jonathan Gordond777a362007-10-14 11:16:20 +0000118 printf("");
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000119
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000120 i=storage_init();
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400121 filesystem_init();
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000122 num_partitions = disk_mount_all();
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000123
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000124 if (num_partitions<=0)
125 {
Rafaël Carré1ec82122010-06-23 05:08:36 +0000126 error(EDISK, num_partitions, true);
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000127 }
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000128
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400129 disk_partinfo(1, &pinfo);
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000130
Jonathan Gordond777a362007-10-14 11:16:20 +0000131#if 0 /* not needed in release builds */
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000132 printf("--- Partition info ---");
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400133 printf("start: %x", pinfo.start);
134 printf("size: %x", pinfo.size);
135 printf("type: %x", pinfo.type);
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000136 printf("reading: %x", (START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK)*512);
Jonathan Gordond777a362007-10-14 11:16:20 +0000137#endif
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000138
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400139 storage_read_sectors(pinfo.start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK,
Jonathan Gordon76e662b2008-11-24 04:43:31 +0000140 1 , sector);
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000141 crc32 = chksum_crc32 (sector, 512);
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000142
Jonathan Gordond777a362007-10-14 11:16:20 +0000143#if 0 /* not needed in release builds */
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000144 printf("--- Hack Status ---");
145 printf("Sector checksum: %x", crc32);
Jonathan Gordond777a362007-10-14 11:16:20 +0000146#endif
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000147
148 if (crc32 == PATCHED_CRC32)
149 {
150 /* Bootloader already patched */
Dave Chapmaneaed7852007-11-09 08:37:14 +0000151 printf("Already unlocked");
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000152 printf("Proceed to Step 2");
153 } else if ((crc32 == KNOWN_CRC32) &&
154 !memcmp(&sector[HACK_OFFSET], knownBytes,
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000155 sizeof(knownBytes)/sizeof(*knownBytes)))
156 {
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000157 /* E200R bootloader detected - patch it */
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000158 memcpy(&sector[HACK_OFFSET], changedBytes,
159 sizeof(changedBytes)/sizeof(*changedBytes));
Frank Gevaerts2dbafc12010-04-03 22:18:16 +0000160 storage_write_sectors(
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400161 pinfo.start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK,
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000162 1 , sector);
Dave Chapmaneaed7852007-11-09 08:37:14 +0000163 printf("Firmware unlocked");
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000164 printf("Proceed to Step 2");
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000165 } else if (is_e200(crc32))
166 {
167 printf("Vanilla E200 detected!");
168 printf("Please install using");
169 printf("Sansapatcher");
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000170 }
171 else
Jonathan Gordond777a362007-10-14 11:16:20 +0000172 {
173 printf("Unknown bootloader");
174 printf("Rockbox installer cannot");
175 printf("continue");
176 }
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000177
178 /* Turn button lights off */
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000179 GPIOG_OUTPUT_VAL &=~0x80;
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000180
Jonathan Gordond777a362007-10-14 11:16:20 +0000181 printf("");
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000182
Jonathan Gordond777a362007-10-14 11:16:20 +0000183 if (button_hold())
184 printf("Release Hold and");
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000185
Jonathan Gordond777a362007-10-14 11:16:20 +0000186 printf("Press any key to shutdown");
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000187
188 while(button_read_device() == BUTTON_NONE);
189
Jonathan Gordond777a362007-10-14 11:16:20 +0000190 power_off();
Dave Chapman82a1f8e2007-11-08 20:37:16 +0000191
Jonathan Gordon9db22ef2007-09-09 11:20:20 +0000192 return NULL;
193}