/
kernel.c
222 lines (171 loc) · 4.83 KB
/
kernel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#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>
#include "elf.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 = ksceIoOpen(filename, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6))) {
LOG("Error opening %s\n", filename);
return;
}
ksceIoWrite(fd, addr, size);
ksceIoClose(fd);
}
void _start() __attribute__ ((weak, alias ("module_start")));
#define MOD_LIST_SIZE 0x80
void doDump(const SceKernelModuleInfo *info) {
char path[128] = {0};
int i;
SceUID fd;
Elf32_Ehdr ehdr;
Elf32_Phdr phdr;
Elf32_Off offset;
snprintf(path, sizeof(path), DUMP_PATH "%s.elf",
info->module_name);
LOG("Dumping %s\n", path);
fd = ksceIoOpen(path, SCE_O_CREAT | SCE_O_WRONLY | SCE_O_TRUNC, 6);
if (fd < 0) {
LOG("Failed to open the file for writing.\n");
return;
}
ehdr.e_ident[EI_MAG0] = ELFMAG0;
ehdr.e_ident[EI_MAG1] = ELFMAG1;
ehdr.e_ident[EI_MAG2] = ELFMAG2;
ehdr.e_ident[EI_MAG3] = ELFMAG3;
ehdr.e_ident[EI_CLASS] = ELFCLASS32;
ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
ehdr.e_ident[EI_VERSION] = EV_CURRENT;
ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM_AEABI;
ehdr.e_ident[EI_ABIVERSION] = 0;
memset(ehdr.e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
ehdr.e_type = ET_CORE;
ehdr.e_machine = EM_ARM;
ehdr.e_version = EV_CURRENT;
ehdr.e_entry = (Elf32_Addr)info->module_start;
ehdr.e_phoff = sizeof(ehdr);
ehdr.e_flags = EF_ARM_HASENTRY
| EF_ARM_ABI_FLOAT_HARD
| EF_ARM_EABI_VER5;
ehdr.e_ehsize = sizeof(ehdr);
ehdr.e_phentsize = sizeof(Elf32_Phdr);
ehdr.e_shentsize = sizeof(Elf32_Shdr);
ehdr.e_shnum = 0;
ehdr.e_shstrndx = 0;
ehdr.e_shoff = 0;
ehdr.e_phnum = 0;
for (i = 0; i < 4; ++i) {
if (info->segments[i].vaddr == NULL)
continue;
++ehdr.e_phnum;
}
ksceIoWrite(fd, &ehdr, sizeof(ehdr));
offset = sizeof(ehdr) + ehdr.e_phnum * sizeof(phdr);
phdr.p_type = PT_LOAD;
phdr.p_paddr = 0;
phdr.p_align = 1;
for (i = 0; i < 4; ++i) {
if (info->segments[i].vaddr == NULL)
continue;
phdr.p_flags = info->segments[i].perms;
phdr.p_offset = offset;
phdr.p_vaddr = (Elf32_Addr)info->segments[i].vaddr;
phdr.p_memsz = info->segments[i].memsz;
phdr.p_filesz = phdr.p_memsz;
ksceIoWrite(fd, &phdr, sizeof(phdr));
offset += phdr.p_filesz;
}
for (i = 0; i < 4; ++i) {
if (info->segments[i].vaddr == NULL) {
LOG("Segment #%x is empty, skipping\n", i);
continue;
}
ksceIoWrite(fd, info->segments[i].vaddr, info->segments[i].memsz);
}
ksceIoClose(fd);
snprintf(path, sizeof(path), DUMP_PATH "%s_info.bin",
info->module_name);
LOG("Dumping %s\n", path);
fd = ksceIoOpen(path, SCE_O_CREAT | SCE_O_WRONLY | SCE_O_TRUNC, 6);
if (fd < 0) {
LOG("Failed to open the file for writing.\n");
return;
}
ksceIoWrite(fd, info, sizeof(*info));
ksceIoClose(fd);
}
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 = ksceKernelGetModuleList(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 = ksceKernelGetModuleInfo(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;
if (seginfo->vaddr == NULL)
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);
}
doDump(&modinfo);
}
return SCE_KERNEL_START_SUCCESS;
}
int module_stop(SceSize argc, const void *args)
{
return SCE_KERNEL_STOP_SUCCESS;
}
void log_reset()
{
SceUID fd = ksceIoOpen(LOG_FILE,
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 6);
if (fd < 0)
return;
ksceIoClose(fd);
}
void log_write(const char *buffer, size_t length)
{
extern int ksceIoMkdir(const char *, int);
ksceIoMkdir(DUMP_PATH, 6);
SceUID fd = ksceIoOpen(LOG_FILE,
SCE_O_WRONLY | SCE_O_CREAT | SCE_O_APPEND, 6);
if (fd < 0)
return;
ksceIoWrite(fd, buffer, length);
ksceIoClose(fd);
}