apa_cache_t *apaFillHeader(s32 device, const apa_params_t *params, u32 start, u32 next, u32 prev, u32 length, int *err) { // used for making a new partition apa_cache_t *clink; if (!(clink = apaCacheGetHeader(device, start, APA_IO_MODE_WRITE, err))) return NULL; memset(clink->header, 0, sizeof(apa_header_t)); clink->header->magic = APA_MAGIC; clink->header->start = start; clink->header->next = next; clink->header->prev = prev; clink->header->length = length; clink->header->type = params->type; clink->header->flags = params->flags; clink->header->modver = APA_MODVER; memcpy(clink->header->id, params->id, APA_IDMAX); if (params->flags & APA_FLAG_SUB) { clink->header->main = params->main; clink->header->number = params->number; } else { if (strncmp(clink->header->id, "_tmp", APA_IDMAX) != 0) { memcpy(clink->header->rpwd, params->rpwd, APA_PASSMAX); memcpy(clink->header->fpwd, params->fpwd, APA_PASSMAX); } } apaGetTime(&clink->header->created); clink->flags |= APA_CACHE_FLAG_DIRTY; return clink; }
apa_cache_t *apaRemovePartition(s32 device, u32 start, u32 next, u32 prev, u32 length) { apa_cache_t *clink; int err; if ((clink = apaCacheGetHeader(device, start, APA_IO_MODE_WRITE, &err)) == NULL) return NULL; memset(clink->header, 0, sizeof(apa_header_t)); clink->header->magic = APA_MAGIC; clink->header->start = start; clink->header->next = next; clink->header->prev = prev; clink->header->length = length; strcpy(clink->header->id, "__empty"); apaGetTime(&clink->header->created); clink->flags |= APA_CACHE_FLAG_DIRTY; return clink; }
void apaMakeEmpty(apa_cache_t *clink) { u32 saved_start; u32 saved_next; u32 saved_prev; u32 saved_length; saved_start = clink->header->start; saved_next = clink->header->next; saved_prev = clink->header->prev; saved_length = clink->header->length; memset(clink->header, 0, sizeof(apa_header_t)); clink->header->magic = APA_MAGIC; clink->header->start = saved_start; clink->header->next = saved_next; clink->header->prev = saved_prev; clink->header->length = saved_length; apaGetTime(&clink->header->created); strcpy(clink->header->id, "__empty"); clink->flags |= APA_CACHE_FLAG_DIRTY; }
int hddDevctl(iop_file_t *f, const char *devname, int cmd, void *arg, unsigned int arglen, void *bufp, unsigned int buflen) { int rv=0; WaitSema(fioSema); switch(cmd) { // Command set 1 ('H') case HDIOC_DEV9OFF: //Early versions called ata_device_smart_save_attr() here, when their old dev9 versions did not support the pre-shutdown callback. dev9Shutdown(); break; case HDIOC_IDLE: rv=ata_device_idle(f->unit, *(char *)arg); break; case HDIOC_MAXSECTOR: rv=hddDevices[f->unit].partitionMaxSize; break; case HDIOC_TOTALSECTOR: rv=hddDevices[f->unit].totalLBA; break; case HDIOC_FLUSH: if(ata_device_flush_cache(f->unit)) rv=-EIO; break; case HDIOC_SWAPTMP: rv=devctlSwapTemp(f->unit, (char *)arg); break; case HDIOC_SMARTSTAT: rv=ata_device_smart_get_status(f->unit); break; case HDIOC_STATUS: rv=hddDevices[f->unit].status; break; case HDIOC_FORMATVER: rv=hddDevices[f->unit].format; break; case HDIOC_FREESECTOR: rv=apaGetFreeSectors(f->unit, bufp, hddDevices); break; case HDIOC_IDLEIMM: rv=ata_device_idle_immediate(f->unit); break; // Command set 2 ('h') case HDIOC_GETTIME: rv=apaGetTime((apa_ps2time_t *)bufp); break; case HDIOC_SETOSDMBR: rv=devctlSetOsdMBR(f->unit, (hddSetOsdMBR_t *)arg); break; case HDIOC_GETSECTORERROR: rv=apaGetPartErrorSector(f->unit, APA_SECTOR_SECTOR_ERROR, 0); break; case HDIOC_GETERRORPARTNAME: rv=apaGetPartErrorName(f->unit, (char *)bufp); break; case HDIOC_READSECTOR: rv=ata_device_sector_io(f->unit, (void *)bufp, ((hddAtaTransfer_t *)arg)->lba, ((hddAtaTransfer_t *)arg)->size, ATA_DIR_READ); break; case HDIOC_WRITESECTOR: rv=ata_device_sector_io(f->unit, ((hddAtaTransfer_t *)arg)->data, ((hddAtaTransfer_t *)arg)->lba, ((hddAtaTransfer_t *)arg)->size, ATA_DIR_WRITE); break; case HDIOC_SCEIDENTIFY: rv=ata_device_sce_identify_drive(f->unit, (u16 *)bufp); break; default: rv=-EINVAL; break; } SignalSema(fioSema); return rv; }
int hddFormat(iop_file_t *f, const char *dev, const char *blockdev, void *arg, int arglen) { int rv=0; apa_cache_t *clink; u32 i; #ifdef APA_FORMAT_MAKE_PARTITIONS apa_params_t params; u32 emptyBlocks[32]; #endif if(f->unit >= 2) return -ENXIO; // clear all errors on hdd clink=apaCacheAlloc(); memset(clink->header, 0, sizeof(apa_header_t)); if(ata_device_sector_io(f->unit, clink->header, APA_SECTOR_SECTOR_ERROR, 1, ATA_DIR_WRITE)){ apaCacheFree(clink); return -EIO; } if(ata_device_sector_io(f->unit, clink->header, APA_SECTOR_PART_ERROR, 1, ATA_DIR_WRITE)){ apaCacheFree(clink); return -EIO; } // clear apa headers for(i=1024*8;i<hddDevices[f->unit].totalLBA;i+=(1024*256)) { ata_device_sector_io(f->unit, clink->header, i, sizeof(apa_header_t)/512, ATA_DIR_WRITE); } apaCacheFree(clink); if((rv=apaJournalReset(f->unit))!=0) return rv; // set up mbr :) if((clink=apaCacheGetHeader(f->unit, 0, APA_IO_MODE_WRITE, &rv))){ apa_header_t *header=clink->header; memset(header, 0, sizeof(apa_header_t)); header->magic=APA_MAGIC; header->length=(1024*256); // 128MB header->type=APA_TYPE_MBR; strcpy(header->id,"__mbr"); #ifdef APA_FORMAT_LOCK_MBR apaEncryptPassword(header->id, header->fpwd, "sce_mbr"); apaEncryptPassword(header->id, header->rpwd, "sce_mbr"); #endif memcpy(header->mbr.magic, apaMBRMagic, sizeof(header->mbr.magic)); header->mbr.version=APA_MBR_VERSION; header->mbr.nsector=0; apaGetTime(&header->created); apaGetTime(&header->mbr.created); header->checksum=apaCheckSum(header); clink->flags|=APA_CACHE_FLAG_DIRTY; apaCacheFlushDirty(clink); ata_device_flush_cache(f->unit); apaCacheFree(clink); hddDevices[f->unit].status=0; hddDevices[f->unit].format=APA_MBR_VERSION; } #ifdef APA_FORMAT_MAKE_PARTITIONS memset(&emptyBlocks, 0, sizeof(emptyBlocks)); memset(¶ms, 0, sizeof(apa_params_t)); params.size=(1024*256); params.type=APA_TYPE_PFS; // add __net, __system.... for(i=0;formatPartList[i];i++) { memset(params.id, 0, APA_IDMAX); strcpy(params.id, formatPartList[i]); if(!(clink=hddAddPartitionHere(f->unit, ¶ms, emptyBlocks, i ? clink->sector : 0, &rv))) return rv; apaCacheFree(clink); params.size<<=1; if(hddDevices[f->unit].partitionMaxSize < params.size) params.size=hddDevices[f->unit].partitionMaxSize; } #endif return rv; }
int _start(int argc, char **argv) { int i, ret; char *input; int cacheSize = 3; apa_ps2time_t tm; ata_devinfo_t *hddInfo; printStartup(); if ((input = strrchr(argv[0], '/'))) input++; else input = argv[0]; argc--; argv++; while (argc) { if (argv[0][0] != '-') break; if (strcmp("-o", argv[0]) == 0) { argc--; argv++; if (!argc) return inputError(input); i = strtol(argv[0], 0, 10); if (i - 1 < 32) apaMaxOpen = i; } else if (strcmp("-n", argv[0]) == 0) { argc--; argv++; if (!argc) return inputError(input); i = strtol(*argv, 0, 10); if (cacheSize < i) cacheSize = i; } argc--; argv++; } APA_PRINTF(APA_DRV_NAME ": max open = %d, %d buffers\n", apaMaxOpen, cacheSize); if (dev9RegisterShutdownCb(0, &hddShutdownCb) != 0) { APA_PRINTF(APA_DRV_NAME ": error: dev9 may not be resident.\n"); return hddInitError(); } if (apaGetTime(&tm) != 0) { APA_PRINTF(APA_DRV_NAME ": error: could not get date.\n"); return hddInitError(); } APA_PRINTF(APA_DRV_NAME ": %02d:%02d:%02d %02d/%02d/%d\n", tm.hour, tm.min, tm.sec, tm.month, tm.day, tm.year); for (i = 0; i < 2; i++) { if (!(hddInfo = ata_get_devinfo(i))) { APA_PRINTF(APA_DRV_NAME ": Error: ata initialization failed.\n"); return hddInitError(); } if (hddInfo->exists != 0 && hddInfo->has_packet == 0) { hddDevices[i].status--; hddDevices[i].totalLBA = hddInfo->total_sectors; hddDevices[i].partitionMaxSize = apaGetPartitionMax(hddInfo->total_sectors); if (unlockDrive(i) == 0) hddDevices[i].status--; APA_PRINTF(APA_DRV_NAME ": disk%d: 0x%08lx sectors, max 0x%08lx\n", i, hddDevices[i].totalLBA, hddDevices[i].partitionMaxSize); } } hddFileSlots = apaAllocMem(apaMaxOpen * sizeof(hdd_file_slot_t)); ret = (hddFileSlots == NULL) ? -ENOMEM : 0; if (ret != 0) { APA_PRINTF(APA_DRV_NAME ": error: file descriptor initialization failed.\n"); return hddInitError(); } memset(hddFileSlots, 0, apaMaxOpen * sizeof(hdd_file_slot_t)); if (apaCacheInit(cacheSize) != 0) { APA_PRINTF(APA_DRV_NAME ": error: cache buffer initialization failed.\n"); return hddInitError(); } for (i = 0; i < 2; i++) { if (hddDevices[i].status < 2) { if (apaJournalRestore(i) != 0) { APA_PRINTF(APA_DRV_NAME ": error: log check failed.\n"); return hddInitError(); } if (apaGetFormat(i, &hddDevices[i].format)) hddDevices[i].status--; APA_PRINTF(APA_DRV_NAME ": drive status %d, format version %08x\n", hddDevices[i].status, hddDevices[i].format); } } DelDrv("hdd"); if (AddDrv(&hddFioDev) == 0) { #ifdef APA_OSD_VER APA_PRINTF(APA_DRV_NAME ": version %04x driver start. This is OSD version!\n", IRX_VER(APA_MODVER_MAJOR, APA_MODVER_MINOR)); #else APA_PRINTF(APA_DRV_NAME ": version %04x driver start.\n", IRX_VER(APA_MODVER_MAJOR, APA_MODVER_MINOR)); #endif return MODULE_RESIDENT_END; } else { APA_PRINTF(APA_DRV_NAME ": error: add device failed.\n"); return hddInitError(); } }