if (CheckRegion(SYS_NAND) == 0)
{
info.drive = SYS_NAND;
info.tidLo = 0x00040010;
info.tidHi = titleid_high[region];
if (FindApp(&info)) // SysNAND only
{
if (FileOpen(&dg, info.tmd, 0))
{
/* Get the MSET TMD version */
unsigned short tmd_ver;
FileRead(&dg, &tmd_ver, 2, 0x1DC);
tmd_ver = bswap_16(tmd_ver);
FileClose(&dg);
/* Verify version number */
if (tmd_ver != mset_ver[mset_dg_ver])
{
/* Open MSET content file */
if (FileOpen(&dg, info.content, 0))
{
unsigned int check_val;
FileRead(&dg, &check_val, 4, 0x130);
FileClose(&dg);
if (check_val != 0)
{
if (checkDgFile(dgpath, mset_hash[mset_dg_ver]))
{
print(strings[STR_PROCESSING], strings[STR_DOWNGRADE_PACK]);
ConsoleShow();
if (FileOpen(&dg, dgpath, 0))
{
unsigned int dgsize = FileGetSize(&dg);
unsigned char *buf = (unsigned char*)0x21000000;
FileRead(&dg, buf, dgsize, 0);
/* Downgrade pack decryption */
uint8_t iv[0x10] = {0};
uint8_t Key[0x10] = {0};
GetTitleKey(&Key[0], info.tidLo, info.tidHi, info.drive);
aes_context aes_ctxt;
aes_setkey_dec(&aes_ctxt, Key, 0x80);
aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, dgsize, iv, buf, buf);
FileWrite(&dg, buf, dgsize, 0);
FileClose(&dg);
if (*((unsigned int*)(buf + 0x100)) == 0x4843434E) // "NCCH" magic word
{
print(strings[STR_DOWNGRADING], L"");
ConsoleShow();
if (FSFileCopy(info.content, dgpath) != 0)
{
print(strings[STR_FAILED]);
}
print(strings[STR_DELETING], dgpath);
ConsoleShow();
f_unlink(dgpath);
} else {
print(strings[STR_WRONG], "", strings[STR_DOWNGRADE_PACK]);
}
} else {
print(strings[STR_ERROR_OPENING], dgpath);
}
} else {
print(strings[STR_WRONG], "", strings[STR_DOWNGRADE_PACK]);
}
} else {
print(strings[STR_DOWNGRADING_NOT_NEEDED], strings[STR_MSET]);
}
} else {
print(strings[STR_ERROR_OPENING], info.content);
}
} else {
print(strings[STR_DOWNGRADING_NOT_NEEDED], strings[STR_MSET]);
}
} else {
print(strings[STR_ERROR_OPENING], info.tmd);
}
} else {
print(strings[STR_MISSING], strings[STR_MSET]);
}
}