usbloadergx/source/usbloader/disc.c

401 lines
8.9 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogcsys.h>
#include <ogc/lwp_watchdog.h>
2009-06-13 02:24:36 +02:00
#include "patches/fst.h"
#include "patches/gamepatches.h"
#include "patches/wip.h"
#include "apploader.h"
#include "disc.h"
#include "video.h"
#include "wdvd.h"
#include "frag.h"
#include "alternatedol.h"
#include "memory/memory.h"
#include "wbfs.h"
2010-10-27 16:45:27 +02:00
#include "../settings/SettingsEnums.h"
#include "../gecko.h"
/* Constants */
#define PTABLE_OFFSET 0x40000
#define WII_MAGIC 0x5D1C9EA3
/* Disc pointers */
2010-09-24 02:48:03 +02:00
static u32 *buffer = (u32 *) 0x93000000;
static u8 *diskid = (u8 *) Disc_ID;
static GXRModeObj *vmode = NULL;
static u32 vmode_reg = 0;
void Disc_SetLowMem(void)
{
/* Setup low memory */
2010-09-24 02:48:03 +02:00
*Sys_Magic = 0x0D15EA5E; // Standard Boot Code
*Version = 0x00000001; // Version
*Arena_L = 0x00000000; // Arena Low
*BI2 = 0x817E5480; // BI2
*Bus_Speed = 0x0E7BE2C0; // Console Bus Speed
*CPU_Speed = 0x2B73A840; // Console CPU Speed
/* Setup low memory */
2010-09-24 02:48:03 +02:00
*Assembler = 0x38A00040; // Assembler
*(u32 *) 0x800000E4 = 0x80431A80;
*Dev_Debugger = 0x81800000; // Dev Debugger Monitor Address
*Simulated_Mem = 0x01800000; // Simulated Memory Size
*(vu32 *) 0xCD00643C = 0x00000000; // 32Mhz on Bus
int iosVer = IOS_GetVersion();
USB Loader GX Release V2.2 New: - Added game categories and filter games list by categories (Can be imported from WiiTDB). - Wiinertag support. - Supporting arguments from meta.xml on boot (--ios=xxx and --usbport=x) (Requires Homebrew Channel 1.0.7+ or UNEO Forwarder v3.0). - New ehci modules by Rodries with better drive compatibility. - Added two new video modes to force progressive video mode, 'FORCE PAL480p' and 'FORCE NTSC480p'. - Added Sneek Video Patch mode. - Added new 'Inherit' setting for game settings named "Use global". If that option is set then the main loader setting is used. - Full d2x cIOS support with it's new features (Block IOS Reload, Return To, Sector Sizes > 512). - Support for sector sizes > 512B with FAT32/NTFS (Requires d2x v6+) - Real support for simultanious use of both USB ports without switching the 2nd drive temporary off. (Requires Hermes cIOS or Rodries MOD of the Hermes cIOS (recommended)) - Added two new settings menus - Added saving of game browser position when returning to USB Loader GX Changes: - Improved several GUI controls/navigations - Changed settings menu layout and sorted the items to their correct place (HDD menu, features menu) - Set games settings to use the global setting by default, set to "use global" to use the main loader settings. - Use TinyXML instead of MXML (better XML support) - Updated to new libs (libogc, libfat, libext2fs, libntfs) Fix: - "Return to" option now work for all games, even problematic games like Prince of Persia. (Requires d2x v4+) - Xflip setting fixed. - Fix the parental lock of Individual game settings (Thanks to NJ7) - Fix Theme downloader - Fixed reset of the loader when loading game with IOS reload and disabled WiiTDB titles - Fixed timeout timer on startup to count correctly. - Fixed reversed disc image download when Custom/Original option is selected - Fixed reload of game list after a game rename - Fixed horizontal text scrolling - Fixed booting games by arguments (headless id feature) - Fixed We Dare game boot (thx oggzee) R1099 Change Log: *Added IOS225 from Rodries cIOS Installer MOD to Hermes IOS types New Forwarder V3.0 Changes: *added support for ext partitions *added support for arguments from xml *clean up of source
2011-06-29 22:45:40 +02:00
if(iosVer != 222 && iosVer != 223 && iosVer != 224 && iosVer != 225 && IOS_GetRevision() >= 18)
2010-09-24 02:48:03 +02:00
*GameID_Address = 0x80000000; // Game ID Address
/* Copy disc ID */
2010-09-24 02:48:03 +02:00
memcpy((void *) Online_Check, (void *) Disc_ID, 4);
/* Flush cache */
2010-09-24 02:48:03 +02:00
DCFlushRange((void *) Disc_ID, 0x3F00);
}
void Disc_SelectVMode(u8 videoselected)
{
vmode = VIDEO_GetPreferredMode(0);
/* Get video mode configuration */
bool progressive = (CONF_GetProgressiveScan() > 0) && VIDEO_HaveComponentCable();
/* Select video mode register */
switch (CONF_GetVideo())
{
case CONF_VIDEO_PAL:
if (CONF_GetEuRGB60() > 0)
{
vmode_reg = VI_EURGB60;
vmode = progressive ? &TVNtsc480Prog : &TVEurgb60Hz480IntDf;
}
else
vmode_reg = VI_PAL;
break;
case CONF_VIDEO_MPAL:
vmode_reg = VI_MPAL;
break;
case CONF_VIDEO_NTSC:
vmode_reg = VI_NTSC;
break;
}
2010-09-24 02:48:03 +02:00
switch (videoselected)
{
default:
case VIDEO_MODE_DISCDEFAULT: // DEFAULT (DISC/GAME)
/* Select video mode */
switch (diskid[3])
{
// PAL
case 'D':
case 'F':
case 'P':
case 'X':
case 'Y':
if (CONF_GetVideo() != CONF_VIDEO_PAL)
{
vmode_reg = VI_PAL;
vmode = progressive ? &TVNtsc480Prog : &TVNtsc480IntDf;
}
break;
// NTSC
case 'E':
case 'J':
default:
if (CONF_GetVideo() != CONF_VIDEO_NTSC)
{
vmode_reg = VI_NTSC;
vmode = progressive ? &TVNtsc480Prog : &TVEurgb60Hz480IntDf;
}
break;
}
break;
case VIDEO_MODE_PAL50: // PAL50
vmode = &TVPal528IntDf;
vmode_reg = vmode->viTVMode >> 2;
break;
case VIDEO_MODE_PAL60: // PAL60
vmode = progressive ? &TVNtsc480Prog : &TVEurgb60Hz480IntDf;
vmode_reg = progressive ? TVEurgb60Hz480Prog.viTVMode >> 2 : vmode->viTVMode >> 2;
break;
case VIDEO_MODE_NTSC: // NTSC
vmode = progressive ? &TVNtsc480Prog : &TVNtsc480IntDf;
vmode_reg = vmode->viTVMode >> 2;
break;
case VIDEO_MODE_PAL480P:
vmode_reg = TVEurgb60Hz480Prog.viTVMode >> 2;
vmode = &TVNtsc480Prog;
break;
case VIDEO_MODE_NTSC480P:
vmode_reg = VI_NTSC;
vmode = &TVNtsc480Prog;
break;
case VIDEO_MODE_SYSDEFAULT: // AUTO PATCH TO SYSTEM
break;
}
}
void __Disc_SetVMode(void)
{
/* Set video mode register */
*Video_Mode = vmode_reg;
/* Set video mode */
if (vmode != NULL)
{
VIDEO_Configure(vmode);
}
/* Setup video */
VIDEO_SetBlack(TRUE);
VIDEO_Flush();
VIDEO_WaitVSync();
if (vmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync();
}
2010-09-24 02:48:03 +02:00
void __Disc_SetTime(void)
{
/* Extern */
2010-09-24 02:48:03 +02:00
extern void settime(u64);
/* Set proper time */
2010-09-24 02:48:03 +02:00
settime(secs_to_ticks( time( NULL ) - 946684800 ));
}
s32 Disc_FindPartition(u64 *outbuf)
{
u64 offset = 0, table_offset = 0;
u32 cnt, nb_partitions;
s32 ret;
/* Read partition info */
2010-09-24 02:48:03 +02:00
ret = WDVD_UnencryptedRead(buffer, 0x20, PTABLE_OFFSET);
if (ret < 0) return ret;
/* Get data */
nb_partitions = buffer[0];
2010-09-24 02:48:03 +02:00
table_offset = buffer[1] << 2;
/* Read partition table */
2010-09-24 02:48:03 +02:00
ret = WDVD_UnencryptedRead(buffer, 0x20, table_offset);
if (ret < 0) return ret;
/* Find game partition */
2010-09-24 02:48:03 +02:00
for (cnt = 0; cnt < nb_partitions; cnt++)
{
u32 type = buffer[cnt * 2 + 1];
/* Game partition */
2010-09-24 02:48:03 +02:00
if (!type) offset = buffer[cnt * 2] << 2;
}
/* No game partition found */
2010-09-24 02:48:03 +02:00
if (!offset) return -1;
/* Set output buffer */
*outbuf = offset;
return 0;
}
2010-09-24 02:48:03 +02:00
s32 Disc_Init(void)
{
/* Init DVD subsystem */
return WDVD_Init();
}
2010-09-24 02:48:03 +02:00
s32 Disc_Open(void)
{
s32 ret;
/* Reset drive */
ret = WDVD_Reset();
2010-09-24 02:48:03 +02:00
if (ret < 0) return ret;
/* Read disc ID */
2010-09-24 02:48:03 +02:00
return WDVD_ReadDiskId(diskid);
}
2010-09-24 02:48:03 +02:00
s32 Disc_Wait(void)
{
u32 cover = 0;
s32 ret;
/* Wait for disc */
2010-09-24 02:48:03 +02:00
while (!(cover & 0x2))
{
/* Get cover status */
2010-09-24 02:48:03 +02:00
ret = WDVD_GetCoverStatus(&cover);
if (ret < 0) return ret;
}
return 0;
}
2010-09-24 02:48:03 +02:00
s32 Disc_SetUSB(const u8 *id)
{
/* Set USB mode */
return WDVD_SetUSBMode((u8 *) id, -1);
}
2010-09-24 02:48:03 +02:00
s32 Disc_ReadHeader(void *outbuf)
{
/* Read disc header */
2010-09-24 02:48:03 +02:00
return WDVD_UnencryptedRead(outbuf, sizeof(struct discHdr), 0);
}
2010-09-24 02:48:03 +02:00
s32 Disc_IsWii(void)
{
2010-09-24 02:48:03 +02:00
struct discHdr *header = (struct discHdr *) buffer;
s32 ret;
/* Read disc header */
2010-09-24 02:48:03 +02:00
ret = Disc_ReadHeader(header);
if (ret < 0) return ret;
/* Check magic word */
2010-09-24 02:48:03 +02:00
if (header->magic != WII_MAGIC) return -1;
return 0;
}
s32 Disc_JumpToEntrypoint(bool enablecheat, u32 dolparameter)
{
/* Set an appropiate video mode */
__Disc_SetVMode();
/* Set time */
__Disc_SetTime();
/* Shutdown IOS subsystems */
extern void __exception_closeall();
u32 level = IRQ_Disable();
__IOS_ShutdownSubsystems();
__exception_closeall();
*Fixed display of partition size on WBFS partitions with a different wbfs sector size than 512bytes. *Made the ProgressWindow for game installation more accurate *Added displaying newly installed games (marked as new) on favorite list, so you don't have to change to full list when installing new games. (Thanks Cyan for the patch) *Lot's a small fixes *Added WDM Menu on game start. You can set it in the alternative DOL option (one new option there). The menu lists all DOLs on the disc and if a wdm file is provided in the WDM path (configurable in the settings) than the dol parameter and dol replacement name will be taken from the wdm. The DOLs that are not listed in the WDM but exist on the DISC will be listed at the end of the list. *Added avoid of multiple app cleanup when game fails to boot *Changed libfat to use FS info sector on FAT32 partitions. This speeds up the free space information getting to instant. For that the FS info sector has to have correct values. The values of all partitions where homebrews were writing to are currently incorrect because the official libfat does not support FS info sector (i submited a patch) (Windows does write it correct though). That is why there needs to be a synchronization of the FS info sector for partitions used with homebrews. For this purpose a new setting was added in the Loader Settings. You can synchronize all your FAT32 partitions on the USB with it once and you are done (if you don't write to that partition with current homebrews). After that you can enable free space display and it will be instant like on WBFS/NTFS/EXT partitions.
2011-01-16 14:12:07 +01:00
/* Originally from tueidj - taken from NeoGamme (thx) */
*(vu32*)0xCC003024 = dolparameter != 0 ? dolparameter : 1;
if (enablecheat)
{
__asm__(
"lis %r3, AppEntrypoint@h\n"
"ori %r3, %r3, AppEntrypoint@l\n"
2010-09-24 02:48:03 +02:00
"lwz %r3, 0(%r3)\n"
"mtlr %r3\n"
"lis %r3, 0x8000\n"
"ori %r3, %r3, 0x18A8\n"
"nop\n"
2010-09-24 02:48:03 +02:00
"mtctr %r3\n"
"bctr\n"
);
}
else
{
__asm__(
"lis %r3, AppEntrypoint@h\n"
"ori %r3, %r3, AppEntrypoint@l\n"
2010-09-24 02:48:03 +02:00
"lwz %r3, 0(%r3)\n"
"mtlr %r3\n"
"blr\n"
);
}
IRQ_Restore(level);
return 0;
}
2010-09-24 02:48:03 +02:00
void PatchCountryStrings(void *Address, int Size)
{
2010-09-24 02:48:03 +02:00
u8 SearchPattern[4] = { 0x00, 0x00, 0x00, 0x00 };
u8 PatchData[4] = { 0x00, 0x00, 0x00, 0x00 };
u8 *Addr = (u8*) Address;
int wiiregion = CONF_GetRegion();
2010-09-24 02:48:03 +02:00
switch (wiiregion)
{
case CONF_REGION_JP:
SearchPattern[0] = 0x00;
SearchPattern[1] = 0x4A; // J
SearchPattern[2] = 0x50; // P
break;
case CONF_REGION_EU:
SearchPattern[0] = 0x02;
SearchPattern[1] = 0x45; // E
SearchPattern[2] = 0x55; // U
break;
case CONF_REGION_KR:
SearchPattern[0] = 0x04;
SearchPattern[1] = 0x4B; // K
SearchPattern[2] = 0x52; // R
break;
case CONF_REGION_CN:
SearchPattern[0] = 0x05;
SearchPattern[1] = 0x43; // C
SearchPattern[2] = 0x4E; // N
break;
case CONF_REGION_US:
default:
SearchPattern[0] = 0x01;
SearchPattern[1] = 0x55; // U
SearchPattern[2] = 0x53; // S
break;
}
2010-09-24 02:48:03 +02:00
switch (diskid[3])
{
case 'J':
PatchData[1] = 0x4A; // J
PatchData[2] = 0x50; // P
break;
case 'D':
case 'F':
case 'P':
case 'X':
case 'Y':
PatchData[1] = 0x45; // E
PatchData[2] = 0x55; // U
break;
case 'E':
default:
PatchData[1] = 0x55; // U
PatchData[2] = 0x53; // S
break;
}
2010-09-24 02:48:03 +02:00
while (Size >= 4)
{
2010-09-24 02:48:03 +02:00
if (Addr[0] == SearchPattern[0] && Addr[1] == SearchPattern[1] && Addr[2] == SearchPattern[2] && Addr[3]
== SearchPattern[3])
{
//*Addr = PatchData[0];
Addr += 1;
*Addr = PatchData[1];
Addr += 1;
*Addr = PatchData[2];
Addr += 1;
//*Addr = PatchData[3];
Addr += 1;
Size -= 4;
}
else
{
Addr += 4;
Size -= 4;
}
}
}