source: xtideuniversalbios/trunk/Serial_Server/library/Image.cpp@ 274

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

For function int13/0h, restored the code to only reset the floppy drives if a floppy drive was passed in for reset. Other minor optimizations. Better create new floppy support in Serial Server.

File size: 7.1 KB
RevLine 
[209]1//======================================================================
2//
3// Project: XTIDE Universal BIOS, Serial Port Server
4//
5// File: image.cpp - Abstract base class for disk image support
6//
7
8#include "library.h"
9#include <memory.h>
10#include <stdlib.h>
11#include <string.h>
[219]12#include <stdio.h>
[209]13
[259]14struct floppyInfo floppyInfos[] =
[258]15{
[259]16 { 1, 2949120 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5"
17 { 0, 2867200 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5" (alternate spelling with 2.8)
18 { 0, 2969600 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5" (alternate spelling with 2.9)
19 { 1, 1474560 / 512, 4, 80, 2, 18 }, // 1.44MB 3.5"
20 { 0, 1433600 / 512, 4, 80, 2, 18 }, // 1.44MB 3.5" (alternate spelling with 1.4)
21 { 1, 1228800 / 512, 2, 80, 2, 15 }, // 1.2MB 5.25"
22 { 1, 737280 / 512, 3, 80, 1, 18 }, // 720KB 3.5"
23 { 1, 368640 / 512, 1, 40, 2, 9 }, // 360KB 5.25"
24 { 1, 327680 / 512, 0, 40, 2, 8 }, // 320KB 5.25"
25 { 1, 184320 / 512, 0, 40, 1, 9 }, // 180KB 5.25" single sided
26 { 1, 163840 / 512, 0, 40, 1, 8 }, // 160KB 5.25" single sided
27 { 0, 0, 0, 0, 0, 0 }
[258]28};
29
[259]30struct floppyInfo *FindFloppyInfoBySize( double size )
31{
32 struct floppyInfo *fi;
33
34 for( fi = floppyInfos; fi->size != 0 && !(size+5 > fi->size && size-5 < fi->size); fi++ ) ;
35
36 if( fi->size == 0 )
37 fi = NULL;
38
39 return( fi );
40}
41
[209]42Image::Image( char *name, int p_readOnly, int p_drive )
43{
44}
45
46Image::Image( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_lba )
47{
48}
49
[217]50Image::Image( 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 )
[209]51{
52}
53
[217]54void Image::init( char *name, int p_readOnly, int p_drive, unsigned long p_cyl, unsigned long p_head, unsigned long p_sect, int p_useCHS )
[209]55{
[217]56 double sizef;
[258]57 char sizeChar;
58 struct floppyInfo *f;
[217]59
[209]60 for( char *c = shortFileName = name; *c; c++ )
61 if( *c == '\\' || *c == '/' || *c == ':' )
62 shortFileName = c+1;
63
64 if( *(shortFileName) == 0 )
[211]65 {
66 log( 1, "Can't parse '%s' for short file name\n\n", name );
67 shortFileName = "SerDrive";
68 }
[209]69
70 readOnly = p_readOnly;
71 drive = p_drive;
[217]72
73 if( totallba > 0xfffffff ) // lba28 limit - 28 bits
74 log( -1, "'%s', Image size larger than LBA28 maximum of 137,438,952,960 bytes, %lu", name, totallba );
75
76 if( totallba == 0 )
77 log( -1, "'%s', Image size zero?" );
78
[258]79 floppy = 0;
[259]80 for( f = floppyInfos; f->size && !(f->size == totallba && f->real); f++ ) ;
[258]81 if( f->size )
82 {
83 floppy = 1;
84 floppyType = f->type;
85 p_useCHS = 1;
86 p_cyl = f->cylinders;
87 p_head = f->heads;
88 p_sect = f->sectors;
[259]89 totallba = p_cyl * p_head * p_sect;
[258]90 }
91
[217]92 if( p_useCHS )
93 {
94 if( p_cyl )
95 {
96 if( p_sect > 63 || (p_head > 16 || p_head < 1) || (p_cyl > 1024 || p_cyl < 1) )
97 log( -1, "'%s', parts of the CHS geometry (%lu:%lu:%lu) are out of the range (1-1024:1-16:1-63)", name, p_cyl, p_head, p_sect );
98 else if( totallba != (p_sect * p_head * p_cyl) )
99 log( -1, "'%s', file size does not match geometry", name );
100 sect = p_sect;
101 head = p_head;
102 cyl = p_cyl;
103 }
104 else
105 {
106 if( (totallba % 16) != 0 || ((totallba/16) % 63) != 0 )
107 log( -1, "'%s', file size does not match standard CHS geometry (x:16:63), please specify geometry explicitly with -g", name );
108 else
109 {
110 sect = 63;
111 head = 16;
112 cyl = (totallba / sect / head);
113 if( cyl > 1024 )
114 log( -1, "'%s', CHS geometry of %lu:%lu:%lu is larger than maximum values 1024:16:63", name, cyl, head, sect );
115 }
116 }
117 }
118 else
119 {
120 sect = 0;
121 head = 0;
122 cyl = 0;
123 }
124 useCHS = p_useCHS;
125
126 sizef = totallba/2048.0;
[258]127 sizeChar = 'M';
128 if( sizef < 1 )
129 {
130 sizef *= 1024;
131 sizeChar = 'K';
132 }
[217]133 if( useCHS )
[258]134 log( 0, "%s: %s with CHS geometry %u:%u:%u, size %.2lf %cB",
135 name, (floppy ? "Floppy Disk" : "Hard Disk"), cyl, head, sect, sizef, sizeChar );
[217]136 else
[258]137 log( 0, "%s: %s with total sectors %lu, size %.2lf %cB",
138 name, (floppy ? "Floppy Disk" : "Hard Disk"), totallba, sizef, sizeChar );
[209]139}
140
[217]141int Image::parseGeometry( char *str, unsigned long *p_cyl, unsigned long *p_head, unsigned long *p_sect )
[209]142{
143 char *c, *s, *h;
144 unsigned long cyl, sect, head;
145
146 c = str;
[217]147 for( h = c; *h && *h != ':' && *h != 'x' && *h != 'X'; h++ ) ;
148 if( !*h )
149 return( 0 );
150
151 *h = '\0';
152 h++;
153 for( s = h+1; *s && *s != ':' && *s != 'x' && *s != 'X'; s++ ) ;
[209]154 if( !*s )
155 return( 0 );
156
157 *s = '\0';
158 s++;
159
160 cyl = atol(c);
[217]161 head = atol(h);
[209]162 sect = atol(s);
163
164 if( cyl == 0 || sect == 0 || head == 0 )
165 return( 0 );
166
167 *p_cyl = cyl;
[217]168 *p_head = head;
[209]169 *p_sect = sect;
170
171 return( 1 );
172}
173
174#define ATA_wGenCfg 0
175#define ATA_wCylCnt 1
176#define ATA_wHeadCnt 3
177#define ATA_wBpTrck 4
178#define ATA_wBpSect 5
179#define ATA_wSPT 6
180#define ATA_strSerial 10
181#define ATA_strFirmware 23
182#define ATA_strModel 27
183#define ATA_wCaps 49
184#define ATA_wCurCyls 54
185#define ATA_wCurHeads 55
186#define ATA_wCurSPT 56
187#define ATA_dwCurSCnt 57
188#define ATA_dwLBACnt 60
189
[258]190// Words carved out of the vendor specific area for our use
191//
192#define ATA_wSerialFloppyFlagAndType 158
193#define ATA_wSerialPortAndBaud 159
[209]194
[258]195// Defines used in the words above
196//
[209]197#define ATA_wCaps_LBA 0x200
198
199#define ATA_wGenCfg_FIXED 0x40
200
[258]201#define ATA_wSerialFloppyFlagAndType_Flag 0x10
202#define ATA_wSerialFloppyFlagAndType_TypePosition 5
203
[209]204struct comPorts {
205 unsigned long port;
206 unsigned char com;
207};
208struct comPorts supportedComPorts[] =
209{
210 { 0x3f8, '1' },
211 { 0x2f8, '2' },
212 { 0x3e8, '3' },
213 { 0x2e8, '4' },
214 { 0x2f0, '5' },
215 { 0x3e0, '6' },
216 { 0x2e0, '7' },
217 { 0x260, '8' },
218 { 0x368, '9' },
219 { 0x268, 'A' },
220 { 0x360, 'B' },
221 { 0x270, 'C' },
222 { 0, 0 }
223};
224
[233]225void Image::respondInquire( unsigned short *buff, struct baudRate *baudRate, unsigned short port, unsigned char scan )
[209]226{
[233]227 memset( &buff[0], 0, 514 );
[209]228
[233]229 if( scan )
[209]230 {
[233]231 unsigned short comPort = 0;
232 struct comPorts *cp;
233
234 if( port )
235 {
236 for( cp = supportedComPorts; cp->port && cp->port != port; cp++ ) ;
237 if( cp->port )
238 comPort = cp->com;
239 }
240
241 if( comPort )
242 sprintf( (char *) &buff[ATA_strModel], "%.15s (COM%c/%s)", shortFileName, comPort, baudRate->display );
243 else
244 sprintf( (char *) &buff[ATA_strModel], "%.25s (%s baud)", shortFileName, baudRate->display );
[209]245 }
246 else
[233]247 sprintf( (char *) &buff[ATA_strModel], "%.30s", shortFileName );
[209]248
249 strncpy( (char *) &buff[ATA_strSerial], "serial", 20 );
250 strncpy( (char *) &buff[ATA_strFirmware], "firmw", 8 );
251
252 for( int t = ATA_strModel; t < ATA_strModel+40; t++ )
253 buff[t] = (buff[t] >> 8) | (buff[t] << 8);
254
[217]255 if( useCHS )
256 {
257 buff[ ATA_wCylCnt ] = cyl;
258 buff[ ATA_wHeadCnt ] = head;
259 buff[ ATA_wSPT ] = sect;
260 }
261 else
262 {
263 buff[ ATA_wCaps ] = ATA_wCaps_LBA;
264 buff[ ATA_dwLBACnt ] = (unsigned short) (totallba & 0xffff);
265 buff[ ATA_dwLBACnt+1 ] = (unsigned short) (totallba >> 16);
266 }
267
[258]268 if( floppy )
269 buff[ ATA_wSerialFloppyFlagAndType ] = ATA_wSerialFloppyFlagAndType_Flag | (floppyType << ATA_wSerialFloppyFlagAndType_TypePosition);
270
271 // we always set this, so that the bulk of the BIOS will consider this disk as a hard disk
272 //
[209]273 buff[ ATA_wGenCfg ] = ATA_wGenCfg_FIXED;
274}
Note: See TracBrowser for help on using the repository browser.