Magisk/jni/magiskboot/parseimg.c

92 lines
1.8 KiB
C
Raw Normal View History

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