text viewer: reworks screen access logics and some bugs fix.
[kugel-rb.git] / utils / MTP / beastpatcher / main.c
blob97d931e4545df9f47015708940564dac89d2d609
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;
108 i = 1;
109 while(i < argc) {
110 if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
111 action = HELP;
113 else if(strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--install") == 0) {
114 action = INSTALL;
115 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
116 bootloader = argv[++i];
118 #ifndef WITH_BOOTOBJS
119 else {
120 action = NONE;
122 #endif
124 else if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--dual-boot") == 0) {
125 action = DUALBOOT;
126 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
127 firmware = argv[++i];
128 #ifndef WITH_BOOTOBJS
129 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
130 bootloader = argv[++i];
132 #endif
134 else {
135 action = NONE;
138 else if(((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--send") == 0)
139 && (i + 1) < argc)) {
140 action = SEND;
141 firmware = argv[++i];
143 i++;
146 if(action == NONE) {
147 print_usage();
148 res = -1;
150 else if(action == HELP) {
151 print_usage();
152 res = 0;
154 else if(action == SEND) {
155 res = sendfirm(firmware);
157 else if(action == DUALBOOT) {
158 res = beastpatcher(bootloader, firmware, interactive);
160 else if(action == INSTALL) {
161 res = beastpatcher(bootloader, NULL, interactive);
162 /* don't ask for enter if started with command line arguments */
163 if(interactive) {
164 printf("\nPress ENTER to exit beastpatcher: ");
165 fgets(yesno,4,stdin);
168 return res;