forked from raidzero/RZrecovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpackbootimg.c
142 lines (119 loc) · 3.81 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
#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 "mincrypt/sha.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);
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 unpackbootimg_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;
}
int unpack_bootimg_main(int argc, char** argv)
{
char tmp[PATH_MAX];
char* directory = "./";
char* filename = NULL;
int pagesize = 0;
argc--;
argv++;
while(argc > 0){
char *arg = argv[0];
char *val = argv[1];
argc -= 2;
argv += 2;
if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
filename = val;
} else if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
directory = val;
} else if(!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) {
pagesize = strtoul(val, 0, 16);
} else {
return unpackbootimg_usage();
}
}
if (filename == NULL) {
return unpackbootimg_usage();
}
int total_read = 0;
FILE* f = fopen(filename, "rb");
boot_img_hdr header;
//printf("Reading header...\n");
fread(&header, sizeof(header), 1, f);
printf("BOARD_KERNEL_CMDLINE %s\n", header.cmdline);
printf("BOARD_KERNEL_BASE %08x\n", header.kernel_addr - 0x00008000);
printf("BOARD_PAGE_SIZE %d\n", header.page_size);
if (pagesize == 0) {
pagesize = header.page_size;
}
//printf("cmdline...\n");
sprintf(tmp, "%s/%s", directory, basename(filename));
strcat(tmp, "-cmdline");
write_string_to_file(tmp, header.cmdline);
//printf("base...\n");
sprintf(tmp, "%s/%s", directory, basename(filename));
strcat(tmp, "-base");
char basetmp[200];
sprintf(basetmp, "%08x", header.kernel_addr - 0x00008000);
write_string_to_file(tmp, basetmp);
//printf("pagesize...\n");
sprintf(tmp, "%s/%s", directory, basename(filename));
strcat(tmp, "-pagesize");
char pagesizetmp[200];
sprintf(pagesizetmp, "%d", header.page_size);
write_string_to_file(tmp, pagesizetmp);
total_read += sizeof(header);
//printf("total read: %d\n", total_read);
total_read += read_padding(f, sizeof(header), pagesize);
sprintf(tmp, "%s/%s", directory, basename(filename));
strcat(tmp, "-zImage");
FILE *k = fopen(tmp, "wb");
byte* kernel = (byte*)malloc(header.kernel_size);
//printf("Reading kernel...\n");
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, basename(filename));
strcat(tmp, "-ramdisk.gz");
FILE *r = fopen(tmp, "wb");
byte* ramdisk = (byte*)malloc(header.ramdisk_size);
//printf("Reading ramdisk...\n");
fread(ramdisk, header.ramdisk_size, 1, f);
total_read += header.ramdisk_size;
fwrite(ramdisk, header.ramdisk_size, 1, r);
fclose(r);
fclose(f);
//printf("Total Read: %d\n", total_read);
return 0;
}