Skip to content

Instantly share code, notes, and snippets.

@kode54
Created March 2, 2015 06:00
Show Gist options
  • Save kode54/7de3d2f291671afb3139 to your computer and use it in GitHub Desktop.
Save kode54/7de3d2f291671afb3139 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <string.h>
#include "psflib.h"
uint32_t ROM_SIZE;
uint8_t ROM[0x4000000];
uint8_t savestatespace[0x80275c];
static void * stdio_fopen( const char * path )
{
return fopen( path, "rb" );
}
static size_t stdio_fread( void *p, size_t size, size_t count, void *f )
{
return fread( p, size, count, (FILE*) f );
}
static int stdio_fseek( void * f, int64_t offset, int whence )
{
return fseek( (FILE*) f, offset, whence );
}
static int stdio_fclose( void * f )
{
return fclose( (FILE*) f );
}
static long stdio_ftell( void * f )
{
return ftell( (FILE*) f );
}
static psf_file_callbacks stdio_callbacks =
{
"\\/:",
stdio_fopen,
stdio_fread,
stdio_fseek,
stdio_fclose,
stdio_ftell
};
static uint32_t get_le32( const void * _p )
{
const uint8_t * p = (const uint8_t *) _p;
return p[0] + p[1] * 0x100 + p[2] * 0x10000 + p[3] * 0x1000000;
}
static int usf_upload_section(const uint8_t * data, size_t size)
{
uint32_t temp;
if ( size < 4 ) return -1;
temp = get_le32( data ); data += 4; size -= 4;
if(temp == 0x34365253) { //there is a rom section
uint32_t len, start;
if ( size < 4 ) return -1;
len = get_le32( data ); data += 4; size -= 4;
while(len) {
if ( size < 4 ) return -1;
start = get_le32( data ); data += 4; size -= 4;
while ( start + size > ROM_SIZE )
ROM_SIZE *= 2;
memcpy( ROM + start, data, len );
data += len; size -= len;
if ( size < 4 ) return -1;
len = get_le32( data ); data += 4; size -= 4;
}
}
if ( size < 4 ) return -1;
temp = get_le32( data ); data += 4; size -= 4;
if(temp == 0x34365253) {
uint32_t len, start;
if ( size < 4 ) return -1;
len = get_le32( data ); data += 4; size -= 4;
while(len) {
if ( size < 4 ) return -1;
start = get_le32( data ); data += 4; size -= 4;
if ( size < len ) return -1;
memcpy( savestatespace + start, data, len );
data += len; size -= len;
if ( size < 4 ) return -1;
len = get_le32( data ); data += 4; size -= 4;
}
}
return 0;
}
static int usf_loader(void * context, const uint8_t * exe, size_t exe_size,
const uint8_t * reserved, size_t reserved_size)
{
if ( exe && exe_size > 0 ) return -1;
return usf_upload_section( reserved, reserved_size );
}
int main(int argc, char ** argv)
{
if ( argc == 2 )
{
unsigned int i;
FILE * f;
ROM_SIZE = 1024 * 1024;
memset( ROM, 0, sizeof(ROM) );
if ( psf_load( argv[1], &stdio_callbacks, 0x21, usf_loader, 0, 0, 0 ) <= 0 )
return 1;
f = fopen( "out.z64", "wb" );
fwrite( ROM, ROM_SIZE, 1, f );
fclose( f );
f = fopen( "out.pj", "wb" );
fwrite( savestatespace, get_le32(savestatespace + 4) == 0x400000 ? 0x40275c : 0x80275c, 1, f );
fclose( f );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment