1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (c) 2009, Dave Chapman
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
18 * * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ****************************************************************************/
41 #if !defined(_MSC_VER)
47 #if !defined(_MSC_VER)
52 #include <sys/types.h>
55 #include "mtp_common.h"
58 #define VERSION "1.0 with v1 bootloader"
60 void print_usage(void)
62 fprintf(stderr
,"Usage: beastpatcher [action]\n");
64 fprintf(stderr
,"Where [action] is one of the following options:\n");
65 fprintf(stderr
," --install (default)\n");
66 fprintf(stderr
," -?, --help\n");
70 /* Code to create a single-boot bootloader.
71 Based on tools/gigabeats.c by Will Robertson.
74 /* Entry point (and load address) for the main Rockbox bootloader */
75 #define BL_ENTRY_POINT 0x8a000000
77 static void put_uint32le(uint32_t x
, unsigned char* p
)
79 p
[0] = (unsigned char)(x
& 0xff);
80 p
[1] = (unsigned char)((x
>> 8) & 0xff);
81 p
[2] = (unsigned char)((x
>> 16) & 0xff);
82 p
[3] = (unsigned char)((x
>> 24) & 0xff);
85 static uint32_t calc_csum(const unsigned char* pb
, int cb
)
93 static void create_single_boot(unsigned char* boot
, int bootlen
,
94 unsigned char** fwbuf
, int* fwsize
)
98 /* 15 bytes for header, 16 for signature bypass,
99 * 12 for record header, size of bootloader, 12 for footer */
100 *fwsize
= 15 + 16 + 12 + bootlen
+ 12;
101 *fwbuf
= malloc(*fwsize
);
104 fprintf(stderr
, "[ERR] Cannot allocate memory.\n" );
112 /* Copy bootloader image. */
113 memcpy(buf
+ 43, boot
, bootlen
);
115 /* Step 2: Create the file header */
116 sprintf((char *)buf
, "B000FF\n");
117 put_uint32le(0x88200000, buf
+7);
119 /* If the value below is too small, the update will attempt to flash.
120 * Be careful when changing this (leaving it as is won't cause issues) */
121 put_uint32le(0xCC0CD8, buf
+11);
123 /* Step 3: Add the signature bypass record */
124 put_uint32le(0x88065A10, buf
+15);
125 put_uint32le(4, buf
+19);
126 put_uint32le(0xE3A00001, buf
+27);
127 put_uint32le(calc_csum(buf
+27,4), buf
+23);
129 /* Step 4: Create a record for the actual code */
130 put_uint32le(BL_ENTRY_POINT
, buf
+31);
131 put_uint32le(bootlen
, buf
+35);
132 put_uint32le(calc_csum(buf
+ 43, bootlen
), buf
+39);
134 /* Step 5: Write the footer */
135 put_uint32le(0, buf
+*fwsize
-12);
136 put_uint32le(BL_ENTRY_POINT
, buf
+*fwsize
-8);
137 put_uint32le(0, buf
+*fwsize
-4);
142 int beastpatcher(int argc
, char* argv
[])
145 unsigned char* fwbuf
;
147 struct mtp_info_t mtp_info
;
151 fprintf(stderr
,"beastpatcher v" VERSION
" - (C) 2009 by the Rockbox developers\n");
152 fprintf(stderr
,"This is free software; see the source for copying conditions. There is NO\n");
153 fprintf(stderr
,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
155 /* No options are currently implemented, so just display help if any are
163 if (mtp_init(&mtp_info
) < 0) {
164 fprintf(stderr
,"[ERR] Can not init MTP\n");
168 /* Scan for attached MTP devices. */
169 if (mtp_scan(&mtp_info
) < 0)
171 fprintf(stderr
,"[ERR] No devices found\n");
175 printf("[INFO] Found device \"%s - %s\"\n", mtp_info
.manufacturer
,
177 printf("[INFO] Device version: \"%s\"\n",mtp_info
.version
);
180 printf("\nEnter i to install the Rockbox bootloader or c to cancel and do nothing (i/c): ");
182 if (fgets(yesno
,4,stdin
))
186 /* Create a single-boot bootloader from the embedded bootloader */
187 create_single_boot(bootimg
, LEN_bootimg
, &fwbuf
, &fwsize
);
192 if (mtp_send_firmware(&mtp_info
, fwbuf
, fwsize
) == 0)
194 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
198 fprintf(stderr
,"[ERR] Bootloader install failed.\n");
201 /* We are now done with the firmware image */
206 fprintf(stderr
,"[INFO] Installation cancelled.\n");
210 mtp_finished(&mtp_info
);
216 int main(int argc
, char* argv
[])
221 res
= beastpatcher(argc
, argv
);
223 printf("\nPress ENTER to exit beastpatcher: ");
224 fgets(yesno
,4,stdin
);