Skip to content

Commit

Permalink
ci
Browse files Browse the repository at this point in the history
  • Loading branch information
lzghzr committed May 19, 2024
1 parent 8c186e6 commit 59d6932
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 123 deletions.
38 changes: 29 additions & 9 deletions qti_battery_charger/Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
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

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
find . -name "*.o" | xargs rm -f
48 changes: 48 additions & 0 deletions qti_battery_charger/qbc_utils.h
Original file line number Diff line number Diff line change
@@ -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 <hook.h>
#include <ksyms.h>
#include <linux/cred.h>
#include <linux/sched.h>
#include <uapi/asm-generic/errno.h>

#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 */
81 changes: 30 additions & 51 deletions qti_battery_charger/qti_battery_charger.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@
#include <linux/printk.h>
#include <linux/string.h>

#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;
Expand All @@ -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);
Expand Down
38 changes: 29 additions & 9 deletions xperia_ii_battery_age/Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
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

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
find . -name "*.o" | xargs rm -f
48 changes: 48 additions & 0 deletions xperia_ii_battery_age/xiiba_utils.h
Original file line number Diff line number Diff line change
@@ -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 <hook.h>
#include <ksyms.h>
#include <linux/cred.h>
#include <linux/sched.h>
#include <uapi/asm-generic/errno.h>

#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 */
Loading

0 comments on commit 59d6932

Please sign in to comment.