fluxfox 0.1.0

A library crate for working with floppy disk images for the IBM PC and compatibles.
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
    FluxFox
    https://github.com/dbalsom/fluxfox

    Copyright 2024 Daniel Balsom

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the “Software”),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

    --------------------------------------------------------------------------

    src/parsers/psi.rs

    A parser for the PSI disk image format.

    PSI format images are PCE Sector Images, an internal format used by the PCE emulator and
    devised by Hampa Hug.

    It is a chunk-based format similar to RIFF.

*/

use crate::{
    chs::{DiskCh, DiskChs, DiskChsn},
    diskimage::{DiskDescriptor, SectorDescriptor},
    file_parsers::{FormatCaps, ParserWriteCompatibility},
    io::{Cursor, ReadSeek, ReadWriteSeek},
};

use crate::{
    DiskDataEncoding,
    DiskDataRate,
    DiskDensity,
    DiskImage,
    DiskImageError,
    DiskImageFileFormat,
    FoxHashMap,
    FoxHashSet,
    LoadingCallback,
    DEFAULT_SECTOR_SIZE,
};
use binrw::{binrw, BinRead};

pub struct PsiFormat;
pub const MAXIMUM_CHUNK_SIZE: usize = 0x100000; // Reasonable 1MB limit for chunk sizes.

pub const SH_FLAG_COMPRESSED: u8 = 0b0001;
pub const SH_FLAG_ALTERNATE: u8 = 0b0010;
pub const SH_FLAG_CRC_ERROR: u8 = 0b0100;
pub const SH_IBM_FLAG_CRC_ERROR_ID: u8 = 0b0001;
pub const SH_IBM_FLAG_CRC_ERROR_DATA: u8 = 0b0010;
pub const SH_IBM_DELETED_DATA: u8 = 0b0100;
pub const SH_IBM_MISSING_DATA: u8 = 0b1000;

#[derive(Default)]
pub struct SectorContext {
    phys_chs: Option<DiskChs>,
    phys_size: usize,
    ibm_chsn: Option<DiskChsn>,
    data_crc_error: bool,
    address_crc_error: bool,
    deleted: bool,
    no_dam: bool,
    alternate: bool,
    bit_offset: Option<u32>,
}

impl SectorContext {
    fn have_context(&self) -> bool {
        self.phys_chs.is_some()
    }

    fn reset(&mut self) {
        *self = SectorContext::default();
    }

    #[allow(dead_code)]
    fn phys_ch(&self) -> DiskCh {
        DiskCh::from(self.phys_chs.unwrap())
    }

    fn sid(&self) -> DiskChsn {
        self.ibm_chsn.unwrap_or(DiskChsn::new(
            self.phys_chs.unwrap().c(),
            self.phys_chs.unwrap().h(),
            self.phys_chs.unwrap().s(),
            DiskChsn::bytes_to_n(self.phys_size),
        ))
    }
}

#[derive(Debug)]
#[binrw]
#[brw(big)]
pub struct PsiChunkHeader {
    pub id:   [u8; 4],
    pub size: u32,
}

#[derive(Debug)]
#[binrw]
#[brw(big)]
pub struct PsiHeader {
    pub version: u16,
    pub sector_format: [u8; 2],
}

#[derive(Debug)]
#[binrw]
#[brw(big)]
pub struct PsiChunkCrc {
    pub crc: u32,
}

#[binrw]
#[brw(big)]
pub struct PsiSectorHeader {
    pub cylinder: u16,
    pub head: u8,
    pub sector: u8,
    pub size: u16,
    pub flags: u8,
    pub compressed_data: u8,
}

#[binrw]
#[brw(big)]
pub struct PsiIbmSectorHeader {
    pub cylinder: u8,
    pub head: u8,
    pub sector: u8,
    pub n: u8,
    pub flags: u8,
    pub encoding: u8,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PsiChunkType {
    FileHeader,
    Text,
    SectorHeader,
    SectorData,
    WeakMask,
    IbmFmSectorHeader,
    IbmMfmSectorHeader,
    MacintoshSectorHeader,
    SectorPositionOffset,
    ClockRateAdjustment,
    End,
    Unknown,
}

pub struct PsiChunk {
    pub chunk_type: PsiChunkType,
    pub data: Vec<u8>,
}

pub(crate) fn psi_crc(buf: &[u8]) -> u32 {
    let mut crc = 0;
    for i in 0..buf.len() {
        crc ^= ((buf[i] & 0xff) as u32) << 24;

        for _j in 0..8 {
            if crc & 0x80000000 != 0 {
                crc = (crc << 1) ^ 0x1edc6f41;
            }
            else {
                crc <<= 1;
            }
        }
    }
    crc & 0xffffffff
}

pub(crate) fn decode_psi_sector_format(sector_format: [u8; 2]) -> Option<(DiskDataEncoding, DiskDensity)> {
    match sector_format {
        [0x00, 0x00] => Some((DiskDataEncoding::Fm, DiskDensity::Standard)),
        [0x01, 0x00] => Some((DiskDataEncoding::Fm, DiskDensity::Double)),
        [0x02, 0x00] => Some((DiskDataEncoding::Fm, DiskDensity::High)),
        [0x02, 0x01] => Some((DiskDataEncoding::Fm, DiskDensity::High)),
        [0x02, 0x02] => Some((DiskDataEncoding::Mfm, DiskDensity::Extended)),
        // TODO: What density are GCR disks? Are they all the same? PSI doesn't specify any variants.
        [0x03, 0x00] => Some((DiskDataEncoding::Gcr, DiskDensity::Double)),
        _ => None,
    }
}

impl PsiFormat {
    #[allow(dead_code)]
    fn format() -> DiskImageFileFormat {
        DiskImageFileFormat::PceSectorImage
    }

    pub(crate) fn capabilities() -> FormatCaps {
        FormatCaps::empty()
    }

    pub(crate) fn extensions() -> Vec<&'static str> {
        vec!["psi"]
    }

    pub(crate) fn detect<RWS: ReadSeek>(mut image: RWS) -> bool {
        let mut detected = false;
        _ = image.seek(std::io::SeekFrom::Start(0));

        if let Ok(file_header) = PsiChunkHeader::read_be(&mut image) {
            if file_header.id == "PSI ".as_bytes() {
                detected = true;
            }
        }

        detected
    }

    pub(crate) fn can_write(_image: &DiskImage) -> ParserWriteCompatibility {
        ParserWriteCompatibility::UnsupportedFormat
    }

    pub(crate) fn read_chunk<RWS: ReadSeek>(mut image: RWS) -> Result<PsiChunk, DiskImageError> {
        let chunk_pos = image.stream_position()?;

        //log::trace!("Reading chunk header...");
        let chunk_header = PsiChunkHeader::read(&mut image)?;

        if let Ok(id) = std::str::from_utf8(&chunk_header.id) {
            log::trace!("Chunk ID: {} Size: {}", id, chunk_header.size);
        }
        else {
            log::trace!("Chunk ID: {:?} Size: {}", chunk_header.id, chunk_header.size);
        }

        let chunk_type = match &chunk_header.id {
            b"PSI " => PsiChunkType::FileHeader,
            b"TEXT" => PsiChunkType::Text,
            b"END " => PsiChunkType::End,
            b"SECT" => PsiChunkType::SectorHeader,
            b"DATA" => PsiChunkType::SectorData,
            b"WEAK" => PsiChunkType::WeakMask,
            b"IBMF" => PsiChunkType::IbmFmSectorHeader,
            b"IBMM" => PsiChunkType::IbmMfmSectorHeader,
            b"MACG" => PsiChunkType::MacintoshSectorHeader,
            b"OFFS" => PsiChunkType::SectorPositionOffset,
            b"TIME" => PsiChunkType::ClockRateAdjustment,
            _ => {
                log::trace!("Unknown chunk type.");
                PsiChunkType::Unknown
            }
        };

        if chunk_header.size > MAXIMUM_CHUNK_SIZE as u32 {
            return Err(DiskImageError::FormatParseError);
        }

        let mut buffer = vec![0u8; chunk_header.size as usize + 8];

        //log::trace!("Seeking to chunk start...");
        image.seek(std::io::SeekFrom::Start(chunk_pos))?;
        image.read_exact(&mut buffer)?;

        let crc_calc = psi_crc(&buffer);
        let chunk_crc = PsiChunkCrc::read(&mut image)?;

        if chunk_crc.crc != crc_calc {
            return Err(DiskImageError::CrcError);
        }

        //log::trace!("CRC matched: {:04X} {:04X}", chunk_crc.crc, crc_calc);

        let chunk = PsiChunk {
            chunk_type,
            data: buffer[8..].to_vec(),
        };
        Ok(chunk)
    }

    pub(crate) fn load_image<RWS: ReadSeek>(
        mut read_buf: RWS,
        disk_image: &mut DiskImage,
        _callback: Option<LoadingCallback>,
    ) -> Result<(), DiskImageError> {
        disk_image.set_source_format(DiskImageFileFormat::PceSectorImage);

        // Seek to start of read_buf.
        read_buf.seek(std::io::SeekFrom::Start(0))?;

        let mut chunk = PsiFormat::read_chunk(&mut read_buf)?;
        // File header must be first chunk.
        if chunk.chunk_type != PsiChunkType::FileHeader {
            return Err(DiskImageError::UnknownFormat);
        }

        let file_header =
            PsiHeader::read(&mut Cursor::new(&chunk.data)).map_err(|_| DiskImageError::FormatParseError)?;
        log::trace!("Read PSI file header. Format version: {}", file_header.version);

        let (default_encoding, disk_density) =
            decode_psi_sector_format(file_header.sector_format).ok_or(DiskImageError::FormatParseError)?;

        let mut comment_string = String::new();

        let mut ctx = SectorContext::default();
        let mut track_set: FoxHashSet<DiskCh> = FoxHashSet::new();
        let mut sector_counts: FoxHashMap<u8, u32> = FoxHashMap::new();
        let mut heads_seen: FoxHashSet<u8> = FoxHashSet::new();
        let mut sectors_per_track = 0;

        let mut current_track = None;

        while chunk.chunk_type != PsiChunkType::End {
            match chunk.chunk_type {
                PsiChunkType::FileHeader => {}
                PsiChunkType::SectorHeader => {
                    //log::trace!("Sector header chunk.");
                    let sector_header = PsiSectorHeader::read(&mut Cursor::new(&chunk.data))?;
                    let chs = DiskChs::from((sector_header.cylinder, sector_header.head, sector_header.sector));
                    let ch = DiskCh::from((sector_header.cylinder, sector_header.head));

                    heads_seen.insert(sector_header.head);

                    if !track_set.contains(&ch) {
                        log::trace!("Adding track...");
                        let new_track =
                            disk_image.add_track_metasector(default_encoding, DiskDataRate::from(disk_density), ch)?;

                        current_track = Some(new_track);
                        track_set.insert(ch);
                        log::trace!("Observing sector count: {}", sectors_per_track);
                        sector_counts
                            .entry(sectors_per_track)
                            .and_modify(|e| *e += 1)
                            .or_insert(1);
                        sectors_per_track = 0;
                    }

                    if sector_header.flags & SH_FLAG_ALTERNATE != 0 {
                        log::trace!("Alternate sector data.");
                        ctx.alternate = true;
                    }
                    else {
                        ctx.alternate = false;
                    }

                    ctx.phys_chs = Some(chs);
                    ctx.phys_size = sector_header.size as usize;
                    ctx.data_crc_error = sector_header.flags & SH_FLAG_CRC_ERROR != 0;

                    // Write sector data immediately if compressed data is indicated (no sector data chunk follows)
                    if sector_header.flags & SH_FLAG_COMPRESSED != 0 {
                        log::trace!("Compressed sector data: {:02X}", sector_header.compressed_data);
                        let chunk_expand = vec![sector_header.compressed_data; sector_header.size as usize];

                        if let Some(ref mut track) = current_track {
                            // Add this sector to track.
                            let sd = SectorDescriptor {
                                id_chsn: DiskChsn::from((chs, DiskChsn::bytes_to_n(sector_header.size as usize))),
                                data: chunk_expand,
                                weak_mask: None,
                                hole_mask: None,
                                address_crc_error: false, // Compressed data cannot encode address CRC state.
                                data_crc_error: ctx.data_crc_error,
                                deleted_mark: false,
                                missing_data: false,
                            };

                            track.add_sector(&sd, ctx.alternate)?;
                            ctx.reset();
                        }
                        else {
                            log::error!("Tried to add sector without a current track.");
                            return Err(DiskImageError::FormatParseError);
                        }
                    }

                    log::trace!(
                        "SECT chunk: Sector ID: {} size: {} data_crc_error: {}",
                        chs,
                        sector_header.size,
                        ctx.data_crc_error
                    );
                }
                PsiChunkType::SectorData => {
                    if !ctx.have_context() {
                        log::error!("Sector data chunk without a preceding sector header.");
                        return Err(DiskImageError::FormatParseError);
                    }

                    log::trace!(
                        "DATA chunk: {} crc_error: {}",
                        ctx.phys_chs.unwrap(),
                        ctx.data_crc_error
                    );

                    if ctx.phys_size != chunk.data.len() {
                        log::warn!(
                            "Sector data size mismatch. Header specified: {} SectorData specified: {}",
                            ctx.phys_size,
                            chunk.data.len()
                        );
                    }

                    if let Some(ref mut track) = current_track {
                        // Add this sector to track.
                        let sd = SectorDescriptor {
                            id_chsn: ctx.sid(),
                            data: chunk.data,
                            weak_mask: None,
                            hole_mask: None,
                            address_crc_error: ctx.address_crc_error,
                            data_crc_error: ctx.data_crc_error,
                            deleted_mark: false,
                            missing_data: false,
                        };

                        track.add_sector(&sd, ctx.alternate)?;
                    }
                    else {
                        log::error!("Tried to add sector without a current track.");
                        return Err(DiskImageError::FormatParseError);
                    }
                    sectors_per_track += 1;
                    ctx.reset();
                }
                PsiChunkType::Text => {
                    // PSI docs:
                    // `If there are multiple TEXT chunks, their contents should be concatenated`
                    if let Ok(text) = std::str::from_utf8(&chunk.data) {
                        comment_string.push_str(text);
                    }
                }
                PsiChunkType::SectorPositionOffset => {
                    let offset = u32::from_be_bytes([chunk.data[0], chunk.data[1], chunk.data[2], chunk.data[3]]);
                    ctx.bit_offset = Some(offset);
                    log::trace!("Sector position offset: {}", offset);
                }
                PsiChunkType::IbmMfmSectorHeader => {
                    let ibm_header = PsiIbmSectorHeader::read(&mut Cursor::new(&chunk.data))?;

                    if ctx.ibm_chsn.is_some() {
                        log::warn!("Duplicate IBM sector header or context not reset");
                    }

                    ctx.ibm_chsn = Some(DiskChsn::from((
                        ibm_header.cylinder as u16,
                        ibm_header.head,
                        ibm_header.sector,
                        ibm_header.n,
                    )));

                    ctx.data_crc_error = ibm_header.flags & SH_IBM_FLAG_CRC_ERROR_DATA != 0;
                    ctx.address_crc_error = ibm_header.flags & SH_IBM_FLAG_CRC_ERROR_ID != 0;
                    ctx.deleted = ibm_header.flags & SH_IBM_DELETED_DATA != 0;
                    ctx.no_dam = ibm_header.flags & SH_IBM_MISSING_DATA != 0;
                }
                PsiChunkType::End => {
                    log::trace!("End chunk.");
                    break;
                }
                _ => {
                    log::warn!("Unhandled chunk type: {:?}", chunk.chunk_type);
                }
            }

            chunk = PsiFormat::read_chunk(&mut read_buf)?;
        }

        let head_ct = heads_seen.len() as u8;
        let track_ct = track_set.len() as u16;
        disk_image.descriptor = DiskDescriptor {
            geometry: DiskCh::from((track_ct / head_ct as u16, head_ct)),
            data_rate: Default::default(),
            data_encoding: DiskDataEncoding::Mfm,
            density: disk_density,
            default_sector_size: DEFAULT_SECTOR_SIZE,
            rpm: None,
            write_protect: None,
        };

        Ok(())
    }

    pub fn save_image<RWS: ReadWriteSeek>(_image: &DiskImage, _output: &mut RWS) -> Result<(), DiskImageError> {
        Err(DiskImageError::UnsupportedFormat)
    }
}