diff --git a/kernel/Makefile b/kernel/Makefile index 0cbe540f..7d23cf61 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -21,30 +21,45 @@ obj-$(CONFIG_KPM) += kpm/ REPO_OWNER := SukiSU-Ultra REPO_NAME := SukiSU-Ultra -REPO_BRANCH := main +KSU_API_VERSION_LOCAL := 3.1.6 GIT_BIN := /usr/bin/env PATH="$$PATH":/usr/bin:/usr/local/bin git CURL_BIN := /usr/bin/env PATH="$$PATH":/usr/bin:/usr/local/bin curl -KSU_GITHUB_VERSION := $(shell $(CURL_BIN) -sI "https://api.github.com/repos/$(REPO_OWNER)/$(REPO_NAME)/commits?sha=$(REPO_BRANCH)&per_page=1" | grep -i "link:" | sed -n 's/.*page=\([0-9]*\)>; rel="last".*/\1/p') +KSU_GITHUB_VERSION := $(shell $(CURL_BIN) -s "https://api.github.com/repos/$(REPO_OWNER)/$(REPO_NAME)/releases/latest" | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/') + +LOCAL_GIT_EXISTS := $(shell test -e $(srctree)/$(src)/../.git && echo 1 || echo 0) + +define get_ksu_api_version +$(shell echo $1 | awk -F'[-.]' '{printf "%d%02d%02d", $$1, $$2, $$3}') +endef + +define get_ksu_kernel_version +v$1-$(shell cd $(srctree)/$(src); $(GIT_BIN) rev-parse --short=8 HEAD)@$(shell cd $(srctree)/$(src); $(GIT_BIN) rev-parse --abbrev-ref HEAD) +endef ifeq ($(KSU_GITHUB_VERSION),) - ifeq ($(shell test -e $(srctree)/$(src)/../.git; echo $$?),0) - $(shell cd $(srctree)/$(src); [ -f ../.git/shallow ] && $(GIT_BIN) fetch --unshallow) - KSU_LOCAL_VERSION := $(shell cd $(srctree)/$(src); $(GIT_BIN) rev-list --count $(REPO_BRANCH)) - $(eval KSU_VERSION := $(shell expr 10000 + $(KSU_LOCAL_VERSION) + 700)) - $(info -- SukiSU-Ultra version (local .git): $(KSU_VERSION)) - else - $(eval KSU_VERSION := 13000) - $(warning -- Could not fetch version online or via local .git! Using fallback version: $(KSU_VERSION)) - endif + ifeq ($(LOCAL_GIT_EXISTS),1) + $(shell cd $(srctree)/$(src); [ -f ../.git/shallow ] && $(GIT_BIN) fetch --unshallow) + KSU_API_VERSION := $(call get_ksu_api_version,$(KSU_API_VERSION_LOCAL)) + KSU_KERNEL_VERSION := $(call get_ksu_kernel_version,$(KSU_API_VERSION_LOCAL)) + $(info -- $(REPO_NAME) version (local .git): $(KSU_KERNEL_VERSION)) + $(info -- $(REPO_NAME) Formatted version (local .git): $(KSU_API_VERSION)) + else + KSU_KERNEL_VERSION := v$(KSU_API_VERSION_LOCAL)-$(REPO_NAME)-unknown@unknown + KSU_API_VERSION := $(call get_ksu_api_version,$(KSU_API_VERSION_LOCAL)) + $(warning -- $(REPO_NAME) version: $(KSU_KERNEL_VERSION)) + endif else - $(eval KSU_VERSION := $(shell expr 10000 + $(KSU_GITHUB_VERSION) + 700)) - $(info -- SukiSU-Ultra version (GitHub): $(KSU_VERSION)) + $(shell cd $(srctree)/$(src); [ -f ../.git/shallow ] && $(GIT_BIN) fetch --unshallow) + KSU_API_VERSION := $(call get_ksu_api_version,$(KSU_GITHUB_VERSION)) + KSU_KERNEL_VERSION := $(call get_ksu_kernel_version,$(KSU_GITHUB_VERSION)) + $(info -- $(REPO_NAME) version (Github): $(KSU_KERNEL_VERSION)) + $(info -- $(REPO_NAME) Formatted version (Github): $(KSU_API_VERSION)) endif -$(info -- SukiSU-Ultra version: $(KSU_VERSION)) -ccflags-y += -DKSU_VERSION=$(KSU_VERSION) +ccflags-y += -DKSU_VERSION=\"$(KSU_KERNEL_VERSION)\" +ccflags-y += -DKSU_API_VERSION=$(KSU_API_VERSION) ifeq ($(strip $(CONFIG_KSU_MANUAL_HOOK)),y) $(info -- SukiSU: Manual hooking enabled!) diff --git a/kernel/core_hook.c b/kernel/core_hook.c index 0820af82..4192db09 100644 --- a/kernel/core_hook.c +++ b/kernel/core_hook.c @@ -55,6 +55,8 @@ #include "kpm/kpm.h" #endif +char ksu_version_id[KSU_MAX_VERSION_NAME] = KSU_VERSION; + #ifdef CONFIG_KSU_SUSFS bool susfs_is_allow_su(void) { @@ -397,7 +399,7 @@ int ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3, if (copy_to_user(arg3, &version, sizeof(version))) { pr_err("prctl reply error, cmd: %lu\n", arg2); } - u32 version_flags = 0; + u32 version_flags = 2; #ifdef MODULE version_flags |= 0x1; #endif @@ -510,6 +512,14 @@ int ksu_handle_prctl(int option, unsigned long arg2, unsigned long arg3, return 0; } + if (arg2 == CMD_GET_FULL_VERSION) { + if (copy_to_user(arg3, &ksu_version_id, KSU_MAX_VERSION_NAME)) { + pr_err("prctl reply error, cmd: %lu\n", arg2); + } + + return 0; + } + #ifdef CONFIG_KPM // ADD: 添加KPM模块控制 if(sukisu_is_kpm_control_code(arg2)) { diff --git a/kernel/ksu.h b/kernel/ksu.h index 28ddaa59..975bea86 100644 --- a/kernel/ksu.h +++ b/kernel/ksu.h @@ -4,7 +4,7 @@ #include #include -#define KERNEL_SU_VERSION KSU_VERSION +#define KERNEL_SU_VERSION KSU_API_VERSION #define KERNEL_SU_OPTION 0xDEADBEEF #define CMD_GRANT_ROOT 0 @@ -23,6 +23,9 @@ #define CMD_UID_SHOULD_UMOUNT 13 #define CMD_IS_SU_ENABLED 14 #define CMD_ENABLE_SU 15 + +#define CMD_GET_FULL_VERSION 30 + #define CMD_ENABLE_KPM 100 #define CMD_HOOK_TYPE 101 #define CMD_GET_SUSFS_FEATURE_STATUS 102 @@ -37,6 +40,12 @@ #define KSU_MAX_GROUPS 32 #define KSU_SELINUX_DOMAIN 64 +#define KSU_MAX_VERSION_NAME 255 + +#ifndef KSU_VERSION +#define KSU_VERSION "v0-unknown-00000000@unkown" +#endif + // SUSFS Functional State Structures struct susfs_feature_status { bool status_sus_path;