-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathunpackbootimg.c
271 lines (234 loc) · 7.97 KB
/
unpackbootimg.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
//#include <libgen.h>
#include "bootimg.h"
typedef unsigned char byte;
int read_padding(FILE* f, unsigned itemsize, int pagesize)
{
byte* buf = (byte*)malloc(sizeof(byte) * pagesize);
unsigned pagemask = pagesize - 1;
unsigned count;
if((itemsize & pagemask) == 0) {
free(buf);
return 0;
}
count = pagesize - (itemsize & pagemask);
if(fread(buf, count, 1, f)){};
free(buf);
return count;
}
void write_string_to_file(char* file, char* string)
{
FILE* f = fopen(file, "w");
fwrite(string, strlen(string), 1, f);
fwrite("\n", 1, 1, f);
fclose(f);
}
int usage() {
printf("usage: unpackbootimg\n");
printf("\t-i|--input boot.img\n");
printf("\t[ -o|--output output_directory]\n");
printf("\t[ -p|--pagesize <size-in-hexadecimal> ]\n");
return 0;
}
static char *basenamee(char *in) {
char *ssc;
int p = 0;
ssc = strstr(in, "/");
if(ssc == NULL) {
ssc = strstr(in, "\\");
if(ssc == NULL) {
return in;
}
}
do {
p = strlen(ssc) + 1;
in = &in[strlen(in)-p+2];
ssc = strstr(in, "/");
if (ssc == NULL)
ssc = strstr(in, "\\");
} while(ssc);
return in;
}
int dump_boot(char* filein, char* fileout)
{
char tmp[4096];
char* directory = "./";
char* filename = NULL;
int pagesize = 0;
int base = 0;
int a=0,b=0,c=0,y=0,m=0;
filename = filein;
directory = fileout;
if (filename == NULL) {
return usage();
}
int total_read = 0;
FILE* f = fopen(filename, "rb");
boot_img_hdr header;
//printf("Reading header...\n");
int i;
int seeklimit = 4096;
for (i = 0; i <= seeklimit; i++) {
fseek(f, i, SEEK_SET);
if(fread(tmp, BOOT_MAGIC_SIZE, 1, f)){};
if (memcmp(tmp, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0)
break;
}
total_read = i;
if (i > seeklimit) {
printf("Android boot magic not found.\n");
return 1;
}
fseek(f, i, SEEK_SET);
if (i > 0) {
printf("Android magic found at: %d\n", i);
}
if(fread(&header, sizeof(header), 1, f)){};
base = header.kernel_addr - 0x00008000;
printf("BOARD_KERNEL_CMDLINE %s\n", header.cmdline);
printf("BOARD_KERNEL_BASE %08x\n", base);
printf("BOARD_NAME %s\n", header.name);
printf("BOARD_PAGE_SIZE %d\n", header.page_size);
printf("BOARD_KERNEL_OFFSET %08x\n", header.kernel_addr - base);
printf("BOARD_RAMDISK_OFFSET %08x\n", header.ramdisk_addr - base);
if (header.second_size != 0) {
printf("BOARD_SECOND_OFFSET %08x\n", header.second_addr - base);
}
printf("BOARD_TAGS_OFFSET %08x\n", header.tags_addr - base);
if (header.os_version != 0) {
int os_version,os_patch_level;
os_version = header.os_version >> 11;
os_patch_level = header.os_version&0x7ff;
a = (os_version >> 14)&0x7f;
b = (os_version >> 7)&0x7f;
c = os_version&0x7f;
printf("BOARD_OS_VERSION %d.%d.%d\n", a, b, c);
y = (os_patch_level >> 4) + 2000;
m = os_patch_level&0xf;
printf("BOARD_OS_PATCH_LEVEL %d-%02d\n", y, m);
}
if (header.dt_size != 0) {
printf("BOARD_DT_SIZE %d\n", header.dt_size);
}
if (pagesize == 0) {
pagesize = header.page_size;
}
//printf("cmdline...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-cmdline");
write_string_to_file(tmp, (char *)header.cmdline);
//printf("board...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-board");
write_string_to_file(tmp, (char *)header.name);
//printf("base...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-base");
char basetmp[200];
sprintf(basetmp, "%08x", base);
write_string_to_file(tmp, basetmp);
//printf("pagesize...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-pagesize");
char pagesizetmp[200];
sprintf(pagesizetmp, "%d", header.page_size);
write_string_to_file(tmp, pagesizetmp);
//printf("kerneloff...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-kerneloff");
char kernelofftmp[200];
sprintf(kernelofftmp, "%08x", header.kernel_addr - base);
write_string_to_file(tmp, kernelofftmp);
//printf("ramdiskoff...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-ramdiskoff");
char ramdiskofftmp[200];
sprintf(ramdiskofftmp, "%08x", header.ramdisk_addr - base);
write_string_to_file(tmp, ramdiskofftmp);
if (header.second_size != 0) {
//printf("secondoff...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-secondoff");
char secondofftmp[200];
sprintf(secondofftmp, "%08x", header.second_addr - base);
write_string_to_file(tmp, secondofftmp);
}
//printf("tagsoff...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-tagsoff");
char tagsofftmp[200];
sprintf(tagsofftmp, "%08x", header.tags_addr - base);
write_string_to_file(tmp, tagsofftmp);
if (header.os_version != 0) {
//printf("os_version...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-osversion");
char osvertmp[200];
sprintf(osvertmp, "%d.%d.%d", a, b, c);
write_string_to_file(tmp, osvertmp);
//printf("os_patch_level...\n");
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-oslevel");
char oslvltmp[200];
sprintf(oslvltmp, "%d-%02d", y, m);
write_string_to_file(tmp, oslvltmp);
}
total_read += sizeof(header);
//printf("total read: %d\n", total_read);
total_read += read_padding(f, sizeof(header), pagesize);
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-zImage");
FILE *k = fopen(tmp, "wb");
byte* kernel = (byte*)malloc(header.kernel_size);
//printf("Reading kernel...\n");
if(fread(kernel, header.kernel_size, 1, f)){};
total_read += header.kernel_size;
fwrite(kernel, header.kernel_size, 1, k);
fclose(k);
//printf("total read: %d\n", header.kernel_size);
total_read += read_padding(f, header.kernel_size, pagesize);
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-ramdisk.gz");
FILE *r = fopen(tmp, "wb");
byte* ramdisk = (byte*)malloc(header.ramdisk_size);
//printf("Reading ramdisk...\n");
if(fread(ramdisk, header.ramdisk_size, 1, f)){};
total_read += header.ramdisk_size;
fwrite(ramdisk, header.ramdisk_size, 1, r);
fclose(r);
//printf("total read: %d\n", header.ramdisk_size);
total_read += read_padding(f, header.ramdisk_size, pagesize);
if (header.second_size != 0) {
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-second");
FILE *s = fopen(tmp, "wb");
byte* second = (byte*)malloc(header.second_size);
//printf("Reading second...\n");
if(fread(second, header.second_size, 1, f)){};
total_read += header.second_size;
fwrite(second, header.second_size, 1, s);
fclose(s);
}
//printf("total read: %d\n", header.second_size);
total_read += read_padding(f, header.second_size, pagesize);
if (header.dt_size != 0) {
sprintf(tmp, "%s/%s", directory, basenamee(filename));
strcat(tmp, "-dtb");
FILE *d = fopen(tmp, "wb");
byte* dtb = (byte*)malloc(header.dt_size);
//printf("Reading dtb...\n");
if(fread(dtb, header.dt_size, 1, f)){};
total_read += header.dt_size;
fwrite(dtb, header.dt_size, 1, d);
fclose(d);
}
fclose(f);
//printf("Total Read: %d\n", total_read);
return 0;
}