Welcome to mirror list, hosted at ThFree Co, Russian Federation.

unpackbootimg.c - github.com/osm0sis/mkbootimg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d4ec0bb1a665827eb84ad6272ce1e681f3de0b5 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#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 <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "mincrypt/sha.h"
#include "mincrypt/sha256.h"
#include "bootimg.h"

typedef unsigned char byte;

char *directory = "./";
char *filename = NULL;
int debug = 0;

int total_read = 0;

int pagesize = 0;
int a = 0, b = 0, c = 0, y = 0, m = 0; // header.os_version component calculation variables

int read_padding(FILE *f, unsigned itemsize)
{
    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);
    if(debug > 1) fprintf(stderr, "read padding: %d\n", count);
    return count;
}

void write_string_to_file(const char *name, const char *string)
{
    char file[PATH_MAX];
    sprintf(file, "%s/%s-%s", directory, basename(filename), name);
    FILE *t = fopen(file, "w");
    if(debug > 0) fprintf(stderr, "%s...\n", name);
    fwrite(string, strlen(string), 1, t);
    fwrite("\n", 1, 1, t);
    fclose(t);
}

void write_buffer_to_file(const char *name, FILE *f, const int size)
{
    char file[PATH_MAX];
    sprintf(file, "%s/%s-%s", directory, basename(filename), name);
    FILE *t = fopen(file, "wb");
    byte *buf = (byte *)malloc(size);
    if(debug > 0) fprintf(stderr, "Reading %s...\n", name);
    if(fread(buf, size, 1, f)) {};
    total_read += size;
    if(debug > 1) fprintf(stderr, "read: %d\n", size);
    fwrite(buf, size, 1, t);
    fclose(t);
    free(buf);
    total_read += read_padding(f, size);
}

const char *detect_hash_type(boot_img_hdr_v2 *hdr)
{
    // sha1 is expected to have zeroes in id[20] and higher
    // offset by 4 to accomodate bootimg variants with BOOT_NAME_SIZE 20
    uint8_t id[SHA256_DIGEST_SIZE];
    memcpy(&id, hdr->id, sizeof(id));
    int i;
    for(i = SHA_DIGEST_SIZE + 4; i < SHA256_DIGEST_SIZE; ++i) {
        if(id[i]) {
            return "sha256";
        }
    }
    return "sha1";
}

int print_os_version(uint32_t hdr_os_ver)
{
    if(hdr_os_ver != 0) {
        int os_version = 0, os_patch_level = 0;
        os_version = hdr_os_ver >> 11;
        os_patch_level = hdr_os_ver&0x7ff;

        a = (os_version >> 14)&0x7f;
        b = (os_version >> 7)&0x7f;
        c = os_version&0x7f;

        y = (os_patch_level >> 4) + 2000;
        m = os_patch_level&0xf;
    }
    if((a < 128) && (b < 128) && (c < 128) && (y >= 2000) && (y < 2128) && (m > 0) && (m <= 12)) {
        printf("BOARD_OS_VERSION %d.%d.%d\n", a, b, c);
        printf("BOARD_OS_PATCH_LEVEL %d-%02d\n", y, m);
        return 0;
    }
    return 1;
}

int usage(void)
{
    fprintf(stderr,
        "usage: unpackbootimg\n"
        "\t-i|--input <filename>\n"
        "\t[ -o|--output <directory> ]\n"
        "\t[ -p|--pagesize <size-in-hexadecimal> ]\n"
    );
    if(debug > 0) fprintf(stderr, "\t[ -d|--debug <debug-level> ]\n");
    return 1;
}

int main(int argc, char **argv)
{
    char tmp[BOOT_MAGIC_SIZE];
    char *magic = NULL;
    int base = 0;

    int seeklimit = 65536; // arbitrary byte limit to search in input file for ANDROID!/VNDRBOOT magic
    int hdr_ver_max = 8; // arbitrary maximum header version value; when greater assume the field is appended dt size

    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 if(!strcmp(arg, "--debug") || !strcmp(arg, "-d")) {
            debug = strtoul(val, 0, 10);
        } else {
            return usage();
        }
    }

    if(filename == NULL) {
        return usage();
    }

    struct stat st;
    if(stat(directory, &st) == (-1)) {
        fprintf(stderr, "Could not stat %s: %s\n", directory, strerror(errno));
        return 1;
    }
    if(!S_ISDIR(st.st_mode)) {
        fprintf(stderr, "%s is not a directory\n", directory);
        return 1;
    }

    FILE *f = fopen(filename, "rb");
    if(!f) {
        fprintf(stderr, "Could not open input file: %s\n", strerror(errno));
        return 1;
    }

    if(debug > 0) fprintf(stderr, "Reading header...\n");
    int i;
    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) {
            magic = BOOT_MAGIC;
            break;
        }
        if(memcmp(tmp, VENDOR_BOOT_MAGIC, VENDOR_BOOT_MAGIC_SIZE) == 0) {
            magic = VENDOR_BOOT_MAGIC;
            break;
        }
    }
    total_read = i;
    if(i > seeklimit) {
        fprintf(stderr, "Boot image magic not found.\n");
        return 1;
    }
    printf("%s magic found at: %d\n", magic, i);

    boot_img_hdr_v3 header;
    fseek(f, i, SEEK_SET);
    if(fread(&header, sizeof(header), 1, f)) {};

    if(magic == BOOT_MAGIC) {
        if((header.header_version < 3) || (header.header_version > hdr_ver_max)) {
            // boot_img_hdr_v2 in the backported header supports all boot_img_hdr versions and cross-compatible variants below 3

            fseek(f, i, SEEK_SET);
            boot_img_hdr_v2 header;
            if(fread(&header, sizeof(header), 1, f)) {};

            base = header.kernel_addr - 0x00008000;
            if(pagesize == 0) {
                pagesize = header.page_size;
            }
            const char *hash_type = detect_hash_type(&header);

            printf("BOARD_KERNEL_CMDLINE %.*s%.*s\n", BOOT_ARGS_SIZE, header.cmdline, BOOT_EXTRA_ARGS_SIZE, header.extra_cmdline);
            printf("BOARD_KERNEL_BASE 0x%08x\n", base);
            printf("BOARD_NAME %s\n", header.name);
            printf("BOARD_PAGE_SIZE %d\n", header.page_size);
            printf("BOARD_HASH_TYPE %s\n", hash_type);
            printf("BOARD_KERNEL_OFFSET 0x%08x\n", header.kernel_addr - base);
            printf("BOARD_RAMDISK_OFFSET 0x%08x\n", header.ramdisk_addr - base);
            printf("BOARD_SECOND_OFFSET 0x%08x\n", header.second_addr - base);
            printf("BOARD_TAGS_OFFSET 0x%08x\n", header.tags_addr - base);
            if(print_os_version(header.os_version) == 1) {
                header.os_version = 0;
            }
            if(header.dt_size > hdr_ver_max) {
                printf("BOARD_DT_SIZE %d\n", header.dt_size);
            } else {
                printf("BOARD_HEADER_VERSION %d\n", header.header_version);
            }
            if(header.header_version <= hdr_ver_max) {
                if(header.header_version > 0) {
                    if(header.recovery_dtbo_size != 0) {
                        printf("BOARD_RECOVERY_DTBO_SIZE %d\n", header.recovery_dtbo_size);
                        printf("BOARD_RECOVERY_DTBO_OFFSET %"PRId64"\n", header.recovery_dtbo_offset);
                    }
                    printf("BOARD_HEADER_SIZE %d\n", header.header_size);
                } else {
                    header.recovery_dtbo_size = 0;
                }
                if(header.header_version > 1) {
                    if(header.dtb_size != 0) {
                        printf("BOARD_DTB_SIZE %d\n", header.dtb_size);
                        printf("BOARD_DTB_OFFSET 0x%08"PRIx64"\n", header.dtb_addr - base);
                    }
                } else {
                    header.dtb_size = 0;
                }
            }

            char cmdlinetmp[BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE+1];
            sprintf(cmdlinetmp, "%.*s%.*s", BOOT_ARGS_SIZE, header.cmdline, BOOT_EXTRA_ARGS_SIZE, header.extra_cmdline);
            cmdlinetmp[BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE]='\0';
            write_string_to_file("cmdline", cmdlinetmp);

            write_string_to_file("board", (char *)header.name);

            char basetmp[200];
            sprintf(basetmp, "0x%08x", base);
            write_string_to_file("base", basetmp);

            char pagesizetmp[200];
            sprintf(pagesizetmp, "%d", header.page_size);
            write_string_to_file("pagesize", pagesizetmp);

            char kernelofftmp[200];
            sprintf(kernelofftmp, "0x%08x", header.kernel_addr - base);
            write_string_to_file("kernel_offset", kernelofftmp);

            char ramdiskofftmp[200];
            sprintf(ramdiskofftmp, "0x%08x", header.ramdisk_addr - base);
            write_string_to_file("ramdisk_offset", ramdiskofftmp);

            char secondofftmp[200];
            sprintf(secondofftmp, "0x%08x", header.second_addr - base);
            write_string_to_file("second_offset", secondofftmp);

            char tagsofftmp[200];
            sprintf(tagsofftmp, "0x%08x", header.tags_addr - base);
            write_string_to_file("tags_offset", tagsofftmp);

            if(header.os_version != 0) {
                char osvertmp[200];
                sprintf(osvertmp, "%d.%d.%d", a, b, c);
                write_string_to_file("os_version", osvertmp);

                char oslvltmp[200];
                sprintf(oslvltmp, "%d-%02d", y, m);
                write_string_to_file("os_patch_level", oslvltmp);
            }

            if(header.header_version <= hdr_ver_max) {
                char hdrvertmp[200];
                sprintf(hdrvertmp, "%d\n", header.header_version);
                write_string_to_file("header_version", hdrvertmp);

                if(header.header_version > 1) {
                    char dtbofftmp[200];
                    sprintf(dtbofftmp, "0x%08"PRIx64, header.dtb_addr - base);
                    write_string_to_file("dtb_offset", dtbofftmp);
                }
            }

            write_string_to_file("hashtype", hash_type);

            total_read += sizeof(header);
            if(debug > 1) fprintf(stderr, "read: %ld\n", sizeof(header)); // this will harmlessly always show 1660 since it uses boot_img_hdr_v2 for all < v3
            total_read += read_padding(f, sizeof(header));

            write_buffer_to_file("kernel", f, header.kernel_size);

            write_buffer_to_file("ramdisk", f, header.ramdisk_size);

            if(header.second_size != 0) {
                write_buffer_to_file("second", f, header.second_size);
            }

            if(header.dt_size > hdr_ver_max) {
                write_buffer_to_file("dt", f, header.dt_size);
            } else {
                if(header.recovery_dtbo_size != 0) {
                    write_buffer_to_file("recovery_dtbo", f, header.recovery_dtbo_size);
                }
                if(header.dtb_size != 0) {
                    write_buffer_to_file("dtb", f, header.dtb_size);
                }
            }
        } else {
            // boot_img_hdr_v3 and above are no longer backwards compatible

            if(pagesize == 0) {
                pagesize = 0x00001000; // page_size is hardcoded to 4096 in boot_img_hdr_v3 and above
            }

            printf("BOARD_KERNEL_CMDLINE %.*s\n", BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE, header.cmdline);
            printf("BOARD_PAGE_SIZE 4096\n");
            print_os_version(header.os_version);
            printf("BOARD_HEADER_VERSION %d\n", header.header_version);
            printf("BOARD_HEADER_SIZE %d\n", header.header_size);

            char cmdlinetmp[BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE+1];
            sprintf(cmdlinetmp, "%.*s", BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE, header.cmdline);
            cmdlinetmp[BOOT_ARGS_SIZE+BOOT_EXTRA_ARGS_SIZE]='\0';
            write_string_to_file("cmdline", cmdlinetmp);

            char osvertmp[200];
            sprintf(osvertmp, "%d.%d.%d", a, b, c);
            write_string_to_file("os_version", osvertmp);

            char oslvltmp[200];
            sprintf(oslvltmp, "%d-%02d", y, m);
            write_string_to_file("os_patch_level", oslvltmp);

            char hdrvertmp[200];
            sprintf(hdrvertmp, "%d\n", header.header_version);
            write_string_to_file("header_version", hdrvertmp);

            total_read += sizeof(header);
            if(debug > 1) fprintf(stderr, "read: %ld\n", sizeof(header));
            total_read += read_padding(f, sizeof(header));

            write_buffer_to_file("kernel", f, header.kernel_size);

            write_buffer_to_file("ramdisk", f, header.ramdisk_size);
        }
    } else {
        // vendor_boot_img_hdr started at v3 and is not cross-compatible with boot_img_hdr

        fseek(f, i, SEEK_SET);
        vendor_boot_img_hdr_v3 header;
        if(fread(&header, sizeof(header), 1, f)) {};

        base = header.kernel_addr - 0x00008000;
        if(pagesize == 0) {
            pagesize = header.page_size;
        }

        printf("BOARD_VENDOR_CMDLINE %.*s\n", VENDOR_BOOT_ARGS_SIZE, header.cmdline);
        printf("BOARD_VENDOR_BASE 0x%08x\n", base);
        printf("BOARD_NAME %s\n", header.name);
        printf("BOARD_PAGE_SIZE %d\n", header.page_size);
        printf("BOARD_KERNEL_OFFSET 0x%08x\n", header.kernel_addr - base);
        printf("BOARD_RAMDISK_OFFSET 0x%08x\n", header.ramdisk_addr - base);
        printf("BOARD_TAGS_OFFSET 0x%08x\n", header.tags_addr - base);
        printf("BOARD_HEADER_VERSION %d\n", header.header_version);
        printf("BOARD_HEADER_SIZE %d\n", header.header_size);
        printf("BOARD_DTB_SIZE %d\n", header.dtb_size);
        printf("BOARD_DTB_OFFSET 0x%08"PRIx64"\n", header.dtb_addr - base);

        char cmdlinetmp[VENDOR_BOOT_ARGS_SIZE+1];
        sprintf(cmdlinetmp, "%.*s", VENDOR_BOOT_ARGS_SIZE, header.cmdline);
        cmdlinetmp[VENDOR_BOOT_ARGS_SIZE]='\0';
        write_string_to_file("vendor_cmdline", cmdlinetmp);

        write_string_to_file("board", (char *)header.name);

        char basetmp[200];
        sprintf(basetmp, "0x%08x", base);
        write_string_to_file("base", basetmp);

        char pagesizetmp[200];
        sprintf(pagesizetmp, "%d", header.page_size);
        write_string_to_file("pagesize", pagesizetmp);

        char kernelofftmp[200];
        sprintf(kernelofftmp, "0x%08x", header.kernel_addr - base);
        write_string_to_file("kernel_offset", kernelofftmp);

        char ramdiskofftmp[200];
        sprintf(ramdiskofftmp, "0x%08x", header.ramdisk_addr - base);
        write_string_to_file("ramdisk_offset", ramdiskofftmp);

        char tagsofftmp[200];
        sprintf(tagsofftmp, "0x%08x", header.tags_addr - base);
        write_string_to_file("tags_offset", tagsofftmp);

        char hdrvertmp[200];
        sprintf(hdrvertmp, "%d\n", header.header_version);
        write_string_to_file("header_version", hdrvertmp);

        char dtbofftmp[200];
        sprintf(dtbofftmp, "0x%08"PRIx64, header.dtb_addr - base);
        write_string_to_file("dtb_offset", dtbofftmp);

        total_read += sizeof(header);
        if(debug > 1) fprintf(stderr, "read: %ld\n", sizeof(header));
        total_read += read_padding(f, sizeof(header));

        write_buffer_to_file("vendor_ramdisk", f, header.vendor_ramdisk_size);

        write_buffer_to_file("dtb", f, header.dtb_size);
    }

    fclose(f);

    if(debug > 0) fprintf(stderr, "Total Read: %d\n", total_read);
    return 0;
}