Genesis-Plus-GX/source/gx/fileio/file_dvd.c

408 lines
11 KiB
C
Raw Normal View History

2008-12-11 18:38:29 +01:00
/*
* file_dvd.c
*
* ISO9660/Joliet DVD loading support
2008-12-11 18:38:29 +01:00
*
* Softdev (2006)
* Eke-Eke (2007,2008,2009)
2008-12-11 18:38:29 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
2008-12-11 18:38:29 +01:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2008-12-10 19:16:30 +01:00
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-12-10 19:16:30 +01:00
*
2008-12-11 18:38:29 +01:00
********************************************************************************/
2008-12-10 19:16:30 +01:00
#include "shared.h"
#include "gui.h"
2008-12-10 19:16:30 +01:00
#include "dvd.h"
2008-12-11 18:38:29 +01:00
#include "unzip.h"
#include "filesel.h"
#include "file_fat.h"
#include "file_dvd.h"
2008-12-10 19:16:30 +01:00
/** 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 Variables **/
static u64 rootdir = 0;
static u64 basedir = 0;
static int rootdirlength = 0;
2008-12-10 19:16:30 +01:00
static int IsJoliet = 0;
static int diroffset = 0;
static int haveDVDdir = 0;
2008-12-10 19:16:30 +01:00
static char dvdbuffer[2048];
/****************************************************************************
* Primary Volume Descriptor
*
* The PVD should reside between sector 16 and 31.
* This is for single session DVD only.
****************************************************************************/
static int getpvd()
2008-12-10 19:16:30 +01:00
{
int sector = 16;
u32 rootdir32;
basedir = rootdirlength = 0;
IsJoliet = -1;
/** Look for Joliet PVD first **/
while (sector < 32)
{
if (dvd_read (&dvdbuffer, 2048, (u64)(sector << 11)))
{
if (memcmp (&dvdbuffer, "\2CD001\1", 8) == 0)
{
memcpy(&rootdir32, &dvdbuffer[PVDROOT + EXTENT], 4);
basedir = (u64)rootdir32;
memcpy (&rootdirlength, &dvdbuffer[PVDROOT + FILE_LENGTH], 4);
basedir <<= 11;
IsJoliet = 1;
break;
}
}
else return 0; /*** Can't read sector! ***/
sector++;
}
if (IsJoliet > 0) return 1; /*** Joliet PVD Found ? ***/
/*** Look for standard ISO9660 PVD ***/
sector = 16;
while (sector < 32)
{
if (dvd_read (&dvdbuffer, 2048, (u64)(sector << 11)))
{
if (memcmp (&dvdbuffer, "\1CD001\1", 8) == 0)
{
memcpy (&rootdir32, &dvdbuffer[PVDROOT + EXTENT], 4);
basedir = (u64)rootdir32;
memcpy (&rootdirlength, &dvdbuffer[PVDROOT + FILE_LENGTH], 4);
IsJoliet = 0;
basedir <<= 11;
break;
}
}
else return 0; /*** Can't read sector! ***/
sector++;
}
return (IsJoliet == 0);
}
/****************************************************************************
* getentry
*
* Support function to return the next file entry, if any
* Declared static to avoid accidental external entry.
****************************************************************************/
static int getentry(int entrycount)
2008-12-10 19:16:30 +01:00
{
char fname[512]; /* Huge, but experience has determined this */
char *ptr;
char *filename;
char *filenamelength;
char *rr;
int j;
u32 offset32;
/* Basic checks */
if (entrycount >= MAXFILES) return 0;
if (diroffset >= 2048) return 0;
/** Decode this entry **/
if (dvdbuffer[diroffset]) /* Record length available */
{
/* 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) return 0;
if (*filenamelength)
{
memset (&fname, 0, 512);
/*** Do ISO 9660 first ***/
if (!IsJoliet) strcpy (fname, filename);
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;
if (strlen (fname) == 0) fname[0] = filename[0];
}
if (strlen (fname) == 0) strcpy (fname, ".");
2008-12-10 19:16:30 +01:00
else
{
if (fname[0] == 1) strcpy (fname, "..");
else
{
/*
* Move *filenamelength to t,
* Only to stop gcc warning for noobs :)
*/
int t = *filenamelength;
fname[t] = 0;
}
}
/** Rockridge Check **/
rr = strstr (fname, ";");
if (rr != NULL) *rr = 0;
strcpy (filelist[entrycount].filename, fname);
memcpy (&offset32, &dvdbuffer[diroffset + EXTENT], 4);
filelist[entrycount].offset = (u64)offset32;
memcpy (&filelist[entrycount].length, &dvdbuffer[diroffset + FILE_LENGTH], 4);
memcpy (&filelist[entrycount].flags, &dvdbuffer[diroffset + FILE_FLAGS], 1);
filelist[entrycount].offset <<= 11;
filelist[entrycount].flags = filelist[entrycount].flags & 2;
/*** Prepare for next entry ***/
diroffset += dvdbuffer[diroffset];
return 1;
}
}
return 0;
}
/***************************************************************************
* DVD_ClearDirectory
*
* Clear DVD directory flag
***************************************************************************/
void DVD_ClearDirectory(void)
{
haveDVDdir = 0;
}
/***************************************************************************
* DVD_UpdateDirectory
2008-12-10 19:16:30 +01:00
*
* Update DVD current root directory
***************************************************************************/
int DVD_UpdateDirectory(bool go_up, u64 offset, u32 length)
2008-12-10 19:16:30 +01:00
{
/* root has no parent directory */
if (go_up && (basedir == rootdir)) return 0;
/* simply update current root directory */
rootdir = offset;
rootdirlength = length;
2008-12-10 19:16:30 +01:00
return 1;
}
/****************************************************************************
* DVD_ParseDirectory
*
* This function will parse the directory tree.
* It relies on rootdir and rootdirlength being pre-populated by a call to
* getpvd, a previous parse or a menu selection.
*
* The return value is number of files collected, or 0 on failure.
****************************************************************************/
int DVD_ParseDirectory(void)
2008-12-10 19:16:30 +01:00
{
int pdlength;
u64 pdoffset;
u64 rdoffset;
int len = 0;
int filecount = 0;
pdoffset = rdoffset = rootdir;
pdlength = rootdirlength;
filecount = 0;
/** Clear any existing values ***/
memset (&filelist, 0, sizeof (FILEENTRIES) * MAXFILES);
/*** Get as many files as possible ***/
while (len < pdlength)
{
if (dvd_read (&dvdbuffer, 2048, pdoffset) == 0) return 0;
2008-12-10 19:16:30 +01:00
diroffset = 0;
while (getentry (filecount))
2008-12-10 19:16:30 +01:00
{
2009-05-07 15:00:57 +02:00
if (strcmp(filelist[filecount].filename,".") && (filecount < MAXFILES))
filecount++;
2008-12-10 19:16:30 +01:00
}
len += 2048;
pdoffset = rdoffset + len;
}
/* Sort the file list */
qsort(filelist, filecount, sizeof(FILEENTRIES), FileSortCallback);
2008-12-10 19:16:30 +01:00
return filecount;
}
/****************************************************************************
* DVD_LoadFile
*
* This function will load a BIN, SMD or ZIP file from DVD into the ROM buffer.
* The index values indicates the file position in filentry list
* This functions return the actual size of data copied into the buffer
*
****************************************************************************/
int DVD_LoadFile(u8 *buffer, u32 selection)
2008-12-10 19:16:30 +01:00
{
/* file size */
int length = filelist[selection].length;
if (length > 0)
{
/* Read first data chunk */
char readbuffer[2048];
u64 discoffset = filelist[selection].offset;
dvd_read (&readbuffer, 2048, discoffset);
/* determine file type */
if (!IsZipFile ((char *) readbuffer))
{
2008-12-11 18:38:29 +01:00
char msg[50];
sprintf(msg,"Loading %d bytes...", length);
GUI_MsgBoxOpen("Information",msg,1);
2008-12-10 19:16:30 +01:00
/* How many 2k blocks to read */
int blocks = length / 2048;
int readoffset = 0;
int i;
/* read data chunks */
for (i = 0; i < blocks; i++)
{
dvd_read(readbuffer, 2048, discoffset);
discoffset += 2048;
memcpy (buffer + readoffset, readbuffer, 2048);
readoffset += 2048;
}
/* final read */
i = length % 2048;
if (i)
{
dvd_read (readbuffer, 2048, discoffset);
memcpy (buffer + readoffset, readbuffer, i);
}
return length;
}
else
{
2008-12-11 18:38:29 +01:00
return UnZipBuffer (buffer, discoffset, NULL);
2008-12-10 19:16:30 +01:00
}
}
return 0;
}
/****************************************************************************
* DVD_Open
*
* Function to load a DVD directory and display to user.
****************************************************************************/
int DVD_Open(void)
2008-12-10 19:16:30 +01:00
{
/* is DVD mounted ? */
if (!getpvd())
{
/* mount DVD */
GUI_MsgBoxOpen("Information", "Mounting DVD ...",1);
2008-12-10 19:16:30 +01:00
#ifdef HW_RVL
u32 val;
DI_GetCoverRegister(&val);
if(val & 0x1)
{
GUI_WaitPrompt("Error","No Disc inserted !");
2008-12-10 19:16:30 +01:00
return 0;
}
DI_Mount();
while(DI_GetStatus() & DVD_INIT) usleep(10);
2008-12-10 19:16:30 +01:00
if (!(DI_GetStatus() & DVD_READY))
{
char msg[50];
sprintf(msg, "DI Status Error: 0x%08X !\n",DI_GetStatus());
GUI_WaitPrompt("Error",msg);
2008-12-10 19:16:30 +01:00
return 0;
}
#else
DVD_Mount();
#endif
haveDVDdir = 0;
if (!getpvd())
{
GUI_WaitPrompt("Error","Disc can not be read !");
2008-12-10 19:16:30 +01:00
return 0;
}
GUI_MsgBoxClose();
2008-12-10 19:16:30 +01:00
}
if (haveDVDdir == 0)
{
/* reset root directory */
rootdir = basedir;
/* parse root directory */
int max = DVD_ParseDirectory ();
if (max)
{
/* set DVD as default */
haveDVDdir = 1;
FAT_ClearDirectory();
2008-12-10 19:16:30 +01:00
/* reset File selector */
ClearSelector(max);
return 1;
2008-12-10 19:16:30 +01:00
}
else
{
/* no entries found */
GUI_WaitPrompt("Error","No files found !");
2008-12-10 19:16:30 +01:00
return 0;
}
}
return 1;
2008-12-10 19:16:30 +01:00
}