Skip to content

Commit

Permalink
kptools: Add targetOS option to patch target system (#36)
Browse files Browse the repository at this point in the history
default: Android

Signed-off-by: sekaiacg <[email protected]>
  • Loading branch information
sekaiacg authored Jan 14, 2024
1 parent 088be2f commit ee09601
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
6 changes: 1 addition & 5 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,4 +18,4 @@ kptools: ${objs}
clean:
rm -rf preset.h
rm -rf kptools
find . -name "*.o" | xargs rm -f
find . -name "*.o" | xargs rm -f
32 changes: 25 additions & 7 deletions tools/kptools.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -67,6 +68,7 @@ void print_usage()
" Print this message.\n"
"\n"
" -p, --patch <kernel_image> <--kpimg kpimg> <--skey super_key> [--out image_patched]\n"
" -t, --targetOS <name>, 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"
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit ee09601

Please sign in to comment.