2007-03-27 17:47:10 +00:00
|
|
|
/*
|
|
|
|
* CiderPress
|
|
|
|
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
|
|
|
|
* See the file LICENSE for distribution terms.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Implementation of DiskFSOzDOS class.
|
|
|
|
*
|
|
|
|
* It would make life MUCH EASIER to have the DiskImg recognize this as
|
|
|
|
* a file format and just rearrange the blocks into linear order for us,
|
|
|
|
* but unfortunately that's not going to happen.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ===========================================================================
|
2014-11-04 00:26:53 +00:00
|
|
|
* DiskFSOzDOS
|
2007-03-27 17:47:10 +00:00
|
|
|
* ===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
const int kExpectedNumBlocks = 1600;
|
2014-11-04 00:26:53 +00:00
|
|
|
const int kExpectedTracks = 50; // 50 tracks of 32 sectors == 400K
|
2007-03-27 17:47:10 +00:00
|
|
|
const int kExpectedSectors = 32;
|
|
|
|
const int kVTOCTrack = 17;
|
|
|
|
const int kVTOCSector = 0;
|
|
|
|
const int kSctSize = 256;
|
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
const int kCatalogEntrySize = 0x23; // length in bytes of catalog entries
|
|
|
|
const int kCatalogEntriesPerSect = 7; // #of entries per catalog sector
|
|
|
|
const int kMaxTSPairs = 0x7a; // 122 entries for 256-byte sectors
|
|
|
|
const int kTSOffset = 0x0c; // first T/S entry in a T/S list
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
const int kMaxTSIterations = 32;
|
|
|
|
const int kMaxCatalogIterations = 64;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a track/sector, adjusting for 32-sector disks being treated as
|
|
|
|
* if they were 16-sector.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
static DIError ReadTrackSectorAdjusted(DiskImg* pImg, int track, int sector,
|
|
|
|
int sectorOffset, uint8_t* buf, DiskImg::SectorOrder imageOrder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
track *= 4;
|
|
|
|
sector = sector * 2 + sectorOffset;
|
|
|
|
while (sector >= 16) {
|
|
|
|
track++;
|
|
|
|
sector -= 16;
|
|
|
|
}
|
|
|
|
return pImg->ReadTrackSectorSwapped(track, sector, buf, imageOrder,
|
|
|
|
DiskImg::kSectorOrderDOS);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test for presence of 400K OzDOS 3.3 volumes.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
static DIError TestImageHalf(DiskImg* pImg, int sectorOffset,
|
|
|
|
DiskImg::SectorOrder imageOrder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr = kDIErrNone;
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t sctBuf[kSctSize];
|
2014-11-04 00:26:53 +00:00
|
|
|
int numTracks, numSectors;
|
|
|
|
int catTrack, catSect;
|
|
|
|
int foundGood = 0;
|
|
|
|
int iterations = 0;
|
|
|
|
|
|
|
|
assert(sectorOffset == 0 || sectorOffset == 1);
|
|
|
|
|
|
|
|
dierr = ReadTrackSectorAdjusted(pImg, kVTOCTrack, kVTOCSector,
|
|
|
|
sectorOffset, sctBuf, imageOrder);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
catTrack = sctBuf[0x01];
|
|
|
|
catSect = sctBuf[0x02];
|
|
|
|
numTracks = sctBuf[0x34];
|
|
|
|
numSectors = sctBuf[0x35];
|
|
|
|
|
|
|
|
if (!(sctBuf[0x27] == kMaxTSPairs) ||
|
|
|
|
/*!(sctBuf[0x36] == 0 && sctBuf[0x37] == 1) ||*/ // bytes per sect
|
|
|
|
!(numTracks == kExpectedTracks) ||
|
|
|
|
!(numSectors == 32) ||
|
|
|
|
!(catTrack < numTracks && catSect < numSectors) ||
|
|
|
|
0)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS header test %d failed", sectorOffset);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrFilesystemNotFound;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk through the catalog track to try to figure out ordering.
|
|
|
|
*/
|
|
|
|
while (catTrack != 0 && catSect != 0 && iterations < kMaxCatalogIterations)
|
|
|
|
{
|
|
|
|
dierr = ReadTrackSectorAdjusted(pImg, catTrack, catSect,
|
|
|
|
sectorOffset, sctBuf, imageOrder);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail_ok; /* allow it if not fully broken */
|
|
|
|
|
|
|
|
if (sctBuf[1] == catTrack && sctBuf[2] == catSect-1)
|
|
|
|
foundGood++;
|
|
|
|
|
|
|
|
catTrack = sctBuf[1];
|
|
|
|
catSect = sctBuf[2];
|
|
|
|
iterations++; // watch for infinite loops
|
|
|
|
}
|
|
|
|
if (iterations >= kMaxCatalogIterations) {
|
|
|
|
dierr = kDIErrDirectoryLoop;
|
|
|
|
goto bail;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
bail_ok:
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS foundGood=%d off=%d swap=%d", foundGood, sectorOffset,
|
2014-11-04 00:26:53 +00:00
|
|
|
imageOrder);
|
|
|
|
/* foundGood hits 3 even when swap is wrong */
|
|
|
|
if (foundGood > 4)
|
|
|
|
dierr = kDIErrNone;
|
|
|
|
else
|
|
|
|
dierr = kDIErrFilesystemNotFound;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
bail:
|
2014-11-04 00:26:53 +00:00
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test both of the DOS partitions.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
static DIError TestImage(DiskImg* pImg, DiskImg::SectorOrder imageOrder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS checking first half (swap=%d)", imageOrder);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = TestImageHalf(pImg, 0, imageOrder);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS checking second half (swap=%d)", imageOrder);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = TestImageHalf(pImg, 1, imageOrder);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test to see if the image is a OzDOS volume.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ DIError DiskFSOzDOS::TestFS(DiskImg* pImg, DiskImg::SectorOrder* pOrder,
|
2007-03-27 17:47:10 +00:00
|
|
|
DiskImg::FSFormat* pFormat, FSLeniency leniency)
|
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
/* only on 800K disks (at the least, insist on numTracks being even) */
|
|
|
|
if (pImg->GetNumBlocks() != kExpectedNumBlocks)
|
|
|
|
return kDIErrFilesystemNotFound;
|
|
|
|
|
|
|
|
/* if a value is specified, try that first -- useful for OverrideFormat */
|
|
|
|
if (*pOrder != DiskImg::kSectorOrderUnknown) {
|
|
|
|
if (TestImage(pImg, *pOrder) == kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS accepted FirstTry value");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskImg::SectorOrder ordering[DiskImg::kSectorOrderMax];
|
|
|
|
|
|
|
|
DiskImg::GetSectorOrderArray(ordering, *pOrder);
|
|
|
|
|
|
|
|
for (int i = 0; i < DiskImg::kSectorOrderMax; i++) {
|
|
|
|
if (ordering[i] == DiskImg::kSectorOrderUnknown)
|
|
|
|
continue;
|
|
|
|
if (TestImage(pImg, ordering[i]) == kDIErrNone) {
|
|
|
|
*pOrder = ordering[i];
|
|
|
|
*pFormat = DiskImg::kFormatOzDOS;
|
|
|
|
return kDIErrNone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS didn't find valid FS");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrFilesystemNotFound;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Test to see if the image is a 'wide' (32-sector) DOS3.3 volume, i.e.
|
|
|
|
* half of a OzDOS volume.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ DIError DiskFS::TestOzWideDOS33(const DiskImg* pImg,
|
|
|
|
DiskImg::SectorOrder* pOrder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr = kDIErrNone;
|
|
|
|
|
|
|
|
/* only on 400K disks (at the least, insist on numTracks being even) */
|
|
|
|
if (pImg->GetNumBlocks() != kExpectedNumBlocks/2)
|
|
|
|
return kDIErrFilesystemNotFound;
|
|
|
|
|
|
|
|
/* if a value is specified, try that first -- useful for OverrideFormat */
|
|
|
|
if (*pOrder != DiskImg::kSectorOrderUnknown) {
|
|
|
|
if (TestImageHalf(pImg, 0, *pOrder) == kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" WideDOS accepted FirstTry value");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TestImageHalf(pImg, 0, DiskImg::kSectorOrderDOS) == kDIErrNone) {
|
|
|
|
*pOrder = DiskImg::kSectorOrderDOS;
|
|
|
|
} else if (TestImageHalf(pImg, 0, DiskImg::kSectorOrderProDOS) == kDIErrNone) {
|
|
|
|
*pOrder = DiskImg::kSectorOrderProDOS;
|
|
|
|
} else if (TestImageHalf(pImg, 0, DiskImg::kSectorOrderPhysical) == kDIErrNone) {
|
|
|
|
*pOrder = DiskImg::kSectorOrderPhysical;
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FS didn't find valid 'wide' DOS3.3");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrFilesystemNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up our sub-volumes.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSOzDOS::Initialize(void)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr = kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fScanForSubVolumes != kScanSubDisabled) {
|
|
|
|
dierr = OpenSubVolume(0);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = OpenSubVolume(1);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzDOS not scanning for sub-volumes");
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
SetVolumeUsageMap();
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open up one of the DOS 3.3 sub-volumes.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSOzDOS::OpenSubVolume(int idx)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr = kDIErrNone;
|
2014-11-18 05:13:13 +00:00
|
|
|
DiskFS* pNewFS = NULL;
|
|
|
|
DiskImg* pNewImg = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
pNewImg = new DiskImg;
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pNewImg == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrMalloc;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the full 800K; SetPairedSectors cuts it in half
|
|
|
|
dierr = pNewImg->OpenImage(fpImg, 0, 0,
|
|
|
|
2 * kExpectedTracks * kExpectedSectors);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzSub: OpenImage(%d,0,%d) failed (err=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
0, 2 * kExpectedTracks * kExpectedSectors, dierr);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(idx == 0 || idx == 1);
|
|
|
|
pNewImg->SetPairedSectors(true, 1-idx);
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzSub: testing for recognizable volume in idx=%d", idx);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = pNewImg->AnalyzeImage();
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzSub: analysis failed (err=%d)", dierr);
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pNewImg->GetFSFormat() == DiskImg::kFormatUnknown ||
|
|
|
|
pNewImg->GetSectorOrder() == DiskImg::kSectorOrderUnknown)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzSub: unable to identify filesystem");
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrUnsupportedFSFmt;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open a DiskFS for the sub-image */
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNISub %d succeeded!", idx);
|
2014-11-04 00:26:53 +00:00
|
|
|
pNewFS = pNewImg->OpenAppropriateDiskFS();
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pNewFS == NULL) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" OzSub: OpenAppropriateDiskFS failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrUnsupportedFSFmt;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load the files from the sub-image */
|
|
|
|
dierr = pNewFS->Initialize(pNewImg, kInitFull);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGE(" OzSub: error %d reading list of files from disk", dierr);
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if this really is DOS 3.3, override the "volume name" */
|
|
|
|
if (pNewImg->GetFSFormat() == DiskImg::kFormatDOS33) {
|
|
|
|
DiskFSDOS33* pDOS = (DiskFSDOS33*) pNewFS; /* eek, a downcast */
|
|
|
|
pDOS->SetDiskVolumeNum(idx+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Success, add it to the sub-volume list.
|
|
|
|
*/
|
|
|
|
AddSubVolumeToList(pNewImg, pNewFS);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
bail:
|
2014-11-04 00:26:53 +00:00
|
|
|
if (dierr != kDIErrNone) {
|
|
|
|
delete pNewFS;
|
|
|
|
delete pNewImg;
|
|
|
|
}
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|