Search
lxdream.org :: lxdream/src/loader.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/loader.c
changeset 1:eea311cfd33e
next2:42349f6ea216
author nkeynes
date Sat Mar 13 00:03:32 2004 +0000 (16 years ago)
permissions -rw-r--r--
last change This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches.
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/loader.c Sat Mar 13 00:03:32 2004 +0000
1.3 @@ -0,0 +1,85 @@
1.4 +/*
1.5 + *
1.6 + */
1.7 +
1.8 +#include <stdio.h>
1.9 +#include <fcntl.h>
1.10 +#include <sys/stat.h>
1.11 +#include <sys/mman.h>
1.12 +#include <errno.h>
1.13 +#include <stdint.h>
1.14 +#include "gui.h"
1.15 +#include "ipbin.h"
1.16 +#include "sh4core.h"
1.17 +#include "mem.h"
1.18 +
1.19 +char ip_bin_magic[32] = "SEGA SEGAKATANA SEGA ENTERPRISES";
1.20 +char iso_magic[6] = "\001CD001";
1.21 +
1.22 +#define CDI_V2 0x80000004
1.23 +#define CDI_V3 0x80000005
1.24 +
1.25 +void open_file( char *filename )
1.26 +{
1.27 + char buf[32];
1.28 + uint32_t tmpa[2];
1.29 + struct stat st;
1.30 +
1.31 + int fd = open( filename, O_RDONLY );
1.32 + if( fd == -1 ) {
1.33 + ERROR( "Unable to open file: '%s' (%s)", filename,
1.34 + strerror(errno) );
1.35 + return;
1.36 + }
1.37 +
1.38 + fstat( fd, &st );
1.39 + if( st.st_size < 32768 ) {
1.40 + ERROR( "File '%s' too small to be a dreamcast image", filename );
1.41 + close(fd);
1.42 + return;
1.43 + }
1.44 +
1.45 + /* begin magic */
1.46 + if( read( fd, buf, 32 ) != 32 ) {
1.47 + ERROR( "Unable to read from file '%s'", filename );
1.48 + close(fd);
1.49 + return;
1.50 + }
1.51 + if( memcmp( buf, ip_bin_magic, 32 ) == 0 ) {
1.52 + /* we have a DC bootstrap */
1.53 + if( st.st_size == BOOTSTRAP_SIZE ) {
1.54 + char *load = mem_get_region( BOOTSTRAP_LOAD_ADDR );
1.55 + /* Just the bootstrap, no image... */
1.56 + WARN( "File '%s' contains bootstrap only, loading anyway",
1.57 + filename );
1.58 + lseek( fd, 0, SEEK_SET );
1.59 + read( fd, load, BOOTSTRAP_SIZE );
1.60 + parse_ipbin( load );
1.61 + sh4_reset();
1.62 + sh4_set_pc( BOOTSTRAP_LOAD_ADDR + 0x300 );
1.63 + set_disassembly_region( BOOTSTRAP_LOAD_ADDR );
1.64 + set_disassembly_pc( sh4r.pc, TRUE );
1.65 + update_registers();
1.66 + } else {
1.67 + /* look for a valid ISO9660 header */
1.68 + lseek( fd, 32768, SEEK_SET );
1.69 + read( fd, buf, 8 );
1.70 + if( memcmp( buf, iso_magic, 6 ) == 0 ) {
1.71 + /* Alright, got it */
1.72 + INFO( "Loading ISO9660 filesystem from '%s'",
1.73 + filename );
1.74 + }
1.75 + }
1.76 + } else {
1.77 + /* check if it's a CDI file: */
1.78 + lseek( fd, -8, SEEK_END );
1.79 + read( fd, &tmpa, 8 );
1.80 + if( tmpa[0] == CDI_V2 || tmpa[0] == CDI_V3 ) {
1.81 + /* Yup, it is */
1.82 + INFO( "Loading CDI file '%s'", filename );
1.83 + } else {
1.84 + ERROR( "Don't know what to do with '%s'", filename );
1.85 + }
1.86 + }
1.87 + close(fd);
1.88 +}
.