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

Last change on this file since 602 was 602, checked in by krille_n_, 5 years ago

Changes:

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