From 59d6932c377b7eebd2fc127f958038c77910d090 Mon Sep 17 00:00:00 2001 From: lzghzr Date: Sun, 19 May 2024 23:38:23 +0800 Subject: [PATCH] ci --- qti_battery_charger/Makefile | 38 +++++-- qti_battery_charger/qbc_utils.h | 48 +++++++++ qti_battery_charger/qti_battery_charger.c | 81 ++++++-------- xperia_ii_battery_age/Makefile | 38 +++++-- xperia_ii_battery_age/xiiba_utils.h | 48 +++++++++ xperia_ii_battery_age/xperia_ii_battery_age.c | 100 ++++++++---------- 6 files changed, 230 insertions(+), 123 deletions(-) create mode 100644 qti_battery_charger/qbc_utils.h create mode 100644 xperia_ii_battery_age/xiiba_utils.h diff --git a/qti_battery_charger/Makefile b/qti_battery_charger/Makefile index c4c29c6..a7132e6 100644 --- a/qti_battery_charger/Makefile +++ b/qti_battery_charger/Makefile @@ -1,14 +1,27 @@ -ifndef TARGET_COMPILE - $(error TARGET_COMPILE not set) -endif +QBC_VERSION := 1.0.2 ifndef KP_DIR KP_DIR = ../KernelPatch endif +OS_NAME = $(shell uname | tr A-Z a-z) +MACHINE = $(shell uname -m) +NDK_BIN_DIR := toolchains/llvm/prebuilt/$(OS_NAME)-$(MACHINE)/bin +ifdef ANDROID_NDK_LATEST_HOME + NDK_PATH ?= $(ANDROID_NDK_LATEST_HOME)/$(NDK_BIN_DIR) +else ifdef ANDROID_NDK + NDK_PATH ?= $(ANDROID_NDK)/$(NDK_BIN_DIR) +endif + +ifdef TARGET_COMPILE + CC := $(TARGET_COMPILE)gcc + LD := $(TARGET_COMPILE)ld +else ifdef NDK_PATH + CC := $(NDK_PATH)/aarch64-linux-android31-clang + LD := $(NDK_PATH)/ld.lld +endif -CC = $(TARGET_COMPILE)gcc -LD = $(TARGET_COMPILE)ld +CFLAGS = -Wall -O2 -fno-PIC -fno-asynchronous-unwind-tables -fno-stack-protector -fno-common -DQBC_VERSION=\"$(QBC_VERSION)$(QBC_VER)\" INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include @@ -16,15 +29,22 @@ INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) objs := qti_battery_charger.o -all: qti_battery_charger.kpm +all: qti_battery_charger_$(QBC_VERSION).kpm + +debug: CFLAGS += -DDEBUG +debug: QBC_VER := _d +debug: qti_battery_charger_$(QBC_VERSION)_debug.kpm + +qti_battery_charger_$(QBC_VERSION).kpm: ${objs} + ${CC} -r -o $@ $^ -qti_battery_charger.kpm: ${objs} +qti_battery_charger_$(QBC_VERSION)_debug.kpm: ${objs} ${CC} -r -o $@ $^ %.o: %.c - ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -T../demo.lds -c -O2 -o $@ $< + ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -c -o $@ $< .PHONY: clean clean: rm -rf *.kpm - find . -name "*.o" | xargs rm -f \ No newline at end of file + find . -name "*.o" | xargs rm -f diff --git a/qti_battery_charger/qbc_utils.h b/qti_battery_charger/qbc_utils.h new file mode 100644 index 0000000..3a7da36 --- /dev/null +++ b/qti_battery_charger/qbc_utils.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2024 bmax121. All Rights Reserved. + * Copyright (C) 2024 lzghzr. All Rights Reserved. + */ +#ifndef __QBC_UTILS_H +#define __QBC_UTILS_H + +#include +#include +#include +#include +#include + +#define lookup_name(func) \ + func = 0; \ + func = (typeof(func))kallsyms_lookup_name(#func); \ + pr_info("kernel function %s addr: %llx\n", #func, func); \ + if (!func) \ + { \ + return -21; \ + } + +#define hook_func(func, argv, before, after, udata) \ + if (!func) \ + { \ + return -22; \ + } \ + hook_err_t hook_err_##func = hook_wrap(func, argv, before, after, udata); \ + if (hook_err_##func) \ + { \ + func = 0; \ + pr_err("hook %s error: %d\n", #func, hook_err_##func); \ + return -23; \ + } \ + else \ + { \ + pr_info("hook %s success\n", #func); \ + } + +#define unhook_func(func) \ + if (func && !is_bad_address(func)) \ + { \ + unhook(func); \ + func = 0; \ + } + +#endif /* __QBC_UTILS_H */ diff --git a/qti_battery_charger/qti_battery_charger.c b/qti_battery_charger/qti_battery_charger.c index d442893..fa3fb5b 100644 --- a/qti_battery_charger/qti_battery_charger.c +++ b/qti_battery_charger/qti_battery_charger.c @@ -10,28 +10,26 @@ #include #include -#include "../demo.h" +#include "qbc_utils.h" #include "battchg.h" KPM_NAME("qti_battery_charger"); -KPM_VERSION("1.0.1"); +KPM_VERSION(QBC_VERSION); KPM_LICENSE("GPL v2"); KPM_AUTHOR("lzghzr"); KPM_DESCRIPTION("set battery_psy_get_prop value"); -int (*do_init_module)(struct module *mod) = 0; -int (*battery_psy_get_prop)(struct power_supply *psy, enum power_supply_property prop, union power_supply_propval *pval) = 0; +int (*do_init_module)(struct module* mod) = 0; +int (*battery_psy_get_prop)(struct power_supply* psy, enum power_supply_property prop, union power_supply_propval* pval) = 0; char MODULE_NAME[] = "qti_battery_charger"; char MODEL_NAME[] = "SNYSCA6"; -void battery_psy_get_prop_after(hook_fargs3_t *args, void *udata) -{ +void battery_psy_get_prop_after(hook_fargs3_t* args, void* udata) { enum power_supply_property prop = args->arg1; - union power_supply_propval *pval = (typeof(pval))args->arg2; + union power_supply_propval* pval = (typeof(pval))args->arg2; - switch (prop) - { + switch (prop) { // case POWER_SUPPLY_PROP_CYCLE_COUNT: // pval->intval = 1; // break; @@ -40,92 +38,73 @@ void battery_psy_get_prop_after(hook_fargs3_t *args, void *udata) // pval->intval = 5000000; // break; case POWER_SUPPLY_PROP_CAPACITY: - if (pval->intval < 10) - { + if (pval->intval < 10) { pval->intval = 10; } break; case POWER_SUPPLY_PROP_MODEL_NAME: - memcpy((char *)pval->strval, MODEL_NAME, sizeof(MODEL_NAME)); + memcpy((char*)pval->strval, MODEL_NAME, sizeof(MODEL_NAME)); + break; + default: break; } } -static long hook_battery_psy_get_prop() -{ +static long hook_battery_psy_get_prop() { battery_psy_get_prop = 0; battery_psy_get_prop = (typeof(battery_psy_get_prop))kallsyms_lookup_name("battery_psy_get_prop"); pr_info("kernel function battery_psy_get_prop addr: %llx\n", battery_psy_get_prop); - if (!battery_psy_get_prop) - { + if (!battery_psy_get_prop) { return -1; } - hook_err_t err = hook_wrap3(battery_psy_get_prop, 0, battery_psy_get_prop_after, 0); - if (err) - { - pr_err("hook battery_psy_get_prop after error: %d\n", err); - return -2; - } - else - { - pr_info("hook battery_psy_get_prop after success\n"); - } + hook_func(battery_psy_get_prop, 3, NULL, battery_psy_get_prop_after, NULL); + return 0; } -void do_init_module_after(hook_fargs1_t *args, void *udata) -{ - struct module *mod = (typeof(mod))args->arg0; - if (unlikely(!memcmp(mod->name, MODULE_NAME, sizeof(MODULE_NAME)))) - { - demo_unhook(do_init_module); +void do_init_module_after(hook_fargs1_t* args, void* udata) { + struct module* mod = (typeof(mod))args->arg0; + if (unlikely(!memcmp(mod->name, MODULE_NAME, sizeof(MODULE_NAME)))) { + unhook_func(do_init_module); hook_battery_psy_get_prop(); } } -static long hook_do_init_module() -{ +static long hook_do_init_module() { do_init_module = 0; do_init_module = (typeof(do_init_module))kallsyms_lookup_name("do_init_module"); pr_info("kernel function do_init_module addr: %llx\n", do_init_module); - if (!do_init_module) - { + if (!do_init_module) { return -1; } hook_err_t err = hook_wrap1(do_init_module, 0, do_init_module_after, 0); - if (err) - { + if (err) { pr_err("hook do_init_module after error: %d\n", err); return -2; - } - else - { + } else { pr_info("hook do_init_module after success\n"); } return 0; } -static long inline_hook_init(const char *args, const char *event, void *__user reserved) -{ +static long inline_hook_init(const char* args, const char* event, void* __user reserved) { int rc; rc = hook_battery_psy_get_prop(); - if (rc < 0) - { + if (rc < 0) { rc = hook_do_init_module(); - if (rc < 0) - { + if (rc < 0) { return rc; } } return 0; } -static long inline_hook_exit(void *__user reserved) -{ - demo_unhook(do_init_module); - demo_unhook(battery_psy_get_prop); +static long inline_hook_exit(void* __user reserved) { + unhook_func(do_init_module); + unhook_func(battery_psy_get_prop); + return 0; } KPM_INIT(inline_hook_init); diff --git a/xperia_ii_battery_age/Makefile b/xperia_ii_battery_age/Makefile index 813892d..206f58c 100644 --- a/xperia_ii_battery_age/Makefile +++ b/xperia_ii_battery_age/Makefile @@ -1,14 +1,27 @@ -ifndef TARGET_COMPILE - $(error TARGET_COMPILE not set) -endif +XIIBA_VERSION := 1.1.2 ifndef KP_DIR KP_DIR = ../KernelPatch endif +OS_NAME = $(shell uname | tr A-Z a-z) +MACHINE = $(shell uname -m) +NDK_BIN_DIR := toolchains/llvm/prebuilt/$(OS_NAME)-$(MACHINE)/bin +ifdef ANDROID_NDK_LATEST_HOME + NDK_PATH ?= $(ANDROID_NDK_LATEST_HOME)/$(NDK_BIN_DIR) +else ifdef ANDROID_NDK + NDK_PATH ?= $(ANDROID_NDK)/$(NDK_BIN_DIR) +endif + +ifdef TARGET_COMPILE + CC := $(TARGET_COMPILE)gcc + LD := $(TARGET_COMPILE)ld +else ifdef NDK_PATH + CC := $(NDK_PATH)/aarch64-linux-android31-clang + LD := $(NDK_PATH)/ld.lld +endif -CC = $(TARGET_COMPILE)gcc -LD = $(TARGET_COMPILE)ld +CFLAGS = -Wall -O2 -fno-PIC -fno-asynchronous-unwind-tables -fno-stack-protector -fno-common -DXIIBA_VERSION=\"$(XIIBA_VERSION)$(XIIBA_VER)\" INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include @@ -16,15 +29,22 @@ INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) objs := xperia_ii_battery_age.o -all: xperia_ii_battery_age.kpm +all: xperia_ii_battery_age_$(XIIBA_VERSION).kpm + +debug: CFLAGS += -DDEBUG +debug: XIIBA_VER := _d +debug: xperia_ii_battery_age_$(XIIBA_VERSION)_debug.kpm + +xperia_ii_battery_age_$(XIIBA_VERSION).kpm: ${objs} + ${CC} -r -o $@ $^ -xperia_ii_battery_age.kpm: ${objs} +xperia_ii_battery_age_$(XIIBA_VERSION)_debug.kpm: ${objs} ${CC} -r -o $@ $^ %.o: %.c - ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -T../demo.lds -c -O2 -o $@ $< + ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -c -o $@ $< .PHONY: clean clean: rm -rf *.kpm - find . -name "*.o" | xargs rm -f \ No newline at end of file + find . -name "*.o" | xargs rm -f diff --git a/xperia_ii_battery_age/xiiba_utils.h b/xperia_ii_battery_age/xiiba_utils.h new file mode 100644 index 0000000..5ee7238 --- /dev/null +++ b/xperia_ii_battery_age/xiiba_utils.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2024 bmax121. All Rights Reserved. + * Copyright (C) 2024 lzghzr. All Rights Reserved. + */ +#ifndef __XIIBA_UTILS_H +#define __XIIBA_UTILS_H + +#include +#include +#include +#include +#include + +#define lookup_name(func) \ + func = 0; \ + func = (typeof(func))kallsyms_lookup_name(#func); \ + pr_info("kernel function %s addr: %llx\n", #func, func); \ + if (!func) \ + { \ + return -21; \ + } + +#define hook_func(func, argv, before, after, udata) \ + if (!func) \ + { \ + return -22; \ + } \ + hook_err_t hook_err_##func = hook_wrap(func, argv, before, after, udata); \ + if (hook_err_##func) \ + { \ + func = 0; \ + pr_err("hook %s error: %d\n", #func, hook_err_##func); \ + return -23; \ + } \ + else \ + { \ + pr_info("hook %s success\n", #func); \ + } + +#define unhook_func(func) \ + if (func && !is_bad_address(func)) \ + { \ + unhook(func); \ + func = 0; \ + } + +#endif /* __XIIBA_UTILS_H */ diff --git a/xperia_ii_battery_age/xperia_ii_battery_age.c b/xperia_ii_battery_age/xperia_ii_battery_age.c index 6a578e9..b644639 100644 --- a/xperia_ii_battery_age/xperia_ii_battery_age.c +++ b/xperia_ii_battery_age/xperia_ii_battery_age.c @@ -10,10 +10,10 @@ #include #include -#include "../demo.h" +#include "xiiba_utils.h" KPM_NAME("xperia_ii_battery_age"); -KPM_VERSION("1.1.1"); +KPM_VERSION(XIIBA_VERSION); KPM_LICENSE("GPL v2"); KPM_AUTHOR("lzghzr"); KPM_DESCRIPTION("set xperia ii battery aging level"); @@ -24,70 +24,62 @@ KPM_DESCRIPTION("set xperia ii battery aging level"); struct fg_dev; -int (*fg_sram_read)(struct fg_dev *fg, u16 address, u8 offset, u8 *val, int len, int flags) = 0; -int (*fg_sram_write)(struct fg_dev *fg, u16 address, u8 offset, u8 *val, int len, int flags) = 0; +static int(*fg_sram_read)(struct fg_dev* fg, u16 address, u8 offset, u8* val, int len, int flags) = 0; +static int(*fg_sram_write)(struct fg_dev* fg, u16 address, u8 offset, u8* val, int len, int flags) = 0; u8 aging = 0; -struct fg_dev *fg = 0; +struct fg_dev* fg = NULL; -static long inline_hook_control0(const char *args, char *__user out_msg, int outlen) -{ - aging = args ? *args - '0' : 0; - if (aging > 5) - { - return -1; - } - int rc = fg_sram_write(fg, SOMC_AGING_LEVEL_WORD, SOMC_AGING_LEVEL_OFFSET, &aging, 1, FG_IMA_DEFAULT); - char echo[64] = ""; - if (rc < 0) - { - sprintf(echo, "error, rc=%d\n", rc); - logke("fg_sram_write %s", echo); - if (out_msg) - { - compat_copy_to_user(out_msg, echo, sizeof(echo)); - return 1; - } +static long inline_hook_control0(const char* args, char* __user out_msg, int outlen) { + aging = args ? *args - '0' : 0; + if (aging > 5) + return -1; + + int rc = fg_sram_write(fg, SOMC_AGING_LEVEL_WORD, SOMC_AGING_LEVEL_OFFSET, &aging, 1, FG_IMA_DEFAULT); + char echo[64] = ""; + if (rc < 0) { + sprintf(echo, "error, rc=%d\n", rc); + logke("fg_sram_write %s", echo); + if (out_msg) { + compat_copy_to_user(out_msg, echo, sizeof(echo)); + return 1; } - else - { - sprintf(echo, "success, set batt_aging_level to %d\n", aging); - logki("fg_sram_write %s", echo); - if (out_msg) - { - compat_copy_to_user(out_msg, echo, sizeof(echo)); - return 0; - } + } else { + sprintf(echo, "success, set batt_aging_level to %d\n", aging); + logki("fg_sram_write %s", echo); + if (out_msg) { + compat_copy_to_user(out_msg, echo, sizeof(echo)); + return 0; } + } + return 0; } -void before_read(hook_fargs6_t *args, void *udata) -{ - demo_unhook(fg_sram_read); - fg = (struct fg_dev *)args->arg0; - // u8 *arg3 = (u8 *)args->arg3; - // logkd("before read fg: %llu, address: %u, offset: %u, val: %u, len: %d, flags: %d\n", args->arg0, (u16)args->arg1, - // (u8)args->arg2, (u8)*arg3, (int)args->arg4, (int)args->arg5); - inline_hook_control0(&aging, NULL, NULL); +void before_read(hook_fargs6_t* args, void* udata) { + unhook_func(fg_sram_read); + fg = (struct fg_dev*)args->arg0; + // u8 *arg3 = (u8 *)args->arg3; + // logkd("before read fg: %llu, address: %u, offset: %u, val: %u, len: %d, flags: %d\n", args->arg0, (u16)args->arg1, + // (u8)args->arg2, (u8)*arg3, (int)args->arg4, (int)args->arg5); + char age[] = "0"; + age[0] = aging + '0'; + inline_hook_control0(age, NULL, NULL); } -static long inline_hook_init(const char *args, const char *event, void *__user reserved) -{ - aging = args ? *args - '0' : 0; - if (aging > 5) - { - return -1; - } +static long inline_hook_init(const char* args, const char* event, void* __user reserved) { + aging = args ? *args - '0' : 0; + if (aging > 5) + return -1; - lookup_name(fg_sram_write); - lookup_name(fg_sram_read); - hook_func(fg_sram_read, 6, before_read, 0, 0); - return 0; + lookup_name(fg_sram_write); + lookup_name(fg_sram_read); + hook_func(fg_sram_read, 6, before_read, 0, 0); + return 0; } -static long inline_hook_exit(void *__user reserved) -{ - unhook_func(fg_sram_read); +static long inline_hook_exit(void* __user reserved) { + unhook_func(fg_sram_read); + return 0; } KPM_INIT(inline_hook_init);