Skip to content

Commit

Permalink
Support dumping kernel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m3vilurr committed Nov 20, 2016
1 parent d49456f commit 3791894
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Makefile
Expand Up @@ -3,18 +3,20 @@ TARGET = mDump
PSVITAIP = 192.168.1.115

MAIN_OBJS = main.o graphics.o font.o
PLUGIN_OBJS = kernel.o
HEADERS = $(wildcard *.h)

LIBS = -lSceDisplay_stub -lSceGxm_stub -lSceCtrl_stub -lSceSysmodule_stub
PLUGIN_LIBS = -Llibtaihen_stub.a -lSceSysclibForDriver_stub -lSceModulemgrForKernel_stub -lSceIofilemgrForDriver_stub

PREFIX = arm-vita-eabi
CC = $(PREFIX)-gcc
CFLAGS = -Wl,-q -Wall -O3
ASFLAGS = $(CFLAGS)

all: $(TARGET).vpk
all: mDump.vpk kDump.skprx

%.vpk: eboot.bin
mDump.vpk: eboot.bin
vita-mksfoex -s TITLE_ID=$(TITLE_ID) "$(TARGET)" param.sfo
vita-pack-vpk -s param.sfo -b eboot.bin $@

Expand All @@ -27,8 +29,17 @@ mDump.velf: mDump.elf
mDump.elf: $(MAIN_OBJS)
$(CC) $(CFLAGS) $^ $(LIBS) -o $@

kDump.skprx: kDump.velf
vita-make-fself $< $@

kDump.velf: kDump.elf
vita-elf-create -e exports.yml $< $@

kDump.elf: $(PLUGIN_OBJS)
$(CC) $(CFLAGS) $^ $(PLUGIN_LIBS) -o $@ -nostdlib

clean:
@rm -rf *.velf *.elf *.vpk $(MAIN_OBJS) param.sfo eboot.bin
@rm -rf *.velf *.elf *.vpk *.skprx $(MAIN_OBJS) $(PLUGIN_OBJS) param.sfo eboot.bin

send: eboot.bin
curl -T eboot.bin ftp://$(PSVITAIP):1337/ux0:/app/$(TITLE_ID)/
Expand Down
8 changes: 8 additions & 0 deletions exports.yml
@@ -0,0 +1,8 @@
vita_dump:
attributes: 0
version:
major: 1
minor: 1
main:
start: module_start
stop: module_stop
116 changes: 116 additions & 0 deletions kernel.c
@@ -0,0 +1,116 @@
#include <stdio.h>
#include <string.h>
#include <taihen.h>
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/threadmgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <psp2kern/io/fcntl.h>

#define DUMP_PATH "ux0:dump/"
#define LOG_FILE DUMP_PATH "kplugin_log.txt"

static void log_reset();
static void log_write(const char *buffer, size_t length);

#define LOG(...) \
do { \
char buffer[256]; \
snprintf(buffer, sizeof(buffer), ##__VA_ARGS__); \
log_write(buffer, strlen(buffer)); \
} while (0)

static void dump_region(const char *filename, void *addr, unsigned int size)
{
SceUID fd;

if (!(fd = sceIoOpenForDriver(filename, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6))) {
LOG("Error opening %s\n", filename);
return;
}

sceIoWriteForDriver(fd, addr, size);

sceIoCloseForDriver(fd);
}

void _start() __attribute__ ((weak, alias ("module_start")));

#define MOD_LIST_SIZE 0x80

int module_start(SceSize argc, const void *args)
{
int i, j;
int ret;
size_t num;
SceKernelModuleInfo modinfo;
SceUID modlist[MOD_LIST_SIZE];

log_reset();

LOG("kplugin by xerpi\n");

memset(modlist, 0, sizeof(modlist));

num = MOD_LIST_SIZE;
ret = sceKernelGetModuleListForKernel(KERNEL_PID, 0x80000001, 1, modlist, &num);
if (ret < 0)
LOG("Error getting the module list\n");

LOG("Found %d modules.\n", num);

for (i = 0; i < num; i++) {
memset(&modinfo, 0, sizeof(modinfo));

ret = sceKernelGetModuleInfoForKernel(KERNEL_PID, modlist[i], &modinfo);
if (ret < 0) {
LOG("Error getting the module info for module: %d\n", i);
continue;
}

LOG("Module %d name: %s\n", i, modinfo.module_name);

for (j = 0; j < 4; j++) {
char path[128];
SceKernelSegmentInfo *seginfo = &modinfo.segments[j];

if (seginfo->size != sizeof(*seginfo))
continue;

snprintf(path, sizeof(path), DUMP_PATH "%s_0x%08X_seg%d.bin",
modinfo.module_name, (uintptr_t)seginfo->vaddr, j);

dump_region(path, seginfo->vaddr, seginfo->memsz);
}
}

return SCE_KERNEL_START_SUCCESS;
}

int module_stop(SceSize argc, const void *args)
{
return SCE_KERNEL_STOP_SUCCESS;
}

void log_reset()
{
SceUID fd = sceIoOpenForDriver(LOG_FILE,
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6);
if (fd < 0)
return;

sceIoCloseForDriver(fd);
}

void log_write(const char *buffer, size_t length)
{
extern int sceIoMkdirForDriver(const char *, int);
sceIoMkdirForDriver(DUMP_PATH, 6);

SceUID fd = sceIoOpenForDriver(LOG_FILE,
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_APPEND, 6);
if (fd < 0)
return;

sceIoWriteForDriver(fd, buffer, length);
sceIoCloseForDriver(fd);
}

0 comments on commit 3791894

Please sign in to comment.