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.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This is a container for the Parsons Engineering FocusDrive.
|
|
|
|
*
|
|
|
|
* The format was reverse-engineered by Ranger Harke.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
|
|
|
|
|
|
|
|
const int kBlkSize = 512;
|
2014-11-04 00:26:53 +00:00
|
|
|
const int kPartMapBlock = 0; // partition map lives here
|
|
|
|
const int kMaxPartitions = 30; // max allowed partitions on a drive
|
|
|
|
const int kPartNameStart = 1; // partition names start here (2 blocks)
|
|
|
|
const int kPartNameLen = 32; // max length of partition name
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
static const char* kSignature = "Parsons Engin.";
|
|
|
|
const int kSignatureLen = 14;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format of partition map. It resides in the first 256 bytes of block 0.
|
|
|
|
* All values are in little-endian order.
|
|
|
|
*
|
|
|
|
* We also make space here for the partition names, which live on blocks 1+2.
|
|
|
|
*/
|
|
|
|
typedef struct DiskFSFocusDrive::PartitionMap {
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t signature[kSignatureLen];
|
|
|
|
uint8_t unknown1;
|
|
|
|
uint8_t partCount; // could be ushort, combined w/unknown1
|
|
|
|
uint8_t unknown2[16];
|
2014-11-04 00:26:53 +00:00
|
|
|
struct Entry {
|
2014-11-24 20:56:19 +00:00
|
|
|
uint32_t startBlock;
|
|
|
|
uint32_t blockCount;
|
|
|
|
uint32_t unknown1;
|
|
|
|
uint32_t unknown2;
|
|
|
|
uint8_t name[kPartNameLen+1];
|
2014-11-04 00:26:53 +00:00
|
|
|
} entry[kMaxPartitions];
|
2007-03-27 17:47:10 +00:00
|
|
|
} PartitionMap;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out if this is a FocusDrive partition.
|
|
|
|
*
|
|
|
|
* The "imageOrder" parameter has no use here, because (in the current
|
|
|
|
* version) embedded parent volumes are implicitly ProDOS-ordered.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ DIError DiskFSFocusDrive::TestImage(DiskImg* pImg,
|
|
|
|
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 blkBuf[kBlkSize];
|
2014-11-04 00:26:53 +00:00
|
|
|
int partCount;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if block 0 is a FocusDrive partition map.
|
|
|
|
*/
|
|
|
|
dierr = pImg->ReadBlockSwapped(kPartMapBlock, blkBuf, imageOrder,
|
|
|
|
DiskImg::kSectorOrderProDOS);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
if (memcmp(blkBuf, kSignature, kSignatureLen) != 0) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDrive partition signature not found in first part block");
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrFilesystemNotFound;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
partCount = blkBuf[0x0f];
|
|
|
|
if (partCount == 0 || partCount > kMaxPartitions) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDrive partition count looks bad (%d)", partCount);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrFilesystemNotFound;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// success!
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" Looks like FocusDrive with %d partitions", partCount);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unpack a partition map block into a partition map data structure.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ void DiskFSFocusDrive::UnpackPartitionMap(const uint8_t* buf,
|
|
|
|
const uint8_t* nameBuf, PartitionMap* pMap)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
const uint8_t* ptr;
|
|
|
|
const uint8_t* namePtr;
|
2014-11-04 00:26:53 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(pMap->signature, &buf[0x00], kSignatureLen);
|
|
|
|
pMap->unknown1 = buf[0x0e];
|
|
|
|
pMap->partCount = buf[0x0f];
|
|
|
|
memcpy(pMap->unknown2, &buf[0x10], 16);
|
|
|
|
|
|
|
|
ptr = &buf[0x20];
|
|
|
|
namePtr = &nameBuf[kPartNameLen]; // not sure what first 32 bytes are
|
|
|
|
for (i = 0; i < kMaxPartitions; i++) {
|
|
|
|
pMap->entry[i].startBlock = GetLongLE(ptr);
|
|
|
|
pMap->entry[i].blockCount = GetLongLE(ptr+4);
|
|
|
|
pMap->entry[i].unknown1 = GetLongLE(ptr+8);
|
|
|
|
pMap->entry[i].unknown2 = GetLongLE(ptr+12);
|
|
|
|
|
|
|
|
memcpy(pMap->entry[i].name, namePtr, kPartNameLen);
|
|
|
|
pMap->entry[i].name[kPartNameLen] = '\0';
|
|
|
|
|
|
|
|
ptr += 0x10;
|
|
|
|
namePtr += kPartNameLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ptr == buf + kBlkSize);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug: dump the contents of the partition map.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ void DiskFSFocusDrive::DumpPartitionMap(const PartitionMap* pMap)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
int i;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDrive partition map (%d partitions):", pMap->partCount);
|
2014-11-04 00:26:53 +00:00
|
|
|
for (i = 0; i < pMap->partCount; i++) {
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGI(" %2d: %8d %8d '%s'", i, pMap->entry[i].startBlock,
|
2014-11-04 00:26:53 +00:00
|
|
|
pMap->entry[i].blockCount, pMap->entry[i].name);
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open up a sub-volume.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSFocusDrive::OpenSubVolume(long startBlock, long numBlocks,
|
2014-11-04 00:26:53 +00:00
|
|
|
const char* name)
|
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
|
|
|
//bool tweaked = false;
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Adding %ld +%ld", startBlock, numBlocks);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
if (startBlock > fpImg->GetNumBlocks()) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("FocusDrive start block out of range (%ld vs %ld)",
|
2014-11-04 00:26:53 +00:00
|
|
|
startBlock, fpImg->GetNumBlocks());
|
|
|
|
return kDIErrBadPartition;
|
|
|
|
}
|
|
|
|
if (startBlock + numBlocks > fpImg->GetNumBlocks()) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("FocusDrive partition too large (%ld vs %ld avail)",
|
2014-11-04 00:26:53 +00:00
|
|
|
numBlocks, fpImg->GetNumBlocks() - startBlock);
|
|
|
|
fpImg->AddNote(DiskImg::kNoteInfo,
|
|
|
|
"Reduced partition from %ld blocks to %ld.\n",
|
|
|
|
numBlocks, fpImg->GetNumBlocks() - startBlock);
|
|
|
|
numBlocks = fpImg->GetNumBlocks() - startBlock;
|
|
|
|
//tweaked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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, startBlock, numBlocks);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDriveSub: OpenImage(%ld,%ld) failed (err=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
startBlock, numBlocks, dierr);
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
//LOGI(" +++ CFFASub: new image has ro=%d (parent=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
// pNewImg->GetReadOnly(), pImg->GetReadOnly());
|
|
|
|
|
|
|
|
/* figure out what the format is */
|
|
|
|
dierr = pNewImg->AnalyzeImage();
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDriveSub: analysis failed (err=%d)", dierr);
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we allow unrecognized partitions */
|
|
|
|
if (pNewImg->GetFSFormat() == DiskImg::kFormatUnknown ||
|
|
|
|
pNewImg->GetSectorOrder() == DiskImg::kSectorOrderUnknown)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDriveSub (%ld,%ld): unable to identify filesystem",
|
2014-11-04 00:26:53 +00:00
|
|
|
startBlock, numBlocks);
|
|
|
|
DiskFSUnknown* pUnknownFS = new DiskFSUnknown;
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pUnknownFS == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrInternal;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
//pUnknownFS->SetVolumeInfo((const char*)pMap->pmParType);
|
|
|
|
pNewFS = pUnknownFS;
|
|
|
|
} else {
|
|
|
|
/* open a DiskFS for the sub-image */
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDriveSub (%ld,%ld) analyze succeeded!", startBlock, numBlocks);
|
2014-11-04 00:26:53 +00:00
|
|
|
pNewFS = pNewImg->OpenAppropriateDiskFS(true);
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pNewFS == NULL) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDriveSub: OpenAppropriateDiskFS failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = kDIErrUnsupportedFSFmt;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pNewImg->AddNote(DiskImg::kNoteInfo, "Partition name='%s'.", name);
|
|
|
|
|
|
|
|
/* we encapsulate arbitrary stuff, so encourage child to scan */
|
|
|
|
pNewFS->SetScanForSubVolumes(kScanSubEnabled);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the files from the sub-image. When doing our initial tests,
|
|
|
|
* or when loading data for the volume copier, we don't want to dig
|
|
|
|
* into our sub-volumes, just figure out what they are and where.
|
|
|
|
*
|
|
|
|
* If "initialize" fails, the sub-volume won't get added to the list.
|
|
|
|
* It's important that a failure at this stage doesn't cause the whole
|
|
|
|
* thing to fall over.
|
|
|
|
*/
|
|
|
|
InitMode initMode;
|
|
|
|
if (GetScanForSubVolumes() == kScanSubContainerOnly)
|
|
|
|
initMode = kInitHeaderOnly;
|
|
|
|
else
|
|
|
|
initMode = kInitFull;
|
|
|
|
dierr = pNewFS->Initialize(pNewImg, initMode);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGE(" FocusDriveSub: error %d reading list of files from disk", dierr);
|
2014-11-04 00:26:53 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add it to the list */
|
|
|
|
AddSubVolumeToList(pNewImg, pNewFS);
|
2014-11-18 05:13:13 +00:00
|
|
|
pNewImg = NULL;
|
|
|
|
pNewFS = NULL;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
bail:
|
2014-11-04 00:26:53 +00:00
|
|
|
delete pNewFS;
|
|
|
|
delete pNewImg;
|
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if this is a FocusDrive volume.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
/*static*/ DIError DiskFSFocusDrive::TestFS(DiskImg* pImg, DiskImg::SectorOrder* pOrder,
|
2014-11-04 00:26:53 +00:00
|
|
|
DiskImg::FSFormat* pFormat, FSLeniency leniency)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (pImg->GetNumBlocks() < kMinInterestingBlocks)
|
|
|
|
return kDIErrFilesystemNotFound;
|
|
|
|
if (pImg->GetIsEmbedded()) // don't look for partitions inside
|
|
|
|
return kDIErrFilesystemNotFound;
|
|
|
|
|
|
|
|
/* assume ProDOS -- shouldn't matter, since it's embedded */
|
|
|
|
if (TestImage(pImg, DiskImg::kSectorOrderProDOS) == kDIErrNone) {
|
|
|
|
*pFormat = DiskImg::kFormatFocusDrive;
|
|
|
|
*pOrder = DiskImg::kSectorOrderProDOS;
|
|
|
|
return kDIErrNone;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FS didn't find valid FocusDrive");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrFilesystemNotFound;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep the FocusDrive "container" for use.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSFocusDrive::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-18 21:05:15 +00:00
|
|
|
LOGI("FocusDrive initializing (scanForSub=%d)", fScanForSubVolumes);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
/* seems pointless *not* to, but we just do what we're told */
|
|
|
|
if (fScanForSubVolumes != kScanSubDisabled) {
|
|
|
|
dierr = FindSubVolumes();
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return dierr;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
/* blank out the volume usage map */
|
|
|
|
SetVolumeUsageMap();
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the various sub-volumes and open them.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSFocusDrive::FindSubVolumes(void)
|
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 buf[kBlkSize];
|
|
|
|
uint8_t nameBuf[kBlkSize*2];
|
2014-11-04 00:26:53 +00:00
|
|
|
PartitionMap map;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dierr = fpImg->ReadBlock(kPartMapBlock, buf);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail;
|
|
|
|
dierr = fpImg->ReadBlock(kPartNameStart, nameBuf);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail;
|
|
|
|
dierr = fpImg->ReadBlock(kPartNameStart+1, nameBuf+kBlkSize);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail;
|
|
|
|
UnpackPartitionMap(buf, nameBuf, &map);
|
|
|
|
DumpPartitionMap(&map);
|
|
|
|
|
|
|
|
for (i = 0; i < map.partCount; i++) {
|
|
|
|
dierr = OpenVol(i, map.entry[i].startBlock, map.entry[i].blockCount,
|
|
|
|
(const char*)map.entry[i].name);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
goto bail;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the volume. If it fails, open a placeholder instead. (If *that*
|
|
|
|
* fails, return with an error.)
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskFSFocusDrive::OpenVol(int idx, long startBlock, long numBlocks,
|
2014-11-04 00:26:53 +00:00
|
|
|
const char* name)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
|
|
|
|
|
|
|
dierr = OpenSubVolume(startBlock, numBlocks, name);
|
|
|
|
if (dierr != kDIErrNone) {
|
|
|
|
if (dierr == kDIErrCancelled)
|
|
|
|
goto bail;
|
2014-11-18 05:13:13 +00:00
|
|
|
DiskFS* pNewFS = NULL;
|
|
|
|
DiskImg* pNewImg = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDrive failed opening sub-volume %d", idx);
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = CreatePlaceholder(startBlock, numBlocks, name, NULL,
|
|
|
|
&pNewImg, &pNewFS);
|
|
|
|
if (dierr == kDIErrNone) {
|
|
|
|
AddSubVolumeToList(pNewImg, pNewFS);
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI(" FocusDrive unable to create placeholder (err=%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr);
|
|
|
|
// fall out with error
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|