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 |
|
---|
14 | FlatImage::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 )
|
---|
27 | log( -1, "Create Failure: '%s' already exists", name );
|
---|
28 |
|
---|
29 | if( !(fp = fopen( name, "w" )) )
|
---|
30 | log( -1, "Could not create file '%s'", name );
|
---|
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 )
|
---|
37 | log( -1, "Create write black sector error" );
|
---|
38 | }
|
---|
39 | fclose( fp );
|
---|
40 |
|
---|
41 | sizef = size2/2048.0;
|
---|
42 | log( 0, "Created file '%s' with geometry %u:%u:%u, size %.1lf megabytes\n", name, p_cyl, p_sect, p_head, sizef );
|
---|
43 | }
|
---|
44 |
|
---|
45 | fp = fopen( name, "r+" );
|
---|
46 | if( !fp )
|
---|
47 | log( -1, "Could not Open '%s'", name );
|
---|
48 |
|
---|
49 | fseek( fp, 0, SEEK_END );
|
---|
50 | totallba = ftell( fp );
|
---|
51 |
|
---|
52 | if( !totallba )
|
---|
53 | log( -1, "Could not get file size for '%s'", name );
|
---|
54 |
|
---|
55 | if( totallba & 0x1ff )
|
---|
56 | log( -1, "'%s' not made up of 512 byte sectors", name );
|
---|
57 |
|
---|
58 | totallba >>= 9;
|
---|
59 | if( totallba != (p_sect * p_head * p_cyl) )
|
---|
60 | {
|
---|
61 | if( p_sect || p_head || p_cyl )
|
---|
62 | log( -1, "'%s', file size does not match geometry", name );
|
---|
63 | else if( (totallba % 16) != 0 || ((totallba/16) % 63) != 0 )
|
---|
64 | log( -1, "'%s', file size does not match standard geometry (x:16:63), please geometry explicitly with -g", name );
|
---|
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;
|
---|
80 | log( 0, "Opening disk image '%s', geometry %u:%u:%u, total size %.1lf MB", name, cyl, sect, head, sizef );
|
---|
81 | }
|
---|
82 |
|
---|
83 | int FlatImage::seekSector( unsigned long cyl, unsigned long sect, unsigned long head )
|
---|
84 | {
|
---|
85 | return( 0 );
|
---|
86 | }
|
---|
87 |
|
---|
88 | int FlatImage::seekSector( unsigned long lba )
|
---|
89 | {
|
---|
90 | return( fseek( fp, lba * 512, SEEK_SET ) );
|
---|
91 | }
|
---|
92 |
|
---|
93 | int 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 |
|
---|
103 | int FlatImage::readSector( void *buff )
|
---|
104 | {
|
---|
105 | return( fread( buff, 1, 512, fp ) == 512 ? 0 : 1 );
|
---|
106 | }
|
---|
107 |
|
---|
108 | FlatImage::~FlatImage()
|
---|
109 | {
|
---|
110 | if( fp )
|
---|
111 | fclose( fp );
|
---|
112 | fp = NULL;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|