-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpayload.c
218 lines (187 loc) · 6.84 KB
/
payload.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
/*
* Copyright (C) 2010 drizzt
*
* Authors:
* drizzt <[email protected]>
* flukes1
* kmeaw
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "payload.h"
#include "hvcall.h"
#include "mm.h"
#ifdef WITH_PL3
extern uint32_t _binary_payload_pl3_patch_txt_start;
extern uint32_t _binary_payload_pl3_patch_txt_size;
extern uint32_t _binary_payload_pl3_payload_bin_start;
extern uint32_t _binary_payload_pl3_payload_bin_size;
#else
extern uint32_t _binary_payload_syscall36_patch_txt_start;
extern uint32_t _binary_payload_syscall36_patch_txt_size;
extern uint32_t _binary_payload_syscall36_payload_bin_start;
extern uint32_t _binary_payload_syscall36_payload_bin_size;
#endif
uint64_t mmap_lpar_addr;
static int poke_syscall = 7;
uint64_t peekq(uint64_t addr)
{
system_call_1(6, addr);
return_to_user_prog(uint64_t);
}
void pokeq(uint64_t addr, uint64_t val)
{
system_call_2(poke_syscall, addr, val);
}
void pokeq32(uint64_t addr, uint32_t val)
{
uint32_t next = peekq(addr) & 0xffffffffUL;
pokeq(addr, (uint64_t) val << 32 | next);
}
static void lv1_poke(uint64_t addr, uint64_t val)
{
system_call_2(7, HV_BASE + addr, val);
}
static inline void _poke(uint64_t addr, uint64_t val)
{
pokeq(0x8000000000000000ULL + addr, val);
}
static inline void _poke32(uint64_t addr, uint32_t val)
{
pokeq32(0x8000000000000000ULL + addr, val);
}
bool is_payload_loaded(void)
{
#ifdef WITH_PL3
uint64_t *tmp = (uint64_t *) (uint64_t) & _binary_payload_pl3_payload_bin_start;
return peekq(0x800000000000ef48ULL) == *tmp;
#else
uint64_t *tmp = (uint64_t *) (uint64_t) & _binary_payload_syscall36_payload_bin_start;
return peekq(0x80000000002be4a0ULL) == *tmp;
#endif
}
void load_payload(void)
{
char *ptr, *ptr2;
unsigned long long addr, value;
int patches = 0;
#ifdef USE_MEMCPY_SYSCALL
/* This does not work on some PS3s */
pokeq(NEW_POKE_SYSCALL_ADDR, 0x4800000428250000ULL);
pokeq(NEW_POKE_SYSCALL_ADDR + 8, 0x4182001438a5ffffULL);
pokeq(NEW_POKE_SYSCALL_ADDR + 16, 0x7cc428ae7cc329aeULL);
pokeq(NEW_POKE_SYSCALL_ADDR + 24, 0x4bffffec4e800020ULL);
#ifdef WITH_PL3
system_call_3(NEW_POKE_SYSCALL, 0x800000000000ef48ULL, (unsigned long long) &&_binary_payload_pl3_payload_bin_start,
(uint64_t) & _binary_payload_pl3_payload_bin_size);
#else
system_call_3(new_poke_syscall, 0x80000000002be4a0ULL,
(unsigned long long) &_binary_payload_syscall36_payload_bin_start,
(uint64_t) & _binary_payload_syscall36_payload_bin_size);
#endif
/* restore syscall */
remove_new_poke();
pokeq(NEW_POKE_SYSCALL_ADDR + 16, 0xebc2fe287c7f1b78);
pokeq(NEW_POKE_SYSCALL_ADDR + 24, 0x3860032dfba100e8);
#else
/* WARNING!! It supports only payload with a size multiple of 4 */
uint32_t i;
#ifdef WITH_PL3
uint64_t *pl64 = (uint64_t *) (uint64_t) & _binary_payload_pl3_payload_bin_start;
for (i = 0; i < (uint64_t) & _binary_payload_pl3_payload_bin_size / sizeof(uint64_t); i++) {
pokeq(0x800000000000ef48ULL + i * sizeof(uint64_t), *pl64++);
}
if ((uint64_t) & _binary_payload_pl3_payload_bin_size % sizeof(uint64_t)) {
pokeq32(0x800000000000ef48ULL + i * sizeof(uint64_t), (uint32_t) * pl64);
}
#else
uint64_t *pl64 = (uint64_t *) (uint64_t) & _binary_payload_syscall36_payload_bin_start;
for (i = 0; i < (uint64_t) & _binary_payload_syscall36_payload_bin_size / sizeof(uint64_t); i++) {
pokeq(0x80000000002be4a0ULL + i * sizeof(uint64_t), *pl64++);
}
if ((uint64_t) & _binary_payload_syscall36_payload_bin_size % sizeof(uint64_t)) {
pokeq(0x80000000002be4a0ULL + i * sizeof(uint64_t), (uint32_t) * pl64);
}
#endif
#endif
#ifdef WITH_PL3
char *tmp = strtok((char *) &_binary_payload_pl3_patch_txt_start, "\n");
#else
char *tmp = strtok((char *) &_binary_payload_syscall36_patch_txt_start, "\n");
#endif
do {
ptr = strchr(tmp, '#');
if (ptr)
*ptr = 0;
ptr = tmp;
while (*ptr == ' ' || *ptr == '\t')
ptr++;
if (!strchr("0123456789abcdefABCDEF", *ptr))
continue;
addr = strtoull(ptr, &ptr, 16);
if (*ptr != ':')
continue;
else
ptr++;
while (*ptr == ' ' || *ptr == '\t')
ptr++;
if (!strchr("0123456789abcdefABCDEF", *ptr))
continue;
ptr2 = ptr;
value = strtoull(ptr, &ptr, 16);
patches++;
if (ptr - ptr2 == 8) {
_poke32(addr, value);
} else if (ptr - ptr2 == 16) {
_poke(addr, value);
} else
patches--;
}
while ((tmp = strtok(NULL, "\n")));
}
int map_lv1(void)
{
int result = lv1_undocumented_function_114(0, 0xC, HV_SIZE, &mmap_lpar_addr);
if (result != 0) {
return 0;
}
result = mm_map_lpar_memory_region(mmap_lpar_addr, HV_BASE, HV_SIZE, 0xC, 0);
if (result) {
return 0;
}
return 1;
}
void unmap_lv1(void)
{
if (mmap_lpar_addr != 0)
lv1_undocumented_function_115(mmap_lpar_addr);
}
void patch_lv2_protection(void)
{
// changes protected area of lv2 to first byte only
lv1_poke(0x363a78, 0x0000000000000001ULL);
lv1_poke(0x363a80, 0xe0d251b556c59f05ULL);
lv1_poke(0x363a88, 0xc232fcad552c80d7ULL);
lv1_poke(0x363a90, 0x65140cd200000000ULL);
}
void install_new_poke(void)
{
// install poke with icbi instruction
pokeq(NEW_POKE_SYSCALL_ADDR, 0xF88300007C001FACULL);
pokeq(NEW_POKE_SYSCALL_ADDR + 8, 0x4C00012C4E800020ULL);
poke_syscall = NEW_POKE_SYSCALL;
}
void remove_new_poke(void)
{
poke_syscall = 7;
pokeq(NEW_POKE_SYSCALL_ADDR, 0xF821FF017C0802A6ULL);
pokeq(NEW_POKE_SYSCALL_ADDR + 8, 0xFBC100F0FBE100F8ULL);
}
/* vim: set ts=4 sw=4 sts=4 tw=120 */