Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / modules / hdd / ps2hdd / hdd.c
blob6d4ad82b9883e188f7da067b3188f4fd4808297c
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
10 # $Id: hdd.c 1511 2009-01-15 09:11:30Z radad $
11 # Start Up routines
14 #include "hdd.h"
16 IRX_ID("hdd_driver", 1, 1);
18 iop_device_ops_t hddOps={
19 hddInit,
20 hddDeinit,
21 hddFormat,
22 hddOpen,
23 hddClose,
24 hddRead,
25 hddWrite,
26 hddLseek,
27 (void*)hddUnsupported,
28 hddRemove,
29 (void*)hddUnsupported,
30 (void*)hddUnsupported,
31 hddDopen,
32 hddClose,
33 hddDread,
34 hddGetStat,
35 (void*)hddUnsupported,
36 hddReName,
37 (void*)hddUnsupported,
38 (void*)hddUnsupported,
39 (void*)hddUnsupported,
40 (void*)hddUnsupported,
41 (void*)hddUnsupported,
42 hddDevctl,
43 (void*)hddUnsupported,
44 (void*)hddUnsupported,
45 hddIoctl2,
47 iop_device_t hddFioDev={
48 "hdd",
49 IOP_DT_BLOCK | IOP_DT_FSEXT,
51 "HDD",
52 (struct _iop_device_ops *)&hddOps,
55 u32 maxOpen=1;
56 hdd_device_t hddDeviceBuf[2]={
57 {0, 0, 0, 3},
58 {0, 0, 0, 3}
61 char mbrMagic[0x20]={
62 0x2B, 0x17, 0x16, 0x01, 0x58, 0x3B, 0x17, 0x15,
63 0x08, 0x0D, 0x0C, 0x1D, 0x0A, 0x58, 0x3D, 0x16,
64 0x0C, 0x1D, 0x0A, 0x0C, 0x19, 0x11, 0x16, 0x15,
65 0x1D, 0x16, 0x0C, 0x58, 0x31, 0x16, 0x1B, 0x56
70 int inputError(char *input)
72 printf("ps2hdd: Error: Usage: %s [-o <maxOpen>] [-n <maxcache>]\n", input);
73 return 1;
76 void printStartup(void)
78 printf("ps2hdd: PS2 APA Driver v1.1 (c) 2003 Vector\n");
79 return;
82 int unlockDrive(u32 device)
84 u8 id[32];
85 int rv;
86 if((rv=getIlinkID(id))==0)
87 return atadSceUnlock(device, id);
88 return rv;
91 int _start(int argc, char **argv)
93 int i;
94 char *input;
95 int cacheSize=3;
96 ps2time tm;
97 t_shddInfo *hddInfo;
99 printStartup();
100 // decode MBR Magic
101 for(i=0;i!=0x20;i++)
102 mbrMagic[i]^='x';
104 if((input=strrchr(argv[0], '/')))
105 input++;
106 else
107 input=argv[0];
109 argc--; argv++;
110 while(argc)
112 if(argv[0][0] != '-')
113 break;
114 if(strcmp("-o", argv[0])==0){
115 argc--; argv++;
116 if(!argc)
117 return inputError(input);
118 i=strtol(argv[0], 0, 10);
119 if(i-1<32)
120 maxOpen=i;
122 else if(strcmp("-n", argv[0])==0){
123 argc--; argv++;
124 if(!argc)
125 return inputError(input);
126 i=strtol(*argv, 0, 10);
127 if(cacheSize<i)
128 cacheSize=i;
130 argc--; argv++;
133 printf("ps2hdd: max open = %ld, %d buffers\n", maxOpen, cacheSize);
134 getPs2Time(&tm);
135 printf("ps2hdd: %02d:%02d:%02d %02d/%02d/%d\n",
136 tm.hour, tm.min, tm.sec, tm.month, tm.day, tm.year);
137 for(i=0;i < 2;i++)
139 if(!(hddInfo=atadInit((u16)i))){
140 printf("ps2hdd: Error: ata initialization failed.\n");
141 return 0;
143 if(hddInfo->exists!=0 && hddInfo->has_packet==0){
144 hddDeviceBuf[i].status--;
145 hddDeviceBuf[i].totalLBA=hddInfo->total_sectors;
146 hddDeviceBuf[i].partitionMaxSize=apaGetPartitionMax(hddInfo->total_sectors);
147 if(unlockDrive(i)==0)
148 hddDeviceBuf[i].status--;
149 printf("ps2hdd: disk%d: 0x%08lx sectors, max 0x%08lx\n", i,
150 hddDeviceBuf[i].totalLBA, hddDeviceBuf[i].partitionMaxSize);
153 fileSlots=allocMem(maxOpen*sizeof(hdd_file_slot_t));
154 if(fileSlots)
155 memset(fileSlots, 0, maxOpen*sizeof(hdd_file_slot_t));
157 cacheInit(cacheSize);
158 for(i=0;i < 2;i++)
160 if(hddDeviceBuf[i].status<2){
161 if(journalResetore(i)!=0)
162 return 1;
163 if(apaGetFormat(i, (int *)&hddDeviceBuf[i].format))
164 hddDeviceBuf[i].status--;
165 printf("ps2hdd: drive status %ld, format version %08lx\n",
166 hddDeviceBuf[i].status, hddDeviceBuf[i].format);
169 DelDrv("hdd");
170 if(AddDrv(&hddFioDev)==0)
172 printf("ps2hdd: driver start.\n");
173 return 0;
175 return 1;