[209] | 1 | //======================================================================
|
---|
| 2 | //
|
---|
| 3 | // Project: XTIDE Universal BIOS, Serial Port Server
|
---|
| 4 | //
|
---|
| 5 | // File: FlatImage.cpp - Basic flat disk image file support
|
---|
| 6 | //
|
---|
| 7 |
|
---|
| 8 | #include <stdio.h>
|
---|
| 9 | #include <stdlib.h>
|
---|
| 10 | #include <memory.h>
|
---|
| 11 |
|
---|
| 12 | #include "FlatImage.h"
|
---|
| 13 |
|
---|
[217] | 14 | FlatImage::FlatImage( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_cyl, unsigned long p_head, unsigned long p_sect, int p_useCHS ) : Image( name, p_readOnly, p_drive, p_create, p_cyl, p_head, p_sect, p_useCHS )
|
---|
[209] | 15 | {
|
---|
[217] | 16 | long filesize;
|
---|
[209] | 17 |
|
---|
| 18 | if( p_create )
|
---|
| 19 | {
|
---|
| 20 | char buff[512];
|
---|
| 21 | unsigned long size;
|
---|
| 22 | unsigned long size2;
|
---|
| 23 | double sizef;
|
---|
| 24 |
|
---|
| 25 | fp = fopen( name, "r" );
|
---|
| 26 | if( fp )
|
---|
[211] | 27 | log( -1, "Create Failure: '%s' already exists", name );
|
---|
[209] | 28 |
|
---|
| 29 | if( !(fp = fopen( name, "w" )) )
|
---|
[211] | 30 | log( -1, "Could not create file '%s'", name );
|
---|
[209] | 31 |
|
---|
| 32 | memset( &buff[0], 0, 512 );
|
---|
| 33 | size2 = size = (unsigned long) p_cyl * (unsigned long) p_sect * (unsigned long) p_head;
|
---|
| 34 | while( size-- )
|
---|
| 35 | {
|
---|
| 36 | if( fwrite( &buff[0], 1, 512, fp ) != 512 )
|
---|
[211] | 37 | log( -1, "Create write black sector error" );
|
---|
[209] | 38 | }
|
---|
| 39 | fclose( fp );
|
---|
| 40 |
|
---|
| 41 | sizef = size2/2048.0;
|
---|
[217] | 42 | if( p_cyl > 1024 )
|
---|
| 43 | log( 0, "Created file '%s', size %.1lf MB", name, sizef );
|
---|
| 44 | else
|
---|
| 45 | log( 0, "Created file '%s', geometry %u:%u:%u, size %.1lf MB", name, p_cyl, p_sect, p_head, sizef );
|
---|
[209] | 46 | }
|
---|
| 47 |
|
---|
| 48 | fp = fopen( name, "r+" );
|
---|
| 49 | if( !fp )
|
---|
[211] | 50 | log( -1, "Could not Open '%s'", name );
|
---|
[209] | 51 |
|
---|
| 52 | fseek( fp, 0, SEEK_END );
|
---|
[217] | 53 | filesize = ftell( fp );
|
---|
[209] | 54 |
|
---|
[217] | 55 | if( filesize == 0 || filesize == -1L )
|
---|
| 56 | log( -1, "Could not get file size for '%s', file possibly larger than 2 GB", name );
|
---|
[209] | 57 |
|
---|
[217] | 58 | if( filesize & 0x1ff )
|
---|
[211] | 59 | log( -1, "'%s' not made up of 512 byte sectors", name );
|
---|
[209] | 60 |
|
---|
[217] | 61 | totallba = filesize >> 9; // 512 bytes per sector
|
---|
[209] | 62 |
|
---|
[217] | 63 | init( name, p_readOnly, p_drive, p_cyl, p_head, p_sect, p_useCHS );
|
---|
[209] | 64 | }
|
---|
| 65 |
|
---|
| 66 | int FlatImage::seekSector( unsigned long cyl, unsigned long sect, unsigned long head )
|
---|
| 67 | {
|
---|
| 68 | return( 0 );
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | int FlatImage::seekSector( unsigned long lba )
|
---|
| 72 | {
|
---|
| 73 | return( fseek( fp, lba * 512, SEEK_SET ) );
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | int FlatImage::writeSector( void *buff )
|
---|
| 77 | {
|
---|
| 78 | int r;
|
---|
| 79 |
|
---|
| 80 | r = fwrite( buff, 1, 512, fp );
|
---|
| 81 | fflush( fp );
|
---|
| 82 |
|
---|
| 83 | return( r == 512 ? 0 : 1 );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | int FlatImage::readSector( void *buff )
|
---|
| 87 | {
|
---|
| 88 | return( fread( buff, 1, 512, fp ) == 512 ? 0 : 1 );
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | FlatImage::~FlatImage()
|
---|
| 92 | {
|
---|
| 93 | if( fp )
|
---|
| 94 | fclose( fp );
|
---|
| 95 | fp = NULL;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|