Magisk/native/jni/init/init.cpp

120 lines
2.9 KiB
C++
Raw Normal View History

2019-05-27 00:29:43 -07:00
#include <sys/stat.h>
#include <sys/types.h>
#include <libgen.h>
2019-07-01 22:58:19 -07:00
#include <vector>
2019-05-27 00:29:43 -07:00
#include <xz.h>
2020-03-09 01:50:30 -07:00
#include <utils.hpp>
#include <binaries.h>
2019-05-27 00:29:43 -07:00
#if defined(__arm__)
#include <armeabi-v7a_binaries.h>
#elif defined(__aarch64__)
#include <arm64-v8a_binaries.h>
#elif defined(__i386__)
#include <x86_binaries.h>
#elif defined(__x86_64__)
#include <x86_64_binaries.h>
#else
#error Unsupported ABI
#endif
2020-03-09 01:50:30 -07:00
#include "init.hpp"
2019-05-27 00:29:43 -07:00
using namespace std;
2021-01-18 04:25:26 -08:00
bool unxz(int fd, const uint8_t *buf, size_t size) {
uint8_t out[8192];
xz_crc32_init();
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
struct xz_buf b = {
.in = buf,
.in_pos = 0,
.in_size = size,
.out = out,
.out_pos = 0,
.out_size = sizeof(out)
};
enum xz_ret ret;
do {
ret = xz_dec_run(dec, &b);
if (ret != XZ_OK && ret != XZ_STREAM_END)
return false;
write(fd, out, b.out_pos);
b.out_pos = 0;
} while (b.in_pos != size);
return true;
2019-05-27 00:29:43 -07:00
}
static int dump_bin(const uint8_t *buf, size_t sz, const char *path, mode_t mode) {
int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
if (fd < 0)
return 1;
if (!unxz(fd, buf, sz))
return 1;
close(fd);
return 0;
2019-05-27 00:29:43 -07:00
}
int dump_manager(const char *path, mode_t mode) {
return dump_bin(manager_xz, sizeof(manager_xz), path, mode);
}
int dump_preload(const char *path, mode_t mode) {
return dump_bin(preload_xz, sizeof(preload_xz), path, mode);
}
2019-06-16 12:45:32 -07:00
class RecoveryInit : public BaseInit {
public:
2022-03-14 04:22:09 -07:00
using BaseInit::BaseInit;
void start() override {
LOGD("Ramdisk is recovery, abort\n");
rename(backup_init(), "/init");
rm_rf("/.backup");
exec_init();
}
2019-06-16 12:45:32 -07:00
};
2019-05-27 00:29:43 -07:00
int main(int argc, char *argv[]) {
umask(0);
2019-05-27 00:29:43 -07:00
auto name = basename(argv[0]);
if (name == "magisk"sv)
return magisk_proxy_main(argc, argv);
2019-12-06 12:02:34 -05:00
if (argc > 1 && argv[1] == "-x"sv) {
2021-01-18 04:25:26 -08:00
if (argc > 2 && argv[2] == "manager"sv)
return dump_manager(argv[3], 0644);
2021-01-18 04:25:26 -08:00
return 1;
}
if (getpid() != 1)
return 1;
BaseInit *init;
2021-10-26 00:35:55 -07:00
BootConfig config{};
if (argc > 1 && argv[1] == "selinux_setup"sv) {
init = new SecondStageInit(argv);
} else {
// This will also mount /sys and /proc
2021-10-26 00:35:55 -07:00
load_kernel_info(&config);
2021-10-26 00:35:55 -07:00
if (config.skip_initramfs)
2022-03-14 04:22:09 -07:00
init = new LegacySARInit(argv, &config);
2021-10-26 00:35:55 -07:00
else if (config.force_normal_boot)
init = new FirstStageInit(argv, &config);
2021-01-18 04:25:26 -08:00
else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
2021-10-26 00:35:55 -07:00
init = new RecoveryInit(argv, &config);
2021-01-18 04:25:26 -08:00
else if (check_two_stage())
2021-10-26 00:35:55 -07:00
init = new FirstStageInit(argv, &config);
2021-01-18 04:25:26 -08:00
else
2021-10-26 00:35:55 -07:00
init = new RootFSInit(argv, &config);
}
// Run the main routine
init->start();
exit(1);
2019-05-27 00:29:43 -07:00
}