2015-06-01 09:35:51 +00:00
|
|
|
package com.bytezone.diskbrowser.prodos;
|
|
|
|
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.swing.tree.DefaultMutableTreeNode;
|
|
|
|
|
|
|
|
import com.bytezone.diskbrowser.applefile.AppleFileSource;
|
|
|
|
import com.bytezone.diskbrowser.applefile.BootSector;
|
|
|
|
import com.bytezone.diskbrowser.disk.*;
|
|
|
|
import com.bytezone.diskbrowser.gui.DataSource;
|
2016-02-24 21:11:14 +00:00
|
|
|
import com.bytezone.diskbrowser.utilities.HexFormatter;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
public class ProdosDisk extends AbstractFormattedDisk
|
|
|
|
{
|
2016-03-24 00:17:09 +00:00
|
|
|
protected final DateFormat df = DateFormat.getInstance ();
|
|
|
|
protected final SimpleDateFormat sdf = new SimpleDateFormat ("d-MMM-yy");
|
|
|
|
protected final SimpleDateFormat stf = new SimpleDateFormat ("H:mm");
|
2016-02-28 07:17:58 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
final SectorType dosSector = new SectorType ("Bootstrap Loader", Color.lightGray);
|
2016-02-28 07:17:58 +00:00
|
|
|
final SectorType catalogSector = new SectorType ("Catalog", new Color (0, 200, 0));
|
2015-06-01 09:35:51 +00:00
|
|
|
final SectorType volumeMapSector = new SectorType ("Volume Map", Color.blue);
|
|
|
|
final SectorType subcatalogSector = new SectorType ("Subcatalog", Color.magenta);
|
|
|
|
final SectorType masterIndexSector = new SectorType ("Master Index", Color.orange);
|
|
|
|
final SectorType indexSector = new SectorType ("Index", Color.cyan);
|
|
|
|
final SectorType dataSector = new SectorType ("Data", Color.red);
|
|
|
|
final SectorType extendedKeySector = new SectorType ("Extended key", Color.gray);
|
|
|
|
|
|
|
|
private final List<DirectoryHeader> headerEntries = new ArrayList<DirectoryHeader> ();
|
2016-02-28 07:17:58 +00:00
|
|
|
protected VolumeDirectoryHeader vdh;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
2016-02-28 07:17:58 +00:00
|
|
|
private static final boolean debug = false;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
public ProdosDisk (Disk disk)
|
|
|
|
{
|
|
|
|
super (disk);
|
|
|
|
|
|
|
|
sectorTypesList.add (dosSector);
|
|
|
|
sectorTypesList.add (catalogSector);
|
|
|
|
sectorTypesList.add (subcatalogSector);
|
|
|
|
sectorTypesList.add (volumeMapSector);
|
|
|
|
sectorTypesList.add (masterIndexSector);
|
|
|
|
sectorTypesList.add (indexSector);
|
|
|
|
sectorTypesList.add (dataSector);
|
|
|
|
sectorTypesList.add (extendedKeySector);
|
|
|
|
|
|
|
|
for (int block = 0; block < 2; block++)
|
|
|
|
if (!disk.isSectorEmpty (disk.getDiskAddress (block)))
|
|
|
|
sectorTypes[block] = dosSector;
|
|
|
|
|
2016-07-17 04:41:04 +00:00
|
|
|
DiskAddress da = disk.getDiskAddress (0);
|
|
|
|
byte[] buffer = disk.readSector (da);
|
|
|
|
bootSector = new BootSector (disk, buffer, "Prodos", da);
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
DefaultMutableTreeNode root = getCatalogTreeRoot ();
|
|
|
|
DefaultMutableTreeNode volumeNode = new DefaultMutableTreeNode ("empty volume node");
|
|
|
|
root.add (volumeNode);
|
|
|
|
processDirectoryBlock (2, null, volumeNode);
|
|
|
|
makeNodeVisible (volumeNode.getFirstLeaf ());
|
|
|
|
|
|
|
|
for (DiskAddress da2 : disk)
|
|
|
|
{
|
|
|
|
int blockNo = da2.getBlock ();
|
|
|
|
if (freeBlocks.get (blockNo))
|
|
|
|
{
|
|
|
|
if (!stillAvailable (da2))
|
|
|
|
falsePositives++;
|
|
|
|
}
|
|
|
|
else if (stillAvailable (da2))
|
|
|
|
falseNegatives++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processDirectoryBlock (int block, FileEntry parent,
|
2016-02-20 09:48:47 +00:00
|
|
|
DefaultMutableTreeNode parentNode)
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
DirectoryHeader localHeader = null;
|
|
|
|
SectorType currentSectorType = null;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
byte[] sectorBuffer = disk.readSector (block);
|
2017-04-02 05:02:29 +00:00
|
|
|
if (!disk.isSectorEmpty (block))
|
|
|
|
sectorTypes[block] = currentSectorType;
|
2015-06-01 09:35:51 +00:00
|
|
|
|
2017-03-14 04:50:23 +00:00
|
|
|
int max = disk.getBlockSize () - ProdosConstants.ENTRY_SIZE;
|
|
|
|
for (int ptr = 4; ptr < max; ptr += ProdosConstants.ENTRY_SIZE)
|
2015-06-01 09:35:51 +00:00
|
|
|
{
|
|
|
|
int storageType = (sectorBuffer[ptr] & 0xF0) >> 4;
|
2017-03-14 04:50:23 +00:00
|
|
|
if (storageType == 0) // deleted or unused
|
2015-06-01 09:35:51 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
byte[] entry = new byte[ProdosConstants.ENTRY_SIZE];
|
|
|
|
System.arraycopy (sectorBuffer, ptr, entry, 0, ProdosConstants.ENTRY_SIZE);
|
|
|
|
|
|
|
|
switch (storageType)
|
|
|
|
{
|
|
|
|
case ProdosConstants.TYPE_DIRECTORY_HEADER:
|
|
|
|
assert headerEntries.size () == 0;
|
|
|
|
vdh = new VolumeDirectoryHeader (this, entry);
|
|
|
|
localHeader = vdh;
|
|
|
|
assert localHeader.entryLength == ProdosConstants.ENTRY_SIZE;
|
|
|
|
headerEntries.add (localHeader);
|
|
|
|
currentSectorType = catalogSector;
|
2017-04-02 05:02:29 +00:00
|
|
|
if (!disk.isSectorEmpty (block))
|
|
|
|
sectorTypes[block] = currentSectorType;
|
2015-06-01 09:35:51 +00:00
|
|
|
for (int i = 0; i < vdh.totalBitMapBlocks; i++)
|
|
|
|
sectorTypes[vdh.bitMapBlock + i] = volumeMapSector;
|
2016-02-28 07:17:58 +00:00
|
|
|
parentNode.setUserObject (vdh); // populate the empty volume node
|
2015-06-01 09:35:51 +00:00
|
|
|
break;
|
2016-02-28 05:41:10 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
case ProdosConstants.TYPE_SUBDIRECTORY_HEADER:
|
|
|
|
localHeader = new SubDirectoryHeader (this, entry, parent);
|
|
|
|
headerEntries.add (localHeader);
|
|
|
|
currentSectorType = subcatalogSector;
|
2017-04-02 05:02:29 +00:00
|
|
|
if (!disk.isSectorEmpty (block))
|
|
|
|
sectorTypes[block] = currentSectorType;
|
2015-06-01 09:35:51 +00:00
|
|
|
break;
|
2016-02-28 05:41:10 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
case ProdosConstants.TYPE_SUBDIRECTORY:
|
|
|
|
FileEntry ce = new FileEntry (this, entry, localHeader, block);
|
|
|
|
fileEntries.add (ce);
|
|
|
|
DefaultMutableTreeNode directoryNode = new DefaultMutableTreeNode (ce);
|
|
|
|
directoryNode.setAllowsChildren (true);
|
|
|
|
parentNode.add (directoryNode);
|
2016-02-28 07:17:58 +00:00
|
|
|
processDirectoryBlock (ce.keyPtr, ce, directoryNode); // Recursion !!
|
2015-06-01 09:35:51 +00:00
|
|
|
break;
|
2016-02-28 05:41:10 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
case ProdosConstants.TYPE_SEEDLING:
|
|
|
|
case ProdosConstants.TYPE_SAPLING:
|
|
|
|
case ProdosConstants.TYPE_TREE:
|
|
|
|
case ProdosConstants.TYPE_PASCAL_ON_PROFILE:
|
|
|
|
case ProdosConstants.TYPE_GSOS_EXTENDED_FILE:
|
|
|
|
FileEntry fe = new FileEntry (this, entry, localHeader, block);
|
|
|
|
fileEntries.add (fe);
|
|
|
|
DefaultMutableTreeNode node = new DefaultMutableTreeNode (fe);
|
|
|
|
node.setAllowsChildren (false);
|
|
|
|
parentNode.add (node);
|
|
|
|
break;
|
2016-02-28 05:41:10 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
default:
|
|
|
|
System.out.println ("Unknown storage type : " + storageType);
|
|
|
|
System.out.println (HexFormatter.format (entry, 0, entry.length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block = HexFormatter.intValue (sectorBuffer[2], sectorBuffer[3]);
|
|
|
|
} while (block > 0);
|
2016-12-31 09:34:15 +00:00
|
|
|
|
|
|
|
// link double hi-res files
|
|
|
|
for (AppleFileSource fe : fileEntries)
|
|
|
|
{
|
|
|
|
String name = fe.getUniqueName ();
|
|
|
|
if (name.endsWith (".AUX"))
|
|
|
|
{
|
|
|
|
String partner1 = name.substring (0, name.length () - 4);
|
|
|
|
String partner2 = name.substring (0, name.length () - 4) + ".BIN";
|
|
|
|
for (AppleFileSource fe2 : fileEntries)
|
|
|
|
if (fe2.getUniqueName ().equals (partner1)
|
|
|
|
|| fe2.getUniqueName ().equals (partner2))
|
|
|
|
{
|
|
|
|
((FileEntry) fe2).link ((FileEntry) fe);
|
|
|
|
((FileEntry) fe).link ((FileEntry) fe2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 09:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isCorrectFormat (AppleDisk disk)
|
|
|
|
{
|
|
|
|
disk.setInterleave (1);
|
|
|
|
if (checkFormat (disk))
|
|
|
|
return true;
|
|
|
|
disk.setInterleave (0);
|
|
|
|
return checkFormat (disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean checkFormat (AppleDisk disk)
|
|
|
|
{
|
|
|
|
byte[] buffer = disk.readSector (2); // Prodos KEY BLOCK
|
|
|
|
if (debug)
|
2016-02-20 09:48:47 +00:00
|
|
|
{
|
2015-06-01 09:35:51 +00:00
|
|
|
System.out.println (HexFormatter.format (buffer));
|
2016-02-20 09:48:47 +00:00
|
|
|
System.out.printf ("Entry length : %02X%n", buffer[0x23]);
|
|
|
|
System.out.printf ("Entry per block: %02X%n", buffer[0x24]);
|
|
|
|
System.out.printf ("Bit map block : %02X%02X%n", buffer[0x27], buffer[0x28]);
|
|
|
|
}
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
// check entry length and entries per block
|
|
|
|
if (buffer[0x23] != 0x27 || buffer[0x24] != 0x0D)
|
|
|
|
return false;
|
|
|
|
int bitMapBlock = HexFormatter.intValue (buffer[0x27], buffer[0x28]);
|
|
|
|
if (bitMapBlock != 6)
|
|
|
|
return false;
|
2016-02-28 05:41:10 +00:00
|
|
|
|
2015-06-01 09:35:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DataSource getFile (int fileNo)
|
|
|
|
{
|
|
|
|
if (fileNo == 0)
|
|
|
|
return ((VolumeDirectoryHeader) headerEntries.get (0)).getDataSource ();
|
|
|
|
return fileEntries.get (fileNo - 1).getDataSource ();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AppleFileSource getCatalog ()
|
|
|
|
{
|
2016-02-20 09:48:47 +00:00
|
|
|
return new DefaultAppleFileSource ("Catalog", headerEntries.get (0).getDataSource (),
|
|
|
|
this);
|
2015-06-01 09:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString ()
|
|
|
|
{
|
|
|
|
StringBuffer text = new StringBuffer ();
|
|
|
|
String newLine = String.format ("%n");
|
|
|
|
|
|
|
|
VolumeDirectoryHeader volumeDirectory = (VolumeDirectoryHeader) headerEntries.get (0);
|
2016-02-20 09:48:47 +00:00
|
|
|
String timeC = volumeDirectory.created == null ? ""
|
|
|
|
: df.format (volumeDirectory.created.getTime ());
|
2015-06-01 09:35:51 +00:00
|
|
|
text.append ("Volume name : " + volumeDirectory.name + newLine);
|
|
|
|
text.append ("Creation date : " + timeC + newLine);
|
|
|
|
text.append ("ProDOS version : " + volumeDirectory.version + newLine);
|
|
|
|
text.append ("Min ProDOS version : " + volumeDirectory.minVersion + newLine);
|
|
|
|
text.append ("Access rights : " + volumeDirectory.access + newLine);
|
|
|
|
text.append ("Entry length : " + volumeDirectory.entryLength + newLine);
|
|
|
|
text.append ("Entries per block : " + volumeDirectory.entriesPerBlock + newLine);
|
|
|
|
text.append ("File count : " + volumeDirectory.fileCount + newLine);
|
|
|
|
text.append ("Bitmap block : " + volumeDirectory.bitMapBlock + newLine);
|
|
|
|
text.append ("Total blocks : " + volumeDirectory.totalBlocks + newLine);
|
|
|
|
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DataSource getFormattedSector (DiskAddress da)
|
|
|
|
{
|
|
|
|
if (da.getBlock () == 0)
|
|
|
|
return bootSector;
|
|
|
|
|
|
|
|
byte[] buffer = disk.readSector (da);
|
|
|
|
SectorType type = sectorTypes[da.getBlock ()];
|
|
|
|
|
|
|
|
if (type == catalogSector || type == subcatalogSector)
|
2016-07-21 05:29:08 +00:00
|
|
|
return new ProdosCatalogSector (this, disk, buffer, da);
|
2015-06-01 09:35:51 +00:00
|
|
|
if (type == volumeMapSector)
|
|
|
|
return new ProdosBitMapSector (this, disk, buffer, da);
|
|
|
|
if (type == masterIndexSector || type == indexSector)
|
2016-07-17 04:41:04 +00:00
|
|
|
return new ProdosIndexSector (getSectorFilename (da), disk, buffer, da);
|
2015-06-01 09:35:51 +00:00
|
|
|
if (type == extendedKeySector)
|
2016-07-17 04:41:04 +00:00
|
|
|
return new ProdosExtendedKeySector (disk, buffer, da);
|
2015-06-01 09:35:51 +00:00
|
|
|
if (type == dosSector)
|
2016-07-17 04:41:04 +00:00
|
|
|
return new DefaultSector ("Boot sector", disk, buffer, da);
|
2015-06-01 09:35:51 +00:00
|
|
|
|
|
|
|
String name = getSectorFilename (da);
|
|
|
|
if (name != null)
|
2016-07-17 04:41:04 +00:00
|
|
|
return new DefaultSector (name, disk, buffer, da);
|
2015-06-01 09:35:51 +00:00
|
|
|
return super.getFormattedSector (da);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<DiskAddress> getFileSectors (int fileNo)
|
|
|
|
{
|
|
|
|
if (fileNo == 0)
|
|
|
|
return ((VolumeDirectoryHeader) headerEntries.get (0)).getSectors ();
|
|
|
|
return fileEntries.get (fileNo - 1).getSectors ();
|
|
|
|
}
|
|
|
|
}
|