-
Notifications
You must be signed in to change notification settings - Fork 0
/
HdfHeader.cpp
204 lines (180 loc) · 5.32 KB
/
HdfHeader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// HdfHeader.cpp: implementation of the CHdfHeader class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "3eExplorer.h"
#include "HdfHeader.h"
#include "3eExplorerDoc.h"
#include "IDEDOS_SystemPartition.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
std::string CHdfHeader::HDF_Signature("RS-IDE");
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHdfHeader::CHdfHeader(CMy3eExplorerDoc * _p3eDoc, char * block, const int blocksize, const char * _name)
: CTreeObject(_p3eDoc, HDF, _name, true)
, bHalvedSectorData(FALSE)
, bHalfWordUsed(FALSE)
{
if (0x80 > blocksize)
{
// we should throw an exception here
throw EHdfInvalid("File is invalid");
}
else
{
// check that it's the right filetype from the first few bytes
std::string header;
header.append(block, 6);
if (!HDF_Signature.compare(header) && 0x1a == block[0x06])
{
// check the revision
revision = block[0x07];
if (0x10 != revision && 0x11 != revision)
{
// it's not an acceptable revision of the HDF format
std::ostringstream except;
int major = (revision >> 4);
int minor = (revision & 0x0f);
except << "Revision " << major << "." << minor << " of HDF format not supported";
throw EHdfUnsupported(except.str());
}
else
{
// we're ok so get more values...
bHalvedSectorData = block[0x08] & 0x01;
bATAPIDevice = block[0x08] & 0x02;
// for the moment ATAPI is not supported - sorry
if (0x11 == revision && bATAPIDevice)
{
throw EHdfUnsupported("ATAPI Devices are not currently supported, sorry.");
}
image_offset = *(reinterpret_cast<WORD*>(&block[0x09]));
bOffset_valid = FALSE;
// verify this offset, tho' it doesn't actually matter
if ((0x10 == revision && 0x0080 == image_offset) ||
(0x11 == revision && 0x0216 == image_offset))
{
bOffset_valid = TRUE;
}
// now copy (some of) the rest of the block into the ATA Device ID output
::CopyMemory(&ata_device_id, &block[0x16], sizeof(T_ATA_Device_ID));
}
}
else
{
// throw an exception
throw EHdfInvalid("Not an HDF image - incorrect magic");
}
}
}
CHdfHeader::~CHdfHeader()
{
}
void CHdfHeader::getCHS(CCHS &_chs) const
{
_chs.c = getCylinders();
_chs.h = getHeads();
_chs.s = getSectors();
}
unsigned int CHdfHeader::getOffset() const
{
return image_offset;
}
unsigned int CHdfHeader::getCylinders() const
{
return ata_device_id.cylinders;
}
unsigned int CHdfHeader::getHeads() const
{
return ata_device_id.heads;
}
unsigned int CHdfHeader::getSectors() const
{
return ata_device_id.sectors;
}
unsigned int CHdfHeader::getSectorSize()
{
// need to parse this now, or the sector size could be wrong
ParseForChildren();
return bHalvedSectorData || bHalfWordUsed ? 256 : 512;
}
void CHdfHeader::ParseForChildren()
{
if (NULL != m_p3eDoc && !m_bParsedForChildren)
{
m_bParsedForChildren = TRUE;
// this is an HDF, so parse the first sector...
// the first item _must_ be a PLUSIDEDOS partition.
// or... it could be on the second track :]
// so we'll validate it...
try
{
CCHS chs(0,0,0);
char * ss_buff;
std::string header = "PLUSIDEDOS ";
std::string block;
m_p3eDoc->ReadSector(chs, &ss_buff);
block.append(ss_buff, 0x10);
if (header.compare(block) || CIDEDOSPartitionDefinition::SYSTEM != ss_buff[0x10])
{
// try the next track
++chs.h;
m_p3eDoc->ReadSector(chs, &ss_buff);
block = "";
block.append(ss_buff, 0x10);
if (header.compare(block) || CIDEDOSPartitionDefinition::SYSTEM != ss_buff[0x10])
{
// ok, it's still not valid.
// we'll try the same thing but with half-word data...
m_p3eDoc->FlushCache();
bHalfWordUsed = TRUE;
--chs.h;
m_p3eDoc->ReadSector(chs, &ss_buff);
block = "";
block.append(ss_buff, 0x10);
if (header.compare(block) || CIDEDOSPartitionDefinition::SYSTEM != ss_buff[0x10])
{
++chs.h;
m_p3eDoc->ReadSector(chs, &ss_buff);
block = "";
block.append(ss_buff, 0x10);
}
}
}
if (!header.compare(block) && CIDEDOSPartitionDefinition::SYSTEM == ss_buff[0x10])
{
// it's a valid PLUSIDEDOS image
// create a new object (system partition) and add it as a child
CIDEDOS_SystemPartition *cide_sys = new CIDEDOS_SystemPartition(m_p3eDoc, ss_buff, chs.h);
m_vChildren.push_back(cide_sys);
}
else
{
throw EHdfInvalid("Not a PLUSIDEDOS image");
}
}
catch (CFileException *e)
{
m_p3eDoc->ReportSaveLoadException(m_p3eDoc->Filename().c_str(), e, FALSE, IDS_FILE_ACCESS_ERROR);
}
catch (EHdfInvalid &e)
{
::AfxMessageBox(e.what(), MB_ICONEXCLAMATION | MB_OK);
}
}
}
std::string CHdfHeader::GetInformation()
{
std::ostringstream ostr;
ostr << "HDF: " << GetName() << "\r\n";
ostr << "CHS: " << getCylinders() << "/" << getHeads() << "/" << getSectors() << "\r\n";
ostr << "Sec: " << getSectorSize() << " bytes";
if (bHalfWordUsed)
ostr << " : half-word mode";
return(ostr.str());
}