MagiskBoot log to stderr

This commit is contained in:
topjohnwu 2017-07-18 11:53:28 +08:00
parent f3078bc903
commit a3fcc64aaa
6 changed files with 44 additions and 44 deletions

View File

@ -83,7 +83,7 @@ int open_new(const char *filename) {
} }
void cleanup() { void cleanup() {
printf("Cleaning up...\n"); fprintf(stderr, "Cleaning up...\n");
char name[PATH_MAX]; char name[PATH_MAX];
unlink(KERNEL_FILE); unlink(KERNEL_FILE);
unlink(RAMDISK_FILE); unlink(RAMDISK_FILE);

View File

@ -26,11 +26,11 @@ static void restore_buf(int fd, const void *buf, size_t size) {
} }
static void print_info() { static void print_info() {
printf("KERNEL [%d] @ 0x%08x\n", hdr.kernel_size, hdr.kernel_addr); fprintf(stderr, "KERNEL [%d] @ 0x%08x\n", hdr.kernel_size, hdr.kernel_addr);
printf("RAMDISK [%d] @ 0x%08x\n", hdr.ramdisk_size, hdr.ramdisk_addr); fprintf(stderr, "RAMDISK [%d] @ 0x%08x\n", hdr.ramdisk_size, hdr.ramdisk_addr);
printf("SECOND [%d] @ 0x%08x\n", hdr.second_size, hdr.second_addr); fprintf(stderr, "SECOND [%d] @ 0x%08x\n", hdr.second_size, hdr.second_addr);
printf("DTB [%d] @ 0x%08x\n", hdr.dt_size, hdr.tags_addr); fprintf(stderr, "DTB [%d] @ 0x%08x\n", hdr.dt_size, hdr.tags_addr);
printf("PAGESIZE [%d]\n", hdr.page_size); fprintf(stderr, "PAGESIZE [%d]\n", hdr.page_size);
if (hdr.os_version != 0) { if (hdr.os_version != 0) {
int a,b,c,y,m = 0; int a,b,c,y,m = 0;
int os_version, os_patch_level; int os_version, os_patch_level;
@ -40,38 +40,38 @@ static void print_info() {
a = (os_version >> 14) & 0x7f; a = (os_version >> 14) & 0x7f;
b = (os_version >> 7) & 0x7f; b = (os_version >> 7) & 0x7f;
c = os_version & 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; y = (os_patch_level >> 4) + 2000;
m = os_patch_level & 0xf; 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); fprintf(stderr, "NAME [%s]\n", hdr.name);
printf("CMDLINE [%s]\n", hdr.cmdline); fprintf(stderr, "CMDLINE [%s]\n", hdr.cmdline);
switch (ramdisk_type) { switch (ramdisk_type) {
case GZIP: case GZIP:
printf("COMPRESSION [%s]\n", "gzip"); fprintf(stderr, "COMPRESSION [%s]\n", "gzip");
break; break;
case XZ: case XZ:
printf("COMPRESSION [%s]\n", "xz"); fprintf(stderr, "COMPRESSION [%s]\n", "xz");
break; break;
case LZMA: case LZMA:
printf("COMPRESSION [%s]\n", "lzma"); fprintf(stderr, "COMPRESSION [%s]\n", "lzma");
break; break;
case BZIP2: case BZIP2:
printf("COMPRESSION [%s]\n", "bzip2"); fprintf(stderr, "COMPRESSION [%s]\n", "bzip2");
break; break;
case LZ4: case LZ4:
printf("COMPRESSION [%s]\n", "lz4"); fprintf(stderr, "COMPRESSION [%s]\n", "lz4");
break; break;
case LZ4_LEGACY: case LZ4_LEGACY:
printf("COMPRESSION [%s]\n", "lz4_legacy"); fprintf(stderr, "COMPRESSION [%s]\n", "lz4_legacy");
break; break;
default: default:
fprintf(stderr, "Unknown ramdisk format!\n"); fprintf(stderr, "Unknown ramdisk format!\n");
} }
printf("\n"); fprintf(stderr, "\n");
} }
int parse_img(unsigned char *orig, size_t size) { int parse_img(unsigned char *orig, size_t size) {
@ -126,11 +126,11 @@ int parse_img(unsigned char *orig, size_t size) {
// Check MTK // Check MTK
if (check_type(kernel) == MTK) { if (check_type(kernel) == MTK) {
printf("MTK header found in kernel\n"); fprintf(stderr, "MTK header found in kernel\n");
mtk_kernel = 1; mtk_kernel = 1;
} }
if (ramdisk_type == MTK) { if (ramdisk_type == MTK) {
printf("MTK header found in ramdisk\n"); fprintf(stderr, "MTK header found in ramdisk\n");
mtk_ramdisk = 1; mtk_ramdisk = 1;
ramdisk_type = check_type(ramdisk + 512); ramdisk_type = check_type(ramdisk + 512);
} }
@ -151,7 +151,7 @@ void unpack(const char* image) {
mmap_ro(image, &orig, &size); mmap_ro(image, &orig, &size);
// Parse image // 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); int ret = parse_img(orig, size);
// Dump kernel // Dump kernel
@ -199,10 +199,10 @@ void repack(const char* orig_image, const char* out_image) {
mmap_ro(orig_image, &orig, &size); mmap_ro(orig_image, &orig, &size);
// Parse original image // 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); 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 // Create new image
int fd = open_new(out_image); int fd = open_new(out_image);

View File

@ -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) { static void report(const int mode, const char* filename) {
switch(mode) { switch(mode) {
case 0: case 0:
printf("Decompressing to [%s]\n\n", filename); fprintf(stderr, "Decompressing to [%s]\n\n", filename);
break; break;
default: default:
printf("Compressing to [%s]\n\n", filename); fprintf(stderr, "Compressing to [%s]\n\n", filename);
break; break;
} }
} }

View File

@ -62,7 +62,7 @@ static int cpio_compare(const void *a, const void *b) {
// Parse cpio file to a vector of cpio_file // Parse cpio file to a vector of cpio_file
static void parse_cpio(const char *filename, struct vector *v) { 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); int fd = xopen(filename, O_RDONLY);
cpio_newc_header header; cpio_newc_header header;
cpio_file *f; 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) { 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); int fd = open_new(filename);
unsigned inode = 300000; unsigned inode = 300000;
char header[111]; 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) if ((recursive && strncmp(f->filename, entry, strlen(entry)) == 0)
|| (strcmp(f->filename, entry) == 0) ) { || (strcmp(f->filename, entry) == 0) ) {
if (!f->remove) { if (!f->remove) {
printf("Remove [%s]\n", entry); fprintf(stderr, "Remove [%s]\n", entry);
f->remove = 1; f->remove = 1;
} }
if (!recursive) return; 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); f->filename = xmalloc(f->namesize);
memcpy(f->filename, entry, f->namesize); memcpy(f->filename, entry, f->namesize);
cpio_vec_insert(v, f); 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) { 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); xxread(fd, f->data, f->filesize);
close(fd); close(fd);
cpio_vec_insert(v, f); 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) { static void cpio_test(struct vector *v) {
@ -270,7 +270,7 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
if (injected) if (injected)
continue; continue;
// Inject magisk script as import // 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 = xcalloc(sizeof(*line), 1);
line->line = strdup("import /init.magisk.rc"); line->line = strdup("import /init.magisk.rc");
line->isNew = 1; line->isNew = 1;
@ -279,7 +279,7 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
injected = 1; injected = 1;
} else if (strstr(line->line, "selinux.reload_policy")) { } else if (strstr(line->line, "selinux.reload_policy")) {
// Remove this line // 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; f->filesize -= strlen(line->line) + 1;
__ = list_pop(&line->pos); __ = list_pop(&line->pos);
free(line); 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) { for (read = 0, write = 0; read < f->filesize; ++read, ++write) {
skip = check_verity_pattern(f->data + read); skip = check_verity_pattern(f->data + read);
if (skip > 0) { 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; read += skip;
} }
f->data[write] = f->data[read]; f->data[write] = f->data[read];
} }
f->filesize = write; f->filesize = write;
} else if (strcmp(f->filename, "verity_key") == 0) { } else if (strcmp(f->filename, "verity_key") == 0) {
printf("Remove [verity_key]\n"); fprintf(stderr, "Remove [verity_key]\n");
f->remove = 1; 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) { for (int i = 0 ; ENCRYPT_LIST[i]; ++i) {
if (strncmp(f->data + read, ENCRYPT_LIST[i], strlen(ENCRYPT_LIST[i])) == 0) { if (strncmp(f->data + read, ENCRYPT_LIST[i], strlen(ENCRYPT_LIST[i])) == 0) {
memcpy(f->data + write, "encryptable", 11); 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; write += 11;
read += strlen(ENCRYPT_LIST[i]); read += strlen(ENCRYPT_LIST[i]);
break; break;
@ -332,7 +332,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector
cpio_file *f; cpio_file *f;
vec_for_each(v, f) { vec_for_each(v, f) {
if (strcmp(f->filename, entry) == 0 && S_ISREG(f->mode)) { 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); int fd = open_new(filename);
xwrite(fd, f->data, f->filesize); xwrite(fd, f->data, f->filesize);
fchmod(fd, f->mode); 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! // Something is missing in new ramdisk, backup!
++i; ++i;
doBak = 1; doBak = 1;
printf("Backup missing entry: "); fprintf(stderr, "Backup missing entry: ");
} else if (res == 0) { } else if (res == 0) {
++i; ++j; ++i; ++j;
if (m->filesize == n->filesize && memcmp(m->data, n->data, m->filesize) == 0) if (m->filesize == n->filesize && memcmp(m->data, n->data, m->filesize) == 0)
continue; continue;
// Not the same! // Not the same!
doBak = 1; doBak = 1;
printf("Backup mismatch entry: "); fprintf(stderr, "Backup mismatch entry: ");
} else { } else {
// Someting new in ramdisk, record in rem // Someting new in ramdisk, record in rem
++j; ++j;
@ -405,14 +405,14 @@ static void cpio_backup(const char *orig, struct vector *v) {
rem->data = xrealloc(rem->data, rem->filesize + n->namesize); rem->data = xrealloc(rem->data, rem->filesize + n->namesize);
memcpy(rem->data + rem->filesize, n->filename, n->namesize); memcpy(rem->data + rem->filesize, n->filename, n->namesize);
rem->filesize += 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) { if (doBak) {
m->namesize += 8; m->namesize += 8;
m->filename = realloc(m->filename, m->namesize); m->filename = realloc(m->filename, m->namesize);
strcpy(buf, m->filename); strcpy(buf, m->filename);
sprintf(m->filename, ".backup/%s", buf); 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); vec_push_back(&bak, m);
// NULL the original entry, so it won't be freed // NULL the original entry, so it won't be freed
vec_entry(o)[i - 1] = NULL; vec_entry(o)[i - 1] = NULL;
@ -459,7 +459,7 @@ static int cpio_restore(struct vector *v) {
n->data = xmalloc(n->filesize); n->data = xmalloc(n->filesize);
memcpy(n->data, f->data, n->filesize); memcpy(n->data, f->data, n->filesize);
n->remove = 0; 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); cpio_vec_insert(v, n);
} }
} }

View File

@ -20,7 +20,7 @@ void hexpatch(const char *image, const char *from, const char *to) {
hex2byte(to, patch); hex2byte(to, patch);
for (size_t i = 0; i < filesize - patternsize; ++i) { for (size_t i = 0; i < filesize - patternsize; ++i) {
if (memcmp(file + i, pattern, patternsize) == 0) { 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); memset(file + i, 0, patternsize);
memcpy(file + i, patch, patchsize); memcpy(file + i, patch, patchsize);
i += patternsize - 1; i += patternsize - 1;

View File

@ -58,7 +58,7 @@ static void usage(char *arg0) {
} }
int main(int argc, char *argv[]) { 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) { if (argc > 1 && strcmp(argv[1], "--cleanup") == 0) {
cleanup(); cleanup();
@ -68,8 +68,8 @@ int main(int argc, char *argv[]) {
mmap_ro(argv[2], (unsigned char **) &buf, &size); mmap_ro(argv[2], (unsigned char **) &buf, &size);
SHA1(sha1, buf, size); SHA1(sha1, buf, size);
for (int i = 0; i < 20; ++i) for (int i = 0; i < 20; ++i)
printf("%02x", sha1[i]); fprintf(stderr, "%02x", sha1[i]);
printf("\n"); fprintf(stderr, "\n");
munmap(buf, size); munmap(buf, size);
} else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) { } else if (argc > 2 && strcmp(argv[1], "--unpack") == 0) {
unpack(argv[2]); unpack(argv[2]);