1 |
/* |
2 |
* PearPC |
3 |
* ata.cc |
4 |
* |
5 |
* Copyright (C) 2003, 2004 Sebastian Biallas (sb@biallas.net) |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include <cstdio> |
22 |
#include <cstring> |
23 |
#include <errno.h> |
24 |
|
25 |
#include "debug/tracers.h" |
26 |
#include "ata.h" |
27 |
|
28 |
#include "tools/snprintf.h" |
29 |
|
30 |
ATADevice::ATADevice(const char *name) |
31 |
: IDEDevice(name) |
32 |
{ |
33 |
} |
34 |
|
35 |
void ATADevice::init(int aHeads, int aCyl, int aSpt) |
36 |
{ |
37 |
mHeads = aHeads; |
38 |
mCyl = aCyl; |
39 |
mSpt = aSpt; |
40 |
} |
41 |
|
42 |
ATADevice::~ATADevice() |
43 |
{ |
44 |
} |
45 |
|
46 |
uint ATADevice::getBlockSize() |
47 |
{ |
48 |
return 512; |
49 |
} |
50 |
|
51 |
uint ATADevice::getBlockCount() |
52 |
{ |
53 |
return blocks; |
54 |
} |
55 |
|
56 |
/* |
57 |
* |
58 |
*/ |
59 |
ATADeviceFile::ATADeviceFile(const char *name, const char *filename) |
60 |
: ATADevice(name) |
61 |
{ |
62 |
mFile = sys_fopen(filename, SYS_OPEN_READ | SYS_OPEN_WRITE); |
63 |
if (mFile) { |
64 |
sys_fseek(mFile, 0, SYS_SEEK_END); |
65 |
uint64 size = sys_ftell(mFile); |
66 |
uint64 cyl = size / 516096ULL; |
67 |
blocks = size / 512; |
68 |
if ((size % 516096) || cyl > 65535) { |
69 |
// we only support disk images with 16 heads and 63 spt |
70 |
sys_fclose(mFile); |
71 |
mFile = NULL; |
72 |
setError("invalid format (filesize isn't a multiple of 516096)"); |
73 |
} else { |
74 |
init(16, cyl, 63); |
75 |
} |
76 |
} else { |
77 |
char buf[256]; |
78 |
ht_snprintf(buf, sizeof buf, "%s: could not open file (%s)", filename, strerror(errno)); |
79 |
setError(buf); |
80 |
} |
81 |
} |
82 |
|
83 |
ATADeviceFile::~ATADeviceFile() |
84 |
{ |
85 |
} |
86 |
|
87 |
bool ATADeviceFile::seek(uint64 blockno) |
88 |
{ |
89 |
sys_fseek(mFile, 512 * (uint64)blockno); |
90 |
return true; |
91 |
} |
92 |
|
93 |
void ATADeviceFile::flush() |
94 |
{ |
95 |
sys_flush(mFile); |
96 |
} |
97 |
|
98 |
int ATADeviceFile::readBlock(byte *buf) |
99 |
{ |
100 |
sys_fread(mFile, buf, 512); |
101 |
if (mMode & ATA_DEVICE_MODE_ECC) { |
102 |
// add ECC bytes.. |
103 |
IO_IDE_ERR("ATADeviceFile: ECC not implemented\n"); |
104 |
} |
105 |
return 0; |
106 |
} |
107 |
|
108 |
int ATADeviceFile::writeBlock(byte *buf) |
109 |
{ |
110 |
sys_fwrite(mFile, buf, 512); |
111 |
return 0; |
112 |
} |
113 |
|
114 |
bool ATADeviceFile::promSeek(FileOfs pos) |
115 |
{ |
116 |
return sys_fseek(mFile, pos) == 0; |
117 |
} |
118 |
|
119 |
uint ATADeviceFile::promRead(byte *buf, uint size) |
120 |
{ |
121 |
return sys_fread(mFile, buf, size); |
122 |
} |
123 |
|