]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/piscsi/piscsi.c
[WIP] PiSCSI custom file system experiments
[pistorm] / platforms / amiga / piscsi / piscsi.c
index 497b6ede71dc5dd613b2ab99cf629e738328515d..2cec2f6d548669854014391fb174612e69ba100e 100644 (file)
@@ -5,16 +5,20 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <endian.h>
+#include "../hunk-reloc.h"
 #include "piscsi.h"
 #include "piscsi-enums.h"
-#include "../hunk-reloc.h"
 #include "../../../config_file/config_file.h"
 #include "../../../gpio/ps_protocol.h"
 
 #define BE(val) be32toh(val)
+#define BE16(val) be16toh(val)
 
 // Comment these lines to restore debug output:
-#define printf(...)
+#define DEBUG(...)
+//#define DEBUG printf
+#define DEBUG_TRIVIAL(...)
+//#define DEBUG_TRIVIAL printf
 #define stop_cpu_emulation(...)
 
 #ifdef FAKESTORM
@@ -25,6 +29,10 @@ extern struct emulator_config *cfg;
 extern void stop_cpu_emulation(uint8_t disasm_cur);
 
 struct piscsi_dev devs[8];
+struct piscsi_fs filesystems[NUM_FILESYSTEMS];
+
+uint8_t piscsi_num_fs = 0;
+
 uint8_t piscsi_cur_drive = 0;
 uint32_t piscsi_u32[4];
 uint32_t piscsi_dbg[8];
@@ -33,7 +41,10 @@ uint8_t *piscsi_rom_ptr;
 
 uint32_t rom_partitions[128];
 uint32_t rom_partition_prio[128];
-uint32_t rom_cur_partition = 0;
+uint32_t rom_partition_dostype[128];
+uint32_t rom_cur_partition = 0, rom_cur_fs = 0;
+
+
 
 extern unsigned char ac_piscsi_rom[];
 
@@ -58,7 +69,7 @@ void piscsi_init() {
 
     FILE *in = fopen("./platforms/amiga/piscsi/piscsi.rom", "rb");
     if (in == NULL) {
-        printf("[PISCSI] Could not open PISCSI Boot ROM file for reading.\n");
+        printf("[PISCSI] Could not open PISCSI Boot ROM file for reading!\n");
         // Zero out the boot ROM offset from the autoconfig ROM.
         ac_piscsi_rom[20] = 0;
         ac_piscsi_rom[21] = 0;
@@ -71,12 +82,9 @@ void piscsi_init() {
     fseek(in, 0, SEEK_SET);
     piscsi_rom_ptr = malloc(piscsi_rom_size);
     fread(piscsi_rom_ptr, piscsi_rom_size, 1, in);
-    fclose(in);
 
-    // Parse the hunks in the device driver to find relocation offsets
-    in = fopen("./platforms/amiga/piscsi/device_driver_amiga/pi-scsi.device", "rb");
-    fseek(in, 0x0, SEEK_SET);
-    process_hunks(in, &piscsi_hinfo, piscsi_hreloc);
+    fseek(in, PISCSI_DRIVER_OFFSET, SEEK_SET);
+    process_hunks(in, &piscsi_hinfo, piscsi_hreloc, PISCSI_DRIVER_OFFSET);
 
     fclose(in);
     printf("[PISCSI] Loaded Boot ROM.\n");
@@ -95,22 +103,28 @@ void piscsi_find_partitions(struct piscsi_dev *d) {
     }
 
     if (!d->rdb || d->rdb->rdb_PartitionList == 0) {
-        printf("[PISCSI] No partitions on disk.\n");
+        DEBUG("[PISCSI] No partitions on disk.\n");
         return;
     }
 
     char *block = malloc(512);
 
-    lseek(fd, be32toh(d->rdb->rdb_PartitionList) * 512, SEEK_SET);
+    lseek(fd, BE(d->rdb->rdb_PartitionList) * 512, SEEK_SET);
 next_partition:;
     read(fd, block, 512);
 
+    uint32_t first = be32toh(*((uint32_t *)&block[0]));
+    if (first != PART_IDENTIFIER) {
+        DEBUG("Entry at block %d is not a valid partition. Aborting.\n", BE(d->rdb->rdb_PartitionList));
+        return;
+    }
+
     struct PartitionBlock *pb = (struct PartitionBlock *)block;
     tmp = pb->pb_DriveName[0];
     pb->pb_DriveName[tmp + 1] = 0x00;
-    printf("[PISCSI] Partition %d: %s\n", cur_partition, pb->pb_DriveName + 1);
-    printf("Checksum: %.8X HostID: %d\n", BE(pb->pb_ChkSum), BE(pb->pb_HostID));
-    printf("Flags: %d (%.8X) Devflags: %d (%.8X)\n", BE(pb->pb_Flags), BE(pb->pb_Flags), BE(pb->pb_DevFlags), BE(pb->pb_DevFlags));
+    DEBUG("[PISCSI] Partition %d: %s\n", cur_partition, pb->pb_DriveName + 1);
+    DEBUG("Checksum: %.8X HostID: %d\n", BE(pb->pb_ChkSum), BE(pb->pb_HostID));
+    DEBUG("Flags: %d (%.8X) Devflags: %d (%.8X)\n", BE(pb->pb_Flags), BE(pb->pb_Flags), BE(pb->pb_DevFlags), BE(pb->pb_DevFlags));
     d->pb[cur_partition] = pb;
 
     if (d->pb[cur_partition]->pb_Next != 0xFFFFFFFF) {
@@ -118,11 +132,12 @@ next_partition:;
         block = malloc(512);
         lseek64(fd, next * 512, SEEK_SET);
         cur_partition++;
-        printf("[PISCSI] Next partition at block %d.\n", be32toh(pb->pb_Next));
+        DEBUG("[PISCSI] Next partition at block %d.\n", be32toh(pb->pb_Next));
         goto next_partition;
     }
-    printf("[PISCSI] No more partitions on disk.\n");
+    DEBUG("[PISCSI] No more partitions on disk.\n");
     d->num_partitions = cur_partition + 1;
+    d->fshd_offs = lseek64(fd, 0, SEEK_CUR);
 
     return;
 }
@@ -142,11 +157,14 @@ int piscsi_parse_rdb(struct piscsi_dev *d) {
     goto no_rdb_found;
 rdb_found:;
     struct RigidDiskBlock *rdb = (struct RigidDiskBlock *)block;
-    printf("[PISCSI] RDB found at block %d.\n", i);
+    DEBUG("[PISCSI] RDB found at block %d.\n", i);
     d->c = be32toh(rdb->rdb_Cylinders);
     d->h = be32toh(rdb->rdb_Heads);
     d->s = be32toh(rdb->rdb_Sectors);
-    printf("[PISCSI] RDB - first partition at block %d.\n", be32toh(rdb->rdb_PartitionList));
+    d->num_partitions = 0;
+    DEBUG("[PISCSI] RDB - first partition at block %d.\n", be32toh(rdb->rdb_PartitionList));
+    if (d->rdb)
+        free(d->rdb);
     d->rdb = rdb;
     sprintf(d->rdb->rdb_DriveInitName, "pi-scsi.device");
     return 0;
@@ -158,6 +176,96 @@ no_rdb_found:;
     return -1;
 }
 
+void piscsi_refresh_drives() {
+    piscsi_num_fs = 0;
+
+    for (int i = 0; i < NUM_FILESYSTEMS; i++) {
+        if (filesystems[i].binary_data) {
+            free(filesystems[i].binary_data);
+            filesystems[i].binary_data = NULL;
+        }
+        if (filesystems[i].fhb) {
+            free(filesystems[i].fhb);
+            filesystems[i].fhb = NULL;
+        }
+        filesystems[i].h_info.current_hunk = 0;
+        filesystems[i].h_info.reloc_hunks = 0;
+        filesystems[i].FS_ID = 0;
+        filesystems[i].handler = 0;
+    }
+
+    rom_cur_fs = 0;
+
+    for (int i = 0; i < NUM_UNITS; i++) {
+        if (devs[i].fd != -1) {
+            piscsi_parse_rdb(&devs[i]);
+            piscsi_find_partitions(&devs[i]);
+            piscsi_find_filesystems(&devs[i]);
+        }
+    }
+}
+
+void piscsi_find_filesystems(struct piscsi_dev *d) {
+    if (!d->num_partitions)
+        return;
+    
+    uint8_t fs_found = 0;
+    
+    uint8_t *fhb_block = malloc(512);
+
+    lseek64(d->fd, d->fshd_offs, SEEK_SET);
+
+    struct FileSysHeaderBlock *fhb = (struct FileSysHeaderBlock *)fhb_block;
+    read(d->fd, fhb_block, 512);
+    
+    while (BE(fhb->fhb_ID) == FS_IDENTIFIER) {
+        char *dosID = (char *)&fhb->fhb_DosType;
+        uint16_t *fsVer = (uint16_t *)&fhb->fhb_Version;
+
+        DEBUG("[FSHD] FSHD Block found.\n");
+        DEBUG("[FSHD] HostID: %d Next: %d Size: %d\n", BE(fhb->fhb_HostID), BE(fhb->fhb_Next), BE(fhb->fhb_SummedLongs));
+        DEBUG("[FSHD] Flags: %.8X DOSType: %c%c%c/%d\n", BE(fhb->fhb_Flags), dosID[0], dosID[1], dosID[2], dosID[3]);
+        DEBUG("[FSHD] Version: %d.%d\n", BE16(fsVer[0]), BE16(fsVer[1]));
+        DEBUG("[FSHD] Patchflags: %d Type: %d\n", BE(fhb->fhb_PatchFlags), BE(fhb->fhb_Type));
+        DEBUG("[FSHD] Task: %d Lock: %d\n", BE(fhb->fhb_Task), BE(fhb->fhb_Lock));
+        DEBUG("[FSHD] Handler: %d StackSize: %d\n", BE(fhb->fhb_Handler), BE(fhb->fhb_StackSize));
+        DEBUG("[FSHD] Prio: %d Startup: %d\n", BE(fhb->fhb_Priority), BE(fhb->fhb_Startup));
+        DEBUG("[FSHD] SegListBlocks: %d GlobalVec: %d\n", BE(fhb->fhb_Priority), BE(fhb->fhb_Startup));
+        DEBUG("[FSHD] FileSysName: %s\n", fhb->fhb_FileSysName + 1);
+
+        for (int i = 0; i < NUM_FILESYSTEMS; i++) {
+            if (filesystems[i].FS_ID == fhb->fhb_DosType) {
+                DEBUG("[FSHD] File system %c%c%c/%d already loaded. Skipping.\n", dosID[0], dosID[1], dosID[2], dosID[3]);
+                if (BE(fhb->fhb_Next) == 0xFFFFFFFF)
+                    goto fs_done;
+                
+                goto skip_fs_load_lseg;
+            }
+        }
+
+        if (load_lseg(d->fd, &filesystems[piscsi_num_fs].binary_data, &filesystems[piscsi_num_fs].h_info, filesystems[piscsi_num_fs].relocs) != -1) {
+            filesystems[piscsi_num_fs].FS_ID = fhb->fhb_DosType;
+            filesystems[piscsi_num_fs].fhb = fhb;
+            printf("[FSHD] Loaded and set up file system %d: %c%c%c/%d\n", piscsi_num_fs + 1, dosID[0], dosID[1], dosID[2], dosID[3]);
+            piscsi_num_fs++;
+        }
+
+skip_fs_load_lseg:;
+        fs_found++;
+        lseek64(d->fd, BE(fhb->fhb_Next) * 512, SEEK_SET);
+        fhb_block = malloc(512);
+        fhb = (struct FileSysHeaderBlock *)fhb_block;
+        read(d->fd, fhb_block, 512);
+    }
+
+    if (!fs_found)
+        DEBUG("[!!!FSHD] No file systems found on hard drive!\n");
+
+fs_done:;
+    if (fhb_block)
+        free(fhb_block);
+}
+
 void piscsi_map_drive(char *filename, uint8_t index) {
     if (index > 7) {
         printf("[PISCSI] Drive index %d out of range.\nUnable to map file %s to drive.\n", index, filename);
@@ -179,20 +287,20 @@ void piscsi_map_drive(char *filename, uint8_t index) {
     printf("[PISCSI] Map %d: [%s] - %llu bytes.\n", index, filename, file_size);
 
     if (piscsi_parse_rdb(d) == -1) {
-        printf("[PISCSI] No RDB found on disk, making up some CHS values.\n");
-        d->h = 64;
+        DEBUG("[PISCSI] No RDB found on disk, making up some CHS values.\n");
+        d->h = 16;
         d->s = 63;
         d->c = (file_size / 512) / (d->s * d->h);
     }
     printf("[PISCSI] CHS: %d %d %d\n", d->c, d->h, d->s);
 
     piscsi_find_partitions(d);
-    //stop_cpu_emulation(1);
+    piscsi_find_filesystems(d);
 }
 
 void piscsi_unmap_drive(uint8_t index) {
     if (devs[index].fd != -1) {
-        printf("[PISCSI] Unmapped drive %d.\n", index);
+        DEBUG("[PISCSI] Unmapped drive %d.\n", index);
         close (devs[index].fd);
         devs[index].fd = -1;
     }
@@ -233,66 +341,130 @@ char *io_cmd_name(int index) {
         case NSCMD_TD_FORMAT64: return "NSCMD_TD_FORMAT64";
 
         default:
-            return "!!!Unhandled IO command";
+            return "[!!!PISCSI] Unhandled IO command";
     }
 }
 
+#define GETSCSINAME(a) case a: return ""#a"";
+#define SCSIUNHANDLED(a) return "[!!!PISCSI] Unhandled SCSI command "#a"";
+
 char *scsi_cmd_name(int index) {
     switch(index) {
-        case 0x00: return "TEST UNIT READY";
-        case 0x12: return "INQUIRY";
-        case 0x08: return "READ (6)";
-        case 0x0A: return "WRITE (6)";
-        case 0x28: return "READ (10)";
-        case 0x2A: return "WRITE (10)";
-        case 0x25: return "READ CAPACITY";
-        case 0x1A: return "MODE SENSE";
-        case 0x37: return "READ DEFECT DATA";
+        GETSCSINAME(SCSICMD_TEST_UNIT_READY);
+        GETSCSINAME(SCSICMD_INQUIRY);
+        GETSCSINAME(SCSICMD_READ_6);
+        GETSCSINAME(SCSICMD_WRITE_6);
+        GETSCSINAME(SCSICMD_READ_10);
+        GETSCSINAME(SCSICMD_WRITE_10);
+        GETSCSINAME(SCSICMD_READ_CAPACITY_10);
+        GETSCSINAME(SCSICMD_MODE_SENSE_6);
+        GETSCSINAME(SCSICMD_READ_DEFECT_DATA_10);
         default:
-            return "!!!Unhandled SCSI command";
+            return "[!!!PISCSI] Unhandled SCSI command";
     }
 }
 
 void print_piscsi_debug_message(int index) {
+    int32_t r = 0;
+
     switch (index) {
         case DBG_INIT:
-            printf("[PISCSI] Initializing devices.\n");
+            DEBUG("[PISCSI] Initializing devices.\n");
             break;
         case DBG_OPENDEV:
-            printf("[PISCSI] Opening device %d (%d). Flags: %d (%.2X)\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2], piscsi_dbg[2]);
+            if (piscsi_dbg[0] != 255)
+                DEBUG("[PISCSI] Opening device %d (%d). Flags: %d (%.2X)\n", piscsi_dbg[0], piscsi_dbg[2], piscsi_dbg[1], piscsi_dbg[1]);
             break;
         case DBG_CLEANUP:
-            printf("[PISCSI] Cleaning up.\n");
+            DEBUG("[PISCSI] Cleaning up.\n");
             break;
         case DBG_CHS:
-            printf("[PISCSI] C/H/S: %d / %d / %d\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2]);
+            DEBUG("[PISCSI] C/H/S: %d / %d / %d\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2]);
             break;
         case DBG_BEGINIO:
-            printf("[PISCSI] BeginIO: io_Command: %d - io_Flags = %d - quick: %d\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2]);
+            DEBUG("[PISCSI] BeginIO: io_Command: %d (%s) - io_Flags = %d - quick: %d\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]), piscsi_dbg[1], piscsi_dbg[2]);
             break;
         case DBG_ABORTIO:
-            printf("[PISCSI] AbortIO!\n");
+            DEBUG("[PISCSI] AbortIO!\n");
             break;
         case DBG_SCSICMD:
-            printf("[PISCSI] SCSI Command %d (%s)\n", piscsi_dbg[1], scsi_cmd_name(piscsi_dbg[1]));
-            printf("Len: %d - %.2X %.2X %.2X - Command Length: %d\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2], piscsi_dbg[3], piscsi_dbg[4]);
+            DEBUG("[PISCSI] SCSI Command %d (%s)\n", piscsi_dbg[1], scsi_cmd_name(piscsi_dbg[1]));
+            DEBUG("Len: %d - %.2X %.2X %.2X - Command Length: %d\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2], piscsi_dbg[3], piscsi_dbg[4]);
             break;
         case DBG_SCSI_UNKNOWN_MODESENSE:
-            printf("SCSI: Unknown modesense %.4X\n", piscsi_dbg[0]);
+            DEBUG("[!!!PISCSI] SCSI: Unknown modesense %.4X\n", piscsi_dbg[0]);
             break;
         case DBG_SCSI_UNKNOWN_COMMAND:
-            printf("SCSI: Unknown command %.4X\n", piscsi_dbg[0]);
+            DEBUG("[!!!PISCSI] SCSI: Unknown command %.4X\n", piscsi_dbg[0]);
             break;
         case DBG_SCSIERR:
-            printf("SCSI: An error occured: %.4X\n", piscsi_dbg[0]);
+            DEBUG("[!!!PISCSI] SCSI: An error occured: %.4X\n", piscsi_dbg[0]);
             break;
         case DBG_IOCMD:
-            printf("[PISCSI] IO Command %d (%s)\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]));
+            DEBUG_TRIVIAL("[PISCSI] IO Command %d (%s)\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]));
             break;
         case DBG_IOCMD_UNHANDLED:
-            printf("[PISCSI] WARN: IO command %.4X (%s) is unhandled by driver.\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]));
+            DEBUG("[!!!PISCSI] WARN: IO command %.4X (%s) is unhandled by driver.\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]));
+            break;
+        case DBG_SCSI_FORMATDEVICE:
+            DEBUG("[PISCSI] Get SCSI FormatDevice MODE SENSE.\n");
+            break;
+        case DBG_SCSI_RDG:
+            DEBUG("[PISCSI] Get SCSI RDG MODE SENSE.\n");
+            break;
+        case DBG_SCSI_DEBUG_MODESENSE_6:
+            DEBUG_TRIVIAL("[PISCSI] SCSI ModeSense debug. Data: %.8X\n", piscsi_dbg[0]);
+            r = get_mapped_item_by_address(cfg, piscsi_dbg[0]);
+            if (r != -1) {
+                uint32_t addr = piscsi_dbg[0] - cfg->map_offset[r];
+                struct SCSICmd_ModeSense6 *sense = (struct SCSICmd_ModeSense6 *)(&cfg->map_data[r][addr]);
+                DEBUG_TRIVIAL("[SenseData] CMD: %.2X\n", sense->opcode);
+                DEBUG_TRIVIAL("[SenseData] DBD: %d\n", sense->reserved_dbd & 0x04);
+                DEBUG_TRIVIAL("[SenseData] PC: %d\n", (sense->pc_pagecode & 0xC0 >> 6));
+                DEBUG_TRIVIAL("[SenseData] PageCodes: %.2X %.2X\n", (sense->pc_pagecode & 0x3F), sense->subpage_code);
+                DEBUG_TRIVIAL("[SenseData] AllocLen: %d\n", sense->alloc_len);
+                DEBUG_TRIVIAL("[SenseData] Control: %.2X (%d)\n", sense->control, sense->control);
+            }
+            else {
+                DEBUG("[!!!PISCSI] ModeSense data not immediately available.\n");
+            }
+            break;
+        default:
+            DEBUG("[!!!PISCSI] No debug message available for index %d.\n", index);
+            break;
+    }
+}
+
+#define DEBUGME_SIMPLE(i, s) case i: DEBUG(s); break;
+
+void piscsi_debugme(uint32_t index) {
+    switch (index) {
+        DEBUGME_SIMPLE(1, "[PISCSI-DEBUGME] Arrived at DiagEntry.\n");
+        DEBUGME_SIMPLE(3, "[PISCSI-DEBUGME] Init: Interrupt disable.\n");
+        DEBUGME_SIMPLE(4, "[PISCSI-DEBUGME] Init: Copy/reloc driver.\n");
+        DEBUGME_SIMPLE(5, "[PISCSI-DEBUGME] Init: InitResident.\n");
+        DEBUGME_SIMPLE(7, "[PISCSI-DEBUGME] Init: Begin partition loop.\n");
+        DEBUGME_SIMPLE(8, "[PISCSI-DEBUGME] Init: Partition loop done. Cleaning up and returning to Exec.\n");
+        DEBUGME_SIMPLE(9, "[PISCSI-DEBUGME] Init: Load file systems.\n");
+        DEBUGME_SIMPLE(10, "[PISCSI-DEBUGME] Init: AllocMem for resident.\n");
+        DEBUGME_SIMPLE(11, "[PISCSI-DEBUGME] Init: Checking if resident is loaded.\n");
+        DEBUGME_SIMPLE(22, "[PISCSI-DEBUGME] Arrived at BootEntry.\n");
+        DEBUGME_SIMPLE(30, "[PISCSI-DEBUGME] LoadFileSystems: Opening FileSystem.resource.\n");
+        DEBUGME_SIMPLE(33, "[PISCSI-DEBUGME] FileSystem.resource not available, creating.\n");
+        case 31:
+            DEBUG("[PISCSI-DEBUGME] OpenResource result: %d\n", piscsi_u32[0]);
+            break;
+        case 32:
+            DEBUG("AAAAHH!\n");
+            break;
+        default:
+            DEBUG("[!!!PISCSI-DEBUGME] No debugme message for index %d!\n", index);
             break;
     }
+
+    if (index == 8) {
+        stop_cpu_emulation(1);
+    }
 }
 
 void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
@@ -307,30 +479,30 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
         case PISCSI_CMD_READ:
             d = &devs[val];
             if (d->fd == -1) {
-                printf ("[PISCSI] BUG: Attempted read from unmapped drive %d.\n", val);
+                DEBUG("[!!!PISCSI] BUG: Attempted read from unmapped drive %d.\n", val);
                 break;
             }
 
             if (cmd == PISCSI_CMD_READ) {
-                printf("[PISCSI] %d byte READ from block %d to address %.8X\n", piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
+                DEBUG("[PISCSI-%d] %d byte READ from block %d to address %.8X\n", val, piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
                 d->lba = piscsi_u32[0];
                 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
             }
             else {
                 uint64_t src = piscsi_u32[3];
                 src = (src << 32) | piscsi_u32[0];
-                printf("[PISCSI] %d byte READ64 from block %lld to address %.8X\n", piscsi_u32[1], (src / 512), piscsi_u32[2]);
+                DEBUG("[PISCSI-%d] %d byte READ64 from block %lld to address %.8X\n", val, piscsi_u32[1], (src / 512), piscsi_u32[2]);
                 d->lba = (src / 512);
                 lseek64(d->fd, src, SEEK_SET);
             }
 
             r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
             if (r != -1 && cfg->map_type[r] == MAPTYPE_RAM) {
-                printf("[PISCSI] \"DMA\" Read goes to mapped range %d.\n", r);
+                DEBUG_TRIVIAL("[PISCSI-%d] \"DMA\" Read goes to mapped range %d.\n", val, r);
                 read(d->fd, cfg->map_data[r] + piscsi_u32[2] - cfg->map_offset[r], piscsi_u32[1]);
             }
             else {
-                printf("[PISCSI] No mapped range found for read.\n");
+                DEBUG_TRIVIAL("[PISCSI-%d] No mapped range found for read.\n", val);
                 uint8_t c = 0;
                 for (uint32_t i = 0; i < piscsi_u32[1]; i++) {
                     read(d->fd, &c, 1);
@@ -342,30 +514,30 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
         case PISCSI_CMD_WRITE:
             d = &devs[val];
             if (d->fd == -1) {
-                printf ("[PISCSI] BUG: Attempted write to unmapped drive %d.\n", val);
+                DEBUG ("[PISCSI] BUG: Attempted write to unmapped drive %d.\n", val);
                 break;
             }
 
             if (cmd == PISCSI_CMD_WRITE) {
-                printf("[PISCSI] %d byte WRITE to block %d from address %.8X\n", piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
+                DEBUG("[PISCSI-%d] %d byte WRITE to block %d from address %.8X\n", val, piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
                 d->lba = piscsi_u32[0];
                 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
             }
             else {
                 uint64_t src = piscsi_u32[3];
                 src = (src << 32) | piscsi_u32[0];
-                printf("[PISCSI] %d byte WRITE64 to block %lld from address %.8X\n", piscsi_u32[1], (src / 512), piscsi_u32[2]);
+                DEBUG("[PISCSI-%d] %d byte WRITE64 to block %lld from address %.8X\n", val, piscsi_u32[1], (src / 512), piscsi_u32[2]);
                 d->lba = (src / 512);
                 lseek64(d->fd, src, SEEK_SET);
             }
 
             r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
             if (r != -1) {
-                printf("[PISCSI] \"DMA\" Write comes from mapped range %d.\n", r);
+                DEBUG_TRIVIAL("[PISCSI-%d] \"DMA\" Write comes from mapped range %d.\n", val, r);
                 write(d->fd, cfg->map_data[r] + piscsi_u32[2] - cfg->map_offset[r], piscsi_u32[1]);
             }
             else {
-                printf("[PISCSI] No mapped range found for write.\n");
+                DEBUG_TRIVIAL("[PISCSI-%d] No mapped range found for write.\n", val);
                 uint8_t c = 0;
                 for (uint32_t i = 0; i < piscsi_u32[1]; i++) {
                     c = read8(piscsi_u32[2] + i);
@@ -379,29 +551,32 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
             break;
         }
         case PISCSI_CMD_DRVNUM:
+            //printf("%d ", val);
             if (val % 10 != 0)
                 piscsi_cur_drive = 255;
             else
                 piscsi_cur_drive = val / 10;
-            printf("[PISCSI] (%s) Drive number set to %d (%d)\n", op_type_names[type], piscsi_cur_drive, val);
+            if (piscsi_cur_drive > NUM_UNITS)
+                piscsi_cur_drive = 255;
+
+            if (piscsi_cur_drive != 255)
+                DEBUG("[PISCSI] (%s) Drive number set to %d (%d)\n", op_type_names[type], piscsi_cur_drive, val);
             break;
         case PISCSI_CMD_DEBUGME:
-            printf("[PISCSI] DebugMe triggered (%d).\n", val);
-            stop_cpu_emulation(1);
+            piscsi_debugme(val);
             break;
         case PISCSI_CMD_DRIVER: {
-            printf("[PISCSI] Driver copy/patch called, destination address %.8X.\n", val);
+            DEBUG("[PISCSI] Driver copy/patch called, destination address %.8X.\n", val);
             int r = get_mapped_item_by_address(cfg, val);
             if (r != -1) {
                 uint32_t addr = val - cfg->map_offset[r];
                 uint8_t *dst_data = cfg->map_data[r];
                 uint8_t cur_partition = 0;
-                memcpy(dst_data + addr, piscsi_rom_ptr + 0x400, 0x3C00);
+                memcpy(dst_data + addr, piscsi_rom_ptr + PISCSI_DRIVER_OFFSET, 0x4000 - PISCSI_DRIVER_OFFSET);
 
                 piscsi_hinfo.base_offset = val;
                 
                 reloc_hunks(piscsi_hreloc, dst_data + addr, &piscsi_hinfo);
-                stop_cpu_emulation(1);
 
                 #define PUTNODELONG(val) *(uint32_t *)&dst_data[p_offs] = htobe32(val); p_offs += 4;
                 #define PUTNODELONGBE(val) *(uint32_t *)&dst_data[p_offs] = val; p_offs += 4;
@@ -409,6 +584,7 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
                 for (int i = 0; i < 128; i++) {
                     rom_partitions[i] = 0;
                     rom_partition_prio[i] = 0;
+                    rom_partition_dostype[i] = 0;
                 }
                 rom_cur_partition = 0;
 
@@ -416,12 +592,16 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
                 sprintf((char *)dst_data + data_addr, "pi-scsi.device");
                 uint32_t addr2 = addr + 0x4000;
                 for (int i = 0; i < NUM_UNITS; i++) {
-                    piscsi_find_partitions(&devs[i]);
+                    if (devs[i].fd != -1)
+                        piscsi_find_partitions(&devs[i]);
+                    else
+                        goto skip_disk;
+
                     if (devs[i].num_partitions) {
                         uint32_t p_offs = addr2;
-                        printf("[PISCSI] Adding %d partitions for unit %d\n", devs[i].num_partitions, i);
+                        DEBUG("[PISCSI] Adding %d partitions for unit %d\n", devs[i].num_partitions, i);
                         for (uint32_t j = 0; j < devs[i].num_partitions; j++) {
-                            printf("Partition %d: %s\n", j, devs[i].pb[j]->pb_DriveName + 1);
+                            DEBUG("Partition %d: %s\n", j, devs[i].pb[j]->pb_DriveName + 1);
                             sprintf((char *)dst_data + p_offs, "%s", devs[i].pb[j]->pb_DriveName + 1);
                             p_offs += 0x20;
                             PUTNODELONG(addr2 + cfg->map_offset[r]);
@@ -434,42 +614,86 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
                             struct pihd_dosnode_data *dat = (struct pihd_dosnode_data *)(&dst_data[addr2+0x20]);
 
                             if (BE(devs[i].pb[j]->pb_Flags) & 0x01) {
-                                printf("Partition is bootable.\n");
+                                DEBUG("Partition is bootable.\n");
                                 rom_partition_prio[cur_partition] = 0;
                                 dat->priority = 0;
                             }
                             else {
-                                printf("Partition is not bootable.\n");
+                                DEBUG("Partition is not bootable.\n");
                                 rom_partition_prio[cur_partition] = -128;
                                 dat->priority = htobe32(-128);
                             }
 
-                            printf("DOSNode Data:\n");
-                            printf("Name: %s Device: %s\n", dst_data + addr2, dst_data + data_addr);
-                            printf("Unit: %d Flags: %d Pad1: %d\n", BE(dat->unit), BE(dat->flags), BE(dat->pad1));
-                            printf("Node len: %d Block len: %d\n", BE(dat->node_len) * 4, BE(dat->block_len) * 4);
-                            printf("H: %d SPB: %d BPS: %d\n", BE(dat->surf), BE(dat->secs_per_block), BE(dat->blocks_per_track));
-                            printf("Reserved: %d Prealloc: %d\n", BE(dat->reserved_blocks), BE(dat->pad2));
-                            printf("Interleaved: %d Buffers: %d Memtype: %d\n", BE(dat->interleave), BE(dat->buffers), BE(dat->mem_type));
-                            printf("Lowcyl: %d Highcyl: %d Prio: %d\n", BE(dat->lowcyl), BE(dat->highcyl), BE(dat->priority));
-                            printf("Maxtransfer: %.8X Mask: %.8X\n", BE(dat->maxtransfer), BE(dat->transfer_mask));
-                            printf("DOSType: %.8X\n", BE(dat->dostype));
+                            DEBUG("DOSNode Data:\n");
+                            DEBUG("Name: %s Device: %s\n", dst_data + addr2, dst_data + data_addr);
+                            DEBUG("Unit: %d Flags: %d Pad1: %d\n", BE(dat->unit), BE(dat->flags), BE(dat->pad1));
+                            DEBUG("Node len: %d Block len: %d\n", BE(dat->node_len) * 4, BE(dat->block_len) * 4);
+                            DEBUG("H: %d SPB: %d BPS: %d\n", BE(dat->surf), BE(dat->secs_per_block), BE(dat->blocks_per_track));
+                            DEBUG("Reserved: %d Prealloc: %d\n", BE(dat->reserved_blocks), BE(dat->pad2));
+                            DEBUG("Interleaved: %d Buffers: %d Memtype: %d\n", BE(dat->interleave), BE(dat->buffers), BE(dat->mem_type));
+                            DEBUG("Lowcyl: %d Highcyl: %d Prio: %d\n", BE(dat->lowcyl), BE(dat->highcyl), BE(dat->priority));
+                            DEBUG("Maxtransfer: %.8X Mask: %.8X\n", BE(dat->maxtransfer), BE(dat->transfer_mask));
+                            DEBUG("DOSType: %.8X\n", BE(dat->dostype));
 
                             rom_partitions[cur_partition] = addr2 + 0x20 + cfg->map_offset[r];
+                            rom_partition_dostype[cur_partition] = dat->dostype;
                             cur_partition++;
                             addr2 += 0x100;
                             p_offs = addr2;
                         }
                     }
+skip_disk:;
                 }
             }
            
             break;
         }
         case PISCSI_CMD_NEXTPART:
-            printf("[PISCSI] Switch partition %d -> %d\n", rom_cur_partition, rom_cur_partition + 1);
+            DEBUG("[PISCSI] Switch partition %d -> %d\n", rom_cur_partition, rom_cur_partition + 1);
             rom_cur_partition++;
             break;
+        case PISCSI_CMD_NEXTFS:
+            DEBUG("[PISCSI] Switch file file system %d -> %d\n", rom_cur_fs, rom_cur_fs + 1);
+            rom_cur_fs++;
+            break;
+        case PISCSI_CMD_COPYFS:
+            DEBUG("[PISCSI] Copy file system %d to %.8X and reloc.\n", rom_cur_fs, piscsi_u32[2]);
+            r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
+            if (r != -1) {
+                uint32_t addr = piscsi_u32[2] - cfg->map_offset[r];
+                memset(cfg->map_data[r] + addr, 0x00, filesystems[rom_cur_fs].h_info.alloc_size);
+                memcpy(cfg->map_data[r] + addr, filesystems[rom_cur_fs].binary_data, filesystems[rom_cur_fs].h_info.byte_size);
+                filesystems[rom_cur_fs].h_info.base_offset = piscsi_u32[2];
+                reloc_hunks(filesystems[rom_cur_fs].relocs, cfg->map_data[r] + addr, &filesystems[rom_cur_fs].h_info);
+                filesystems[rom_cur_fs].handler = piscsi_u32[2];
+            }
+            break;
+        case PISCSI_CMD_SETFSH: {
+            int i = 0;
+            DEBUG("[PISCSI] Set handler for partition %d (DeviceNode: %.8X)\n", rom_cur_partition, val);
+            r = get_mapped_item_by_address(cfg, val);
+            if (r != -1) {
+                uint32_t addr = val - cfg->map_offset[r];
+                struct DeviceNode *node = (struct DeviceNode *)(cfg->map_data[r] + addr);
+                char *dosID = (char *)&rom_partition_dostype[rom_cur_partition];
+                DEBUG("[PISCSI] Partition DOSType is %c%c%c/%d\n", dosID[0], dosID[1], dosID[2], dosID[3]);
+                for (i = 0; i < piscsi_num_fs; i++) {
+                    if (rom_partition_dostype[rom_cur_partition] == filesystems[i].FS_ID) {
+                        node->dn_SegList = htobe32(filesystems[i].handler);
+                        goto fs_found;
+                    }
+                }
+                DEBUG("[!!!PISCSI] Found no handler for file system!\n");
+fs_found:;
+                DEBUG("[FS-HANDLER] Next: %d Type: %d\n", BE(node->dn_Next), BE(node->dn_Type));
+                DEBUG("[FS-HANDLER] Task: %d Lock: %d\n", BE(node->dn_Task), BE(node->dn_Lock));
+                DEBUG("[FS-HANDLER] Handler: %d Stacksize: %d\n", BE((uint32_t)node->dn_Handler), BE(node->dn_StackSize));
+                DEBUG("[FS-HANDLER] Priority: %d Startup: %d\n", BE((uint32_t)node->dn_Priority), BE(node->dn_Startup));
+                DEBUG("[FS-HANDLER] SegList: %d GlobalVec: %d\n", BE((uint32_t)node->dn_SegList), BE(node->dn_GlobalVec));
+                DEBUG("[PISCSI] Handler for partition %.8X set to %.8X (%.8X).\n", BE((uint32_t)node->dn_Name), filesystems[i].FS_ID, filesystems[i].handler);
+            }
+            break;
+        }
         case PISCSI_DBG_VAL1: case PISCSI_DBG_VAL2: case PISCSI_DBG_VAL3: case PISCSI_DBG_VAL4:
         case PISCSI_DBG_VAL5: case PISCSI_DBG_VAL6: case PISCSI_DBG_VAL7: case PISCSI_DBG_VAL8: {
             int i = ((addr & 0xFFFF) - PISCSI_DBG_VAL1) / 4;
@@ -480,7 +704,7 @@ void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
             print_piscsi_debug_message(val);
             break;
         default:
-            printf("[PISCSI] WARN: Unhandled %s register write to %.8X: %d\n", op_type_names[type], addr, val);
+            DEBUG("[!!!PISCSI] WARN: Unhandled %s register write to %.8X: %d\n", op_type_names[type], addr, val);
             break;
     }
 }
@@ -493,20 +717,20 @@ uint32_t handle_piscsi_read(uint32_t addr, uint8_t type) {
     if ((addr & 0xFFFF) >= PISCSI_CMD_ROM) {
         uint32_t romoffs = (addr & 0xFFFF) - PISCSI_CMD_ROM;
         if (romoffs < (piscsi_rom_size + PIB)) {
-            //printf("[PISCSI] %s read from Boot ROM @$%.4X (%.8X): ", op_type_names[type], romoffs, addr);
+            //DEBUG("[PISCSI] %s read from Boot ROM @$%.4X (%.8X): ", op_type_names[type], romoffs, addr);
             uint32_t v = 0;
             switch (type) {
                 case OP_TYPE_BYTE:
                     v = piscsi_rom_ptr[romoffs - PIB];
-                    //printf("%.2X\n", v);
+                    //DEBUG("%.2X\n", v);
                     break;
                 case OP_TYPE_WORD:
                     v = be16toh(*((uint16_t *)&piscsi_rom_ptr[romoffs - PIB]));
-                    //printf("%.4X\n", v);
+                    //DEBUG("%.4X\n", v);
                     break;
                 case OP_TYPE_LONGWORD:
                     v = be32toh(*((uint32_t *)&piscsi_rom_ptr[romoffs - PIB]));
-                    //printf("%.8X\n", v);
+                    //DEBUG("%.8X\n", v);
                     break;
             }
             return v;
@@ -522,45 +746,51 @@ uint32_t handle_piscsi_read(uint32_t addr, uint8_t type) {
         }
         case PISCSI_CMD_DRVTYPE:
             if (devs[piscsi_cur_drive].fd == -1) {
-                printf("[PISCSI] %s Read from DRVTYPE %d, drive not attached.\n", op_type_names[type], piscsi_cur_drive);
+                DEBUG("[PISCSI] %s Read from DRVTYPE %d, drive not attached.\n", op_type_names[type], piscsi_cur_drive);
                 return 0;
             }
-            printf("[PISCSI] %s Read from DRVTYPE %d, drive attached.\n", op_type_names[type], piscsi_cur_drive);
+            DEBUG("[PISCSI] %s Read from DRVTYPE %d, drive attached.\n", op_type_names[type], piscsi_cur_drive);
             return 1;
             break;
         case PISCSI_CMD_DRVNUM:
             return piscsi_cur_drive;
             break;
         case PISCSI_CMD_CYLS:
-            printf("[PISCSI] %s Read from CYLS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].c);
+            DEBUG("[PISCSI] %s Read from CYLS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].c);
             return devs[piscsi_cur_drive].c;
             break;
         case PISCSI_CMD_HEADS:
-            printf("[PISCSI] %s Read from HEADS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].h);
+            DEBUG("[PISCSI] %s Read from HEADS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].h);
             return devs[piscsi_cur_drive].h;
             break;
         case PISCSI_CMD_SECS:
-            printf("[PISCSI] %s Read from SECS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].s);
+            DEBUG("[PISCSI] %s Read from SECS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].s);
             return devs[piscsi_cur_drive].s;
             break;
         case PISCSI_CMD_BLOCKS: {
             uint32_t blox = devs[piscsi_cur_drive].fs / 512;
-            printf("[PISCSI] %s Read from BLOCKS %d: %d\n", op_type_names[type], piscsi_cur_drive, (uint32_t)(devs[piscsi_cur_drive].fs / 512));
-            printf("fs: %lld (%d)\n", devs[piscsi_cur_drive].fs, blox);
+            DEBUG("[PISCSI] %s Read from BLOCKS %d: %d\n", op_type_names[type], piscsi_cur_drive, (uint32_t)(devs[piscsi_cur_drive].fs / 512));
+            DEBUG("fs: %lld (%d)\n", devs[piscsi_cur_drive].fs, blox);
             return blox;
             break;
         }
         case PISCSI_CMD_GETPART: {
-            printf("[PISCSI] Get ROM partition %d offset: %.8X\n", rom_cur_partition, rom_partitions[rom_cur_partition]);
+            DEBUG("[PISCSI] Get ROM partition %d offset: %.8X\n", rom_cur_partition, rom_partitions[rom_cur_partition]);
             return rom_partitions[rom_cur_partition];
             break;
         }
         case PISCSI_CMD_GETPRIO:
-            printf("[PISCSI] Get partition %d boot priority: %d\n", rom_cur_partition, rom_partition_prio[rom_cur_partition]);
+            DEBUG("[PISCSI] Get partition %d boot priority: %d\n", rom_cur_partition, rom_partition_prio[rom_cur_partition]);
             return rom_partition_prio[rom_cur_partition];
             break;
+        case PISCSI_CMD_CHECKFS:
+            DEBUG("[PISCSI] Get current loaded file system: %.8X\n", filesystems[rom_cur_fs].FS_ID);
+            return filesystems[rom_cur_fs].FS_ID;
+        case PISCSI_CMD_FSSIZE:
+            DEBUG("[PISCSI] Get alloc size of loaded file system: %d\n", filesystems[rom_cur_fs].h_info.alloc_size);
+            return filesystems[rom_cur_fs].h_info.alloc_size;
         default:
-            printf("[PISCSI] Unhandled %s register read from %.8X\n", op_type_names[type], addr);
+            DEBUG("[!!!PISCSI] WARN: Unhandled %s register read from %.8X\n", op_type_names[type], addr);
             break;
     }