source: xtideuniversalbios/trunk/Serial_Server/library/FlatImage.cpp@ 214

Last change on this file since 214 was 213, checked in by gregli@…, 13 years ago

Minor improvements to serial server.

File size: 2.7 KB
RevLine 
[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
14FlatImage::FlatImage( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_cyl, unsigned long p_sect, unsigned long p_head ) : Image( name, p_readOnly, p_drive, p_create, p_cyl, p_sect, p_head )
15{
16 double sizef;
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;
[211]42 log( 0, "Created file '%s' with geometry %u:%u:%u, size %.1lf megabytes\n", name, p_cyl, p_sect, p_head, sizef );
[209]43 }
44
45 fp = fopen( name, "r+" );
46 if( !fp )
[211]47 log( -1, "Could not Open '%s'", name );
[209]48
49 fseek( fp, 0, SEEK_END );
50 totallba = ftell( fp );
51
52 if( !totallba )
[211]53 log( -1, "Could not get file size for '%s'", name );
[209]54
55 if( totallba & 0x1ff )
[211]56 log( -1, "'%s' not made up of 512 byte sectors", name );
[209]57
58 totallba >>= 9;
59 if( totallba != (p_sect * p_head * p_cyl) )
60 {
61 if( p_sect || p_head || p_cyl )
[211]62 log( -1, "'%s', file size does not match geometry", name );
[209]63 else if( (totallba % 16) != 0 || ((totallba/16) % 63) != 0 )
[211]64 log( -1, "'%s', file size does not match standard geometry (x:16:63), please geometry explicitly with -g", name );
[209]65 else
66 {
67 sect = 63;
68 head = 16;
69 cyl = (totallba / sect / head);
70 }
71 }
72 else
73 {
74 sect = p_sect;
75 head = p_head;
76 cyl = p_cyl;
77 }
78
79 sizef = totallba/2048.0;
[213]80 log( 0, "Opening disk '%s', geometry %u:%u:%u, total size %.1lf MB", name, cyl, sect, head, sizef );
[209]81}
82
83int FlatImage::seekSector( unsigned long cyl, unsigned long sect, unsigned long head )
84{
85 return( 0 );
86}
87
88int FlatImage::seekSector( unsigned long lba )
89{
90 return( fseek( fp, lba * 512, SEEK_SET ) );
91}
92
93int FlatImage::writeSector( void *buff )
94{
95 int r;
96
97 r = fwrite( buff, 1, 512, fp );
98 fflush( fp );
99
100 return( r == 512 ? 0 : 1 );
101}
102
103int FlatImage::readSector( void *buff )
104{
105 return( fread( buff, 1, 512, fp ) == 512 ? 0 : 1 );
106}
107
108FlatImage::~FlatImage()
109{
110 if( fp )
111 fclose( fp );
112 fp = NULL;
113}
114
115
Note: See TracBrowser for help on using the repository browser.