early-access version 3446

This commit is contained in:
pineappleEA 2023-03-09 19:11:49 +01:00
parent a2ce0fc4b7
commit 3100016035
5 changed files with 167 additions and 32 deletions

View file

@ -1,7 +1,7 @@
yuzu emulator early access yuzu emulator early access
============= =============
This is the source code for early-access 3445. This is the source code for early-access 3446.
## Legal Notice ## Legal Notice

View file

@ -80,13 +80,16 @@ NTAG215File NfcDataToEncodedData(const EncryptedNTAG215File& nfc_data) {
encoded_data.hmac_data = nfc_data.user_memory.hmac_data; encoded_data.hmac_data = nfc_data.user_memory.hmac_data;
encoded_data.constant_value = nfc_data.user_memory.constant_value; encoded_data.constant_value = nfc_data.user_memory.constant_value;
encoded_data.write_counter = nfc_data.user_memory.write_counter; encoded_data.write_counter = nfc_data.user_memory.write_counter;
encoded_data.amiibo_version = nfc_data.user_memory.amiibo_version;
encoded_data.settings = nfc_data.user_memory.settings; encoded_data.settings = nfc_data.user_memory.settings;
encoded_data.owner_mii = nfc_data.user_memory.owner_mii; encoded_data.owner_mii = nfc_data.user_memory.owner_mii;
encoded_data.title_id = nfc_data.user_memory.title_id; encoded_data.application_id = nfc_data.user_memory.application_id;
encoded_data.applicaton_write_counter = nfc_data.user_memory.applicaton_write_counter; encoded_data.application_write_counter = nfc_data.user_memory.application_write_counter;
encoded_data.application_area_id = nfc_data.user_memory.application_area_id; encoded_data.application_area_id = nfc_data.user_memory.application_area_id;
encoded_data.application_id_byte = nfc_data.user_memory.application_id_byte;
encoded_data.unknown = nfc_data.user_memory.unknown; encoded_data.unknown = nfc_data.user_memory.unknown;
encoded_data.unknown2 = nfc_data.user_memory.unknown2; encoded_data.unknown2 = nfc_data.user_memory.unknown2;
encoded_data.application_area_crc = nfc_data.user_memory.application_area_crc;
encoded_data.application_area = nfc_data.user_memory.application_area; encoded_data.application_area = nfc_data.user_memory.application_area;
encoded_data.hmac_tag = nfc_data.user_memory.hmac_tag; encoded_data.hmac_tag = nfc_data.user_memory.hmac_tag;
encoded_data.lock_bytes = nfc_data.uuid.lock_bytes; encoded_data.lock_bytes = nfc_data.uuid.lock_bytes;
@ -111,13 +114,16 @@ EncryptedNTAG215File EncodedDataToNfcData(const NTAG215File& encoded_data) {
nfc_data.user_memory.hmac_data = encoded_data.hmac_data; nfc_data.user_memory.hmac_data = encoded_data.hmac_data;
nfc_data.user_memory.constant_value = encoded_data.constant_value; nfc_data.user_memory.constant_value = encoded_data.constant_value;
nfc_data.user_memory.write_counter = encoded_data.write_counter; nfc_data.user_memory.write_counter = encoded_data.write_counter;
nfc_data.user_memory.amiibo_version = encoded_data.amiibo_version;
nfc_data.user_memory.settings = encoded_data.settings; nfc_data.user_memory.settings = encoded_data.settings;
nfc_data.user_memory.owner_mii = encoded_data.owner_mii; nfc_data.user_memory.owner_mii = encoded_data.owner_mii;
nfc_data.user_memory.title_id = encoded_data.title_id; nfc_data.user_memory.application_id = encoded_data.application_id;
nfc_data.user_memory.applicaton_write_counter = encoded_data.applicaton_write_counter; nfc_data.user_memory.application_write_counter = encoded_data.application_write_counter;
nfc_data.user_memory.application_area_id = encoded_data.application_area_id; nfc_data.user_memory.application_area_id = encoded_data.application_area_id;
nfc_data.user_memory.application_id_byte = encoded_data.application_id_byte;
nfc_data.user_memory.unknown = encoded_data.unknown; nfc_data.user_memory.unknown = encoded_data.unknown;
nfc_data.user_memory.unknown2 = encoded_data.unknown2; nfc_data.user_memory.unknown2 = encoded_data.unknown2;
nfc_data.user_memory.application_area_crc = encoded_data.application_area_crc;
nfc_data.user_memory.application_area = encoded_data.application_area; nfc_data.user_memory.application_area = encoded_data.application_area;
nfc_data.user_memory.hmac_tag = encoded_data.hmac_tag; nfc_data.user_memory.hmac_tag = encoded_data.hmac_tag;
nfc_data.user_memory.model_info = encoded_data.model_info; nfc_data.user_memory.model_info = encoded_data.model_info;

View file

@ -174,8 +174,8 @@ Result NfpDevice::StopDetection() {
if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) { if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) {
CloseAmiibo(); CloseAmiibo();
return ResultSuccess;
} }
if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) { if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) {
device_state = DeviceState::Initialized; device_state = DeviceState::Initialized;
return ResultSuccess; return ResultSuccess;
@ -204,9 +204,7 @@ Result NfpDevice::Flush() {
const auto& current_date = GetAmiiboDate(current_posix_time); const auto& current_date = GetAmiiboDate(current_posix_time);
if (settings.write_date.raw_date != current_date.raw_date) { if (settings.write_date.raw_date != current_date.raw_date) {
settings.write_date = current_date; settings.write_date = current_date;
settings.crc_counter++; UpdateSettingsCrc();
// TODO: Find how to calculate the crc check
// settings.crc = CalculateCRC(settings);
} }
tag_data.write_counter++; tag_data.write_counter++;
@ -318,7 +316,7 @@ Result NfpDevice::GetCommonInfo(CommonInfo& common_info) const {
common_info = { common_info = {
.last_write_date = settings.write_date.GetWriteDate(), .last_write_date = settings.write_date.GetWriteDate(),
.write_counter = tag_data.write_counter, .write_counter = tag_data.write_counter,
.version = 0, .version = tag_data.amiibo_version,
.application_area_size = sizeof(ApplicationArea), .application_area_size = sizeof(ApplicationArea),
}; };
return ResultSuccess; return ResultSuccess;
@ -370,7 +368,57 @@ Result NfpDevice::GetRegisterInfo(RegisterInfo& register_info) const {
.mii_char_info = manager.ConvertV3ToCharInfo(tag_data.owner_mii), .mii_char_info = manager.ConvertV3ToCharInfo(tag_data.owner_mii),
.creation_date = settings.init_date.GetWriteDate(), .creation_date = settings.init_date.GetWriteDate(),
.amiibo_name = GetAmiiboName(settings), .amiibo_name = GetAmiiboName(settings),
.font_region = {}, .font_region = settings.settings.font_region,
};
return ResultSuccess;
}
Result NfpDevice::GetAdminInfo(AdminInfo& admin_info) const {
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFC, "Wrong device state {}", device_state);
if (device_state == DeviceState::TagRemoved) {
return TagRemoved;
}
return WrongDeviceState;
}
if (mount_target == MountTarget::None || mount_target == MountTarget::Rom) {
LOG_ERROR(Service_NFC, "Amiibo is read only", device_state);
return WrongDeviceState;
}
u8 flags = static_cast<u8>(tag_data.settings.settings.raw >> 0x4);
if (tag_data.settings.settings.amiibo_initialized == 0) {
flags = flags & 0xfe;
}
u64 application_id = 0;
u32 application_area_id = 0;
AppAreaVersion app_area_version = AppAreaVersion::NotSet;
if (tag_data.settings.settings.appdata_initialized != 0) {
application_id = tag_data.application_id;
app_area_version =
static_cast<AppAreaVersion>(application_id >> application_id_version_offset & 0xf);
// Restore application id to original value
if (application_id >> 0x38 != 0) {
const u8 application_byte = tag_data.application_id_byte & 0xf;
application_id &= ~(0xfULL << application_id_version_offset);
application_id |= static_cast<u64>(application_byte) << application_id_version_offset;
}
application_area_id = tag_data.application_area_id;
}
// TODO: Validate this data
admin_info = {
.application_id = application_id,
.application_area_id = application_area_id,
.crc_change_counter = tag_data.settings.crc_counter,
.flags = flags,
.tag_type = PackedTagType::Type2,
.app_area_version = app_area_version,
}; };
return ResultSuccess; return ResultSuccess;
@ -395,9 +443,7 @@ Result NfpDevice::SetNicknameAndOwner(const AmiiboName& amiibo_name) {
settings.init_date = GetAmiiboDate(current_posix_time); settings.init_date = GetAmiiboDate(current_posix_time);
settings.write_date = GetAmiiboDate(current_posix_time); settings.write_date = GetAmiiboDate(current_posix_time);
settings.crc_counter++; UpdateSettingsCrc();
// TODO: Find how to calculate the crc check
// settings.crc = CalculateCRC(settings);
SetAmiiboName(settings, amiibo_name); SetAmiiboName(settings, amiibo_name);
tag_data.owner_mii = manager.ConvertCharInfoToV3(manager.BuildDefault(0)); tag_data.owner_mii = manager.ConvertCharInfoToV3(manager.BuildDefault(0));
@ -569,7 +615,10 @@ Result NfpDevice::SetApplicationArea(std::span<const u8> data) {
rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(), rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(),
sizeof(ApplicationArea) - data.size()); sizeof(ApplicationArea) - data.size());
tag_data.applicaton_write_counter++; if (tag_data.application_write_counter != counter_limit) {
tag_data.application_write_counter++;
}
is_data_moddified = true; is_data_moddified = true;
return ResultSuccess; return ResultSuccess;
@ -617,14 +666,27 @@ Result NfpDevice::RecreateApplicationArea(u32 access_id, std::span<const u8> dat
rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(), rng.GenerateRandomBytes(tag_data.application_area.data() + data.size(),
sizeof(ApplicationArea) - data.size()); sizeof(ApplicationArea) - data.size());
// TODO: Investigate why the title id needs to be moddified if (tag_data.application_write_counter != counter_limit) {
tag_data.title_id = system.GetApplicationProcessProgramID(); tag_data.application_write_counter++;
tag_data.title_id = tag_data.title_id | 0x30000000ULL; }
const u64 application_id = system.GetApplicationProcessProgramID();
const u64 application_id_without_version_byte =
application_id & ~(0xfULL << application_id_version_offset);
tag_data.application_id_byte =
static_cast<u8>(application_id >> application_id_version_offset & 0xf);
tag_data.application_id =
application_id_without_version_byte |
(static_cast<u64>(AppAreaVersion::NintendoSwitch) << application_id_version_offset);
tag_data.settings.settings.appdata_initialized.Assign(1); tag_data.settings.settings.appdata_initialized.Assign(1);
tag_data.application_area_id = access_id; tag_data.application_area_id = access_id;
tag_data.applicaton_write_counter++;
tag_data.unknown = {}; tag_data.unknown = {};
// TODO: this is a mix of tag_data input
std::array<u8, 0x7e> unknown_input{};
tag_data.application_area_crc = CalculateCrc(unknown_input);
return Flush(); return Flush();
} }
@ -644,10 +706,10 @@ Result NfpDevice::DeleteApplicationArea() {
Common::TinyMT rng{}; Common::TinyMT rng{};
rng.GenerateRandomBytes(tag_data.application_area.data(), sizeof(ApplicationArea)); rng.GenerateRandomBytes(tag_data.application_area.data(), sizeof(ApplicationArea));
rng.GenerateRandomBytes(&tag_data.title_id, sizeof(u64)); rng.GenerateRandomBytes(&tag_data.application_id, sizeof(u64));
rng.GenerateRandomBytes(&tag_data.application_area_id, sizeof(u32)); rng.GenerateRandomBytes(&tag_data.application_area_id, sizeof(u32));
tag_data.settings.settings.appdata_initialized.Assign(0); tag_data.settings.settings.appdata_initialized.Assign(0);
tag_data.applicaton_write_counter++; tag_data.application_write_counter++;
tag_data.unknown = {}; tag_data.unknown = {};
return Flush(); return Flush();
@ -719,4 +781,41 @@ AmiiboDate NfpDevice::GetAmiiboDate(s64 posix_time) const {
return amiibo_date; return amiibo_date;
} }
void NfpDevice::UpdateSettingsCrc() {
auto& settings = tag_data.settings;
if (settings.crc_counter != counter_limit) {
settings.crc_counter++;
}
// TODO: this reads data from a global, find what it is
std::array<u8, 8> unknown_input{};
settings.crc = CalculateCrc(unknown_input);
}
u32 NfpDevice::CalculateCrc(std::span<u8> data) {
constexpr u32 magic = 0xedb88320;
u32 crc = 0xffffffff;
if (data.size() == 0) {
return 0;
}
for (u8 input : data) {
u32 temp = (crc ^ input) >> 1;
if (((crc ^ input) & 1) != 0) {
temp = temp ^ magic;
}
for (std::size_t step = 0; step < 7; ++step) {
crc = temp >> 1;
if ((temp & 1) != 0) {
crc = temp >> 1 ^ magic;
}
}
}
return ~crc;
}
} // namespace Service::NFP } // namespace Service::NFP

View file

@ -47,6 +47,7 @@ public:
Result GetCommonInfo(CommonInfo& common_info) const; Result GetCommonInfo(CommonInfo& common_info) const;
Result GetModelInfo(ModelInfo& model_info) const; Result GetModelInfo(ModelInfo& model_info) const;
Result GetRegisterInfo(RegisterInfo& register_info) const; Result GetRegisterInfo(RegisterInfo& register_info) const;
Result GetAdminInfo(AdminInfo& admin_info) const;
Result SetNicknameAndOwner(const AmiiboName& amiibo_name); Result SetNicknameAndOwner(const AmiiboName& amiibo_name);
Result RestoreAmiibo(); Result RestoreAmiibo();
@ -76,6 +77,8 @@ private:
AmiiboName GetAmiiboName(const AmiiboSettings& settings) const; AmiiboName GetAmiiboName(const AmiiboSettings& settings) const;
void SetAmiiboName(AmiiboSettings& settings, const AmiiboName& amiibo_name); void SetAmiiboName(AmiiboSettings& settings, const AmiiboName& amiibo_name);
AmiiboDate GetAmiiboDate(s64 posix_time) const; AmiiboDate GetAmiiboDate(s64 posix_time) const;
void UpdateSettingsCrc();
u32 CalculateCrc(std::span<u8> data);
bool is_controller_set{}; bool is_controller_set{};
int callback_key; int callback_key;

View file

@ -10,6 +10,8 @@
namespace Service::NFP { namespace Service::NFP {
static constexpr std::size_t amiibo_name_length = 0xA; static constexpr std::size_t amiibo_name_length = 0xA;
static constexpr std::size_t application_id_version_offset = 0x1c;
static constexpr std::size_t counter_limit = 0xffff;
enum class ServiceType : u32 { enum class ServiceType : u32 {
User, User,
@ -99,6 +101,14 @@ enum class TagProtocol : u32 {
All = 0xFFFFFFFFU, All = 0xFFFFFFFFU,
}; };
enum class AppAreaVersion : u8 {
Nintendo3DS = 0,
NintendoWiiU = 1,
Nintendo3DSv2 = 2,
NintendoSwitch = 3,
NotSet = 0xFF,
};
enum class CabinetMode : u8 { enum class CabinetMode : u8 {
StartNicknameAndOwnerSettings, StartNicknameAndOwnerSettings,
StartGameDataEraser, StartGameDataEraser,
@ -197,6 +207,7 @@ struct Settings {
union { union {
u8 raw{}; u8 raw{};
BitField<0, 4, u8> font_region;
BitField<4, 1, u8> amiibo_initialized; BitField<4, 1, u8> amiibo_initialized;
BitField<5, 1, u8> appdata_initialized; BitField<5, 1, u8> appdata_initialized;
}; };
@ -236,18 +247,20 @@ static_assert(sizeof(NTAG215Password) == 0x8, "NTAG215Password is an invalid siz
struct EncryptedAmiiboFile { struct EncryptedAmiiboFile {
u8 constant_value; // Must be A5 u8 constant_value; // Must be A5
u16_be write_counter; // Number of times the amiibo has been written? u16_be write_counter; // Number of times the amiibo has been written?
INSERT_PADDING_BYTES(0x1); // Unknown 1 u8 amiibo_version; // Amiibo file version
AmiiboSettings settings; // Encrypted amiibo settings AmiiboSettings settings; // Encrypted amiibo settings
HashData hmac_tag; // Hash HashData hmac_tag; // Hash
AmiiboModelInfo model_info; // Encrypted amiibo model info AmiiboModelInfo model_info; // Encrypted amiibo model info
HashData keygen_salt; // Salt HashData keygen_salt; // Salt
HashData hmac_data; // Hash HashData hmac_data; // Hash
Service::Mii::Ver3StoreData owner_mii; // Encrypted Mii data Service::Mii::Ver3StoreData owner_mii; // Encrypted Mii data
u64_be title_id; // Encrypted Game id u64_be application_id; // Encrypted Game id
u16_be applicaton_write_counter; // Encrypted Counter u16_be application_write_counter; // Encrypted Counter
u32_be application_area_id; // Encrypted Game id u32_be application_area_id; // Encrypted Game id
std::array<u8, 0x2> unknown; u8 application_id_byte;
std::array<u32, 0x8> unknown2; u8 unknown;
std::array<u32, 0x7> unknown2;
u32_be application_area_crc;
ApplicationArea application_area; // Encrypted Game data ApplicationArea application_area; // Encrypted Game data
}; };
static_assert(sizeof(EncryptedAmiiboFile) == 0x1F8, "AmiiboFile is an invalid size"); static_assert(sizeof(EncryptedAmiiboFile) == 0x1F8, "AmiiboFile is an invalid size");
@ -259,14 +272,16 @@ struct NTAG215File {
HashData hmac_data; // Hash HashData hmac_data; // Hash
u8 constant_value; // Must be A5 u8 constant_value; // Must be A5
u16_be write_counter; // Number of times the amiibo has been written? u16_be write_counter; // Number of times the amiibo has been written?
INSERT_PADDING_BYTES(0x1); // Unknown 1 u8 amiibo_version; // Amiibo file version
AmiiboSettings settings; AmiiboSettings settings;
Service::Mii::Ver3StoreData owner_mii; // Encrypted Mii data Service::Mii::Ver3StoreData owner_mii; // Mii data
u64_be title_id; u64_be application_id; // Game id
u16_be applicaton_write_counter; // Encrypted Counter u16_be application_write_counter; // Counter
u32_be application_area_id; u32_be application_area_id;
std::array<u8, 0x2> unknown; u8 application_id_byte;
std::array<u32, 0x8> unknown2; u8 unknown;
std::array<u32, 0x7> unknown2;
u32_be application_area_crc;
ApplicationArea application_area; // Encrypted Game data ApplicationArea application_area; // Encrypted Game data
HashData hmac_tag; // Hash HashData hmac_tag; // Hash
UniqueSerialNumber uid; // Unique serial number UniqueSerialNumber uid; // Unique serial number
@ -336,6 +351,18 @@ struct RegisterInfo {
}; };
static_assert(sizeof(RegisterInfo) == 0x100, "RegisterInfo is an invalid size"); static_assert(sizeof(RegisterInfo) == 0x100, "RegisterInfo is an invalid size");
struct AdminInfo {
u64 application_id;
u32 application_area_id;
u16 crc_change_counter;
u8 flags;
PackedTagType tag_type;
AppAreaVersion app_area_version;
INSERT_PADDING_BYTES(0x7);
INSERT_PADDING_BYTES(0x28);
};
static_assert(sizeof(AdminInfo) == 0x40, "AdminInfo is an invalid size");
struct SectorKey { struct SectorKey {
MifareCmd command; MifareCmd command;
u8 unknown; // Usually 1 u8 unknown; // Usually 1