aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxcd/isofs.c
diff options
context:
space:
mode:
authorJohn "Lameguy" Wilbert Villamor <lameguy64@gmail.com>2022-10-19 17:57:06 +0800
committerGitHub <noreply@github.com>2022-10-19 17:57:06 +0800
commite08a3d9366f8ca14a76b3dd569dac1fb9f569748 (patch)
tree33654513b0b184c27f8035dbc405640fcbeb44ab /libpsn00b/psxcd/isofs.c
parentc4a2533d21dfd05cde841ea48c67b05e0e6a853f (diff)
parent9b2ffc6078a850b7d354855cca7622090b41f30c (diff)
downloadpsn00bsdk-e08a3d9366f8ca14a76b3dd569dac1fb9f569748.tar.gz
Merge pull request #59 from spicyjpeg/psxmdec
IRQ handler fix, .STR playback example, multiple library builds (v0.21)
Diffstat (limited to 'libpsn00b/psxcd/isofs.c')
-rw-r--r--libpsn00b/psxcd/isofs.c285
1 files changed, 110 insertions, 175 deletions
diff --git a/libpsn00b/psxcd/isofs.c b/libpsn00b/psxcd/isofs.c
index 582b8d9..0425c0d 100644
--- a/libpsn00b/psxcd/isofs.c
+++ b/libpsn00b/psxcd/isofs.c
@@ -1,15 +1,12 @@
#include <stdint.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <psxgpu.h>
-#include <psxsio.h>
+#include <psxapi.h>
+#include <psxetc.h>
#include "psxcd.h"
#include "isofs.h"
-// Uncommend to enable debug output
-//#define DEBUG
-
#define DEFAULT_PATH_SEP '\\'
#define IS_PATH_SEP(ch) (((ch) == '/') || ((ch) == '\\'))
@@ -46,9 +43,8 @@ static int _CdReadIsoDescriptor(int session_offs)
CdControl(CdlNop, 0, 0);
if( (CdStatus()&0x10) )
{
-#ifdef DEBUG
- printf("psxcd: Lid is still open.\n");
-#endif
+ _sdk_log("psxcd: Lid is still open.\n");
+
_cd_iso_error = CdlIsoLidOpen;
return -1;
}
@@ -61,61 +57,47 @@ static int _CdReadIsoDescriptor(int session_offs)
{
return 0;
}
-
-#ifdef DEBUG
- printf("psxcd: Parsing ISO file system.\n");
-#endif
+
+ _sdk_log("psxcd: Parsing ISO file system.\n");
// Seek to volume descriptor
CdIntToPos(16+session_offs, &loc);
if( !CdControl(CdlSetloc, (uint8_t*)&loc, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Could not set seek destination.\n");
-#endif
+ _sdk_log("psxcd: Could not set seek destination.\n");
+
_cd_iso_error = CdlIsoSeekError;
return -1;
}
-#ifdef DEBUG
- printf("psxcd: Set seek target.\n");
-#endif
-
-#ifdef DEBUG
- printf("psxcd: Read sectors.\n");
-#endif
+ _sdk_log("psxcd: Read sectors.\n");
+
// Read volume descriptor
CdRead(1, (uint32_t*)_cd_iso_descriptor_buff, CdlModeSpeed);
if( CdReadSync(0, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Error reading ISO volume descriptor.\n");
-#endif
+ _sdk_log("psxcd: Error reading ISO volume descriptor.\n");
+
_cd_iso_error = CdlIsoReadError;
return -1;
}
-
-#ifdef DEBUG
- printf("psxcd: Read complete.\n");
-#endif
-
+
+ _sdk_log("psxcd: Read complete.\n");
+
// Verify if volume descriptor is present
descriptor = (ISO_DESCRIPTOR*)_cd_iso_descriptor_buff;
if( strncmp("CD001", descriptor->header.id, 5) )
{
-#ifdef DEBUG
- printf("psxcd: Disc does not contain a ISO9660 file system.\n");
-#endif
+ _sdk_log("psxcd: Disc does not contain a ISO9660 file system.\n");
+
_cd_iso_error = CdlIsoInvalidFs;
return -1;
}
-
-#ifdef DEBUG
- printf("psxcd_dbg: Path table LBA = %d\n", descriptor->pathTable1Offs);
- printf("psxcd_dbg: Path table len = %d\n", descriptor->pathTableSize.lsb);
-#endif
-
+
+ _sdk_log("psxcd: Path table LBA = %d\n", descriptor->pathTable1Offs);
+ _sdk_log("psxcd: Path table len = %d\n", descriptor->pathTableSize.lsb);
+
// Allocate path table buffer
i = ((2047+descriptor->pathTableSize.lsb)>>11)<<11;
if( _cd_iso_pathtable_buff )
@@ -123,20 +105,17 @@ static int _CdReadIsoDescriptor(int session_offs)
free(_cd_iso_pathtable_buff);
}
_cd_iso_pathtable_buff = (uint8_t*)malloc(i);
-
-#ifdef DEBUG
- printf("psxcd_dbg: Allocated %d bytes for path table.\n", i);
-#endif
-
+
+ _sdk_log("psxcd: Allocated %d bytes for path table.\n", i);
+
// Read path table
CdIntToPos(descriptor->pathTable1Offs, &loc);
CdControl(CdlSetloc, (uint8_t*)&loc, 0);
CdRead(i>>11, (uint32_t*)_cd_iso_pathtable_buff, CdlModeSpeed);
if( CdReadSync(0, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Error reading ISO path table.\n");
-#endif
+ _sdk_log("psxcd: Error reading ISO path table.\n");
+
_cd_iso_error = CdlIsoReadError;
return -1;
}
@@ -162,14 +141,13 @@ static int _CdReadIsoDirectory(int lba)
CdIntToPos(lba, &loc);
i = CdPosToInt(&loc);
-#ifdef DEBUG
- printf("psxcd_dbg: Seek to sector %d\n", i);
-#endif
+
+ _sdk_log("psxcd: Seek to sector %d\n", i);
+
if( !CdControl(CdlSetloc, (uint8_t*)&loc, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Could not set seek destination.\n");
-#endif
+ _sdk_log("psxcd: Could not set seek destination.\n");
+
_cd_iso_error = CdlIsoSeekError;
return -1;
}
@@ -184,28 +162,24 @@ static int _CdReadIsoDirectory(int lba)
CdRead(1, (uint32_t*)_cd_iso_directory_buff, CdlModeSpeed);
if( CdReadSync(0, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Error reading initial directory record.\n");
-#endif
+ _sdk_log("psxcd: Error reading initial directory record.\n");
+
_cd_iso_error = CdlIsoReadError;
return -1;
}
direntry = (ISO_DIR_ENTRY*)_cd_iso_directory_buff;
_cd_iso_directory_len = direntry->entrySize.lsb;
-
-#ifdef DEBUG
- printf("psxcd_dbg: Location of directory record = %d\n", direntry->entryOffs.lsb);
- printf("psxcd_dbg: Size of directory record = %d\n", _cd_iso_directory_len);
-#endif
+
+ _sdk_log("psxcd: Location of directory record = %d\n", direntry->entryOffs.lsb);
+ _sdk_log("psxcd: Size of directory record = %d\n", _cd_iso_directory_len);
if( _cd_iso_directory_len > 2048 )
{
if( !CdControl(CdlSetloc, (uint8_t*)&loc, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Could not set seek destination.\n");
-#endif
+ _sdk_log("psxcd: Could not set seek destination.\n");
+
_cd_iso_error = CdlIsoSeekError;
return -1;
}
@@ -213,16 +187,14 @@ static int _CdReadIsoDirectory(int lba)
free(_cd_iso_directory_buff);
i = ((2047+_cd_iso_directory_len)>>11)<<11;
_cd_iso_directory_buff = (uint8_t*)malloc(i);
-#ifdef DEBUG
- printf("psxcd_dbg: Allocated %d bytes for directory record.\n", i);
-#endif
+
+ _sdk_log("psxcd: Allocated %d bytes for directory record.\n", i);
CdRead(i>>11, (uint32_t*)_cd_iso_directory_buff, CdlModeSpeed);
if( CdReadSync(0, 0) )
{
-#ifdef DEBUG
- printf("psxcd: Error reading remaining directory record.\n");
-#endif
+ _sdk_log("psxcd: Error reading remaining directory record.\n");
+
_cd_iso_error = CdlIsoReadError;
return -1;
}
@@ -234,7 +206,7 @@ static int _CdReadIsoDirectory(int lba)
return 0;
}
-#ifdef DEBUG
+#ifndef NDEBUG
static void dump_directory(void)
{
@@ -243,7 +215,7 @@ static void dump_directory(void)
ISO_DIR_ENTRY *dir_entry;
char namebuff[16];
- printf("psxcd_dbg: Cached directory record contents:\n");
+ _sdk_log("psxcd: Cached directory record contents:\n");
i = 0;
dir_pos = 0;
@@ -254,7 +226,7 @@ static void dump_directory(void)
strncpy(namebuff,
_cd_iso_directory_buff+dir_pos+sizeof(ISO_DIR_ENTRY), dir_entry->identifierLen);
- printf("P:%d L:%d %s\n", dir_pos, dir_entry->identifierLen, namebuff);
+ _sdk_log("psxcd: P:%d L:%d %s\n", dir_pos, dir_entry->identifierLen, namebuff);
dir_pos += dir_entry->entryLength;
i++;
@@ -273,7 +245,7 @@ static void dump_directory(void)
}
}
- printf("--\n");
+ _sdk_log("psxcd: --\n");
}
@@ -284,7 +256,7 @@ static void dump_pathtable(void)
ISO_DESCRIPTOR *descriptor;
char namebuff[16];
- printf("psxcd_dbg: Path table entries:\n");
+ _sdk_log("psxcd: Path table entries:\n");
descriptor = (ISO_DESCRIPTOR*)_cd_iso_descriptor_buff;
@@ -298,7 +270,7 @@ static void dump_pathtable(void)
tbl_pos+sizeof(ISO_PATHTABLE_ENTRY),
tbl_entry->nameLength);
- printf("psxcd_dbg: %s\n", namebuff);
+ _sdk_log("psxcd: %s\n", namebuff);
// Advance to next entry
tbl_pos += sizeof(ISO_PATHTABLE_ENTRY)
@@ -393,10 +365,8 @@ static int find_dir_entry(const char *name, ISO_DIR_ENTRY *dirent)
int dir_pos;
ISO_DIR_ENTRY *dir_entry;
char namebuff[16];
-
-#ifdef DEBUG
- printf( "psxcd_dbg: Locating file %s.\n", name );
-#endif
+
+ _sdk_log("psxcd: Locating file %s.\n", name);
i = 0;
dir_pos = 0;
@@ -434,8 +404,8 @@ static int find_dir_entry(const char *name, ISO_DIR_ENTRY *dirent)
static char* get_pathname(char *path, const char *filename)
{
- char *c = 0;
- for (char *i = filename; *i; i++) {
+ const char *c = 0;
+ for (const char *i = filename; *i; i++) {
if (IS_PATH_SEP(*i))
c = i;
}
@@ -453,8 +423,8 @@ static char* get_pathname(char *path, const char *filename)
static char* get_filename(char *name, const char *filename)
{
- char *c = 0;
- for (char *i = filename; *i; i++) {
+ const char *c = 0;
+ for (const char *i = filename; *i; i++) {
if (IS_PATH_SEP(*i))
c = i;
}
@@ -489,40 +459,35 @@ CdlFILE *CdSearchFile(CdlFILE *fp, const char *filename)
// Read ISO descriptor and path table
if( _CdReadIsoDescriptor(0) )
{
-#ifdef DEBUG
- printf("psxcd: Could not read ISO file system.\n");
-#endif
+ _sdk_log("psxcd: Could not read ISO file system.\n");
return NULL;
}
-#ifdef DEBUG
- // printf("psxcd: ISO file system cache updated.\n");
-#endif
+
+ // _sdk_log("psxcd: ISO file system cache updated.\n");
// _cd_media_changed = 0;
//}
// Get number of directories in path table
num_dirs = get_pathtable_entry(0, NULL, NULL);
-#ifdef DEBUG
- printf("psxcd_dbg: Directories in path table: %d\n", num_dirs);
+#ifndef NDEBUG
+ _sdk_log("psxcd: Directories in path table: %d\n", num_dirs);
rbuff = resolve_pathtable_path(num_dirs-1, tpath_rbuff+127);
if( !rbuff )
{
- printf("psxcd_dbg: Could not resolve path.\n");
+ _sdk_log("psxcd: Could not resolve path.\n");
}
else
{
- printf("psxcd_dbg: Longest path: %s|\n", rbuff);
+ _sdk_log("psxcd: Longest path: %s|\n", rbuff);
}
#endif
if( get_pathname(search_path, filename) )
{
-#ifdef DEBUG
- printf("psxcd_dbg: Search path = %s|\n", search_path);
-#endif
+ _sdk_log("psxcd: Search path = %s|\n", search_path);
}
// Search the pathtable for a matching path
@@ -530,9 +495,8 @@ CdlFILE *CdSearchFile(CdlFILE *fp, const char *filename)
for(i=1; i<num_dirs; i++)
{
rbuff = resolve_pathtable_path(i, tpath_rbuff+127);
-#ifdef DEBUG
- printf("psxcd_dbg: Found = %s|\n", rbuff);
-#endif
+ _sdk_log("psxcd: Found = %s|\n", rbuff);
+
if( rbuff )
{
if( strcmp(search_path, rbuff) == 0 )
@@ -545,24 +509,16 @@ CdlFILE *CdSearchFile(CdlFILE *fp, const char *filename)
if( !found_dir )
{
-#ifdef DEBUG
- printf("psxcd_dbg: Directory path not found.\n");
-#endif
+ _sdk_log("psxcd: Directory path not found.\n");
return NULL;
}
-
-#ifdef DEBUG
- printf("psxcd_dbg: Found directory at record %d!\n", found_dir);
-#endif
- get_pathtable_entry(found_dir, &tbl_entry, NULL);
+ _sdk_log("psxcd: Found directory at record %d!\n", found_dir);
-#ifdef DEBUG
- printf("psxcd_dbg: Directory LBA = %d\n", tbl_entry.dirOffs);
-#endif
+ get_pathtable_entry(found_dir, &tbl_entry, NULL);
+ _sdk_log("psxcd: Directory LBA = %d\n", tbl_entry.dirOffs);
_CdReadIsoDirectory(tbl_entry.dirOffs);
-
get_filename(fp->name, filename);
// Add version number if not specified
@@ -571,21 +527,18 @@ CdlFILE *CdSearchFile(CdlFILE *fp, const char *filename)
strcat(fp->name, ";1");
}
-#ifdef DEBUG
- dump_directory();
+#ifndef NDEBUG
+ //dump_directory();
#endif
if( find_dir_entry(fp->name, &dir_entry) )
{
-#ifdef DEBUG
- printf("psxcd: Could not find file.\n");
-#endif
+ _sdk_log("psxcd: Could not find file.\n");
+
return NULL;
}
-
-#ifdef DEBUG
- printf("psxcd_dbg: Located file at LBA %d.\n", dir_entry.entryOffs.lsb);
-#endif
+
+ _sdk_log("psxcd: Located file at LBA %d.\n", dir_entry.entryOffs.lsb);
CdIntToPos(dir_entry.entryOffs.lsb, &fp->pos);
fp->size = dir_entry.entrySize.lsb;
@@ -609,14 +562,11 @@ CdlDIR *CdOpenDir(const char* path)
// Read ISO descriptor and path table
if( _CdReadIsoDescriptor( 0 ) )
{
-#ifdef DEBUG
- printf( "psxcd: Could not read ISO file system.\n" );
-#endif
+ _sdk_log( "psxcd: Could not read ISO file system.\n" );
return NULL;
}
-#ifdef DEBUG
-// printf( "psxcd: ISO file system cache updated.\n" );
-#endif
+
+// _sdk_log( "psxcd: ISO file system cache updated.\n" );
// _cd_media_changed = 0;
// }
@@ -626,9 +576,8 @@ CdlDIR *CdOpenDir(const char* path)
for( i=1; i<num_dirs; i++ )
{
rbuff = resolve_pathtable_path( i, tpath_rbuff+127 );
-#ifdef DEBUG
- printf( "psxcd_dbg: Found = %s|\n", rbuff );
-#endif
+ _sdk_log( "psxcd: Found = %s|\n", rbuff );
+
if( rbuff )
{
if( strcmp( path, rbuff ) == 0 )
@@ -641,21 +590,14 @@ CdlDIR *CdOpenDir(const char* path)
if( !found_dir )
{
-#ifdef DEBUG
- printf( "psxcd_dbg: Directory path not found.\n" );
-#endif
+ _sdk_log( "psxcd: Directory path not found.\n" );
return NULL;
}
-
-#ifdef DEBUG
- printf( "psxcd_dbg: Found directory at record %d!\n", found_dir );
-#endif
+
+ _sdk_log( "psxcd: Found directory at record %d!\n", found_dir );
get_pathtable_entry( found_dir, &tbl_entry, NULL );
-
-#ifdef DEBUG
- printf( "psxcd_dbg: Directory LBA = %d\n", tbl_entry.dirOffs );
-#endif
+ _sdk_log( "psxcd: Directory LBA = %d\n", tbl_entry.dirOffs );
_CdReadIsoDirectory( tbl_entry.dirOffs );
@@ -720,15 +662,11 @@ int CdReadDir(CdlDIR *dir, CdlFILE* file)
file->size = dir_entry->entrySize.lsb;
-#ifdef DEBUG
- printf("dir_entry->entryLength = %d, ", dir_entry->entryLength);
-#endif
-
+ _sdk_log("psxcd: dir_entry->entryLength = %d, ", dir_entry->entryLength);
+
d_dir->_pos += dir_entry->entryLength;
-
-#ifdef DEBUG
- printf("d_dir->_pos = %d\n", d_dir->_pos);
-#endif
+
+ _sdk_log("psxcd: d_dir->_pos = %d\n", d_dir->_pos);
// Check if padding is reached (end of record sector)
if( d_dir->_dir[d_dir->_pos] == 0 )
@@ -832,19 +770,13 @@ int CdLoadSession(int session)
int i;
// Seek to specified session
-#ifdef DEBUG
- printf("psxcd: CdLoadSession(): Seeking to session %d...\n", session);
-#endif
+ _sdk_log("psxcd: CdLoadSession(): Seeking to session %d...\n", session);
CdControl(CdlSetsession, (unsigned char*)&session,
(unsigned char*)&resultbuff);
if( CdSync(0, 0) == CdlDiskError )
{
-#ifdef DEBUG
- printf("psxcd: CdLoadSession(): Session seek failed, "
- "session does not exist.\n");
- printf("psxcd: CdLoadSession(): Restarting CD-ROM...\n");
-#endif
+ _sdk_log("psxcd: CdLoadSession(): Session seek failed, session does not exist. Restarting CD-ROM...\n");
// Restart CD-ROM on session seek failure
CdControl(CdlNop, 0, 0);
@@ -855,39 +787,46 @@ int CdLoadSession(int session)
}
// Set search routine callback
+ EnterCriticalSection();
ready_oldcb = CdReadyCallback(_scan_callback);
-
+ ExitCriticalSection();
+
_ses_scanfound = 0;
_ses_scancount = 0;
_ses_scancomplete = 0;
_ses_scanbuff = scanbuff;
// Begin scan for an ISO volume descriptor
-#ifdef DEBUG
- printf("psxcd: CdLoadSession(): Scanning for ISO9660 volume descriptor.\n");
-#endif
+ _sdk_log("psxcd: CdLoadSession(): Scanning for ISO9660 volume descriptor.\n");
+
i = CdlModeSpeed;
CdControl(CdlSetmode, (unsigned char*)&i, 0);
CdControl(CdlReadN, 0, (unsigned char*)resultbuff);
// Wait until scan complete
while(!_ses_scancomplete);
-
+
+ EnterCriticalSection();
CdReadyCallback((void*)_ready_oldcb);
-
+ ExitCriticalSection();
+
if( !_ses_scanfound )
{
-#ifdef DEBUG
- printf("psxcd: CdLoadSession(): Did not find volume descriptor.\n");
-#endif
+ _sdk_log("psxcd: CdLoadSession(): Did not find volume descriptor.\n");
+
_cd_iso_error = CdlIsoInvalidFs;
+ EnterCriticalSection();
CdReadyCallback((CdlCB)ready_oldcb);
+ ExitCriticalSection();
+
return -1;
}
// Restore old callback if any
+ EnterCriticalSection();
CdReadyCallback((CdlCB)ready_oldcb);
-
+ ExitCriticalSection();
+
// Wait until CD-ROM has completely stopped reading, to get a consistent
// fix of the CD-ROM pickup's current location
do
@@ -901,16 +840,12 @@ int CdLoadSession(int session)
CdSync(0, 0);
loc = (CdlLOC*)resultbuff;
-
-#ifdef DEBUG
- printf("psxcd: CdLoadSession(): Session found in %02d:%02d:%02d (LBA=%d)\n",
+
+ _sdk_log("psxcd: CdLoadSession(): Session found in %02d:%02d:%02d (LBA=%d)\n",
btoi(loc->minute), btoi(loc->second), btoi(loc->sector), CdPosToInt(loc));
-#endif
-
+
i = CdPosToInt(loc)-17;
-#ifdef DEBUG
- printf("psxcd: CdLoadSession(): Session starting at LBA=%d\n", i);
-#endif
+ _sdk_log("psxcd: CdLoadSession(): Session starting at LBA=%d\n", i);
_cd_media_changed = 1;