Sansa AMS bootloader: enter USB mode only when needed
[kugel-rb.git] / bootloader / main-e200r-installer.c
blob178a03b42e4f972c9c331bd6ddd0bf33e4a9e132
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Barry Wardell
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
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.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
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 "storage.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"
41 #include "power.h"
42 #include "version.h"
44 #define START_SECTOR_OF_ROM 1
45 #define ROMSECTOR_TO_HACK 63
46 #define HACK_OFFSET 498
47 #define KNOWN_CRC32 0x5a09c266 /* E200R CRC before patching */
48 #define PATCHED_CRC32 0x0a162b34 /* E200R CRC after patching */
50 static unsigned char knownBytes[] = {0x00, 0x24, 0x07, 0xe1};
51 static unsigned char changedBytes[] = {0xc0, 0x46, 0xc0, 0x46 };
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.
57 These are all known E200 bootloaders as of 8 November 2007.
61 static uint32_t e200_crcs[] =
63 0xbeceba58,
64 0x4e6b038f,
65 0x5e4f4219,
66 0xae087742,
67 0x3dd94852,
68 0x72fa69f3,
69 0x4ce0d10b
72 #define NUM_E200_CRCS ((int)((sizeof(e200_crcs) / sizeof(uint32_t))))
74 static bool is_e200(uint32_t crc)
76 int i;
78 for (i = 0 ; i < NUM_E200_CRCS ; i++)
80 if (crc == e200_crcs[i])
81 return true;
84 return false;
88 void* main(void)
90 int i;
91 int btn;
92 int num_partitions;
93 int crc32;
94 char sector[512];
95 struct partinfo* pinfo;
97 chksum_crc32gentab ();
99 system_init();
100 kernel_init();
101 lcd_init();
102 font_init();
103 button_init();
104 i2c_init();
105 _backlight_on();
107 lcd_set_foreground(LCD_WHITE);
108 lcd_set_background(LCD_BLACK);
109 lcd_clear_display();
111 btn = button_read_device();
112 verbose = true;
114 lcd_setfont(FONT_SYSFIXED);
116 printf("Rockbox e200R installer");
117 printf("Version: " RBVERSION);
118 printf(MODEL_NAME);
119 printf("");
121 i=storage_init();
122 disk_init(IF_MV(0));
123 num_partitions = disk_mount_all();
125 if (num_partitions<=0)
127 error(EDISK, num_partitions, true);
130 pinfo = disk_partinfo(1);
132 #if 0 /* not needed in release builds */
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);
138 #endif
140 storage_read_sectors(pinfo->start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK,
141 1 , sector);
142 crc32 = chksum_crc32 (sector, 512);
144 #if 0 /* not needed in release builds */
145 printf("--- Hack Status ---");
146 printf("Sector checksum: %x", crc32);
147 #endif
149 if (crc32 == PATCHED_CRC32)
151 /* Bootloader already patched */
152 printf("Already unlocked");
153 printf("Proceed to Step 2");
154 } else if ((crc32 == KNOWN_CRC32) &&
155 !memcmp(&sector[HACK_OFFSET], knownBytes,
156 sizeof(knownBytes)/sizeof(*knownBytes)))
158 /* E200R bootloader detected - patch it */
159 memcpy(&sector[HACK_OFFSET], changedBytes,
160 sizeof(changedBytes)/sizeof(*changedBytes));
161 storage_write_sectors(
162 pinfo->start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK,
163 1 , sector);
164 printf("Firmware unlocked");
165 printf("Proceed to Step 2");
166 } else if (is_e200(crc32))
168 printf("Vanilla E200 detected!");
169 printf("Please install using");
170 printf("Sansapatcher");
172 else
174 printf("Unknown bootloader");
175 printf("Rockbox installer cannot");
176 printf("continue");
179 /* Turn button lights off */
180 GPIOG_OUTPUT_VAL &=~0x80;
182 printf("");
184 if (button_hold())
185 printf("Release Hold and");
187 printf("Press any key to shutdown");
189 while(button_read_device() == BUTTON_NONE);
191 power_off();
193 return NULL;