-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtakeover.c
211 lines (166 loc) · 5.5 KB
/
takeover.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctr/types.h>
#include <ctr/srv.h>
#include <ctr/svc.h>
#include <ctr/FS.h>
#include <ctr/GSP.h>
#include <ctr/GX.h>
#include "sys.h"
#include "3dsx.h"
#include "../../build/constants.h"
#include "../../app_targets/app_targets.h"
// decompression code stolen from ctrtool
u32 getle32(const u8* p)
{
return (p[0]<<0) | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
}
u32 lzss_get_decompressed_size(u8* compressed, u32 compressedsize)
{
u8* footer = compressed + compressedsize - 8;
u32 originalbottom = getle32(footer+4);
return originalbottom + compressedsize;
}
int lzss_decompress(u8* compressed, u32 compressedsize, u8* decompressed, u32 decompressedsize)
{
u8* footer = compressed + compressedsize - 8;
u32 buffertopandbottom = getle32(footer+0);
//u32 originalbottom = getle32(footer+4);
u32 i, j;
u32 out = decompressedsize;
u32 index = compressedsize - ((buffertopandbottom>>24)&0xFF);
u32 segmentoffset;
u32 segmentsize;
u8 control;
u32 stopindex = compressedsize - (buffertopandbottom&0xFFFFFF);
memset(decompressed, 0, decompressedsize);
memcpy(decompressed, compressed, compressedsize);
while(index > stopindex)
{
control = compressed[--index];
for(i=0; i<8; i++)
{
if (index <= stopindex)
break;
if (index <= 0)
break;
if (out <= 0)
break;
if (control & 0x80)
{
if (index < 2)
{
// fprintf(stderr, "Error, compression out of bounds\n");
goto clean;
}
index -= 2;
segmentoffset = compressed[index] | (compressed[index+1]<<8);
segmentsize = ((segmentoffset >> 12)&15)+3;
segmentoffset &= 0x0FFF;
segmentoffset += 2;
if (out < segmentsize)
{
// fprintf(stderr, "Error, compression out of bounds\n");
goto clean;
}
for(j=0; j<segmentsize; j++)
{
u8 data;
if (out+segmentoffset >= decompressedsize)
{
// fprintf(stderr, "Error, compression out of bounds\n");
goto clean;
}
data = decompressed[out+segmentoffset];
decompressed[--out] = data;
}
}
else
{
if (out < 1)
{
// fprintf(stderr, "Error, compression out of bounds\n");
goto clean;
}
decompressed[--out] = compressed[--index];
}
control <<= 1;
}
}
return 0;
clean:
return -1;
}
Result loadTitleCode(Handle fsuserHandle, u8 mediatype, u32 tid_low, u32 tid_high, u8* out, u32* out_size, u32* tmp)
{
Handle fileHandle;
u32 archivePath[] = {tid_low, tid_high, mediatype, 0x00000000};
static const u32 filePath[] = {0x00000000, 0x00000000, 0x00000002, 0x646F632E, 0x00000065};
Result ret = FSUSER_OpenFileDirectly(fsuserHandle, &fileHandle, (FS_archive){0x2345678a, (FS_path){PATH_BINARY, 0x10, (u8*)archivePath}}, (FS_path){PATH_BINARY, 0x14, (u8*)filePath}, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
if(ret)return ret;
u64 size64;
ret = FSFILE_GetSize(fileHandle, &size64);
if(ret)return ret;
u32 size = (u32)size64;
u32* compressed = tmp;
if(!compressed)return -1;
u32 bytesRead;
ret = FSFILE_Read(fileHandle, &bytesRead, 0x0, compressed, size);
if(ret)
{
return ret;
}
u32 decompressed_size = lzss_get_decompressed_size((u8*)compressed, size);
// out is already allocated
ret = lzss_decompress((u8*)compressed, size, out, decompressed_size);
FSFILE_Close(fileHandle);
if(out_size) *out_size = decompressed_size;
return ret;
}
void getProcessMap(Handle fsuserHandle, u8 mediatype, u32 tid_low, u32 tid_high, memorymap_t* out, u32* tmp)
{
if(!out)return;
u32 size = 0;
const u32 comp_offset = 0x01000000;
Result ret = loadTitleCode(fsuserHandle, mediatype, tid_low, tid_high, (u8*)tmp, &size, &tmp[comp_offset / 4]);
if(ret)*(u32*)ret = 0xdeadf00d;
int i;
// first, fill out basic info
out->header.processHookTidLow = tid_low;
out->header.processHookTidHigh = tid_high;
out->header.mediatype = mediatype;
out->header.num = 0;
// uber conservative estimates
// TODO : heuristic based on quick analysis for text size
out->header.text_end = 0x00160000;
out->header.data_address = 0x00100000 + (size & ~0xfff) - 0x8000;
// out->header.data_size = 0x8000;
out->header.data_size = 0x0;
out->header.processAppCodeAddress = 0x00105000;
for(i=0; i<0x10; i++)out->header.capabilities[i] = false;
// find hook target
// assume second instruction is a jump. if it's not we're fucked anyway so it's ok to ignore that possibility i guess.
out->header.processHookAddress = (((tmp[1] & 0x00ffffff) << 2) + 0x0010000C) & ~0x1f;
if(out->header.processHookAddress < 0x00105000 || out->header.processHookAddress > 0x0010A000) out->header.processAppCodeAddress = 0x00105000;
else out->header.processAppCodeAddress = 0x0010B000;
// figure out the physical memory map
u32 current_size = size;
u32 current_offset = 0x00000000;
for(i=0; i<3; i++)
// for(i=0; i<2; i++)
{
const u32 mask = 0x000fffff >> (4*i);
u32 section_size = current_size & ~mask;
if(section_size)
{
if(!out->header.num) out->header.processLinearOffset = section_size;
current_offset += section_size;
out->map[out->header.num++] = (memorymap_entry_t){0x00100000 + current_offset - section_size - 0x00008000, - (current_offset - out->header.processLinearOffset), section_size};
current_size -= section_size;
}
}
out->map[0].src += 0x00008000;
out->map[0].dst += 0x00008000;
out->map[0].size -= 0x00008000;
}