diff --git a/jni/magiskboot/magiskboot.h b/jni/magiskboot/magiskboot.h index 00930b1aa..c58378ccb 100644 --- a/jni/magiskboot/magiskboot.h +++ b/jni/magiskboot/magiskboot.h @@ -94,7 +94,6 @@ extern unsigned char *kernel, *ramdisk, *second, *dtb, *extra; extern boot_img_hdr hdr; extern file_t boot_type, ramdisk_type, dtb_type; extern int mtk_kernel, mtk_ramdisk; -extern int zero; // Main entries void unpack(const char *image); @@ -119,6 +118,7 @@ void decomp_file(char *from, const char *to); void mmap_ro(const char *filename, unsigned char **buf, size_t *size); void mmap_rw(const char *filename, unsigned char **buf, size_t *size); file_t check_type(const unsigned char *buf); +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); diff --git a/jni/magiskboot/repack.c b/jni/magiskboot/repack.c index fac089159..c76805fb5 100644 --- a/jni/magiskboot/repack.c +++ b/jni/magiskboot/repack.c @@ -21,9 +21,6 @@ static void restore_buf(int fd, const void *buf, size_t size) { } void repack(const char* orig_image, const char* out_image) { - zero = open("/dev/zero", O_RDONLY); - if (zero < 0) error(1, "Cannot open /dev/zero"); - size_t size; unsigned char *orig; char name[PATH_MAX]; @@ -51,12 +48,12 @@ void repack(const char* orig_image, const char* out_image) { hdr.dt_size = 0; // Skip a page for header - sendfile(fd, zero, NULL, hdr.page_size); + write_zero(fd, hdr.page_size); // Restore kernel if (mtk_kernel) { mtk_kernel_off = lseek(fd, 0, SEEK_CUR); - sendfile(fd, zero, NULL, 512); + write_zero(fd, 512); memcpy(&mtk_kernel_hdr, kernel, sizeof(mtk_kernel_hdr)); } hdr.kernel_size = restore(KERNEL_FILE, fd); @@ -65,7 +62,7 @@ void repack(const char* orig_image, const char* out_image) { // Restore ramdisk if (mtk_ramdisk) { mtk_ramdisk_off = lseek(fd, 0, SEEK_CUR); - sendfile(fd, zero, NULL, 512); + write_zero(fd, 512); memcpy(&mtk_ramdisk_hdr, ramdisk, sizeof(mtk_ramdisk_hdr)); } if (access(RAMDISK_FILE, R_OK) == 0) { diff --git a/jni/magiskboot/utils.c b/jni/magiskboot/utils.c index 514fe917e..18c48ea3e 100644 --- a/jni/magiskboot/utils.c +++ b/jni/magiskboot/utils.c @@ -3,7 +3,6 @@ char *SUP_EXT_LIST[SUP_NUM] = { "gz", "xz", "lzma", "bz2", "lz4" }; file_t SUP_TYPE_LIST[SUP_NUM] = { GZIP, XZ, LZMA, BZIP2, LZ4 }; -int zero = -1; void mmap_ro(const char *filename, unsigned char **buf, size_t *size) { int fd = open(filename, O_RDONLY); @@ -56,6 +55,12 @@ file_t check_type(const unsigned char *buf) { } } +void write_zero(int fd, size_t size) { + size_t pos = lseek(fd, 0, SEEK_CUR); + ftruncate(fd, pos + size); + lseek(fd, pos + size, SEEK_SET); +} + void mem_align(size_t *pos, size_t align) { size_t mask = align - 1; if (*pos & mask) { @@ -70,11 +75,7 @@ void file_align(int fd, size_t align, int out) { if (pos & mask) { off = align - (pos & mask); if (out) { - if (zero < 0) { - zero = open("/dev/zero", O_RDONLY); - if (zero < 0) error(1, "Cannot open /dev/zero"); - } - sendfile(fd, zero, NULL, off); + write_zero(fd, off); } else { lseek(fd, pos + off, SEEK_SET); }