diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml new file mode 100644 index 0000000..fd76773 --- /dev/null +++ b/.github/workflows/compile.yml @@ -0,0 +1,66 @@ +name: build module + +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v3 + - name: Install pnpm + uses: pnpm/action-setup@v4 + with: + version: 10 + - uses: actions/setup-node@v6 + with: + node-version: 22 + cache: pnpm + + - name: grab zig + run: | + curl -L https://ziglang.org/download/0.15.2/zig-x86-linux-0.15.2.tar.xz -o zig.tar.xz + tar -xf zig.tar.xz + mv zig-* zig + + - name: grab sstrip from ELFkickers + run: | + curl -L https://www.muppetlabs.com/~breadbox/pub/software/ELFkickers-3.2.tar.gz -o sstrip.tar.gz + tar xf sstrip.tar.gz + make -C ELFkickers-3.2 sstrip + cp ELFkickers-3.2/sstrip/sstrip . + + - name: webui + run: | + cd webui + pnpm i + pnpm build + + - name: compile binary via zigcc + run: | + echo "arm, aarch64 and x86_64" + pwd + chmod +x sstrip ; mv sstrip strip + + PATH=$(pwd)/zig:$(pwd):$PATH + + cd src; make + + wc -c bin/mm_* + upx --ultra-brute bin/mm_* + + wc -c bin/mm_* + file bin/mm_* + + cd ../template + pwd + mkdir bin + pwd + mv -f ../src/bin/* bin/ + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: zip + path: template diff --git a/src/Makefile b/src/Makefile index df31864..955d223 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,13 +13,13 @@ SRCS := utils.c magic_mount.c main.c OUTDIR := bin # common flags -CFLAGS_COMMON := -D_POSIX_C_SOURCE=200809L -std=c23 -Wall -Wextra -pedantic +CFLAGS_COMMON := -Wl,--gc-sections -static -Wpedantic -Werror CFLAGS_COMMON += -DVERSION=\"$(VERSION)\" LDFLAGS_COMMON := -static # build mode specific flags -CFLAGS_RELEASE := -O2 -DNDEBUG +CFLAGS_RELEASE := -Oz -s -DNDEBUG CFLAGS_DEBUG := -O0 -g -DDEBUG # zig target triples @@ -84,4 +84,4 @@ $(BIN_ARMV7): $(SRCS) $(CC) -target $(TARGET_ARMV7) $(CFLAGS) $^ -o $@ $(LDFLAGS_COMMON) clean: - rm -rf $(OUTDIR) \ No newline at end of file + rm -rf $(OUTDIR) diff --git a/src/magic_mount.c b/src/magic_mount.c index 1c10207..edae7d9 100644 --- a/src/magic_mount.c +++ b/src/magic_mount.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include MountStats g_stats = { 0 }; @@ -26,6 +28,25 @@ int g_failed_modules_count = 0; char **g_extra_parts = NULL; int g_extra_parts_count = 0; +int global_fd = 0; + +static void grab_fd(void) { syscall(SYS_reboot, KSU_INSTALL_MAGIC1, KSU_INSTALL_MAGIC2, 0, (void *)&global_fd); } + +static void send_unmountable(const char *mntpoint) +{ + struct ksu_add_try_umount_cmd cmd = {0}; + + if (!global_fd) + return; + + cmd.arg = (uint64_t)mntpoint; + cmd.flags = 0x2; + cmd.mode = 1; + + ioctl(global_fd, KSU_IOCTL_ADD_TRY_UMOUNT, &cmd); + +} + static void register_module_failure(const char *module_name) { if (!module_name) @@ -496,7 +517,8 @@ static int do_magic(const char *base, const char *wbase, Node *node, LOGE("bind %s->%s: %s", node->module_path, target, strerror(errno)); return -1; - } + } else + send_unmountable(target); // tell ksu about this mount (void)mount(NULL, target, NULL, MS_REMOUNT | MS_BIND | MS_RDONLY, NULL); @@ -704,6 +726,10 @@ static int do_magic(const char *base, const char *wbase, Node *node, LOGI("move mountpoint success: %s -> %s", wpath, path); (void)mount(NULL, path, NULL, MS_REC | MS_PRIVATE, NULL); + + // tell ksu about this one too + send_unmountable(path); + } g_stats.nodes_mounted++; @@ -800,6 +826,8 @@ int magic_mount(const char *tmp_root) node_free(root); return -1; } + + grab_fd(); LOGI("starting magic_mount core logic: tmpfs_source=%s tmp_dir=%s", g_mount_source, tmp_dir); diff --git a/src/magic_mount.h b/src/magic_mount.h index a3e8e69..9710d7c 100644 --- a/src/magic_mount.h +++ b/src/magic_mount.h @@ -2,6 +2,7 @@ #define MAGIC_MOUNT_H #include +#include #define DISABLE_FILE_NAME "disable" #define REMOVE_FILE_NAME "remove" @@ -36,4 +37,16 @@ int magic_mount(const char *tmp_root); void add_extra_part_token(const char *start, size_t len); +// kernelsu api, atleast ones we care about +#define KSU_INSTALL_MAGIC1 0xDEADBEEF +#define KSU_INSTALL_MAGIC2 0xCAFEBABE + +struct ksu_add_try_umount_cmd { + uint64_t arg; // char ptr, this is the mountpoint + uint32_t flags; // this is the flag we use for it + uint8_t mode; // denotes what to do with it 0:wipe_list 1:add_to_list 2:delete_entry +}; + +#define KSU_IOCTL_ADD_TRY_UMOUNT _IOC(_IOC_WRITE, 'K', 18, 0) + #endif /* MAGIC_MOUNT_H */ diff --git a/template/metainstall.sh b/template/metainstall.sh index 9e299ee..a3306f6 100644 --- a/template/metainstall.sh +++ b/template/metainstall.sh @@ -18,4 +18,21 @@ handle_partition() { # call install function, this is important! install_module +mm_handle_partition() { + partition="$1" + + if [ ! -d "$MODPATH/system/$partition" ]; then + return + fi + + if [ -L "/system/$partition" ] && [ -d "/$partition" ]; then + ui_print "- Handle partition /$partition" + ln -sf "./system/$partition" "$MODPATH/$partition" + fi +} + +mm_handle_partition system_ext +mm_handle_partition vendor +mm_handle_partition product + ui_print "- Installation complete" diff --git a/template/metamount.sh b/template/metamount.sh index c9e1f6b..c4d9584 100644 --- a/template/metamount.sh +++ b/template/metamount.sh @@ -34,10 +34,11 @@ $BINARY EXIT_CODE=$? -if [ $EXIT_CODE -ne 0 ]; then +if [ "$EXIT_CODE" = 0 ]; then + /data/adb/ksud kernel notify-module-mounted + log "Mount completed successfully" +else log "Mount failed with exit code $EXIT_CODE" - exit $EXIT_CODE fi -log "Mount completed successfully" exit 0