forked from kakaroto/PSFreedom
/
psfreedom.c
541 lines (457 loc) · 15.1 KB
/
psfreedom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* psfreedom.c -- PS3 Jailbreak exploit Gadget Driver
*
* Copyright (C) Youness Alaoui (KaKaRoTo)
*
* This software is distributed under the terms of the GNU General Public
* License ("GPL") version 2, as published by the Free Software Foundation.
*
* This code is based in part on:
*
* PSGroove
* USB MIDI Gadget Driver, Copyright (C) 2006 Thumtronics Pty Ltd.
* Gadget Zero driver, Copyright (C) 2003-2004 David Brownell.
* USB Audio driver, Copyright (C) 2002 by Takashi Iwai.
* USB MIDI driver, Copyright (C) 2002-2005 Clemens Ladisch.
*
*/
#define DEBUG
//#define VERBOSE_DEBUG
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/device.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#else
#include <linux/usb.h>
#include <linux/usb_gadget.h>
#endif
/*-------------------------------------------------------------------------*/
MODULE_AUTHOR("Youness Alaoui (KaKaRoTo)");
MODULE_LICENSE("GPL v3");
#define DRIVER_VERSION "29 August 2010"
static const char shortname[] = "PSFreedom";
static const char longname[] = "PS3 Jailbreak exploit";
/* big enough to hold our biggest descriptor */
#define USB_BUFSIZ 4000
/* States for the state machine */
enum PsfreedomState {
INIT,
HUB_READY,
DEVICE1_WAIT_READY,
DEVICE1_READY,
DEVICE1_WAIT_DISCONNECT,
DEVICE1_DISCONNECTED,
DEVICE2_WAIT_READY,
DEVICE2_READY,
DEVICE2_WAIT_DISCONNECT,
DEVICE2_DISCONNECTED,
DEVICE3_WAIT_READY,
DEVICE3_READY,
DEVICE3_WAIT_DISCONNECT,
DEVICE3_DISCONNECTED,
DEVICE4_WAIT_READY,
DEVICE4_READY,
DEVICE4_WAIT_DISCONNECT,
DEVICE4_DISCONNECTED,
DEVICE5_WAIT_READY,
DEVICE5_CHALLENGED,
DEVICE5_READY,
DEVICE5_WAIT_DISCONNECT,
DEVICE5_DISCONNECTED,
DEVICE6_WAIT_READY,
DEVICE6_READY,
DONE,
};
#define STATUS_STR(s) ( \
s==INIT?"INIT": \
s==HUB_READY?"HUB_READY": \
s==DEVICE1_WAIT_READY?"DEVICE1_WAIT_READY": \
s==DEVICE1_READY?"DEVICE1_READY": \
s==DEVICE1_WAIT_DISCONNECT?"DEVICE1_WAIT_DISCONNECT": \
s==DEVICE1_DISCONNECTED?"DEVICE1_DISCONNECTED": \
s==DEVICE2_WAIT_READY?"DEVICE2_WAIT_READY": \
s==DEVICE2_READY?"DEVICE2_READY": \
s==DEVICE2_WAIT_DISCONNECT?"DEVICE2_WAIT_DISCONNECT": \
s==DEVICE2_DISCONNECTED?"DEVICE2_DISCONNECTED": \
s==DEVICE3_WAIT_READY?"DEVICE3_WAIT_READY": \
s==DEVICE3_READY?"DEVICE3_READY": \
s==DEVICE3_WAIT_DISCONNECT?"DEVICE3_WAIT_DISCONNECT": \
s==DEVICE3_DISCONNECTED?"DEVICE3_DISCONNECTED": \
s==DEVICE4_WAIT_READY?"DEVICE4_WAIT_READY": \
s==DEVICE4_READY?"DEVICE4_READY": \
s==DEVICE4_WAIT_DISCONNECT?"DEVICE4_WAIT_DISCONNECT": \
s==DEVICE4_DISCONNECTED?"DEVICE4_DISCONNECTED": \
s==DEVICE5_WAIT_READY?"DEVICE5_WAIT_READY": \
s==DEVICE5_CHALLENGED?"DEVICE5_CHALLENGED": \
s==DEVICE5_READY?"DEVICE5_READY": \
s==DEVICE5_WAIT_DISCONNECT?"DEVICE5_WAIT_DISCONNECT": \
s==DEVICE5_DISCONNECTED?"DEVICE5_DISCONNECTED": \
s==DEVICE6_WAIT_READY?"DEVICE6_WAIT_READY": \
s==DEVICE6_READY?"DEVICE6_READY": \
s==DONE?"DONE": \
"UNKNOWN_STATE")
/* User-friendly string for the request */
#define REQUEST_STR(r) ( \
r==0x8006?"GET_DESCRIPTOR": \
r==0xa006?"GET_HUB_DESCRIPTOR": \
r==0x0009?"SET_CONFIGURATION": \
r==0x2303?"SET_PORT_FEATURE": \
r==0xa300?"GET_PORT_STATUS": \
r==0x2301?"CLEAR_PORT_FEATURE": \
r==0x000B?"SET_INTERFACE": \
r==0x21AA?"FREEDOM": \
"UNKNOWN")
#include "hub.h"
#include "psfreedom_machine.c"
/* Out device structure */
struct psfreedom_device {
spinlock_t lock;
struct usb_gadget *gadget;
/* for control responses */
struct usb_request *req;
/* The hub uses a non standard ep2in */
struct usb_ep *hub_ep;
/* BULK IN for the JIG */
struct usb_ep *in_ep;
/* BULK OUT for the JIG */
struct usb_ep *out_ep;
/* status of the state machine */
enum PsfreedomState status;
/* The port to switch to after a delay */
int switch_to_port_delayed;
/* Received length of the JIG challenge */
int challenge_len;
/* Sent length of the JIG response */
int response_len;
/* Hub port status/change */
struct hub_port hub_ports[6];
/* Currently enabled port on the hub (0 == hub) */
unsigned int current_port;
/* The address of all ports (0 == hub) */
u8 port_address[7];
};
/* Undef these if it gets defined by the controller's include in
psfreedom_machine.c */
#ifdef DBG
# undef DBG
#endif
#ifdef VDBG
# undef VDBG
#endif
#ifdef INFO
# undef INFO
#endif
#ifdef ERROR
# undef ERROR
#endif
#define INFO(d, fmt, args...) \
dev_info(&(d)->gadget->dev , fmt , ## args)
#define ERROR(d, fmt, args...) \
dev_err(&(d)->gadget->dev , fmt , ## args)
#define DBG(d, fmt, args...) \
dev_dbg(&(d)->gadget->dev , fmt , ## args)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
#define VDBG(d, fmt, args...) \
dev_vdbg(&(d)->gadget->dev , fmt , ## args)
#else
#define VDBG DBG
#endif
static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length);
static void free_ep_req(struct usb_ep *ep, struct usb_request *req);
/* Timer functions and macro to run the state machine */
static int timer_added = 0;
static struct timer_list psfreedom_state_machine_timer;
#define SET_TIMER(ms) DBG (dev, "Setting timer to %dms\n", ms); \
mod_timer (&psfreedom_state_machine_timer, jiffies + msecs_to_jiffies(ms))
#include "hub.c"
#include "psfreedom_devices.c"
static void psfreedom_state_machine_timeout(unsigned long data)
{
struct usb_gadget *gadget = (struct usb_gadget *)data;
struct psfreedom_device *dev = get_gadget_data (gadget);
unsigned long flags;
spin_lock_irqsave (&dev->lock, flags);
DBG (dev, "Timer fired, status is %s\n", STATUS_STR (dev->status));
/* We need to delay switching the address because otherwise we will respond
to the request (that triggered the port switch) with address 0. So we need
to reply with the hub's address, THEN switch to 0.
*/
if (dev->switch_to_port_delayed >= 0)
switch_to_port (dev, dev->switch_to_port_delayed);
dev->switch_to_port_delayed = -1;
switch (dev->status) {
case HUB_READY:
dev->status = DEVICE1_WAIT_READY;
hub_connect_port (dev, 1);
break;
case DEVICE1_READY:
dev->status = DEVICE2_WAIT_READY;
hub_connect_port (dev, 2);
break;
case DEVICE2_READY:
dev->status = DEVICE3_WAIT_READY;
hub_connect_port (dev, 3);
break;
case DEVICE3_READY:
dev->status = DEVICE2_WAIT_DISCONNECT;
hub_disconnect_port (dev, 2);
break;
case DEVICE2_DISCONNECTED:
dev->status = DEVICE4_WAIT_READY;
hub_connect_port (dev, 4);
break;
case DEVICE4_READY:
dev->status = DEVICE5_WAIT_READY;
hub_connect_port (dev, 5);
break;
case DEVICE5_CHALLENGED:
jig_response_send (dev, NULL);
break;
case DEVICE5_READY:
dev->status = DEVICE3_WAIT_DISCONNECT;
hub_disconnect_port (dev, 3);
break;
case DEVICE3_DISCONNECTED:
dev->status = DEVICE5_WAIT_DISCONNECT;
hub_disconnect_port (dev, 5);
break;
case DEVICE5_DISCONNECTED:
dev->status = DEVICE4_WAIT_DISCONNECT;
hub_disconnect_port (dev, 4);
break;
case DEVICE4_DISCONNECTED:
dev->status = DEVICE1_WAIT_DISCONNECT;
hub_disconnect_port (dev, 1);
break;
case DEVICE1_DISCONNECTED:
dev->status = DEVICE6_WAIT_READY;
hub_connect_port (dev, 6);
break;
case DEVICE6_READY:
dev->status = DONE;
INFO (dev, "Congratulations, worked!");
del_timer (&psfreedom_state_machine_timer);
timer_added = 0;
break;
default:
break;
}
spin_unlock_irqrestore (&dev->lock, flags);
}
static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
{
struct usb_request *req;
req = usb_ep_alloc_request(ep, GFP_ATOMIC);
if (req) {
req->length = length;
req->buf = kmalloc(length, GFP_ATOMIC);
if (!req->buf) {
usb_ep_free_request(ep, req);
req = NULL;
}
}
return req;
}
static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
{
kfree(req->buf);
usb_ep_free_request(ep, req);
}
static void psfreedom_disconnect (struct usb_gadget *gadget)
{
struct psfreedom_device *dev = get_gadget_data (gadget);
unsigned long flags;
int i;
spin_lock_irqsave (&dev->lock, flags);
DBG (dev, "Got disconnected\n");
/* Reinitialize all device variables*/
dev->challenge_len = 0;
dev->response_len = 0;
dev->current_port = 0;
for (i = 0; i < 6; i++)
dev->hub_ports[i].status = dev->hub_ports[i].change = 0;
for (i = 0; i < 7; i++)
dev->port_address[i] = 0;
hub_disconnect (gadget);
devices_disconnect (gadget);
if (timer_added)
del_timer (&psfreedom_state_machine_timer);
timer_added = 0;
dev->switch_to_port_delayed = -1;
dev->status = INIT;
spin_unlock_irqrestore (&dev->lock, flags);
}
static void psfreedom_setup_complete(struct usb_ep *ep, struct usb_request *req)
{
struct psfreedom_device *dev = ep->driver_data;
unsigned long flags;
spin_lock_irqsave (&dev->lock, flags);
if (req->status || req->actual != req->length) {
struct psfreedom_device * dev = (struct psfreedom_device *) ep->driver_data;
DBG(dev, "%s setup complete FAIL --> %d, %d/%d\n",
STATUS_STR (dev->status), req->status, req->actual, req->length);
} else {
VDBG(dev, "%s setup complete SUCCESS --> %d, %d/%d\n",
STATUS_STR (dev->status), req->status, req->actual, req->length);
}
spin_unlock_irqrestore (&dev->lock, flags);
}
/*
* The setup() callback implements all the ep0 functionality that's
* not handled lower down, in hardware or the hardware driver (like
* device and endpoint feature flags, and their status). It's all
* housekeeping for the gadget function we're implementing. Most of
* the work is in config-specific setup.
*/
static int psfreedom_setup(struct usb_gadget *gadget,
const struct usb_ctrlrequest *ctrl)
{
struct psfreedom_device *dev = get_gadget_data(gadget);
struct usb_request *req = dev->req;
int value = -EOPNOTSUPP;
u16 w_index = le16_to_cpu(ctrl->wIndex);
u16 w_value = le16_to_cpu(ctrl->wValue);
u16 w_length = le16_to_cpu(ctrl->wLength);
u8 address = psfreedom_get_address (dev->gadget);
unsigned long flags;
u16 request = (ctrl->bRequestType << 8) | ctrl->bRequest;
spin_lock_irqsave (&dev->lock, flags);
VDBG (dev, "Setup called %d (%d) -- %d -- %d. Myaddr :%d\n", ctrl->bRequest,
ctrl->bRequestType, w_value, w_index, address);
req->zero = 0;
/* Enable the timer if it's not already enabled */
if (timer_added == 0)
add_timer (&psfreedom_state_machine_timer);
timer_added = 1;
/* Set the address of the port */
if (address)
dev->port_address[dev->current_port] = address;
/* Setup the hub or the devices */
if (dev->current_port == 0)
value = hub_setup (gadget, ctrl, request, w_index, w_value, w_length);
else
value = devices_setup (gadget, ctrl, request, w_index, w_value, w_length);
DBG (dev, "%s Setup called %s (%d - %d) -> %d (w_length=%d)\n",
STATUS_STR (dev->status), REQUEST_STR (request), w_value, w_index,
value, w_length);
/* respond with data transfer before status phase? */
if (value >= 0) {
req->length = value;
req->zero = value < w_length;
value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
if (value < 0) {
DBG(dev, "ep_queue --> %d\n", value);
req->status = 0;
spin_unlock_irqrestore (&dev->lock, flags);
psfreedom_setup_complete(gadget->ep0, req);
return value;
}
}
spin_unlock_irqrestore (&dev->lock, flags);
/* device either stalls (value < 0) or reports success */
return value;
}
static void /* __init_or_exit */ psfreedom_unbind(struct usb_gadget *gadget)
{
struct psfreedom_device *dev = get_gadget_data(gadget);
DBG(dev, "unbind\n");
if (timer_added)
del_timer (&psfreedom_state_machine_timer);
timer_added = 0;
/* we've already been disconnected ... no i/o is active */
if (dev) {
if (dev->req) {
dev->req->length = USB_BUFSIZ;
free_ep_req(gadget->ep0, dev->req);
}
kfree(dev);
set_gadget_data(gadget, NULL);
}
}
static int __init psfreedom_bind(struct usb_gadget *gadget)
{
struct psfreedom_device *dev;
int err = 0;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev) {
return -ENOMEM;
}
spin_lock_init(&dev->lock);
usb_gadget_set_selfpowered (gadget);
dev->gadget = gadget;
set_gadget_data(gadget, dev);
/* preallocate control response and buffer */
dev->req = alloc_ep_req(gadget->ep0, USB_BUFSIZ);
if (!dev->req) {
err = -ENOMEM;
goto fail;
}
dev->req->complete = psfreedom_setup_complete;
gadget->ep0->driver_data = dev;
INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
/* Bind the hub and devices */
err = hub_bind (gadget, dev);
if (err < 0)
goto fail;
err = devices_bind (gadget, dev);
if (err < 0)
goto fail;
DBG(dev, "psfreedom_bind finished ok\n");
setup_timer(&psfreedom_state_machine_timer, psfreedom_state_machine_timeout,
(unsigned long) gadget);
psfreedom_disconnect (gadget);
return 0;
fail:
psfreedom_unbind(gadget);
return err;
}
static void psfreedom_suspend(struct usb_gadget *gadget)
{
struct psfreedom_device *dev = get_gadget_data(gadget);
if (gadget->speed == USB_SPEED_UNKNOWN) {
return;
}
DBG(dev, "suspend\n");
}
static void psfreedom_resume(struct usb_gadget *gadget)
{
struct psfreedom_device *dev = get_gadget_data(gadget);
DBG(dev, "resume\n");
}
static struct usb_gadget_driver psfreedom_driver = {
.speed = USB_SPEED_HIGH,
.function = (char *)longname,
.bind = psfreedom_bind,
.unbind = psfreedom_unbind,
.setup = psfreedom_setup,
.disconnect = psfreedom_disconnect,
.suspend = psfreedom_suspend,
.resume = psfreedom_resume,
.driver = {
.name = (char *)shortname,
.owner = THIS_MODULE,
},
};
static int __init psfreedom_init(void)
{
int ret = 0;
printk(KERN_INFO "init\n");
/* Determine what speed the controller supports */
if (psfreedom_is_high_speed ())
psfreedom_driver.speed = USB_SPEED_HIGH;
else if (psfreedom_is_low_speed ())
psfreedom_driver.speed = USB_SPEED_HIGH;
else
psfreedom_driver.speed = USB_SPEED_FULL;
ret = usb_gadget_register_driver(&psfreedom_driver);
printk(KERN_INFO "register driver returned %d\n", ret);
return ret;
}
module_init(psfreedom_init);
static void __exit psfreedom_cleanup(void)
{
usb_gadget_unregister_driver(&psfreedom_driver);
}
module_exit(psfreedom_cleanup);