[aa2218]: arch / powerpc / platforms / embedded6xx / starlet-es.c Maximize Restore History

Download this file

starlet-es.c    673 lines (542 with data), 14.3 kB

  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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* arch/powerpc/platforms/embedded6xx/starlet-es.c
*
* Nintendo Wii starlet ES routines
* Copyright (C) 2008-2009 The GameCube Linux Team
* Copyright (C) 2008,2009 Albert Herranz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
*/
#define DEBUG
#include <linux/kernel.h>
#include <linux/of_platform.h>
#include <linux/scatterlist.h>
#include <asm/starlet.h>
#define DRV_MODULE_NAME "starlet-es"
#define DRV_DESCRIPTION "Nintendo Wii starlet ES driver"
#define DRV_AUTHOR "Albert Herranz"
static const char starlet_es_driver_version[] = "0.2i";
#define DBG(fmt, arg...) pr_debug(fmt, ##arg)
#define drv_printk(level, format, arg...) \
printk(level DRV_MODULE_NAME ": " format , ## arg)
#define STARLET_ES_IOS_MIN 30
#define STARLET_ES_IOS_MAX 36
#define STARLET_ES_TIMEOUT 1000000 /* usecs */
struct starlet_es_device {
int fd;
u64 ios_title;
struct device *dev;
};
struct starlet_es_ticket_limit {
u32 tag;
u32 value;
} __attribute__((packed));
struct starlet_es_ticket_view {
u32 view;
u64 ticketid;
u32 devicetype;
u64 title;
u16 access_mask;
u8 reserved[0x3c];
u8 cidx_mask[0x40];
u16 padding;
struct starlet_es_ticket_limit limits[8];
} __attribute__((packed));
/*
* /dev/es
*
*/
#define ES_IOCTLV_LAUNCHTITLE 0x08
#define ES_IOCTLV_GETTITLECOUNT 0x0e
#define ES_IOCTLV_GETTITLES 0x0f
#define ES_IOCTLV_GETTICKETVIEWCOUNT 0x12
#define ES_IOCTLV_GETTICKETVIEWS 0x13
static const char dev_es[] = "/dev/es";
/*
* Handy small buffer routines.
* We use a small static aligned buffer to avoid allocations for short-lived
* operations involving 1 to 4 byte data transfers to/from IOS.
*
*/
static u32 es_small_buf[L1_CACHE_BYTES / sizeof(u32)]
__attribute__ ((aligned(STARLET_IPC_DMA_ALIGN + 1)));
static const size_t es_small_buf_size = sizeof(es_small_buf_size);
static DEFINE_MUTEX(es_small_buf_lock);
static u32 *es_small_buf_get(void)
{
u32 *buf;
if (!mutex_trylock(&es_small_buf_lock))
buf = starlet_kzalloc(es_small_buf_size, GFP_ATOMIC);
else {
memset(es_small_buf, 0, es_small_buf_size);
buf = es_small_buf;
}
return buf;
}
static void es_small_buf_put(u32 *buf)
{
if (buf == es_small_buf)
mutex_unlock(&es_small_buf_lock);
else
starlet_kfree(buf);
}
/*
*
*
*/
static struct starlet_es_device *starlet_es_device_instance;
/**
* starlet_es_get_device() - get ES device handle
*
* Returns the handle for the Encryption Services (ES) device instance.
*/
struct starlet_es_device *starlet_es_get_device(void)
{
if (!starlet_es_device_instance)
drv_printk(KERN_ERR, "uninitialized device instance!\n");
return starlet_es_device_instance;
}
EXPORT_SYMBOL_GPL(starlet_es_get_device);
/*
*
*/
static int starlet_es_get_title_count(unsigned long *count)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
struct scatterlist io[1];
u32 *count_buf;
int error;
if (!es_dev)
return -ENODEV;
count_buf = es_small_buf_get();
if (!count_buf)
return -ENOMEM;
*count_buf = 0;
sg_init_one(io, count_buf, sizeof(*count_buf));
error = starlet_ioctlv(es_dev->fd, ES_IOCTLV_GETTITLECOUNT,
0, NULL, 1, io);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
else
*count = *count_buf;
es_small_buf_put(count_buf);
return error;
}
/*
*
*/
static int starlet_es_get_titles(u64 *titles, unsigned long count)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
struct scatterlist in[1], io[1];
u32 *count_buf;
int error;
if (!es_dev)
return -ENODEV;
count_buf = es_small_buf_get();
if (!count_buf)
return -ENOMEM;
*count_buf = count;
sg_init_one(in, count_buf, sizeof(*count_buf));
sg_init_one(io, titles, sizeof(*titles)*count);
error = starlet_ioctlv(es_dev->fd, ES_IOCTLV_GETTITLES,
1, in, 1, io);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
es_small_buf_put(count_buf);
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int starlet_es_get_ticket_view_count(u64 title, unsigned long *count)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
struct scatterlist in[1], io[1];
u64 *title_buf;
u32 *count_buf;
int error;
if (!es_dev)
return -ENODEV;
title_buf = starlet_kzalloc(sizeof(*title_buf), GFP_ATOMIC);
if (!title_buf)
return -ENOMEM;
count_buf = es_small_buf_get();
if (!count_buf) {
starlet_kfree(title_buf);
return -ENOMEM;
}
*title_buf = title;
sg_init_one(in, title_buf, sizeof(*title_buf));
sg_init_one(io, count_buf, sizeof(*count_buf));
error = starlet_ioctlv_polled(es_dev->fd, ES_IOCTLV_GETTICKETVIEWCOUNT,
1, in, 1, io, STARLET_ES_TIMEOUT);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
else
*count = *count_buf;
starlet_kfree(title_buf);
es_small_buf_put(count_buf);
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int starlet_es_get_ticket_views(u64 title,
struct starlet_es_ticket_view *views,
unsigned long count)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
struct scatterlist in[2], io[1];
u32 *count_buf;
u64 *title_buf;
int error;
if (!es_dev)
return -ENODEV;
title_buf = starlet_kzalloc(sizeof(*title_buf), GFP_ATOMIC);
if (!title_buf)
return -ENOMEM;
count_buf = es_small_buf_get();
if (!count_buf) {
starlet_kfree(title_buf);
return -ENOMEM;
}
*title_buf = title;
*count_buf = count;
sg_init_table(in, 2);
sg_set_buf(&in[0], title_buf, sizeof(*title_buf));
sg_set_buf(&in[1], count_buf, sizeof(*count_buf));
sg_init_one(io, views, sizeof(*views)*count);
error = starlet_ioctlv_polled(es_dev->fd, ES_IOCTLV_GETTICKETVIEWS,
2, in, 1, io, STARLET_ES_TIMEOUT);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
es_small_buf_put(count_buf);
starlet_kfree(title_buf);
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int starlet_es_launch_title_view(u64 title,
struct starlet_es_ticket_view *view)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
struct scatterlist in[2];
u64 *title_buf;
int error;
if (!es_dev)
return -ENODEV;
title_buf = starlet_kzalloc(sizeof(*title_buf), GFP_ATOMIC);
if (!title_buf)
return -ENOMEM;
*title_buf = title;
sg_init_table(in, 2);
sg_set_buf(&in[0], title_buf, sizeof(*title_buf));
sg_set_buf(&in[1], view, sizeof(*view));
error = starlet_ioctlv_and_reboot(es_dev->fd,
ES_IOCTLV_LAUNCHTITLE,
2, in, 0, NULL);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
starlet_kfree(title_buf);
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int starlet_es_launch_title(struct starlet_es_device *es_dev, u64 title)
{
struct starlet_es_ticket_view *views;
unsigned long count;
int error;
error = starlet_es_get_ticket_view_count(title, &count);
if (error)
return error;
views = starlet_kzalloc(sizeof(*views)*count, GFP_ATOMIC);
if (!views) {
DBG("%s: out of memory\n", __func__);
return -ENOMEM;
}
error = starlet_es_get_ticket_views(title, views, count);
if (error) {
starlet_kfree(views);
return error;
}
drv_printk(KERN_INFO, "launching title %u-%u\n",
(u32)(title >> 32), (u32)title);
error = starlet_es_launch_title_view(title, views); /* first view */
starlet_kfree(views);
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int starlet_es_reopen(struct starlet_es_device *es_dev)
{
int error;
error = starlet_open_polled(dev_es, 0, STARLET_ES_TIMEOUT);
if (error < 0) {
drv_printk(KERN_ERR, "unable to reopen %s (%d)\n",
dev_es, error);
goto out;
}
es_dev->fd = error;
error = 0;
out:
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int __starlet_es_reload_ios(struct starlet_es_device *es_dev)
{
int error = -EINVAL;
if (!es_dev->ios_title) {
drv_printk(KERN_ERR, "no IOS previously loaded\n");
goto out;
}
error = starlet_es_launch_title(es_dev, es_dev->ios_title);
if (!error)
es_dev->fd = -1;
out:
return error;
}
/*
* This call may be used in a non-sleeping context
*/
static int starlet_es_reload_ios(struct starlet_es_device *es_dev)
{
int error;
error = __starlet_es_reload_ios(es_dev);
if (!error)
error = starlet_es_reopen(es_dev);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
return error;
}
/**
* starlet_es_reload_ios_and_discard() - reload IOS and stop using it
*
* Reloads the version of IOS loaded at boot time.
* All IOS dependent devices will fail after this call unless they are
* reinitialized.
*
* This call may be used in a non-sleeping context
*/
int starlet_es_reload_ios_and_discard(void)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
int error = -EINVAL;
if (!es_dev)
goto err_out;
error = starlet_es_reload_ios(es_dev);
err_out:
return error;
}
EXPORT_SYMBOL_GPL(starlet_es_reload_ios_and_discard);
/**
* starlet_es_reload_ios_and_launch() - reload IOS and launch a title
*
* Reload the version of IOS loaded at boot time and launch a title.
* If the title loaded is a non-IOS title, this function will not return and
* is equivalent to a platform restart.
*
* This call may be used in a non-sleeping context
*/
int starlet_es_reload_ios_and_launch(u64 title)
{
struct starlet_es_device *es_dev = starlet_es_get_device();
int error = -EINVAL;
if (!es_dev)
goto err_out;
error = starlet_es_reload_ios(es_dev);
if (!error)
error = starlet_es_launch_title(es_dev, title);
err_out:
return error;
}
EXPORT_SYMBOL_GPL(starlet_es_reload_ios_and_launch);
static int starlet_es_find_newest_title(struct starlet_es_device *es_dev,
u64 *title,
u64 title_min, u64 title_max)
{
u64 *titles;
u64 candidate;
unsigned long count;
int found, i;
int error;
error = starlet_es_get_title_count(&count);
if (error)
return error;
titles = starlet_kzalloc(sizeof(*titles)*count, GFP_KERNEL);
if (!titles) {
DBG("%s: out of memory\n", __func__);
return -ENOMEM;
}
error = starlet_es_get_titles(titles, count);
if (error) {
starlet_kfree(titles);
return error;
}
found = 0;
candidate = title_min;
for (i = 0; i < count; i++) {
if (titles[i] > candidate && titles[i] <= title_max) {
candidate = titles[i];
found = 1;
}
}
starlet_kfree(titles);
if (!found)
return 0;
*title = candidate;
return 1;
}
static int starlet_es_load_preferred_ios(struct starlet_es_device *es_dev,
u64 ios_min, u64 ios_max)
{
u64 title;
int error;
error = starlet_es_find_newest_title(es_dev, &title, ios_min, ios_max);
if (!error)
return -EINVAL;
if (error > 0) {
es_dev->ios_title = title;
error = starlet_es_launch_title(es_dev, title);
if (!error)
error = starlet_es_reopen(es_dev);
}
return error;
}
static int starlet_nwc24_stop_scheduler(void)
{
void *obuf;
const size_t osize = 0x20;
int fd;
int error = 0;
obuf = es_small_buf_get();
if (!obuf)
return -ENOMEM;
fd = starlet_open("/dev/net/kd/request", 0);
if (fd >= 0) {
error = starlet_ioctl(fd, 1, NULL, 0, obuf, osize);
starlet_close(fd);
}
es_small_buf_put(obuf);
if (error)
DBG("%s: error=%d (%08x)\n", __func__, error, error);
return error;
}
static int starlet_es_init(struct starlet_es_device *es_dev)
{
u64 ios_min, ios_max;
int error;
error = starlet_open(dev_es, 0);
if (error >= 0) {
starlet_es_device_instance = es_dev;
es_dev->fd = error;
ios_min = 0x100000000ULL | STARLET_ES_IOS_MIN;
ios_max = 0x100000000ULL | STARLET_ES_IOS_MAX;
error = starlet_es_load_preferred_ios(es_dev, ios_min, ios_max);
if (error)
drv_printk(KERN_WARNING, "unable to load preferred"
" IOS version (min %llx, max %llx)\n",
ios_min, ios_max);
}
/*
* Try to disable the Nintendo Wifi Connect 24 scheduler.
* And do this even if we failed to load our preferred IOS.
*
* When the scheduler kicks in, starlet IPC calls from Broadway fail.
*/
starlet_nwc24_stop_scheduler();
return error;
}
static void starlet_es_exit(struct starlet_es_device *es_dev)
{
starlet_es_device_instance = NULL;
starlet_close(es_dev->fd);
es_dev->fd = -1;
}
/*
* Driver model helper routines.
*
*/
static int starlet_es_do_probe(struct device *dev)
{
struct starlet_es_device *es_dev;
int retval;
es_dev = kzalloc(sizeof(*es_dev), GFP_KERNEL);
if (!es_dev) {
drv_printk(KERN_ERR, "failed to allocate es_dev\n");
return -ENOMEM;
}
dev_set_drvdata(dev, es_dev);
es_dev->dev = dev;
retval = starlet_es_init(es_dev);
if (retval) {
dev_set_drvdata(dev, NULL);
kfree(es_dev);
}
return retval;
}
static int starlet_es_do_remove(struct device *dev)
{
struct starlet_es_device *es_dev = dev_get_drvdata(dev);
if (es_dev) {
starlet_es_exit(es_dev);
dev_set_drvdata(dev, NULL);
kfree(es_dev);
return 0;
}
return -ENODEV;
}
/*
* OF platform driver hooks.
*
*/
static int starlet_es_of_probe(struct of_device *odev,
const struct of_device_id *dev_id)
{
return starlet_es_do_probe(&odev->dev);
}
static int starlet_es_of_remove(struct of_device *odev)
{
return starlet_es_do_remove(&odev->dev);
}
static struct of_device_id starlet_es_of_match[] = {
{ .compatible = "nintendo,starlet-es" },
{ },
};
MODULE_DEVICE_TABLE(of, starlet_es_of_match);
static struct of_platform_driver starlet_es_of_driver = {
.owner = THIS_MODULE,
.name = DRV_MODULE_NAME,
.match_table = starlet_es_of_match,
.probe = starlet_es_of_probe,
.remove = starlet_es_of_remove,
};
/*
* Kernel module interface hooks.
*
*/
static int __init starlet_es_init_module(void)
{
drv_printk(KERN_INFO, "%s - version %s\n", DRV_DESCRIPTION,
starlet_es_driver_version);
return of_register_platform_driver(&starlet_es_of_driver);
}
static void __exit starlet_es_exit_module(void)
{
of_unregister_platform_driver(&starlet_es_of_driver);
}
module_init(starlet_es_init_module);
module_exit(starlet_es_exit_module);
MODULE_DESCRIPTION(DRV_DESCRIPTION);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_LICENSE("GPL");