2017-06-24 14:37:45 +00:00
|
|
|
/* img.c - All image related functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2017-10-11 20:20:32 +00:00
|
|
|
#include <libgen.h>
|
2018-02-01 20:47:16 +00:00
|
|
|
#include <fcntl.h>
|
2017-06-24 14:37:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/mount.h>
|
2018-02-01 20:47:16 +00:00
|
|
|
#include <sys/sendfile.h>
|
2018-07-12 21:41:29 +00:00
|
|
|
#include <sys/statfs.h>
|
2018-07-06 17:32:58 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2017-06-24 14:37:45 +00:00
|
|
|
#include <linux/loop.h>
|
|
|
|
|
|
|
|
#include "magisk.h"
|
|
|
|
#include "utils.h"
|
2018-08-09 16:40:23 +00:00
|
|
|
#include "img.h"
|
2017-06-24 14:37:45 +00:00
|
|
|
|
2018-07-06 14:04:06 +00: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"
|
|
|
|
|
2018-06-21 22:18:06 +00:00
|
|
|
struct fs_info {
|
|
|
|
unsigned size;
|
|
|
|
unsigned free;
|
|
|
|
unsigned used;
|
|
|
|
};
|
|
|
|
|
2017-07-02 15:04:57 +00:00
|
|
|
static char *loopsetup(const char *img) {
|
2018-07-06 17:32:58 +00:00
|
|
|
char device[32];
|
2017-07-02 15:04:57 +00:00
|
|
|
struct loop_info64 info;
|
2018-07-06 17:32:58 +00:00
|
|
|
int lfd = -1, ffd;
|
2017-07-02 15:04:57 +00:00
|
|
|
memset(&info, 0, sizeof(info));
|
2018-07-06 17:32:58 +00: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 15:04:57 +00:00
|
|
|
}
|
2018-07-06 17:32:58 +00: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 15:04:57 +00:00
|
|
|
ffd = xopen(img, O_RDWR);
|
|
|
|
if (ioctl(lfd, LOOP_SET_FD, ffd) == -1)
|
|
|
|
return NULL;
|
2018-07-06 17:32:58 +00:00
|
|
|
strncpy((char *) info.lo_file_name, img, sizeof(info.lo_file_name));
|
2017-07-02 15:04:57 +00:00
|
|
|
ioctl(lfd, LOOP_SET_STATUS64, &info);
|
|
|
|
close(lfd);
|
|
|
|
close(ffd);
|
|
|
|
return strdup(device);
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:18:06 +00:00
|
|
|
static void check_filesystem(struct fs_info *info, const char *img, const char *mount) {
|
|
|
|
struct stat st;
|
2018-07-12 21:41:29 +00:00
|
|
|
struct statfs fs;
|
2018-06-21 22:18:06 +00:00
|
|
|
stat(img, &st);
|
2018-07-12 21:41:29 +00:00
|
|
|
statfs(mount, &fs);
|
2018-06-21 22:18:06 +00:00
|
|
|
info->size = st.st_size / 1048576;
|
2018-07-12 21:41:29 +00:00
|
|
|
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;
|
2018-06-21 22:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 14:04:06 +00:00
|
|
|
" merge SRC TGT merge SRC to TGT\n"
|
|
|
|
" trim IMG trim IMG to save space\n"
|
2018-06-21 22:18:06 +00:00
|
|
|
);
|
|
|
|
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 14:04:06 +00:00
|
|
|
return resize_img(argv[2], atoi(argv[3]));
|
2018-06-21 22:18:06 +00:00
|
|
|
} else if (strcmp(argv[1], "mount") == 0) {
|
|
|
|
if (argc < 4)
|
|
|
|
usage();
|
2018-07-06 14:04:06 +00:00
|
|
|
// Redirect 1 > /dev/null
|
|
|
|
int fd = open("/dev/null", O_WRONLY);
|
|
|
|
int out = dup(STDOUT_FILENO);
|
|
|
|
xdup2(fd, STDOUT_FILENO);
|
2018-06-21 22:18:06 +00:00
|
|
|
char *loop = mount_image(argv[2], argv[3]);
|
2018-07-06 14:04:06 +00:00
|
|
|
// Restore stdin
|
|
|
|
xdup2(out, STDOUT_FILENO);
|
|
|
|
close(fd);
|
|
|
|
close(out);
|
2018-06-21 22:18:06 +00:00
|
|
|
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 14:04:06 +00: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;
|
2018-06-21 22:18:06 +00:00
|
|
|
}
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-24 14:37:45 +00:00
|
|
|
int create_img(const char *img, int size) {
|
2017-12-21 18:39:44 +00:00
|
|
|
if (size == 128) /* WTF...? */
|
|
|
|
size = 132;
|
2017-06-24 14:37:45 +00:00
|
|
|
unlink(img);
|
|
|
|
LOGI("Create %s with size %dM\n", img, size);
|
2018-03-10 18:52:24 +00:00
|
|
|
char size_str[16];
|
|
|
|
snprintf(size_str, sizeof(size_str), "%dM", size);
|
2018-06-21 22:18:06 +00:00
|
|
|
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)
|
2018-03-10 18:52:24 +00:00
|
|
|
// On Android P there is no make_ext4fs, use mke2fs
|
2018-06-21 22:18:06 +00:00
|
|
|
return exec_command_sync("/system/bin/mke2fs", "-b", "4096", "-t", "ext4", img, size_str, NULL);
|
|
|
|
else
|
2017-06-24 14:37:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-06 14:04:06 +00:00
|
|
|
int resize_img(const char *img, int size) {
|
2017-06-24 14:37:45 +00:00
|
|
|
LOGI("Resize %s to %dM\n", img, size);
|
2018-07-06 14:04:06 +00: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 14:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *mount_image(const char *img, const char *target) {
|
2018-05-26 13:25:59 +00:00
|
|
|
if (access(img, F_OK) == -1 || access(target, F_OK) == -1)
|
2017-06-30 15:22:26 +00:00
|
|
|
return NULL;
|
2018-07-06 14:04:06 +00:00
|
|
|
exec_command_sync("/system/bin/e2fsck", "-yf", img, NULL);
|
2017-06-24 14:37:45 +00:00
|
|
|
char *device = loopsetup(img);
|
|
|
|
if (device)
|
|
|
|
xmount(device, target, "ext4", 0, NULL);
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:18:06 +00:00
|
|
|
int umount_image(const char *target, const char *device) {
|
|
|
|
int ret = 0;
|
|
|
|
ret |= xumount(target);
|
2017-06-24 14:37:45 +00:00
|
|
|
int fd = xopen(device, O_RDWR);
|
2018-06-21 22:18:06 +00:00
|
|
|
ret |= ioctl(fd, LOOP_CLR_FD);
|
2017-06-24 14:37:45 +00:00
|
|
|
close(fd);
|
2018-06-21 22:18:06 +00:00
|
|
|
return ret;
|
2017-06-24 14:37:45 +00:00
|
|
|
}
|
2017-08-10 09:26:37 +00: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 14:04:06 +00:00
|
|
|
LOGI("* Move %s -> %s\n", source, target);
|
2018-02-01 20:47:16 +00:00
|
|
|
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 09:26:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-06 14:04:06 +00:00
|
|
|
char buf[PATH_MAX];
|
2017-08-10 09:26:37 +00:00
|
|
|
|
|
|
|
xmkdir(SOURCE_TMP, 0755);
|
|
|
|
xmkdir(TARGET_TMP, 0755);
|
2018-07-06 14:04:06 +00:00
|
|
|
char *s_loop, *t_loop, *m_loop;
|
2017-08-10 09:26:37 +00:00
|
|
|
s_loop = mount_image(source, SOURCE_TMP);
|
2018-06-21 22:18:06 +00:00
|
|
|
if (s_loop == NULL)
|
|
|
|
return 1;
|
2017-08-10 09:26:37 +00:00
|
|
|
t_loop = mount_image(target, TARGET_TMP);
|
2018-06-21 22:18:06 +00:00
|
|
|
if (t_loop == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2018-07-06 14:04:06 +00: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 09:26:37 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2017-10-13 16:08:12 +00:00
|
|
|
if (!(dir = xopendir(SOURCE_TMP)))
|
2017-08-10 09:26:37 +00: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 14:04:06 +00:00
|
|
|
strcmp(entry->d_name, ".core") == 0)
|
2017-08-10 09:26:37 +00:00
|
|
|
continue;
|
|
|
|
// Cleanup old module if exists
|
2018-07-06 14:04:06 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", TARGET_TMP, entry->d_name);
|
|
|
|
if (access(buf, F_OK) == 0)
|
|
|
|
rm_rf(buf);
|
2018-06-21 22:18:06 +00:00
|
|
|
LOGI("Upgrade/New module: %s\n", entry->d_name);
|
2017-08-10 09:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
2018-07-06 14:04:06 +00: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 09:26:37 +00:00
|
|
|
|
|
|
|
// Unmount all loop devices
|
|
|
|
umount_image(SOURCE_TMP, s_loop);
|
|
|
|
umount_image(TARGET_TMP, t_loop);
|
2018-07-06 14:04:06 +00:00
|
|
|
umount_image(MERGE_TMP, m_loop);
|
2017-08-10 09:26:37 +00:00
|
|
|
rmdir(SOURCE_TMP);
|
|
|
|
rmdir(TARGET_TMP);
|
2018-07-06 14:04:06 +00:00
|
|
|
rmdir(MERGE_TMP);
|
2017-08-10 09:26:37 +00:00
|
|
|
free(s_loop);
|
|
|
|
free(t_loop);
|
2018-07-06 14:04:06 +00:00
|
|
|
free(m_loop);
|
|
|
|
// Cleanup
|
2017-08-10 09:26:37 +00:00
|
|
|
unlink(source);
|
2018-07-06 14:04:06 +00:00
|
|
|
LOGI("* Move %s -> %s", buf, target);
|
|
|
|
rename(buf, target);
|
2017-08-10 09:26:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:18:06 +00:00
|
|
|
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 14:04:06 +00:00
|
|
|
resize_img(img, new_size);
|
|
|
|
char *loop2 = mount_image(img, mount);
|
|
|
|
if (loop2 == NULL)
|
2018-06-21 22:18:06 +00:00
|
|
|
return 1;
|
2018-07-06 14:04:06 +00:00
|
|
|
strcpy(loop, loop2);
|
|
|
|
free(loop2);
|
2018-06-21 22:18:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2017-08-10 09:26:37 +00:00
|
|
|
}
|