Magisk/jni/magiskboot/parseimg.c

92 lines
1.8 KiB
C
Raw Normal View History

2017-02-24 19:29:12 +00:00
#include "bootimg.h"
2017-02-27 21:37:47 +00:00
#include "magiskboot.h"
2017-02-24 19:29:12 +00:00
unsigned char *kernel, *ramdisk, *second, *dtb, *extra;
2017-02-27 21:37:47 +00:00
boot_img_hdr hdr;
int mtk_kernel = 0, mtk_ramdisk = 0;
2017-04-27 19:15:48 +00:00
file_t ramdisk_type;
2017-02-27 21:37:47 +00:00
static void check_headers() {
// Check ramdisk compression type
2017-03-02 13:59:37 +00:00
ramdisk_type = check_type(ramdisk);
2017-02-27 21:37:47 +00:00
// Check MTK
2017-03-02 13:59:37 +00:00
if (check_type(kernel) == MTK) {
2017-02-27 21:37:47 +00:00
printf("MTK header found in kernel\n");
mtk_kernel = 1;
}
2017-03-28 20:30:51 +00:00
if (ramdisk_type == MTK) {
2017-02-27 21:37:47 +00:00
printf("MTK header found in ramdisk\n");
2017-02-28 16:50:49 +00:00
mtk_ramdisk = 1;
2017-03-28 20:30:51 +00:00
ramdisk_type = check_type(ramdisk + 512);
2017-02-27 21:37:47 +00:00
}
// Print info
print_info();
2017-02-24 19:29:12 +00:00
}
static void parse_aosp(unsigned char *base, size_t size) {
2017-02-24 19:29:12 +00:00
2017-04-27 19:15:48 +00:00
// printf("IMG [AOSP]\n");
2017-02-24 19:29:12 +00:00
2017-03-04 13:16:59 +00:00
size_t pos = 0;
2017-02-24 19:29:12 +00:00
// Read the header
2017-03-04 13:16:59 +00:00
memcpy(&hdr, base, sizeof(hdr));
2017-02-24 19:29:12 +00:00
pos += hdr.page_size;
// Kernel position
2017-03-04 13:16:59 +00:00
kernel = base + pos;
2017-02-24 19:29:12 +00:00
pos += hdr.kernel_size;
2017-03-04 13:16:59 +00:00
mem_align(&pos, hdr.page_size);
2017-02-24 19:29:12 +00:00
// Ramdisk position
2017-03-04 13:16:59 +00:00
ramdisk = base + pos;
2017-02-24 19:29:12 +00:00
pos += hdr.ramdisk_size;
2017-03-04 13:16:59 +00:00
mem_align(&pos, hdr.page_size);
2017-02-24 19:29:12 +00:00
if (hdr.second_size) {
// Second position
2017-03-04 13:16:59 +00:00
second = base + pos;
2017-02-24 19:29:12 +00:00
pos += hdr.second_size;
2017-03-04 13:16:59 +00:00
mem_align(&pos, hdr.page_size);
2017-02-24 19:29:12 +00:00
}
if (hdr.dt_size) {
// dtb position
2017-03-04 13:16:59 +00:00
dtb = base + pos;
2017-02-24 19:29:12 +00:00
pos += hdr.dt_size;
2017-03-04 13:16:59 +00:00
mem_align(&pos, hdr.page_size);
2017-02-24 19:29:12 +00:00
}
if (pos < size) {
extra = base + pos;
}
2017-02-27 21:37:47 +00: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 13:59:37 +00:00
switch (check_type(base)) {
2017-04-27 19:15:48 +00: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-27 21:37:47 +00:00
}
}
2017-04-28 13:48:38 +00:00
LOGE(1, "No boot image magic found!\n");
2017-02-27 21:37:47 +00:00
}