forked from grumpycoders/pcsx-redux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps1-packer.cc
118 lines (105 loc) · 4.91 KB
/
ps1-packer.cc
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
/***************************************************************************
* Copyright (C) 2023 PCSX-Redux authors *
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/
#include "supportpsx/ps1-packer.h"
#include "flags.h"
#include "fmt/format.h"
#include "support/file.h"
#include "support/mem4g.h"
#include "supportpsx/binloader.h"
int main(int argc, char** argv) {
CommandLine::args args(argc, argv);
auto output = args.get<std::string>("o");
fmt::print(R"(
ps1-packer by Nicolas "Pixel" Noble
https://github.com/grumpycoders/pcsx-redux/tree/main/tools/ps1-packer/
)");
auto inputs = args.positional();
const bool asksForHelp = args.get<bool>("h").value_or(false);
const bool hasOutput = output.has_value();
const uint32_t tload = std::stoul(args.get<std::string>("tload").value_or("0"), nullptr, 0);
const bool oneInput = inputs.size() == 1;
const bool shell = args.get<bool>("shell").value_or(false);
const bool raw = args.get<bool>("raw").value_or(false);
const bool booty = args.get<bool>("booty").value_or(false);
const bool rom = args.get<bool>("rom").value_or(false);
const bool cpe = args.get<bool>("cpe").value_or(false);
unsigned outputTypeCount = (raw ? 1 : 0) + (booty ? 1 : 0) + (rom ? 1 : 0) + (cpe ? 1 : 0);
if (asksForHelp || !oneInput || !hasOutput || (outputTypeCount > 1)) {
fmt::print(R"(
Usage: {} input.ps-exe [-h] [-tload addr] [-shell] [-raw | -booty | -rom | -cpe] -o output.ps-exe
input.ps-exe mandatory: specify the input binary file.
-o output.ps-exe mandatory: name of the output file.
-h displays this help information and exit.
-tload force loading at this address instead of doing in-place.
-shell adds a kernel reset stub.
These options control the output format, and are mutually exclusive:
-raw outputs a raw file.
-booty outputs a counter-booty payload.
-rom outputs a bootable rom, which can be used in a cheat cart.
-cpe outputs a CPE file instead of a ps-exe one.
If none of these options is provided, a ps-exe file will be emitted by default.
Valid input binary files can be in the following formats:
- PS-EXE (needs the "PS-X EXE" signature)
- ELF
- CPE
- PSF
- MiniPSF
)",
argv[0]);
return -1;
}
auto& input = inputs[0];
PCSX::IO<PCSX::File> file(new PCSX::PosixFile(input));
if (file->failed()) {
fmt::print("Unable to open file: {}\n", input);
return -1;
}
PCSX::BinaryLoader::Info info;
PCSX::IO<PCSX::Mem4G> memory(new PCSX::Mem4G());
std::map<uint32_t, std::string> symbols;
bool success = PCSX::BinaryLoader::load(file, memory, info, symbols);
if (!success) {
fmt::print("Unable to load file: {}\n", input);
return -1;
}
if (!info.pc.has_value()) {
fmt::print("File {} is invalid.\n", input);
return -1;
}
PCSX::PS1Packer::Options options;
options.booty = booty;
options.raw = raw;
options.rom = rom;
options.cpe = cpe;
options.shell = shell;
options.tload = tload;
PCSX::IO<PCSX::File> out(new PCSX::PosixFile(output.value().c_str(), PCSX::FileOps::TRUNCATE));
PCSX::PS1Packer::pack(new PCSX::SubFile(memory, memory->lowestAddress(), memory->actualSize()), out,
memory->lowestAddress(), info.pc.value_or(0), info.gp.value_or(0), info.sp.value_or(0),
options);
fmt::print(R"(
Input file: {}
pc: 0x{:08x} gp: 0x{:08x} sp: 0x{:08x}
file size: {} -> {}
File {} created. All done.
)",
input, info.pc.value_or(0), info.gp.value_or(0), info.sp.value_or(0), file->size(), out->size(),
output.value());
return 0;
}