Major W32 beastpatcher rework (FS#10220).
[kugel-rb.git] / utils / MTP / beastpatcher / beastpatcher.c
blob783b11f202c730887cf5d11c2b6eb2cf6e31a718
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * $Id$
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
16 * met:
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 ****************************************************************************/
40 #include <stdio.h>
41 #if !defined(_MSC_VER)
42 #include <unistd.h>
43 #include <fcntl.h>
44 #endif
45 #include <string.h>
46 #include <stdlib.h>
47 #if !defined(_MSC_VER)
48 #include <inttypes.h>
49 #else
50 #include "pstdint.h"
51 #endif
52 #include <sys/types.h>
53 #include <sys/stat.h>
55 #include "mtp_common.h"
56 #include "bootimg.h"
58 #define VERSION "1.0 with v1 bootloader"
60 void print_usage(void)
62 fprintf(stderr,"Usage: beastpatcher [action]\n");
63 fprintf(stderr,"\n");
64 fprintf(stderr,"Where [action] is one of the following options:\n");
65 fprintf(stderr," --install (default)\n");
66 fprintf(stderr," -?, --help\n");
67 fprintf(stderr,"\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)
87 uint32_t l = 0;
88 while (cb--)
89 l += *pb++;
90 return l;
93 static void create_single_boot(unsigned char* boot, int bootlen,
94 unsigned char** fwbuf, int* fwsize)
96 unsigned char* buf;
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);
103 if(fwbuf == NULL) {
104 fprintf(stderr, "[ERR] Cannot allocate memory.\n" );
105 *fwbuf = NULL;
106 *fwsize = 0;
107 return;
110 buf = *fwbuf;
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);
139 return;
142 int beastpatcher(int argc, char* argv[])
144 char yesno[4];
145 unsigned char* fwbuf;
146 int fwsize;
147 struct mtp_info_t mtp_info;
149 (void)argv;
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
156 provided. */
158 if (argc > 1) {
159 print_usage();
160 return 1;
163 if (mtp_init(&mtp_info) < 0) {
164 fprintf(stderr,"[ERR] Can not init MTP\n");
165 return 1;
168 /* Scan for attached MTP devices. */
169 if (mtp_scan(&mtp_info) < 0)
171 fprintf(stderr,"[ERR] No devices found\n");
172 return 1;
175 printf("[INFO] Found device \"%s - %s\"\n", mtp_info.manufacturer,
176 mtp_info.modelname);
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))
184 if (yesno[0]=='i')
186 /* Create a single-boot bootloader from the embedded bootloader */
187 create_single_boot(bootimg, LEN_bootimg, &fwbuf, &fwsize);
189 if (fwbuf == NULL)
190 return 1;
192 if (mtp_send_firmware(&mtp_info, fwbuf, fwsize) == 0)
194 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
196 else
198 fprintf(stderr,"[ERR] Bootloader install failed.\n");
201 /* We are now done with the firmware image */
202 free(fwbuf);
204 else
206 fprintf(stderr,"[INFO] Installation cancelled.\n");
210 mtp_finished(&mtp_info);
212 return 0;
216 int main(int argc, char* argv[])
218 int res;
219 char yesno[4];
221 res = beastpatcher(argc, argv);
223 printf("\nPress ENTER to exit beastpatcher: ");
224 fgets(yesno,4,stdin);
226 return res;