Re-organize MagiskBoot

This commit is contained in:
topjohnwu 2017-09-14 23:11:56 +08:00
parent 1816ca6b02
commit 8b2ec23a89
12 changed files with 216 additions and 183 deletions

View File

@ -80,6 +80,7 @@ LOCAL_SRC_FILES := \
magiskboot/boot_utils.c \ magiskboot/boot_utils.c \
magiskboot/cpio.c \ magiskboot/cpio.c \
magiskboot/sha1.c \ magiskboot/sha1.c \
magiskboot/types.c \
utils/xwrap.c \ utils/xwrap.c \
utils/vector.c \ utils/vector.c \
utils/list.c utils/list.c

View File

@ -41,7 +41,7 @@ static inline void stub(const char *fmt, ...) {}
#include <stdio.h> #include <stdio.h>
#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); } #define PLOGE(fmt, args...) { fprintf(stderr, fmt " failed with %d: %s\n\n", ##args, errno, strerror(errno)); exit(1); }
#endif // IS_DAEMON #endif // IS_DAEMON

View File

@ -1,8 +1,8 @@
#include "magiskboot.h" #include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
char *SUP_LIST[] = { "gzip", "xz", "lzma", "bzip2", "lz4", "lz4_legacy", NULL }; #include "utils.h"
char *SUP_EXT_LIST[] = { "gz", "xz", "lzma", "bz2", "lz4", "lz4", NULL };
file_t SUP_TYPE_LIST[] = { GZIP, XZ, LZMA, BZIP2, LZ4, LZ4_LEGACY, 0 };
void mmap_ro(const char *filename, void **buf, size_t *size) { void mmap_ro(const char *filename, void **buf, size_t *size) {
int fd = xopen(filename, O_RDONLY); int fd = xopen(filename, O_RDONLY);
@ -20,81 +20,6 @@ void mmap_rw(const char *filename, void **buf, size_t *size) {
close(fd); 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) { void write_zero(int fd, size_t size) {
size_t pos = lseek(fd, 0, SEEK_CUR); size_t pos = lseek(fd, 0, SEEK_CUR);
ftruncate(fd, pos + size); ftruncate(fd, pos + size);
@ -125,17 +50,3 @@ void file_align(int fd, size_t align, int out) {
int open_new(const char *filename) { int open_new(const char *filename) {
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); 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);
}
}

View File

@ -1,5 +1,11 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include "bootimg.h" #include "bootimg.h"
#include "magiskboot.h" #include "magiskboot.h"
#include "utils.h"
#include "logging.h"
static void dump(void *buf, size_t size, const char *filename) { static void dump(void *buf, size_t size, const char *filename) {
int fd = open_new(filename); int fd = open_new(filename);
@ -139,7 +145,7 @@ int parse_img(void *orig, size_t size, boot_img *boot) {
continue; continue;
} }
} }
LOGE(1, "No boot image magic found!\n"); LOGE("No boot image magic found!\n");
} }
void unpack(const char* image) { void unpack(const char* image) {
@ -165,7 +171,7 @@ void unpack(const char* image) {
// Dump ramdisk // Dump ramdisk
if (boot.ramdisk_type == UNKNOWN) { if (boot.ramdisk_type == UNKNOWN) {
dump(boot.ramdisk, boot.hdr.ramdisk_size, RAMDISK_FILE ".raw"); 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 { } else {
fd = open_new(RAMDISK_FILE); fd = open_new(RAMDISK_FILE);
decomp(boot.ramdisk_type, fd, boot.ramdisk, boot.hdr.ramdisk_size); 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) if (!found)
LOGE(1, "No ramdisk exists!\n"); LOGE("No ramdisk exists!\n");
boot.hdr.ramdisk_size = restore(name, fd); boot.hdr.ramdisk_size = restore(name, fd);
} }
file_align(fd, boot.hdr.page_size, 1); file_align(fd, boot.hdr.page_size, 1);

View File

@ -16,7 +16,7 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include "magic.h" #include "types.h"
#ifndef _BOOT_IMAGE_H_ #ifndef _BOOT_IMAGE_H_
#define _BOOT_IMAGE_H_ #define _BOOT_IMAGE_H_

View File

@ -1,3 +1,6 @@
#include <unistd.h>
#include <sys/mman.h>
#include <zlib.h> #include <zlib.h>
#include <lzma.h> #include <lzma.h>
#include <lz4.h> #include <lz4.h>
@ -5,6 +8,8 @@
#include <bzlib.h> #include <bzlib.h>
#include "magiskboot.h" #include "magiskboot.h"
#include "logging.h"
#include "utils.h"
#define windowBits 15 #define windowBits 15
#define ZLIB_GZIP 16 #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); ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY);
break; break;
default: default:
LOGE(1, "Unsupported gzip mode!\n"); LOGE("Unsupported gzip mode!\n");
} }
if (ret != Z_OK) if (ret != Z_OK)
LOGE(1, "Unable to init zlib stream\n"); LOGE("Unable to init zlib stream\n");
do { do {
strm.next_in = buf + pos; strm.next_in = buf + pos;
@ -62,7 +67,7 @@ size_t gzip(int mode, int fd, const void *buf, size_t size) {
break; break;
} }
if (ret == Z_STREAM_ERROR) if (ret == Z_STREAM_ERROR)
LOGE(1, "Error when running gzip\n"); LOGE("Error when running gzip\n");
have = CHUNK - strm.avail_out; have = CHUNK - strm.avail_out;
total += xwrite(fd, out, have); 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); ret = lzma_alone_encoder(&strm, &opt);
break; break;
default: default:
LOGE(1, "Unsupported lzma mode!\n"); LOGE("Unsupported lzma mode!\n");
} }
if (ret != LZMA_OK) if (ret != LZMA_OK)
LOGE(1, "Unable to init lzma stream\n"); LOGE("Unable to init lzma stream\n");
do { do {
strm.next_in = buf + pos; 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); } while (strm.avail_out == 0 && ret == LZMA_OK);
if (ret != LZMA_OK && ret != LZMA_STREAM_END) if (ret != LZMA_OK && ret != LZMA_STREAM_END)
LOGE(1, "LZMA error %d!\n", ret); LOGE("LZMA error %d!\n", ret);
} while (pos < size); } 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); ret = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION);
break; break;
default: default:
LOGE(1, "Unsupported lz4 mode!\n"); LOGE("Unsupported lz4 mode!\n");
} }
if (LZ4F_isError(ret)) 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 // Allocate out buffer
blockSize = 1 << 22; blockSize = 1 << 22;
@ -178,7 +183,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
read = blockSize; read = blockSize;
ret = LZ4F_getFrameInfo(dctx, &info, buf, &read); ret = LZ4F_getFrameInfo(dctx, &info, buf, &read);
if (LZ4F_isError(ret)) 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) { switch (info.blockSizeID) {
case LZ4F_default: case LZ4F_default:
case LZ4F_max64KB: outCapacity = 1 << 16; break; 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_max1MB: outCapacity = 1 << 20; break;
case LZ4F_max4MB: outCapacity = 1 << 22; break; case LZ4F_max4MB: outCapacity = 1 << 22; break;
default: default:
LOGE(1, "Impossible unless more block sizes are allowed\n"); LOGE("Impossible unless more block sizes are allowed\n");
} }
pos += read; pos += read;
break; break;
@ -208,7 +213,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
prefs.frameInfo.contentChecksumFlag = 1; prefs.frameInfo.contentChecksumFlag = 1;
have = ret = LZ4F_compressBegin(cctx, out, size, &prefs); have = ret = LZ4F_compressBegin(cctx, out, size, &prefs);
if (LZ4F_isError(ret)) 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); total += xwrite(fd, out, have);
} }
@ -232,7 +237,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
break; break;
} }
if (LZ4F_isError(ret)) 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); total += xwrite(fd, out, have);
// Update status // Update status
@ -249,7 +254,7 @@ size_t lz4(int mode, int fd, const void *buf, size_t size) {
case 1: case 1:
have = ret = LZ4F_compressEnd(cctx, out, outCapacity, NULL); have = ret = LZ4F_compressEnd(cctx, out, outCapacity, NULL);
if (LZ4F_isError(ret)) 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); 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); ret = BZ2_bzCompressInit(&strm, 9, 0, 0);
break; break;
default: default:
LOGE(1, "Unsupported bzip2 mode!\n"); LOGE("Unsupported bzip2 mode!\n");
} }
if (ret != BZ_OK) if (ret != BZ_OK)
LOGE(1, "Unable to init bzlib stream\n"); LOGE("Unable to init bzlib stream\n");
do { do {
strm.next_in = (char *) buf + pos; 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); total += xwrite(fd, "\x02\x21\x4c\x18", 4);
break; break;
default: default:
LOGE(1, "Unsupported lz4_legacy mode!\n"); LOGE("Unsupported lz4_legacy mode!\n");
} }
do { 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; block_size += ((unsigned)buff[pos + 3])<<24;
pos += 4; pos += 4;
if (block_size > LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE)) 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); have = LZ4_decompress_safe((const char*) (buf + pos), out, block_size, LZ4_LEGACY_BLOCKSIZE);
if (have < 0) if (have < 0)
LOGE(1, "Cannot decode lz4_legacy block\n"); LOGE("Cannot decode lz4_legacy block\n");
pos += block_size; pos += block_size;
break; break;
case 1: case 1:
@ -372,7 +377,7 @@ size_t lz4_legacy(int mode, int fd, const void* buf, size_t size) {
insize = LZ4_LEGACY_BLOCKSIZE; insize = LZ4_LEGACY_BLOCKSIZE;
have = LZ4_compress_default((const char*) (buf + pos), out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE)); have = LZ4_compress_default((const char*) (buf + pos), out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE));
if (have == 0) if (have == 0)
LOGE(1, "lz4_legacy compression error\n"); LOGE("lz4_legacy compression error\n");
pos += insize; pos += insize;
block_size_le[0] = have & 0xff; block_size_le[0] = have & 0xff;
block_size_le[1] = (have >> 8) & 0xff; block_size_le[1] = (have >> 8) & 0xff;
@ -443,7 +448,7 @@ void decomp_file(char *from, const char *to) {
char *ext; char *ext;
ext = strrchr(from, '.'); ext = strrchr(from, '.');
if (ext == NULL) if (ext == NULL)
LOGE(1, "Bad filename extention\n"); LOGE("Bad filename extention\n");
// File type and extension should match // File type and extension should match
switch (type) { switch (type) {
@ -469,7 +474,7 @@ void decomp_file(char *from, const char *to) {
ok = 0; ok = 0;
break; break;
default: 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 (ok) {
// If all match, strip out the suffix // If all match, strip out the suffix
@ -486,7 +491,7 @@ void decomp_file(char *from, const char *to) {
unlink(from); unlink(from);
} }
} else { } else {
LOGE(1, "Bad filename extention \'%s\'\n", ext); LOGE("Bad filename extention \'%s\'\n", ext);
} }
munmap(file, size); munmap(file, size);
} }

View File

@ -1,7 +1,10 @@
#include <stdio.h>
#include <unistd.h>
#include "magiskboot.h" #include "magiskboot.h"
#include "cpio.h" #include "cpio.h"
#include "vector.h" #include "logging.h"
#include "list.h" #include "utils.h"
static uint32_t x8u(char *hex) { static uint32_t x8u(char *hex) {
uint32_t val, inpos = 8, outpos; 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. // Because scanf gratuitously treats %*X differently than printf does.
sprintf(pattern, "%%%dx%%n", inpos); sprintf(pattern, "%%%dx%%n", inpos);
sscanf(hex, pattern, &val, &outpos); sscanf(hex, pattern, &val, &outpos);
if (inpos != outpos) LOGE(1, "bad cpio header\n"); if (inpos != outpos) LOGE("bad cpio header\n");
return val; return val;
} }
@ -332,7 +335,7 @@ static void cpio_extract(const char *entry, const char *filename, struct vector
exit(0); 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) { static void cpio_backup(const char *orig, struct vector *v) {

View File

@ -1,4 +1,10 @@
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/mman.h>
#include "magiskboot.h" #include "magiskboot.h"
#include "utils.h"
static void hex2byte(const char *hex, unsigned char *str) { static void hex2byte(const char *hex, unsigned char *str) {
char high, low; char high, low;

View File

@ -1,22 +1,9 @@
#ifndef _MAGISKBOOT_H_ #ifndef _MAGISKBOOT_H_
#define _MAGISKBOOT_H_ #define _MAGISKBOOT_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include "bootimg.h" #include "bootimg.h"
#include "sha1.h"
#include "logging.h"
#include "utils.h"
#include "magic.h"
#define KERNEL_FILE "kernel" #define KERNEL_FILE "kernel"
#define RAMDISK_FILE "ramdisk.cpio" #define RAMDISK_FILE "ramdisk.cpio"
@ -27,10 +14,6 @@
#define str(a) #a #define str(a) #a
#define xstr(a) str(a) #define xstr(a) str(a)
extern char *SUP_LIST[];
extern char *SUP_EXT_LIST[];
extern file_t SUP_TYPE_LIST[];
// Main entries // Main entries
void unpack(const char *image); void unpack(const char *image);
void repack(const char* orig_image, const char* out_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[]); int cpio_commands(const char *command, int argc, char *argv[]);
void comp_file(const char *method, const char *from, const char *to); void comp_file(const char *method, const char *from, const char *to);
void decomp_file(char *from, const char *to); void decomp_file(char *from, const char *to);
void cleanup();
// Compressions // Compressions
size_t gzip(int mode, int fd, const void *buf, size_t size); 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); long long decomp(file_t type, int to, const void *from, size_t size);
// Utils // Utils
void mmap_ro(const char *filename, void **buf, size_t *size); extern void mmap_ro(const char *filename, void **buf, size_t *size);
void mmap_rw(const char *filename, void **buf, size_t *size); extern void mmap_rw(const char *filename, void **buf, size_t *size);
file_t check_type(const void *buf); extern void write_zero(int fd, size_t size);
void get_type_name(file_t type, char *name); extern void mem_align(size_t *pos, size_t align);
void write_zero(int fd, size_t size); extern void file_align(int fd, size_t align, int out);
void mem_align(size_t *pos, size_t align); extern int open_new(const char *filename);
void file_align(int fd, size_t align, int out);
int open_new(const char *filename);
#endif #endif

View File

@ -1,4 +1,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include "magiskboot.h" #include "magiskboot.h"
#include "sha1.h"
/******************** /********************
Patch Boot Image Patch Boot Image
@ -6,55 +13,69 @@
static void usage(char *arg0) { static void usage(char *arg0) {
fprintf(stderr, fprintf(stderr,
"%s --unpack <bootimg>\n" "Usage: %s <action> [args...]\n"
" Unpack <bootimg> to kernel, ramdisk.cpio, (second), (dtb) into the\n current directory\n"
"\n" "\n"
"%s --repack <origbootimg> [outbootimg]\n" "Supported actions:\n"
" --unpack <bootimg>\n"
" Unpack <bootimg> to kernel, ramdisk.cpio, (second), (dtb) into the\n"
" current directory\n"
"\n"
" --repack <origbootimg> [outbootimg]\n"
" Repack kernel, ramdisk.cpio[.ext], second, dtb... from current directory\n" " Repack kernel, ramdisk.cpio[.ext], second, dtb... from current directory\n"
" to [outbootimg], or new-boot.img if not specified.\n" " to [outbootimg], or new-boot.img if not specified.\n"
" It will compress ramdisk.cpio with the same method used in <origbootimg>\n" " It will compress ramdisk.cpio with the same method used in <origbootimg>\n"
" if exists, or attempt to find ramdisk.cpio.[ext], and repack\n" " if exists, or attempt to find ramdisk.cpio.[ext], and repack\n"
" directly with the compressed ramdisk file\n" " directly with the compressed ramdisk file\n"
"\n" "\n"
"%s --hexpatch <file> <hexpattern1> <hexpattern2>\n" " --hexpatch <file> <hexpattern1> <hexpattern2>\n"
" Search <hexpattern1> in <file>, and replace with <hexpattern2>\n" " Search <hexpattern1> in <file>, and replace with <hexpattern2>\n"
"\n" "\n"
"%s --cpio-<cmd> <incpio> [flags...] [params...]\n" " --cpio-<cmd> <incpio> [flags...] [args...]\n"
" Do cpio related cmds to <incpio> (modifications are done directly)\n Supported commands and params:\n" " Do cpio related cmds to <incpio> (modifications are done directly)\n"
" -rm [-r] <entry>\n Remove entry from <incpio>, flag -r to remove recursively\n" " Supported commands:\n"
" -mkdir <mode> <entry>\n Create directory as an <entry>\n" " -rm [-r] <entry>\n"
" -add <mode> <entry> <infile>\n Add <infile> as an <entry>; replaces <entry> if already exists\n" " Remove entry from <incpio>, flag -r to remove recursively\n"
" -mv <from-entry> <to-entry>\n Move <from-entry> to <to-entry>\n" " -mkdir <mode> <entry>\n"
" -extract <entry> <outfile>\n Extract <entry> to <outfile>\n" " Create directory as an <entry>\n"
" -test \n Return value: 0/not patched 1/Magisk 2/Other (e.g. phh, SuperSU)\n" " -add <mode> <entry> <infile>\n"
" -patch <KEEPVERITY> <KEEPFORCEENCRYPT>\n Patch cpio for Magisk. KEEP**** are true/false values\n" " Add <infile> as an <entry>; replaces <entry> if already exists\n"
" -backup <origcpio>\n Create ramdisk backups into <incpio> from <origcpio>\n" " -mv <from-entry> <to-entry>\n"
" -restore\n Restore ramdisk from ramdisk backup within <incpio>\n" " Move <from-entry> to <to-entry>\n"
" -stocksha1\n Get stock boot SHA1 recorded within <incpio>\n" " -extract <entry> <outfile>\n"
" Extract <entry> to <outfile>\n"
" -test \n"
" Return value: 0/stock 1/Magisk 2/other (e.g. phh, SuperSU)\n"
" -patch <KEEPVERITY> <KEEPFORCEENCRYPT>\n"
" Patch cpio for Magisk. KEEP**** are true/false values\n"
" -backup <origcpio>\n"
" Create ramdisk backups into <incpio> from <origcpio>\n"
" -restore\n"
" Restore ramdisk from ramdisk backup within <incpio>\n"
" -stocksha1\n"
" Get stock boot SHA1 recorded within <incpio>\n"
"\n" "\n"
"%s --compress[=method] <infile> [outfile]\n" " --compress[=method] <infile> [outfile]\n"
" Compress <infile> with [method] (default: gzip), optionally to [outfile]\n Supported methods: " " Compress <infile> with [method] (default: gzip), optionally to [outfile]\n"
, arg0, arg0, arg0, arg0, arg0); " Supported methods: "
for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]);
fprintf(stderr,
"\n"
"\n"
"%s --decompress <infile> [outfile]\n"
" Detect method and decompress <infile>, optionally to [outfile]\n Supported methods: "
, arg0); , arg0);
for (int i = 0; SUP_LIST[i]; ++i) for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]); fprintf(stderr, "%s ", SUP_LIST[i]);
fprintf(stderr, fprintf(stderr,
"\n" "\n"
"\n" "\n"
"%s --sha1 <file>\n" " --decompress <infile> [outfile]\n"
" Detect method and decompress <infile>, 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 <file>\n"
" Print the SHA1 checksum for <file>\n" " Print the SHA1 checksum for <file>\n"
"\n" "\n"
"%s --cleanup\n" " --cleanup\n"
" Cleanup the current working directory\n" " Cleanup the current working directory\n"
"\n" "\n");
, arg0, arg0);
exit(1); 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"); 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(); 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) { } else if (argc > 2 && strcmp(argv[1], "--sha1") == 0) {
char sha1[21], *buf; char sha1[21], *buf;
size_t size; size_t size;

83
jni/magiskboot/types.c Normal file
View File

@ -0,0 +1,83 @@
#include <string.h>
#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);
}

View File

@ -1,5 +1,5 @@
#ifndef _MAGIC_H_ #ifndef _TYPES_H_
#define _MAGIC_H_ #define _TYPES_H_
typedef enum { typedef enum {
UNKNOWN, UNKNOWN,
@ -31,4 +31,11 @@ typedef enum {
#define DTB_MAGIC "\xd0\x0d\xfe\xed" #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" #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 #endif