[209] | 1 | //====================================================================== |
---|
| 2 | // |
---|
| 3 | // Project: XTIDE Universal BIOS, Serial Port Server |
---|
| 4 | // |
---|
[589] | 5 | // File: Image.cpp - Abstract base class for disk image support |
---|
[209] | 6 | // |
---|
| 7 | |
---|
[376] | 8 | // |
---|
[526] | 9 | // XTIDE Universal BIOS and Associated Tools |
---|
| 10 | // Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team. |
---|
[376] | 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. |
---|
[526] | 16 | // |
---|
[376] | 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 |
---|
[526] | 20 | // GNU General Public License for more details. |
---|
[376] | 21 | // Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 22 | // |
---|
| 23 | |
---|
[589] | 24 | #include "Library.h" |
---|
[209] | 25 | #include <memory.h> |
---|
| 26 | #include <stdlib.h> |
---|
| 27 | #include <string.h> |
---|
[219] | 28 | #include <stdio.h> |
---|
[209] | 29 | |
---|
[526] | 30 | struct floppyInfo floppyInfos[] = |
---|
[258] | 31 | { |
---|
[602] | 32 | // { 0, 2969600 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5" (alternate spelling with 2.9) |
---|
[259] | 33 | { 1, 2949120 / 512, 6, 80, 2, 36 }, // 2.88MB 3.5" |
---|
[602] | 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) |
---|
[259] | 36 | { 1, 1474560 / 512, 4, 80, 2, 18 }, // 1.44MB 3.5" |
---|
[602] | 37 | // { 0, 1433600 / 512, 4, 80, 2, 18 }, // 1.44MB 3.5" (alternate spelling with 1.4) |
---|
[259] | 38 | { 1, 1228800 / 512, 2, 80, 2, 15 }, // 1.2MB 5.25" |
---|
[602] | 39 | { 1, 737280 / 512, 3, 80, 2, 9 }, // 720KB 3.5" |
---|
[259] | 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 } |
---|
[258] | 45 | }; |
---|
| 46 | |
---|
[259] | 47 | struct 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 | |
---|
[277] | 59 | void 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 | |
---|
[209] | 65 | Image::Image( char *name, int p_readOnly, int p_drive ) |
---|
| 66 | { |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | Image::Image( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_lba ) |
---|
| 70 | { |
---|
| 71 | } |
---|
| 72 | |
---|
[217] | 73 | 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 ) |
---|
[209] | 74 | { |
---|
| 75 | } |
---|
| 76 | |
---|
[217] | 77 | 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 ) |
---|
[209] | 78 | { |
---|
[217] | 79 | double sizef; |
---|
[258] | 80 | char sizeChar; |
---|
| 81 | struct floppyInfo *f; |
---|
[217] | 82 | |
---|
[209] | 83 | for( char *c = shortFileName = name; *c; c++ ) |
---|
| 84 | if( *c == '\\' || *c == '/' || *c == ':' ) |
---|
| 85 | shortFileName = c+1; |
---|
| 86 | |
---|
| 87 | if( *(shortFileName) == 0 ) |
---|
[211] | 88 | { |
---|
| 89 | log( 1, "Can't parse '%s' for short file name\n\n", name ); |
---|
| 90 | shortFileName = "SerDrive"; |
---|
| 91 | } |
---|
[526] | 92 | |
---|
[209] | 93 | readOnly = p_readOnly; |
---|
| 94 | drive = p_drive; |
---|
[217] | 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 | |
---|
[258] | 102 | floppy = 0; |
---|
[259] | 103 | for( f = floppyInfos; f->size && !(f->size == totallba && f->real); f++ ) ; |
---|
[258] | 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; |
---|
[259] | 112 | totallba = p_cyl * p_head * p_sect; |
---|
[258] | 113 | } |
---|
| 114 | |
---|
[430] | 115 | if( p_cyl ) |
---|
[217] | 116 | { |
---|
[430] | 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 ) |
---|
[217] | 128 | { |
---|
[430] | 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; |
---|
[217] | 133 | } |
---|
[592] | 134 | else if( (totallba & 15) != 0 || ((totallba/16) % 63) != 0 ) |
---|
[217] | 135 | { |
---|
[430] | 136 | log( -1, "'%s', file size does not match standard CHS geometry (x:16:63), please specify geometry explicitly with -g", name ); |
---|
| 137 | } |
---|
[526] | 138 | else |
---|
[430] | 139 | { |
---|
| 140 | sect = 63; |
---|
| 141 | head = 16; |
---|
| 142 | cyl = (totallba / sect / head); |
---|
| 143 | if( cyl > 65536 ) |
---|
[217] | 144 | { |
---|
[430] | 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 ); |
---|
[217] | 146 | } |
---|
| 147 | } |
---|
| 148 | } |
---|
[430] | 149 | |
---|
[217] | 150 | useCHS = p_useCHS; |
---|
| 151 | |
---|
| 152 | sizef = totallba/2048.0; |
---|
[258] | 153 | sizeChar = 'M'; |
---|
[526] | 154 | if( sizef < 1 ) |
---|
[258] | 155 | { |
---|
| 156 | sizef *= 1024; |
---|
| 157 | sizeChar = 'K'; |
---|
| 158 | } |
---|
[217] | 159 | if( useCHS ) |
---|
[258] | 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 ); |
---|
[217] | 162 | else |
---|
[526] | 163 | log( 0, "%s: %s with %lu LBA sectors, size %.2lf %cB (CHS geometry %u:%u:%u)", |
---|
[430] | 164 | name, (floppy ? "Floppy Disk" : "Hard Disk"), totallba, sizef, sizeChar, cyl, head, sect ); |
---|
[209] | 165 | } |
---|
| 166 | |
---|
[217] | 167 | int Image::parseGeometry( char *str, unsigned long *p_cyl, unsigned long *p_head, unsigned long *p_sect ) |
---|
[209] | 168 | { |
---|
| 169 | char *c, *s, *h; |
---|
| 170 | unsigned long cyl, sect, head; |
---|
| 171 | |
---|
| 172 | c = str; |
---|
[217] | 173 | for( h = c; *h && *h != ':' && *h != 'x' && *h != 'X'; h++ ) ; |
---|
| 174 | if( !*h ) |
---|
| 175 | return( 0 ); |
---|
| 176 | |
---|
| 177 | *h = '\0'; |
---|
| 178 | h++; |
---|
[526] | 179 | for( s = h+1; *s && *s != ':' && *s != 'x' && *s != 'X'; s++ ) ; |
---|
[209] | 180 | if( !*s ) |
---|
| 181 | return( 0 ); |
---|
| 182 | |
---|
| 183 | *s = '\0'; |
---|
| 184 | s++; |
---|
| 185 | |
---|
| 186 | cyl = atol(c); |
---|
[217] | 187 | head = atol(h); |
---|
[209] | 188 | sect = atol(s); |
---|
| 189 | |
---|
| 190 | if( cyl == 0 || sect == 0 || head == 0 ) |
---|
| 191 | return( 0 ); |
---|
| 192 | |
---|
| 193 | *p_cyl = cyl; |
---|
[217] | 194 | *p_head = head; |
---|
[209] | 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 |
---|
[277] | 206 | |
---|
[209] | 207 | #define ATA_strSerial 10 |
---|
[277] | 208 | #define ATA_strSerial_Length 20 |
---|
| 209 | |
---|
[209] | 210 | #define ATA_strFirmware 23 |
---|
[277] | 211 | #define ATA_strFirmware_Length 8 |
---|
| 212 | |
---|
[209] | 213 | #define ATA_strModel 27 |
---|
[284] | 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 |
---|
[277] | 216 | |
---|
[209] | 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 | |
---|
[258] | 224 | // Words carved out of the vendor specific area for our use |
---|
| 225 | // |
---|
[277] | 226 | #define ATA_wSerialServerVersion 157 |
---|
[334] | 227 | #define ATA_wSerialDriveFlags 158 |
---|
[258] | 228 | #define ATA_wSerialPortAndBaud 159 |
---|
[209] | 229 | |
---|
[258] | 230 | // Defines used in the words above |
---|
| 231 | // |
---|
[209] | 232 | #define ATA_wCaps_LBA 0x200 |
---|
| 233 | |
---|
| 234 | #define ATA_wGenCfg_FIXED 0x40 |
---|
| 235 | |
---|
[334] | 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 |
---|
[258] | 242 | |
---|
[209] | 243 | struct comPorts { |
---|
| 244 | unsigned long port; |
---|
| 245 | unsigned char com; |
---|
| 246 | }; |
---|
[526] | 247 | struct comPorts supportedComPorts[] = |
---|
| 248 | { |
---|
| 249 | { 0x3f8, '1' }, |
---|
| 250 | { 0x2f8, '2' }, |
---|
| 251 | { 0x3e8, '3' }, |
---|
| 252 | { 0x2e8, '4' }, |
---|
| 253 | { 0x2f0, '5' }, |
---|
| 254 | { 0x3e0, '6' }, |
---|
| 255 | { 0x2e0, '7' }, |
---|
[209] | 256 | { 0x260, '8' }, |
---|
| 257 | { 0x368, '9' }, |
---|
| 258 | { 0x268, 'A' }, |
---|
| 259 | { 0x360, 'B' }, |
---|
| 260 | { 0x270, 'C' }, |
---|
[526] | 261 | { 0, 0 } |
---|
[209] | 262 | }; |
---|
| 263 | |
---|
[277] | 264 | void Image::respondInquire( unsigned short *buff, unsigned short originalPortAndBaud, struct baudRate *baudRate, unsigned short port, unsigned char scan ) |
---|
[209] | 265 | { |
---|
[277] | 266 | char formatBuff[ 128 ]; |
---|
[284] | 267 | char speedBuff[ 128 ]; |
---|
[277] | 268 | |
---|
[233] | 269 | memset( &buff[0], 0, 514 ); |
---|
[209] | 270 | |
---|
[233] | 271 | if( scan ) |
---|
[209] | 272 | { |
---|
[233] | 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] | 284 | sprintf( speedBuff, " (COM%c/%s)", comPort, baudRate->display ); |
---|
[233] | 285 | else |
---|
[284] | 286 | sprintf( speedBuff, " (%s baud)", shortFileName, baudRate->display ); |
---|
| 287 | |
---|
| 288 | sprintf( formatBuff, "%.*s%s ", XTIDEBIOS_strModel_Length - strlen(speedBuff), shortFileName, speedBuff ); |
---|
[209] | 289 | } |
---|
| 290 | else |
---|
[284] | 291 | sprintf( formatBuff, "%.*s ", XTIDEBIOS_strModel_Length, shortFileName ); |
---|
[277] | 292 | strncpy( (char *) &buff[ATA_strModel], formatBuff, ATA_strModel_Length ); |
---|
| 293 | flipEndian( &buff[ATA_strModel], ATA_strModel_Length ); |
---|
[209] | 294 | |
---|
[277] | 295 | strncpy( (char *) &buff[ATA_strSerial], "SerialDrive ", ATA_strSerial_Length ); |
---|
| 296 | flipEndian( &buff[ATA_strSerial], ATA_strSerial_Length ); |
---|
[209] | 297 | |
---|
[277] | 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 ); |
---|
[209] | 301 | |
---|
[430] | 302 | buff[ ATA_wCylCnt ] = cyl; |
---|
| 303 | buff[ ATA_wHeadCnt ] = head; |
---|
| 304 | buff[ ATA_wSPT ] = sect; |
---|
| 305 | |
---|
| 306 | if( !useCHS ) |
---|
[217] | 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 | |
---|
[526] | 313 | // We echo back the port and baud that we were called on from the client, |
---|
[277] | 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 | |
---|
[334] | 322 | buff[ ATA_wSerialDriveFlags ] = ATA_wSerialDriveFlags_Present; |
---|
[258] | 323 | if( floppy ) |
---|
[526] | 324 | buff[ ATA_wSerialDriveFlags ] |= |
---|
[334] | 325 | ATA_wSerialDriveFlags_Floppy | (floppyType << ATA_wSerialDriveFlags_FloppyType_FieldPosition); |
---|
[258] | 326 | |
---|
| 327 | // we always set this, so that the bulk of the BIOS will consider this disk as a hard disk |
---|
| 328 | // |
---|
[209] | 329 | buff[ ATA_wGenCfg ] = ATA_wGenCfg_FIXED; |
---|
| 330 | } |
---|
[284] | 331 | |
---|