-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkmzupdate.c
162 lines (142 loc) · 3.58 KB
/
mkmzupdate.c
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
/*
* Copyright 2016 (c) Yousong Zhou
*
* This is free software, licensed under the GNU General Public License v2.
* See /LICENSE for more information.
*/
#include <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aescrypt.h"
#include "mkmzupdate.h"
static struct model_data *find_model_data(const char *model)
{
int i;
struct model_data *p;
if (!model) {
return NULL;
}
for (i = 0; meizu_models[i]; i++) {
p = meizu_models[i];
if (!strncmp(p->model, model, strlen(p->model))) {
return p;
}
}
return NULL;
}
static void show_usage(const char *arg0)
{
int i;
struct model_data *p;
#define OPT_INDENT " "
fprintf(stderr, "Usage: %s -m <model> -e|-d [-i FILE] [-o FILE]\n", arg0);
fprintf(stderr, "Encrypt or decrypt firmware for use with MEIZU devices.\n\n");
fprintf(stderr, OPT_INDENT "%-15s %s\n", "-m, --model", "model name, e.g. mx3, etc.");
fprintf(stderr, OPT_INDENT "%-15s %s\n", "-i, --input", "input file (defaults to stdin)");
fprintf(stderr, OPT_INDENT "%-15s %s\n", "-o, --output", "output file (defaults to stdout)");
fprintf(stderr, OPT_INDENT "%-15s %s\n", "-e, --encrypt", "encrypt image");
fprintf(stderr, OPT_INDENT "%-15s %s\n", "-d, --decrypt", "decrypt image");
fprintf(stderr, OPT_INDENT "%-15s %s\n", "-h, --help", "show this help text");
fprintf(stderr, "\nCurrently supported models:");
for (i = 0; meizu_models[i]; i++) {
p = meizu_models[i];
fprintf(stderr, " %s", p->model);
}
fprintf(stderr, "\n");
}
static struct option long_options[] = {
{"model", required_argument, 0, 'm'},
{"input", required_argument, 0, 'i'},
{"output", required_argument, 0, 'o'},
{"encrypt", no_argument, 0, 'e'},
{"decrypt", no_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0 }
};
#define short_options "m:i:o:edh"
int main(int argc, char **argv)
{
int opt_encrypt = 0;
int opt_decrypt = 0;
char *input_filename = NULL;
char *output_filename = NULL;
char *model_string = NULL;
struct model_data *md = NULL;
FILE *fin, *fout;
int ret = EXIT_FAILURE;
while (1) {
int c = getopt_long(argc, argv, short_options,
long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'm':
model_string = optarg;
break;
case 'i':
if (strcmp("-", optarg) != 0) {
input_filename = optarg;
}
break;
case 'o':
if (strcmp("-", optarg) != 0) {
output_filename = optarg;
}
break;
case 'e':
opt_encrypt = 1;
break;
case 'd':
opt_decrypt = 1;
break;
case 'h':
default:
show_usage(argv[0]);
exit(EXIT_SUCCESS);
}
}
if (model_string == NULL) {
show_usage(argv[0]);
exit(EXIT_SUCCESS);
}
md = find_model_data(model_string);
if (!md) {
fprintf(stderr, "%s: cannot find model data for: %s\n",
argv[0], model_string);
show_usage(argv[0]);
exit(EXIT_FAILURE);
}
if (input_filename) {
fin = fopen(input_filename, "rb");
if (fin == NULL) {
fprintf(stderr, "Can't open %s for reading: %s\n", input_filename,
strerror(errno));
exit(EXIT_FAILURE);
}
} else {
fin = stdin;
}
if (output_filename) {
fout = fopen(output_filename, "wb");
if (fout == NULL) {
fprintf(stderr, "Can't open %s for writing: %s\n", output_filename,
strerror(errno));
goto quit_close;
}
} else {
exit(EXIT_FAILURE);
fout = stdout;
}
if (opt_decrypt) {
ret = decrypt_stream(fin, fout, md->key, md->keylen);
} else if (opt_encrypt) {
ret = encrypt_stream(fin, fout, md->key, md->keylen);
}
quit_close:
fclose(fin);
fclose(fout);
return ret;
}