PS2SDK
PS2 Homebrew Libraries
CardAuth.c
1 #include <stdio.h>
2 #include <sysclib.h>
3 
4 #include "secrman.h"
5 
6 #include "main.h"
7 #include "CardAuth.h"
8 
9 static McCommandHandler_t McCommandHandler; // 0x00003d10
10 static McDevIDHandler_t McDevIDHandler; // 0x00003d14
11 
12 #if 0
13 static void _printf4(const char *format, ...);
14 #endif
15 
16 // 0x00001f60
17 #if 0
18 static void _printf4(const char *format, ...){
19 
20 }
21 #endif
22 
23 // 0x00001fd0
24 void ResetMcHandlers(void)
25 {
26  McCommandHandler = NULL;
27  McDevIDHandler = NULL;
28 }
29 
30 // 0x00002a00
31 void SetMcCommandHandler(McCommandHandler_t handler)
32 {
33  McCommandHandler = handler;
34 }
35 
36 // 0x00002a10
37 McCommandHandler_t GetMcCommandHandler(void)
38 {
39  return McCommandHandler;
40 }
41 
42 // 0x00002a20
43 void SetMcDevIDHandler(McDevIDHandler_t handler)
44 {
45  McDevIDHandler = handler;
46 }
47 
48 // 0x00002a30
49 McDevIDHandler_t GetMcDevIDHandler(void)
50 {
51  return McDevIDHandler;
52 }
53 
54 // 0x00002a40
55 int McDeviceIDToCNum(int port, int slot)
56 {
57  if (McDevIDHandler == NULL) {
58  return -1;
59  }
60 
61  return McDevIDHandler(port, slot);
62 }
63 
64 // 0x00001f78
65 int SendMcCommand(int port, int slot, sio2_transfer_data_t *sio2packet)
66 {
67  return McCommandHandler(port, slot, sio2packet);
68 }
69 
70 // 0x00001fa0
71 unsigned char calculate_sio2_buffer_checksum(const void *buffer, unsigned int length)
72 {
73  unsigned int i, checksum;
74 
75  for (checksum = 0, i = 0; i < length; i++)
76  checksum ^= ((const unsigned char *)buffer)[i];
77 
78  return checksum;
79 }
80 
81 // 0x00002814
82 int card_auth_write(int port, int slot, const void *buffer, int arg3, int command)
83 {
84  int i;
85  sio2_transfer_data_t sio2packet;
86  unsigned char wrbuf[14], rdbuf[14];
87 
88  sio2packet.in = wrbuf;
89  sio2packet.out = rdbuf;
90  memset(sio2packet.port_ctrl1, 0, sizeof(sio2packet.port_ctrl1));
91  memset(sio2packet.port_ctrl2, 0, sizeof(sio2packet.port_ctrl2));
92  sio2packet.port_ctrl1[port] = 0xFF060505;
93  sio2packet.port_ctrl2[port] = 0x0003FFFF;
94  sio2packet.regdata[0] = (port & 3) | 0x00380E40;
95  sio2packet.regdata[1] = 0;
96  sio2packet.out_size = sizeof(rdbuf);
97  sio2packet.in_size = sizeof(wrbuf);
98  memset(sio2packet.in, 0, sizeof(wrbuf));
99  sio2packet.in[0] = 0x81;
100  sio2packet.in[1] = arg3;
101  sio2packet.in[2] = command;
102  for (i = 0; i < 8; i++)
103  sio2packet.in[3 + i] = ((const unsigned char *)buffer)[7 - i];
104  sio2packet.in[11] = calculate_sio2_buffer_checksum(buffer, 8);
105  sio2packet.out_dma.addr = NULL;
106  sio2packet.in_dma.addr = NULL;
107 
108  if (SendMcCommand(port, slot, &sio2packet) == 0) {
109  _printf4("card_command error %d\n", (unsigned char)command);
110  return 0;
111  }
112 
113  if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
114  _printf4("sio2 command error %d\n", (unsigned char)command);
115  return 0;
116  }
117 
118  if (rdbuf[12] != 0x2B) {
119  _printf4("ID error %d\n", (unsigned char)command);
120  return 0;
121  }
122 
123  if (rdbuf[13] == 0x66) {
124  _printf4("result failed %d\n", (unsigned char)command);
125  return 0;
126  }
127 
128  return 1;
129 }
130 
131 // 0x00002620
132 int card_auth_read(int port, int slot, void *buffer, int arg3, int command)
133 {
134  int i;
135  sio2_transfer_data_t sio2packet;
136  u8 wrbuf[14];
137  u8 rdbuf[14];
138 
139  sio2packet.in = wrbuf;
140  sio2packet.out = rdbuf;
141  memset(sio2packet.port_ctrl1, 0, sizeof(sio2packet.port_ctrl1));
142  memset(sio2packet.port_ctrl2, 0, sizeof(sio2packet.port_ctrl2));
143  sio2packet.port_ctrl1[port] = 0xFF060505;
144  sio2packet.port_ctrl2[port] = 0x0003FFFF;
145  sio2packet.regdata[0] = (port & 3) | 0x00380E40;
146  sio2packet.regdata[1] = 0;
147  sio2packet.out_size = sizeof(rdbuf);
148  sio2packet.in_size = sizeof(wrbuf);
149  memset(sio2packet.in, 0, sizeof(wrbuf));
150  sio2packet.in[0] = 0x81;
151  sio2packet.in[1] = arg3;
152  sio2packet.in[2] = command;
153  sio2packet.out_dma.addr = NULL;
154  sio2packet.in_dma.addr = NULL;
155 
156  if (SendMcCommand(port, slot, &sio2packet) == 0) {
157  _printf4("card_command error %d\n", (unsigned char)command);
158  return 0;
159  }
160 
161  if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
162  _printf4("sio2 command error %d\n", (unsigned char)command);
163  return 0;
164  }
165 
166  if (rdbuf[3] != 0x2B) {
167  _printf4("ID error %d\n", (unsigned char)command);
168  return 0;
169  }
170 
171  if (rdbuf[13] == 0x66) {
172  _printf4("result failed %d\n", (unsigned char)command);
173  return 0;
174  }
175 
176  if ((unsigned char)calculate_sio2_buffer_checksum(&rdbuf[4], 8) != rdbuf[12]) {
177  _printf4("check sum error %d\n", (unsigned char)command);
178  return 0;
179  }
180 
181  for (i = 0; i < 8; i++)
182  ((unsigned char *)buffer)[7 - i] = rdbuf[4 + i];
183 
184  return 1;
185 }
186 
187 // 0x00001fe4
188 int card_auth_60(int port, int slot)
189 {
190  sio2_transfer_data_t sio2packet;
191  unsigned char wrbuf[5], rdbuf[5];
192 
193  sio2packet.in = wrbuf;
194  sio2packet.out = rdbuf;
195  memset(sio2packet.port_ctrl1, 0, sizeof(sio2packet.port_ctrl1));
196  memset(sio2packet.port_ctrl2, 0, sizeof(sio2packet.port_ctrl2));
197  sio2packet.port_ctrl1[port] = 0xFF060505;
198  sio2packet.port_ctrl2[port] = 0x0003FFFF;
199  sio2packet.regdata[0] = (port & 3) | 0x00140540;
200  sio2packet.regdata[1] = 0;
201  sio2packet.out_size = sizeof(rdbuf);
202  sio2packet.in_size = sizeof(wrbuf);
203  memset(sio2packet.in, 0, sizeof(wrbuf));
204  sio2packet.in[0] = 0x81;
205  sio2packet.in[1] = 0xF3;
206  sio2packet.out_dma.addr = NULL;
207  sio2packet.in_dma.addr = NULL;
208 
209  if (SendMcCommand(port, slot, &sio2packet) == 0) {
210  _printf4("card_command error 0\n");
211  return 0;
212  }
213 
214  if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
215  _printf4("sio2 command error 0\n");
216  return 0;
217  }
218 
219  if (rdbuf[3] != 0x2B) {
220  _printf4("ID error 0\n");
221  return 0;
222  }
223 
224  if (rdbuf[4] == 0x66) {
225  _printf4("result failed 0\n");
226  return 0;
227  }
228 
229  return 1;
230 }
231 
232 // 0x00002168
233 int card_auth_key_change(int port, int slot, int command)
234 {
235  sio2_transfer_data_t sio2packet;
236  unsigned char wrbuf[5], rdbuf[5];
237 
238  sio2packet.in = wrbuf;
239  sio2packet.out = rdbuf;
240  memset(sio2packet.port_ctrl1, 0, sizeof(sio2packet.port_ctrl1));
241  memset(sio2packet.port_ctrl2, 0, sizeof(sio2packet.port_ctrl2));
242  sio2packet.port_ctrl1[port] = 0xFF060505;
243  sio2packet.port_ctrl2[port] = 0x0003FFFF;
244  sio2packet.regdata[0] = (port & 3) | 0x00140540;
245  sio2packet.regdata[1] = 0;
246  sio2packet.out_size = sizeof(rdbuf);
247  sio2packet.in_size = sizeof(wrbuf);
248  memset(sio2packet.in, 0, sizeof(wrbuf));
249  sio2packet.in[0] = 0x81;
250  sio2packet.in[1] = 0xF7;
251  sio2packet.in[2] = command;
252  sio2packet.out_dma.addr = NULL;
253  sio2packet.in_dma.addr = NULL;
254 
255  if (SendMcCommand(port, slot, &sio2packet) == 0) {
256  _printf4("card_command error 0\n");
257  return 0;
258  }
259 
260  if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
261  _printf4("sio2 command error 0\n");
262  return 0;
263  }
264 
265  if (rdbuf[3] != 0x2B) {
266  _printf4("ID error 0\n");
267  return 0;
268  }
269 
270  if (rdbuf[4] == 0x66) {
271  _printf4("result failed 0\n");
272  return 0;
273  }
274 
275  return 1;
276 }
277 
278 // 0x0000248c
279 int card_auth2(int port, int slot, int arg3, int command)
280 {
281  sio2_transfer_data_t sio2packet;
282  unsigned char wrbuf[5], rdbuf[5];
283 
284  sio2packet.in = wrbuf;
285  sio2packet.out = rdbuf;
286  memset(sio2packet.port_ctrl1, 0, sizeof(sio2packet.port_ctrl1));
287  memset(sio2packet.port_ctrl2, 0, sizeof(sio2packet.port_ctrl2));
288  sio2packet.port_ctrl1[port] = 0xFF060505;
289  sio2packet.port_ctrl2[port] = 0x0003FFFF;
290  sio2packet.regdata[0] = (port & 3) | 0x00140540;
291  sio2packet.regdata[1] = 0;
292  sio2packet.out_size = sizeof(rdbuf);
293  sio2packet.in_size = sizeof(wrbuf);
294  memset(sio2packet.in, 0, sizeof(wrbuf));
295  sio2packet.in[0] = 0x81;
296  sio2packet.in[1] = arg3;
297  sio2packet.in[2] = command;
298  sio2packet.out_dma.addr = NULL;
299  sio2packet.in_dma.addr = NULL;
300 
301  if (SendMcCommand(port, slot, &sio2packet) == 0) {
302  _printf4("card_command error %d\n", (unsigned char)command);
303  return 0;
304  }
305 
306  if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
307  _printf4("sio2 command error %d\n", (unsigned char)command);
308  return 0;
309  }
310 
311  if (rdbuf[3] != 0x2B) {
312  _printf4("ID error %d\n", (unsigned char)command);
313  return 0;
314  }
315 
316  if (rdbuf[4] == 0x66) {
317  _printf4("result failed %d\n", (unsigned char)command);
318  return 0;
319  }
320 
321  return 1;
322 }
323 
324 // 0x000022f8
325 int card_auth(int port, int slot, int arg3, int command)
326 {
327  sio2_transfer_data_t sio2packet;
328  unsigned char wrbuf[5], rdbuf[5];
329 
330  sio2packet.in = wrbuf;
331  sio2packet.out = rdbuf;
332  memset(sio2packet.port_ctrl1, 0, sizeof(sio2packet.port_ctrl1));
333  memset(sio2packet.port_ctrl2, 0, sizeof(sio2packet.port_ctrl2));
334  sio2packet.port_ctrl1[port] = 0xFF060505;
335  sio2packet.port_ctrl2[port] = 0x0003FFFF;
336  sio2packet.regdata[0] = (port & 3) | 0x00140540;
337  sio2packet.regdata[1] = 0;
338  sio2packet.out_size = sizeof(rdbuf);
339  sio2packet.in_size = sizeof(wrbuf);
340  memset(sio2packet.in, 0, sizeof(wrbuf));
341  sio2packet.in[0] = 0x81;
342  sio2packet.in[1] = arg3;
343  sio2packet.in[2] = command;
344  sio2packet.out_dma.addr = NULL;
345  sio2packet.in_dma.addr = NULL;
346 
347  if (SendMcCommand(port, slot, &sio2packet) == 0) {
348  _printf4("card_command error %d\n", (unsigned char)command);
349  return 0;
350  }
351 
352  if ((!((sio2packet.stat6c >> 13) & 1) && !((sio2packet.stat6c >> 14) & 3)) == 0) {
353  _printf4("sio2 command error %d\n", (unsigned char)command);
354  return 0;
355  }
356 
357  if (rdbuf[3] != 0x2B) {
358  _printf4("ID error %d\n", (unsigned char)command);
359  return 0;
360  }
361 
362  if (rdbuf[4] == 0x66) {
363  _printf4("result failed %d\n", (unsigned char)command);
364  return 0;
365  }
366 
367  return 1;
368 }
sysclib.h
stdio.h
sio2_transfer_data_t
Definition: sio2man.h:33
secrman.h