Skylanders: Improve figure data view and generation

Co-authored-by: deReeperJosh <joshua@dereeper.co.nz>
This commit is contained in:
Mandar1jn
2023-08-24 17:24:30 +02:00
committed by Admiral H. Curtiss
parent 220988d064
commit afdf6de041
20 changed files with 1369 additions and 146 deletions

View File

@ -413,8 +413,12 @@ add_library(core
IOS/USB/Common.h
IOS/USB/Emulated/Infinity.cpp
IOS/USB/Emulated/Infinity.h
IOS/USB/Emulated/Skylander.cpp
IOS/USB/Emulated/Skylander.h
IOS/USB/Emulated/Skylanders/Skylander.cpp
IOS/USB/Emulated/Skylanders/Skylander.h
IOS/USB/Emulated/Skylanders/SkylanderCrypto.cpp
IOS/USB/Emulated/Skylanders/SkylanderCrypto.h
IOS/USB/Emulated/Skylanders/SkylanderFigure.cpp
IOS/USB/Emulated/Skylanders/SkylanderFigure.h
IOS/USB/Host.cpp
IOS/USB/Host.h
IOS/USB/OH0/OH0.cpp

View File

@ -1,7 +1,7 @@
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Core/IOS/USB/Emulated/Skylander.h"
#include "Core/IOS/USB/Emulated/Skylanders/Skylander.h"
#include <map>
#include <mutex>
@ -14,6 +14,7 @@
#include "Common/Timer.h"
#include "Core/Core.h"
#include "Core/HW/Memmap.h"
#include "Core/IOS/USB/Emulated/Skylanders/SkylanderCrypto.h"
#include "Core/System.h"
namespace IOS::HLE::USB
@ -1030,15 +1031,6 @@ void SkylanderUSB::ScheduleTransfer(std::unique_ptr<TransferCommand> command,
command->ScheduleTransferCompletion(expected_count, expected_time_us);
}
void Skylander::Save()
{
if (!sky_file)
return;
sky_file.Seek(0, File::SeekOrigin::Begin);
sky_file.WriteBytes(data.data(), 0x40 * 0x10);
}
void SkylanderPortal::Activate()
{
std::lock_guard lock(sky_mutex);
@ -1190,14 +1182,14 @@ void SkylanderPortal::QueryBlock(u8 sky_num, u8 block, u8* reply_buf)
reply_buf[0] = 'Q';
reply_buf[2] = block;
if (skylander.status & 1)
if (skylander.status & Skylander::READY)
{
reply_buf[1] = (0x10 | sky_num);
memcpy(reply_buf + 3, skylander.data.data() + (16 * block), 16);
skylander.figure->GetBlock(block, reply_buf + 3);
}
else
{
reply_buf[1] = sky_num;
reply_buf[1] = 0x01;
}
}
@ -1216,98 +1208,15 @@ void SkylanderPortal::WriteBlock(u8 sky_num, u8 block, const u8* to_write_buf, u
if (skylander.status & 1)
{
reply_buf[1] = (0x10 | sky_num);
memcpy(skylander.data.data() + (block * 16), to_write_buf, 16);
skylander.Save();
skylander.figure->SetBlock(block, to_write_buf);
skylander.figure->Save();
}
else
{
reply_buf[1] = sky_num;
reply_buf[1] = 0x01;
}
}
static u16 SkylanderCRC16(u16 init_value, const u8* buffer, u32 size)
{
static constexpr std::array<u16, 256> CRC_CCITT_TABLE{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A,
0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294,
0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 0x2462,
0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509,
0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695,
0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, 0x48C4, 0x58E5,
0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948,
0x9969, 0xA90A, 0xB92B, 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4,
0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B,
0x8D68, 0x9D49, 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 0xFF9F,
0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, 0x9188, 0x81A9, 0xB1CA, 0xA1EB,
0xD10C, 0xC12D, 0xF14E, 0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046,
0x6067, 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290,
0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E,
0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691,
0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9,
0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, 0xCB7D,
0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 0x7A16,
0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8,
0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E,
0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93,
0x3EB2, 0x0ED1, 0x1EF0};
u16 crc = init_value;
for (u32 i = 0; i < size; i++)
{
const u16 tmp = (crc >> 8) ^ buffer[i];
crc = (crc << 8) ^ CRC_CCITT_TABLE[tmp];
}
return crc;
}
bool SkylanderPortal::CreateSkylander(const std::string& file_path, u16 m_sky_id, u16 m_sky_var)
{
File::IOFile sky_file(file_path, "w+b");
if (!sky_file)
{
return false;
}
std::array<u8, 0x40 * 0x10> buf{};
const auto file_data = buf.data();
// Set the block permissions
u32 first_block = 0x690F0F0F;
u32 other_blocks = 0x69080F7F;
memcpy(&file_data[0x36], &first_block, sizeof(first_block));
for (u32 index = 1; index < 0x10; index++)
{
memcpy(&file_data[(index * 0x40) + 0x36], &other_blocks, sizeof(other_blocks));
}
// Set the NUID of the figure
Common::Random::Generate(&file_data[0], 4);
// The BCC (Block Check Character)
file_data[4] = file_data[0] ^ file_data[1] ^ file_data[2] ^ file_data[3];
// ATQA
file_data[5] = 0x81;
file_data[6] = 0x01;
// SAK
file_data[7] = 0x0F;
// Set the skylander info
memcpy(&file_data[0x10], &m_sky_id, sizeof(m_sky_id));
memcpy(&file_data[0x1C], &m_sky_var, sizeof(m_sky_var));
// Set checksum
u16 checksum = SkylanderCRC16(0xFFFF, file_data, 0x1E);
memcpy(&file_data[0x1E], &checksum, sizeof(checksum));
sky_file.WriteBytes(buf.data(), buf.size());
return true;
}
bool SkylanderPortal::RemoveSkylander(u8 sky_num)
{
if (!IsSkylanderNumberValid(sky_num))
@ -1317,13 +1226,13 @@ bool SkylanderPortal::RemoveSkylander(u8 sky_num)
std::lock_guard lock(sky_mutex);
auto& skylander = skylanders[sky_num];
if (skylander.sky_file.IsOpen())
if (skylander.figure->FileIsOpen())
{
skylander.Save();
skylander.sky_file.Close();
skylander.figure->Save();
skylander.figure->Close();
}
if (skylander.status & 1)
if (skylander.status & Skylander::READY)
{
skylander.status = Skylander::REMOVING;
skylander.queued_status.push(Skylander::REMOVING);
@ -1334,15 +1243,17 @@ bool SkylanderPortal::RemoveSkylander(u8 sky_num)
return false;
}
u8 SkylanderPortal::LoadSkylander(u8* buf, File::IOFile in_file)
u8 SkylanderPortal::LoadSkylander(std::unique_ptr<SkylanderFigure> figure)
{
std::lock_guard lock(sky_mutex);
u32 sky_serial = 0;
std::array<u8, 0x10> block = {};
figure->GetBlock(0, block.data());
for (int i = 3; i > -1; i--)
{
sky_serial <<= 8;
sky_serial |= buf[i];
sky_serial |= block[i];
}
u8 found_slot = 0xFF;
@ -1370,10 +1281,7 @@ u8 SkylanderPortal::LoadSkylander(u8* buf, File::IOFile in_file)
if (found_slot != 0xFF)
{
auto& skylander = skylanders[found_slot];
memcpy(skylander.data.data(), buf, skylander.data.size());
DEBUG_LOG_FMT(IOS_USB, "Skylander Data: \n{}",
HexDump(skylander.data.data(), skylander.data.size()));
skylander.sky_file = std::move(in_file);
skylander.figure = std::move(figure);
skylander.status = Skylander::ADDED;
skylander.queued_status.push(Skylander::ADDED);
skylander.queued_status.push(Skylander::READY);
@ -1403,4 +1311,32 @@ std::pair<u16, u16> SkylanderPortal::CalculateIDs(const std::array<u8, 0x40 * 0x
return std::make_pair(m_sky_id, m_sky_var);
}
Skylander* SkylanderPortal::GetSkylander(u8 slot)
{
return &skylanders[slot];
}
Type NormalizeSkylanderType(Type type)
{
switch (type)
{
case Type::Skylander:
case Type::Giant:
case Type::Swapper:
case Type::TrapMaster:
case Type::Mini:
return Type::Skylander;
case Type::Trophy:
return Type::Trophy;
default:
case Type::Item:
case Type::Trap:
case Type::Vehicle:
case Type::Unknown:
// until these get seperate data logic (except unknown and item since items don't save data and
// unknown is unknown)
return Type::Unknown;
}
}
} // namespace IOS::HLE::USB

View File

@ -11,6 +11,7 @@
#include "Common/CommonTypes.h"
#include "Common/IOFile.h"
#include "Core/IOS/USB/Common.h"
#include "Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h"
// The maximum possible characters the portal can handle.
// The status array is 32 bits and every character takes 2 bits.
@ -30,7 +31,7 @@ enum class Game
TrapTeam,
Superchargers
};
enum class Type
enum class Type : u8
{
Skylander = 1,
Giant,
@ -43,6 +44,7 @@ enum class Type
Trap,
Unknown
};
Type NormalizeSkylanderType(Type type);
enum class Element
{
Magic = 1,
@ -104,12 +106,10 @@ private:
struct Skylander final
{
File::IOFile sky_file;
std::unique_ptr<SkylanderFigure> figure;
u8 status = 0;
std::queue<u8> queued_status;
std::array<u8, 0x40 * 0x10> data{};
u32 last_id = 0;
void Save();
enum : u8
{
@ -140,9 +140,9 @@ public:
void QueryBlock(u8 sky_num, u8 block, u8* reply_buf);
void WriteBlock(u8 sky_num, u8 block, const u8* to_write_buf, u8* reply_buf);
bool CreateSkylander(const std::string& file_path, u16 m_sky_id, u16 m_sky_var);
bool RemoveSkylander(u8 sky_num);
u8 LoadSkylander(u8* buf, File::IOFile in_file);
u8 LoadSkylander(std::unique_ptr<SkylanderFigure> figure);
Skylander* GetSkylander(u8 slot);
std::pair<u16, u16> CalculateIDs(const std::array<u8, 0x40 * 0x10>& file_data);
private:

View File

@ -0,0 +1,154 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Core/IOS/USB/Emulated/Skylanders/SkylanderCrypto.h"
#include <array>
#include <span>
#include <string>
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
#include "Common/Swap.h"
namespace IOS::HLE::USB::SkylanderCrypto
{
u16 ComputeCRC16(std::span<const u8> data)
{
const u16 polynomial = 0x1021;
u16 crc = 0xFFFF;
for (size_t i = 0; i < data.size(); ++i)
{
crc ^= data[i] << 8;
for (size_t j = 0; j < 8; j++)
{
if (Common::ExtractBit(crc, 15))
{
crc = (crc << 1) ^ polynomial;
}
else
{
crc <<= 1;
}
}
}
return crc;
}
// CRC-64 algorithm that is limited to 48 bits every iteration
u64 ComputeCRC48(std::span<const u8> data)
{
const u64 polynomial = 0x42f0e1eba9ea3693;
const u64 initial_register_value = 2ULL * 2ULL * 3ULL * 1103ULL * 12868356821ULL;
u64 crc = initial_register_value;
for (size_t i = 0; i < data.size(); ++i)
{
crc ^= (static_cast<u64>(data[i]) << 40);
for (size_t j = 0; j < 8; ++j)
{
if (Common::ExtractBit(crc, 47))
{
crc = (crc << 1) ^ polynomial;
}
else
{
crc <<= 1;
}
}
}
return crc & 0x0000FFFFFFFFFFFF;
}
u64 CalculateKeyA(u8 sector, std::span<const u8, 0x4> nuid)
{
if (sector == 0)
{
return 73ULL * 2017ULL * 560381651ULL;
}
std::array<u8, 5> data = {nuid[0], nuid[1], nuid[2], nuid[3], sector};
u64 big_endian_crc = ComputeCRC48(data);
u64 little_endian_crc = Common::swap64(big_endian_crc) >> 16;
return little_endian_crc;
}
void ComputeChecksumType0(const u8* data_start, u8* output)
{
std::array<u8, 0x1E> input = {};
memcpy(input.data(), data_start, 0x1E);
u16 crc = ComputeCRC16(input);
memcpy(output, &crc, 2);
}
void ComputeChecksumType1(const u8* data_start, u8* output)
{
std::array<u8, 0x10> input = {};
memcpy(input.data(), data_start, 0x10);
input[0xE] = 0x05;
input[0xF] = 0x00;
u16 crc = ComputeCRC16(input);
memcpy(output, &crc, 2);
}
void ComputeChecksumType2(const u8* data_start, u8* output)
{
std::array<u8, 0x30> input = {};
memcpy(input.data(), data_start, 0x20);
memcpy(input.data() + 0x20, data_start + 0x30, 0x10);
u16 crc = ComputeCRC16(input);
memcpy(output, &crc, 2);
}
void ComputeChecksumType3(const u8* data_start, u8* output)
{
std::array<u8, 0x110> input = {};
memcpy(input.data(), data_start, 0x20);
memcpy(input.data() + 0x20, data_start + 0x30, 0x10);
u16 crc = ComputeCRC16(input);
memcpy(output, &crc, 2);
}
void ComputeChecksumType6(const u8* data_start, u8* output)
{
std::array<u8, 0x40> input = {};
memcpy(input.data(), data_start, 0x20);
memcpy(input.data() + 0x20, data_start + 0x30, 0x20);
input[0x0] = 0x06;
input[0x1] = 0x01;
u16 crc = ComputeCRC16(input);
memcpy(output, &crc, 2);
}
std::array<u8, 11> ComputeToyCode(u64 code)
{
if (code == 0)
{
static constexpr std::array<u8, 11> invalid_code_result{
static_cast<u8>('N'), static_cast<u8>('/'), static_cast<u8>('A')};
return invalid_code_result;
}
std::array<u8, 10> code_bytes;
for (size_t i = 0; i < code_bytes.size(); ++i)
{
code_bytes[i] = static_cast<u8>(code % 29);
code /= 29;
}
static constexpr char lookup_table[] = "23456789BCDFGHJKLMNPQRSTVWXYZ";
std::array<u8, 10> code_chars;
for (size_t i = 0; i < code_bytes.size(); ++i)
{
code_chars[i] = static_cast<u8>(lookup_table[code_bytes[9 - i]]);
}
std::array<u8, 11> result;
std::memcpy(&result[0], &code_chars[0], 5);
result[5] = static_cast<u8>('-');
std::memcpy(&result[6], &code_chars[5], 5);
return result;
}
} // namespace IOS::HLE::USB::SkylanderCrypto

View File

@ -0,0 +1,22 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <span>
#include "Common/CommonTypes.h"
namespace IOS::HLE::USB::SkylanderCrypto
{
u16 ComputeCRC16(std::span<const u8> data);
u64 ComputeCRC48(std::span<const u8> data);
u64 CalculateKeyA(u8 sector, std::span<const u8, 0x4> nuid);
void ComputeChecksumType0(const u8* data_start, u8* output);
void ComputeChecksumType1(const u8* data_start, u8* output);
void ComputeChecksumType2(const u8* data_start, u8* output);
void ComputeChecksumType3(const u8* data_start, u8* output);
void ComputeChecksumType6(const u8* data_start, u8* output);
std::array<u8, 11> ComputeToyCode(u64 code);
} // namespace IOS::HLE::USB::SkylanderCrypto

View File

@ -0,0 +1,423 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Core/IOS/USB/Emulated/Skylanders/SkylanderFigure.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <span>
#include <string>
#include <mbedtls/aes.h>
#include <mbedtls/md5.h>
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
#include "Common/IOFile.h"
#include "Common/Logging/Log.h"
#include "Common/Random.h"
#include "Common/StringUtil.h"
#include "Core/IOS/USB/Emulated/Skylanders/Skylander.h"
#include "Core/IOS/USB/Emulated/Skylanders/SkylanderCrypto.h"
using namespace IOS::HLE::USB::SkylanderCrypto;
namespace IOS::HLE::USB
{
void SkylanderFigure::PopulateSectorTrailers()
{
// Set the sector permissions
u32 first_block = 0x690F0F0F;
u32 other_blocks = 0x69080F7F;
memcpy(&m_data[0x36], &first_block, sizeof(first_block));
for (size_t index = 1; index < 0x10; index++)
{
memcpy(&m_data[(index * 0x40) + 0x36], &other_blocks, sizeof(other_blocks));
}
}
void SkylanderFigure::PopulateKeys()
{
for (u8 sector = 0; sector < 0x10; sector++)
{
u16 key_offset = (sector * 64) + (3 * 16);
u64 key = CalculateKeyA(sector, std::span<u8, 4>(m_data.begin(), 4));
for (u32 j = 0; j < 6; j++)
{
u16 index = key_offset + (5 - j);
u8 byte = (key >> (j * 8)) & 0xFF;
m_data[index] = byte;
}
}
}
SkylanderFigure::SkylanderFigure(const std::string& file_path)
{
m_sky_file = File::IOFile(file_path, "w+b");
m_data = {};
}
// Generate a AES key without the block filled in
void SkylanderFigure::GenerateIncompleteHashIn(u8* dest) const
{
std::array<u8, 0x56> hash_in = {};
// copy first 2 blocks into hash
GetBlock(0, hash_in.data());
GetBlock(1, hash_in.data() + 0x10);
// Skip 1 byte. Is a block index that needs to be set per block.
// Byte array of ascii string " Copyright (C) 2010 Activision. All Rights Reserved.". The space at
// the start of the string is intentional
static constexpr std::array<u8, 0x35> HASH_CONST = {
0x20, 0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x43, 0x29,
0x20, 0x32, 0x30, 0x31, 0x30, 0x20, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x73, 0x69,
0x6F, 0x6E, 0x2E, 0x20, 0x41, 0x6C, 0x6C, 0x20, 0x52, 0x69, 0x67, 0x68, 0x74, 0x73,
0x20, 0x52, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x2E, 0x20};
memcpy(hash_in.data() + 0x21, HASH_CONST.data(), HASH_CONST.size());
memcpy(dest, hash_in.data(), 0x56);
}
void SkylanderFigure::Encrypt(std::span<const u8, FIGURE_SIZE> input)
{
std::array<u8, 0x56> hash_in = {};
GenerateIncompleteHashIn(hash_in.data());
std::array<u8, FIGURE_SIZE> encrypted = {};
std::array<u8, BLOCK_SIZE> current_block = {};
// Run for every block
for (u8 i = 0; i < 64; ++i)
{
memcpy(current_block.data(), input.data() + (i * BLOCK_SIZE), BLOCK_SIZE);
// Skip sector trailer and the first 8 blocks
if (((i + 1) % 4 == 0) || i < 8)
{
memcpy(encrypted.data() + (i * BLOCK_SIZE), current_block.data(), BLOCK_SIZE);
continue;
}
// Block index
hash_in[0x20] = i;
std::array<u8, BLOCK_SIZE> hash_out = {};
mbedtls_md5_ret(hash_in.data(), 0x56, hash_out.data());
mbedtls_aes_context aes_context = {};
mbedtls_aes_setkey_enc(&aes_context, hash_out.data(), 128);
mbedtls_aes_crypt_ecb(&aes_context, MBEDTLS_AES_ENCRYPT, current_block.data(),
encrypted.data() + (i * BLOCK_SIZE));
}
memcpy(m_data.data(), encrypted.data(), FIGURE_SIZE);
DEBUG_LOG_FMT(IOS_USB, "Encrypted skylander data: \n{}", HexDump(encrypted.data(), FIGURE_SIZE));
}
SkylanderFigure::SkylanderFigure(File::IOFile file)
{
m_sky_file = std::move(file);
m_sky_file.Seek(0, File::SeekOrigin::Begin);
m_sky_file.ReadBytes(m_data.data(), m_data.size());
}
bool SkylanderFigure::Create(u16 sky_id, u16 sky_var,
std::optional<std::array<u8, 4>> requested_nuid)
{
if (!m_sky_file)
{
return false;
}
memset(m_data.data(), 0, m_data.size());
PopulateSectorTrailers();
// Set the NUID of the figure
if (requested_nuid)
std::memcpy(&m_data[0], requested_nuid->data(), 4);
else
Common::Random::Generate(&m_data[0], 4);
// The BCC (Block Check Character)
m_data[4] = m_data[0] ^ m_data[1] ^ m_data[2] ^ m_data[3];
// ATQA
m_data[5] = 0x81;
m_data[6] = 0x01;
// SAK
m_data[7] = 0x0F;
// Set the skylander info
memcpy(&m_data[0x10], &sky_id, sizeof(sky_id));
memcpy(&m_data[0x1C], &sky_var, sizeof(sky_var));
// Set checksum
ComputeChecksumType0(m_data.data(), m_data.data() + 0x1E);
PopulateKeys();
Save();
return true;
}
void SkylanderFigure::Save()
{
m_sky_file.Seek(0, File::SeekOrigin::Begin);
m_sky_file.WriteBytes(m_data.data(), FIGURE_SIZE);
}
void SkylanderFigure::GetBlock(u8 index, u8* dest) const
{
memcpy(dest, m_data.data() + (index * BLOCK_SIZE), BLOCK_SIZE);
}
FigureData SkylanderFigure::GetData() const
{
FigureData figure_data = {.figure_id = Common::BitCastPtr<u16>(m_data.data() + 0x10),
.variant_id = Common::BitCastPtr<u16>(m_data.data() + 0x1C)};
auto filter = std::make_pair(figure_data.figure_id, figure_data.variant_id);
Type type = Type::Item;
if (IOS::HLE::USB::list_skylanders.count(filter) != 0)
{
auto found = IOS::HLE::USB::list_skylanders.at(filter);
type = found.type;
}
figure_data.normalized_type = NormalizeSkylanderType(type);
if (figure_data.normalized_type == Type::Skylander)
{
std::array<u8, FIGURE_SIZE> decrypted = {};
DecryptFigure(&decrypted);
// Area with highest area counter is the newest
u16 area_offset = ((decrypted[0x89] + 1U) != decrypted[0x249]) ? 0x80 : 0x240;
figure_data.skylander_data = {
.money = Common::BitCastPtr<u16>(decrypted.data() + area_offset + 0x3),
.hero_level = Common::BitCastPtr<u16>(decrypted.data() + area_offset + 0x5A),
.playtime = Common::BitCastPtr<u32>(decrypted.data() + area_offset + 0x5),
.last_reset = {.minute = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x60),
.hour = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x61),
.day = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x62),
.month = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x63),
.year = Common::BitCastPtr<u16>(decrypted.data() + area_offset + 0x64)},
.last_placed = {.minute = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x50),
.hour = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x51),
.day = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x52),
.month = Common::BitCastPtr<u8>(decrypted.data() + area_offset + 0x53),
.year = Common::BitCastPtr<u16>(decrypted.data() + area_offset + 0x54)}};
figure_data.skylander_data.toy_code =
ComputeToyCode(Common::BitCastPtr<u64>(decrypted.data() + 0x14));
std::array<u16, 16> nickname{};
// First nickname half
for (size_t i = 0; i < 8; ++i)
{
nickname[i] = Common::BitCastPtr<u16>(decrypted.data() + area_offset + 0x20 + (i * 2));
}
// Second nickname half
for (size_t i = 0; i < 8; ++i)
{
nickname[i + 8] = Common::BitCastPtr<u16>(decrypted.data() + area_offset + 0x40 + (i * 2));
}
figure_data.skylander_data.nickname = nickname;
}
else if (figure_data.normalized_type == Type::Trophy)
{
std::array<u8, FIGURE_SIZE> decrypted = {};
DecryptFigure(&decrypted);
// Area with highest area counter is the newest
u16 area_offset = ((decrypted[0x89] + 1U) != decrypted[0x249]) ? 0x80 : 0x240;
figure_data.trophy_data.unlocked_villains = *(decrypted.data() + area_offset + 0x14);
}
return figure_data;
}
void SkylanderFigure::SetData(FigureData* figure_data)
{
std::array<u8, FIGURE_SIZE> decrypted = {};
DecryptFigure(&decrypted);
if (figure_data->normalized_type == Type::Skylander)
{
// Only update area with lowest counter
u16 area_offset = (decrypted[0x89] != (decrypted[0x249] + 1U)) ? 0x80 : 0x240;
u16 other_area_offset = (area_offset == 0x80) ? 0x240 : 0x80;
memcpy(decrypted.data() + area_offset + 0x3, &figure_data->skylander_data.money, 2);
memcpy(decrypted.data() + area_offset + 0x5A, &figure_data->skylander_data.hero_level, 2);
memcpy(decrypted.data() + area_offset + 0x5, &figure_data->skylander_data.playtime, 4);
{
memcpy(decrypted.data() + area_offset + 0x60, &figure_data->skylander_data.last_reset.minute,
1);
memcpy(decrypted.data() + area_offset + 0x61, &figure_data->skylander_data.last_reset.hour,
1);
memcpy(decrypted.data() + area_offset + 0x62, &figure_data->skylander_data.last_reset.day, 1);
memcpy(decrypted.data() + area_offset + 0x63, &figure_data->skylander_data.last_reset.month,
1);
memcpy(decrypted.data() + area_offset + 0x64, &figure_data->skylander_data.last_reset.year,
2);
}
{
memcpy(decrypted.data() + area_offset + 0x50, &figure_data->skylander_data.last_placed.minute,
1);
memcpy(decrypted.data() + area_offset + 0x51, &figure_data->skylander_data.last_placed.hour,
1);
memcpy(decrypted.data() + area_offset + 0x52, &figure_data->skylander_data.last_placed.day,
1);
memcpy(decrypted.data() + area_offset + 0x53, &figure_data->skylander_data.last_placed.month,
1);
memcpy(decrypted.data() + area_offset + 0x54, &figure_data->skylander_data.last_placed.year,
2);
}
{
for (size_t i = 0; i < 8; ++i)
{
memcpy(decrypted.data() + area_offset + 0x20 + (i * 2),
&figure_data->skylander_data.nickname[i], 0x2);
}
for (size_t i = 0; i < 8; ++i)
{
memcpy(decrypted.data() + area_offset + 0x40 + (i * 2),
&figure_data->skylander_data.nickname[8 + i], 0x2);
}
}
{
ComputeChecksumType3(decrypted.data() + area_offset + 0x50,
decrypted.data() + area_offset + 0xA);
ComputeChecksumType2(decrypted.data() + area_offset + 0x10,
decrypted.data() + area_offset + 0xC);
decrypted[area_offset + 9] = decrypted[other_area_offset + 9] + 1;
ComputeChecksumType1(decrypted.data() + area_offset, decrypted.data() + area_offset + 0xE);
}
{
area_offset = (decrypted[0x112] != (decrypted[0x2D2] + 1U)) ? 0x110 : 0x2D0;
other_area_offset = (area_offset == 0x110) ? 0x2D0 : 0x110;
decrypted[area_offset + 2] = decrypted[other_area_offset + 2] + 1;
ComputeChecksumType6(decrypted.data() + area_offset, decrypted.data() + area_offset);
}
}
else if (figure_data->normalized_type == Type::Trophy)
{
u16 area_offset = (decrypted[0x89] != (decrypted[0x249] + 1U)) ? 0x80 : 0x240;
u16 other_area_offset = (area_offset == 0x80) ? 0x240 : 0x80;
{
memcpy(decrypted.data() + area_offset + 0x14, &figure_data->trophy_data.unlocked_villains, 1);
{
ComputeChecksumType3(decrypted.data() + area_offset + 0x50,
decrypted.data() + area_offset + 0xA);
ComputeChecksumType2(decrypted.data() + area_offset + 0x10,
decrypted.data() + area_offset + 0xC);
decrypted[area_offset + 9] = decrypted[other_area_offset + 9] + 1;
ComputeChecksumType1(decrypted.data() + area_offset, decrypted.data() + area_offset + 0xE);
}
}
{
area_offset = (decrypted[0x112] != (decrypted[0x2D2] + 1U)) ? 0x110 : 0x2D0;
other_area_offset = (area_offset == 0x110) ? 0x2D0 : 0x110;
decrypted[area_offset + 2] = decrypted[other_area_offset + 2] + 1;
ComputeChecksumType6(decrypted.data() + area_offset, decrypted.data() + area_offset);
}
}
// Encrypt again
Encrypt(decrypted);
Save();
}
void SkylanderFigure::DecryptFigure(std::array<u8, FIGURE_SIZE>* dest) const
{
std::array<u8, 0x56> hash_in = {};
GenerateIncompleteHashIn(hash_in.data());
std::array<u8, FIGURE_SIZE> decrypted = {};
std::array<u8, BLOCK_SIZE> current_block = {};
// Run for every block
for (u8 i = 0; i < BLOCK_COUNT; ++i)
{
GetBlock(i, current_block.data());
// Skip sector trailer and the first 8 blocks
if (((i + 1) % 4 == 0) || i < 8)
{
memcpy(decrypted.data() + (i * BLOCK_SIZE), current_block.data(), BLOCK_SIZE);
continue;
}
// Check if block is all 0
u16 total = 0;
for (size_t j = 0; j < BLOCK_SIZE; j++)
{
total += current_block[j];
}
if (total == 0)
{
continue;
}
// Block index
hash_in[0x20] = i;
std::array<u8, BLOCK_SIZE> hash_out = {};
mbedtls_md5_ret(hash_in.data(), 0x56, hash_out.data());
mbedtls_aes_context aes_context = {};
mbedtls_aes_setkey_dec(&aes_context, hash_out.data(), 128);
mbedtls_aes_crypt_ecb(&aes_context, MBEDTLS_AES_DECRYPT, current_block.data(),
decrypted.data() + (i * BLOCK_SIZE));
}
memcpy(dest->data(), decrypted.data(), FIGURE_SIZE);
DEBUG_LOG_FMT(IOS_USB, "Decrypted skylander data: \n{}", HexDump(decrypted.data(), FIGURE_SIZE));
}
void SkylanderFigure::Close()
{
m_sky_file.Close();
}
void SkylanderFigure::SetBlock(u8 block, const u8* buf)
{
memcpy(m_data.data() + (block * BLOCK_SIZE), buf, BLOCK_SIZE);
}
bool SkylanderFigure::FileIsOpen() const
{
return m_sky_file.IsOpen();
}
} // namespace IOS::HLE::USB

View File

@ -0,0 +1,85 @@
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <optional>
#include <span>
#include <string>
#include "Common/CommonTypes.h"
#include "Common/IOFile.h"
namespace IOS::HLE::USB
{
enum class Type : u8;
struct SkylanderDateTime final
{
u8 minute;
u8 hour;
u8 day;
u8 month;
u16 year;
};
struct SkylanderData final
{
std::array<u8, 11> toy_code;
u16 money;
u16 hero_level;
u32 playtime;
// Null-terminated UTF-16 string
std::array<u16, 0x10> nickname;
SkylanderDateTime last_reset;
SkylanderDateTime last_placed;
};
struct Trophydata final
{
u8 unlocked_villains;
};
struct FigureData final
{
Type normalized_type;
u16 figure_id;
u16 variant_id;
union
{
SkylanderData skylander_data;
Trophydata trophy_data;
};
};
constexpr u32 BLOCK_COUNT = 0x40;
constexpr u32 BLOCK_SIZE = 0x10;
constexpr u32 FIGURE_SIZE = BLOCK_COUNT * BLOCK_SIZE;
class SkylanderFigure
{
public:
SkylanderFigure(const std::string& file_path);
SkylanderFigure(File::IOFile file);
bool Create(u16 sky_id, u16 sky_var,
std::optional<std::array<u8, 4>> requested_nuid = std::nullopt);
void Save();
void Close();
bool FileIsOpen() const;
void GetBlock(u8 index, u8* dest) const;
void SetBlock(u8 block, const u8* buf);
void DecryptFigure(std::array<u8, FIGURE_SIZE>* dest) const;
FigureData GetData() const;
void SetData(FigureData* data);
private:
void PopulateSectorTrailers();
void PopulateKeys();
void GenerateIncompleteHashIn(u8* dest) const;
void Encrypt(std::span<const u8, FIGURE_SIZE>);
File::IOFile m_sky_file;
std::array<u8, FIGURE_SIZE> m_data;
};
} // namespace IOS::HLE::USB

View File

@ -23,7 +23,7 @@
#include "Core/Core.h"
#include "Core/IOS/USB/Common.h"
#include "Core/IOS/USB/Emulated/Infinity.h"
#include "Core/IOS/USB/Emulated/Skylander.h"
#include "Core/IOS/USB/Emulated/Skylanders/Skylander.h"
#include "Core/IOS/USB/LibusbDevice.h"
#include "Core/NetPlayProto.h"
#include "Core/System.h"

View File

@ -26,7 +26,7 @@
#include "Core/PowerPC/JitInterface.h"
#include "Core/PowerPC/PowerPC.h"
#include "IOS/USB/Emulated/Infinity.h"
#include "IOS/USB/Emulated/Skylander.h"
#include "IOS/USB/Emulated/Skylanders/Skylander.h"
#include "VideoCommon/Assets/CustomAssetLoader.h"
#include "VideoCommon/CommandProcessor.h"
#include "VideoCommon/Fifo.h"