forked from neo-technologies/rockchip-mkbootimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmkbootimg.c
209 lines (181 loc) · 5.6 KB
/
unmkbootimg.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
/* tools/mkbootimg/unmkbootimg.c
**
** Copyright 2013, Pete Batard <[email protected]>
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "bootimg.h"
static void *load_file(const char *fn, unsigned *_sz)
{
char *data;
size_t sz;
long offset;
FILE *fd;
data = 0;
fd = fopen(fn, "rb");
if(fd == 0) return 0;
if(fseek(fd, 0, SEEK_END) != 0) goto oops;
offset = ftell(fd);
if(offset < 0) goto oops;
sz = offset;
if(fseek(fd, 0, SEEK_SET) != 0) goto oops;
data = (char*) malloc(sz);
if(data == 0) goto oops;
if(fread(data, 1, sz, fd) != sz) goto oops;
fclose(fd);
if(_sz) *_sz = sz;
return data;
oops:
fclose(fd);
if(data != 0) free(data);
return 0;
}
static unsigned save_file(const char *fn, const void* data, const unsigned sz)
{
FILE *fd;
size_t _sz = 0;
fd = fopen(fn, "wb");
if(fd == 0) return 0;
_sz = fwrite(data, 1, sz, fd);
fclose(fd);
return _sz;
}
int usage(void)
{
fprintf(stderr,"usage: unmkbootimg\n"
" [ --kernel <filename> ]\n"
" [ --ramdisk <filename> ]\n"
" [ --second <2ndbootloader-filename> ]\n"
" -i|--input <filename>\n"
);
return 1;
}
int main(int argc, char **argv)
{
void *file_data = 0;
unsigned file_size = 0;
boot_img_hdr *hdr = 0;
char *kernel_fn = "kernel";
char *ramdisk_fn = "ramdisk.cpio.gz";
char *second_fn = "second_bootloader";
char *bootimg = 0;
unsigned offset;
argc--;
argv++;
while(argc > 0){
char *arg = argv[0];
char *val = argv[1];
if(argc < 2) {
return usage();
}
argc -= 2;
argv += 2;
if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
bootimg = val;
} else if(!strcmp(arg, "--kernel")) {
kernel_fn = val;
} else if(!strcmp(arg, "--ramdisk")) {
ramdisk_fn = val;
} else if(!strcmp(arg, "--second")) {
second_fn = val;
} else {
return usage();
}
}
if(bootimg == 0) {
fprintf(stderr,"error: no input filename specified\n");
return usage();
}
file_data = load_file(bootimg, &file_size);
if(file_data == 0) {
fprintf(stderr,"error: could not load image '%s'\n", bootimg);
return 1;
}
if(file_size < sizeof(boot_img_hdr)) {
fprintf(stderr,"error: file too small for a boot image\n");
goto fail;
}
hdr = (boot_img_hdr *)file_data;
if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE) != 0) {
fprintf(stderr,"error: not an Android boot image\n");
goto fail;
}
if(hdr->kernel_size != 0) {
offset = hdr->page_size;
if (save_file(kernel_fn, &((char *)file_data)[offset],
hdr->kernel_size) != hdr->kernel_size) {
fprintf(stderr,"error: could not save kernel '%s'\n", kernel_fn);
return 1;
}
printf("kernel written to '%s' (%d bytes)\n", kernel_fn,
hdr->kernel_size);
}
if(hdr->ramdisk_size != 0) {
offset = ((hdr->kernel_size + 2*hdr->page_size - 1) /
hdr->page_size) * hdr->page_size;
if (save_file(ramdisk_fn, &((char *)file_data)[offset],
hdr->ramdisk_size) != hdr->ramdisk_size) {
fprintf(stderr,"error: could not save ramdisk '%s'\n",
ramdisk_fn);
return 1;
}
printf("ramdisk written to '%s' (%d bytes)\n", ramdisk_fn,
hdr->ramdisk_size);
}
if(hdr->second_size != 0) {
offset = ((hdr->kernel_size + hdr->ramdisk_size
+ 2*hdr->page_size - 1) / hdr->page_size) * hdr->page_size;
if (save_file(second_fn, &((char *)file_data)[offset],
hdr->second_size) != hdr->second_size) {
fprintf(stderr,"error: could not save second bootloader '%s'\n",
second_fn);
return 1;
}
printf("second bootloader written to '%s' (%d bytes)\n",
second_fn, hdr->second_size);
}
/* Ideally, we'd also check the SHA sums here */
printf("\nTo rebuild this boot image, you can use the command:\n");
printf(" mkbootimg --base 0 --pagesize %d ", hdr->page_size);
if(hdr->name[0] != 0) {
printf("--board %s ", hdr->name);
}
printf("--kernel_offset 0x%08x --ramdisk_offset 0x%08x ",
hdr->kernel_addr, hdr->ramdisk_addr);
printf("--second_offset 0x%08x --tags_offset 0x%08x ",
hdr->second_addr, hdr->tags_addr);
if(hdr->cmdline[0] != 0) {
printf("--cmdline '%s%s' ", hdr->cmdline, hdr->extra_cmdline);
}
if(hdr->kernel_size != 0) {
printf("--kernel %s ", kernel_fn);
}
if(hdr->ramdisk_size != 0) {
printf("--ramdisk %s ", ramdisk_fn);
}
if(hdr->second_size != 0) {
printf("--second %s ", second_fn);
}
printf("-o %s\n", bootimg);
free(file_data);
return 0;
fail:
free(file_data);
return 1;
}