summaryrefslogtreecommitdiffstats
path: root/heimdall/source/InterfaceManager.cpp
blob: 26ef0288be967f4cf7439ea7ad2039d0f5fb7eb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* Copyright (c) 2010 Benjamin Dobell, Glass Echidna
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.*/

// C/C++ Standard Library
#include <cstdarg>
#include <cstdlib>
#include <stdio.h>

// Heimdall
#include "Heimdall.h"
#include "InterfaceManager.h"

using namespace std;
using namespace Heimdall;

string InterfaceManager::actionNames[kActionCount] = { "flash", "close-pc-screen", "dump", "print-pit", "help" };

string InterfaceManager::flashArgumentNames[kFlashArgCount * 2] = {
	// --- Long Names ---
	"-repartition",

	"-pit", "-factoryfs", "-cache", "-dbdata", "-primary-boot",	"-secondary-boot", "-secondary-boot-backup", "-param", "-kernel", "-recovery", "-efs", "-modem",
	"-normal-boot", "-system", "-user-data", "-fota", "-hidden", "-movinand", "-data", "-ums", "-emmc", "-%d",

	// --- Short Names ---
	"r",

	"pit",  "fs",         "cache",  "db",      "boot",           "sbl",            "sbl2",                   "param",  "z",       "rec",       "efs",  "m",
	"norm",         "sys",     "udata",      "fota",  "hide",    "nand",      "data",  "ums",  "emmc",  "%d"
};

string InterfaceManager::dumpArgumentNames[kDumpArgCount * 2] = {
	// --- Long Names ---
	"-chip-type", "-chip-id", "-output",

	// --- Short Names ---
	"type",       "id",       "out"
};

string InterfaceManager::commonArgumentNames[kCommonArgCount * 2] = {
	// --- Long Names ---
	"-verbose", "-no-reboot",

	"-delay",

	// --- Short Names ---
	"v",        "nobt",

	"d"
};

// argumentNames[kActionCount][] stores common arguments. 
string *InterfaceManager::actionArgumentNames[kActionCount + 1] = {
	// kActionFlash
	flashArgumentNames,

	// kActionClosePcScreen
	nullptr,

	// kActionDump
	dumpArgumentNames,

	// kActionPrintPit
	nullptr,

	// kActionHelp
	nullptr,

	// Common (kActionCount)
	commonArgumentNames
};

int InterfaceManager::actionArgumentCounts[kActionCount + 1] = {
	kFlashArgCount, 0, kDumpArgCount, 0, 0, kCommonArgCount
};

int InterfaceManager::actionValuelessArgumentCounts[kActionCount + 1] = {
	kFlashArgPit, 0, kDumpArgChipType, 0, 0, kCommonArgDelay
};

const char *InterfaceManager::usage = "\nHeimdall v1.2.0, Copyright (c) 2010-2011, Benjamin Dobell, Glass Echidna\n\n\
Usage: heimdall <action> <arguments> [--verbose] [--no-reboot] [--delay <ms>]\n\
\n\
action: flash\n\
arguments:\n\
    --repartition --pit <filename> [--factoryfs <filename>]\n\
    [--cache <filename>] [--dbdata <filename>] [--primary-boot <filename>]\n\
    [--secondary-boot <filename>] [--param <filename>] [--kernel <filename>]\n\
	[--modem <filename>] [--normal-boot <filename>] [--system <filename>]\n\
	[--user-data <filename>] [--fota <filename>] [--hidden <filename>]\n\
	[--movinand <filename>] [--data <filename>] [--ums <filename>]\n\
	[--emmc <filename>] [--<partition identifier> <filename>]\n\
  or:\n\
    [--factoryfs <filename>] [--cache <filename>] [--dbdata <filename>]\n\
    [--primary-boot <filename>] [--secondary-boot <filename>]\n\
    [--secondary-boot-backup <filename>] [--param <filename>]\n\
    [--kernel <filename>] [--recovery <filename>] [--efs <filename>]\n\
    [--modem <filename>] [--normal-boot <filename>] [--system <filename>]\n\
	[--user-data <filename>] [--fota <filename>] [--hidden <filename>]\n\
	[--movinand <filename>] [--data <filename>] [--ums <filename>]\n\
	[--emmc <filename>] [--<partition identifier> <filename>]\n\
description: Flashes firmware files to your phone.\n\
WARNING: If you're repartitioning it's strongly recommended you specify\n\
         all files at your disposal, including bootloaders.\n\
\n\
action: close-pc-screen\n\
description: Attempts to get rid off the \"connect phone to PC\" screen.\n\
\n\
action: dump\n\
arguments: --chip-type <NAND | RAM> --chip-id <integer> --output <filename>\n\
description: Attempts to dump data from the phone corresponding to the\n\
	specified chip type and chip ID.\n\
NOTE: Galaxy S phones don't appear to properly support this functionality.\n\
\n\
action: print-pit\n\
description: Dumps the PIT file from the connected device and prints it in\n\
    a human readable format.\n\
\n\
action: help\n\
description: Display this dialogue.\n";

bool InterfaceManager::GetArguments(int argc, char **argv, map<string, string>& argumentMap, int *actionIndex)
{
	if (argc < 2)
	{
		Print(usage);
		return (false);
	}

	const char *actionName = argv[1];
	*actionIndex = -1;

	for (int i = 0; i < kActionCount; i++)
	{
		if (actionNames[i] == actionName)
		{
			*actionIndex = i;
			break;
		}
	}

	if (*actionIndex < 0)
	{
		Print("Unknown action \"%s\"\n\n", actionName);
		Print(usage);
		return (false);
	}

	int actionArgumentCount = actionArgumentCounts[*actionIndex];
	int commonArgumentCount = actionArgumentCounts[kActionCount];

	int actionValuelessArgumentCount = actionValuelessArgumentCounts[*actionIndex];
	int commonValuelessArgumentCount = actionValuelessArgumentCounts[kActionCount];

	string *argumentNames = actionArgumentNames[*actionIndex];
	string *commonArgumentNames = actionArgumentNames[kActionCount];

	for (int argIndex = 2; argIndex < argc; argIndex++)
	{
		if (*(argv[argIndex]) != '-')
		{
			Print(usage);
			return (false);
		}

		string argumentName = (char *)(argv[argIndex] + 1);

		// Check if the argument is a valid valueless argument
		bool valid = false;

		for (int i = 0; i < actionValuelessArgumentCount; i++)
		{
			if (argumentName == argumentNames[i] || argumentName == argumentNames[actionArgumentCount + i])
			{
				argumentName = argumentNames[i];
				valid = true;
				break;
			}
		}

		if (!valid)
		{
			// Check if it's a common valueless argument
			for (int i = 0; i < commonValuelessArgumentCount; i++)
			{
				if (argumentName == commonArgumentNames[i] || argumentName == commonArgumentNames[commonArgumentCount + i])
				{
					argumentName = commonArgumentNames[i];
					valid = true;
					break;
				}
			}
		}

		if (valid)
		{
			// The argument is valueless
			argumentMap.insert(pair<string, string>(argumentName, ""));
			continue;
		}

		if (argumentNames != nullptr)
		{
			// Check if the argument is a valid regular argument
			for (int i = actionValuelessArgumentCount; i < actionArgumentCount; i++)
			{
				// Support for --<integer> and -<integer> parameters.
				if (argumentName.length() > 1 && argumentNames[i].compare("-%d") == 0)
				{
					if (atoi(argumentName.substr(1).c_str()) > 0 || argumentName.compare("-0") == 0)
					{
						valid = true;
						break;
					}
				}
				else if (argumentNames[i].compare("%d") == 0)
				{
					if (atoi(argumentName.c_str()) > 0 || argumentName.compare("0") == 0)
					{
						argumentName = "-" + argumentName;
						valid = true;
						break;
					}
				}

				if (argumentName == argumentNames[i] || argumentName == argumentNames[actionArgumentCount + i])
				{
					argumentName = argumentNames[i];
					valid = true;
					break;
				}
			}
		}

		if (!valid)
		{
			// Check if it's a common regular argument
			for (int i = commonValuelessArgumentCount; i < commonArgumentCount; i++)
			{
				// Support for --<integer> and -<integer> parameters.
				if (argumentName.length() > 1 && commonArgumentNames[i].compare("-%d"))
				{
					if (atoi(argumentName.substr(1).c_str()) > 0 || argumentName.compare("-0") == 0)
					{
						valid = true;
						break;
					}
				}
				else if (commonArgumentNames[i].compare("%d"))
				{
					if (atoi(argumentName.c_str()) > 0 || argumentName.compare("0") == 0)
					{
						argumentName = "-" + argumentName;
						valid = true;
						break;
					}
				}

				if (argumentName == commonArgumentNames[i] || argumentName == commonArgumentNames[commonArgumentCount + i])
				{
					argumentName = commonArgumentNames[i];
					valid = true;
					break;
				}
			}
		}

		if (!valid)
		{
			PrintError("\"%s\" is not a valid argument\n", argumentName.c_str());
			return (false);
		}

		argIndex++;

		if (argIndex >= argc)
		{
			PrintError("\"%s\" is missing a value\n", argumentName.c_str());
			return (false);
		}

		argumentMap.insert(pair<string, string>(argumentName, argv[argIndex]));
	}

	return (true);
}

void InterfaceManager::Print(const char *format, ...)
{
	va_list args;

	va_start(args, format);
	vfprintf(stdout, format, args);
	va_end(args);

	fflush(stdout);	// Make sure output isn't buffered.
}

void InterfaceManager::PrintError(const char *format, ...)
{
	va_list args;

	va_start(args, format);
	vfprintf(stderr, format, args);
	va_end(args);

	fflush(stderr);	// Make sure output isn't buffered.
}