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

302 lines
6.5 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
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 "id.h"
#include "wiibasics.h"
#include "detect_settings.h"
2022-04-21 03:38:39 +02:00
u16 get_installed_title_version(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, 0x1dc, 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, 2);
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
2022-04-21 02:12:38 +02:00
return be16(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->title_version;
}
2022-04-21 03:38:39 +02:00
}
2022-04-21 02:12:38 +02:00
return 0;
}
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
2022-04-21 02:12:38 +02:00
return be64(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)
by looking into it's resource content file for region information. */
char get_sysmenu_region(void)
{
2022-04-21 03:38:39 +02:00
s32 ret, cfd;
2022-04-21 02:12:38 +02:00
static u8 fbuffer[0x500] ATTRIBUTE_ALIGN(32);
static tikview viewdata[0x10] ATTRIBUTE_ALIGN(32);
u32 views;
2022-04-21 03:38:39 +02:00
static u64 tid ATTRIBUTE_ALIGN(32) = TITLE_ID(1, 2);
2022-04-21 02:12:38 +02:00
u8 region, match[] = "FINAL";
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
/*ret = ES_SetUID(TITLE_ID(1,2));
if (ret){
printf("Error! ES_GetSetUID %d\n", ret);
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ES_GetTitleID(&tid);
if (ret){
printf("Error! ES_GetTitleID %d\n", ret);
wait_anyKey();
return 0;
}
if (tid != TITLE_ID(1,2)){
printf("Error! Not System Menu! %016llx\n", tid);
wait_anyKey();
return 0;
}
ret = ES_OpenContent(1);
if (ret < 0)
{
printf("Error! ES_OpenContent (ret = %d)\n", ret);
wait_anyKey();
return 0;
}*/
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ES_GetNumTicketViews(tid, &views);
2022-04-21 03:38:39 +02:00
if (ret < 0)
{
printf(" Error! ES_GetNumTickets (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return ret;
}
2022-04-21 03:38:39 +02:00
if (!views)
{
2022-04-21 02:12:38 +02:00
printf(" No tickets found!\n");
wait_anyKey();
return 0;
2022-04-21 03:38:39 +02:00
}
else if (views > 16)
{
printf(" Too many ticket views! (views = %d)\n", views);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ES_GetTicketViews(tid, viewdata, 1);
if (ret < 0)
{
printf("Error! ES_OpenTitleContent (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
ret = ES_OpenTitleContent(tid, viewdata, 1);
if (ret < 0)
{
printf("Error! ES_OpenTitleContent (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
2022-04-21 02:12:38 +02:00
cfd = ret;
region = 0;
2022-04-21 03:38:39 +02:00
while (!region)
{
2022-04-21 02:12:38 +02:00
int i;
2022-04-21 03:38:39 +02:00
ret = ES_ReadContent(cfd, fbuffer, 0x500);
2022-04-21 02:12:38 +02:00
if (ret < 0)
{
printf("Error! ES_ReadContent (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
for (i = 0; i < 0x500; i++)
{
if (fbuffer[i] == 'F')
{
if (!memcmp(&fbuffer[i], match, 6))
{
region = fbuffer[i + 6];
2022-04-21 02:12:38 +02:00
break;
}
}
}
}
ret = ES_CloseContent(cfd);
if (ret < 0)
{
printf("Error! ES_CloseContent (ret = %d)\n", ret);
2022-04-21 02:12:38 +02:00
wait_anyKey();
return 0;
}
2022-04-21 03:38:39 +02:00
switch (region)
{
2022-04-21 02:12:38 +02:00
case 'U':
case 'E':
case 'J':
2022-04-21 02:46:43 +02:00
case 'K':
2022-04-21 02:12:38 +02:00
return region;
2022-04-21 03:38:39 +02:00
break;
2022-04-21 02:12:38 +02:00
default:
return -1;
2022-04-21 03:38:39 +02:00
break;
2022-04-21 02:12:38 +02:00
}
2022-04-21 02:46:43 +02:00
}