-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimd.cpp
349 lines (303 loc) · 11.9 KB
/
imd.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
imd.c: read and write ImageDisk .IMD files
Copyright (C) 2013 Adam Sampson <[email protected]>
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all
copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#define _POSIX_C_SOURCE 200809L
#include "disk.h"
#include "imd.h"
#include "util.h"
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#define IMD_END_OF_COMMENT 0x1A
#define IMD_HEAD_MASK 0x03
#define IMD_NEED_CYL_MAP 0x80
#define IMD_NEED_HEAD_MAP 0x40
#define IMD_ALL_FLAGS (IMD_HEAD_MASK | IMD_NEED_CYL_MAP | IMD_NEED_HEAD_MAP)
// These Sector Data Record flags are combined by +, not |.
#define IMD_SDR_DATA 0x01
#define IMD_SDR_IS_COMPRESSED 0x01
#define IMD_SDR_IS_DELETED 0x02
#define IMD_SDR_IS_ERROR 0x04
#define IMD_SDR_ANOTHER_DATA_FOLLOWS 0x08 // Extension to original .IMD file format.
#define IMD_SDR_HAS_DATA_COUNT 0x10 // Extension to original .IMD file format.
// Read a track and add it to the disk. Return false on EOF.
static bool read_imd_track(FILE *image, disk_t& disk) {
uint8_t header[5];
int count = fread(header, 1, 5, image);
if (count == 0 && feof(image)) {
return false;
}
if (count != 5) {
die("Couldn't read IMD track header");
}
int phys_cyl = header[1];
if (phys_cyl >= MAX_CYLS) {
die("IMD track cylinder value too large: %d", phys_cyl);
}
if (phys_cyl >= disk.num_phys_cyls) {
disk.num_phys_cyls = phys_cyl + 1;
}
if ((header[2] & ~IMD_ALL_FLAGS) != 0) {
die("IMD track has unsupported flags: %02x", header[2]);
}
int phys_head = header[2] & IMD_HEAD_MASK;
if (phys_head >= MAX_HEADS) {
die("IMD track head value too large: %d", phys_head);
}
if (phys_head >= disk.num_phys_heads) {
disk.num_phys_heads = phys_head + 1;
}
track_t& track = disk.tracks[phys_cyl][phys_head];
track.status = TRACK_PROBED;
for (int i = 0; ; i++) {
if (DATA_MODES[i].name == NULL) {
die("IMD track mode unknown: %d", header[0]);
}
if (DATA_MODES[i].imd_mode == header[0]) {
track.data_mode = &DATA_MODES[i];
break;
}
}
track.phys_cyl = phys_cyl;
track.phys_head = phys_head;
size_t num_sectors = header[3];
track.num_sectors = num_sectors;
track.sector_size_code = header[4];
if (track.num_sectors == 0) {
return true; // Nothing else to do. (Note: a completely unreadable track will have no sectors and sector_size_code 0xFF.)
}
if (track.sector_size_code == 0xFF) {
// FIXME: implement this (by having arbitrary sector sizes)
die("IMD variable sector size extension not supported");
}
size_t sector_size = sector_bytes(track.sector_size_code);
uint8_t sec_map[num_sectors];
uint8_t cyl_map[num_sectors];
uint8_t head_map[num_sectors];
if (fread(sec_map, 1, num_sectors, image) != num_sectors) {
die("Couldn't read IMD sector map");
}
if (header[2] & IMD_NEED_CYL_MAP) {
if (fread(cyl_map, 1, num_sectors, image) != num_sectors) {
die("Couldn't read IMD cylinder map");
}
} else {
memset(cyl_map, phys_cyl, num_sectors);
}
if (header[2] & IMD_NEED_HEAD_MAP) {
if (fread(head_map, 1, num_sectors, image) != num_sectors) {
die("Couldn't read IMD head map");
}
} else {
memset(head_map, phys_head, num_sectors);
}
for (size_t phys_sec = 0; phys_sec < num_sectors; phys_sec++) {
sector_t& sector = track.sectors[phys_sec];
assert(sector.status == SECTOR_MISSING);
sector.log_cyl = cyl_map[phys_sec];
sector.log_head = head_map[phys_sec];
sector.log_sector = sec_map[phys_sec];
sector.deleted = false;
sector.datas.clear();
bool first_read = true;
bool have_data_to_read = true;
while (have_data_to_read) {
have_data_to_read = false; // By default we only have one sector type to read.
uint8_t type, orig_type;
uint32_t count = 1;
if (fread(&type, 1, 1, image) != 1) {
die("Couldn't read IMD sector header");
}
orig_type = type;
if (type > 0) {
//printf("%s:%d got type %08x for (%d.%d.%d)\n", __FILE__, __LINE__, type, sector.log_cyl, sector.log_head, sector.log_sector);
type -= IMD_SDR_DATA;
if (type >= IMD_SDR_HAS_DATA_COUNT) {
type -= IMD_SDR_HAS_DATA_COUNT;
if (fread(&count, sizeof(count), 1, image) != 1) {
die_errno("Couldn't read IMD data count");
}
count = ntohl(count);
assert(count > 1);
}
if (type >= IMD_SDR_ANOTHER_DATA_FOLLOWS) {
type -= IMD_SDR_ANOTHER_DATA_FOLLOWS;
have_data_to_read = true; // This flag means another data follows this one.
}
if (first_read) {
if (type >= IMD_SDR_IS_ERROR) {
type -= IMD_SDR_IS_ERROR;
sector.status = SECTOR_BAD;
} else {
sector.status = SECTOR_GOOD;
}
} else {
assert(type < IMD_SDR_IS_ERROR);
}
if (first_read) {
if (type >= IMD_SDR_IS_DELETED) {
type -= IMD_SDR_IS_DELETED;
sector.deleted = true;
}
} else {
assert(type < IMD_SDR_IS_DELETED);
}
data_t this_data;
if (type >= IMD_SDR_IS_COMPRESSED) {
type -= IMD_SDR_IS_COMPRESSED;
uint8_t fill;
if (fread(&fill, 1, 1, image) != 1) {
die("Couldn't read IMD compressed sector data");
}
this_data.assign(sector_size, fill);
} else {
uint8_t data_buf[sector_size];
if (fread(data_buf, 1, sector_size, image) != sector_size) {
die("Couldn't read IMD sector data");
}
this_data.assign(data_buf, sector_size);
}
std::pair<data_map_t::iterator, bool> ret = sector.datas.insert(data_map_t::value_type(this_data, count));
if (!ret.second) {
die("unexpected duplicate data");
}
if (type != 0) {
die("IMD sector has unsupported flags: %08x", orig_type);
}
}
first_read = false;
}
}
return true;
}
void read_imd(FILE *image, disk_t& disk) {
init_disk(disk);
// Read the comment.
char* read_buf;
size_t dummy = 0;
ssize_t count = getdelim(&read_buf, &dummy, IMD_END_OF_COMMENT, image);
if (count < 0 || read_buf[count-1] != IMD_END_OF_COMMENT) {
die("Couldn't find IMD comment delimiter");
}
disk.comment = std::string(read_buf, count-1);
free(read_buf); // free in accordance with getdelim spec
disk.num_phys_cyls = 0;
disk.num_phys_heads = 0;
while (read_imd_track(image, disk)) {
// Nothing.
}
}
void write_imd_header(const disk_t& disk, FILE *image) {
if (!disk.comment.empty()) {
fwrite(disk.comment.c_str(), 1, disk.comment.length(), image);
}
fputc(IMD_END_OF_COMMENT, image);
}
void write_imd_track(const track_t& track, FILE *image) {
uint8_t flags = 0;
uint8_t sec_map[track.num_sectors];
uint8_t cyl_map[track.num_sectors];
uint8_t head_map[track.num_sectors];
for (int i = 0; i < track.num_sectors; i++) {
const sector_t& sector = track.sectors[i];
sec_map[i] = sector.log_sector;
cyl_map[i] = sector.log_cyl;
head_map[i] = sector.log_head;
if (cyl_map[i] != track.phys_cyl) {
flags |= IMD_NEED_CYL_MAP;
}
if (head_map[i] != track.phys_head) {
flags |= IMD_NEED_HEAD_MAP;
}
}
assert(track.data_mode);
const uint8_t header[] = {
track.data_mode->imd_mode,
track.phys_cyl,
uint8_t(flags | track.phys_head),
track.num_sectors,
track.sector_size_code,
};
fwrite(header, 1, 5, image);
fwrite(sec_map, 1, track.num_sectors, image);
if (flags & IMD_NEED_CYL_MAP) {
fwrite(cyl_map, 1, track.num_sectors, image);
}
if (flags & IMD_NEED_HEAD_MAP) {
fwrite(head_map, 1, track.num_sectors, image);
}
for (int i = 0; i < track.num_sectors; i++) {
const sector_t& sector = track.sectors[i];
uint8_t type = 0;
//printf("sector i %d status %d\n", i, sector.status);
assert(sector.datas.empty() == (sector.status == SECTOR_MISSING));
switch (sector.status) {
case SECTOR_MISSING:
break;
case SECTOR_BAD:
type = IMD_SDR_DATA + IMD_SDR_IS_ERROR;
break;
case SECTOR_GOOD:
type = IMD_SDR_DATA;
break;
}
if (sector.deleted) {
type += IMD_SDR_IS_DELETED;
assert(!sector.datas.empty());
}
if (!sector.datas.empty()) {
for (data_map_t::const_iterator iter = sector.datas.begin(); iter != sector.datas.end(); iter++) {
assert(iter->first.length() == sector_bytes(track.sector_size_code));
if (iter->second > 1) {
type += IMD_SDR_HAS_DATA_COUNT;
}
if (std::distance(iter, sector.datas.end()) != 1) {
type += IMD_SDR_ANOTHER_DATA_FOLLOWS;
}
// If every byte in the sector is identical, just store it once, with a "compressed" flag.
const uint8_t first = iter->first[0];
bool can_compress = true;
type += IMD_SDR_IS_COMPRESSED;
for (unsigned int i = 0; i < iter->first.length(); i++) {
if (iter->first[i] != first) {
can_compress = false;
type -= IMD_SDR_IS_COMPRESSED;
break;
}
}
//printf("%s:%d wrote type %08x for (%d.%d.%d)\n", __FILE__, __LINE__, type, sector.log_cyl, sector.log_head, sector.log_sector);
fputc(type, image);
if (iter->second > 1) {
uint32_t buf = htonl(iter->second);
size_t ret = fwrite(&buf, sizeof(buf), 1, image);
if (ret != 1) { die_errno("fwrite failed"); }
}
if (can_compress) {
fputc(first, image);
} else {
fwrite(iter->first.data(), 1, iter->first.length(), image);
}
type = IMD_SDR_DATA; // Only the first 'type' contains error flags.
}
} else {
//printf("%s:%d wrote type %08x for (%d.%d.%d)\n", __FILE__, __LINE__, type, sector.log_cyl, sector.log_head, sector.log_sector);
fputc(type, image);
}
}
}