Any-Region-Changer-ModMii-E.../source/detect_settings.c

273 lines
6.3 KiB
C
Raw Normal View History

2022-04-21 02:12:38 +02:00
/*-------------------------------------------------------------
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
detect_settings.c -- detects various system settings
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
Copyright (C) 2008 tona
Unless other credit specified
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
1.The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
2.Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
Hi i made some changes
- thepikachugamer
2022-04-21 02:12:38 +02:00
3.This notice may not be removed or altered from any source
distribution.
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
-------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <gccore.h>
#include "detect_settings.h"
2023-12-20 18:52:13 +01:00
#include "wiibasics.h"
2022-04-21 02:12:38 +02:00
2023-12-20 18:52:13 +01:00
/* (gdb) print (unsigned short)-1
* $1 = 65535
* This is a valid version number */
// u16 get_installed_title_version(u64 title)
s32 get_installed_title_version(u64 title)
2022-04-21 03:38:39 +02:00
{
2022-04-21 02:12:38 +02:00
s32 ret, fd;
2023-12-20 18:52:13 +01:00
static char filepath[ISFS_MAXPATH] ATTRIBUTE_ALIGN(32);
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
// Check to see if title exists
2022-04-21 03:38:39 +02:00
if (ES_GetDataDir(title, filepath) >= 0)
{
2022-04-21 02:12:38 +02:00
u32 tmd_size;
static u8 tmd_buf[MAX_SIGNED_TMD_SIZE] ATTRIBUTE_ALIGN(32);
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ES_GetStoredTMDSize(title, &tmd_size);
2022-04-21 03:38:39 +02:00
if (ret < 0)
{
2022-04-21 02:12:38 +02:00
// If we fail to use the ES function, try reading manually
// This is a workaround added since some IOS (like 21) don't like our
// call to ES_GetStoredTMDSize
2022-04-21 03:38:39 +02:00
// printf("Error! ES_GetStoredTMDSize: %d\n", ret);
sprintf(filepath, "/title/%08x/%08x/content/title.tmd", TITLE_UPPER(title), TITLE_LOWER(title));
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
fd = ret = ISFS_Open(filepath, ISFS_OPEN_READ);
if (ret < 0)
2022-04-21 02:12:38 +02:00
{
printf("Error! ISFS_Open (ret = %d)\n", ret);
2023-12-20 18:52:13 +01:00
return ret;
2022-04-21 02:12:38 +02:00
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ISFS_Seek(fd, 0x1dc, 0);
if (ret < 0)
{
printf("Error! ISFS_Seek (ret = %d)\n", ret);
2023-12-20 18:52:13 +01:00
return ret;
2022-04-21 02:12:38 +02:00
}
2022-04-21 03:38:39 +02:00
ret = ISFS_Read(fd, tmd_buf, 2);
2023-12-20 18:52:13 +01:00
ISFS_Close(fd);
2022-04-21 02:12:38 +02:00
if (ret < 0)
{
printf("Error! ISFS_Read (ret = %d)\n", ret);
2023-12-20 18:52:13 +01:00
return ret;
2022-04-21 02:12:38 +02:00
}
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
return *(u16*)tmd_buf;
2022-04-21 03:38:39 +02:00
}
else
{
2022-04-21 02:12:38 +02:00
// Normal versions of IOS won't have a problem, so we do things the "right" way.
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
// Some of this code adapted from bushing's title_lister.c
signed_blob *s_tmd = (signed_blob *)tmd_buf;
ret = ES_GetStoredTMD(title, s_tmd, tmd_size);
2022-04-21 03:38:39 +02:00
if (ret < 0)
{
printf("Error! ES_GetStoredTMD: %d\n", ret);
2023-12-20 18:52:13 +01:00
// return -1;
return ret;
2022-04-21 02:12:38 +02:00
}
tmd *t = SIGNATURE_PAYLOAD(s_tmd);
return t->title_version;
}
2022-04-21 03:38:39 +02:00
}
2023-12-20 18:52:13 +01:00
return -106;
2022-04-21 02:12:38 +02:00
}
2022-04-21 03:38:39 +02:00
u64 get_title_ios(u64 title)
{
2022-04-21 02:12:38 +02:00
s32 ret, fd;
2022-04-21 03:38:39 +02:00
static char filepath[256] ATTRIBUTE_ALIGN(32);
2022-04-21 02:12:38 +02:00
// Check to see if title exists
2022-04-21 03:38:39 +02:00
if (ES_GetDataDir(title, filepath) >= 0)
{
2022-04-21 02:12:38 +02:00
u32 tmd_size;
static u8 tmd_buf[MAX_SIGNED_TMD_SIZE] ATTRIBUTE_ALIGN(32);
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ES_GetStoredTMDSize(title, &tmd_size);
2022-04-21 03:38:39 +02:00
if (ret < 0)
{
2022-04-21 02:12:38 +02:00
// If we fail to use the ES function, try reading manually
// This is a workaround added since some IOS (like 21) don't like our
// call to ES_GetStoredTMDSize
2022-04-21 03:38:39 +02:00
// printf("Error! ES_GetStoredTMDSize: %d\n", ret);
sprintf(filepath, "/title/%08x/%08x/content/title.tmd", TITLE_UPPER(title), TITLE_LOWER(title));
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ISFS_Open(filepath, ISFS_OPEN_READ);
if (ret <= 0)
{
printf("Error! ISFS_Open (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
return 0;
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
fd = ret;
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ISFS_Seek(fd, 0x184, 0);
if (ret < 0)
{
printf("Error! ISFS_Seek (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
return 0;
}
2022-04-21 03:38:39 +02:00
ret = ISFS_Read(fd, tmd_buf, 8);
2022-04-21 02:12:38 +02:00
if (ret < 0)
{
printf("Error! ISFS_Read (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
return 0;
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ISFS_Close(fd);
if (ret < 0)
{
printf("Error! ISFS_Close (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
return 0;
}
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
// it's very nicely aligned i don't think the broadway will crash with this one
return *(u64*)tmd_buf;
2022-04-21 03:38:39 +02:00
}
else
{
2022-04-21 02:12:38 +02:00
// Normal versions of IOS won't have a problem, so we do things the "right" way.
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
// Some of this code adapted from bushing's title_lister.c
signed_blob *s_tmd = (signed_blob *)tmd_buf;
ret = ES_GetStoredTMD(title, s_tmd, tmd_size);
2022-04-21 03:38:39 +02:00
if (ret < 0)
{
printf("Error! ES_GetStoredTMD: %d\n", ret);
2022-04-21 02:12:38 +02:00
return -1;
}
tmd *t = SIGNATURE_PAYLOAD(s_tmd);
return t->sys_version;
}
2022-04-21 03:38:39 +02:00
}
2022-04-21 02:12:38 +02:00
return 0;
}
/* Get Sysmenu Region identifies the region of the system menu (not your Wii)
2023-12-20 18:52:13 +01:00
by looking into it's resource content file for region information. */ // <-- Semibricked Wiis:
2022-04-21 02:12:38 +02:00
char get_sysmenu_region(void)
{
2023-12-20 18:52:13 +01:00
s32 ret;
u16 version;
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
version = get_installed_title_version(TITLE_ID(1, 2));
if (version <= 0)
2022-04-21 02:12:38 +02:00
return 0;
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
/* "mauifrog's 4.1 mod"(?) */
if ((version / 1000) == 54)
version %= 1000;
2022-04-21 02:12:38 +02:00
2023-12-20 18:52:13 +01:00
switch (version & 0b0000000000001111)
2022-04-21 03:38:39 +02:00
{
2023-12-20 18:52:13 +01:00
case 0:
return 'J';
case 1:
return 'U';
case 2:
return 'E';
case 6:
return 'K';
default:
printf("Infected system menu (version number is %hu)\n", version);
wait_anyKey();
break;
2022-04-21 02:12:38 +02:00
}
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
// Plan B
tikview view ATTRIBUTE_ALIGN(32) = {};
s32 cfd;
char region = 0;
unsigned char buffer[0x1000] = {};
const char search[] = "\\ipl\\bin\\RVL\\Final_";
ret = ES_GetTicketViews(TITLE_ID(1, 2), &view, 1);
2022-04-21 02:12:38 +02:00
if (ret < 0)
{
2023-12-20 18:52:13 +01:00
printf("Error! ES_GetTicketViews (ret = %i)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
// .......right, this isn't a vWii with Priiloader installed lol
cfd = ret = ES_OpenTitleContent(TITLE_ID(1, 2), &view, 1);
2022-04-21 02:12:38 +02:00
if (ret < 0)
{
2023-12-20 18:52:13 +01:00
printf("Error! ES_OpenTitleContent (ret = %i)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
while (true)
2022-04-21 03:38:39 +02:00
{
2023-12-20 18:52:13 +01:00
ret = ES_ReadContent(cfd, buffer, sizeof(buffer));
if (ret <= 0)
break;
2022-04-21 03:38:39 +02:00
2023-12-20 18:52:13 +01:00
for (int i = 0; i < (ret - sizeof(search)); i++)
2022-04-21 03:38:39 +02:00
{
2023-12-20 18:52:13 +01:00
// Not looking for the null byte!!!!
if (memcmp(buffer, search, sizeof(search) - 1) == 0)
2022-04-21 03:38:39 +02:00
{
2023-12-20 18:52:13 +01:00
region = *(buffer + i + strlen(search));
break;
2022-04-21 02:12:38 +02:00
}
}
2023-12-20 18:52:13 +01:00
if (region)
break;
2022-04-21 02:12:38 +02:00
}
2023-12-20 18:52:13 +01:00
ES_CloseContent(cfd);
2022-04-21 02:12:38 +02:00
if (ret < 0)
{
2023-12-20 18:52:13 +01:00
printf("Error! ES_ReadContent (ret = %i)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
}
2023-12-20 18:52:13 +01:00
else if (!region)
2022-04-21 03:38:39 +02:00
{
2023-12-20 18:52:13 +01:00
printf("Unable to identify system menu region!!\n");
wait_anyKey();
2022-04-21 02:12:38 +02:00
}
2023-12-20 18:52:13 +01:00
return region;
}