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 DiskFSUNIDOS class.
|
|
|
|
*
|
|
|
|
* The "UNIDOS" filesystem doesn't actually hold files. Instead, it holds
|
|
|
|
* two 400K DOS 3.3 volumes on an 800K disk.
|
|
|
|
*
|
|
|
|
* We do have a test here for "wide" DOS 3.3, which is largely a clone of
|
|
|
|
* the standard DOS 3.3 test. The trick is that we have to adjust our
|
|
|
|
* detection to account for 32-sector tracks, and do so while the object
|
|
|
|
* is still in a state where it believes it has 16 sectors per track.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ===========================================================================
|
2014-11-04 00:26:53 +00:00
|
|
|
* DiskFSUNIDOS
|
2007-03-27 17:47:10 +00:00
|
|
|
* ===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
const int kExpectedNumBlocks = 1600;
|
|
|
|
const int kExpectedTracks = 50;
|
|
|
|
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 trackOffset, uint8_t* buf, DiskImg::SectorOrder imageOrder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
track += trackOffset;
|
|
|
|
track *= 2;
|
|
|
|
if (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 DOS 3.3 volumes.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
static DIError TestImageHalf(DiskImg* pImg, int trackOffset,
|
|
|
|
DiskImg::SectorOrder imageOrder, int* pGoodCount)
|
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;
|
|
|
|
|
|
|
|
*pGoodCount = 0;
|
|
|
|
|
|
|
|
dierr = ReadTrackSectorAdjusted(pImg, kVTOCTrack, kVTOCSector,
|
|
|
|
trackOffset, 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(" UNI/Wide DOS header test failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrFilesystemNotFound;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
foundGood++; // score one for a valid-looking VTOC
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk through the catalog track to try to figure out ordering.
|
|
|
|
*/
|
|
|
|
while (catTrack != 0 && catSect != 0 &&
|
|
|
|
iterations < DiskFSDOS33::kMaxCatalogSectors)
|
|
|
|
{
|
|
|
|
dierr = ReadTrackSectorAdjusted(pImg, catTrack, catSect,
|
|
|
|
trackOffset, sctBuf, imageOrder);
|
|
|
|
if (dierr != kDIErrNone) {
|
|
|
|
dierr = kDIErrNone;
|
|
|
|
break; /* allow it if earlier stuff was okay */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (catTrack == sctBuf[1] && catSect == sctBuf[2] +1)
|
|
|
|
foundGood++;
|
|
|
|
else if (catTrack == sctBuf[1] && catSect == sctBuf[2]) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" WideDOS detected self-reference on cat (%d,%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
catTrack, catSect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
catTrack = sctBuf[1];
|
|
|
|
catSect = sctBuf[2];
|
|
|
|
iterations++; // watch for infinite loops
|
|
|
|
}
|
|
|
|
if (iterations >= DiskFSDOS33::kMaxCatalogSectors) {
|
|
|
|
dierr = kDIErrDirectoryLoop;
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" WideDOS directory links cause a loop");
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" WideDOS foundGood=%d off=%d swap=%d", foundGood,
|
2014-11-04 00:26:53 +00:00
|
|
|
trackOffset, imageOrder);
|
|
|
|
*pGoodCount = foundGood;
|
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,
|
|
|
|
int* pGoodCount)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
|
|
|
int goodCount1, goodCount2;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
*pGoodCount = 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNIDOS checking first half (imageOrder=%d)", imageOrder);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = TestImageHalf(pImg, 0, imageOrder, &goodCount1);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNIDOS checking second half (imageOrder=%d)", imageOrder);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = TestImageHalf(pImg, kExpectedTracks, imageOrder, &goodCount2);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (goodCount1 > goodCount2)
|
|
|
|
*pGoodCount = goodCount1;
|
|
|
|
else
|
|
|
|
*pGoodCount = goodCount2;
|
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 UNIDOS volume.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ DIError DiskFSUNIDOS::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;
|
|
|
|
|
|
|
|
DiskImg::SectorOrder ordering[DiskImg::kSectorOrderMax];
|
|
|
|
|
|
|
|
DiskImg::GetSectorOrderArray(ordering, *pOrder);
|
|
|
|
|
|
|
|
DiskImg::SectorOrder bestOrder = DiskImg::kSectorOrderUnknown;
|
|
|
|
int bestCount = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < DiskImg::kSectorOrderMax; i++) {
|
|
|
|
int goodCount = 0;
|
|
|
|
|
|
|
|
if (ordering[i] == DiskImg::kSectorOrderUnknown)
|
|
|
|
continue;
|
|
|
|
if (TestImage(pImg, ordering[i], &goodCount) == kDIErrNone) {
|
|
|
|
if (goodCount > bestCount) {
|
|
|
|
bestCount = goodCount;
|
|
|
|
bestOrder = ordering[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestCount >= 4 ||
|
|
|
|
(leniency == kLeniencyVery && bestCount >= 2))
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" WideDOS test: bestCount=%d for order=%d", bestCount, bestOrder);
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(bestOrder != DiskImg::kSectorOrderUnknown);
|
|
|
|
*pOrder = bestOrder;
|
|
|
|
*pFormat = DiskImg::kFormatUNIDOS;
|
|
|
|
return kDIErrNone;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNIDOS didn't find valid FS");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrFilesystemNotFound;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test to see if the image is a 'wide' (32-sector) DOS3.3 volume, i.e.
|
|
|
|
* half of a UNIDOS volume (usually found embedded in ProDOS).
|
|
|
|
*
|
|
|
|
* Trying all possible formats is important here, because the wrong value for
|
|
|
|
* swap can return a "good" value of 7 (much less than the expected 30, but
|
|
|
|
* above a threshold of reasonableness).
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ DIError DiskFSUNIDOS::TestWideFS(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 400K "disks" */
|
|
|
|
if (pImg->GetNumBlocks() != kExpectedNumBlocks/2) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" WideDOS ignoring volume (numBlocks=%ld)",
|
2014-11-04 00:26:53 +00:00
|
|
|
pImg->GetNumBlocks());
|
|
|
|
return kDIErrFilesystemNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskImg::SectorOrder ordering[DiskImg::kSectorOrderMax];
|
|
|
|
|
|
|
|
DiskImg::GetSectorOrderArray(ordering, *pOrder);
|
|
|
|
|
|
|
|
DiskImg::SectorOrder bestOrder = DiskImg::kSectorOrderUnknown;
|
|
|
|
int bestCount = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < DiskImg::kSectorOrderMax; i++) {
|
|
|
|
int goodCount = 0;
|
|
|
|
|
|
|
|
if (ordering[i] == DiskImg::kSectorOrderUnknown)
|
|
|
|
continue;
|
|
|
|
if (TestImageHalf(pImg, 0, ordering[i], &goodCount) == kDIErrNone) {
|
|
|
|
if (goodCount > bestCount) {
|
|
|
|
bestCount = goodCount;
|
|
|
|
bestOrder = ordering[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestCount >= 4 ||
|
|
|
|
(leniency == kLeniencyVery && bestCount >= 2))
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNI/Wide test: bestCount=%d for order=%d", bestCount, bestOrder);
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(bestOrder != DiskImg::kSectorOrderUnknown);
|
|
|
|
*pOrder = bestOrder;
|
|
|
|
*pFormat = DiskImg::kFormatDOS33;
|
|
|
|
// up to the caller to adjust numTracks/numSectPerTrack
|
|
|
|
return kDIErrNone;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNI/Wide didn't find valid FS");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrFilesystemNotFound;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up our sub-volumes.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSUNIDOS::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(" UNIDOS 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 DiskFSUNIDOS::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
dierr = pNewImg->OpenImage(fpImg, kExpectedTracks * idx, 0,
|
|
|
|
kExpectedTracks * kExpectedSectors);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNISub: OpenImage(%d,0,%d) failed (err=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
kExpectedTracks * idx, kExpectedTracks * kExpectedSectors, dierr);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
dierr = pNewImg->AnalyzeImage();
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" UNISub: 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(" UNISub: 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(" UNISub: 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(" UNISub: 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
|
|
|
}
|