From 257a39cf8dcde82b426a6a78cd4576b4019cbc83 Mon Sep 17 00:00:00 2001 From: sekaiacg Date: Fri, 12 Jan 2024 20:26:44 +0800 Subject: [PATCH] kptools: Add targetOS option to patch target system default: Android Signed-off-by: sekaiacg --- tools/Makefile | 6 +----- tools/kptools.c | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/tools/Makefile b/tools/Makefile index e9454706..b0629743 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -2,10 +2,6 @@ CFLAGS = -std=c11 -Wall -Wextra -Wno-unused -Wno-unused-parameter # CFLAGS += -g -ifdef ANDROID - CFLAGS += -DANDROID -endif - objs := image.o kallsym.o kptools.o order.o .PHONY: all @@ -22,4 +18,4 @@ kptools: ${objs} clean: rm -rf preset.h rm -rf kptools - find . -name "*.o" | xargs rm -f \ No newline at end of file + find . -name "*.o" | xargs rm -f diff --git a/tools/kptools.c b/tools/kptools.c index 18bc1db4..d474dfdc 100644 --- a/tools/kptools.c +++ b/tools/kptools.c @@ -53,6 +53,7 @@ static char image[FILENAME_MAX] = { '\0' }; static char out[FILENAME_MAX] = { '\0' }; static char kpimg[FILENAME_MAX] = { '\0' }; static char superkey[SUPER_KEY_LEN] = { '\0' }; +static char configReserved[256] = { '\0' }; static kernel_info_t kinfo; static kallsym_t kallsym; @@ -67,6 +68,7 @@ void print_usage() " Print this message.\n" "\n" " -p, --patch <--kpimg kpimg> <--skey super_key> [--out image_patched]\n" + " -t, --targetOS , name: Android or Linux, default: Android\n" " Patch kernel_image with kpimg.\n" " If --out is not specified, default ${kernel_image}__patched will be used.\n" " super_key: Authentication key for supercall system call.\n" @@ -248,8 +250,22 @@ void select_map_area(kallsym_t *kallsym, char *image_buf, int32_t *map_start, in *max_size = 0x800; } +void set_config_reserved(const char *osName) +{ + if (strcmp(osName, "Android") == 0) { + strncpy(configReserved, "/data/adb/ap/init.ini", sizeof(configReserved) - 1); + } else if (strcmp(osName, "Linux") == 0) { + strncpy(configReserved, "/etc/kp/init.ini", sizeof(configReserved) - 1); + } +} + int patch_image() { + if (!strlen(configReserved)) { + set_config_reserved("Android"); + } + fprintf(stdout, "[+] kptools patch config reserved is %s\n", configReserved); + if (!strlen(out)) { strcpy(out, image); strcat(out, "_patched"); @@ -381,11 +397,7 @@ int patch_image() } patch_config_t *config = &preset->patch_config; -#ifdef ANDROID - strncpy(config->config_reserved, "/data/adb/ap/init.ini", sizeof(config->config_reserved) - 1); -#else - strncpy(config->config_reserved, "/etc/kp/init.ini", sizeof(config->config_reserved) - 1); -#endif + strncpy(config->config_reserved, configReserved, sizeof(config->config_reserved) - 1); // todo: // kernel_resize(&kinfo, out_buf, align_kernel_size + align_image_len); @@ -414,8 +426,9 @@ int main(int argc, char *argv[]) struct option longopts[] = { { "version", no_argument, NULL, 'v' }, { "help", no_argument, NULL, 'h' }, { "patch", required_argument, NULL, 'p' }, { "skey", required_argument, NULL, 's' }, { "out", required_argument, NULL, 'o' }, { "kpimg", required_argument, NULL, 'k' }, - { "dump", required_argument, NULL, 'd' }, { 0, 0, 0, 0 } }; - char *optstr = "vhp:d:o:"; + { "dump", required_argument, NULL, 'd' }, { "targetOS", required_argument, NULL, 't' }, + { 0, 0, 0, 0 } }; + char *optstr = "vhp:d:o:t:"; int cmd = '\0'; int opt = -1; @@ -442,6 +455,11 @@ int main(int argc, char *argv[]) case 's': strncpy(superkey, optarg, SUPER_KEY_LEN); break; + case 't': + if (optarg && strlen(optarg) > 0) { + set_config_reserved(optarg); + } + break; default: break; }