-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathredumper.ixx
More file actions
541 lines (448 loc) · 19.7 KB
/
Copy pathredumper.ixx
File metadata and controls
541 lines (448 loc) · 19.7 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
module;
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <format>
#include <fstream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "throw_line.hh"
export module redumper;
import cd.cd;
import cd.dump;
import cd.dump_extra;
import cd.fix_msf;
import cd.protection;
import cd.scrambler;
import cd.split;
import cd.subcode;
import cd.toc;
import common;
import debug;
import drive;
import drive.detect;
import drive.flash.mt1959;
import drive.flash.plextor;
import drive.flash.tsst;
import drive.flash.sd616;
import drive.test;
import dvd.dump;
import dvd.key;
import dvd.split;
import hash;
import info;
import options;
import rings;
import scsi.cmd;
import scsi.mmc;
import scsi.sptd;
import skeleton;
import utils.file_io;
import utils.logger;
import utils.misc;
import utils.signal;
import utils.strings;
import version;
namespace gpsxre
{
int redumper_dump(Context &ctx, Options &options)
{
int exit_code = 0;
if(ctx.disc_type == DiscType::CD)
ctx.refine = redumper_dump_cd(ctx, options, true);
else
ctx.refine = redumper_dump_dvd(ctx, options, true);
return exit_code;
}
int redumper_refine(Context &ctx, Options &options)
{
int exit_code = 0;
if(!ctx.refine || *ctx.refine && options.retries)
{
if(ctx.disc_type == DiscType::CD)
redumper_dump_cd(ctx, options, false);
else
redumper_dump_dvd(ctx, options, false);
}
return exit_code;
}
int redumper_eject(Context &ctx, Options &options)
{
int exit_code = 0;
auto status = cmd_start_stop_unit(*ctx.sptd, 1, 0);
if(status.status_code)
LOG("warning: failed to eject, SCSI ({})", SPTD::StatusMessage(status));
return exit_code;
}
int redumper_split(Context &ctx, Options &options)
{
int exit_code = 0;
auto image_prefix = (std::filesystem::path(options.image_path) / options.image_name).string();
if(std::filesystem::exists(image_prefix + ".iso") || std::filesystem::exists(image_prefix + ".sdram") || std::filesystem::exists(image_prefix + ".sbram"))
redumper_split_dvd(ctx, options);
else
redumper_split_cd(ctx, options);
return exit_code;
}
struct Command
{
using Handler = int (*)(Context &, Options &);
bool drive_required;
bool drive_ready;
bool drive_autoselect;
bool image_name_required;
bool image_name_generate;
Handler handler;
};
const std::map<std::string, Command> COMMANDS{
// NAME DRIVE READY AUTO IMAGE GENERATE HANDLER
{ "rings", { true, true, true, false, false, redumper_rings } },
{ "dump", { true, true, true, true, true, redumper_dump } },
{ "dump::extra", { true, true, true, true, false, redumper_dump_extra } },
{ "refine", { true, true, true, true, false, redumper_refine } },
{ "dvdkey", { true, true, true, false, false, redumper_dvdkey } },
{ "eject", { true, false, false, false, false, redumper_eject } },
{ "dvdisokey", { false, false, false, true, false, redumper_dvdisokey } },
{ "protection", { false, false, false, true, false, redumper_protection } },
{ "split", { false, false, false, true, false, redumper_split } },
{ "hash", { false, false, false, true, false, redumper_hash } },
{ "info", { false, false, false, true, false, redumper_info } },
{ "skeleton", { false, false, false, true, false, redumper_skeleton } },
{ "flash::mt1339", { true, false, false, false, false, redumper_flash_tsst } },
{ "flash::sd616", { true, false, false, false, false, redumper_flash_sd616 } },
{ "flash::plextor", { true, false, false, false, false, redumper_flash_plextor } },
{ "flash::mt1959", { true, false, false, false, false, redumper_flash_mt1959 } },
{ "subchannel", { false, false, false, true, false, redumper_subchannel } },
{ "debug", { false, false, false, false, false, redumper_debug } },
{ "tools::fixmsf", { false, false, false, true, false, redumper_fix_msf } },
{ "tools::fixmsf::shift", { false, false, false, true, false, redumper_fix_msf_shift } },
{ "tools::trim", { false, false, false, true, false, redumper_trim } },
{ "debug::flip", { false, false, false, true, false, redumper_flip } },
{ "drive::test", { true, true, true, false, false, redumper_drive_test } },
{ "drive::detect", { true, true, true, false, false, redumper_drive_detect } },
};
const std::map<DiscType, std::string> DISC_TYPE_STRING = {
{ DiscType::CD, "CD" },
{ DiscType::DVD, "DVD" },
{ DiscType::BLURAY, "BLURAY" },
{ DiscType::BLURAY_R, "BLURAY-R" },
{ DiscType::HDDVD, "HD-DVD" }
};
const std::map<GET_CONFIGURATION_FeatureCode_ProfileList, std::string> PROFILE_STRING = {
{ GET_CONFIGURATION_FeatureCode_ProfileList::RESERVED, "reserved" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::NON_REMOVABLE_DISK, "non removable disk" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::REMOVABLE_DISK, "removable disk" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::MO_ERASABLE, "MO erasable" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::MO_WRITE_ONCE, "MO write once" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::AS_MO, "AS MO" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::CD_ROM, "CD-ROM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::CD_R, "CD-R" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::CD_RW, "CD-RW" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_ROM, "DVD-ROM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R, "DVD-R" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_RAM, "DVD-RAM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_RW_RO, "DVD-RW RO" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_RW, "DVD-RW" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R_DL, "DVD-R DL" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R_DL_LJR, "DVD-R DL LJR" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_RW, "DVD+RW" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R, "DVD+R" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DDCD_ROM, "DDCD-ROM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DDCD_R, "DDCD-R" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DDCD_RW, "DDCD-RW" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_RW_DL, "DVD+RW DL" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R_DL, "DVD+R DL" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::BD_ROM, "BD-ROM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::BD_R, "BD-R" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::BD_R_RRM, "BD-R RRM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::BD_RW, "BD-RW" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_ROM, "HD DVD-ROM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_R, "HD DVD-R" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_RAM, "HD DVD-RAM" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_RW, "HD DVD-RW" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_R_DL, "HD DVD-R DL" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_RW_DL, "HD DVD-RW DL" },
{ GET_CONFIGURATION_FeatureCode_ProfileList::NON_STANDARD, "NON STANDARD" }
};
DiscType profile_to_disc_type(GET_CONFIGURATION_FeatureCode_ProfileList profile)
{
DiscType disc_type;
switch(profile)
{
default:
case GET_CONFIGURATION_FeatureCode_ProfileList::CD_ROM:
case GET_CONFIGURATION_FeatureCode_ProfileList::CD_R:
case GET_CONFIGURATION_FeatureCode_ProfileList::CD_RW:
disc_type = DiscType::CD;
break;
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_ROM:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_RAM:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_RW_RO:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_RW:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R_DL:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R_DL_LJR:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_RW:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_RW_DL:
case GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R_DL:
disc_type = DiscType::DVD;
break;
case GET_CONFIGURATION_FeatureCode_ProfileList::BD_ROM:
disc_type = DiscType::BLURAY;
break;
case GET_CONFIGURATION_FeatureCode_ProfileList::BD_R:
case GET_CONFIGURATION_FeatureCode_ProfileList::BD_R_RRM:
case GET_CONFIGURATION_FeatureCode_ProfileList::BD_RW:
disc_type = DiscType::BLURAY_R;
break;
case GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_ROM:
case GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_R:
case GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_RAM:
case GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_RW:
case GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_R_DL:
case GET_CONFIGURATION_FeatureCode_ProfileList::HDDVD_RW_DL:
disc_type = DiscType::HDDVD;
break;
}
return disc_type;
}
std::list<std::pair<std::string, Command>> redumper_disc_get_commands(Options &options)
{
std::list<std::pair<std::string, Command>> commands;
std::list<std::string> cd_commands{ "dump", "dump::extra", "protection", "refine", "dvdkey", "split", "hash", "info" };
if(options.rings)
cd_commands.insert(cd_commands.begin(), "rings");
if(options.auto_detect)
cd_commands.insert(cd_commands.begin(), "drive::detect");
if(options.auto_eject)
{
if(auto it = std::find(cd_commands.begin(), cd_commands.end(), "split"); it != cd_commands.end())
cd_commands.insert(it, "eject");
}
if(options.skeleton)
cd_commands.push_back("skeleton");
if(auto cit = options.cd_continue ? std::find(cd_commands.begin(), cd_commands.end(), *options.cd_continue) : cd_commands.begin(); cit == cd_commands.end())
throw_line("cd continue command is unavailable (command: {})", *options.cd_continue);
else
{
for(auto it = cd_commands.begin(); it != cit;)
{
if(*it == "rings" && std::find(cit, cd_commands.end(), "dump") != cd_commands.end() || *it == "protection" || *it == "drive::detect")
++it;
else
it = cd_commands.erase(it);
}
}
for(auto c : cd_commands)
{
auto handler_it = COMMANDS.find(c);
if(handler_it == COMMANDS.end())
throw_line("unknown command (command: {})", c);
commands.emplace_back(handler_it->first, handler_it->second);
}
return commands;
}
std::string first_ready_drive(const Options &options)
{
std::string drive;
for(const auto &d : SPTD::listDrives())
{
try
{
SPTD sptd(d, options.scsi_timeout);
// flush pending UNIT ATTENTION
cmd_drive_ready(sptd);
GET_CONFIGURATION_FeatureCode_ProfileList profile;
auto status = cmd_get_configuration_current_profile(sptd, profile);
if(!status.status_code && profile != GET_CONFIGURATION_FeatureCode_ProfileList::RESERVED)
{
drive = d;
break;
}
}
// drive busy
catch(const std::exception &)
{
;
}
}
return drive;
}
std::string generate_image_name(std::string drive)
{
auto pos = drive.find_last_of('/');
std::string d(drive, pos == std::string::npos ? 0 : pos + 1);
erase_all_inplace(d, ':');
return std::format("dump_{}_{}", system_date_time("%y%m%d_%H%M%S"), d);
}
export void redumper_print_drives(bool all)
{
print_drives(all);
}
export int redumper(Options &options)
{
std::list<std::pair<std::string, Command>> commands;
Command aggregate = {};
if(options.command.empty() || options.command == "disc")
{
commands = redumper_disc_get_commands(options);
for(auto const &c : commands)
{
aggregate.drive_required = aggregate.drive_required || c.second.drive_required;
aggregate.drive_ready = aggregate.drive_ready || c.second.drive_ready;
aggregate.drive_autoselect = aggregate.drive_autoselect || c.second.drive_autoselect;
aggregate.image_name_required = aggregate.image_name_required || c.second.image_name_required;
aggregate.image_name_generate = aggregate.image_name_generate || c.second.image_name_generate;
}
}
else
{
auto it = COMMANDS.find(options.command);
if(it == COMMANDS.end())
throw_line("unknown command (command: {})", options.command);
commands.emplace_back(it->first, it->second);
aggregate = it->second;
}
Context ctx;
if(aggregate.drive_required)
{
if(options.drive.empty())
{
if(aggregate.drive_autoselect)
{
options.drive = first_ready_drive(options);
if(options.drive.empty())
throw_line("no ready drives detected on the system");
}
else
throw_line("drive is not provided");
}
ctx.sptd = std::make_shared<SPTD>(options.drive, options.scsi_timeout);
if(aggregate.drive_ready)
{
auto status = cmd_drive_ready(*ctx.sptd);
if(status.status_code)
throw_line("drive not ready, SCSI ({})", SPTD::StatusMessage(status));
}
}
if(aggregate.image_name_required && options.image_name.empty())
{
if(aggregate.image_name_generate)
options.image_name = generate_image_name(options.drive);
else
throw_line("image name is not provided");
}
// init log file early not to miss any messages
if(!options.image_name.empty())
Logger::get().setFile((std::filesystem::path(options.image_path) / options.image_name).string() + ".log");
LOG("{}", redumper_version());
if(options.arguments.empty())
LOGC("[print usage: {}]", Options::helpKeys());
if(!options.arguments.empty())
{
LOG("");
LOG("arguments: {}", options.arguments);
}
ctx.disc_type = DiscType::CD;
if(aggregate.drive_required)
{
// query/override drive configuration
ctx.drive_config = drive_get_config(cmd_drive_query(*ctx.sptd));
drive_override_config(ctx.drive_config, options.drive_type.get(), options.drive_read_offset.get(), options.drive_c2_shift.get(), options.drive_pregap_start.get(),
options.drive_read_method.get(), options.drive_sector_order.get());
std::optional<GET_CONFIGURATION_FeatureCode_ProfileList> current_profile;
if(aggregate.drive_ready)
{
// get current profile
GET_CONFIGURATION_FeatureCode_ProfileList cp;
if(auto status = cmd_get_configuration_current_profile(*ctx.sptd, cp); status.status_code)
LOG("warning: failed to query current profile, SCSI ({})", SPTD::StatusMessage(status));
else
{
if(cp == GET_CONFIGURATION_FeatureCode_ProfileList::RESERVED)
throw_line("drive has no mounted media");
current_profile = cp;
}
}
// set disc type
if(options.disc_type)
ctx.disc_type = string_to_enum(*options.disc_type, DISC_TYPE_STRING);
else if(current_profile)
ctx.disc_type = profile_to_disc_type(*current_profile);
// set speed (depends on a disc type)
std::optional<uint16_t> speed;
if(aggregate.drive_ready)
{
uint16_t s = 0xFFFF;
if(options.speed)
{
float speed_modifier = 176.4;
if(ctx.disc_type == DiscType::DVD)
speed_modifier = 1385.0;
else if(ctx.disc_type == DiscType::BLURAY || ctx.disc_type == DiscType::BLURAY_R || ctx.disc_type == DiscType::HDDVD)
speed_modifier = 4500.0;
s = speed_modifier * *options.speed;
}
if(auto status = cmd_set_cd_speed(*ctx.sptd, s); status.status_code)
LOG("warning: failed to set drive read speed, SCSI ({})", SPTD::StatusMessage(status));
else
speed = s;
}
// drive features check
if(ctx.disc_type == DiscType::CD)
{
if(!drive_is_recommended(ctx.drive_config.vendor_id, ctx.drive_config.product_id, ctx.drive_config.product_revision_level, ctx.drive_config.vendor_specific))
LOG("warning: using generic drive");
auto layout = sector_order_layout(ctx.drive_config.sector_order);
if(layout.subcode_offset == CD_RAW_DATA_SIZE)
LOG("warning: drive doesn't support reading of subchannel data");
if(layout.c2_offset == CD_RAW_DATA_SIZE)
LOG("warning: drive doesn't support C2 error pointers");
}
LOG("");
LOG("drive information");
LOG(" path: {}", options.drive);
LOG(" inquiry: {}", drive_info_string(ctx.drive_config));
LOG(" configuration: {}", drive_config_string(ctx.drive_config));
if(current_profile)
LOG(" profile: {}", enum_to_string(*current_profile, PROFILE_STRING));
if(speed)
LOG(" read speed: {}", *speed == 0xFFFF ? "<optimal>" : std::format("{} KB", *speed));
if(auto version = is_omnidrive_firmware(ctx.drive_config))
{
if(*version < omnidrive_minimum_version())
LOG(" warning: outdated OmniDrive drive firmware (current: {}, recommended: {})", omnidrive_version_string(*version), omnidrive_version_string(omnidrive_minimum_version()));
else
LOG(" firmware: OmniDrive {}", omnidrive_version_string(*version));
}
}
if(!options.image_name.empty())
{
LOG("");
LOG("image path: {}", str_quoted_if_space(options.image_path.empty() ? "." : options.image_path));
LOG("image name: {}", str_quoted_if_space(options.image_name));
}
int exit_code = 0;
std::chrono::seconds time_check = std::chrono::seconds::zero();
for(auto const &c : commands)
{
LOG("");
LOG("*** {}{}", str_uppercase(c.first), std::format(" (time check: {}s)", time_check.count()));
LOG("");
auto time_start = std::chrono::high_resolution_clock::now();
exit_code = c.second.handler(ctx, options);
auto time_stop = std::chrono::high_resolution_clock::now();
time_check = std::chrono::duration_cast<std::chrono::seconds>(time_stop - time_start);
if(exit_code)
break;
}
LOG("");
LOG("*** END{}", std::format(" (time check: {}s)", time_check.count()));
return exit_code;
}
}