2019-05-27 07:29:43 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <libgen.h>
|
2019-07-02 05:58:19 +00:00
|
|
|
#include <vector>
|
2019-05-27 07:29:43 +00:00
|
|
|
|
|
|
|
#include <xz.h>
|
2021-09-08 02:35:28 +00:00
|
|
|
|
2022-05-12 09:03:42 +00:00
|
|
|
#include <base.hpp>
|
2022-08-19 11:49:19 +00:00
|
|
|
#include <embed.hpp>
|
2022-04-08 09:13:31 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include "init.hpp"
|
2019-05-27 07:29:43 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2023-09-21 12:47:21 +00:00
|
|
|
bool unxz(out_stream &strm, rust::Slice<const uint8_t> bytes) {
|
2020-12-31 06:11:24 +00:00
|
|
|
uint8_t out[8192];
|
|
|
|
xz_crc32_init();
|
2023-09-21 12:47:21 +00:00
|
|
|
size_t size = bytes.size();
|
2020-12-31 06:11:24 +00:00
|
|
|
struct xz_dec *dec = xz_dec_init(XZ_DYNALLOC, 1 << 26);
|
|
|
|
struct xz_buf b = {
|
2023-09-21 12:47:21 +00:00
|
|
|
.in = bytes.data(),
|
2020-12-31 06:11:24 +00:00
|
|
|
.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;
|
2023-09-21 12:47:21 +00:00
|
|
|
strm.write(out, b.out_pos);
|
2020-12-31 06:11:24 +00:00
|
|
|
b.out_pos = 0;
|
|
|
|
} while (b.in_pos != size);
|
|
|
|
return true;
|
2019-05-27 07:29:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-25 23:27:37 +00:00
|
|
|
void restore_ramdisk_init() {
|
|
|
|
unlink("/init");
|
|
|
|
|
|
|
|
const char *orig_init = backup_init();
|
|
|
|
if (access(orig_init, F_OK) == 0) {
|
|
|
|
xrename(orig_init, "/init");
|
|
|
|
} else {
|
|
|
|
// If the backup init is missing, this means that the boot ramdisk
|
|
|
|
// was created from scratch, and the real init is in a separate CPIO,
|
|
|
|
// which is guaranteed to be placed at /system/bin/init.
|
|
|
|
xsymlink(INIT_PATH, "/init");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 19:45:32 +00:00
|
|
|
class RecoveryInit : public BaseInit {
|
|
|
|
public:
|
2022-03-14 11:22:09 +00:00
|
|
|
using BaseInit::BaseInit;
|
2020-12-31 06:11:24 +00:00
|
|
|
void start() override {
|
|
|
|
LOGD("Ramdisk is recovery, abort\n");
|
2022-06-25 23:27:37 +00:00
|
|
|
restore_ramdisk_init();
|
2020-12-31 06:11:24 +00:00
|
|
|
rm_rf("/.backup");
|
|
|
|
exec_init();
|
|
|
|
}
|
2019-06-16 19:45:32 +00:00
|
|
|
};
|
|
|
|
|
2019-05-27 07:29:43 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2020-12-31 06:11:24 +00:00
|
|
|
umask(0);
|
2019-05-27 07:29:43 +00:00
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
auto name = basename(argv[0]);
|
|
|
|
if (name == "magisk"sv)
|
|
|
|
return magisk_proxy_main(argc, argv);
|
2019-12-06 17:02:34 +00:00
|
|
|
|
2020-12-31 06:11:24 +00:00
|
|
|
if (getpid() != 1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
BaseInit *init;
|
2021-10-26 07:35:55 +00:00
|
|
|
BootConfig config{};
|
2020-12-31 06:11:24 +00:00
|
|
|
|
|
|
|
if (argc > 1 && argv[1] == "selinux_setup"sv) {
|
2023-05-02 23:49:43 +00:00
|
|
|
rust::setup_klog();
|
2020-12-31 06:11:24 +00:00
|
|
|
init = new SecondStageInit(argv);
|
|
|
|
} else {
|
|
|
|
// This will also mount /sys and /proc
|
2021-10-26 07:35:55 +00:00
|
|
|
load_kernel_info(&config);
|
2020-12-31 06:11:24 +00:00
|
|
|
|
2021-10-26 07:35:55 +00:00
|
|
|
if (config.skip_initramfs)
|
2022-03-14 11:22:09 +00:00
|
|
|
init = new LegacySARInit(argv, &config);
|
2021-10-26 07:35:55 +00:00
|
|
|
else if (config.force_normal_boot)
|
|
|
|
init = new FirstStageInit(argv, &config);
|
2021-01-18 12:25:26 +00:00
|
|
|
else if (access("/sbin/recovery", F_OK) == 0 || access("/system/bin/recovery", F_OK) == 0)
|
2021-10-26 07:35:55 +00:00
|
|
|
init = new RecoveryInit(argv, &config);
|
2021-01-18 12:25:26 +00:00
|
|
|
else if (check_two_stage())
|
2021-10-26 07:35:55 +00:00
|
|
|
init = new FirstStageInit(argv, &config);
|
2021-01-18 12:25:26 +00:00
|
|
|
else
|
2021-10-26 07:35:55 +00:00
|
|
|
init = new RootFSInit(argv, &config);
|
2020-12-31 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the main routine
|
|
|
|
init->start();
|
|
|
|
exit(1);
|
2019-05-27 07:29:43 +00:00
|
|
|
}
|