Magisk/native/jni/core/img.cpp

298 lines
7.7 KiB
C++
Raw Normal View History

2018-11-04 03:38:06 -05:00
/* img.cpp - All image related functions
2017-06-24 23:37:45 +09:00
*/
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
2018-09-27 03:11:10 -04:00
#include <string.h>
#include <stdlib.h>
2017-06-24 23:37:45 +09:00
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/sendfile.h>
#include <sys/statfs.h>
2018-07-07 01:32:58 +08:00
#include <sys/sysmacros.h>
2017-06-24 23:37:45 +09:00
#include <linux/loop.h>
#include "magisk.h"
#include "utils.h"
2018-08-10 00:40:23 +08:00
#include "img.h"
2018-09-27 03:56:56 -04:00
#include "flags.h"
2017-06-24 23:37:45 +09:00
2018-07-06 22:04:06 +08:00
#define round_size(a) ((((a) / 32) + 2) * 32)
#define SOURCE_TMP "/dev/.img_src"
#define TARGET_TMP "/dev/.img_tgt"
#define MERGE_TMP "/dev/.img_mrg"
struct fs_info {
unsigned size;
unsigned free;
unsigned used;
};
2017-07-02 23:04:57 +08:00
static char *loopsetup(const char *img) {
2018-07-07 01:32:58 +08:00
char device[32];
2017-07-02 23:04:57 +08:00
struct loop_info64 info;
2018-07-07 01:32:58 +08:00
int lfd = -1, ffd;
2017-07-02 23:04:57 +08:00
memset(&info, 0, sizeof(info));
2018-07-07 01:32:58 +08:00
if (access(BLOCKDIR, F_OK) == 0) {
for (int i = 8; i < 100; ++i) {
sprintf(device, BLOCKDIR "/loop%02d", i);
if (access(device, F_OK) != 0)
mknod(device, S_IFBLK | 0600, makedev(7, i * 8));
lfd = open(device, O_RDWR);
if (lfd < 0) /* Kernel does not support this */
break;
if (ioctl(lfd, LOOP_GET_STATUS64, &info) == -1)
break;
close(lfd);
lfd = -1;
}
2017-07-02 23:04:57 +08:00
}
2018-07-07 01:32:58 +08:00
// Fallback to existing loop in dev, but in reverse order
if (lfd < 0) {
for (int i = 7; i >= 0; --i) {
sprintf(device, "/dev/block/loop%d", i);
lfd = xopen(device, O_RDWR);
if (ioctl(lfd, LOOP_GET_STATUS64, &info) == -1)
break;
close(lfd);
lfd = -1;
}
}
if (lfd < 0)
2019-01-26 13:39:24 -05:00
return nullptr;
2017-07-02 23:04:57 +08:00
ffd = xopen(img, O_RDWR);
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
2019-01-26 13:39:24 -05:00
return nullptr;
2018-07-07 01:32:58 +08:00
strncpy((char *) info.lo_file_name, img, sizeof(info.lo_file_name));
2017-07-02 23:04:57 +08:00
ioctl(lfd, LOOP_SET_STATUS64, &info);
close(lfd);
close(ffd);
return strdup(device);
}
static void check_filesystem(struct fs_info *info, const char *img, const char *mount) {
struct stat st;
struct statfs fs;
stat(img, &st);
statfs(mount, &fs);
info->size = st.st_size / 1048576;
info->free = fs.f_bfree * (uint64_t)fs.f_frsize / 1048576;
info->used = (fs.f_blocks - fs.f_bfree) * (uint64_t)fs.f_frsize / 1048576;
}
static void usage() {
fprintf(stderr,
"ImgTool v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu) - EXT4 Image Tools\n"
"\n"
"Usage: imgtool <action> [args...]\n"
"\n"
"Actions:\n"
" create IMG SIZE create ext4 image. SIZE is interpreted in MB\n"
" resize IMG SIZE resize ext4 image. SIZE is interpreted in MB\n"
" mount IMG PATH mount IMG to PATH and prints the loop device\n"
" umount PATH LOOP unmount PATH and delete LOOP device\n"
2018-07-06 22:04:06 +08:00
" merge SRC TGT merge SRC to TGT\n"
" trim IMG trim IMG to save space\n"
);
exit(1);
}
int imgtool_main(int argc, char *argv[]) {
if (argc < 2)
usage();
if (strcmp(argv[1], "create") == 0) {
if (argc < 4)
usage();
return create_img(argv[2], atoi(argv[3]));
} else if (strcmp(argv[1], "resize") == 0) {
if (argc < 4)
usage();
2018-07-06 22:04:06 +08:00
return resize_img(argv[2], atoi(argv[3]));
} else if (strcmp(argv[1], "mount") == 0) {
if (argc < 4)
usage();
2018-07-06 22:04:06 +08:00
// Redirect 1 > /dev/null
int fd = open("/dev/null", O_WRONLY);
int out = dup(STDOUT_FILENO);
xdup2(fd, STDOUT_FILENO);
char *loop = mount_image(argv[2], argv[3]);
2018-07-06 22:04:06 +08:00
// Restore stdin
xdup2(out, STDOUT_FILENO);
close(fd);
close(out);
2019-01-26 13:39:24 -05:00
if (loop == nullptr) {
fprintf(stderr, "Cannot mount image!\n");
return 1;
} else {
printf("%s\n", loop);
free(loop);
return 0;
}
} else if (strcmp(argv[1], "umount") == 0) {
if (argc < 4)
usage();
umount_image(argv[2], argv[3]);
return 0;
2018-07-06 22:04:06 +08:00
} else if (strcmp(argv[1], "merge") == 0) {
if (argc < 4)
usage();
return merge_img(argv[2], argv[3]);
} else if (strcmp(argv[1], "trim") == 0) {
if (argc < 3)
usage();
xmkdir(SOURCE_TMP, 0755);
char *loop = mount_image(argv[2], SOURCE_TMP);
int ret = trim_img(argv[2], SOURCE_TMP, loop);
umount_image(SOURCE_TMP, loop);
rmdir(SOURCE_TMP);
free(loop);
return ret;
}
usage();
return 1;
}
2017-06-24 23:37:45 +09:00
int create_img(const char *img, int size) {
if (size == 128) /* WTF...? */
size = 132;
2017-06-24 23:37:45 +09:00
unlink(img);
LOGI("Create %s with size %dM\n", img, size);
char size_str[16];
snprintf(size_str, sizeof(size_str), "%dM", size);
if (access("/system/bin/make_ext4fs", X_OK) == 0)
2019-01-26 13:39:24 -05:00
return exec_command_sync("/system/bin/make_ext4fs", "-b", "4096", "-l", size_str, img);
else if (access("/system/bin/mke2fs", X_OK) == 0)
// On Android P there is no make_ext4fs, use mke2fs
2019-01-26 13:39:24 -05:00
return exec_command_sync("/system/bin/mke2fs", "-b", "4096", "-t", "ext4", img, size_str);
else
2017-06-24 23:37:45 +09:00
return 1;
}
2018-07-06 22:04:06 +08:00
int resize_img(const char *img, int size) {
2017-06-24 23:37:45 +09:00
LOGI("Resize %s to %dM\n", img, size);
2019-01-26 13:39:24 -05:00
exec_command_sync("/system/bin/e2fsck", "-yf", img);
2018-07-06 22:04:06 +08:00
char ss[16];
snprintf(ss, sizeof(ss), "%dM", size);
2019-01-26 13:39:24 -05:00
return exec_command_sync("/system/bin/resize2fs", img, ss);
2017-06-24 23:37:45 +09:00
}
char *mount_image(const char *img, const char *target) {
2018-05-26 21:25:59 +08:00
if (access(img, F_OK) == -1 || access(target, F_OK) == -1)
2019-01-26 13:39:24 -05:00
return nullptr;
exec_command_sync("/system/bin/e2fsck", "-yf", img);
2017-06-24 23:37:45 +09:00
char *device = loopsetup(img);
if (device)
2019-01-26 13:39:24 -05:00
xmount(device, target, "ext4", MS_NOATIME, nullptr);
2017-06-24 23:37:45 +09:00
return device;
}
int umount_image(const char *target, const char *device) {
int ret = 0;
ret |= xumount(target);
2017-06-24 23:37:45 +09:00
int fd = xopen(device, O_RDWR);
ret |= ioctl(fd, LOOP_CLR_FD);
2017-06-24 23:37:45 +09:00
close(fd);
return ret;
2017-06-24 23:37:45 +09:00
}
2017-08-10 17:26:37 +08:00
int merge_img(const char *source, const char *target) {
if (access(source, F_OK) == -1)
return 0;
if (access(target, F_OK) == -1) {
2018-07-06 22:04:06 +08:00
LOGI("* Move %s -> %s\n", source, target);
if (rename(source, target) < 0) {
// Copy and remove
int tgt = creat(target, 0644);
int src = xopen(source, O_RDONLY | O_CLOEXEC);
sendfile(tgt, src, 0, INT_MAX);
close(tgt);
close(src);
unlink(source);
}
2017-08-10 17:26:37 +08:00
return 0;
}
2018-07-06 22:04:06 +08:00
char buf[PATH_MAX];
2017-08-10 17:26:37 +08:00
xmkdir(SOURCE_TMP, 0755);
xmkdir(TARGET_TMP, 0755);
2018-07-06 22:04:06 +08:00
char *s_loop, *t_loop, *m_loop;
2017-08-10 17:26:37 +08:00
s_loop = mount_image(source, SOURCE_TMP);
2019-01-26 13:39:24 -05:00
if (s_loop == nullptr)
return 1;
2017-08-10 17:26:37 +08:00
t_loop = mount_image(target, TARGET_TMP);
2019-01-26 13:39:24 -05:00
if (t_loop == nullptr)
return 1;
2018-07-06 22:04:06 +08:00
snprintf(buf, sizeof(buf), "%s/%s", SOURCE_TMP, "lost+found");
rm_rf(buf);
snprintf(buf, sizeof(buf), "%s/%s", TARGET_TMP, "lost+found");
rm_rf(buf);
2017-08-10 17:26:37 +08:00
DIR *dir;
struct dirent *entry;
2017-10-14 00:08:12 +08:00
if (!(dir = xopendir(SOURCE_TMP)))
2017-08-10 17:26:37 +08:00
return 1;
while ((entry = xreaddir(dir))) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0 ||
2018-07-06 22:04:06 +08:00
strcmp(entry->d_name, ".core") == 0)
2017-08-10 17:26:37 +08:00
continue;
// Cleanup old module if exists
2018-07-06 22:04:06 +08:00
snprintf(buf, sizeof(buf), "%s/%s", TARGET_TMP, entry->d_name);
if (access(buf, F_OK) == 0)
rm_rf(buf);
LOGI("Upgrade/New module: %s\n", entry->d_name);
2017-08-10 17:26:37 +08:00
}
}
closedir(dir);
2018-07-06 22:04:06 +08:00
struct fs_info src, tgt;
check_filesystem(&src, source, SOURCE_TMP);
check_filesystem(&tgt, target, TARGET_TMP);
snprintf(buf, sizeof(buf), "%s/tmp.img", dirname(target));
create_img(buf, round_size(src.used + tgt.used));
xmkdir(MERGE_TMP, 0755);
m_loop = mount_image(buf, MERGE_TMP);
2019-01-26 13:39:24 -05:00
if (m_loop == nullptr)
2018-07-06 22:04:06 +08:00
return 1;
LOGI("* Merging %s + %s -> %s", source, target, buf);
cp_afc(TARGET_TMP, MERGE_TMP);
cp_afc(SOURCE_TMP, MERGE_TMP);
2017-08-10 17:26:37 +08:00
// Unmount all loop devices
umount_image(SOURCE_TMP, s_loop);
umount_image(TARGET_TMP, t_loop);
2018-07-06 22:04:06 +08:00
umount_image(MERGE_TMP, m_loop);
2017-08-10 17:26:37 +08:00
rmdir(SOURCE_TMP);
rmdir(TARGET_TMP);
2018-07-06 22:04:06 +08:00
rmdir(MERGE_TMP);
2017-08-10 17:26:37 +08:00
free(s_loop);
free(t_loop);
2018-07-06 22:04:06 +08:00
free(m_loop);
// Cleanup
2017-08-10 17:26:37 +08:00
unlink(source);
2018-07-06 22:04:06 +08:00
LOGI("* Move %s -> %s", buf, target);
rename(buf, target);
2017-08-10 17:26:37 +08:00
return 0;
}
int trim_img(const char *img, const char *mount, char *loop) {
struct fs_info info;
check_filesystem(&info, img, mount);
int new_size = round_size(info.used);
if (info.size > new_size) {
umount_image(mount, loop);
2018-07-06 22:04:06 +08:00
resize_img(img, new_size);
char *loop2 = mount_image(img, mount);
2019-01-26 13:39:24 -05:00
if (loop2 == nullptr)
return 1;
2018-07-06 22:04:06 +08:00
strcpy(loop, loop2);
free(loop2);
}
return 0;
2017-08-10 17:26:37 +08:00
}