forked from wuyongzheng/gimgtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gimgunlock.c
251 lines (222 loc) · 7.45 KB
/
gimgunlock.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "util_indep.h"
struct patch_struct {
unsigned char *data;
struct patch_struct *next;
off_t offset;
unsigned long size;
};
void unlockml (unsigned char *dst, const unsigned char *src,
int size, unsigned int key)
{
static const unsigned char shuf[] = {
0xb, 0xc, 0xa, 0x0,
0x8, 0xf, 0x2, 0x1,
0x6, 0x4, 0x9, 0x3,
0xd, 0x5, 0x7, 0xe};
int i, ringctr;
int key_sum = shuf[((key >> 24) + (key >> 16) + (key >> 8) + key) & 0xf];
for (i = 0, ringctr = 16; i < size; i ++) {
unsigned int upper = src[i] >> 4;
unsigned int lower = src[i];
upper -= key_sum;
upper -= key >> ringctr;
upper -= shuf[(key >> ringctr) & 0xf];
ringctr = ringctr ? ringctr - 4 : 16;
lower -= key_sum;
lower -= key >> ringctr;
lower -= shuf[(key >> ringctr) & 0xf];
ringctr = ringctr ? ringctr - 4 : 16;
dst[i] = ((upper << 4) & 0xf0) | (lower & 0xf);
}
}
struct patch_struct *prepend_patch (struct patch_struct *patch_list, unsigned long size)
{
struct patch_struct *new_patch =
(struct patch_struct *)malloc(sizeof(struct patch_struct) + size);
assert(new_patch != NULL);
memset(new_patch, 0, sizeof(struct patch_struct) + size);
new_patch->size = size;
new_patch->data = (unsigned char *)new_patch + sizeof(struct patch_struct);
new_patch->next = patch_list;
return new_patch;
}
struct patch_struct *unlock_tre (FILE *fp, struct patch_struct *patch,
off_t base, off_t header)
{
unsigned int key, mloff, mlsize;
unsigned char encml[64]; /* at most 16 levels */
key = read_4byte_at(fp, header + 0xaa);
printf("key: 0x%x\n", key);
mloff = read_4byte_at(fp, header + 0x21);
mlsize = read_4byte_at(fp, header + 0x25);
if (mlsize > 64)
errexit("Map level's size %d > 64\n", mlsize);
read_bytes_at(fp, base + mloff, encml, mlsize);
printf("encrypted ml: ");
hexdump(encml, mlsize);
patch = prepend_patch(patch, mlsize);
patch->offset = base + mloff;
unlockml(patch->data, encml, mlsize, key);
printf("decrypted ml: ");
hexdump(patch->data, mlsize);
patch = prepend_patch(patch, 20);
patch->offset = header + 0x9a;
read_bytes_at(fp, header + 0x9a, patch->data, 20);
printf("orig key: ");
hexdump(patch->data, 20);
patch->data[1] ^= 0x80;
memcpy(patch->data + 8, patch->data + 12, 4);
memset(patch->data + 16, 0, 4);
printf("new key: ");
hexdump(patch->data, 20);
patch = prepend_patch(patch, 1);
patch->offset = header + 0xd;
patch->data[0] = read_byte_at(fp, (header + 0xd)) ^ 0x80;
return patch;
}
struct patch_struct *create_patch (FILE *fp)
{
struct patch_struct *patch = NULL;
int block_size, fatstart, fatend, fatcount;
if ((read_byte_at(fp, 0) ^ read_byte_at(fp, 0x10)) != 'D' ||
(read_byte_at(fp, 0) ^ read_byte_at(fp, 0x15)) != 'G')
errexit("Not a garmin img file.\n");
if (read_byte_at(fp, 0))
errexit("XOR is not 0. Fix it first.\n");
block_size = 1 << (read_byte_at(fp, 0x61) + read_byte_at(fp, 0x62));
fatstart = read_byte_at(fp, 0x40) == 0 ? 3 : read_byte_at(fp, 0x40);
if (read_4byte_at(fp, 0x40c) == 0) { // use root dir. assume it's the first file
off_t offset = fatstart * 512;
if (read_byte_at(fp, offset) != 1 ||
read_byte_at(fp, offset + 0x1) != ' ' ||
read_byte_at(fp, offset + 0x9) != ' ')
errexit("imgheader.data_offset = 0 but the first file is not root dir!\n");
fatstart ++;
if (read_4byte_at(fp, offset + 0xc) % 512 != 0 ||
read_4byte_at(fp, offset + 0xc) <= fatstart * 512)
errexit("rootdir.size = %x which is bad\n", read_4byte_at(fp, offset + 0xc));
fatend = read_4byte_at(fp, offset + 0xc) / 512;
printf("Parsing fat use rootdir, fatstart=%d, fatend=%d\n", fatstart, fatend);
} else {
fatend = read_4byte_at(fp, 0x40c) / 512;
printf("Parsing fat use data_offset, fatstart=%d, fatend=%d\n", fatstart, fatend);
}
for (fatcount = fatstart; fatcount < fatend; fatcount ++) {
off_t offset = fatcount * 512;
char subfile_name[16];
if (read_byte_at(fp, offset) != 1)
continue;
if (read_byte_at(fp, offset + 0x1) == ' ') /* rootdir */
continue;
if (read_2byte_at(fp, offset + 0x10) != 0)
continue;
// TODO fix space padding
read_bytes_at(fp, offset + 0x1, (unsigned char *)subfile_name, 8);
subfile_name[8] = '.';
read_bytes_at(fp, offset + 0x9, (unsigned char *)subfile_name + 9, 3);
subfile_name[12] = '\0';
if (read_byte_at(fp, offset + 0x9) == 'G' &&
read_byte_at(fp, offset + 0xa) == 'M' &&
read_byte_at(fp, offset + 0xb) == 'P') {
off_t tre_offset;
offset = read_2byte_at(fp, offset + 0x20) * block_size;
if (read_byte_at(fp, offset + 0x2) != 'G' ||
read_byte_at(fp, offset + 0x9) != 'G' ||
read_byte_at(fp, offset + 0xa) != 'M' ||
read_byte_at(fp, offset + 0xb) != 'P')
errexit("Unknown GMP file header.\n");
if (read_byte_at(fp, offset + 0xd) & 0x80)
printf("WARNING: don't know how to unlock GMP file %s\n", subfile_name);
tre_offset = offset + read_4byte_at(fp, offset + 0x19);
if (read_byte_at(fp, tre_offset + 0x2) != 'G' ||
read_byte_at(fp, tre_offset + 0x9) != 'T' ||
read_byte_at(fp, tre_offset + 0xa) != 'R' ||
read_byte_at(fp, tre_offset + 0xb) != 'E')
errexit("Unknown TRE file header at %x in GMP.\n", (unsigned int)tre_offset);
if (read_byte_at(fp, tre_offset + 0xd) & 0x80) {
printf("Unlocking subfile %s\n", subfile_name);
patch = unlock_tre(fp, patch, offset, tre_offset);
} else {
printf("Skipping plain subfile %s\n", subfile_name);
}
//TODO check other subsubfiles
} else if (read_byte_at(fp, offset + 0x9) == 'T' &&
read_byte_at(fp, offset + 0xa) == 'R' &&
read_byte_at(fp, offset + 0xb) == 'E') {
offset = read_2byte_at(fp, offset + 0x20) * block_size;
if (read_byte_at(fp, offset + 0x2) != 'G' ||
read_byte_at(fp, offset + 0x9) != 'T' ||
read_byte_at(fp, offset + 0xa) != 'R' ||
read_byte_at(fp, offset + 0xb) != 'E')
errexit("Unknown TRE file header.\n");
if (read_byte_at(fp, offset + 0xd) & 0x80) {
printf("Unlocking subfile %s\n", subfile_name);
patch = unlock_tre(fp, patch, offset, offset);
} else {
printf("Skipping plain subfile %s\n", subfile_name);
}
} else if (strcmp(subfile_name, "MAPSOURC.MPS") == 0) {
printf("Skipping MAPSOURC.MPS\n");
} else {
offset = read_2byte_at(fp, offset + 0x20) * block_size;
if (read_byte_at(fp, offset + 0xd) & 0x80) {
printf("WARNING: don't know how to unlock subfile %s\n", subfile_name);
} else {
printf("Skipping plain subfile %s\n", subfile_name);
}
}
}
return patch;
}
void apply_patch (FILE *fp, struct patch_struct *patch_list)
{
struct patch_struct *patch;
for (patch = patch_list; patch != NULL; patch = patch->next) {
if (myfseek64(fp, patch->offset)) {
perror(NULL);
return;
}
if (fwrite(patch->data, patch->size, 1, fp) != 1) {
perror(NULL);
return;
}
}
}
int main (int argc, char *argv[])
{
FILE *fp;
struct patch_struct *patch;
if (argc != 2) {
printf("usage: %s file.img\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-?") == 0) {
printf("usage: %s file.img\n", argv[0]);
return 1;
}
printf("Analyzing file.\n");
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("can't open %s\n", argv[1]);
return 1;
}
patch = create_patch(fp);
fclose(fp);
if (patch == NULL)
return 1;
printf("Writing to file.\n");
fp = fopen(argv[1], "rb+");
if (fp == NULL) {
printf("can't open %s for writing\n", argv[1]);
return 1;
}
apply_patch(fp, patch);
fclose(fp);
return 0;
}