ctr_Repair/trunk/ConsoleDataMigration/sources/common/Util.cpp
N2614 80f6c3a8f7 RTCの表示を止める
cciのみビルドするように

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@285 385bec56-5757-e545-9c3a-d8741f4650f1
2011-05-26 04:38:44 +00:00

317 lines
7.6 KiB
C++

/*---------------------------------------------------------------------------*
Project: Horizon
File: Util.cpp
Copyright 2009 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.
$Rev$
*---------------------------------------------------------------------------*/
#include <cctype>
#include <nn.h>
#include <nn/ptm/CTR/ptm_ApiSysmenu.h>
#include <nn/fs/CTR/fs_ArchiveTypesForSystem.h>
#include <nn/fs/CTR/MPCore/fs_FileSystemBasePrivate.h>
#include <nn/fs/fs_ApiSysSaveData.h>
#include <nn/cfg/CTR/cfg_Api.h>
#include <nn/cfg/CTR/cfg_ApiInit.h>
#include <nn/cfg/CTR/cfg_ApiSys.h>
#include <nn/friends.h>
#include <nn/friends/CTR/friends_ApiPrivate.h>
#include <nn/ps.h>
#include "Util.h"
#include "FileName.h"
#include "CommonLogger.h"
#include "HeapManager.h"
namespace common
{
Util::Util() :
m_BatteryRemain(100), m_CanReadSerialNumber(false), m_CanReadIvs(false)
{
}
Util::~Util()
{
}
void Util::Initialize()
{
// friendsの初期化
nn::Result result = nn::friends::detail::Initialize();
// mcuの初期化
nn::mcu::CTR::InitializeHwCheck(&m_McuSession);
mp_Mcu = new nn::mcu::CTR::HwCheck(m_McuSession);
// 完全性検証SEEDの取得
result = nn::fs::MountSpecialArchive(common::NAND_ARCHIVE_NAME, nn::fs::CTR::ARCHIVE_TYPE_CTR_NAND);
if (result.IsSuccess())
{
nn::fs::FileInputStream fis;
result = fis.TryInitialize(common::IVS_NAND_PATHNAME);
if (result.IsSuccess())
{
s64 fileSize = fis.GetSize();
s32 ret;
void* addr = NULL;
addr = common::HeapManager::GetHeap()->Allocate(fileSize);
if (addr != NULL)
{
mp_Ivs = addr;
m_SizeofIvs = fileSize;
result = fis.TryRead(&ret, addr, fileSize);
if (result.IsSuccess())
{
m_CanReadIvs = true;
}
// 後でIVSを参照するのでFreeしない
}
}
fis.Finalize();
}
// 一旦アンマウントしておく
nn::fs::Unmount(common::NAND_ARCHIVE_NAME);
// シリアルナンバーの取得
std::memset(m_SerialNo, '\0',
nn::cfg::CTR::CFG_SECURE_INFO_SERIAL_NO_LEN);
result = nn::cfg::CTR::system::GetSerialNo(m_SerialNo);
if(result.IsSuccess())
{
m_CanReadSerialNumber = true;
}
COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result);
AddCheckDigit(reinterpret_cast<char*>(m_SerialNo));
// デバイスIDの取得
result = nn::ps::CTR::GetDeviceId(&m_DeviceId);
COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result);
// リージョンの取得
m_Region = nn::cfg::CTR::GetRegion();
// バージョンの取得
common::GetSystemVersion(&m_VerData, m_Region);
// MACアドレスの取得
nn::nwm::Mac mac;
result = nn::nwm::GetMacAddress(mac);
COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result);
mac.GetString(m_MacAddress);
COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result);
// フレンドコードの取得
nn::friends::CTR::FriendKey friendKey;
result = nn::friends::CTR::GetMyFriendKey(&friendKey);
COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result);
m_FriendCode = nn::friends::CTR::FriendKeyToFriendCode(friendKey);
}
void Util::Finalize()
{
nn::mcu::CTR::FinalizeHwCheck(&m_McuSession);
nn::friends::detail::Finalize();
}
// NULL終端されたシリアルナンバーを受け取る
// NULL終端された場所にチェックデジットを付加して新たにNULL終端する
void Util::AddCheckDigit(char* serial)
{
size_t len = std::strlen(serial);
u8 digit = 0;
bool odd = true;
for(u8 i = len - 1; i > 0 && std::isdigit(serial[i]); i--)
{
if(odd)
{
digit += (serial[i] - '0') * 3;
}
else
{
digit += (serial[i] - '0');
}
odd = !odd;
}
if(digit % 10 != 0)
{
serial[len] = 10 - (digit % 10) + '0';
}
else
{
serial[len] = '0';
}
serial[len + 1] = '\0';
}
// IVSからセーブデータディレクトリ名を生成する
void Util::GetSaveDataDirectoryRoot(::std::string& sysSaveRoot, void* ivs, size_t size)
{
nn::Result result;
using namespace nn::dbg;
const size_t SEED_SIZE = 16;
bit8 hash[nn::crypto::Sha256Context::HASH_SIZE];
const size_t SYS_SAVE_ROOT_LENGTH = 16;
char rootHash[SYS_SAVE_ROOT_LENGTH];
char rootStr[SYS_SAVE_ROOT_LENGTH * 2 + 1];
// 最後の16バイトのハッシュを使う
nn::crypto::CalculateSha256(hash, &reinterpret_cast<bit8*> (ivs)[size - SEED_SIZE], SEED_SIZE);
for (u8 i = 0; i < SEED_SIZE / 4; i++)
{
for (u8 j = 0; j < SEED_SIZE / 4; j++)
{
rootHash[i * 4 + j] = hash[i * 4 + 3 - j];
}
}
// 得られたハッシュから文字列を生成
for (s32 k = 0; k < SEED_SIZE; k++)
{
for (s32 i = 6; i < 8; ++i)
{
bit32 n = (rootHash[k] >> ((7 - i) * 4)) & 0xf;
NN_TASSERT_(n < 16);
rootStr[i - 6 + k * 2] = static_cast<char> (n < 10 ? '0' + n : 'a' + (n - 10));
}
}
rootStr[SYS_SAVE_ROOT_LENGTH * 2] = '\0';
// セーブデータディレクトリ名を保存する
sysSaveRoot = ::std::string(rootStr);
NN_LOG("%s\n", sysSaveRoot.c_str());
}
bool Util::IsAdapterConnected()
{
return nn::ptm::CTR::GetAdapterState() == nn::ptm::CTR::ADAPTERSTATE_CONNECTED;
}
bool Util::IsBatteryLower()
{
m_BatteryRemain = GetBatteryRemain();
return m_BatteryRemain <= 10;
}
bool Util::CanReadIVS()
{
return m_CanReadIvs;
}
bool Util::CanReadSerialNumber()
{
return m_CanReadSerialNumber;
}
void Util::GetSerialNumber(u8** serial, size_t* size)
{
*serial = m_SerialNo;
*size = nn::cfg::CTR::CFG_SECURE_INFO_SERIAL_NO_LEN;
}
u8* Util::GetSerialNumber()
{
return m_SerialNo;
}
void Util::GetIvs(void** ivs, size_t* size)
{
*ivs = mp_Ivs;
*size = m_SizeofIvs;
}
bit32 Util::GetDeviceId()
{
return m_DeviceId;
}
u8 Util::GetCupMajorVersion()
{
return m_VerData.cup.majorVersion;
}
u8 Util::GetCupMinorVersion()
{
return m_VerData.cup.minorVersion;
}
u8 Util::GetCupMicroVersion()
{
return m_VerData.cup.microVersion;
}
u8 Util::GetNupVersion()
{
return m_VerData.nup.majorVersion;
}
nn::Handle Util::GetMcuHandle()
{
return m_McuSession;
}
u32 Util::GetBatteryRemain()
{
u8 remain;
mp_Mcu->GetBatteryRemain(&remain);
return remain;
}
u64 Util::GetInfraDeviceId()
{
bit64 infraDeviceId;
infraDeviceId = m_DeviceId + common::INFRA_DEVICE_ID_OFFSET;
return infraDeviceId;
}
u64 Util::GetFriendcode()
{
return m_FriendCode;
}
char8* Util::GetMacAddress()
{
return m_MacAddress;
}
nn::cfg::CTR::CfgRegionCode Util::GetRegion()
{
return m_Region;
}
const char* Util::GetRegionCodeA3()
{
return nn::cfg::GetRegionCodeA3(m_Region);
}
void Util::GetVersionData(common::VerDef* version)
{
*version = m_VerData;
}
}