Magisk/jni/magiskboot/unpack.c

73 lines
1.5 KiB
C
Raw Normal View History

2017-02-27 21:37:47 +00:00
#include "magiskboot.h"
2017-02-24 19:29:12 +00:00
static void dump(unsigned char *buf, size_t size, const char *filename) {
2017-03-04 13:16:59 +00:00
int fd = open_new(filename);
if (write(fd, buf, size) != size)
2017-02-24 19:29:12 +00:00
error(1, "Cannot dump %s", filename);
2017-03-04 13:16:59 +00:00
close(fd);
}
2017-02-27 21:37:47 +00:00
void unpack(const char* image) {
2017-03-04 13:16:59 +00:00
size_t size;
unsigned char *orig;
mmap_ro(image, &orig, &size);
2017-02-27 21:37:47 +00:00
// Parse image
parse_img(orig, size);
if (boot_type == CHROMEOS) {
// The caller should know it's chromeos, as it needs additional signing
2017-03-04 13:16:59 +00:00
dump(orig, 0, "chromeos");
2017-02-24 19:29:12 +00:00
}
2017-02-27 21:37:47 +00:00
char name[PATH_MAX];
2017-02-24 19:29:12 +00:00
// Dump kernel
2017-02-27 21:37:47 +00:00
if (mtk_kernel) {
2017-02-24 19:29:12 +00:00
kernel += 512;
hdr.kernel_size -= 512;
}
2017-02-27 21:37:47 +00:00
dump(kernel, hdr.kernel_size, KERNEL_FILE);
// Dump ramdisk
2017-02-27 21:37:47 +00:00
if (mtk_ramdisk) {
2017-02-24 19:29:12 +00:00
ramdisk += 512;
hdr.ramdisk_size -= 512;
}
2017-02-27 21:37:47 +00:00
2017-03-02 13:59:37 +00:00
if (decomp(ramdisk_type, RAMDISK_FILE, ramdisk, hdr.ramdisk_size)) {
printf("Unsupported format! Please decompress manually!\n");
switch (ramdisk_type) {
case LZOP:
sprintf(name, "%s.%s", RAMDISK_FILE, "lzo");
break;
default:
// Never happens
break;
}
// Dump the compressed ramdisk
dump(ramdisk, hdr.ramdisk_size, name);
}
if (hdr.second_size) {
// Dump second
2017-02-27 21:37:47 +00:00
dump(second, hdr.second_size, SECOND_FILE);
}
if (hdr.dt_size) {
2017-03-02 13:59:37 +00:00
if (boot_type == ELF && (dtb_type != QCDT && dtb_type != ELF)) {
2017-02-27 21:37:47 +00:00
printf("Non QC dtb found in ELF kernel, recreate kernel\n");
2017-02-28 16:46:11 +00:00
gzip(1, KERNEL_FILE, kernel, hdr.kernel_size);
2017-02-27 21:37:47 +00:00
int kfp = open(KERNEL_FILE, O_WRONLY | O_APPEND);
write(kfp, dtb, hdr.dt_size);
close(kfp);
} else {
// Dump dtb
dump(dtb, hdr.dt_size, DTB_FILE);
}
}
munmap(orig, size);
}