2017-02-28 05:37:47 +08:00
|
|
|
#include "magiskboot.h"
|
2016-09-08 20:59:48 +08:00
|
|
|
|
2017-02-25 03:29:12 +08:00
|
|
|
static void dump(unsigned char *buf, size_t size, const char *filename) {
|
2017-02-28 05:37:47 +08:00
|
|
|
int ofd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
2017-02-25 03:29:12 +08:00
|
|
|
if (ofd < 0)
|
|
|
|
error(1, "Cannot open %s", filename);
|
|
|
|
if (write(ofd, buf, size) != size)
|
|
|
|
error(1, "Cannot dump %s", filename);
|
2016-09-08 20:59:48 +08:00
|
|
|
close(ofd);
|
|
|
|
}
|
|
|
|
|
2017-02-28 05:37:47 +08:00
|
|
|
void unpack(const char* image) {
|
|
|
|
int fd = open(image, O_RDONLY);
|
2017-02-25 03:29:12 +08:00
|
|
|
if (fd < 0)
|
|
|
|
error(1, "Cannot open %s", image);
|
2016-09-08 20:59:48 +08:00
|
|
|
|
2017-02-25 03:29:12 +08:00
|
|
|
size_t size = lseek(fd, 0, SEEK_END);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
unsigned char *orig = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
2016-09-08 20:59:48 +08:00
|
|
|
|
2017-02-28 05:37:47 +08:00
|
|
|
// Parse image
|
|
|
|
parse_img(orig, size);
|
|
|
|
|
|
|
|
if (boot_type == CHROMEOS) {
|
|
|
|
// The caller should know it's chromeos, as it needs additional signing
|
|
|
|
dump(base, 0, "chromeos");
|
2017-02-25 03:29:12 +08:00
|
|
|
}
|
2017-02-24 07:45:48 +08:00
|
|
|
|
2017-02-28 05:37:47 +08:00
|
|
|
char name[PATH_MAX];
|
2017-02-24 07:45:48 +08:00
|
|
|
|
2017-02-25 03:29:12 +08:00
|
|
|
// Dump kernel
|
2017-02-28 05:37:47 +08:00
|
|
|
if (mtk_kernel) {
|
2017-02-25 03:29:12 +08:00
|
|
|
kernel += 512;
|
2017-02-24 07:45:48 +08:00
|
|
|
hdr.kernel_size -= 512;
|
|
|
|
}
|
2017-02-28 05:37:47 +08:00
|
|
|
dump(kernel, hdr.kernel_size, KERNEL_FILE);
|
2017-02-24 07:45:48 +08:00
|
|
|
|
|
|
|
// Dump ramdisk
|
2017-02-28 05:37:47 +08:00
|
|
|
if (mtk_ramdisk) {
|
2017-02-25 03:29:12 +08:00
|
|
|
ramdisk += 512;
|
2017-02-24 07:45:48 +08:00
|
|
|
hdr.ramdisk_size -= 512;
|
|
|
|
}
|
2017-02-28 05:37:47 +08:00
|
|
|
|
|
|
|
switch (ramdisk_type) {
|
|
|
|
case GZIP:
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, "gz");
|
2017-03-01 00:46:11 +08:00
|
|
|
gzip(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
|
2017-02-28 05:37:47 +08:00
|
|
|
break;
|
|
|
|
case LZOP:
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, "lzo");
|
|
|
|
break;
|
|
|
|
case XZ:
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, "xz");
|
2017-03-01 00:46:11 +08:00
|
|
|
lzma(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
|
2017-02-28 05:37:47 +08:00
|
|
|
break;
|
|
|
|
case LZMA:
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, "lzma");
|
2017-03-01 00:46:11 +08:00
|
|
|
lzma(0, RAMDISK_FILE, ramdisk, hdr.ramdisk_size);
|
2017-02-28 05:37:47 +08:00
|
|
|
break;
|
|
|
|
case BZIP2:
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, "bz2");
|
|
|
|
break;
|
|
|
|
case LZ4:
|
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, "lz4");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Never happens
|
|
|
|
break;
|
2016-09-08 20:59:48 +08:00
|
|
|
}
|
2017-02-28 05:37:47 +08:00
|
|
|
// Dump the compressed ramdisk, just in case
|
2017-02-25 03:29:12 +08:00
|
|
|
dump(ramdisk, hdr.ramdisk_size, name);
|
2017-02-24 07:45:48 +08:00
|
|
|
|
|
|
|
if (hdr.second_size) {
|
|
|
|
// Dump second
|
2017-02-28 05:37:47 +08:00
|
|
|
dump(second, hdr.second_size, SECOND_FILE);
|
2017-02-24 07:45:48 +08:00
|
|
|
}
|
2016-09-08 20:59:48 +08:00
|
|
|
|
2017-02-24 07:45:48 +08:00
|
|
|
if (hdr.dt_size) {
|
2017-02-28 05:37:47 +08:00
|
|
|
if (boot_type == ELF && (dtb_type != QCDT && dtb_type != ELF )) {
|
|
|
|
printf("Non QC dtb found in ELF kernel, recreate kernel\n");
|
2017-03-01 00:46:11 +08:00
|
|
|
gzip(1, KERNEL_FILE, kernel, hdr.kernel_size);
|
2017-02-28 05:37:47 +08: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);
|
|
|
|
}
|
2016-09-08 20:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
munmap(orig, size);
|
|
|
|
close(fd);
|
|
|
|
}
|
2017-02-24 07:45:48 +08:00
|
|
|
|