-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdebug.ixx
More file actions
466 lines (374 loc) · 13.3 KB
/
debug.ixx
File metadata and controls
466 lines (374 loc) · 13.3 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
module;
#include <bit>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "throw_line.hh"
export module debug;
import cd.cd;
import cd.common;
import cd.subcode;
import cd.toc;
import common;
import drive;
import drive.mediatek;
import dvd.dump;
import dvd.xbox;
import options;
import scsi.cmd;
import scsi.mmc;
import scsi.sptd;
import utils.endian;
import utils.file_io;
import utils.logger;
import utils.misc;
import utils.win32;
namespace gpsxre
{
export int redumper_subchannel(Context &ctx, Options &options)
{
int exit_code = 0;
std::string image_prefix = (std::filesystem::path(options.image_path) / options.image_name).string();
std::filesystem::path sub_path(image_prefix + ".subcode");
uint32_t sectors_count = check_file(sub_path, CD_SUBCODE_SIZE);
std::fstream sub_fs(sub_path, std::fstream::in | std::fstream::binary);
if(!sub_fs.is_open())
throw_line("unable to open file ({})", sub_path.filename().string());
ChannelQ q_empty;
memset(&q_empty, 0, sizeof(q_empty));
bool empty = false;
std::vector<uint8_t> sub_buffer(CD_SUBCODE_SIZE);
for(uint32_t lba_index = 0; lba_index < sectors_count; ++lba_index)
{
read_entry(sub_fs, sub_buffer.data(), CD_SUBCODE_SIZE, lba_index, 1, 0, 0);
ChannelQ Q;
subcode_extract_channel((uint8_t *)&Q, sub_buffer.data(), Subchannel::Q);
uint8_t P[12];
subcode_extract_channel(P, sub_buffer.data(), Subchannel::P);
uint32_t p_bits = 0;
for(uint32_t i = 0; i < sizeof(P); ++i)
p_bits += std::popcount(P[i]);
// Q is available
if(memcmp(&Q, &q_empty, sizeof(q_empty)))
{
int32_t lbaq = BCDMSF_to_LBA(Q.mode1.a_msf);
LOG("[LBA: {:6}, LBAQ: {:6}] {} P: {}/96", LBA_START + (int32_t)lba_index, lbaq, Q.Decode(), p_bits);
empty = false;
}
else if(!empty)
{
LOG("...");
empty = true;
}
}
return exit_code;
}
#pragma pack(push, 1)
struct SBIEntry
{
MSF msf;
uint8_t one;
struct Q
{
uint8_t adr :4;
uint8_t control :4;
uint8_t tno;
uint8_t point_index;
MSF msf;
uint8_t zero;
MSF a_msf;
} q;
};
#pragma pack(pop)
export int redumper_debug(Context &ctx, Options &options)
{
int exit_code = 0;
#ifndef NDEBUG
std::string image_prefix = (std::filesystem::path(options.image_path) / options.image_name).string();
std::filesystem::path state_path(image_prefix + ".state");
std::filesystem::path cache_path(image_prefix + ".cache");
std::filesystem::path toc_path(image_prefix + ".toc");
std::filesystem::path fulltoc_path(image_prefix + ".fulltoc");
std::filesystem::path cdtext_path(image_prefix + ".cdtext");
std::filesystem::path cue_path(image_prefix + ".cue");
std::filesystem::path physical_path(image_prefix + ".physical");
std::filesystem::path sub_path(image_prefix + ".subcode");
if(0)
{
// auto ss = read_vector("security_sector3.debug");
auto ss = read_vector("dvd.security");
xbox::clean_security_sector(ss);
write_vector("security_sector3_cleaned.debug", ss);
// auto ranges = get_security_layer_descriptor_ranges(ss_layer_descriptor);
/*
auto ss = read_vector("ss.raw");
auto leadout = read_vector("leadout.raw");
xbox::merge_xgd3_security_layer_descriptor(ss, leadout);
write_vector("ss_repaired.raw", ss);
*/
LOG("");
// LOG("{:X}", offsetof(xbox::SecurityLayerDescriptor, xgd23.xgd3.crd[4]));
// LOG("sizeof {}", sizeof(xbox::SecurityLayerDescriptor));
LOG("");
}
if(0)
{
auto drives = SPTD::listDrives();
for(const auto &d : drives)
{
try
{
SPTD sptd(d, options.scsi_timeout);
auto status = cmd_drive_ready(sptd);
if(!status.status_code)
{
LOG("chosen drive: {}", d);
break;
}
}
// drive busy
catch(const std::exception &e)
{
LOG("caught: {}", e.what());
}
}
}
if(0)
{
auto file_data = read_vector("SETUP.EXE");
auto sss = get_pe_executable_extent(file_data);
if(sss)
LOG("size: {}", sss);
LOG("");
}
if(0)
{
std::vector<uint8_t> fulltoc_buffer = read_vector(fulltoc_path);
TOC toc_full(fulltoc_buffer, true);
print_toc(toc_full);
LOG("");
}
// CDB decode
if(0)
{
// uint8_t cdb_raw[] = { 0xbe, 0x04, 0x00, 0x00, 0x0b, 0xd0, 0x00, 0x00, 0x18, 0x14, 0x00, 0x00 };
uint8_t cdb_raw[] = { 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x04, 0x00 };
auto cdb = (CDB12_ReadCD *)cdb_raw;
LOG("");
}
// TOC bad split
if(0)
{
std::vector<uint8_t> toc_buffer = read_vector(toc_path);
TOC toc(toc_buffer, false);
uint32_t subcode_sectors_count = check_file(sub_path, CD_SUBCODE_SIZE);
// preload subchannel P/Q
std::vector<uint8_t> subp;
std::vector<ChannelQ> subq;
if(std::filesystem::exists(sub_path))
{
std::vector<ChannelP> subp_raw;
subcode_load_subpq(subp_raw, subq, sub_path);
LOG_F("correcting P... ");
subp = subcode_correct_subp(subp_raw.data(), subcode_sectors_count);
LOG("done");
LOG_F("correcting Q... ");
if(!subcode_correct_subq(subq.data(), subcode_sectors_count))
subq.clear();
LOG("done");
LOG("");
}
toc.updateQ(subq.data(), subp.data(), subcode_sectors_count, LBA_START, options.legacy_subs);
toc.printCUE(std::cout, options.image_name, 0, false, false);
LOG("");
}
// BluRay structure print
if(0)
{
std::vector<uint8_t> structure = read_vector(physical_path);
LOG("disc structure:");
// print_di_units_structure(&structure[sizeof(CMD_ParameterListHeader)], false);
LOG("");
}
// DVD sectors count
if(0)
{
/*
READ_DVD_STRUCTURE_LayerDescriptor layer_descriptor;
layer_descriptor.data_start_sector = endian_swap<uint32_t>(0x30000);
layer_descriptor.data_end_sector = endian_swap<uint32_t>(0xfff648e8);
layer_descriptor.layer0_end_sector = endian_swap<uint32_t>(1569279);
layer_descriptor.track_path = 1;
int32_t test = 0xfff648e8;
int32_t test2 = sign_extend<24>(endian_swap(layer_descriptor.data_end_sector));
uint32_t sectors_count = get_layer_length(layer_descriptor);
LOG("DVD sectors count: {}", sectors_count);
LOG("");
*/
}
// popcnt test
if(0)
{
/*
for(uint32_t i = 0; i < 0xffffffff; ++i)
{
uint32_t test = __popcnt(i);
uint32_t test2 = bits_count(i);
if(test != test2)
LOG("{} <=> {}", test, test2);
}
*/
}
// CD-TEXT debug
if(0)
{
std::vector<uint8_t> toc_buffer = read_vector(toc_path);
TOC toc(toc_buffer, false);
std::vector<uint8_t> cdtext_buffer = read_vector(cdtext_path);
toc.updateCDTEXT(cdtext_buffer);
std::fstream fs(cue_path, std::fstream::out);
if(!fs.is_open())
throw_line("unable to create file ({})", cue_path.string());
toc.printCUE(fs, options.image_name, 0, false, false);
LOG("");
}
// MEDIATEK cache read
if(0)
{
SPTD sptd(options.drive, options.scsi_timeout);
std::vector<uint8_t> cache;
SPTD::Status status = mediatek_cache_read(sptd, cache, mediatek_get_config(Type::MTK3).size_mb * CHUNK_1MB);
LOG("");
}
// MEDIATEK cache dump extract
if(1)
{
std::vector<uint8_t> cache = read_vector(cache_path);
auto drive_type = Type::MTK2B;
mediatek_cache_print_subq(cache, drive_type);
// auto asd = mediatek_cache_unroll(cache);
// auto asd = mediatek_cache_extract(cache, 128224, 0);
auto mediatek_leadout_buffer = mediatek_cache_extract(cache, 271204, 100, drive_type);
uint32_t entries_count = (uint32_t)mediatek_leadout_buffer.size() / CD_RAW_DATA_SIZE;
LOG("entries count: {}", entries_count);
std::ofstream ofs_data(image_prefix + ".mediatek.data", std::ofstream::binary);
std::ofstream ofs_c2(image_prefix + ".mediatek.c2", std::ofstream::binary);
std::ofstream ofs_sub(image_prefix + ".mediatek.sub", std::ofstream::binary);
for(uint32_t i = 0; i < entries_count; ++i)
{
uint8_t *entry = &mediatek_leadout_buffer[CD_RAW_DATA_SIZE * i];
ofs_data.write((char *)entry, CD_DATA_SIZE);
ofs_c2.write((char *)entry + CD_DATA_SIZE, CD_C2_SIZE);
ofs_sub.write((char *)entry + CD_DATA_SIZE + CD_C2_SIZE, CD_SUBCODE_SIZE);
}
LOG("");
}
// convert old state file to new state file
if(0)
{
std::fstream fs_state(state_path, std::fstream::out | std::fstream::in | std::fstream::binary);
uint64_t states_count = std::filesystem::file_size(state_path) / sizeof(State);
std::vector<State> states((std::vector<State>::size_type)states_count);
fs_state.read((char *)states.data(), states.size() * sizeof(State));
for(auto &s : states)
{
uint8_t value = (uint8_t)s;
if(value == 0)
s = (State)4;
else if(value == 1)
s = (State)3;
else if(value == 3)
s = (State)1;
else if(value == 4)
s = (State)0;
}
fs_state.seekp(0);
fs_state.write((char *)states.data(), states.size() * sizeof(State));
LOG("");
}
// SBI
if(0)
{
std::vector<std::pair<std::vector<std::string>, std::set<int32_t>>> dictionary;
for(auto &entry : std::filesystem::directory_iterator(options.image_path))
{
LOG("{}", entry.path().string());
std::ifstream ifs(entry.path(), std::ifstream::binary);
if(ifs.is_open())
{
char magic[4];
uint32_t entries_count = (std::filesystem::file_size(entry.path()) - sizeof(magic)) / sizeof(SBIEntry);
std::vector<SBIEntry> entries(entries_count);
ifs.read(magic, sizeof(magic));
for(auto &e : entries)
ifs.read((char *)&e, sizeof(e));
std::set<int32_t> values;
for(auto const &e : entries)
values.insert(BCDMSF_to_LBA(e.msf));
bool add = true;
for(auto &d : dictionary)
if(d.second == values)
{
d.first.push_back(entry.path().filename().string());
add = false;
break;
}
if(add)
{
std::vector<std::string> names;
names.push_back(entry.path().filename().string());
dictionary.emplace_back(names, values);
}
LOG("");
}
}
LOG("");
}
LOG("");
#endif
return exit_code;
}
export int redumper_flip(Context &ctx, Options &options)
{
int exit_code = 0;
std::string image_prefix = (std::filesystem::path(options.image_path) / options.image_name).string();
std::filesystem::path state_path(image_prefix + ".state");
std::filesystem::path scram_path(image_prefix + ".scram");
std::filesystem::path flip_path(image_prefix + ".flip");
if(std::filesystem::exists(flip_path) && !options.overwrite)
throw_line("file already exists ({})", flip_path.filename().string());
std::fstream scram_fs(scram_path, std::fstream::in | std::fstream::binary);
if(!scram_fs.is_open())
throw_line("unable to open file ({})", scram_path.filename().string());
std::fstream state_fs(state_path, std::fstream::in | std::fstream::binary);
if(!state_fs.is_open())
throw_line("unable to open file ({})", state_path.filename().string());
std::fstream flip_fs(flip_path, std::fstream::out | std::fstream::binary);
if(!flip_fs.is_open())
throw_line("unable to open file ({})", flip_path.filename().string());
uint32_t samples_count = std::filesystem::file_size(state_path);
for(uint32_t i = 0; i < samples_count; ++i)
{
State state;
state_fs.read((char *)&state, sizeof(state));
if(state_fs.fail())
throw_line("state read failed");
uint32_t sample;
scram_fs.read((char *)&sample, sizeof(sample));
if(scram_fs.fail())
throw_line("sample read failed");
if(state != State::SUCCESS)
sample = ~sample;
flip_fs.write((char *)&sample, sizeof(sample));
if(flip_fs.fail())
throw_line("flip write failed");
}
return exit_code;
}
}