305 lines
9.8 KiB
C++
305 lines
9.8 KiB
C++
#include <cctype>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "ac_io.h"
|
|
|
|
namespace {
|
|
|
|
std::vector<std::uint8_t> ReadHex() {
|
|
std::vector<std::uint8_t> out;
|
|
std::string digits;
|
|
int c = 0;
|
|
while ((c = std::getchar()) != EOF) {
|
|
if (std::isxdigit(c) != 0) {
|
|
digits.push_back(static_cast<char>(c));
|
|
}
|
|
}
|
|
for (std::size_t i = 0; i + 1 < digits.size(); i += 2) {
|
|
out.push_back(static_cast<std::uint8_t>(
|
|
std::strtol(digits.substr(i, 2).c_str(), nullptr, 16)));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void PutHex(const void *data, std::size_t size) {
|
|
const auto *bytes = static_cast<const std::uint8_t *>(data);
|
|
for (std::size_t i = 0; i < size; ++i) {
|
|
std::printf("%02x", bytes[i]);
|
|
}
|
|
std::printf("\n");
|
|
}
|
|
|
|
std::uint32_t Word(const std::vector<std::uint8_t> &in, std::size_t offset) {
|
|
std::uint32_t value = 0;
|
|
if (offset + 4 <= in.size()) {
|
|
std::memcpy(&value, in.data() + offset, 4);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
int RunNoop() {
|
|
gk::acio::ac_io_bmpu_consume_coinstock();
|
|
PutHex(nullptr, 0);
|
|
return 0;
|
|
}
|
|
|
|
int RunCurrentCoinstock(const std::vector<std::uint8_t> &in) {
|
|
std::uint32_t out = 0xDEADBEEF;
|
|
gk::acio::ac_io_bmpu_current_coinstock(Word(in, 0), &out);
|
|
PutHex(&out, sizeof(out));
|
|
return 0;
|
|
}
|
|
|
|
int RunGetControlStatus(const std::vector<std::uint8_t> &in) {
|
|
std::memcpy(gk::acio::g_control_status, in.data(),
|
|
in.size() < sizeof(gk::acio::g_control_status)
|
|
? in.size()
|
|
: sizeof(gk::acio::g_control_status));
|
|
std::uint32_t out[16];
|
|
std::memset(out, 0, sizeof(out));
|
|
gk::acio::ac_io_bmpu_get_control_status_buffer(out);
|
|
PutHex(out, sizeof(out));
|
|
return 0;
|
|
}
|
|
|
|
int RunLedOff() {
|
|
const std::uint32_t result = gk::acio::ac_io_bmpu_control_1p_start_led_off();
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunUpdate() {
|
|
const std::uint8_t result = gk::acio::ac_io_update();
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunIccaStatus(const std::vector<std::uint8_t> &in) {
|
|
std::uint32_t status[5];
|
|
std::memset(status, 0, sizeof(status));
|
|
std::memcpy(status, in.data(),
|
|
in.size() < sizeof(status) ? in.size() : sizeof(status));
|
|
const std::uint32_t result = gk::acio::ac_io_icca_get_status(status);
|
|
std::uint8_t blob[sizeof(status) + sizeof(result)];
|
|
std::memcpy(blob, status, sizeof(status));
|
|
std::memcpy(blob + sizeof(status), &result, sizeof(result));
|
|
PutHex(blob, sizeof(blob));
|
|
return 0;
|
|
}
|
|
|
|
int RunIsActive(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t result =
|
|
gk::acio::ac_io_is_active_device(static_cast<int>(Word(in, 0))) ? 1u : 0u;
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunGetVersion(const std::vector<std::uint8_t> &in) {
|
|
std::uint8_t buffer[12];
|
|
std::memset(buffer, 0, sizeof(buffer));
|
|
std::memcpy(buffer, in.data(),
|
|
in.size() < sizeof(buffer) ? in.size() : sizeof(buffer));
|
|
const std::uint32_t result = gk::acio::ac_io_get_version(buffer);
|
|
std::uint8_t blob[sizeof(buffer) + sizeof(result)];
|
|
std::memcpy(blob, buffer, sizeof(buffer));
|
|
std::memcpy(blob + sizeof(buffer), &result, sizeof(result));
|
|
PutHex(blob, sizeof(blob));
|
|
return 0;
|
|
}
|
|
|
|
int RunSub1380(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t result =
|
|
gk::acio::sub_10001380(Word(in, 0), Word(in, 4), Word(in, 8));
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunSoftwareId(const std::vector<std::uint8_t> &in) {
|
|
gk::acio::g_softwareid = Word(in, 0);
|
|
const std::uint32_t result = gk::acio::ac_io_bmpu_get_softwareid();
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunSub1437(const std::vector<std::uint8_t> &in) {
|
|
gk::acio::g_state_d3f8 = Word(in, 0);
|
|
gk::acio::g_state_df80 = Word(in, 4);
|
|
const std::uint32_t result = gk::acio::sub_10001437() ? 1u : 0u;
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunSub2469(const std::vector<std::uint8_t> &in) {
|
|
gk::acio::g_state_dfa4 = 0xDEADBEEF;
|
|
gk::acio::sub_10002469(Word(in, 0));
|
|
PutHex(&gk::acio::g_state_dfa4, sizeof(gk::acio::g_state_dfa4));
|
|
return 0;
|
|
}
|
|
|
|
// --- wave 2 modes ---
|
|
|
|
const std::uint32_t kStoreSentinel = 0xAAAAAAAAu;
|
|
|
|
int RunMapError(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t result =
|
|
static_cast<std::uint32_t>(gk::acio::sub_10002593(static_cast<std::int32_t>(Word(in, 0))));
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunRtcInit() {
|
|
gk::acio::rtc_initialize();
|
|
PutHex(nullptr, 0);
|
|
return 0;
|
|
}
|
|
|
|
int RunSub3bfe() {
|
|
gk::acio::sub_10003bfe();
|
|
PutHex(nullptr, 0);
|
|
return 0;
|
|
}
|
|
|
|
int RunStrlen(const std::vector<std::uint8_t> &in) {
|
|
std::string s(in.begin(), in.end());
|
|
const std::uint32_t result = gk::acio::crt_strlen(s.c_str());
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunSub5095(const std::vector<std::uint8_t> &in) {
|
|
gk::acio::g_state_ea54 = Word(in, 0);
|
|
gk::acio::sub_10005095();
|
|
PutHex(&gk::acio::g_state_ea54, sizeof(gk::acio::g_state_ea54));
|
|
return 0;
|
|
}
|
|
|
|
int RunValidatePe(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t result =
|
|
static_cast<std::uint32_t>(gk::acio::validate_image_base(in.data()));
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunFindSection(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t rva = Word(in, 0);
|
|
const std::uint8_t *image = in.data() + 4;
|
|
const std::uint8_t *found = gk::acio::find_pe_section(image, rva);
|
|
const std::uint32_t offset =
|
|
found == nullptr ? 0xFFFFFFFFu : static_cast<std::uint32_t>(found - image);
|
|
PutHex(&offset, sizeof(offset));
|
|
return 0;
|
|
}
|
|
|
|
template <void (*Store)(std::uint32_t), std::uint32_t *Global>
|
|
int RunStore(const std::vector<std::uint8_t> &in) {
|
|
*Global = kStoreSentinel;
|
|
Store(Word(in, 0));
|
|
PutHex(Global, sizeof(*Global));
|
|
return 0;
|
|
}
|
|
|
|
int RunStrcmp(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t n1 = Word(in, 0);
|
|
const char *s1 = reinterpret_cast<const char *>(in.data() + 4);
|
|
const char *s2 = reinterpret_cast<const char *>(in.data() + 4 + n1);
|
|
const std::uint32_t result = static_cast<std::uint32_t>(gk::acio::crt_strcmp(s1, s2));
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunStrcspn(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t n1 = Word(in, 0);
|
|
const char *s = reinterpret_cast<const char *>(in.data() + 4);
|
|
const char *ctrl = reinterpret_cast<const char *>(in.data() + 4 + n1);
|
|
const std::uint32_t result = gk::acio::crt_strcspn(s, ctrl);
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
int RunStrpbrk(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t n1 = Word(in, 0);
|
|
const char *s = reinterpret_cast<const char *>(in.data() + 4);
|
|
const char *ctrl = reinterpret_cast<const char *>(in.data() + 4 + n1);
|
|
const char *found = gk::acio::crt_strpbrk(s, ctrl);
|
|
const std::uint32_t offset = found == nullptr ? 0xFFFFFFFFu
|
|
: static_cast<std::uint32_t>(found - s);
|
|
PutHex(&offset, sizeof(offset));
|
|
return 0;
|
|
}
|
|
|
|
int RunFastZero(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t count = Word(in, 0);
|
|
std::vector<std::uint8_t> buf(in.begin() + 4, in.end());
|
|
gk::acio::fast_zero(buf.data(), count);
|
|
PutHex(buf.data(), buf.size());
|
|
return 0;
|
|
}
|
|
|
|
int RunVecMemcpy(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t count = Word(in, 0);
|
|
const std::uint32_t buflen = Word(in, 4);
|
|
std::vector<std::uint8_t> dst(in.begin() + 8, in.begin() + 8 + buflen);
|
|
const std::uint8_t *src = in.data() + 8 + buflen;
|
|
gk::acio::vec_memcpy(dst.data(), src, count);
|
|
PutHex(dst.data(), dst.size());
|
|
return 0;
|
|
}
|
|
|
|
int RunAsciiStrnicmp(const std::vector<std::uint8_t> &in) {
|
|
const std::uint32_t n1 = Word(in, 0);
|
|
const std::uint32_t count = Word(in, 4);
|
|
const char *s1 = reinterpret_cast<const char *>(in.data() + 8);
|
|
const char *s2 = reinterpret_cast<const char *>(in.data() + 8 + n1);
|
|
const std::uint32_t result =
|
|
static_cast<std::uint32_t>(gk::acio::crt_ascii_strnicmp(s1, s2, count));
|
|
PutHex(&result, sizeof(result));
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 2) {
|
|
std::fprintf(stderr, "usage: recon_libacio <mode>\n");
|
|
return 2;
|
|
}
|
|
const std::vector<std::uint8_t> in = ReadHex();
|
|
const std::string mode = argv[1];
|
|
if (mode == "noop") return RunNoop();
|
|
if (mode == "current_coinstock") return RunCurrentCoinstock(in);
|
|
if (mode == "get_control_status") return RunGetControlStatus(in);
|
|
if (mode == "led_off") return RunLedOff();
|
|
if (mode == "update") return RunUpdate();
|
|
if (mode == "icca_status") return RunIccaStatus(in);
|
|
if (mode == "is_active") return RunIsActive(in);
|
|
if (mode == "get_version") return RunGetVersion(in);
|
|
if (mode == "sub1380") return RunSub1380(in);
|
|
if (mode == "softwareid") return RunSoftwareId(in);
|
|
if (mode == "sub1437") return RunSub1437(in);
|
|
if (mode == "sub2469") return RunSub2469(in);
|
|
if (mode == "map_error") return RunMapError(in);
|
|
if (mode == "rtc_init") return RunRtcInit();
|
|
if (mode == "sub3bfe") return RunSub3bfe();
|
|
if (mode == "strlen") return RunStrlen(in);
|
|
if (mode == "sub5095") return RunSub5095(in);
|
|
if (mode == "validate_pe") return RunValidatePe(in);
|
|
if (mode == "find_section") return RunFindSection(in);
|
|
if (mode == "store_e8f4") return RunStore<gk::acio::sub_1000590e, &gk::acio::g_state_e8f4>(in);
|
|
if (mode == "store_e900") return RunStore<gk::acio::sub_10005918, &gk::acio::g_state_e900>(in);
|
|
if (mode == "store_e904") return RunStore<gk::acio::sub_10005922, &gk::acio::g_state_e904>(in);
|
|
if (mode == "store_e908") return RunStore<gk::acio::sub_10005a01, &gk::acio::g_state_e908>(in);
|
|
if (mode == "strcmp") return RunStrcmp(in);
|
|
if (mode == "strcspn") return RunStrcspn(in);
|
|
if (mode == "strpbrk") return RunStrpbrk(in);
|
|
if (mode == "fast_zero") return RunFastZero(in);
|
|
if (mode == "vec_memcpy") return RunVecMemcpy(in);
|
|
if (mode == "ascii_strnicmp") return RunAsciiStrnicmp(in);
|
|
std::fprintf(stderr, "unknown mode %s\n", argv[1]);
|
|
return 2;
|
|
}
|