From 8b2ec23a89b96e8f2888552b6b883ab8018a3dfa Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 14 Sep 2017 23:11:56 +0800 Subject: [PATCH] Re-organize MagiskBoot --- jni/Android.mk | 1 + jni/include/logging.h | 2 +- jni/magiskboot/boot_utils.c | 97 ++--------------------------- jni/magiskboot/bootimg.c | 12 +++- jni/magiskboot/bootimg.h | 2 +- jni/magiskboot/compress.c | 49 ++++++++------- jni/magiskboot/cpio.c | 11 ++-- jni/magiskboot/hexpatch.c | 6 ++ jni/magiskboot/magiskboot.h | 32 ++-------- jni/magiskboot/main.c | 93 ++++++++++++++++++--------- jni/magiskboot/types.c | 83 ++++++++++++++++++++++++ jni/magiskboot/{magic.h => types.h} | 11 +++- 12 files changed, 216 insertions(+), 183 deletions(-) create mode 100644 jni/magiskboot/types.c rename jni/magiskboot/{magic.h => types.h} (78%) diff --git a/jni/Android.mk b/jni/Android.mk index c79041721..c9980a81c 100644 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -80,6 +80,7 @@ LOCAL_SRC_FILES := \ magiskboot/boot_utils.c \ magiskboot/cpio.c \ magiskboot/sha1.c \ + magiskboot/types.c \ utils/xwrap.c \ utils/vector.c \ utils/list.c diff --git a/jni/include/logging.h b/jni/include/logging.h index 6a95211b5..63e1980c9 100644 --- a/jni/include/logging.h +++ b/jni/include/logging.h @@ -41,7 +41,7 @@ static inline void stub(const char *fmt, ...) {} #include -#define LOGE(err, ...) { fprintf(stderr, __VA_ARGS__); exit(err); } +#define LOGE(...) { fprintf(stderr, __VA_ARGS__); exit(1); } #define PLOGE(fmt, args...) { fprintf(stderr, fmt " failed with %d: %s\n\n", ##args, errno, strerror(errno)); exit(1); } #endif // IS_DAEMON diff --git a/jni/magiskboot/boot_utils.c b/jni/magiskboot/boot_utils.c index 6e37329a6..3c7cd9fe6 100644 --- a/jni/magiskboot/boot_utils.c +++ b/jni/magiskboot/boot_utils.c @@ -1,8 +1,8 @@ -#include "magiskboot.h" +#include +#include +#include -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 }; +#include "utils.h" void mmap_ro(const char *filename, void **buf, size_t *size) { int fd = xopen(filename, O_RDONLY); @@ -20,81 +20,6 @@ void mmap_rw(const char *filename, void **buf, size_t *size) { close(fd); } -file_t check_type(const void *buf) { - if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) { - return CHROMEOS; - } else if (memcmp(buf, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0) { - return AOSP; - } else if (memcmp(buf, ELF32_MAGIC, 5) == 0) { - return ELF32; - } else if (memcmp(buf, ELF64_MAGIC, 5) == 0) { - return ELF64; - } else if (memcmp(buf, GZIP_MAGIC, 4) == 0) { - return GZIP; - } else if (memcmp(buf, LZOP_MAGIC, 9) == 0) { - return LZOP; - } else if (memcmp(buf, XZ_MAGIC, 6) == 0) { - return XZ; - } else if (memcmp(buf, "\x5d\x00\x00", 3) == 0 - && (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) { - return LZMA; - } else if (memcmp(buf, BZIP_MAGIC, 3) == 0) { - return BZIP2; - } else if (memcmp(buf, LZ4_MAGIC, 4) == 0) { - return LZ4; - } else if (memcmp(buf, LZ4_LEG_MAGIC, 4) == 0) { - return LZ4_LEGACY; - } else if (memcmp(buf, MTK_MAGIC, 4) == 0) { - return MTK; - } else if (memcmp(buf, DTB_MAGIC, 4) == 0) { - return DTB; - } else { - return UNKNOWN; - } -} - -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); -} - void write_zero(int fd, size_t size) { size_t pos = lseek(fd, 0, SEEK_CUR); ftruncate(fd, pos + size); @@ -125,17 +50,3 @@ void file_align(int fd, size_t align, int out) { int open_new(const char *filename) { return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); } - -void cleanup() { - fprintf(stderr, "Cleaning up...\n"); - char name[PATH_MAX]; - unlink(KERNEL_FILE); - unlink(RAMDISK_FILE); - unlink(RAMDISK_FILE ".unsupport"); - unlink(SECOND_FILE); - unlink(DTB_FILE); - for (int i = 0; SUP_EXT_LIST[i]; ++i) { - sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]); - unlink(name); - } -} diff --git a/jni/magiskboot/bootimg.c b/jni/magiskboot/bootimg.c index 52c85dfb5..37f5f3c26 100644 --- a/jni/magiskboot/bootimg.c +++ b/jni/magiskboot/bootimg.c @@ -1,5 +1,11 @@ +#include +#include +#include + #include "bootimg.h" #include "magiskboot.h" +#include "utils.h" +#include "logging.h" static void dump(void *buf, size_t size, const char *filename) { int fd = open_new(filename); @@ -139,7 +145,7 @@ int parse_img(void *orig, size_t size, boot_img *boot) { continue; } } - LOGE(1, "No boot image magic found!\n"); + LOGE("No boot image magic found!\n"); } void unpack(const char* image) { @@ -165,7 +171,7 @@ void unpack(const char* image) { // Dump ramdisk if (boot.ramdisk_type == UNKNOWN) { dump(boot.ramdisk, boot.hdr.ramdisk_size, RAMDISK_FILE ".raw"); - LOGE(1, "Unknown ramdisk format! Dumped to %s\n", RAMDISK_FILE ".raw"); + LOGE("Unknown ramdisk format! Dumped to %s\n", RAMDISK_FILE ".raw"); } else { fd = open_new(RAMDISK_FILE); decomp(boot.ramdisk_type, fd, boot.ramdisk, boot.hdr.ramdisk_size); @@ -253,7 +259,7 @@ void repack(const char* orig_image, const char* out_image) { } } if (!found) - LOGE(1, "No ramdisk exists!\n"); + LOGE("No ramdisk exists!\n"); boot.hdr.ramdisk_size = restore(name, fd); } file_align(fd, boot.hdr.page_size, 1); diff --git a/jni/magiskboot/bootimg.h b/jni/magiskboot/bootimg.h index 15f1c6fba..2c412d2c6 100644 --- a/jni/magiskboot/bootimg.h +++ b/jni/magiskboot/bootimg.h @@ -16,7 +16,7 @@ */ #include -#include "magic.h" +#include "types.h" #ifndef _BOOT_IMAGE_H_ #define _BOOT_IMAGE_H_ diff --git a/jni/magiskboot/compress.c b/jni/magiskboot/compress.c index 179088618..2d13f0f71 100644 --- a/jni/magiskboot/compress.c +++ b/jni/magiskboot/compress.c @@ -1,3 +1,6 @@ +#include +#include + #include #include #include @@ -5,6 +8,8 @@ #include #include "magiskboot.h" +#include "logging.h" +#include "utils.h" #define windowBits 15 #define ZLIB_GZIP 16 @@ -33,11 +38,11 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) { ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY); break; default: - LOGE(1, "Unsupported gzip mode!\n"); + LOGE("Unsupported gzip mode!\n"); } if (ret != Z_OK) - LOGE(1, "Unable to init zlib stream\n"); + LOGE("Unable to init zlib stream\n"); do { strm.next_in = buf + pos; @@ -62,7 +67,7 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) { break; } if (ret == Z_STREAM_ERROR) - LOGE(1, "Error when running gzip\n"); + LOGE("Error when running gzip\n"); have = CHUNK - strm.avail_out; total += xwrite(fd, out, have); @@ -110,12 +115,12 @@ size_t lzma(int mode, int fd, const void *buf, size_t size) { ret = lzma_alone_encoder(&strm, &opt); break; default: - LOGE(1, "Unsupported lzma mode!\n"); + LOGE("Unsupported lzma mode!\n"); } if (ret != LZMA_OK) - LOGE(1, "Unable to init lzma stream\n"); + LOGE("Unable to init lzma stream\n"); do { strm.next_in = buf + pos; @@ -137,7 +142,7 @@ size_t lzma(int mode, int fd, const void *buf, size_t size) { } while (strm.avail_out == 0 && ret == LZMA_OK); if (ret != LZMA_OK && ret != LZMA_STREAM_END) - LOGE(1, "LZMA error %d!\n", ret); + LOGE("LZMA error %d!\n", ret); } while (pos < size); @@ -164,11 +169,11 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) { ret = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION); break; default: - LOGE(1, "Unsupported lz4 mode!\n"); + LOGE("Unsupported lz4 mode!\n"); } if (LZ4F_isError(ret)) - LOGE(1, "Context creation error: %s\n", LZ4F_getErrorName(ret)); + LOGE("Context creation error: %s\n", LZ4F_getErrorName(ret)); // Allocate out buffer blockSize = 1 << 22; @@ -178,7 +183,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) { read = blockSize; ret = LZ4F_getFrameInfo(dctx, &info, buf, &read); if (LZ4F_isError(ret)) - LOGE(1, "LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(ret)); + LOGE("LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(ret)); switch (info.blockSizeID) { case LZ4F_default: case LZ4F_max64KB: outCapacity = 1 << 16; break; @@ -186,7 +191,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) { case LZ4F_max1MB: outCapacity = 1 << 20; break; case LZ4F_max4MB: outCapacity = 1 << 22; break; default: - LOGE(1, "Impossible unless more block sizes are allowed\n"); + LOGE("Impossible unless more block sizes are allowed\n"); } pos += read; break; @@ -208,7 +213,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) { prefs.frameInfo.contentChecksumFlag = 1; have = ret = LZ4F_compressBegin(cctx, out, size, &prefs); if (LZ4F_isError(ret)) - LOGE(1, "Failed to start compression: error %s\n", LZ4F_getErrorName(ret)); + LOGE("Failed to start compression: error %s\n", LZ4F_getErrorName(ret)); total += xwrite(fd, out, have); } @@ -232,7 +237,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) { break; } if (LZ4F_isError(ret)) - LOGE(1, "LZ4 coding error: %s\n", LZ4F_getErrorName(ret)); + LOGE("LZ4 coding error: %s\n", LZ4F_getErrorName(ret)); total += xwrite(fd, out, have); // Update status @@ -249,7 +254,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) { case 1: have = ret = LZ4F_compressEnd(cctx, out, outCapacity, NULL); if (LZ4F_isError(ret)) - LOGE(1, "Failed to end compression: error %s\n", LZ4F_getErrorName(ret)); + LOGE("Failed to end compression: error %s\n", LZ4F_getErrorName(ret)); total += xwrite(fd, out, have); @@ -279,11 +284,11 @@ size_t bzip2(int mode, int fd, const void* buf, size_t size) { ret = BZ2_bzCompressInit(&strm, 9, 0, 0); break; default: - LOGE(1, "Unsupported bzip2 mode!\n"); + LOGE("Unsupported bzip2 mode!\n"); } if (ret != BZ_OK) - LOGE(1, "Unable to init bzlib stream\n"); + LOGE("Unable to init bzlib stream\n"); do { strm.next_in = (char *) buf + pos; @@ -346,7 +351,7 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) { total += xwrite(fd, "\x02\x21\x4c\x18", 4); break; default: - LOGE(1, "Unsupported lz4_legacy mode!\n"); + LOGE("Unsupported lz4_legacy mode!\n"); } do { @@ -359,10 +364,10 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) { block_size += ((unsigned)buff[pos + 3])<<24; pos += 4; if (block_size > LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE)) - LOGE(1, "lz4_legacy block size too large!\n"); + LOGE("lz4_legacy block size too large!\n"); have = LZ4_decompress_safe((const char*) (buf + pos), out, block_size, LZ4_LEGACY_BLOCKSIZE); if (have < 0) - LOGE(1, "Cannot decode lz4_legacy block\n"); + LOGE("Cannot decode lz4_legacy block\n"); pos += block_size; break; case 1: @@ -372,7 +377,7 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) { insize = LZ4_LEGACY_BLOCKSIZE; have = LZ4_compress_default((const char*) (buf + pos), out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE)); if (have == 0) - LOGE(1, "lz4_legacy compression error\n"); + LOGE("lz4_legacy compression error\n"); pos += insize; block_size_le[0] = have & 0xff; block_size_le[1] = (have >> 8) & 0xff; @@ -443,7 +448,7 @@ void decomp_file(char *from, const char *to) { char *ext; ext = strrchr(from, '.'); if (ext == NULL) - LOGE(1, "Bad filename extention\n"); + LOGE("Bad filename extention\n"); // File type and extension should match switch (type) { @@ -469,7 +474,7 @@ void decomp_file(char *from, const char *to) { ok = 0; break; default: - LOGE(1, "Provided file \'%s\' is not a supported archive format\n", from); + LOGE("Provided file \'%s\' is not a supported archive format\n", from); } if (ok) { // If all match, strip out the suffix @@ -486,7 +491,7 @@ void decomp_file(char *from, const char *to) { unlink(from); } } else { - LOGE(1, "Bad filename extention \'%s\'\n", ext); + LOGE("Bad filename extention \'%s\'\n", ext); } munmap(file, size); } diff --git a/jni/magiskboot/cpio.c b/jni/magiskboot/cpio.c index 539a753ae..2864cabbe 100644 --- a/jni/magiskboot/cpio.c +++ b/jni/magiskboot/cpio.c @@ -1,7 +1,10 @@ +#include +#include + #include "magiskboot.h" #include "cpio.h" -#include "vector.h" -#include "list.h" +#include "logging.h" +#include "utils.h" static uint32_t x8u(char *hex) { uint32_t val, inpos = 8, outpos; @@ -14,7 +17,7 @@ static uint32_t x8u(char *hex) { // Because scanf gratuitously treats %*X differently than printf does. sprintf(pattern, "%%%dx%%n", inpos); sscanf(hex, pattern, &val, &outpos); - if (inpos != outpos) LOGE(1, "bad cpio header\n"); + if (inpos != outpos) LOGE("bad cpio header\n"); return val; } @@ -332,7 +335,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector exit(0); } } - LOGE(1, "Cannot find the file entry [%s]\n", entry); + LOGE("Cannot find the file entry [%s]\n", entry); } static void cpio_backup(const char *orig, struct vector *v) { diff --git a/jni/magiskboot/hexpatch.c b/jni/magiskboot/hexpatch.c index b707116dd..cf5aa0e19 100644 --- a/jni/magiskboot/hexpatch.c +++ b/jni/magiskboot/hexpatch.c @@ -1,4 +1,10 @@ +#include +#include +#include +#include + #include "magiskboot.h" +#include "utils.h" static void hex2byte(const char *hex, unsigned char *str) { char high, low; diff --git a/jni/magiskboot/magiskboot.h b/jni/magiskboot/magiskboot.h index ce404cdaa..a0d473e53 100644 --- a/jni/magiskboot/magiskboot.h +++ b/jni/magiskboot/magiskboot.h @@ -1,22 +1,9 @@ #ifndef _MAGISKBOOT_H_ #define _MAGISKBOOT_H_ -#include -#include -#include -#include -#include #include -#include -#include -#include -#include #include "bootimg.h" -#include "sha1.h" -#include "logging.h" -#include "utils.h" -#include "magic.h" #define KERNEL_FILE "kernel" #define RAMDISK_FILE "ramdisk.cpio" @@ -27,10 +14,6 @@ #define str(a) #a #define xstr(a) str(a) -extern char *SUP_LIST[]; -extern char *SUP_EXT_LIST[]; -extern file_t SUP_TYPE_LIST[]; - // Main entries void unpack(const char *image); void repack(const char* orig_image, const char* out_image); @@ -39,7 +22,6 @@ int parse_img(void *orig, size_t size, boot_img *boot); int cpio_commands(const char *command, int argc, char *argv[]); void comp_file(const char *method, const char *from, const char *to); void decomp_file(char *from, const char *to); -void cleanup(); // Compressions size_t gzip(int mode, int fd, const void *buf, size_t size); @@ -51,13 +33,11 @@ long long comp(file_t type, int to, const void *from, size_t size); long long decomp(file_t type, int to, const void *from, size_t size); // Utils -void mmap_ro(const char *filename, void **buf, size_t *size); -void mmap_rw(const char *filename, void **buf, size_t *size); -file_t check_type(const void *buf); -void get_type_name(file_t type, char *name); -void write_zero(int fd, size_t size); -void mem_align(size_t *pos, size_t align); -void file_align(int fd, size_t align, int out); -int open_new(const char *filename); +extern void mmap_ro(const char *filename, void **buf, size_t *size); +extern void mmap_rw(const char *filename, void **buf, size_t *size); +extern void write_zero(int fd, size_t size); +extern void mem_align(size_t *pos, size_t align); +extern void file_align(int fd, size_t align, int out); +extern int open_new(const char *filename); #endif diff --git a/jni/magiskboot/main.c b/jni/magiskboot/main.c index dd7379064..99683b8d0 100644 --- a/jni/magiskboot/main.c +++ b/jni/magiskboot/main.c @@ -1,4 +1,11 @@ +#include +#include +#include +#include +#include + #include "magiskboot.h" +#include "sha1.h" /******************** Patch Boot Image @@ -6,55 +13,69 @@ static void usage(char *arg0) { fprintf(stderr, - "%s --unpack \n" - " Unpack to kernel, ramdisk.cpio, (second), (dtb) into the\n current directory\n" + "Usage: %s [args...]\n" "\n" - "%s --repack [outbootimg]\n" + "Supported actions:\n" + " --unpack \n" + " Unpack to kernel, ramdisk.cpio, (second), (dtb) into the\n" + " current directory\n" + "\n" + " --repack [outbootimg]\n" " Repack kernel, ramdisk.cpio[.ext], second, dtb... from current directory\n" " to [outbootimg], or new-boot.img if not specified.\n" " It will compress ramdisk.cpio with the same method used in \n" " if exists, or attempt to find ramdisk.cpio.[ext], and repack\n" " directly with the compressed ramdisk file\n" "\n" - "%s --hexpatch \n" + " --hexpatch \n" " Search in , and replace with \n" "\n" - "%s --cpio- [flags...] [params...]\n" - " Do cpio related cmds to (modifications are done directly)\n Supported commands and params:\n" - " -rm [-r] \n Remove entry from , flag -r to remove recursively\n" - " -mkdir \n Create directory as an \n" - " -add \n Add as an ; replaces if already exists\n" - " -mv \n Move to \n" - " -extract \n Extract to \n" - " -test \n Return value: 0/not patched 1/Magisk 2/Other (e.g. phh, SuperSU)\n" - " -patch \n Patch cpio for Magisk. KEEP**** are true/false values\n" - " -backup \n Create ramdisk backups into from \n" - " -restore\n Restore ramdisk from ramdisk backup within \n" - " -stocksha1\n Get stock boot SHA1 recorded within \n" + " --cpio- [flags...] [args...]\n" + " Do cpio related cmds to (modifications are done directly)\n" + " Supported commands:\n" + " -rm [-r] \n" + " Remove entry from , flag -r to remove recursively\n" + " -mkdir \n" + " Create directory as an \n" + " -add \n" + " Add as an ; replaces if already exists\n" + " -mv \n" + " Move to \n" + " -extract \n" + " Extract to \n" + " -test \n" + " Return value: 0/stock 1/Magisk 2/other (e.g. phh, SuperSU)\n" + " -patch \n" + " Patch cpio for Magisk. KEEP**** are true/false values\n" + " -backup \n" + " Create ramdisk backups into from \n" + " -restore\n" + " Restore ramdisk from ramdisk backup within \n" + " -stocksha1\n" + " Get stock boot SHA1 recorded within \n" "\n" - "%s --compress[=method] [outfile]\n" - " Compress with [method] (default: gzip), optionally to [outfile]\n Supported methods: " - , arg0, arg0, arg0, arg0, arg0); - for (int i = 0; SUP_LIST[i]; ++i) - fprintf(stderr, "%s ", SUP_LIST[i]); - fprintf(stderr, - "\n" - "\n" - "%s --decompress [outfile]\n" - " Detect method and decompress , optionally to [outfile]\n Supported methods: " + " --compress[=method] [outfile]\n" + " Compress with [method] (default: gzip), optionally to [outfile]\n" + " Supported methods: " , arg0); for (int i = 0; SUP_LIST[i]; ++i) fprintf(stderr, "%s ", SUP_LIST[i]); fprintf(stderr, "\n" "\n" - "%s --sha1 \n" + " --decompress [outfile]\n" + " Detect method and decompress , optionally to [outfile]\n Supported methods: "); + for (int i = 0; SUP_LIST[i]; ++i) + fprintf(stderr, "%s ", SUP_LIST[i]); + fprintf(stderr, + "\n" + "\n" + " --sha1 \n" " Print the SHA1 checksum for \n" "\n" - "%s --cleanup\n" + " --cleanup\n" " Cleanup the current working directory\n" - "\n" - , arg0, arg0); + "\n"); exit(1); } @@ -63,7 +84,17 @@ int main(int argc, char *argv[]) { fprintf(stderr, "MagiskBoot v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Boot Image Modification Tool\n\n"); if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) { - cleanup(); + fprintf(stderr, "Cleaning up...\n\n"); + char name[PATH_MAX]; + unlink(KERNEL_FILE); + unlink(RAMDISK_FILE); + unlink(RAMDISK_FILE ".raw"); + unlink(SECOND_FILE); + unlink(DTB_FILE); + for (int i = 0; SUP_EXT_LIST[i]; ++i) { + sprintf(name, "%s.%s", RAMDISK_FILE, SUP_EXT_LIST[i]); + unlink(name); + } } else if (argc > 2 && strcmp(argv[1], "--sha1") == 0) { char sha1[21], *buf; size_t size; diff --git a/jni/magiskboot/types.c b/jni/magiskboot/types.c new file mode 100644 index 000000000..275f87eb1 --- /dev/null +++ b/jni/magiskboot/types.c @@ -0,0 +1,83 @@ +#include + +#include "bootimg.h" +#include "types.h" + +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 }; + +file_t check_type(const void *buf) { + if (memcmp(buf, CHROMEOS_MAGIC, 8) == 0) { + return CHROMEOS; + } else if (memcmp(buf, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0) { + return AOSP; + } else if (memcmp(buf, ELF32_MAGIC, 5) == 0) { + return ELF32; + } else if (memcmp(buf, ELF64_MAGIC, 5) == 0) { + return ELF64; + } else if (memcmp(buf, GZIP_MAGIC, 4) == 0) { + return GZIP; + } else if (memcmp(buf, LZOP_MAGIC, 9) == 0) { + return LZOP; + } else if (memcmp(buf, XZ_MAGIC, 6) == 0) { + return XZ; + } else if (memcmp(buf, "\x5d\x00\x00", 3) == 0 + && (((char *)buf)[12] == '\xff' || ((char *)buf)[12] == '\x00')) { + return LZMA; + } else if (memcmp(buf, BZIP_MAGIC, 3) == 0) { + return BZIP2; + } else if (memcmp(buf, LZ4_MAGIC, 4) == 0) { + return LZ4; + } else if (memcmp(buf, LZ4_LEG_MAGIC, 4) == 0) { + return LZ4_LEGACY; + } else if (memcmp(buf, MTK_MAGIC, 4) == 0) { + return MTK; + } else if (memcmp(buf, DTB_MAGIC, 4) == 0) { + return DTB; + } else { + return UNKNOWN; + } +} + +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); +} diff --git a/jni/magiskboot/magic.h b/jni/magiskboot/types.h similarity index 78% rename from jni/magiskboot/magic.h rename to jni/magiskboot/types.h index 0ab3ea1d0..e4c851825 100644 --- a/jni/magiskboot/magic.h +++ b/jni/magiskboot/types.h @@ -1,5 +1,5 @@ -#ifndef _MAGIC_H_ -#define _MAGIC_H_ +#ifndef _TYPES_H_ +#define _TYPES_H_ typedef enum { UNKNOWN, @@ -31,4 +31,11 @@ typedef enum { #define DTB_MAGIC "\xd0\x0d\xfe\xed" #define LG_BUMP_MAGIC "\x41\xa9\xe4\x67\x74\x4d\x1d\x1b\xa4\x29\xf2\xec\xea\x65\x52\x79" +extern char *SUP_LIST[]; +extern char *SUP_EXT_LIST[]; +extern file_t SUP_TYPE_LIST[]; + +file_t check_type(const void *buf); +void get_type_name(file_t type, char *name); + #endif