2017-03-04 21:16:59 +08:00
|
|
|
#include "magiskboot.h"
|
|
|
|
|
2017-04-28 03:15:48 +08:00
|
|
|
char *SUP_LIST[] = { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL };
|
|
|
|
char *SUP_EXT_LIST[] = { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL };
|
|
|
|
file_t SUP_TYPE_LIST[] = { GZIP, XZ, LZMA, BZIP2, LZ4, LZ4_LEGACY, 0 };
|
2017-03-05 01:50:36 +08:00
|
|
|
|
2017-09-12 15:27:28 +08:00
|
|
|
void mmap_ro(const char *filename, void **buf, size_t *size) {
|
2017-04-28 21:48:38 +08:00
|
|
|
int fd = xopen(filename, O_RDONLY);
|
2017-03-04 21:16:59 +08:00
|
|
|
*size = lseek(fd, 0, SEEK_END);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2017-04-28 21:48:38 +08:00
|
|
|
*buf = xmmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0);
|
2017-03-04 21:16:59 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:27:28 +08:00
|
|
|
void mmap_rw(const char *filename, void **buf, size_t *size) {
|
2017-04-28 21:48:38 +08:00
|
|
|
int fd = xopen(filename, O_RDWR);
|
2017-03-04 21:16:59 +08:00
|
|
|
*size = lseek(fd, 0, SEEK_END);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2017-04-28 21:48:38 +08:00
|
|
|
*buf = xmmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2017-03-04 21:16:59 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:27:28 +08:00
|
|
|
file_t check_type(const void *buf) {
|
2017-04-28 03:15:48 +08:00
|
|
|
if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return CHROMEOS;
|
|
|
|
} else if (memcmp(buf, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0) {
|
|
|
|
return AOSP;
|
2017-04-28 03:15:48 +08:00
|
|
|
} else if (memcmp(buf, ELF32_MAGIC, 5) == 0) {
|
|
|
|
return ELF32;
|
|
|
|
} else if (memcmp(buf, ELF64_MAGIC, 5) == 0) {
|
|
|
|
return ELF64;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, GZIP_MAGIC, 4) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return GZIP;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, LZOP_MAGIC, 9) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return LZOP;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, XZ_MAGIC, 6) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return XZ;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, "\x5d\x00\x00", 3) == 0
|
|
|
|
&& (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return LZMA;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, BZIP_MAGIC, 3) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return BZIP2;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, LZ4_MAGIC, 4) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return LZ4;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, LZ4_LEG_MAGIC, 4) == 0) {
|
2017-03-29 04:09:59 +08:00
|
|
|
return LZ4_LEGACY;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, MTK_MAGIC, 4) == 0) {
|
2017-03-04 21:16:59 +08:00
|
|
|
return MTK;
|
2017-09-12 15:27:28 +08:00
|
|
|
} else if (memcmp(buf, DTB_MAGIC, 4) == 0) {
|
|
|
|
return DTB;
|
2017-03-04 21:16:59 +08:00
|
|
|
} else {
|
|
|
|
return UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 15:27:28 +08:00
|
|
|
void get_type_name(file_t type, char *name) {
|
|
|
|
char *s;
|
|
|
|
switch (type) {
|
|
|
|
case CHROMEOS:
|
|
|
|
s = "chromeos";
|
|
|
|
break;
|
|
|
|
case AOSP:
|
|
|
|
s = "aosp";
|
|
|
|
break;
|
|
|
|
case GZIP:
|
|
|
|
s = "gzip";
|
|
|
|
break;
|
|
|
|
case LZOP:
|
|
|
|
s = "lzop";
|
|
|
|
break;
|
|
|
|
case XZ:
|
|
|
|
s = "xz";
|
|
|
|
break;
|
|
|
|
case LZMA:
|
|
|
|
s = "lzma";
|
|
|
|
break;
|
|
|
|
case BZIP2:
|
|
|
|
s = "bzip2";
|
|
|
|
break;
|
|
|
|
case LZ4:
|
|
|
|
s = "lz4";
|
|
|
|
break;
|
|
|
|
case LZ4_LEGACY:
|
|
|
|
s = "lz4_legacy";
|
|
|
|
break;
|
|
|
|
case MTK:
|
|
|
|
s = "mtk";
|
|
|
|
break;
|
|
|
|
case DTB:
|
|
|
|
s = "dtb";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s = "raw";
|
|
|
|
}
|
|
|
|
strcpy(name, s);
|
|
|
|
}
|
|
|
|
|
2017-03-13 05:05:51 +08:00
|
|
|
void write_zero(int fd, size_t size) {
|
|
|
|
size_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
ftruncate(fd, pos + size);
|
|
|
|
lseek(fd, pos + size, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2017-03-04 21:16:59 +08:00
|
|
|
void mem_align(size_t *pos, size_t align) {
|
|
|
|
size_t mask = align - 1;
|
|
|
|
if (*pos & mask) {
|
|
|
|
*pos += align - (*pos & mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:54:23 +08:00
|
|
|
void file_align(int fd, size_t align, int out) {
|
2017-03-04 21:16:59 +08:00
|
|
|
size_t pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
size_t mask = align - 1;
|
2017-03-13 04:19:30 +08:00
|
|
|
size_t off;
|
2017-03-04 21:16:59 +08:00
|
|
|
if (pos & mask) {
|
2017-03-13 04:19:30 +08:00
|
|
|
off = align - (pos & mask);
|
2017-03-08 00:54:23 +08:00
|
|
|
if (out) {
|
2017-03-13 05:05:51 +08:00
|
|
|
write_zero(fd, off);
|
2017-03-13 04:19:30 +08:00
|
|
|
} else {
|
|
|
|
lseek(fd, pos + off, SEEK_SET);
|
2017-03-08 00:54:23 +08:00
|
|
|
}
|
2017-03-04 21:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_new(const char *filename) {
|
2017-04-28 21:48:38 +08:00
|
|
|
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
2017-03-04 21:16:59 +08:00
|
|
|
}
|
2017-03-05 01:50:36 +08:00
|
|
|
|
|
|
|
void cleanup() {
|
2017-07-18 11:53:28 +08:00
|
|
|
fprintf(stderr, "Cleaning up...\n");
|
2017-03-05 01:50:36 +08:00
|
|
|
char name[PATH_MAX];
|
|
|
|
unlink(KERNEL_FILE);
|
|
|
|
unlink(RAMDISK_FILE);
|
|
|
|
unlink(RAMDISK_FILE ".unsupport");
|
|
|
|
unlink(SECOND_FILE);
|
|
|
|
unlink(DTB_FILE);
|
2017-04-28 03:15:48 +08:00
|
|
|
for (int i = 0; SUP_EXT_LIST[i]; ++i) {
|
2017-03-05 01:50:36 +08:00
|
|
|
sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]);
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
}
|