From a3fcc64aaa022896437e9ff417bbe56905ffef0a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 18 Jul 2017 11:53:28 +0800 Subject: [PATCH] MagiskBoot log to stderr --- jni/magiskboot/boot_utils.c | 2 +- jni/magiskboot/bootimg.c | 42 ++++++++++++++++++------------------- jni/magiskboot/compress.c | 4 ++-- jni/magiskboot/cpio.c | 32 ++++++++++++++-------------- jni/magiskboot/hexpatch.c | 2 +- jni/magiskboot/main.c | 6 +++--- 6 files changed, 44 insertions(+), 44 deletions(-) diff --git a/jni/magiskboot/boot_utils.c b/jni/magiskboot/boot_utils.c index bc6e89d2c..463764d99 100644 --- a/jni/magiskboot/boot_utils.c +++ b/jni/magiskboot/boot_utils.c @@ -83,7 +83,7 @@ int open_new(const char *filename) { } void cleanup() { - printf("Cleaning up...\n"); + fprintf(stderr, "Cleaning up...\n"); char name[PATH_MAX]; unlink(KERNEL_FILE); unlink(RAMDISK_FILE); diff --git a/jni/magiskboot/bootimg.c b/jni/magiskboot/bootimg.c index 63c7c9d02..286a10ad7 100644 --- a/jni/magiskboot/bootimg.c +++ b/jni/magiskboot/bootimg.c @@ -26,11 +26,11 @@ static void restore_buf(int fd, const void *buf, size_t size) { } static void print_info() { - printf("KERNEL [%d] @ 0x%08x\n", hdr.kernel_size, hdr.kernel_addr); - printf("RAMDISK [%d] @ 0x%08x\n", hdr.ramdisk_size, hdr.ramdisk_addr); - printf("SECOND [%d] @ 0x%08x\n", hdr.second_size, hdr.second_addr); - printf("DTB [%d] @ 0x%08x\n", hdr.dt_size, hdr.tags_addr); - printf("PAGESIZE [%d]\n", hdr.page_size); + fprintf(stderr, "KERNEL [%d] @ 0x%08x\n", hdr.kernel_size, hdr.kernel_addr); + fprintf(stderr, "RAMDISK [%d] @ 0x%08x\n", hdr.ramdisk_size, hdr.ramdisk_addr); + fprintf(stderr, "SECOND [%d] @ 0x%08x\n", hdr.second_size, hdr.second_addr); + fprintf(stderr, "DTB [%d] @ 0x%08x\n", hdr.dt_size, hdr.tags_addr); + fprintf(stderr, "PAGESIZE [%d]\n", hdr.page_size); if (hdr.os_version != 0) { int a,b,c,y,m = 0; int os_version, os_patch_level; @@ -40,38 +40,38 @@ static void print_info() { a = (os_version >> 14) & 0x7f; b = (os_version >> 7) & 0x7f; c = os_version & 0x7f; - printf("OS_VERSION [%d.%d.%d]\n", a, b, c); + fprintf(stderr, "OS_VERSION [%d.%d.%d]\n", a, b, c); y = (os_patch_level >> 4) + 2000; m = os_patch_level & 0xf; - printf("PATCH_LEVEL [%d-%02d]\n", y, m); + fprintf(stderr, "PATCH_LEVEL [%d-%02d]\n", y, m); } - printf("NAME [%s]\n", hdr.name); - printf("CMDLINE [%s]\n", hdr.cmdline); + fprintf(stderr, "NAME [%s]\n", hdr.name); + fprintf(stderr, "CMDLINE [%s]\n", hdr.cmdline); switch (ramdisk_type) { case GZIP: - printf("COMPRESSION [%s]\n", "gzip"); + fprintf(stderr, "COMPRESSION [%s]\n", "gzip"); break; case XZ: - printf("COMPRESSION [%s]\n", "xz"); + fprintf(stderr, "COMPRESSION [%s]\n", "xz"); break; case LZMA: - printf("COMPRESSION [%s]\n", "lzma"); + fprintf(stderr, "COMPRESSION [%s]\n", "lzma"); break; case BZIP2: - printf("COMPRESSION [%s]\n", "bzip2"); + fprintf(stderr, "COMPRESSION [%s]\n", "bzip2"); break; case LZ4: - printf("COMPRESSION [%s]\n", "lz4"); + fprintf(stderr, "COMPRESSION [%s]\n", "lz4"); break; case LZ4_LEGACY: - printf("COMPRESSION [%s]\n", "lz4_legacy"); + fprintf(stderr, "COMPRESSION [%s]\n", "lz4_legacy"); break; default: fprintf(stderr, "Unknown ramdisk format!\n"); } - printf("\n"); + fprintf(stderr, "\n"); } int parse_img(unsigned char *orig, size_t size) { @@ -126,11 +126,11 @@ int parse_img(unsigned char *orig, size_t size) { // Check MTK if (check_type(kernel) == MTK) { - printf("MTK header found in kernel\n"); + fprintf(stderr, "MTK header found in kernel\n"); mtk_kernel = 1; } if (ramdisk_type == MTK) { - printf("MTK header found in ramdisk\n"); + fprintf(stderr, "MTK header found in ramdisk\n"); mtk_ramdisk = 1; ramdisk_type = check_type(ramdisk + 512); } @@ -151,7 +151,7 @@ void unpack(const char* image) { mmap_ro(image, &orig, &size); // Parse image - printf("Parsing boot image: [%s]\n\n", image); + fprintf(stderr, "Parsing boot image: [%s]\n\n", image); int ret = parse_img(orig, size); // Dump kernel @@ -199,10 +199,10 @@ void repack(const char* orig_image, const char* out_image) { mmap_ro(orig_image, &orig, &size); // Parse original image - printf("Parsing boot image: [%s]\n\n", orig_image); + fprintf(stderr, "Parsing boot image: [%s]\n\n", orig_image); parse_img(orig, size); - printf("Repack to boot image: [%s]\n\n", out_image); + fprintf(stderr, "Repack to boot image: [%s]\n\n", out_image); // Create new image int fd = open_new(out_image); diff --git a/jni/magiskboot/compress.c b/jni/magiskboot/compress.c index 5c815b55e..a4d14b59f 100644 --- a/jni/magiskboot/compress.c +++ b/jni/magiskboot/compress.c @@ -22,10 +22,10 @@ static void write_file(const int fd, const void *buf, const size_t size, const c static void report(const int mode, const char* filename) { switch(mode) { case 0: - printf("Decompressing to [%s]\n\n", filename); + fprintf(stderr, "Decompressing to [%s]\n\n", filename); break; default: - printf("Compressing to [%s]\n\n", filename); + fprintf(stderr, "Compressing to [%s]\n\n", filename); break; } } diff --git a/jni/magiskboot/cpio.c b/jni/magiskboot/cpio.c index be196f3ba..cbc743f1f 100644 --- a/jni/magiskboot/cpio.c +++ b/jni/magiskboot/cpio.c @@ -62,7 +62,7 @@ static int cpio_compare(const void *a, const void *b) { // Parse cpio file to a vector of cpio_file static void parse_cpio(const char *filename, struct vector *v) { - printf("Loading cpio: [%s]\n\n", filename); + fprintf(stderr, "Loading cpio: [%s]\n\n", filename); int fd = xopen(filename, O_RDONLY); cpio_newc_header header; cpio_file *f; @@ -105,7 +105,7 @@ static void parse_cpio(const char *filename, struct vector *v) { } static void dump_cpio(const char *filename, struct vector *v) { - printf("\nDump cpio: [%s]\n\n", filename); + fprintf(stderr, "\nDump cpio: [%s]\n\n", filename); int fd = open_new(filename); unsigned inode = 300000; char header[111]; @@ -158,7 +158,7 @@ static void cpio_rm(int recursive, const char *entry, struct vector *v) { if ((recursive && strncmp(f->filename, entry, strlen(entry)) == 0) || (strcmp(f->filename, entry) == 0) ) { if (!f->remove) { - printf("Remove [%s]\n", entry); + fprintf(stderr, "Remove [%s]\n", entry); f->remove = 1; } if (!recursive) return; @@ -173,7 +173,7 @@ static void cpio_mkdir(mode_t mode, const char *entry, struct vector *v) { f->filename = xmalloc(f->namesize); memcpy(f->filename, entry, f->namesize); cpio_vec_insert(v, f); - printf("Create directory [%s] (%04o)\n",entry, mode); + fprintf(stderr, "Create directory [%s] (%04o)\n",entry, mode); } static void cpio_add(mode_t mode, const char *entry, const char *filename, struct vector *v) { @@ -189,7 +189,7 @@ static void cpio_add(mode_t mode, const char *entry, const char *filename, struc xxread(fd, f->data, f->filesize); close(fd); cpio_vec_insert(v, f); - printf("Add entry [%s] (%04o)\n", entry, mode); + fprintf(stderr, "Add entry [%s] (%04o)\n", entry, mode); } static void cpio_test(struct vector *v) { @@ -270,7 +270,7 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) { if (injected) continue; // Inject magisk script as import - printf("Inject new line [import /init.magisk.rc] in [init.rc]\n"); + fprintf(stderr, "Inject new line [import /init.magisk.rc] in [init.rc]\n"); line = xcalloc(sizeof(*line), 1); line->line = strdup("import /init.magisk.rc"); line->isNew = 1; @@ -279,7 +279,7 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) { injected = 1; } else if (strstr(line->line, "selinux.reload_policy")) { // Remove this line - printf("Remove line [%s] in [init.rc]\n", line->line); + fprintf(stderr, "Remove line [%s] in [init.rc]\n", line->line); f->filesize -= strlen(line->line) + 1; __ = list_pop(&line->pos); free(line); @@ -296,14 +296,14 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) { for (read = 0, write = 0; read < f->filesize; ++read, ++write) { skip = check_verity_pattern(f->data + read); if (skip > 0) { - printf("Remove pattern [%.*s] in [%s]\n", skip, f->data + read, f->filename); + fprintf(stderr, "Remove pattern [%.*s] in [%s]\n", skip, f->data + read, f->filename); read += skip; } f->data[write] = f->data[read]; } f->filesize = write; } else if (strcmp(f->filename, "verity_key") == 0) { - printf("Remove [verity_key]\n"); + fprintf(stderr, "Remove [verity_key]\n"); f->remove = 1; } } @@ -313,7 +313,7 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) { for (int i = 0 ; ENCRYPT_LIST[i]; ++i) { if (strncmp(f->data + read, ENCRYPT_LIST[i], strlen(ENCRYPT_LIST[i])) == 0) { memcpy(f->data + write, "encryptable", 11); - printf("Replace [%s] with [%s] in [%s]\n", ENCRYPT_LIST[i], "encryptable", f->filename); + fprintf(stderr, "Replace [%s] with [%s] in [%s]\n", ENCRYPT_LIST[i], "encryptable", f->filename); write += 11; read += strlen(ENCRYPT_LIST[i]); break; @@ -332,7 +332,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector cpio_file *f; vec_for_each(v, f) { if (strcmp(f->filename, entry) == 0 && S_ISREG(f->mode)) { - printf("Extracting [%s] to [%s]\n\n", entry, filename); + fprintf(stderr, "Extracting [%s] to [%s]\n\n", entry, filename); int fd = open_new(filename); xwrite(fd, f->data, f->filesize); fchmod(fd, f->mode); @@ -390,14 +390,14 @@ static void cpio_backup(const char *orig, struct vector *v) { // Something is missing in new ramdisk, backup! ++i; doBak = 1; - printf("Backup missing entry: "); + fprintf(stderr, "Backup missing entry: "); } else if (res == 0) { ++i; ++j; if (m->filesize == n->filesize && memcmp(m->data, n->data, m->filesize) == 0) continue; // Not the same! doBak = 1; - printf("Backup mismatch entry: "); + fprintf(stderr, "Backup mismatch entry: "); } else { // Someting new in ramdisk, record in rem ++j; @@ -405,14 +405,14 @@ static void cpio_backup(const char *orig, struct vector *v) { rem->data = xrealloc(rem->data, rem->filesize + n->namesize); memcpy(rem->data + rem->filesize, n->filename, n->namesize); rem->filesize += n->namesize; - printf("Record new entry: [%s] -> [.backup/.rmlist]\n", n->filename); + fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", n->filename); } if (doBak) { m->namesize += 8; m->filename = realloc(m->filename, m->namesize); strcpy(buf, m->filename); sprintf(m->filename, ".backup/%s", buf); - printf("[%s] -> [%s]\n", buf, m->filename); + fprintf(stderr, "[%s] -> [%s]\n", buf, m->filename); vec_push_back(&bak, m); // NULL the original entry, so it won't be freed vec_entry(o)[i - 1] = NULL; @@ -459,7 +459,7 @@ static int cpio_restore(struct vector *v) { n->data = xmalloc(n->filesize); memcpy(n->data, f->data, n->filesize); n->remove = 0; - printf("Restoring [%s] -> [%s]\n", f->filename, n->filename); + fprintf(stderr, "Restoring [%s] -> [%s]\n", f->filename, n->filename); cpio_vec_insert(v, n); } } diff --git a/jni/magiskboot/hexpatch.c b/jni/magiskboot/hexpatch.c index 764c33fb0..0d67503a9 100644 --- a/jni/magiskboot/hexpatch.c +++ b/jni/magiskboot/hexpatch.c @@ -20,7 +20,7 @@ void hexpatch(const char *image, const char *from, const char *to) { hex2byte(to, patch); for (size_t i = 0; i < filesize - patternsize; ++i) { if (memcmp(file + i, pattern, patternsize) == 0) { - printf("Pattern %s found!\nPatching to %s\n", from, to); + fprintf(stderr, "Pattern %s found!\nPatching to %s\n", from, to); memset(file + i, 0, patternsize); memcpy(file + i, patch, patchsize); i += patternsize - 1; diff --git a/jni/magiskboot/main.c b/jni/magiskboot/main.c index 0653bae7e..7b2402463 100644 --- a/jni/magiskboot/main.c +++ b/jni/magiskboot/main.c @@ -58,7 +58,7 @@ static void usage(char *arg0) { } int main(int argc, char *argv[]) { - printf("MagiskBoot v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - Boot Image Modification Tool\n\n"); + 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(); @@ -68,8 +68,8 @@ int main(int argc, char *argv[]) { mmap_ro(argv[2], (unsigned char **) &buf, &size); SHA1(sha1, buf, size); for (int i = 0; i < 20; ++i) - printf("%02x", sha1[i]); - printf("\n"); + fprintf(stderr, "%02x", sha1[i]); + fprintf(stderr, "\n"); munmap(buf, size); } else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) { unpack(argv[2]);