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 | //
|
---|
9 | // XTIDE Universal BIOS and Associated Tools
|
---|
10 | // Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
|
---|
11 | //
|
---|
12 | // This program is free software; you can redistribute it and/or modify
|
---|
13 | // it under the terms of the GNU General Public License as published by
|
---|
14 | // the Free Software Foundation; either version 2 of the License, or
|
---|
15 | // (at your option) any later version.
|
---|
16 | //
|
---|
17 | // This program is distributed in the hope that it will be useful,
|
---|
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | // GNU General Public License for more details.
|
---|
21 | // Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
22 | //
|
---|
23 |
|
---|
24 | #include "library.h"
|
---|
25 | #include <memory.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <stdio.h>
|
---|
29 |
|
---|
30 | struct floppyInfo floppyInfos[] =
|
---|
31 | {
|
---|
32 | { 1, 2949120 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5"
|
---|
33 | { 0, 2867200 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5" (alternate spelling with 2.8)
|
---|
34 | { 0, 2969600 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5" (alternate spelling with 2.9)
|
---|
35 | { 1, 1474560 / 512, 4, 80, 2, 18 }, // 1.44MB 3.5"
|
---|
36 | { 0, 1433600 / 512, 4, 80, 2, 18 }, // 1.44MB 3.5" (alternate spelling with 1.4)
|
---|
37 | { 1, 1228800 / 512, 2, 80, 2, 15 }, // 1.2MB 5.25"
|
---|
38 | { 1, 737280 / 512, 3, 80, 1, 18 }, // 720KB 3.5"
|
---|
39 | { 1, 368640 / 512, 1, 40, 2, 9 }, // 360KB 5.25"
|
---|
40 | { 1, 327680 / 512, 0, 40, 2, 8 }, // 320KB 5.25"
|
---|
41 | { 1, 184320 / 512, 0, 40, 1, 9 }, // 180KB 5.25" single sided
|
---|
42 | { 1, 163840 / 512, 0, 40, 1, 8 }, // 160KB 5.25" single sided
|
---|
43 | { 0, 0, 0, 0, 0, 0 }
|
---|
44 | };
|
---|
45 |
|
---|
46 | struct floppyInfo *FindFloppyInfoBySize( double size )
|
---|
47 | {
|
---|
48 | struct floppyInfo *fi;
|
---|
49 |
|
---|
50 | for( fi = floppyInfos; fi->size != 0 && !(size+5 > fi->size && size-5 < fi->size); fi++ ) ;
|
---|
51 |
|
---|
52 | if( fi->size == 0 )
|
---|
53 | fi = NULL;
|
---|
54 |
|
---|
55 | return( fi );
|
---|
56 | }
|
---|
57 |
|
---|
58 | void flipEndian( unsigned short *buff, unsigned int len )
|
---|
59 | {
|
---|
60 | for( unsigned int t = 0; t < len/2; t++ )
|
---|
61 | buff[t] = (buff[t] & 0xff) << 8 | (buff[t] & 0xff00) >> 8;
|
---|
62 | }
|
---|
63 |
|
---|
64 | Image::Image( char *name, int p_readOnly, int p_drive )
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | Image::Image( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_lba )
|
---|
69 | {
|
---|
70 | }
|
---|
71 |
|
---|
72 | Image::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 )
|
---|
73 | {
|
---|
74 | }
|
---|
75 |
|
---|
76 | void 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 )
|
---|
77 | {
|
---|
78 | double sizef;
|
---|
79 | char sizeChar;
|
---|
80 | struct floppyInfo *f;
|
---|
81 |
|
---|
82 | for( char *c = shortFileName = name; *c; c++ )
|
---|
83 | if( *c == '\\' || *c == '/' || *c == ':' )
|
---|
84 | shortFileName = c+1;
|
---|
85 |
|
---|
86 | if( *(shortFileName) == 0 )
|
---|
87 | {
|
---|
88 | log( 1, "Can't parse '%s' for short file name\n\n", name );
|
---|
89 | shortFileName = "SerDrive";
|
---|
90 | }
|
---|
91 |
|
---|
92 | readOnly = p_readOnly;
|
---|
93 | drive = p_drive;
|
---|
94 |
|
---|
95 | if( totallba > 0xfffffff ) // lba28 limit - 28 bits
|
---|
96 | log( -1, "'%s', Image size larger than LBA28 maximum of 137,438,952,960 bytes, %lu", name, totallba );
|
---|
97 |
|
---|
98 | if( totallba == 0 )
|
---|
99 | log( -1, "'%s', Image size zero?" );
|
---|
100 |
|
---|
101 | floppy = 0;
|
---|
102 | for( f = floppyInfos; f->size && !(f->size == totallba && f->real); f++ ) ;
|
---|
103 | if( f->size )
|
---|
104 | {
|
---|
105 | floppy = 1;
|
---|
106 | floppyType = f->type;
|
---|
107 | p_useCHS = 1;
|
---|
108 | p_cyl = f->cylinders;
|
---|
109 | p_head = f->heads;
|
---|
110 | p_sect = f->sectors;
|
---|
111 | totallba = p_cyl * p_head * p_sect;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if( p_useCHS )
|
---|
115 | {
|
---|
116 | if( p_cyl )
|
---|
117 | {
|
---|
118 | if( p_sect > 63 || (p_head > 16 || p_head < 1) || (p_cyl > 1024 || p_cyl < 1) )
|
---|
119 | 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 );
|
---|
120 | else if( totallba != (p_sect * p_head * p_cyl) )
|
---|
121 | log( -1, "'%s', file size does not match geometry", name );
|
---|
122 | sect = p_sect;
|
---|
123 | head = p_head;
|
---|
124 | cyl = p_cyl;
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | if( (totallba % 16) != 0 || ((totallba/16) % 63) != 0 )
|
---|
129 | log( -1, "'%s', file size does not match standard CHS geometry (x:16:63), please specify geometry explicitly with -g", name );
|
---|
130 | else
|
---|
131 | {
|
---|
132 | sect = 63;
|
---|
133 | head = 16;
|
---|
134 | cyl = (totallba / sect / head);
|
---|
135 | if( cyl > 1024 )
|
---|
136 | log( -1, "'%s', CHS geometry of %lu:%lu:%lu is larger than maximum values 1024:16:63", name, cyl, head, sect );
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | sect = 0;
|
---|
143 | head = 0;
|
---|
144 | cyl = 0;
|
---|
145 | }
|
---|
146 | useCHS = p_useCHS;
|
---|
147 |
|
---|
148 | sizef = totallba/2048.0;
|
---|
149 | sizeChar = 'M';
|
---|
150 | if( sizef < 1 )
|
---|
151 | {
|
---|
152 | sizef *= 1024;
|
---|
153 | sizeChar = 'K';
|
---|
154 | }
|
---|
155 | if( useCHS )
|
---|
156 | log( 0, "%s: %s with CHS geometry %u:%u:%u, size %.2lf %cB",
|
---|
157 | name, (floppy ? "Floppy Disk" : "Hard Disk"), cyl, head, sect, sizef, sizeChar );
|
---|
158 | else
|
---|
159 | log( 0, "%s: %s with total sectors %lu, size %.2lf %cB",
|
---|
160 | name, (floppy ? "Floppy Disk" : "Hard Disk"), totallba, sizef, sizeChar );
|
---|
161 | }
|
---|
162 |
|
---|
163 | int Image::parseGeometry( char *str, unsigned long *p_cyl, unsigned long *p_head, unsigned long *p_sect )
|
---|
164 | {
|
---|
165 | char *c, *s, *h;
|
---|
166 | unsigned long cyl, sect, head;
|
---|
167 |
|
---|
168 | c = str;
|
---|
169 | for( h = c; *h && *h != ':' && *h != 'x' && *h != 'X'; h++ ) ;
|
---|
170 | if( !*h )
|
---|
171 | return( 0 );
|
---|
172 |
|
---|
173 | *h = '\0';
|
---|
174 | h++;
|
---|
175 | for( s = h+1; *s && *s != ':' && *s != 'x' && *s != 'X'; s++ ) ;
|
---|
176 | if( !*s )
|
---|
177 | return( 0 );
|
---|
178 |
|
---|
179 | *s = '\0';
|
---|
180 | s++;
|
---|
181 |
|
---|
182 | cyl = atol(c);
|
---|
183 | head = atol(h);
|
---|
184 | sect = atol(s);
|
---|
185 |
|
---|
186 | if( cyl == 0 || sect == 0 || head == 0 )
|
---|
187 | return( 0 );
|
---|
188 |
|
---|
189 | *p_cyl = cyl;
|
---|
190 | *p_head = head;
|
---|
191 | *p_sect = sect;
|
---|
192 |
|
---|
193 | return( 1 );
|
---|
194 | }
|
---|
195 |
|
---|
196 | #define ATA_wGenCfg 0
|
---|
197 | #define ATA_wCylCnt 1
|
---|
198 | #define ATA_wHeadCnt 3
|
---|
199 | #define ATA_wBpTrck 4
|
---|
200 | #define ATA_wBpSect 5
|
---|
201 | #define ATA_wSPT 6
|
---|
202 |
|
---|
203 | #define ATA_strSerial 10
|
---|
204 | #define ATA_strSerial_Length 20
|
---|
205 |
|
---|
206 | #define ATA_strFirmware 23
|
---|
207 | #define ATA_strFirmware_Length 8
|
---|
208 |
|
---|
209 | #define ATA_strModel 27
|
---|
210 | #define ATA_strModel_Length 40 // Maximum allowable length of the string according to the ATA spec
|
---|
211 | #define XTIDEBIOS_strModel_Length 30 // Maximum length copied out of the ATA information by the BIOS
|
---|
212 |
|
---|
213 | #define ATA_wCaps 49
|
---|
214 | #define ATA_wCurCyls 54
|
---|
215 | #define ATA_wCurHeads 55
|
---|
216 | #define ATA_wCurSPT 56
|
---|
217 | #define ATA_dwCurSCnt 57
|
---|
218 | #define ATA_dwLBACnt 60
|
---|
219 |
|
---|
220 | // Words carved out of the vendor specific area for our use
|
---|
221 | //
|
---|
222 | #define ATA_wSerialServerVersion 157
|
---|
223 | #define ATA_wSerialDriveFlags 158
|
---|
224 | #define ATA_wSerialPortAndBaud 159
|
---|
225 |
|
---|
226 | // Defines used in the words above
|
---|
227 | //
|
---|
228 | #define ATA_wCaps_LBA 0x200
|
---|
229 |
|
---|
230 | #define ATA_wGenCfg_FIXED 0x40
|
---|
231 |
|
---|
232 | // These are all shifted by 1 bit to the right, so that SerialDPT_Finalize can shift them into proper position
|
---|
233 | // and shift the high order bit into the carry flag to indicate a floppy drive is present.
|
---|
234 | //
|
---|
235 | #define ATA_wSerialDriveFlags_Floppy 0x88
|
---|
236 | #define ATA_wSerialDriveFlags_Present 0x02
|
---|
237 | #define ATA_wSerialDriveFlags_FloppyType_FieldPosition 4
|
---|
238 |
|
---|
239 | struct comPorts {
|
---|
240 | unsigned long port;
|
---|
241 | unsigned char com;
|
---|
242 | };
|
---|
243 | struct comPorts supportedComPorts[] =
|
---|
244 | {
|
---|
245 | { 0x3f8, '1' },
|
---|
246 | { 0x2f8, '2' },
|
---|
247 | { 0x3e8, '3' },
|
---|
248 | { 0x2e8, '4' },
|
---|
249 | { 0x2f0, '5' },
|
---|
250 | { 0x3e0, '6' },
|
---|
251 | { 0x2e0, '7' },
|
---|
252 | { 0x260, '8' },
|
---|
253 | { 0x368, '9' },
|
---|
254 | { 0x268, 'A' },
|
---|
255 | { 0x360, 'B' },
|
---|
256 | { 0x270, 'C' },
|
---|
257 | { 0, 0 }
|
---|
258 | };
|
---|
259 |
|
---|
260 | void Image::respondInquire( unsigned short *buff, unsigned short originalPortAndBaud, struct baudRate *baudRate, unsigned short port, unsigned char scan )
|
---|
261 | {
|
---|
262 | char formatBuff[ 128 ];
|
---|
263 | char speedBuff[ 128 ];
|
---|
264 |
|
---|
265 | memset( &buff[0], 0, 514 );
|
---|
266 |
|
---|
267 | if( scan )
|
---|
268 | {
|
---|
269 | unsigned short comPort = 0;
|
---|
270 | struct comPorts *cp;
|
---|
271 |
|
---|
272 | if( port )
|
---|
273 | {
|
---|
274 | for( cp = supportedComPorts; cp->port && cp->port != port; cp++ ) ;
|
---|
275 | if( cp->port )
|
---|
276 | comPort = cp->com;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if( comPort )
|
---|
280 | sprintf( speedBuff, " (COM%c/%s)", comPort, baudRate->display );
|
---|
281 | else
|
---|
282 | sprintf( speedBuff, " (%s baud)", shortFileName, baudRate->display );
|
---|
283 |
|
---|
284 | sprintf( formatBuff, "%.*s%s ", XTIDEBIOS_strModel_Length - strlen(speedBuff), shortFileName, speedBuff );
|
---|
285 | }
|
---|
286 | else
|
---|
287 | sprintf( formatBuff, "%.*s ", XTIDEBIOS_strModel_Length, shortFileName );
|
---|
288 | strncpy( (char *) &buff[ATA_strModel], formatBuff, ATA_strModel_Length );
|
---|
289 | flipEndian( &buff[ATA_strModel], ATA_strModel_Length );
|
---|
290 |
|
---|
291 | strncpy( (char *) &buff[ATA_strSerial], "SerialDrive ", ATA_strSerial_Length );
|
---|
292 | flipEndian( &buff[ATA_strSerial], ATA_strSerial_Length );
|
---|
293 |
|
---|
294 | sprintf( formatBuff, "%d.%d ", SERIAL_SERVER_MAJORVERSION, SERIAL_SERVER_MINORVERSION );
|
---|
295 | strncpy( (char *) &buff[ATA_strFirmware], formatBuff, ATA_strFirmware_Length );
|
---|
296 | flipEndian( &buff[ATA_strFirmware], ATA_strFirmware_Length );
|
---|
297 |
|
---|
298 | if( useCHS )
|
---|
299 | {
|
---|
300 | buff[ ATA_wCylCnt ] = cyl;
|
---|
301 | buff[ ATA_wHeadCnt ] = head;
|
---|
302 | buff[ ATA_wSPT ] = sect;
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | buff[ ATA_wCaps ] = ATA_wCaps_LBA;
|
---|
307 | buff[ ATA_dwLBACnt ] = (unsigned short) (totallba & 0xffff);
|
---|
308 | buff[ ATA_dwLBACnt+1 ] = (unsigned short) (totallba >> 16);
|
---|
309 | }
|
---|
310 |
|
---|
311 | // We echo back the port and baud that we were called on from the client,
|
---|
312 | // the client then uses this value to finalize the DPT.
|
---|
313 | //
|
---|
314 | buff[ ATA_wSerialPortAndBaud ] = originalPortAndBaud;
|
---|
315 |
|
---|
316 | // In case the client requires a specific server version...
|
---|
317 | //
|
---|
318 | buff[ ATA_wSerialServerVersion ] = (SERIAL_SERVER_MAJORVERSION << 8) | SERIAL_SERVER_MINORVERSION;
|
---|
319 |
|
---|
320 | buff[ ATA_wSerialDriveFlags ] = ATA_wSerialDriveFlags_Present;
|
---|
321 | if( floppy )
|
---|
322 | buff[ ATA_wSerialDriveFlags ] |=
|
---|
323 | ATA_wSerialDriveFlags_Floppy | (floppyType << ATA_wSerialDriveFlags_FloppyType_FieldPosition);
|
---|
324 |
|
---|
325 | // we always set this, so that the bulk of the BIOS will consider this disk as a hard disk
|
---|
326 | //
|
---|
327 | buff[ ATA_wGenCfg ] = ATA_wGenCfg_FIXED;
|
---|
328 | }
|
---|
329 |
|
---|