Magisk/jni/magiskboot/parseimg.c

92 lines
1.8 KiB
C
Raw Normal View History

2017-02-25 03:29:12 +08:00
#include "bootimg.h"
2017-02-28 05:37:47 +08:00
#include "magiskboot.h"
2017-02-25 03:29:12 +08:00
unsigned char *kernel, *ramdisk, *second, *dtb, *extra;
2017-02-28 05:37:47 +08:00
boot_img_hdr hdr;
int mtk_kernel = 0, mtk_ramdisk = 0;
2017-04-28 03:15:48 +08:00
file_t ramdisk_type;
2017-02-28 05:37:47 +08:00
static void check_headers() {
// Check ramdisk compression type
2017-03-02 21:59:37 +08:00
ramdisk_type = check_type(ramdisk);
2017-02-28 05:37:47 +08:00
// Check MTK
2017-03-02 21:59:37 +08:00
if (check_type(kernel) == MTK) {
2017-02-28 05:37:47 +08:00
printf("MTK header found in kernel\n");
mtk_kernel = 1;
}
2017-03-29 04:30:51 +08:00
if (ramdisk_type == MTK) {
2017-02-28 05:37:47 +08:00
printf("MTK header found in ramdisk\n");
2017-03-01 00:50:49 +08:00
mtk_ramdisk = 1;
2017-03-29 04:30:51 +08:00
ramdisk_type = check_type(ramdisk + 512);
2017-02-28 05:37:47 +08:00
}
// Print info
print_info();
2017-02-25 03:29:12 +08:00
}
static void parse_aosp(unsigned char *base, size_t size) {
2017-02-25 03:29:12 +08:00
2017-04-28 03:15:48 +08:00
// printf("IMG [AOSP]\n");
2017-02-25 03:29:12 +08:00
2017-03-04 21:16:59 +08:00
size_t pos = 0;
2017-02-25 03:29:12 +08:00
// Read the header
2017-03-04 21:16:59 +08:00
memcpy(&hdr, base, sizeof(hdr));
2017-02-25 03:29:12 +08:00
pos += hdr.page_size;
// Kernel position
2017-03-04 21:16:59 +08:00
kernel = base + pos;
2017-02-25 03:29:12 +08:00
pos += hdr.kernel_size;
2017-03-04 21:16:59 +08:00
mem_align(&pos, hdr.page_size);
2017-02-25 03:29:12 +08:00
// Ramdisk position
2017-03-04 21:16:59 +08:00
ramdisk = base + pos;
2017-02-25 03:29:12 +08:00
pos += hdr.ramdisk_size;
2017-03-04 21:16:59 +08:00
mem_align(&pos, hdr.page_size);
2017-02-25 03:29:12 +08:00
if (hdr.second_size) {
// Second position
2017-03-04 21:16:59 +08:00
second = base + pos;
2017-02-25 03:29:12 +08:00
pos += hdr.second_size;
2017-03-04 21:16:59 +08:00
mem_align(&pos, hdr.page_size);
2017-02-25 03:29:12 +08:00
}
if (hdr.dt_size) {
// dtb position
2017-03-04 21:16:59 +08:00
dtb = base + pos;
2017-02-25 03:29:12 +08:00
pos += hdr.dt_size;
2017-03-04 21:16:59 +08:00
mem_align(&pos, hdr.page_size);
2017-02-25 03:29:12 +08:00
}
if (pos < size) {
extra = base + pos;
}
2017-02-28 05:37:47 +08:00
check_headers();
}
void parse_img(unsigned char *orig, size_t size) {
unsigned char *base, *end;
for(base = orig, end = orig + size; base < end; base += 256, size -= 256) {
2017-03-02 21:59:37 +08:00
switch (check_type(base)) {
2017-04-28 03:15:48 +08:00
case CHROMEOS:
// The caller should know it's chromeos, as it needs additional signing
close(open_new("chromeos"));
continue;
case ELF32:
exit(2);
return;
case ELF64:
exit(3);
return;
case AOSP:
parse_aosp(base, size);
return;
default:
continue;
2017-02-28 05:37:47 +08:00
}
}
2017-04-28 21:48:38 +08:00
LOGE(1, "No boot image magic found!\n");
2017-02-28 05:37:47 +08:00
}