+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);
+}
+