Magisk/native/jni/img.c

299 lines
7.7 KiB
C
Raw Normal View History

2017-06-24 16:37:45 +02:00
/* img.c - All image related functions
*/
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
2018-09-27 09:11:10 +02:00
#include <string.h>
#include <stdlib.h>
2017-06-24 16:37:45 +02:00
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/sendfile.h>
#include <sys/statfs.h>
2018-07-06 19:32:58 +02:00
#include <sys/sysmacros.h>
2017-06-24 16:37:45 +02:00
#include <linux/loop.h>
#include "magisk.h"
#include "utils.h"
2018-08-09 18:40:23 +02:00
#include "img.h"
2018-09-27 09:56:56 +02:00
#include "flags.h"
2017-06-24 16:37:45 +02:00
2018-07-06 16:04:06 +02: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 17:04:57 +02:00
static char *loopsetup(const char *img) {
2018-07-06 19:32:58 +02:00
char device[32];
2017-07-02 17:04:57 +02:00
struct loop_info64 info;
2018-07-06 19:32:58 +02:00
int lfd = -1, ffd;
2017-07-02 17:04:57 +02:00
memset(&info, 0, sizeof(info));
2018-07-06 19:32:58 +02: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 17:04:57 +02:00
}
2018-07-06 19:32:58 +02: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)
return NULL;
2017-07-02 17:04:57 +02:00
ffd = xopen(img, O_RDWR);
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
return NULL;
2018-07-06 19:32:58 +02:00
strncpy((char *) info.lo_file_name, img, sizeof(info.lo_file_name));
2017-07-02 17:04:57 +02: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 16:04:06 +02: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[]) {
2018-09-27 09:11:10 +02:00
cmdline_logging();
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 16:04:06 +02:00
return resize_img(argv[2], atoi(argv[3]));
} else if (strcmp(argv[1], "mount") == 0) {
if (argc < 4)
usage();
2018-07-06 16:04:06 +02: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 16:04:06 +02:00
// Restore stdin
xdup2(out, STDOUT_FILENO);
close(fd);
close(out);
if (loop == NULL) {
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 16:04:06 +02: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 16:37:45 +02:00
int create_img(const char *img, int size) {
if (size == 128) /* WTF...? */
size = 132;
2017-06-24 16:37:45 +02: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)
return exec_command_sync("/system/bin/make_ext4fs", "-b", "4096", "-l", size_str, img, NULL);
else if (access("/system/bin/mke2fs", X_OK) == 0)
// On Android P there is no make_ext4fs, use mke2fs
return exec_command_sync("/system/bin/mke2fs", "-b", "4096", "-t", "ext4", img, size_str, NULL);
else
2017-06-24 16:37:45 +02:00
return 1;
}
2018-07-06 16:04:06 +02:00
int resize_img(const char *img, int size) {
2017-06-24 16:37:45 +02:00
LOGI("Resize %s to %dM\n", img, size);
2018-07-06 16:04:06 +02:00
exec_command_sync("/system/bin/e2fsck", "-yf", img, NULL);
char ss[16];
snprintf(ss, sizeof(ss), "%dM", size);
return exec_command_sync("/system/bin/resize2fs", img, ss, NULL);
2017-06-24 16:37:45 +02:00
}
char *mount_image(const char *img, const char *target) {
2018-05-26 15:25:59 +02:00
if (access(img, F_OK) == -1 || access(target, F_OK) == -1)
2017-06-30 17:22:26 +02:00
return NULL;
2018-07-06 16:04:06 +02:00
exec_command_sync("/system/bin/e2fsck", "-yf", img, NULL);
2017-06-24 16:37:45 +02:00
char *device = loopsetup(img);
if (device)
xmount(device, target, "ext4", 0, NULL);
return device;
}
int umount_image(const char *target, const char *device) {
int ret = 0;
ret |= xumount(target);
2017-06-24 16:37:45 +02:00
int fd = xopen(device, O_RDWR);
ret |= ioctl(fd, LOOP_CLR_FD);
2017-06-24 16:37:45 +02:00
close(fd);
return ret;
2017-06-24 16:37:45 +02:00
}
2017-08-10 11:26:37 +02: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 16:04:06 +02: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 11:26:37 +02:00
return 0;
}
2018-07-06 16:04:06 +02:00
char buf[PATH_MAX];
2017-08-10 11:26:37 +02:00
xmkdir(SOURCE_TMP, 0755);
xmkdir(TARGET_TMP, 0755);
2018-07-06 16:04:06 +02:00
char *s_loop, *t_loop, *m_loop;
2017-08-10 11:26:37 +02:00
s_loop = mount_image(source, SOURCE_TMP);
if (s_loop == NULL)
return 1;
2017-08-10 11:26:37 +02:00
t_loop = mount_image(target, TARGET_TMP);
if (t_loop == NULL)
return 1;
2018-07-06 16:04:06 +02: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 11:26:37 +02:00
DIR *dir;
struct dirent *entry;
2017-10-13 18:08:12 +02:00
if (!(dir = xopendir(SOURCE_TMP)))
2017-08-10 11:26:37 +02: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 16:04:06 +02:00
strcmp(entry->d_name, ".core") == 0)
2017-08-10 11:26:37 +02:00
continue;
// Cleanup old module if exists
2018-07-06 16:04:06 +02: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 11:26:37 +02:00
}
}
closedir(dir);
2018-07-06 16:04:06 +02: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);
if (m_loop == NULL)
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 11:26:37 +02:00
// Unmount all loop devices
umount_image(SOURCE_TMP, s_loop);
umount_image(TARGET_TMP, t_loop);
2018-07-06 16:04:06 +02:00
umount_image(MERGE_TMP, m_loop);
2017-08-10 11:26:37 +02:00
rmdir(SOURCE_TMP);
rmdir(TARGET_TMP);
2018-07-06 16:04:06 +02:00
rmdir(MERGE_TMP);
2017-08-10 11:26:37 +02:00
free(s_loop);
free(t_loop);
2018-07-06 16:04:06 +02:00
free(m_loop);
// Cleanup
2017-08-10 11:26:37 +02:00
unlink(source);
2018-07-06 16:04:06 +02:00
LOGI("* Move %s -> %s", buf, target);
rename(buf, target);
2017-08-10 11:26:37 +02: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 16:04:06 +02:00
resize_img(img, new_size);
char *loop2 = mount_image(img, mount);
if (loop2 == NULL)
return 1;
2018-07-06 16:04:06 +02:00
strcpy(loop, loop2);
free(loop2);
}
return 0;
2017-08-10 11:26:37 +02:00
}