Fix FS#12824 : Malfunctioning FFT plugin in Sansa Clip Zip
[maemo-rb.git] / utils / MTP / beastpatcher / main.c
blob3f7957a8c2152b31d668cd5ccc67a6093b6ee95c
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 #include <string.h>
42 #include "beastpatcher.h"
43 #include "mtp_common.h"
45 #if defined(__WIN32__) || defined(_WIN32)
46 #include <windows.h>
47 #include "../MTP_DLL/MTP_DLL.h"
48 #endif
50 #ifdef WITH_BOOTOBJS
51 #define VERSION "1.0 with v1 bootloader"
52 #else
53 #define VERSION "1.0"
54 #endif
56 enum actions {
57 NONE,
58 INSTALL,
59 DUALBOOT,
60 SEND,
61 HELP
64 static void print_usage(void)
66 fprintf(stderr,"Usage: beastpatcher [action]\n");
67 fprintf(stderr,"\n");
68 fprintf(stderr,"Where [action] is one of the following options:\n");
69 #ifdef WITH_BOOTOBJS
70 fprintf(stderr," -i, --install [bootloader.bin]\n");
71 fprintf(stderr," -d, --dual-boot <nk.bin> [bootloader.bin]\n");
72 #else
73 fprintf(stderr," -i, --install <bootloader.bin>\n");
74 fprintf(stderr," -d --dual-boot <nk.bin> <bootloader.bin>\n");
75 #endif
76 fprintf(stderr," -s, --send <nk.bin>\n");
77 fprintf(stderr," -h, --help\n");
78 fprintf(stderr,"\n");
79 #ifdef WITH_BOOTOBJS
80 fprintf(stderr,"With bootloader file omitted the embedded one will be used.\n");
81 fprintf(stderr,"\n");
82 #endif
86 int main(int argc, char* argv[])
88 int res = 0;
89 char yesno[4];
90 int i;
91 char* bootloader = NULL;
92 char* firmware = NULL;
93 #ifdef WITH_BOOTOBJS
94 enum actions action = INSTALL;
95 int interactive = 1;
96 #else
97 enum actions action = NONE;
98 int interactive = 0;
99 #endif
101 fprintf(stderr,"beastpatcher v" VERSION " - (C) 2009 by the Rockbox developers\n");
102 fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n");
103 fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
104 if(argc > 1) {
105 interactive = 0;
107 #if defined(__WIN32__) || defined(_WIN32)
108 if(mtp_wmp_version() < 11) {
109 fprintf(stderr, "beastpacher requires at least Windows Media Player 11 to run!\n");
110 fprintf(stderr, "Please update your installation of Windows Media Player.\n");
111 return -1;
113 #endif
115 i = 1;
116 while(i < argc) {
117 if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
118 action = HELP;
120 else if(strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--install") == 0) {
121 action = INSTALL;
122 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
123 bootloader = argv[++i];
125 #ifndef WITH_BOOTOBJS
126 else {
127 action = NONE;
129 #endif
131 else if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--dual-boot") == 0) {
132 action = DUALBOOT;
133 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
134 firmware = argv[++i];
135 #ifndef WITH_BOOTOBJS
136 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
137 bootloader = argv[++i];
139 #endif
141 else {
142 action = NONE;
145 else if(((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--send") == 0)
146 && (i + 1) < argc)) {
147 action = SEND;
148 firmware = argv[++i];
150 i++;
153 if(action == NONE) {
154 print_usage();
155 res = -1;
157 else if(action == HELP) {
158 print_usage();
159 res = 0;
161 else if(action == SEND) {
162 res = sendfirm(firmware);
164 else if(action == DUALBOOT) {
165 res = beastpatcher(bootloader, firmware, interactive);
167 else if(action == INSTALL) {
168 res = beastpatcher(bootloader, NULL, interactive);
169 /* don't ask for enter if started with command line arguments */
170 if(interactive) {
171 printf("\nPress ENTER to exit beastpatcher: ");
172 fgets(yesno,4,stdin);
175 return res;