Reduce duplicate code for MagiskBoot

This commit is contained in:
topjohnwu 2017-04-28 21:48:38 +08:00
parent 62fe92d922
commit d3d5703f3f
15 changed files with 145 additions and 129 deletions

View File

@ -15,6 +15,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <selinux/selinux.h>
#include "magisk.h"
#include "utils.h"
@ -114,7 +115,7 @@ void start_daemon() {
return;
}
xsetsid();
xsetcon("u:r:su:s0");
setcon("u:r:su:s0");
umask(022);
// Patch selinux with medium patch before we do anything

View File

@ -20,6 +20,7 @@ LOCAL_SRC_FILES := \
utils.c \
cpio.c \
sha1.c \
../utils/xwrap.c \
../utils/vector.c
LOCAL_CFLAGS += -DZLIB_CONST
include $(BUILD_EXECUTABLE)

View File

@ -16,8 +16,7 @@
#define LZ4_LEGACY_BLOCKSIZE 0x800000
static void write_file(const int fd, const void *buf, const size_t size, const char *filename) {
if (write(fd, buf, size) != size)
error(1, "Error in writing %s", filename);
xwrite(fd, buf, size);
}
static void report(const int mode, const char* filename) {
@ -52,11 +51,11 @@ void gzip(int mode, const char* filename, const unsigned char* buf, size_t size)
ret = deflateInit2(&strm, 9, Z_DEFLATED, windowBits | ZLIB_GZIP, memLevel, Z_DEFAULT_STRATEGY);
break;
default:
error(1, "Unsupported gzip mode!");
LOGE(1, "Unsupported gzip mode!\n");
}
if (ret != Z_OK)
error(1, "Unable to init zlib stream");
LOGE(1, "Unable to init zlib stream\n");
do {
strm.next_in = buf + pos;
@ -81,7 +80,7 @@ void gzip(int mode, const char* filename, const unsigned char* buf, size_t size)
break;
}
if (ret == Z_STREAM_ERROR)
error(1, "Error when running gzip");
LOGE(1, "Error when running gzip\n");
have = CHUNK - strm.avail_out;
write_file(fd, out, have, filename);
@ -132,12 +131,12 @@ void lzma(int mode, const char* filename, const unsigned char* buf, size_t size)
ret = lzma_alone_encoder(&strm, &opt);
break;
default:
error(1, "Unsupported lzma mode!");
LOGE(1, "Unsupported lzma mode!\n");
}
if (ret != LZMA_OK)
error(1, "Unable to init lzma stream");
LOGE(1, "Unable to init lzma stream\n");
do {
strm.next_in = buf + pos;
@ -159,7 +158,7 @@ void lzma(int mode, const char* filename, const unsigned char* buf, size_t size)
} while (strm.avail_out == 0 && ret == LZMA_OK);
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
error(1, "LZMA error %d!", ret);
LOGE(1, "LZMA error %d!\n", ret);
} while (pos < size);
@ -189,11 +188,11 @@ void lz4(int mode, const char* filename, const unsigned char* buf, size_t size)
ret = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION);
break;
default:
error(1, "Unsupported lz4 mode!");
LOGE(1, "Unsupported lz4 mode!\n");
}
if (LZ4F_isError(ret))
error(1, "Context creation error: %s\n", LZ4F_getErrorName(ret));
LOGE(1, "Context creation error: %s\n", LZ4F_getErrorName(ret));
// Allocate out buffer
switch(mode) {
@ -202,7 +201,7 @@ void lz4(int mode, const char* filename, const unsigned char* buf, size_t size)
read = CHUNK;
ret = LZ4F_getFrameInfo(dctx, &info, buf, &read);
if (LZ4F_isError(ret))
error(1, "LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(ret));
LOGE(1, "LZ4F_getFrameInfo error: %s\n", LZ4F_getErrorName(ret));
switch (info.blockSizeID) {
case LZ4F_default:
case LZ4F_max64KB: outCapacity = 1 << 16; break;
@ -210,7 +209,7 @@ void lz4(int mode, const char* filename, const unsigned char* buf, size_t size)
case LZ4F_max1MB: outCapacity = 1 << 20; break;
case LZ4F_max4MB: outCapacity = 1 << 22; break;
default:
error(1, "Impossible unless more block sizes are allowed\n");
LOGE(1, "Impossible unless more block sizes are allowed\n");
}
pos += read;
break;
@ -219,15 +218,13 @@ void lz4(int mode, const char* filename, const unsigned char* buf, size_t size)
break;
}
out = malloc(outCapacity);
if (!out)
error(1, "LZ4 malloc error!");
out = xmalloc(outCapacity);
// Write header
if (mode == 1) {
have = ret = LZ4F_compressBegin(cctx, out, size, NULL);
if (LZ4F_isError(ret))
error(1, "Failed to start compression: error %s\n", LZ4F_getErrorName(ret));
LOGE(1, "Failed to start compression: error %s\n", LZ4F_getErrorName(ret));
write_file(fd, out, have, filename);
}
@ -250,7 +247,7 @@ void lz4(int mode, const char* filename, const unsigned char* buf, size_t size)
break;
}
if (LZ4F_isError(ret))
error(1, "LZ4 coding error: %s\n", LZ4F_getErrorName(ret));
LOGE(1, "LZ4 coding error: %s\n", LZ4F_getErrorName(ret));
write_file(fd, out, have, filename);
// Update status
@ -267,7 +264,7 @@ void lz4(int mode, const char* filename, const unsigned char* buf, size_t size)
case 1:
have = ret = LZ4F_compressEnd(cctx, out, outCapacity, NULL);
if (LZ4F_isError(ret))
error(1, "Failed to end compression: error %s\n", LZ4F_getErrorName(ret));
LOGE(1, "Failed to end compression: error %s\n", LZ4F_getErrorName(ret));
write_file(fd, out, have, filename);
@ -300,11 +297,11 @@ void bzip2(int mode, const char* filename, const unsigned char* buf, size_t size
ret = BZ2_bzCompressInit(&strm, 9, 0, 0);
break;
default:
error(1, "Unsupported bzip2 mode!");
LOGE(1, "Unsupported bzip2 mode!\n");
}
if (ret != BZ_OK)
error(1, "Unable to init bzlib stream");
LOGE(1, "Unable to init bzlib stream\n");
do {
strm.next_in = (char *) buf + pos;
@ -361,22 +358,19 @@ void lz4_legacy(int mode, const char* filename, const unsigned char* buf, size_t
switch(mode) {
case 0:
out = malloc(LZ4_LEGACY_BLOCKSIZE);
out = xmalloc(LZ4_LEGACY_BLOCKSIZE);
// Skip magic
pos += 4;
break;
case 1:
out = malloc(LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE));
out = xmalloc(LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE));
// Write magic
write_file(fd, "\x02\x21\x4c\x18", 4, filename);
break;
default:
error(1, "Unsupported lz4_legacy mode!");
LOGE(1, "Unsupported lz4_legacy mode!\n");
}
if (!out)
error(1, "lz4_legacy malloc error");
do {
switch(mode) {
case 0:
@ -386,10 +380,10 @@ void lz4_legacy(int mode, const char* filename, const unsigned char* buf, size_t
block_size += ((unsigned)buf[pos + 3])<<24;
pos += 4;
if (block_size > LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE))
error(1, "lz4_legacy block size too large!");
LOGE(1, "lz4_legacy block size too large!\n");
have = LZ4_decompress_safe((const char*) (buf + pos), out, block_size, LZ4_LEGACY_BLOCKSIZE);
if (have < 0)
error(1, "Cannot decode lz4_legacy block");
LOGE(1, "Cannot decode lz4_legacy block\n");
pos += block_size;
break;
case 1:
@ -399,7 +393,7 @@ void lz4_legacy(int mode, const char* filename, const unsigned char* buf, size_t
insize = LZ4_LEGACY_BLOCKSIZE;
have = LZ4_compress_default((const char*) (buf + pos), out, insize, LZ4_COMPRESSBOUND(LZ4_LEGACY_BLOCKSIZE));
if (have == 0)
error(1, "lz4_legacy compression error");
LOGE(1, "lz4_legacy compression error\n");
pos += insize;
block_size_le[0] = (unsigned char)have;
block_size_le[1] = (unsigned char)(have >> 8);
@ -496,7 +490,7 @@ void decomp_file(char *from, const char *to) {
char *ext;
ext = strrchr(from, '.');
if (ext == NULL)
error(1, "Bad filename extention");
LOGE(1, "Bad filename extention\n");
// File type and extension should match
switch (type) {
@ -522,7 +516,7 @@ void decomp_file(char *from, const char *to) {
ok = 0;
break;
default:
error(1, "Provided file \'%s\' is not a supported archive format", from);
LOGE(1, "Provided file \'%s\' is not a supported archive format\n", from);
}
if (ok) {
// If all match, strip out the suffix
@ -536,7 +530,7 @@ void decomp_file(char *from, const char *to) {
unlink(from);
}
} else {
error(1, "Bad filename extention \'%s\'", ext);
LOGE(1, "Bad filename extention \'%s\'\n", ext);
}
munmap(file, size);
}
@ -556,7 +550,11 @@ void comp_file(const char *method, const char *from, const char *to) {
} else if (strcmp(method, "bzip2") == 0) {
type = BZIP2;
} else {
error(1, "Only support following methods: ");
fprintf(stderr, "Only support following methods: ");
for (int i = 0; SUP_LIST[i]; ++i)
fprintf(stderr, "%s ", SUP_LIST[i]);
fprintf(stderr, "\n");
exit(1);
}
unsigned char *file;
size_t size;

View File

@ -13,7 +13,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) error(1, "bad cpio header");
if (inpos != outpos) LOGE(1, "bad cpio header\n");
return val;
}
@ -62,13 +62,11 @@ 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);
int fd = open(filename, O_RDONLY);
if (fd < 0)
error(1, "Cannot open %s", filename);
int fd = xopen(filename, O_RDONLY);
cpio_newc_header header;
cpio_file *f;
while(read(fd, &header, 110) == 110) {
f = calloc(sizeof(*f), 1);
f = xcalloc(sizeof(*f), 1);
// f->ino = x8u(header.ino);
f->mode = x8u(header.mode);
f->uid = x8u(header.uid);
@ -83,7 +81,7 @@ static void parse_cpio(const char *filename, struct vector *v) {
f->namesize = x8u(header.namesize);
// f->check = x8u(header.check);
f->filename = malloc(f->namesize);
read(fd, f->filename, f->namesize);
xxread(fd, f->filename, f->namesize);
file_align(fd, 4, 0);
if (strcmp(f->filename, ".") == 0 || strcmp(f->filename, "..") == 0) {
cpio_free(f);
@ -95,7 +93,7 @@ static void parse_cpio(const char *filename, struct vector *v) {
}
if (f->filesize) {
f->data = malloc(f->filesize);
read(fd, f->data, f->filesize);
xxread(fd, f->data, f->filesize);
file_align(fd, 4, 0);
}
vec_push_back(v, f);
@ -128,18 +126,18 @@ static void dump_cpio(const char *filename, struct vector *v) {
f->namesize,
0 // f->check
);
write(fd, header, 110);
write(fd, f->filename, f->namesize);
xwrite(fd, header, 110);
xwrite(fd, f->filename, f->namesize);
file_align(fd, 4, 1);
if (f->filesize) {
write(fd, f->data, f->filesize);
xwrite(fd, f->data, f->filesize);
file_align(fd, 4, 1);
}
}
// Write trailer
sprintf(header, "070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", inode++, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 11, 0);
write(fd, header, 110);
write(fd, "TRAILER!!!\0", 11);
xwrite(fd, header, 110);
xwrite(fd, "TRAILER!!!\0", 11);
file_align(fd, 4, 1);
close(fd);
}
@ -168,28 +166,26 @@ static void cpio_rm(int recursive, const char *entry, struct vector *v) {
}
static void cpio_mkdir(mode_t mode, const char *entry, struct vector *v) {
cpio_file *f = calloc(sizeof(*f), 1);
cpio_file *f = xcalloc(sizeof(*f), 1);
f->mode = S_IFDIR | mode;
f->namesize = strlen(entry) + 1;
f->filename = malloc(f->namesize);
f->filename = xmalloc(f->namesize);
memcpy(f->filename, entry, f->namesize);
cpio_vec_insert(v, f);
printf("Create directory [%s] (%04o)\n",entry, mode);
}
static void cpio_add(mode_t mode, const char *entry, const char *filename, struct vector *v) {
int fd = open(filename, O_RDONLY);
if (fd < 0)
error(1, "Cannot open %s", filename);
cpio_file *f = calloc(sizeof(*f), 1);
int fd = xopen(filename, O_RDONLY);
cpio_file *f = xcalloc(sizeof(*f), 1);
f->mode = S_IFREG | mode;
f->namesize = strlen(entry) + 1;
f->filename = malloc(f->namesize);
f->filename = xmalloc(f->namesize);
memcpy(f->filename, entry, f->namesize);
f->filesize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
f->data = malloc(f->filesize);
read(fd, f->data, f->filesize);
xxread(fd, f->data, f->filesize);
close(fd);
cpio_vec_insert(v, f);
printf("Add entry [%s] (%04o)\n", entry, mode);
@ -274,14 +270,14 @@ static void cpio_extract(const char *entry, const char *filename, struct vector
if (strcmp(f->filename, entry) == 0 && S_ISREG(f->mode)) {
printf("Extracting [%s] to [%s]\n\n", entry, filename);
int fd = open_new(filename);
write(fd, f->data, f->filesize);
xwrite(fd, f->data, f->filesize);
fchmod(fd, f->mode);
fchown(fd, f->uid, f->gid);
close(fd);
exit(0);
}
}
error(1, "Cannot find the file entry [%s]", entry);
LOGE(1, "Cannot find the file entry [%s]\n", entry);
}
static void cpio_backup(const char *orig, struct vector *v) {
@ -290,8 +286,8 @@ static void cpio_backup(const char *orig, struct vector *v) {
char chk1[21], chk2[21], buf[PATH_MAX];
int res, doBak;
dir = calloc(sizeof(*dir), 1);
rem = calloc(sizeof(*rem), 1);
dir = xcalloc(sizeof(*dir), 1);
rem = xcalloc(sizeof(*rem), 1);
vec_init(o);
vec_init(&bak);
// First push back the directory and the rmlist
@ -342,7 +338,7 @@ static void cpio_backup(const char *orig, struct vector *v) {
// Someting new in ramdisk, record in rem
++j;
if (n->remove) continue;
rem->data = realloc(rem->data, rem->filesize + n->namesize);
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);
@ -391,12 +387,12 @@ static int cpio_restore(struct vector *v) {
cpio_rm(0, f->data + pos, v);
continue;
}
n = calloc(sizeof(*n), 1);
n = xcalloc(sizeof(*n), 1);
memcpy(n, f, sizeof(*f));
n->namesize -= 8;
n->filename = malloc(n->namesize);
n->filename = xmalloc(n->namesize);
memcpy(n->filename, f->filename + 8, n->namesize);
n->data = malloc(n->filesize);
n->data = xmalloc(n->filesize);
memcpy(n->data, f->data, n->filesize);
n->remove = 0;
printf("Restoring [%s] -> [%s]\n", f->filename, n->filename);

View File

@ -14,8 +14,8 @@ void hexpatch(const char *image, const char *from, const char *to) {
size_t filesize;
unsigned char *file, *pattern, *patch;
mmap_rw(image, &file, &filesize);
pattern = malloc(patternsize);
patch = malloc(patchsize);
pattern = xmalloc(patternsize);
patch = xmalloc(patchsize);
hex2byte(from, pattern);
hex2byte(to, patch);
for (size_t i = 0; i < filesize - patternsize; ++i) {

10
jni/magiskboot/magisk.h Normal file
View File

@ -0,0 +1,10 @@
/* magisk.h - Let MagiskBoot use the same error handling API as main magisk program
*/
#ifndef _MAGISK_H_
#define _MAGISK_H_
#define LOGE(err, ...) { fprintf(stderr, __VA_ARGS__); exit(err); }
#define PLOGE(fmt, args...) { fprintf(stderr, fmt " failed with %d: %s\n\n", ##args, errno, strerror(errno)); exit(1); }
#endif

View File

@ -8,23 +8,24 @@
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include "bootimg.h"
#include "sha1.h"
#include "magisk.h"
#include "utils.h"
#define CHROMEOS_MAGIC "CHROMEOS"
#define ELF32_MAGIC "\x7f""ELF\x01"
#define ELF64_MAGIC "\x7f""ELF\x02"
#define KERNEL_FILE "kernel"
#define RAMDISK_FILE "ramdisk.cpio"
#define SECOND_FILE "second"
#define DTB_FILE "dtb"
#define NEW_BOOT "new-boot.img"
#define KERNEL_FILE "kernel"
#define RAMDISK_FILE "ramdisk.cpio"
#define SECOND_FILE "second"
#define DTB_FILE "dtb"
#define NEW_BOOT "new-boot.img"
#define str(a) #a
#define xstr(a) str(a)
@ -72,7 +73,6 @@ extern int mtk_kernel, mtk_ramdisk;
void unpack(const char *image);
void repack(const char* orig_image, const char* out_image);
void hexpatch(const char *image, const char *from, const char *to);
void error(int rc, const char *msg, ...);
void parse_img(unsigned char *orig, size_t size);
int cpio_commands(const char *command, int argc, char *argv[]);
void cleanup();

View File

@ -58,15 +58,6 @@ static void usage(char *arg0) {
exit(1);
}
void error(int rc, const char *msg, ...) {
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
fprintf(stderr,"\n\n");
va_end(ap);
exit(rc);
}
int main(int argc, char *argv[]) {
printf("MagiskBoot v" xstr(MAGISK_VERSION) " (by topjohnwu) - Boot Image Modification Tool\n\n");

View File

@ -87,5 +87,5 @@ void parse_img(unsigned char *orig, size_t size) {
continue;
}
}
error(1, "No boot image magic found!");
LOGE(1, "No boot image magic found!\n");
}

View File

@ -1,23 +1,16 @@
#include "magiskboot.h"
static size_t restore(const char *filename, int fd) {
int ifd = open(filename, O_RDONLY);
if (ifd < 0)
error(1, "Cannot open %s\n", filename);
int ifd = xopen(filename, O_RDONLY);
size_t size = lseek(ifd, 0, SEEK_END);
lseek(ifd, 0, SEEK_SET);
if (sendfile(fd, ifd, NULL, size) != size) {
error(1, "Cannot write %s\n", filename);
}
xsendfile(fd, ifd, NULL, size);
close(ifd);
return size;
}
static void restore_buf(int fd, const void *buf, size_t size) {
if (write(fd, buf, size) != size) {
error(1, "Cannot dump from input file\n");
}
xwrite(fd, buf, size);
}
void repack(const char* orig_image, const char* out_image) {
@ -79,7 +72,7 @@ void repack(const char* orig_image, const char* out_image) {
mmap_ro(RAMDISK_FILE, &cpio, &cpio_size);
if (comp(ramdisk_type, RAMDISK_FILE, cpio, cpio_size))
error(1, "Unsupported ramdisk format!");
LOGE(1, "Unsupported ramdisk format!\n");
munmap(cpio, cpio_size);
}
@ -94,7 +87,7 @@ void repack(const char* orig_image, const char* out_image) {
}
}
if (!found)
error(1, "No ramdisk exists!");
LOGE(1, "No ramdisk exists!\n");
hdr.ramdisk_size = restore(name, fd);
file_align(fd, hdr.page_size, 1);
@ -140,7 +133,7 @@ void repack(const char* orig_image, const char* out_image) {
munmap(orig, size);
if (lseek(fd, 0, SEEK_END) > size) {
error(2, "Boot partition too small!");
LOGE(2, "Boot partition too small!\n");
}
close(fd);
}

View File

@ -2,8 +2,7 @@
static void dump(unsigned char *buf, size_t size, const char *filename) {
int fd = open_new(filename);
if (write(fd, buf, size) != size)
error(1, "Cannot dump %s", filename);
xwrite(fd, buf, size);
close(fd);
}
@ -33,7 +32,7 @@ void unpack(const char* image) {
if (decomp(ramdisk_type, RAMDISK_FILE, ramdisk, hdr.ramdisk_size)) {
// Dump the compressed ramdisk
dump(ramdisk, hdr.ramdisk_size, RAMDISK_FILE ".unsupport");
error(1, "Unsupported ramdisk format! Dumped to %s", RAMDISK_FILE ".unsupport");
LOGE(1, "Unsupported ramdisk format! Dumped to %s\n", RAMDISK_FILE ".unsupport");
}
if (hdr.second_size) {

View File

@ -5,22 +5,18 @@ 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, unsigned char **buf, size_t *size) {
int fd = open(filename, O_RDONLY);
if (fd < 0)
error(1, "Cannot open %s", filename);
int fd = xopen(filename, O_RDONLY);
*size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
*buf = mmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0);
*buf = xmmap(NULL, *size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
}
void mmap_rw(const char *filename, unsigned char **buf, size_t *size) {
int fd = open(filename, O_RDWR);
if (fd < 0)
error(1, "Cannot open %s", filename);
int fd = xopen(filename, O_RDWR);
*size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
*buf = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
*buf = xmmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
}
@ -83,10 +79,7 @@ void file_align(int fd, size_t align, int out) {
}
int open_new(const char *filename) {
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
error(1, "Unable to create %s", filename);
return fd;
return xopen(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
void print_info() {

View File

@ -23,7 +23,10 @@ extern int quit_signals[];
// xwrap.c
FILE *xfopen(const char *pathname, const char *mode);
int xopen(const char *pathname, int flags);
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define xopen(...) GET_MACRO(__VA_ARGS__, xopen3, xopen2)(__VA_ARGS__)
int xopen2(const char *pathname, int flags);
int xopen3(const char *pathname, int flags, mode_t mode);
ssize_t xwrite(int fd, const void *buf, size_t count);
ssize_t xread(int fd, void *buf, size_t count);
ssize_t xxread(int fd, void *buf, size_t count);
@ -32,7 +35,6 @@ int xsetns(int fd, int nstype);
DIR *xopendir(const char *name);
struct dirent *xreaddir(DIR *dirp);
pid_t xsetsid();
int xsetcon(char *context);
int xsocket(int domain, int type, int protocol);
int xbind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int xconnect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
@ -56,6 +58,9 @@ int xmount(const char *source, const char *target,
int xchmod(const char *pathname, mode_t mode);
int xrename(const char *oldpath, const char *newpath);
int xmkdir(const char *pathname, mode_t mode);
void *xmmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset);
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
// misc.c

View File

@ -18,7 +18,8 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <selinux/selinux.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include "magisk.h"
#include "utils.h"
@ -31,7 +32,7 @@ FILE *xfopen(const char *pathname, const char *mode) {
return fp;
}
int xopen(const char *pathname, int flags) {
int xopen2(const char *pathname, int flags) {
int fd = open(pathname, flags);
if (fd < 0) {
PLOGE("open: %s", pathname);
@ -39,6 +40,14 @@ int xopen(const char *pathname, int flags) {
return fd;
}
int xopen3(const char *pathname, int flags, mode_t mode) {
int fd = open(pathname, flags, mode);
if (fd < 0) {
PLOGE("open: %s", pathname);
}
return fd;
}
ssize_t xwrite(int fd, const void *buf, size_t count) {
int ret = write(fd, buf, count);
if (count != ret) {
@ -106,14 +115,6 @@ pid_t xsetsid() {
return pid;
}
int xsetcon(char *context) {
int ret = setcon(context);
if (ret == -1) {
PLOGE("setcon: %s", context);
}
return ret;
}
int xsocket(int domain, int type, int protocol) {
int fd = socket(domain, type, protocol);
if (fd == -1) {
@ -280,4 +281,21 @@ int xmkdir(const char *pathname, mode_t mode) {
return ret;
}
void *xmmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset) {
void *ret = mmap(addr, length, prot, flags, fd, offset);
if (ret == MAP_FAILED) {
PLOGE("mmap");
}
return ret;
}
ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
ssize_t ret = sendfile(out_fd, in_fd, offset, count);
if (count != ret) {
PLOGE("sendfile");
}
return ret;
}

View File

@ -243,8 +243,7 @@ ui_print "- Device platform: $ARCH"
BINDIR=$INSTALLER/$ARCH
chmod -R 755 $CHROMEDIR $BINDIR
SYSTEMLIB=/system/lib
$IS64BIT && SYSTEMLIB=/system/lib64
$IS64BIT && SYSTEMLIB=/system/lib64 || SYSTEMLIB=/system/lib
find_boot_image
if [ -z $BOOTIMAGE ]; then
@ -315,10 +314,22 @@ cd $BOOTTMP
ui_print "- Unpacking boot image"
LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/magiskboot --unpack $BOOTIMAGE
if [ $? -ne 0 ]; then
ui_print "! Unable to unpack boot image"
exit 1
fi
case $? in
1 )
ui_print "! Unable to unpack boot image"
exit 1
;;
2 )
ui_print "! Sony ELF32 format detected"
ui_print "! Please use BootBridge from @AdrianDC to flash Magisk"
exit 1
;;
3 )
ui_print "! Sony ELF64 format detected"
ui_print "! Stock kernel cannot be patched, please use a custom kernel"
exit 1
esac
##########################################################################################
# Ramdisk restores
@ -497,11 +508,11 @@ cd /
if ! $BOOTMODE; then
ui_print "- Unmounting partitions"
umount /magisk
losetup -d $MAGISKLOOP
losetup -d $MAGISKLOOP 2>/dev/null
rmdir /magisk
if $SUPERSU; then
umount /su
losetup -d $SUPERSULOOP
losetup -d $SUPERSULOOP 2>/dev/null
rmdir /su
fi
umount /system