8 #include "../hunk-reloc.h"
10 #include "piscsi-enums.h"
11 #include "../../../config_file/config_file.h"
12 #include "../../../gpio/ps_protocol.h"
14 #define BE(val) be32toh(val)
15 #define BE16(val) be16toh(val)
17 // Comment these lines to restore debug output:
19 //#define DEBUG printf
20 #define DEBUG_TRIVIAL(...)
21 //#define DEBUG_TRIVIAL printf
22 #define stop_cpu_emulation(...)
28 extern struct emulator_config *cfg;
29 extern void stop_cpu_emulation(uint8_t disasm_cur);
31 struct piscsi_dev devs[8];
32 struct piscsi_fs filesystems[NUM_FILESYSTEMS];
34 uint8_t piscsi_num_fs = 0;
36 uint8_t piscsi_cur_drive = 0;
37 uint32_t piscsi_u32[4];
38 uint32_t piscsi_dbg[8];
39 uint32_t piscsi_rom_size = 0;
40 uint8_t *piscsi_rom_ptr;
42 uint32_t rom_partitions[128];
43 uint32_t rom_partition_prio[128];
44 uint32_t rom_partition_dostype[128];
45 uint32_t rom_cur_partition = 0, rom_cur_fs = 0;
49 extern unsigned char ac_piscsi_rom[];
51 static const char *op_type_names[4] = {
58 //static const char *partition_marker = "PART";
60 struct hunk_info piscsi_hinfo;
61 struct hunk_reloc piscsi_hreloc[256];
64 for (int i = 0; i < 8; i++) {
67 devs[i].c = devs[i].h = devs[i].s = 0;
70 FILE *in = fopen("./platforms/amiga/piscsi/piscsi.rom", "rb");
72 printf("[PISCSI] Could not open PISCSI Boot ROM file for reading!\n");
73 // Zero out the boot ROM offset from the autoconfig ROM.
74 ac_piscsi_rom[20] = 0;
75 ac_piscsi_rom[21] = 0;
76 ac_piscsi_rom[22] = 0;
77 ac_piscsi_rom[23] = 0;
80 fseek(in, 0, SEEK_END);
81 piscsi_rom_size = ftell(in);
82 fseek(in, 0, SEEK_SET);
83 piscsi_rom_ptr = malloc(piscsi_rom_size);
84 fread(piscsi_rom_ptr, piscsi_rom_size, 1, in);
86 fseek(in, PISCSI_DRIVER_OFFSET, SEEK_SET);
87 process_hunks(in, &piscsi_hinfo, piscsi_hreloc, PISCSI_DRIVER_OFFSET);
90 printf("[PISCSI] Loaded Boot ROM.\n");
93 void piscsi_find_partitions(struct piscsi_dev *d) {
95 int cur_partition = 0;
98 for (int i = 0; i < 16; i++) {
105 if (!d->rdb || d->rdb->rdb_PartitionList == 0) {
106 DEBUG("[PISCSI] No partitions on disk.\n");
110 char *block = malloc(512);
112 lseek(fd, BE(d->rdb->rdb_PartitionList) * 512, SEEK_SET);
114 read(fd, block, 512);
116 uint32_t first = be32toh(*((uint32_t *)&block[0]));
117 if (first != PART_IDENTIFIER) {
118 DEBUG("Entry at block %d is not a valid partition. Aborting.\n", BE(d->rdb->rdb_PartitionList));
122 struct PartitionBlock *pb = (struct PartitionBlock *)block;
123 tmp = pb->pb_DriveName[0];
124 pb->pb_DriveName[tmp + 1] = 0x00;
125 DEBUG("[PISCSI] Partition %d: %s\n", cur_partition, pb->pb_DriveName + 1);
126 DEBUG("Checksum: %.8X HostID: %d\n", BE(pb->pb_ChkSum), BE(pb->pb_HostID));
127 DEBUG("Flags: %d (%.8X) Devflags: %d (%.8X)\n", BE(pb->pb_Flags), BE(pb->pb_Flags), BE(pb->pb_DevFlags), BE(pb->pb_DevFlags));
128 d->pb[cur_partition] = pb;
130 if (d->pb[cur_partition]->pb_Next != 0xFFFFFFFF) {
131 uint64_t next = be32toh(pb->pb_Next);
133 lseek64(fd, next * 512, SEEK_SET);
135 DEBUG("[PISCSI] Next partition at block %d.\n", be32toh(pb->pb_Next));
138 DEBUG("[PISCSI] No more partitions on disk.\n");
139 d->num_partitions = cur_partition + 1;
140 d->fshd_offs = lseek64(fd, 0, SEEK_CUR);
145 int piscsi_parse_rdb(struct piscsi_dev *d) {
148 uint8_t *block = malloc(512);
150 lseek(fd, 0, SEEK_SET);
151 for (i = 0; i < RDB_BLOCK_LIMIT; i++) {
152 read(fd, block, 512);
153 uint32_t first = be32toh(*((uint32_t *)&block[0]));
154 if (first == RDB_IDENTIFIER)
159 struct RigidDiskBlock *rdb = (struct RigidDiskBlock *)block;
160 DEBUG("[PISCSI] RDB found at block %d.\n", i);
161 d->c = be32toh(rdb->rdb_Cylinders);
162 d->h = be32toh(rdb->rdb_Heads);
163 d->s = be32toh(rdb->rdb_Sectors);
164 d->num_partitions = 0;
165 DEBUG("[PISCSI] RDB - first partition at block %d.\n", be32toh(rdb->rdb_PartitionList));
169 sprintf(d->rdb->rdb_DriveInitName, "pi-scsi.device");
179 void piscsi_refresh_drives() {
182 for (int i = 0; i < NUM_FILESYSTEMS; i++) {
183 if (filesystems[i].binary_data) {
184 free(filesystems[i].binary_data);
185 filesystems[i].binary_data = NULL;
187 if (filesystems[i].fhb) {
188 free(filesystems[i].fhb);
189 filesystems[i].fhb = NULL;
191 filesystems[i].h_info.current_hunk = 0;
192 filesystems[i].h_info.reloc_hunks = 0;
193 filesystems[i].FS_ID = 0;
194 filesystems[i].handler = 0;
199 for (int i = 0; i < NUM_UNITS; i++) {
200 if (devs[i].fd != -1) {
201 piscsi_parse_rdb(&devs[i]);
202 piscsi_find_partitions(&devs[i]);
203 piscsi_find_filesystems(&devs[i]);
208 void piscsi_find_filesystems(struct piscsi_dev *d) {
209 if (!d->num_partitions)
212 uint8_t fs_found = 0;
214 uint8_t *fhb_block = malloc(512);
216 lseek64(d->fd, d->fshd_offs, SEEK_SET);
218 struct FileSysHeaderBlock *fhb = (struct FileSysHeaderBlock *)fhb_block;
219 read(d->fd, fhb_block, 512);
221 while (BE(fhb->fhb_ID) == FS_IDENTIFIER) {
222 char *dosID = (char *)&fhb->fhb_DosType;
223 uint16_t *fsVer = (uint16_t *)&fhb->fhb_Version;
225 DEBUG("[FSHD] FSHD Block found.\n");
226 DEBUG("[FSHD] HostID: %d Next: %d Size: %d\n", BE(fhb->fhb_HostID), BE(fhb->fhb_Next), BE(fhb->fhb_SummedLongs));
227 DEBUG("[FSHD] Flags: %.8X DOSType: %c%c%c/%d\n", BE(fhb->fhb_Flags), dosID[0], dosID[1], dosID[2], dosID[3]);
228 DEBUG("[FSHD] Version: %d.%d\n", BE16(fsVer[0]), BE16(fsVer[1]));
229 DEBUG("[FSHD] Patchflags: %d Type: %d\n", BE(fhb->fhb_PatchFlags), BE(fhb->fhb_Type));
230 DEBUG("[FSHD] Task: %d Lock: %d\n", BE(fhb->fhb_Task), BE(fhb->fhb_Lock));
231 DEBUG("[FSHD] Handler: %d StackSize: %d\n", BE(fhb->fhb_Handler), BE(fhb->fhb_StackSize));
232 DEBUG("[FSHD] Prio: %d Startup: %d\n", BE(fhb->fhb_Priority), BE(fhb->fhb_Startup));
233 DEBUG("[FSHD] SegListBlocks: %d GlobalVec: %d\n", BE(fhb->fhb_Priority), BE(fhb->fhb_Startup));
234 DEBUG("[FSHD] FileSysName: %s\n", fhb->fhb_FileSysName + 1);
236 for (int i = 0; i < NUM_FILESYSTEMS; i++) {
237 if (filesystems[i].FS_ID == fhb->fhb_DosType) {
238 DEBUG("[FSHD] File system %c%c%c/%d already loaded. Skipping.\n", dosID[0], dosID[1], dosID[2], dosID[3]);
239 if (BE(fhb->fhb_Next) == 0xFFFFFFFF)
242 goto skip_fs_load_lseg;
246 if (load_lseg(d->fd, &filesystems[piscsi_num_fs].binary_data, &filesystems[piscsi_num_fs].h_info, filesystems[piscsi_num_fs].relocs) != -1) {
247 filesystems[piscsi_num_fs].FS_ID = fhb->fhb_DosType;
248 filesystems[piscsi_num_fs].fhb = fhb;
249 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]);
255 lseek64(d->fd, BE(fhb->fhb_Next) * 512, SEEK_SET);
256 fhb_block = malloc(512);
257 fhb = (struct FileSysHeaderBlock *)fhb_block;
258 read(d->fd, fhb_block, 512);
262 DEBUG("[!!!FSHD] No file systems found on hard drive!\n");
270 void piscsi_map_drive(char *filename, uint8_t index) {
272 printf("[PISCSI] Drive index %d out of range.\nUnable to map file %s to drive.\n", index, filename);
276 int32_t tmp_fd = open(filename, O_RDWR);
278 printf("[PISCSI] Failed to open file %s, could not map drive %d.\n", filename, index);
282 struct piscsi_dev *d = &devs[index];
284 uint64_t file_size = lseek(tmp_fd, 0, SEEK_END);
287 lseek(tmp_fd, 0, SEEK_SET);
288 printf("[PISCSI] Map %d: [%s] - %llu bytes.\n", index, filename, file_size);
290 if (piscsi_parse_rdb(d) == -1) {
291 DEBUG("[PISCSI] No RDB found on disk, making up some CHS values.\n");
294 d->c = (file_size / 512) / (d->s * d->h);
296 printf("[PISCSI] CHS: %d %d %d\n", d->c, d->h, d->s);
298 piscsi_find_partitions(d);
299 piscsi_find_filesystems(d);
302 void piscsi_unmap_drive(uint8_t index) {
303 if (devs[index].fd != -1) {
304 DEBUG("[PISCSI] Unmapped drive %d.\n", index);
305 close (devs[index].fd);
310 char *io_cmd_name(int index) {
312 case CMD_INVALID: return "INVALID";
313 case CMD_RESET: return "RESET";
314 case CMD_READ: return "READ";
315 case CMD_WRITE: return "WRITE";
316 case CMD_UPDATE: return "UPDATE";
317 case CMD_CLEAR: return "CLEAR";
318 case CMD_STOP: return "STOP";
319 case CMD_START: return "START";
320 case CMD_FLUSH: return "FLUSH";
321 case TD_MOTOR: return "TD_MOTOR";
322 case TD_SEEK: return "SEEK";
323 case TD_FORMAT: return "FORMAT";
324 case TD_REMOVE: return "REMOVE";
325 case TD_CHANGENUM: return "CHANGENUM";
326 case TD_CHANGESTATE: return "CHANGESTATE";
327 case TD_PROTSTATUS: return "PROTSTATUS";
328 case TD_RAWREAD: return "RAWREAD";
329 case TD_RAWWRITE: return "RAWWRITE";
330 case TD_GETDRIVETYPE: return "GETDRIVETYPE";
331 case TD_GETNUMTRACKS: return "GETNUMTRACKS";
332 case TD_ADDCHANGEINT: return "ADDCHANGEINT";
333 case TD_REMCHANGEINT: return "REMCHANGEINT";
334 case TD_GETGEOMETRY: return "GETGEOMETRY";
335 case TD_EJECT: return "EJECT";
336 case TD_LASTCOMM: return "LASTCOMM/READ64";
337 case TD_WRITE64: return "WRITE64";
338 case HD_SCSICMD: return "HD_SCSICMD";
339 case NSCMD_DEVICEQUERY: return "NSCMD_DEVICEQUERY";
340 case NSCMD_TD_READ64: return "NSCMD_TD_READ64";
341 case NSCMD_TD_WRITE64: return "NSCMD_TD_WRITE64";
342 case NSCMD_TD_FORMAT64: return "NSCMD_TD_FORMAT64";
345 return "[!!!PISCSI] Unhandled IO command";
349 #define GETSCSINAME(a) case a: return ""#a"";
350 #define SCSIUNHANDLED(a) return "[!!!PISCSI] Unhandled SCSI command "#a"";
352 char *scsi_cmd_name(int index) {
354 GETSCSINAME(SCSICMD_TEST_UNIT_READY);
355 GETSCSINAME(SCSICMD_INQUIRY);
356 GETSCSINAME(SCSICMD_READ_6);
357 GETSCSINAME(SCSICMD_WRITE_6);
358 GETSCSINAME(SCSICMD_READ_10);
359 GETSCSINAME(SCSICMD_WRITE_10);
360 GETSCSINAME(SCSICMD_READ_CAPACITY_10);
361 GETSCSINAME(SCSICMD_MODE_SENSE_6);
362 GETSCSINAME(SCSICMD_READ_DEFECT_DATA_10);
364 return "[!!!PISCSI] Unhandled SCSI command";
368 void print_piscsi_debug_message(int index) {
373 DEBUG("[PISCSI] Initializing devices.\n");
376 if (piscsi_dbg[0] != 255) {
377 DEBUG("[PISCSI] Opening device %d (%d). Flags: %d (%.2X)\n", piscsi_dbg[0], piscsi_dbg[2], piscsi_dbg[1], piscsi_dbg[1]);
381 DEBUG("[PISCSI] Cleaning up.\n");
384 DEBUG("[PISCSI] C/H/S: %d / %d / %d\n", piscsi_dbg[0], piscsi_dbg[1], piscsi_dbg[2]);
387 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]);
390 DEBUG("[PISCSI] AbortIO!\n");
393 DEBUG("[PISCSI] SCSI Command %d (%s)\n", piscsi_dbg[1], scsi_cmd_name(piscsi_dbg[1]));
394 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]);
396 case DBG_SCSI_UNKNOWN_MODESENSE:
397 DEBUG("[!!!PISCSI] SCSI: Unknown modesense %.4X\n", piscsi_dbg[0]);
399 case DBG_SCSI_UNKNOWN_COMMAND:
400 DEBUG("[!!!PISCSI] SCSI: Unknown command %.4X\n", piscsi_dbg[0]);
403 DEBUG("[!!!PISCSI] SCSI: An error occured: %.4X\n", piscsi_dbg[0]);
406 DEBUG_TRIVIAL("[PISCSI] IO Command %d (%s)\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]));
408 case DBG_IOCMD_UNHANDLED:
409 DEBUG("[!!!PISCSI] WARN: IO command %.4X (%s) is unhandled by driver.\n", piscsi_dbg[0], io_cmd_name(piscsi_dbg[0]));
411 case DBG_SCSI_FORMATDEVICE:
412 DEBUG("[PISCSI] Get SCSI FormatDevice MODE SENSE.\n");
415 DEBUG("[PISCSI] Get SCSI RDG MODE SENSE.\n");
417 case DBG_SCSI_DEBUG_MODESENSE_6:
418 DEBUG_TRIVIAL("[PISCSI] SCSI ModeSense debug. Data: %.8X\n", piscsi_dbg[0]);
419 r = get_mapped_item_by_address(cfg, piscsi_dbg[0]);
421 uint32_t addr = piscsi_dbg[0] - cfg->map_offset[r];
422 struct SCSICmd_ModeSense6 *sense = (struct SCSICmd_ModeSense6 *)(&cfg->map_data[r][addr]);
423 DEBUG_TRIVIAL("[SenseData] CMD: %.2X\n", sense->opcode);
424 DEBUG_TRIVIAL("[SenseData] DBD: %d\n", sense->reserved_dbd & 0x04);
425 DEBUG_TRIVIAL("[SenseData] PC: %d\n", (sense->pc_pagecode & 0xC0 >> 6));
426 DEBUG_TRIVIAL("[SenseData] PageCodes: %.2X %.2X\n", (sense->pc_pagecode & 0x3F), sense->subpage_code);
427 DEBUG_TRIVIAL("[SenseData] AllocLen: %d\n", sense->alloc_len);
428 DEBUG_TRIVIAL("[SenseData] Control: %.2X (%d)\n", sense->control, sense->control);
431 DEBUG("[!!!PISCSI] ModeSense data not immediately available.\n");
435 DEBUG("[!!!PISCSI] No debug message available for index %d.\n", index);
440 #define DEBUGME_SIMPLE(i, s) case i: DEBUG(s); break;
442 void piscsi_debugme(uint32_t index) {
444 DEBUGME_SIMPLE(1, "[PISCSI-DEBUGME] Arrived at DiagEntry.\n");
445 DEBUGME_SIMPLE(3, "[PISCSI-DEBUGME] Init: Interrupt disable.\n");
446 DEBUGME_SIMPLE(4, "[PISCSI-DEBUGME] Init: Copy/reloc driver.\n");
447 DEBUGME_SIMPLE(5, "[PISCSI-DEBUGME] Init: InitResident.\n");
448 DEBUGME_SIMPLE(7, "[PISCSI-DEBUGME] Init: Begin partition loop.\n");
449 DEBUGME_SIMPLE(8, "[PISCSI-DEBUGME] Init: Partition loop done. Cleaning up and returning to Exec.\n");
450 DEBUGME_SIMPLE(9, "[PISCSI-DEBUGME] Init: Load file systems.\n");
451 DEBUGME_SIMPLE(10, "[PISCSI-DEBUGME] Init: AllocMem for resident.\n");
452 DEBUGME_SIMPLE(11, "[PISCSI-DEBUGME] Init: Checking if resident is loaded.\n");
453 DEBUGME_SIMPLE(22, "[PISCSI-DEBUGME] Arrived at BootEntry.\n");
454 DEBUGME_SIMPLE(30, "[PISCSI-DEBUGME] LoadFileSystems: Opening FileSystem.resource.\n");
455 DEBUGME_SIMPLE(33, "[PISCSI-DEBUGME] FileSystem.resource not available, creating.\n");
457 DEBUG("[PISCSI-DEBUGME] OpenResource result: %d\n", piscsi_u32[0]);
463 DEBUG("[!!!PISCSI-DEBUGME] No debugme message for index %d!\n", index);
468 stop_cpu_emulation(1);
472 void handle_piscsi_write(uint32_t addr, uint32_t val, uint8_t type) {
475 struct piscsi_dev *d = &devs[piscsi_cur_drive];
477 uint16_t cmd = (addr & 0xFFFF);
480 case PISCSI_CMD_READ64:
481 case PISCSI_CMD_READ:
484 DEBUG("[!!!PISCSI] BUG: Attempted read from unmapped drive %d.\n", val);
488 if (cmd == PISCSI_CMD_READ) {
489 DEBUG("[PISCSI-%d] %d byte READ from block %d to address %.8X\n", val, piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
490 d->lba = piscsi_u32[0];
491 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
494 uint64_t src = piscsi_u32[3];
495 src = (src << 32) | piscsi_u32[0];
496 DEBUG("[PISCSI-%d] %d byte READ64 from block %lld to address %.8X\n", val, piscsi_u32[1], (src / 512), piscsi_u32[2]);
497 d->lba = (src / 512);
498 lseek64(d->fd, src, SEEK_SET);
501 r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
502 if (r != -1 && cfg->map_type[r] == MAPTYPE_RAM) {
503 DEBUG_TRIVIAL("[PISCSI-%d] \"DMA\" Read goes to mapped range %d.\n", val, r);
504 read(d->fd, cfg->map_data[r] + piscsi_u32[2] - cfg->map_offset[r], piscsi_u32[1]);
507 DEBUG_TRIVIAL("[PISCSI-%d] No mapped range found for read.\n", val);
509 for (uint32_t i = 0; i < piscsi_u32[1]; i++) {
511 write8(piscsi_u32[2] + i, (uint32_t)c);
515 case PISCSI_CMD_WRITE64:
516 case PISCSI_CMD_WRITE:
519 DEBUG ("[PISCSI] BUG: Attempted write to unmapped drive %d.\n", val);
523 if (cmd == PISCSI_CMD_WRITE) {
524 DEBUG("[PISCSI-%d] %d byte WRITE to block %d from address %.8X\n", val, piscsi_u32[1], piscsi_u32[0], piscsi_u32[2]);
525 d->lba = piscsi_u32[0];
526 lseek(d->fd, (piscsi_u32[0] * 512), SEEK_SET);
529 uint64_t src = piscsi_u32[3];
530 src = (src << 32) | piscsi_u32[0];
531 DEBUG("[PISCSI-%d] %d byte WRITE64 to block %lld from address %.8X\n", val, piscsi_u32[1], (src / 512), piscsi_u32[2]);
532 d->lba = (src / 512);
533 lseek64(d->fd, src, SEEK_SET);
536 r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
538 DEBUG_TRIVIAL("[PISCSI-%d] \"DMA\" Write comes from mapped range %d.\n", val, r);
539 write(d->fd, cfg->map_data[r] + piscsi_u32[2] - cfg->map_offset[r], piscsi_u32[1]);
542 DEBUG_TRIVIAL("[PISCSI-%d] No mapped range found for write.\n", val);
544 for (uint32_t i = 0; i < piscsi_u32[1]; i++) {
545 c = read8(piscsi_u32[2] + i);
550 case PISCSI_CMD_ADDR1: case PISCSI_CMD_ADDR2: case PISCSI_CMD_ADDR3: case PISCSI_CMD_ADDR4: {
551 int i = ((addr & 0xFFFF) - PISCSI_CMD_ADDR1) / 4;
555 case PISCSI_CMD_DRVNUM:
556 //printf("%d ", val);
558 piscsi_cur_drive = 255;
560 piscsi_cur_drive = val / 10;
561 if (piscsi_cur_drive > NUM_UNITS)
562 piscsi_cur_drive = 255;
564 if (piscsi_cur_drive != 255) {
565 DEBUG("[PISCSI] (%s) Drive number set to %d (%d)\n", op_type_names[type], piscsi_cur_drive, val);
568 case PISCSI_CMD_DEBUGME:
571 case PISCSI_CMD_DRIVER: {
572 DEBUG("[PISCSI] Driver copy/patch called, destination address %.8X.\n", val);
573 int r = get_mapped_item_by_address(cfg, val);
575 uint32_t addr = val - cfg->map_offset[r];
576 uint8_t *dst_data = cfg->map_data[r];
577 uint8_t cur_partition = 0;
578 memcpy(dst_data + addr, piscsi_rom_ptr + PISCSI_DRIVER_OFFSET, 0x4000 - PISCSI_DRIVER_OFFSET);
580 piscsi_hinfo.base_offset = val;
582 reloc_hunks(piscsi_hreloc, dst_data + addr, &piscsi_hinfo);
584 #define PUTNODELONG(val) *(uint32_t *)&dst_data[p_offs] = htobe32(val); p_offs += 4;
585 #define PUTNODELONGBE(val) *(uint32_t *)&dst_data[p_offs] = val; p_offs += 4;
587 for (int i = 0; i < 128; i++) {
588 rom_partitions[i] = 0;
589 rom_partition_prio[i] = 0;
590 rom_partition_dostype[i] = 0;
592 rom_cur_partition = 0;
594 uint32_t data_addr = addr + 0x3F00;
595 sprintf((char *)dst_data + data_addr, "pi-scsi.device");
596 uint32_t addr2 = addr + 0x4000;
597 for (int i = 0; i < NUM_UNITS; i++) {
598 if (devs[i].fd != -1)
599 piscsi_find_partitions(&devs[i]);
603 if (devs[i].num_partitions) {
604 uint32_t p_offs = addr2;
605 DEBUG("[PISCSI] Adding %d partitions for unit %d\n", devs[i].num_partitions, i);
606 for (uint32_t j = 0; j < devs[i].num_partitions; j++) {
607 DEBUG("Partition %d: %s\n", j, devs[i].pb[j]->pb_DriveName + 1);
608 sprintf((char *)dst_data + p_offs, "%s", devs[i].pb[j]->pb_DriveName + 1);
610 PUTNODELONG(addr2 + cfg->map_offset[r]);
611 PUTNODELONG(data_addr + cfg->map_offset[r]);
612 PUTNODELONG((i * 10));
614 uint32_t nodesize = (be32toh(devs[i].pb[j]->pb_Environment[0]) + 1) * 4;
615 memcpy(dst_data + p_offs, devs[i].pb[j]->pb_Environment, nodesize);
617 struct pihd_dosnode_data *dat = (struct pihd_dosnode_data *)(&dst_data[addr2+0x20]);
619 if (BE(devs[i].pb[j]->pb_Flags) & 0x01) {
620 DEBUG("Partition is bootable.\n");
621 rom_partition_prio[cur_partition] = 0;
625 DEBUG("Partition is not bootable.\n");
626 rom_partition_prio[cur_partition] = -128;
627 dat->priority = htobe32(-128);
630 DEBUG("DOSNode Data:\n");
631 DEBUG("Name: %s Device: %s\n", dst_data + addr2, dst_data + data_addr);
632 DEBUG("Unit: %d Flags: %d Pad1: %d\n", BE(dat->unit), BE(dat->flags), BE(dat->pad1));
633 DEBUG("Node len: %d Block len: %d\n", BE(dat->node_len) * 4, BE(dat->block_len) * 4);
634 DEBUG("H: %d SPB: %d BPS: %d\n", BE(dat->surf), BE(dat->secs_per_block), BE(dat->blocks_per_track));
635 DEBUG("Reserved: %d Prealloc: %d\n", BE(dat->reserved_blocks), BE(dat->pad2));
636 DEBUG("Interleaved: %d Buffers: %d Memtype: %d\n", BE(dat->interleave), BE(dat->buffers), BE(dat->mem_type));
637 DEBUG("Lowcyl: %d Highcyl: %d Prio: %d\n", BE(dat->lowcyl), BE(dat->highcyl), BE(dat->priority));
638 DEBUG("Maxtransfer: %.8X Mask: %.8X\n", BE(dat->maxtransfer), BE(dat->transfer_mask));
639 DEBUG("DOSType: %.8X\n", BE(dat->dostype));
641 rom_partitions[cur_partition] = addr2 + 0x20 + cfg->map_offset[r];
642 rom_partition_dostype[cur_partition] = dat->dostype;
654 case PISCSI_CMD_NEXTPART:
655 DEBUG("[PISCSI] Switch partition %d -> %d\n", rom_cur_partition, rom_cur_partition + 1);
658 case PISCSI_CMD_NEXTFS:
659 DEBUG("[PISCSI] Switch file file system %d -> %d\n", rom_cur_fs, rom_cur_fs + 1);
662 case PISCSI_CMD_COPYFS:
663 DEBUG("[PISCSI] Copy file system %d to %.8X and reloc.\n", rom_cur_fs, piscsi_u32[2]);
664 r = get_mapped_item_by_address(cfg, piscsi_u32[2]);
666 uint32_t addr = piscsi_u32[2] - cfg->map_offset[r];
667 memset(cfg->map_data[r] + addr, 0x00, filesystems[rom_cur_fs].h_info.alloc_size);
668 memcpy(cfg->map_data[r] + addr, filesystems[rom_cur_fs].binary_data, filesystems[rom_cur_fs].h_info.byte_size);
669 filesystems[rom_cur_fs].h_info.base_offset = piscsi_u32[2];
670 reloc_hunks(filesystems[rom_cur_fs].relocs, cfg->map_data[r] + addr, &filesystems[rom_cur_fs].h_info);
671 filesystems[rom_cur_fs].handler = piscsi_u32[2];
674 case PISCSI_CMD_SETFSH: {
676 DEBUG("[PISCSI] Set handler for partition %d (DeviceNode: %.8X)\n", rom_cur_partition, val);
677 r = get_mapped_item_by_address(cfg, val);
679 uint32_t addr = val - cfg->map_offset[r];
680 struct DeviceNode *node = (struct DeviceNode *)(cfg->map_data[r] + addr);
681 char *dosID = (char *)&rom_partition_dostype[rom_cur_partition];
682 DEBUG("[PISCSI] Partition DOSType is %c%c%c/%d\n", dosID[0], dosID[1], dosID[2], dosID[3]);
683 for (i = 0; i < piscsi_num_fs; i++) {
684 if (rom_partition_dostype[rom_cur_partition] == filesystems[i].FS_ID) {
685 node->dn_SegList = htobe32((filesystems[i].handler >> 2));
686 node->dn_GlobalVec = 0xFFFFFFFF;
690 DEBUG("[!!!PISCSI] Found no handler for file system!\n");
692 DEBUG("[FS-HANDLER] Next: %d Type: %.8X\n", BE(node->dn_Next), BE(node->dn_Type));
693 DEBUG("[FS-HANDLER] Task: %d Lock: %d\n", BE(node->dn_Task), BE(node->dn_Lock));
694 DEBUG("[FS-HANDLER] Handler: %d Stacksize: %d\n", BE((uint32_t)node->dn_Handler), BE(node->dn_StackSize));
695 DEBUG("[FS-HANDLER] Priority: %d Startup: %d\n", BE((uint32_t)node->dn_Priority), BE(node->dn_Startup));
696 DEBUG("[FS-HANDLER] SegList: %.8X GlobalVec: %d\n", BE((uint32_t)node->dn_SegList), BE(node->dn_GlobalVec));
697 DEBUG("[PISCSI] Handler for partition %.8X set to %.8X (%.8X).\n", BE((uint32_t)node->dn_Name), filesystems[i].FS_ID, filesystems[i].handler);
701 case PISCSI_DBG_VAL1: case PISCSI_DBG_VAL2: case PISCSI_DBG_VAL3: case PISCSI_DBG_VAL4:
702 case PISCSI_DBG_VAL5: case PISCSI_DBG_VAL6: case PISCSI_DBG_VAL7: case PISCSI_DBG_VAL8: {
703 int i = ((addr & 0xFFFF) - PISCSI_DBG_VAL1) / 4;
708 print_piscsi_debug_message(val);
711 DEBUG("[!!!PISCSI] WARN: Unhandled %s register write to %.8X: %d\n", op_type_names[type], addr, val);
718 uint32_t handle_piscsi_read(uint32_t addr, uint8_t type) {
721 if ((addr & 0xFFFF) >= PISCSI_CMD_ROM) {
722 uint32_t romoffs = (addr & 0xFFFF) - PISCSI_CMD_ROM;
723 if (romoffs < (piscsi_rom_size + PIB)) {
724 //DEBUG("[PISCSI] %s read from Boot ROM @$%.4X (%.8X): ", op_type_names[type], romoffs, addr);
728 v = piscsi_rom_ptr[romoffs - PIB];
729 //DEBUG("%.2X\n", v);
732 v = be16toh(*((uint16_t *)&piscsi_rom_ptr[romoffs - PIB]));
733 //DEBUG("%.4X\n", v);
735 case OP_TYPE_LONGWORD:
736 v = be32toh(*((uint32_t *)&piscsi_rom_ptr[romoffs - PIB]));
737 //DEBUG("%.8X\n", v);
745 switch (addr & 0xFFFF) {
746 case PISCSI_CMD_ADDR1: case PISCSI_CMD_ADDR2: case PISCSI_CMD_ADDR3: case PISCSI_CMD_ADDR4: {
747 int i = ((addr & 0xFFFF) - PISCSI_CMD_ADDR1) / 4;
748 return piscsi_u32[i];
751 case PISCSI_CMD_DRVTYPE:
752 if (devs[piscsi_cur_drive].fd == -1) {
753 DEBUG("[PISCSI] %s Read from DRVTYPE %d, drive not attached.\n", op_type_names[type], piscsi_cur_drive);
756 DEBUG("[PISCSI] %s Read from DRVTYPE %d, drive attached.\n", op_type_names[type], piscsi_cur_drive);
759 case PISCSI_CMD_DRVNUM:
760 return piscsi_cur_drive;
762 case PISCSI_CMD_CYLS:
763 DEBUG("[PISCSI] %s Read from CYLS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].c);
764 return devs[piscsi_cur_drive].c;
766 case PISCSI_CMD_HEADS:
767 DEBUG("[PISCSI] %s Read from HEADS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].h);
768 return devs[piscsi_cur_drive].h;
770 case PISCSI_CMD_SECS:
771 DEBUG("[PISCSI] %s Read from SECS %d: %d\n", op_type_names[type], piscsi_cur_drive, devs[piscsi_cur_drive].s);
772 return devs[piscsi_cur_drive].s;
774 case PISCSI_CMD_BLOCKS: {
775 uint32_t blox = devs[piscsi_cur_drive].fs / 512;
776 DEBUG("[PISCSI] %s Read from BLOCKS %d: %d\n", op_type_names[type], piscsi_cur_drive, (uint32_t)(devs[piscsi_cur_drive].fs / 512));
777 DEBUG("fs: %lld (%d)\n", devs[piscsi_cur_drive].fs, blox);
781 case PISCSI_CMD_GETPART: {
782 DEBUG("[PISCSI] Get ROM partition %d offset: %.8X\n", rom_cur_partition, rom_partitions[rom_cur_partition]);
783 return rom_partitions[rom_cur_partition];
786 case PISCSI_CMD_GETPRIO:
787 DEBUG("[PISCSI] Get partition %d boot priority: %d\n", rom_cur_partition, rom_partition_prio[rom_cur_partition]);
788 return rom_partition_prio[rom_cur_partition];
790 case PISCSI_CMD_CHECKFS:
791 DEBUG("[PISCSI] Get current loaded file system: %.8X\n", filesystems[rom_cur_fs].FS_ID);
792 return filesystems[rom_cur_fs].FS_ID;
793 case PISCSI_CMD_FSSIZE:
794 DEBUG("[PISCSI] Get alloc size of loaded file system: %d\n", filesystems[rom_cur_fs].h_info.alloc_size);
795 return filesystems[rom_cur_fs].h_info.alloc_size;
797 DEBUG("[!!!PISCSI] WARN: Unhandled %s register read from %.8X\n", op_type_names[type], addr);