snes9xgx/source/ngc/dvd.cpp

838 lines
22 KiB
C++
Raw Normal View History

/****************************************************************************
* Snes9x 1.51 Nintendo Wii/Gamecube Port
*
* softdev July 2006
* svpe & crunchy2 June 2007
* Tantric September 2008
*
* dvd.cpp
*
* DVD I/O functions
***************************************************************************/
#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2008-10-15 20:29:01 +02:00
#include <malloc.h>
2008-08-12 05:25:16 +02:00
#ifdef HW_RVL
2008-08-23 05:20:54 +02:00
extern "C" {
#include <di/di.h>
2008-08-23 05:20:54 +02:00
}
#endif
2009-03-11 18:28:37 +01:00
#include "menu.h"
#include "gcunzip.h"
2009-03-11 18:28:37 +01:00
#include "filebrowser.h"
2008-12-18 19:36:30 +01:00
#include "snes9xGX.h"
2008-08-12 05:25:16 +02:00
#define MAXDVDFILES 2000
static int diroffset = 0;
static u64 dvddir = 0; // offset of currently selected file or folder
static int dvddirlength = 0; // length of currently selected file or folder
static u64 dvdrootdir = 0; // offset of DVD root
static int dvdrootlength = 0; // length of DVD root
static bool isWii = false;
#ifdef HW_DOL
/** DVD I/O Address base **/
volatile unsigned long *dvd = (volatile unsigned long *) 0xCC006000;
#endif
/****************************************************************************
* dvd_read
*
2008-10-15 20:29:01 +02:00
* Main DVD function, everything else uses this!
* returns: 1 - ok ; 0 - error
***************************************************************************/
2008-10-15 20:29:01 +02:00
#define ALIGN_FORWARD(x,align) ((typeof(x))((((uint32_t)(x)) + (align) - 1) & (~(align-1))))
#define ALIGN_BACKWARD(x,align) ((typeof(x))(((uint32_t)(x)) & (~(align-1))))
static int
dvd_read (void *dst, unsigned int len, u64 offset)
{
if (len > 2048)
return 0; /*** We only allow 2k reads **/
// don't read past the end of the DVD (1.5 GB for GC DVD, 4.7 GB for DVD)
2008-10-04 04:27:05 +02:00
if((offset < 0x57057C00) || (isWii && (offset < 0x118244F00LL)))
{
2008-10-15 20:29:01 +02:00
u8 * buffer = (u8 *)memalign(32, 0x8000);
u32 off_size = 0;
2008-10-04 04:27:05 +02:00
DCInvalidateRange ((void *) buffer, len);
2008-08-20 09:58:55 +02:00
2008-10-04 04:27:05 +02:00
#ifdef HW_DOL
dvd[0] = 0x2E;
dvd[1] = 0;
dvd[2] = 0xA8000000;
dvd[3] = (u32)(offset >> 2);
dvd[4] = len;
dvd[5] = (u32) buffer;
dvd[6] = len;
dvd[7] = 3;
// Enable reading with DMA
2009-03-17 06:09:45 +01:00
while (dvd[7] & 1);
2008-10-04 04:27:05 +02:00
// Ensure it has completed
if (dvd[0] & 0x4)
return 0;
#else
2008-10-15 20:29:01 +02:00
off_size = offset - ALIGN_BACKWARD(offset,0x800);
if (DI_ReadDVD(
buffer,
(ALIGN_FORWARD(offset + len,0x800) - ALIGN_BACKWARD(offset,0x800)) >> 11,
(u32)(ALIGN_BACKWARD(offset, 0x800) >> 11)
))
2008-10-04 04:27:05 +02:00
return 0;
#endif
2008-08-20 09:58:55 +02:00
2008-10-15 20:29:01 +02:00
memcpy (dst, buffer+off_size, len);
free(buffer);
return 1;
}
2008-08-20 09:58:55 +02:00
return 0;
}
2008-10-14 11:21:34 +02:00
/****************************************************************************
* dvd_buffered_read
*
* the GC's dvd drive only supports offsets and length which are a multiple
* of 32 bytes additionally the max length of a read is 2048 bytes
* this function removes these limitations
* additionally the 7zip SDK does often read data in 1 byte parts from the
* DVD even when it could read 32 bytes. the dvdsf_buffer has been added to
* avoid having to read the same sector over and over again
***************************************************************************/
#define DVD_LENGTH_MULTIPLY 32
#define DVD_OFFSET_MULTIPLY 32
#define DVD_MAX_READ_LENGTH 2048
#define DVD_SECTOR_SIZE 2048
static unsigned char dvdsf_buffer[DVD_SECTOR_SIZE];
static u64 dvdsf_last_offset = 0;
static u64 dvdsf_last_length = 0;
2008-10-14 11:21:34 +02:00
static int dvd_buffered_read(void *dst, u32 len, u64 offset)
2008-10-14 11:21:34 +02:00
{
int ret = 1;
2008-10-14 11:21:34 +02:00
// only read data if the data inside dvdsf_buffer cannot be used
if(offset != dvdsf_last_offset || len > dvdsf_last_length)
{
memset(&dvdsf_buffer, '\0', DVD_SECTOR_SIZE);
ret = dvd_read(&dvdsf_buffer, len, offset);
dvdsf_last_offset = offset;
2008-10-14 11:21:34 +02:00
dvdsf_last_length = len;
}
2008-10-14 11:21:34 +02:00
memcpy(dst, &dvdsf_buffer, len);
return ret;
2008-10-14 11:21:34 +02:00
}
/****************************************************************************
* dvd_safe_read
*
* A 'safer' DVD read function
* This function relies on dvddir (file offset) being prepopulated!
* returns: 1 - ok ; 0 - error
***************************************************************************/
int dvd_safe_read(void *dst_v, u32 len, u64 fileoffset)
2008-10-14 11:21:34 +02:00
{
u64 offset = dvddir + fileoffset;
unsigned char buffer[DVD_SECTOR_SIZE]; // buffer for one dvd sector
2008-10-14 11:21:34 +02:00
// if read size and length are a multiply of DVD_(OFFSET,LENGTH)_MULTIPLY and length < DVD_MAX_READ_LENGTH
// we don't need to fix anything
if(len % DVD_LENGTH_MULTIPLY == 0 && offset % DVD_OFFSET_MULTIPLY == 0 && len <= DVD_MAX_READ_LENGTH)
{
int ret = dvd_buffered_read(buffer, len, offset);
memcpy(dst_v, &buffer, len);
return ret;
}
else
{
2008-12-18 19:36:30 +01:00
// no errors yet -> ret = 1
// the return value of dvd_read will be AND'd with ret
// because dvd_read does return 0 on error and 1 on success and
// because 1 & 0 = 0 ret will also contain 0 if at least one error
// occured and 1 otherwise ;)
int ret = 1; // return value of dvd_read
2008-10-14 11:21:34 +02:00
// we might need to fix all 3 issues
unsigned char *dst = (unsigned char *)dst_v; // gcc will not allow to use var[num] on void* types
u64 bytesToRead; // the number of bytes we still need to read & copy to the output buffer
u64 currentOffset; // the current dvd offset
u64 bufferOffset; // the current buffer offset
u64 i, j, k; // temporary variables which might be used for different stuff
// unsigned char buffer[DVD_SECTOR_SIZE]; // buffer for one dvd sector
currentOffset = offset;
bytesToRead = len;
bufferOffset = 0;
// fix first issue (offset is not a multiply of 32)
if(offset % DVD_OFFSET_MULTIPLY)
{
// calculate offset of the prior 32 byte position
i = currentOffset - (currentOffset % DVD_OFFSET_MULTIPLY);
// calculate the offset from which the data of the dvd buffer will be copied
j = currentOffset % DVD_OFFSET_MULTIPLY;
// calculate the number of bytes needed to reach the next DVD_OFFSET_MULTIPLY byte mark
k = DVD_OFFSET_MULTIPLY - j;
// maybe we'll only need to copy a few bytes and we therefore don't even reach the next sector
if(k > len)
{
k = len;
}
// read 32 bytes from the last 32 byte position
2008-12-18 19:36:30 +01:00
ret &= dvd_buffered_read(buffer, DVD_OFFSET_MULTIPLY, i);
2008-10-14 11:21:34 +02:00
// copy the bytes to the output buffer and update currentOffset, bufferOffset and bytesToRead
memcpy(&dst[bufferOffset], &buffer[j], k);
currentOffset += k;
bufferOffset += k;
bytesToRead -= k;
}
// fix second issue (more than 2048 bytes are needed)
if(bytesToRead > DVD_MAX_READ_LENGTH)
{
// calculate the number of 2048 bytes sector needed to get all data
i = (bytesToRead - (bytesToRead % DVD_MAX_READ_LENGTH)) / DVD_MAX_READ_LENGTH;
// read data in 2048 byte sector
for(j = 0; j < i; j++)
{
2008-12-18 19:36:30 +01:00
ret &= dvd_buffered_read(buffer, DVD_MAX_READ_LENGTH, currentOffset); // read sector
2008-10-14 11:21:34 +02:00
memcpy(&dst[bufferOffset], buffer, DVD_MAX_READ_LENGTH); // copy to output buffer
// update currentOffset, bufferOffset and bytesToRead
currentOffset += DVD_MAX_READ_LENGTH;
bufferOffset += DVD_MAX_READ_LENGTH;
bytesToRead -= DVD_MAX_READ_LENGTH;
}
}
// fix third issue (length is not a multiply of 32)
if(bytesToRead)
{
2008-12-18 19:36:30 +01:00
ret &= dvd_buffered_read(buffer, DVD_MAX_READ_LENGTH, currentOffset); // read 32 byte from the dvd
2008-10-14 11:21:34 +02:00
memcpy(&dst[bufferOffset], buffer, bytesToRead); // copy bytes to output buffer
}
return ret;
}
}
/** Minimal ISO Directory Definition **/
#define RECLEN 0 /* Record length */
#define EXTENT 6 /* Extent */
#define FILE_LENGTH 14 /* File length (BIG ENDIAN) */
#define FILE_FLAGS 25 /* File flags */
#define FILENAME_LENGTH 32 /* Filename length */
#define FILENAME 33 /* ASCIIZ filename */
/** Minimal Primary Volume Descriptor **/
#define PVDROOT 0x9c
static int IsJoliet = 0;
/****************************************************************************
* Primary Volume Descriptor
*
* The PVD should reside between sector 16 and 31.
* This is for single session DVD only.
***************************************************************************/
static int
getpvd ()
{
2008-08-16 02:02:08 +02:00
int sector = 16;
u32 rootdir32;
2008-10-15 20:29:01 +02:00
unsigned char dvdbuffer[2048];
2008-08-16 02:02:08 +02:00
dvddir = dvddirlength = 0;
IsJoliet = -1;
/** Look for Joliet PVD first **/
2008-08-16 02:02:08 +02:00
while (sector < 32)
{
if (dvd_read (&dvdbuffer, 2048, (u64)(sector << 11)))
2008-08-16 02:02:08 +02:00
{
if (memcmp (&dvdbuffer, "\2CD001\1", 8) == 0)
{
memcpy(&rootdir32, &dvdbuffer[PVDROOT + EXTENT], 4);
dvddir = (u64)rootdir32;
dvddir <<= 11;
memcpy (&dvddirlength, &dvdbuffer[PVDROOT + FILE_LENGTH], 4);
2008-11-12 08:50:39 +01:00
dvdrootdir = dvddir;
dvdrootlength = dvddirlength;
2008-08-16 02:02:08 +02:00
IsJoliet = 1;
break;
}
2008-08-16 02:02:08 +02:00
}
else
return 0; /*** Can't read sector! ***/
sector++;
}
2008-08-16 02:02:08 +02:00
if (IsJoliet > 0) /*** Joliet PVD Found ? ***/
return 1;
2008-08-16 02:02:08 +02:00
sector = 16;
/*** Look for standard ISO9660 PVD ***/
2008-08-16 02:02:08 +02:00
while (sector < 32)
{
2008-08-16 02:02:08 +02:00
if (dvd_read (&dvdbuffer, 2048, sector << 11))
{
if (memcmp (&dvdbuffer, "\1CD001\1", 8) == 0)
{
memcpy (&rootdir32, &dvdbuffer[PVDROOT + EXTENT], 4);
dvddir = (u64)rootdir32;
dvddir <<= 11;
memcpy (&dvddirlength, &dvdbuffer[PVDROOT + FILE_LENGTH], 4);
2008-11-12 09:33:12 +01:00
dvdrootdir = dvddir;
dvdrootlength = dvddirlength;
2008-08-16 02:02:08 +02:00
IsJoliet = 0;
break;
}
}
else
return 0; /*** Can't read sector! ***/
sector++;
}
2008-08-16 02:02:08 +02:00
return (IsJoliet == 0);
}
2008-08-16 02:02:08 +02:00
/****************************************************************************
2008-12-18 19:36:30 +01:00
* MountDVD()
2008-08-16 02:02:08 +02:00
*
2008-12-18 19:36:30 +01:00
* Tests if a ISO9660 DVD is inserted and available, and mounts it
***************************************************************************/
2008-12-18 19:36:30 +01:00
bool MountDVD(bool silent)
2008-08-16 02:02:08 +02:00
{
2009-03-11 18:28:37 +01:00
bool res = false;
if (getpvd())
{
return true;
}
else
2008-08-16 02:02:08 +02:00
{
2008-12-18 19:36:30 +01:00
ShowAction("Loading DVD...");
#ifdef HW_DOL
2008-12-18 19:36:30 +01:00
DVD_Mount(); // mount the DVD unit again
2009-03-11 18:28:37 +01:00
#else
2008-12-18 19:36:30 +01:00
u32 val;
DI_GetCoverRegister(&val);
if(val & 0x1) // True if no disc inside, use (val & 0x2) for true if disc inside.
{
if(!silent)
2009-03-11 18:28:37 +01:00
ErrorPrompt("No disc inserted!");
CancelAction();
2008-12-18 19:36:30 +01:00
return false;
}
DI_Mount();
2009-03-11 18:28:37 +01:00
while(DI_GetStatus() & DVD_INIT) usleep(20000);
#endif
2008-12-18 19:36:30 +01:00
2009-03-11 18:28:37 +01:00
if (getpvd())
res = true;
else if(!silent)
ErrorPrompt("Invalid DVD.");
2008-08-16 02:02:08 +02:00
}
2009-03-11 18:28:37 +01:00
CancelAction();
return res;
}
/****************************************************************************
* getentry
*
* Support function to return the next file entry, if any
* Declared static to avoid accidental external entry.
***************************************************************************/
static int
2008-10-15 20:29:01 +02:00
getentry (int entrycount, unsigned char dvdbuffer[])
{
2008-08-16 02:02:08 +02:00
char fname[512]; /* Huge, but experience has determined this */
2008-11-26 08:40:12 +01:00
char tmpname[512];
2008-08-16 02:02:08 +02:00
char *ptr;
char *filename;
char *filenamelength;
char *rr;
int j;
u32 offset32;
2008-08-16 02:02:08 +02:00
/* Basic checks */
if (entrycount >= MAXDVDFILES)
2008-08-16 02:02:08 +02:00
return 0;
if (diroffset >= 2048 || diroffset < 0)
2008-08-16 02:02:08 +02:00
return 0;
2008-08-16 02:02:08 +02:00
/** Decode this entry **/
if (dvdbuffer[diroffset]) /* Record length available */
{
2008-08-16 02:02:08 +02:00
/* Update offsets into sector buffer */
ptr = (char *) &dvdbuffer[0];
ptr += diroffset;
filename = ptr + FILENAME;
filenamelength = ptr + FILENAME_LENGTH;
/* Check for wrap round - illegal in ISO spec,
* but certain crap writers do it! */
if ((diroffset + dvdbuffer[diroffset]) > 2048 || (diroffset + dvdbuffer[diroffset]) < 0)
2008-08-16 02:02:08 +02:00
return 0;
if (*filenamelength)
{
2008-08-16 02:02:08 +02:00
memset (&fname, 0, 512);
if (!IsJoliet) /*** Do ISO 9660 first ***/
{
strncpy (fname, filename, 512);
}
2008-08-16 02:02:08 +02:00
else
{ /*** The more tortuous unicode joliet entries ***/
for (j = 0; j < (*filenamelength >> 1); j++)
{
fname[j] = filename[j * 2 + 1];
}
fname[j] = 0;
if (strlen (fname) >= MAXJOLIET)
fname[MAXJOLIET - 1] = 0;
2008-08-16 02:02:08 +02:00
if (strlen (fname) == 0)
fname[0] = filename[0];
}
if (strlen (fname) == 0) // root entry
{
fname[0] = 0; // we'll skip it by setting the filename to 0 length
}
else
{
if (fname[0] == 1)
{
if(dvddir == dvdrootdir) // at root already, don't show ..
fname[0] = 0;
else
2009-03-19 08:46:51 +01:00
strcpy (fname, "..");
2008-08-16 02:02:08 +02:00
}
else
{
/*
* Move *filenamelength to t,
* Only to stop gcc warning for noobs :)
*/
int t = *filenamelength;
fname[t] = 0;
}
}
/** Rockridge Check **/
2008-08-16 02:02:08 +02:00
rr = strstr (fname, ";");
if (rr != NULL)
*rr = 0;
BROWSERENTRY * newBrowserList = (BROWSERENTRY *)realloc(browserList, (entrycount+1) * sizeof(BROWSERENTRY));
if(!newBrowserList) // failed to allocate required memory
{
ResetBrowser();
2009-03-11 18:28:37 +01:00
ErrorPrompt("Out of memory: too many files!");
return 0;
}
else
{
browserList = newBrowserList;
}
memset(&(browserList[entrycount]), 0, sizeof(BROWSERENTRY)); // clear the new entry
strncpy (browserList[entrycount].filename, fname, MAXJOLIET);
2009-03-19 08:46:51 +01:00
if(strcmp(fname,"..") == 0)
{
sprintf(browserList[entrycount].displayname, "Up One Level");
}
else
{
StripExt(tmpname, fname); // hide file extension
strncpy (browserList[entrycount].displayname, tmpname, MAXDISPLAY);
}
2008-08-16 02:02:08 +02:00
memcpy (&offset32, &dvdbuffer[diroffset + EXTENT], 4);
browserList[entrycount].offset = (u64)offset32;
memcpy (&(browserList[entrycount].length), &dvdbuffer[diroffset + FILE_LENGTH], 4);
memcpy (&(browserList[entrycount].isdir), &dvdbuffer[diroffset + FILE_FLAGS], 1);
browserList[entrycount].offset <<= 11;
browserList[entrycount].isdir = browserList[entrycount].isdir & 2;
2008-08-16 02:02:08 +02:00
/*** Prepare for next entry ***/
2008-08-16 02:02:08 +02:00
diroffset += dvdbuffer[diroffset];
return 1;
}
}
2008-08-16 02:02:08 +02:00
return 0;
}
/****************************************************************************
* ParseDVDdirectory
*
* This function will parse the directory tree.
2008-08-16 02:02:08 +02:00
* It relies on dvddir and dvddirlength being pre-populated by a call to
* getpvd, a previous parse or a menu selection.
*
2009-03-11 18:28:37 +01:00
* The return value is number of files collected, or -1 on failure.
***************************************************************************/
int
2008-08-12 05:25:16 +02:00
ParseDVDdirectory ()
{
2008-08-16 02:02:08 +02:00
int pdlength;
u64 pdoffset;
u64 rdoffset;
int len = 0;
int filecount = 0;
2008-10-15 20:29:01 +02:00
unsigned char dvdbuffer[2048];
// reset browser
ResetBrowser();
2008-08-12 05:25:16 +02:00
2008-08-16 02:02:08 +02:00
pdoffset = rdoffset = dvddir;
pdlength = dvddirlength;
filecount = 0;
/*** Get as many files as possible ***/
2008-08-16 02:02:08 +02:00
while (len < pdlength)
{
if (dvd_read (&dvdbuffer, 2048, pdoffset) == 0)
2009-03-11 18:28:37 +01:00
return -1;
2008-08-16 02:02:08 +02:00
diroffset = 0;
2008-10-15 20:29:01 +02:00
while (getentry (filecount, dvdbuffer))
2008-08-16 02:02:08 +02:00
{
if(strlen(browserList[filecount].filename) > 0 && filecount < MAXDVDFILES)
2008-08-16 02:02:08 +02:00
filecount++;
}
len += 2048;
pdoffset = rdoffset + len;
}
// Sort the file list
qsort(browserList, filecount, sizeof(BROWSERENTRY), FileSortCallback);
2008-08-16 02:02:08 +02:00
2009-03-11 18:28:37 +01:00
browser.numEntries = filecount;
2008-08-16 02:02:08 +02:00
return filecount;
}
/****************************************************************************
* SetDVDdirectory
* Set the current DVD file offset
***************************************************************************/
void SetDVDdirectory(u64 dir, int length)
{
dvddir = dir;
dvddirlength = length;
}
/****************************************************************************
* DirectorySearch
*
* Searches for the directory name specified within the current directory
* Returns the index of the directory, or -1 if not found
***************************************************************************/
static int DirectorySearch(char dir[512])
2008-08-16 02:02:08 +02:00
{
for (int i = 0; i < browser.numEntries; i++ )
if (strcmp(browserList[i].filename, dir) == 0)
2008-08-16 02:02:08 +02:00
return i;
2008-08-17 07:25:33 +02:00
return -1;
2008-08-16 02:02:08 +02:00
}
/****************************************************************************
* SwitchDVDFolderR
*
* Recursively searches for any directory path 'dir' specified
2008-12-18 19:36:30 +01:00
* Also can be used to find and set the offset for a file
* Also loads the directory contents via ParseDVDdirectory()
* It relies on dvddir, dvddirlength, and filelist being pre-populated
***************************************************************************/
static bool SwitchDVDFolderR(char * dir, int maxDepth)
2008-08-16 02:02:08 +02:00
{
if(maxDepth > 8) // only search to a max depth of 8 levels
return false;
bool lastdir = false;
2008-08-17 07:25:33 +02:00
char * nextdir = NULL;
2008-08-16 02:02:08 +02:00
unsigned int t = strcspn(dir, "/");
2008-08-16 02:02:08 +02:00
if(t != strlen(dir))
nextdir = dir + t + 1; // next directory path to find
else
lastdir = true;
2008-08-16 02:02:08 +02:00
dir[t] = 0;
int dirindex = DirectorySearch(dir);
2008-08-17 07:25:33 +02:00
if(dirindex >= 0)
{
browser.selIndex = dirindex;
2008-11-13 06:44:21 +01:00
if(browserList[dirindex].isdir) // only parse directories
2009-03-19 08:46:51 +01:00
{
UpdateDirName(METHOD_DVD);
2009-03-11 18:28:37 +01:00
ParseDVDdirectory();
2009-03-19 08:46:51 +01:00
}
else
{
dvddir = browserList[dirindex].offset;
dvddirlength = browserList[dirindex].length;
}
2008-08-16 02:02:08 +02:00
if(lastdir)
return true;
else
return SwitchDVDFolderR(nextdir, maxDepth++);
}
2008-08-16 02:02:08 +02:00
return false;
}
2008-08-17 07:25:33 +02:00
bool SwitchDVDFolder(char origdir[])
2008-08-16 02:02:08 +02:00
{
2008-08-17 07:25:33 +02:00
// make a copy of origdir so we don't mess with original
2009-03-11 18:28:37 +01:00
char dir[1024];
strncpy(dir, origdir, 1024);
2008-08-17 07:25:33 +02:00
char * dirptr = dir;
2008-08-16 02:02:08 +02:00
// strip off leading/trailing slashes on the directory path
// we don't want to screw up our recursion!
if(dir[0] == '/')
2008-08-17 07:25:33 +02:00
dirptr = dirptr + 1;
2008-08-16 02:02:08 +02:00
if(dir[strlen(dir)-1] == '/')
dir[strlen(dir)-1] = 0;
2008-11-12 08:50:39 +01:00
// start searching at root of DVD
dvddir = dvdrootdir;
dvddirlength = dvdrootlength;
2009-03-11 18:28:37 +01:00
browser.dir[0] = 0;
2008-11-12 08:50:39 +01:00
ParseDVDdirectory();
return SwitchDVDFolderR(dirptr, 0);
}
2008-08-12 05:25:16 +02:00
/****************************************************************************
* LoadDVDFileOffset
* This function will load a file from DVD
* It assumes dvddir and dvddirlength are prepopulated
***************************************************************************/
2008-08-12 05:25:16 +02:00
int
2008-11-12 08:50:39 +01:00
LoadDVDFileOffset (unsigned char *buffer, int length)
2008-08-12 05:25:16 +02:00
{
2009-03-11 18:28:37 +01:00
int result = 0;
2008-08-16 02:02:08 +02:00
int offset;
int blocks;
int i;
2008-12-18 19:36:30 +01:00
int ret = 0;
2008-08-16 02:02:08 +02:00
u64 discoffset;
char readbuffer[2048];
// How many 2k blocks to read
blocks = dvddirlength / 2048;
offset = 0;
discoffset = dvddir;
2008-12-18 19:36:30 +01:00
ShowAction ("Loading...");
2008-08-16 02:02:08 +02:00
2008-10-15 08:35:14 +02:00
if(length > 0 && length <= 2048)
2008-08-16 02:02:08 +02:00
{
2008-12-18 19:36:30 +01:00
ret = dvd_read (buffer, length, discoffset);
if(ret <= 0) // read failure
2009-03-11 18:28:37 +01:00
goto done;
else
result = length;
}
else // load whole file
{
2008-12-18 19:36:30 +01:00
ret = dvd_read (readbuffer, 2048, discoffset);
if(ret <= 0) // read failure
2009-03-11 18:28:37 +01:00
goto done;
2008-11-12 08:50:39 +01:00
if (IsZipFile (readbuffer))
{
2009-03-11 18:28:37 +01:00
result = UnZipBuffer (buffer, METHOD_DVD); // unzip from dvd
2008-11-12 08:50:39 +01:00
}
else
2008-08-16 02:02:08 +02:00
{
for (i = 0; i < blocks; i++)
{
2008-12-18 19:36:30 +01:00
ret = dvd_read (readbuffer, 2048, discoffset);
if(ret <= 0) // read failure
2009-03-11 18:28:37 +01:00
goto done;
memcpy (buffer + offset, readbuffer, 2048);
offset += 2048;
discoffset += 2048;
2008-12-18 19:36:30 +01:00
ShowProgress ("Loading...", offset, length);
}
2008-08-12 05:25:16 +02:00
/*** And final cleanup ***/
if (dvddirlength % 2048)
{
i = dvddirlength % 2048;
2008-12-18 19:36:30 +01:00
ret = dvd_read (readbuffer, 2048, discoffset);
if(ret <= 0) // read failure
2009-03-11 18:28:37 +01:00
goto done;
memcpy (buffer + offset, readbuffer, i);
}
2009-03-11 18:28:37 +01:00
result = dvddirlength;
}
2008-08-16 02:02:08 +02:00
}
2009-03-11 18:28:37 +01:00
done:
CancelAction();
return result;
2008-08-12 05:25:16 +02:00
}
/****************************************************************************
* LoadDVDFile
* This function will load a file from DVD, given a filepath
* It will attempt to find the offset of the file, and if successful it
* will populate dvddir and dvddirlength, and load the file
***************************************************************************/
2008-11-12 08:50:39 +01:00
int
LoadDVDFile(char * buffer, char *filepath, int datasize, bool silent)
{
2009-03-19 08:46:51 +01:00
int ret = 0;
// retain original browser information
char origDir[MAXPATHLEN];
memset(origDir, 0, MAXPATHLEN);
strncpy(origDir, browser.dir, MAXPATHLEN);
int origSelIndex = browser.selIndex;
int origPageIndex = browser.selIndex;
2008-11-12 08:50:39 +01:00
if(SwitchDVDFolder(filepath))
2009-03-19 08:46:51 +01:00
ret = LoadDVDFileOffset ((unsigned char *)buffer, datasize);
else if(!silent)
ErrorPrompt("Error loading file!");
// restore browser information
memset(browser.dir, 0, MAXPATHLEN);
strncpy(browser.dir, origDir, MAXPATHLEN);
browser.selIndex = origSelIndex;
browser.pageIndex = origPageIndex;
return ret;
2008-11-12 08:50:39 +01:00
}
/****************************************************************************
* uselessinquiry
*
* As the name suggests, this function is quite useless.
* It's only purpose is to stop any pending DVD interrupts while we use the
* memcard interface.
*
* libOGC tends to foul up if you don't, and sometimes does if you do!
***************************************************************************/
#ifdef HW_DOL
void uselessinquiry ()
{
2008-08-16 02:02:08 +02:00
dvd[0] = 0;
dvd[1] = 0;
dvd[2] = 0x12000000;
dvd[3] = 0;
dvd[4] = 0x20;
dvd[5] = 0x80000000;
dvd[6] = 0x20;
dvd[7] = 1;
2009-03-17 06:09:45 +01:00
while (dvd[7] & 1);
}
/****************************************************************************
* dvd_motor_off( )
* Turns off DVD drive motor so it doesn't make noise (Gamecube only)
***************************************************************************/
2008-09-28 05:12:15 +02:00
void dvd_motor_off ()
{
dvd[0] = 0x2e;
dvd[1] = 0;
dvd[2] = 0xe3000000;
dvd[3] = 0;
dvd[4] = 0;
dvd[5] = 0;
dvd[6] = 0;
dvd[7] = 1; // Do immediate
2009-03-17 06:09:45 +01:00
while (dvd[7] & 1);
/*** PSO Stops blackscreen at reload ***/
dvd[0] = 0x14;
dvd[1] = 0;
}
2008-08-20 09:58:55 +02:00
/****************************************************************************
* dvd_driveid
*
* Gets and returns the dvd driveid
***************************************************************************/
2008-08-20 09:58:55 +02:00
int dvd_driveid()
{
static unsigned char *inquiry=(unsigned char *)0x80000004;
dvd[0] = 0x2e;
dvd[1] = 0;
dvd[2] = 0x12000000;
dvd[3] = 0;
dvd[4] = 0x20;
dvd[5] = 0x80000000;
dvd[6] = 0x20;
dvd[7] = 3;
2009-03-17 06:09:45 +01:00
while( dvd[7] & 1 );
2009-03-11 18:28:37 +01:00
2008-08-20 09:58:55 +02:00
DCFlushRange((void *)0x80000000, 32);
return (int)inquiry[2];
}
2008-09-10 07:57:37 +02:00
#endif
/****************************************************************************
* SetDVDDriveType()
*
* Sets the DVD drive ID for use to determine disc size (1.5 GB or 4.7 GB)
***************************************************************************/
2008-09-10 07:57:37 +02:00
void SetDVDDriveType()
{
#ifdef HW_RVL
isWii = true;
#else
int drvid = dvd_driveid ();
if ( drvid == 4 || drvid == 6 || drvid == 8 )
isWii = false;
else
isWii = true;
#endif
}