-
Notifications
You must be signed in to change notification settings - Fork 20
/
dump.c
201 lines (177 loc) · 4.31 KB
/
dump.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
#include "dump.h"
#include "ui/menu.h"
#include <kernel.h>
#include <libcdvd.h>
#include <stdlib.h>
#include <stdio.h>
#include "modelname.h"
#include "sysman/sysinfo.h" // t_SysmanHardwareInfo
#include "sysman_rpc.h"
static u32 dump_rom0_func();
static u32 dump_rom1_func();
static u32 dump_rom2_func();
static u32 dump_nvm_func();
static u32 dump_mec_func();
static u8* dump_shared_buffer;
extern t_SysmanHardwareInfo g_hardwareInfo;
static t_dump dump_jobs[6];
static char dump_filename[MODEL_NAME_MAX_LEN];
static u32 dump_file_usb = 0;
extern int errno;
static u32 dump_file(t_dump job)
{
char path[256];
sprintf(path, "%s%s.%s", dump_file_usb ? "mass:" : "host:", dump_filename, job.dump_fext);
FILE *f = fopen(path, "wb");
if (!f)
{
menu_status("Failed to open %d file %s\n", f, path);
return 1;
}
u32 written_size = job.dump_size;
while(written_size > 0)
{
u32 write_size = fwrite(dump_shared_buffer, 1, written_size, f);
if (write_size == 0)
{
menu_status("Failed to write to file %s\n", path);
fclose(f);
return 2;
}
written_size -= write_size;
}
FlushCache(0);
fclose(f);
return 0;
}
void dump_init(u32 use_usb)
{
dump_shared_buffer = (u8*) aligned_alloc(64, 0x400000);
dump_file_usb = use_usb;
// ROM0
{
dump_jobs[0].dump_name = "ROM0";
dump_jobs[0].dump_fext = "rom0";
dump_jobs[0].dump_func = dump_rom0_func;
dump_jobs[0].dump_size = 0x400000;
dump_jobs[0].enabled = g_hardwareInfo.ROMs[0].IsExists;
}
// ROM1
{
dump_jobs[1].dump_name = "ROM1";
dump_jobs[1].dump_fext = "rom1";
dump_jobs[1].dump_func = dump_rom1_func;
dump_jobs[1].dump_size = 0x400000;
dump_jobs[1].enabled = g_hardwareInfo.ROMs[1].IsExists;
}
// ROM2
{
dump_jobs[2].dump_name = "ROM2";
dump_jobs[2].dump_fext = "rom2";
dump_jobs[2].dump_func = dump_rom2_func;
dump_jobs[2].dump_size = 0x80000;
dump_jobs[2].enabled = g_hardwareInfo.ROMs[1].IsExists;
}
// NVM
{
dump_jobs[4].dump_name = "NVM";
dump_jobs[4].dump_fext = "nvm";
dump_jobs[4].dump_func = dump_nvm_func;
dump_jobs[4].dump_size = 1024;
dump_jobs[4].enabled = g_hardwareInfo.DVD_ROM.IsExists;
}
// MEC
{
dump_jobs[5].dump_name = "MEC";
dump_jobs[5].dump_fext = "mec";
dump_jobs[5].dump_func = dump_mec_func;
dump_jobs[5].dump_size = 4;
dump_jobs[5].enabled = g_hardwareInfo.DVD_ROM.IsExists;
}
if(modelname_read(dump_filename) != 0)
menu_status("Warning: Unable to get the model name\nFile name will be set to 'Unknown'");
}
void dump_exec()
{
for (u32 i = 0; i < 6; i++)
{
if (dump_jobs[i].enabled)
{
menu_status("Dumping %s...", dump_jobs[i].dump_name);
u32 ret = dump_jobs[i].dump_func();
if (!ret)
{
FlushCache(0);
menu_status("Writing to file...");
dump_file(dump_jobs[i]);
menu_status("Finished\n");
}
else
{
menu_status("Dump failed, result %d\n", ret);
}
}
}
}
void dump_cleanup()
{
free(dump_shared_buffer);
}
// Used for ROMx dumps
// Pretty much just a SysmanReadMemory wrapper
static void common_dump_func(u32 start, u32 size)
{
u32 area_ptr = start;
u32 buf_ptr = (u32)dump_shared_buffer;
for (int block = 0; block < (size / MEM_IO_BLOCK_SIZE); block++)
{
SysmanSync(0);
while (SysmanReadMemory((void *)(area_ptr), (void *)(buf_ptr), MEM_IO_BLOCK_SIZE, 1) != 0)
nopdelay();
area_ptr += MEM_IO_BLOCK_SIZE;
buf_ptr += MEM_IO_BLOCK_SIZE;
}
if ((size % MEM_IO_BLOCK_SIZE) != 0)
{
SysmanSync(0);
while (SysmanReadMemory((void *)(area_ptr), (void *)(buf_ptr), (size % MEM_IO_BLOCK_SIZE), 1) != 0)
nopdelay();
}
}
static u32 dump_rom0_func()
{
common_dump_func(g_hardwareInfo.ROMs[0].StartAddress, 0x400000);
return 0;
}
static u32 dump_rom1_func()
{
common_dump_func(g_hardwareInfo.ROMs[1].StartAddress, 0x400000);
return 0;
}
static u32 dump_rom2_func()
{
common_dump_func(g_hardwareInfo.ROMs[2].StartAddress, 0x80000);
return 0;
}
static u32 dump_nvm_func()
{
// The sceCdReadNVM function retrieves data one u16 at a time.
// There are 512 u16 blocks in the NVRAM.
u8 result;
for (u32 i = 0; i < 512; i++)
{
if (sceCdReadNVM(i, (u16 *)dump_shared_buffer + i, &result) != 1 || result != 0)
{
return result;
}
}
return 0;
}
static u32 dump_mec_func()
{
// cmdNum 0x03: Mechacon cmd
// inBuff[0] 0x00: Read MEC version
// Mechacon version is only 4 bytes long
u32 _unused;
return !sceCdMV(dump_shared_buffer, &_unused);
}