Install both 32 and 64 bit binaries

This commit is contained in:
topjohnwu 2021-01-18 12:37:08 -08:00
parent 5a71998b4e
commit d2bc2cfcf8
5 changed files with 74 additions and 54 deletions

View File

@ -35,7 +35,8 @@ int main(int argc, char *argv[]) {
cmdline_logging();
init_argv0(argc, argv);
if (basename(argv[0]) == "magisk"sv) {
string_view base = basename(argv[0]);
if (base == "magisk" || base == "magisk32" || base == "magisk64") {
if (argc > 1 && argv[1][0] != '-') {
// Calling applet via magisk [applet] args
--argc;

View File

@ -93,10 +93,9 @@ void restore_tmpcon() {
auto dir = xopen_dir(MAGISKTMP.data());
int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (SDK_INT >= 26 && entry->d_name == "magisk"sv)
setfilecon_at(dfd, entry->d_name, EXEC_CON);
else
for (dirent *entry; (entry = xreaddir(dir.get()));)
setfilecon_at(dfd, entry->d_name, SYSTEM_CON);
}
string magisk = MAGISKTMP + "/magisk";
setfilecon(magisk.data(), EXEC_CON);
}

View File

@ -175,7 +175,8 @@ static void magic_mount(const string &sdir, const string &ddir = "") {
}
static void patch_socket_name(const char *path) {
char rstr[16];
static char rstr[16] = { 0 };
if (rstr[0] == '\0')
gen_rand_str(rstr, sizeof(rstr));
auto bin = mmap_data::rw(path);
bin.patch({ make_pair(MAIN_SOCKET, rstr) });
@ -289,12 +290,23 @@ void SARBase::patch_rootdir() {
// Extract magisk
{
auto magisk = mmap_data::ro("magisk.xz");
unlink("magisk.xz");
int fd = xopen("magisk", O_WRONLY | O_CREAT, 0755);
auto magisk = mmap_data::ro("magisk32.xz");
unlink("magisk32.xz");
int fd = xopen("magisk32", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf, magisk.sz);
close(fd);
patch_socket_name("magisk");
patch_socket_name("magisk32");
if (access("magisk64.xz", F_OK) == 0) {
magisk = mmap_data::ro("magisk64.xz");
unlink("magisk64.xz");
fd = xopen("magisk64", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf, magisk.sz);
close(fd);
patch_socket_name("magisk64");
xsymlink("./magisk64", "magisk");
} else {
xsymlink("./magisk32", "magisk");
}
}
// Mount rootdir
@ -352,22 +364,33 @@ void MagiskProxy::start() {
// Backup stuffs before removing them
self = mmap_data::ro("/sbin/magisk");
config = mmap_data::ro("/.backup/.magisk");
auto magisk = mmap_data::ro("/sbin/magisk.xz");
auto magisk = mmap_data::ro("/sbin/magisk32.xz");
auto magisk64 = mmap_data::ro("/sbin/magisk64.xz");
char custom_rules_dir[64];
custom_rules_dir[0] = '\0';
xreadlink(TMP_RULESDIR, custom_rules_dir, sizeof(custom_rules_dir));
unlink("/sbin/magisk");
unlink("/sbin/magisk.xz");
unlink("/sbin/magisk32.xz");
unlink("/sbin/magisk64.xz");
rm_rf("/.backup");
setup_tmp("/sbin");
// Extract magisk
int fd = xopen("/sbin/magisk", O_WRONLY | O_CREAT, 0755);
int fd = xopen("/sbin/magisk32", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk.buf, magisk.sz);
close(fd);
patch_socket_name("/sbin/magisk");
patch_socket_name("/sbin/magisk32");
if (magisk64.sz) {
fd = xopen("/sbin/magisk64", O_WRONLY | O_CREAT, 0755);
unxz(fd, magisk64.buf, magisk64.sz);
close(fd);
patch_socket_name("/sbin/magisk64");
xsymlink("./magisk64", "/sbin/magisk");
} else {
xsymlink("./magisk32", "/sbin/magisk");
}
// Create symlinks pointing back to /root
recreate_sbin("/root", false);

View File

@ -1,7 +1,3 @@
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <utils.hpp>
#include <cpio.hpp>
@ -35,7 +31,7 @@ public:
bool check_env(const char *name) {
const char *val = getenv(name);
return val ? strcmp(val, "true") == 0 : false;
return val ? val == "true"sv : false;
}
void magisk_cpio::patch() {
@ -284,60 +280,62 @@ int cpio_commands(int argc, char *argv[]) {
int cmdc;
char *cmdv[6];
while (argc) {
// Clean up
for (int i = 0; i < argc; ++i) {
// Reset
cmdc = 0;
memset(cmdv, NULL, sizeof(cmdv));
memset(cmdv, 0, sizeof(cmdv));
// Split the commands
for (char *tok = strtok(argv[0], " "); tok; tok = strtok(nullptr, " "))
char *tok = strtok(argv[i], " ");
while (tok && cmdc < std::size(cmdv)) {
if (cmdc == 0 && tok[0] == '#')
break;
cmdv[cmdc++] = tok;
tok = strtok(nullptr, " ");
}
if (cmdc == 0)
continue;
if (strcmp(cmdv[0], "test") == 0) {
if (cmdv[0] == "test"sv) {
exit(cpio.test());
} else if (strcmp(cmdv[0], "restore") == 0) {
} else if (cmdv[0] == "restore"sv) {
cpio.restore();
} else if (strcmp(cmdv[0], "sha1") == 0) {
} else if (cmdv[0] == "sha1"sv) {
char *sha1 = cpio.sha1();
if (sha1) printf("%s\n", sha1);
return 0;
} else if (strcmp(cmdv[0], "compress") == 0){
} else if (cmdv[0] == "compress"sv){
cpio.compress();
} else if (strcmp(cmdv[0], "decompress") == 0){
} else if (cmdv[0] == "decompress"sv){
cpio.decompress();
} else if (strcmp(cmdv[0], "patch") == 0) {
} else if (cmdv[0] == "patch"sv) {
cpio.patch();
} else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) {
} else if (cmdc == 2 && cmdv[0] == "exists"sv) {
exit(!cpio.exists(cmdv[1]));
} else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) {
} else if (cmdc == 2 && cmdv[0] == "backup"sv) {
cpio.backup(cmdv[1]);
} else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) {
bool r = cmdc > 2 && strcmp(cmdv[1], "-r") == 0;
} else if (cmdc >= 2 && cmdv[0] == "rm"sv) {
bool r = cmdc > 2 && cmdv[1] == "-r"sv;
cpio.rm(cmdv[1 + r], r);
} else if (cmdc == 3 && strcmp(cmdv[0], "mv") == 0) {
} else if (cmdc == 3 && cmdv[0] == "mv"sv) {
cpio.mv(cmdv[1], cmdv[2]);
} else if (strcmp(cmdv[0], "extract") == 0) {
} else if (cmdv[0] == "extract"sv) {
if (cmdc == 3) {
return !cpio.extract(cmdv[1], cmdv[2]);
} else {
cpio.extract();
return 0;
}
} else if (cmdc == 3 && strcmp(cmdv[0], "mkdir") == 0) {
} else if (cmdc == 3 && cmdv[0] == "mkdir"sv) {
cpio.mkdir(strtoul(cmdv[1], nullptr, 8), cmdv[2]);
} else if (cmdc == 3 && strcmp(cmdv[0], "ln") == 0) {
} else if (cmdc == 3 && cmdv[0] == "ln"sv) {
cpio.ln(cmdv[1], cmdv[2]);
} else if (cmdc == 4 && strcmp(cmdv[0], "add") == 0) {
} else if (cmdc == 4 && cmdv[0] == "add"sv) {
cpio.add(strtoul(cmdv[1], nullptr, 8), cmdv[2], cmdv[3]);
} else {
return 1;
}
--argc;
++argv;
}
cpio.dump(incpio);

View File

@ -53,8 +53,8 @@ if [ -z $SOURCEDMODE ]; then
cd "`getdir "${BASH_SOURCE:-$0}"`"
# Load utility functions
. ./util_functions.sh
# Detect version and architecture
api_level_arch_detect
# Check if 64-bit
[ -d /system/lib64 ] && IS64BIT=true || IS64BIT=false
fi
BOOTIMAGE="$1"
@ -135,23 +135,22 @@ echo "RECOVERYMODE=$RECOVERYMODE" >> config
[ ! -z $SHA1 ] && echo "SHA1=$SHA1" >> config
# Compress to save precious ramdisk space
if $IS64BIT; then
./magiskboot compress=xz magisk64 magisk.xz
else
./magiskboot compress=xz magisk32 magisk.xz
fi
./magiskboot compress=xz magisk32 magisk32.xz
./magiskboot compress=xz magisk64 magisk64.xz
$IS64BIT && SKIP64="" || SKIP64="#"
./magiskboot cpio ramdisk.cpio \
"add 750 init magiskinit" \
"add 0750 init magiskinit" \
"mkdir 0750 overlay.d" \
"mkdir 0750 overlay.d/sbin" \
"add 750 overlay.d/sbin/magisk.xz magisk.xz" \
"add 0644 overlay.d/sbin/magisk32.xz magisk32.xz" \
"$SKIP64 add 0644 overlay.d/sbin/magisk64.xz magisk64.xz" \
"patch" \
"backup ramdisk.cpio.orig" \
"mkdir 000 .backup" \
"add 000 .backup/.magisk config"
rm -f ramdisk.cpio.orig config magisk.xz
rm -f ramdisk.cpio.orig config magisk*.xz
#################
# Binary Patches