-
Notifications
You must be signed in to change notification settings - Fork 13
/
wiidisc.c
7237 lines (6070 loc) · 183 KB
/
wiidisc.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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* __ __ _ ___________ *
* \ \ / /| |____ ____| *
* \ \ / / | | | | *
* \ \ /\ / / | | | | *
* \ \/ \/ / | | | | *
* \ /\ / | | | | *
* \/ \/ |_| |_| *
* *
* Wiimms ISO Tools *
* https://wit.wiimm.de/ *
* *
***************************************************************************
* *
* This file is part of the WIT project. *
* Visit https://wit.wiimm.de/ for project details and sources. *
* *
* Copyright (c) 2009-2017 by Dirk Clemens <wiimm@wiimm.de> *
* *
***************************************************************************
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
* *
***************************************************************************/
#include "wiidisc.h"
#include <ctype.h>
//-----------------------------------------------------------------------------
#ifdef __CYGWIN__
#define ISALNUM(a) isalnum((int)(a))
#else
#define ISALNUM isalnum
#endif
//-----------------------------------------------------------------------------
#define WDPRINT noPRINT
//
///////////////////////////////////////////////////////////////////////////////
/////////////// common key & encryption ///////////////
///////////////////////////////////////////////////////////////////////////////
// The real common key must be set by the application.
// Only the first WII_KEY_SIZE bytes of 'common_key' are used.
static u8 common_key_templ[WII_KEY_SIZE] = "common key unset";
#ifdef SUPPORT_CKEY_DEVELOP
static u8 common_key[WD_CKEY__N][WII_KEY_SIZE] = {"","",""};
#else
static u8 common_key[WD_CKEY__N][WII_KEY_SIZE] = {"",""};
#endif
static const u8 iv0[WII_KEY_SIZE] = {0}; // always NULL
///////////////////////////////////////////////////////////////////////////////
static void wd_setup_common_key()
{
static bool done = false;
if (!done)
{
done = true;
memset(common_key,0,sizeof(common_key));
int i;
for ( i = 0; i < WD_CKEY__N; i++ )
memcpy(common_key[i],common_key_templ,sizeof(common_key[0]));
}
}
///////////////////////////////////////////////////////////////////////////////
const u8 * wd_get_common_key
(
wd_ckey_index_t ckey_index // index of common key
)
{
wd_setup_common_key();
return (unsigned)ckey_index < WD_CKEY__N ? common_key[ckey_index] : 0;
}
///////////////////////////////////////////////////////////////////////////////
const u8 * wd_set_common_key
(
wd_ckey_index_t ckey_index, // index of common key
const void * new_key // the new key
)
{
wd_setup_common_key();
if ( (unsigned)ckey_index >= WD_CKEY__N )
return 0;
u8 * ckey = common_key[ckey_index];
if (new_key)
{
memcpy(ckey,new_key,WII_KEY_SIZE);
#ifdef TEST
PRINT("new common key[%d]:\n",ckey_index);
HEXDUMP16(8,0,ckey,WII_KEY_SIZE);
#endif
}
return ckey;
}
///////////////////////////////////////////////////////////////////////////////
void wd_decrypt_title_key
(
const wd_ticket_t * tik, // pointer to ticket
u8 * title_key // the result
)
{
u8 iv[WII_KEY_SIZE];
memset( iv, 0, sizeof(iv) );
memcpy( iv, tik->title_id, 8 );
const wd_ckey_index_t ckey_index = tik->common_key_index < WD_CKEY__N
? tik->common_key_index : 0;
aes_key_t akey;
wd_aes_set_key( &akey, common_key[ckey_index] );
wd_aes_decrypt( &akey, iv, &tik->title_key, title_key, WII_KEY_SIZE );
TRACE("| IV: "); TRACE_HEXDUMP(0,0,1,WII_KEY_SIZE,iv,WII_KEY_SIZE);
TRACE("| IN: "); TRACE_HEXDUMP(0,0,1,WII_KEY_SIZE,&tik->title_key,WII_KEY_SIZE);
TRACE("| OUT: "); TRACE_HEXDUMP(0,0,1,WII_KEY_SIZE,title_key,WII_KEY_SIZE);
}
///////////////////////////////////////////////////////////////////////////////
void wd_encrypt_title_key
(
wd_ticket_t * tik, // pointer to ticket (modified)
const u8 * title_key // valid pointer to wanted title key
)
{
u8 iv[WII_KEY_SIZE];
memset( iv, 0, sizeof(iv) );
memcpy( iv, tik->title_id, 8 );
const wd_ckey_index_t ckey_index = tik->common_key_index < WD_CKEY__N
? tik->common_key_index : 0;
aes_key_t akey;
wd_aes_set_key( &akey, common_key[ckey_index] );
wd_aes_encrypt( &akey, iv, title_key, &tik->title_key, WII_KEY_SIZE );
TRACE("| IV: "); TRACE_HEXDUMP(0,0,1,WII_KEY_SIZE,iv,WII_KEY_SIZE);
TRACE("| IN: "); TRACE_HEXDUMP(0,0,1,WII_KEY_SIZE,&tik->title_key,WII_KEY_SIZE);
TRACE("| OUT: "); TRACE_HEXDUMP(0,0,1,WII_KEY_SIZE,title_key,WII_KEY_SIZE);
}
///////////////////////////////////////////////////////////////////////////////
void wd_decrypt_sectors
(
wd_part_t * part, // NULL or pointer to partition
const aes_key_t * akey, // aes key, if NULL use 'part'
const void * sect_src, // pointer to first source sector
void * sect_dest, // pointer to destination
void * hash_dest, // if not NULL: store hash tables here
u32 n_sectors // number of wii sectors to decrypt
)
{
if (!akey)
{
DASSERT(part);
wd_disc_t * disc = part->disc;
DASSERT(disc);
if ( disc->akey_part != part )
{
disc->akey_part = part;
wd_aes_set_key(&disc->akey,part->key);
}
akey = &disc->akey;
}
const u8 * src = sect_src;
u8 * dest = sect_dest;
u8 tempbuf[WII_SECTOR_SIZE];
if (hash_dest)
{
u8 * hdest = hash_dest;
while ( n_sectors-- > 0 )
{
const u8 * src1 = src;
if ( src == dest )
{
// inplace decryption not possible => use tempbuf
memcpy(tempbuf,src,WII_SECTOR_SIZE);
src1 = tempbuf;
}
// decrypt header first, because of possible overlay of 'src1' & 'dest'
wd_aes_decrypt( akey,
iv0,
src1,
hdest,
WII_SECTOR_HASH_SIZE);
// decrypt data
wd_aes_decrypt( akey,
src1 + WII_SECTOR_IV_OFF,
src1 + WII_SECTOR_HASH_SIZE,
dest,
WII_SECTOR_DATA_SIZE );
src += WII_SECTOR_SIZE;
dest += WII_SECTOR_DATA_SIZE;
hdest += WII_SECTOR_HASH_SIZE;
}
}
else
{
while ( n_sectors-- > 0 )
{
const u8 * src1 = src;
if ( src == dest )
{
// inplace decryption not possible => use tempbuf
memcpy(tempbuf,src,WII_SECTOR_SIZE);
src1 = tempbuf;
}
// decrypt data
wd_aes_decrypt( akey,
src1 + WII_SECTOR_IV_OFF,
src1 + WII_SECTOR_HASH_SIZE,
dest + WII_SECTOR_HASH_SIZE,
WII_SECTOR_DATA_SIZE );
// decrypt header
wd_aes_decrypt( akey,
iv0,
src1,
dest,
WII_SECTOR_HASH_SIZE);
src += WII_SECTOR_SIZE;
dest += WII_SECTOR_SIZE;
}
}
}
///////////////////////////////////////////////////////////////////////////////
void wd_encrypt_sectors
(
wd_part_t * part, // NULL or pointer to partition
const aes_key_t * akey, // aes key, if NULL use 'part'
const void * sect_src, // pointer to first source sector
const void * hash_src, // if not NULL: source of hash tables
void * sect_dest, // pointer to first destination sector
u32 n_sectors // number of wii sectors to encrypt
)
{
if (!akey)
{
DASSERT(part);
wd_disc_t * disc = part->disc;
DASSERT(disc);
if ( disc->akey_part != part )
{
disc->akey_part = part;
wd_aes_set_key(&disc->akey,part->key);
}
akey = &disc->akey;
}
const u8 * src = sect_src;
u8 * dest = sect_dest;
u8 tempbuf[WII_SECTOR_SIZE];
if (hash_src)
{
// copy reverse to allow overlap of 'data_src' and 'sect_dest'
const u8 * hsrc = (const u8*)hash_src + n_sectors * WII_SECTOR_HASH_SIZE;
src += n_sectors * WII_SECTOR_DATA_SIZE;
dest += n_sectors * WII_SECTOR_SIZE;
while ( n_sectors-- > 0 )
{
src -= WII_SECTOR_DATA_SIZE;
hsrc -= WII_SECTOR_HASH_SIZE;
dest -= WII_SECTOR_SIZE;
// encrypt header
wd_aes_encrypt( akey,
iv0,
hsrc,
tempbuf,
WII_SECTOR_HASH_SIZE );
// encrypt data
wd_aes_encrypt( akey,
tempbuf + WII_SECTOR_IV_OFF,
src,
tempbuf + WII_SECTOR_HASH_SIZE,
WII_SECTOR_DATA_SIZE );
memcpy(dest,tempbuf,WII_SECTOR_SIZE);
}
}
else
{
while ( n_sectors-- > 0 )
{
// encrypt header
wd_aes_encrypt( akey,
iv0,
src,
dest,
WII_SECTOR_HASH_SIZE );
// encrypt data
wd_aes_encrypt( akey,
dest + WII_SECTOR_IV_OFF,
src + WII_SECTOR_HASH_SIZE,
dest + WII_SECTOR_HASH_SIZE,
WII_SECTOR_DATA_SIZE );
src += WII_SECTOR_SIZE;
dest += WII_SECTOR_SIZE;
}
}
}
///////////////////////////////////////////////////////////////////////////////
void wd_split_sectors
(
const void * sect_src, // pointer to first source sector
void * data_dest, // pointer to data destination
void * hash_dest, // pointer to hash destination
u32 n_sectors // number of wii sectors to decrypt
)
{
DASSERT(sect_src);
DASSERT(data_dest);
DASSERT(hash_dest);
DASSERT( sect_src != hash_dest );
const u8 * src = sect_src;
u8 * data = data_dest;
u8 * hash = hash_dest;
while ( n_sectors-- > 0 )
{
memcpy( hash, src, WII_SECTOR_HASH_SIZE );
hash += WII_SECTOR_HASH_SIZE;
src += WII_SECTOR_HASH_SIZE;
memmove( data, src, WII_SECTOR_DATA_SIZE );
data += WII_SECTOR_DATA_SIZE;
src += WII_SECTOR_DATA_SIZE;
}
}
///////////////////////////////////////////////////////////////////////////////
void wd_join_sectors
(
const void * data_src, // pointer to data source
const void * hash_src, // pointer to hash source
void * sect_dest, // pointer to first destination sector
u32 n_sectors // number of wii sectors to encrypt
)
{
DASSERT(data_src);
DASSERT(hash_src);
DASSERT(sect_dest);
DASSERT( hash_src != sect_dest );
// copy reverse to allow overlap of 'data_src' and 'sect_dest'
const u8 * data = (const u8*)data_src + n_sectors * WII_SECTOR_DATA_SIZE;
const u8 * hash = (const u8*)hash_src + n_sectors * WII_SECTOR_HASH_SIZE;
u8 * dest = (u8*) sect_dest + n_sectors * WII_SECTOR_SIZE;
while ( n_sectors-- > 0 )
{
dest -= WII_SECTOR_DATA_SIZE;
data -= WII_SECTOR_DATA_SIZE;
memmove( dest, data, WII_SECTOR_DATA_SIZE );
dest -= WII_SECTOR_HASH_SIZE;
hash -= WII_SECTOR_HASH_SIZE;
memcpy( dest, hash, WII_SECTOR_HASH_SIZE );
}
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// names, ids, titles, ... ///////////////
///////////////////////////////////////////////////////////////////////////////
const char * wd_disc_type_name[]
= { "unknown", "GameCube", "Wii", 0 };
//-----------------------------------------------------------------------------
const char * wd_get_disc_type_name
(
wd_disc_type_t disc_type, // disc type
ccp not_found // result if no name found
)
{
return (unsigned)disc_type < WD_DT__N
? wd_disc_type_name[disc_type]
: not_found;
}
///////////////////////////////////////////////////////////////////////////////
const char * wd_part_name[]
= { "DATA", "UPDATE", "CHANNEL", 0 };
//-----------------------------------------------------------------------------
const char * wd_get_part_name
(
u32 ptype, // partition type
ccp not_found // result if no name found
)
{
return ptype < sizeof(wd_part_name)/sizeof(*wd_part_name)-1
? wd_part_name[ptype]
: not_found;
}
///////////////////////////////////////////////////////////////////////////////
char * wd_print_part_name
(
char * buf, // result buffer
// If NULL, a local circulary static buffer is used
size_t buf_size, // size of 'buf', ignored if buf==NULL
u32 ptype, // partition type
wd_pname_mode_t mode // print mode
)
{
if (!buf)
buf = GetCircBuf( buf_size = 20 );
ccp id4 = (ccp)&ptype;
const bool is_id = ISALNUM(id4[0]) && ISALNUM(id4[1])
&& ISALNUM(id4[2]) && ISALNUM(id4[3]);
const ccp name = ptype < sizeof(wd_part_name)/sizeof(*wd_part_name)-1
? wd_part_name[ptype] : 0;
switch (mode)
{
case WD_PNAME_NAME_NUM_9:
if (name)
snprintf(buf,buf_size,"%7s=%x",name,ptype);
else if (is_id)
{
ptype = htonl(ptype); // we need big endian here
snprintf(buf,buf_size," \"%.4s\"",id4);
}
else
snprintf(buf,buf_size,"%9x",ptype);
break;
case WD_PNAME_COLUMN_9:
if (name)
snprintf(buf,buf_size,"%7s %x",name,ptype);
else if (is_id)
{
ptype = htonl(ptype); // we need big endian here
snprintf(buf,buf_size," \"%.4s\"",id4);
}
else
snprintf(buf,buf_size,"%9x",ptype);
break;
case WD_PNAME_NAME:
if (name)
{
snprintf(buf,buf_size,"%s",name);
break;
}
// fall through
case WD_PNAME_NUM:
if (is_id)
{
ptype = htonl(ptype); // we need big endian here
snprintf(buf,buf_size,"%.4s",id4);
}
else
snprintf(buf,buf_size,"%x",ptype);
break;
case WD_PNAME_P_NAME:
if (name)
snprintf(buf,buf_size,"%s",name);
else if (is_id)
{
ptype = htonl(ptype); // we need big endian here
snprintf(buf,buf_size,"P-%.4s",id4);
}
else
snprintf(buf,buf_size,"P%x",ptype);
break;
//case WD_PNAME_NUM_INFO:
default:
if (name)
snprintf(buf,buf_size,"%x [%s]",ptype,name);
else if (is_id)
{
ptype = htonl(ptype); // we need big endian here
snprintf(buf,buf_size,"0x%x [\"%.4s\"]",ptype,id4);
}
else
snprintf(buf,buf_size,"0x%x [%u]",ptype,ptype);
break;
}
return buf;
}
///////////////////////////////////////////////////////////////////////////////
// returns a pointer to a printable ID, teminated with 0
char * wd_print_id
(
const void * id, // ID to convert in printable format
size_t id_len, // len of 'id'
void * buf // Pointer to buffer, size >= id_len + 1
// If NULL, a local circulary static buffer is used
)
{
DASSERT(id);
if (!buf)
buf = GetCircBuf( id_len + 1);
ccp src = id;
char * dest = buf;
while ( id_len-- > 0 )
{
const char ch = *src++;
*dest++ = ch >= ' ' && ch < 0x7f ? ch : '.';
}
*dest = 0;
return buf;
}
///////////////////////////////////////////////////////////////////////////////
// returns a pointer to a printable ID with colors, teminated with 0
char * wd_print_id_col
(
const void * id, // NULL | EMPTY | ID to convert
size_t id_len, // len of 'id'
const void * ref_id, // reference ID
const ColorSet_t *colset // NULL or color set
)
{
if ( !id || !*(u8*)id )
return " --";
if ( !ref_id || !memcmp(id,ref_id,id_len) || !colset || !colset->colorize )
return wd_print_id(id,id_len,0);
char buf[100];
char *dest = StringCopyS(buf,sizeof(buf)-id_len,colset->highlight);
ccp src = id;
while ( id_len-- > 0 )
{
const char ch = *src++;
*dest++ = ch >= ' ' && ch < 0x7f ? ch : '.';
}
dest = StringCopyE(dest,buf+sizeof(buf),colset->reset);
const uint len = dest - buf + 1;
char *res = GetCircBuf(len);
memcpy(res,buf,len);
return res;
}
///////////////////////////////////////////////////////////////////////////////
// rename ID and title of a ISO header
int wd_rename
(
void * p_data, // pointer to ISO data
ccp new_id, // if !NULL: take the first 6 chars as ID
ccp new_title // if !NULL: take the first 0x39 chars as title
)
{
DASSERT(p_data);
int done = 0;
if ( new_id )
{
ccp end_id;
char * dest = p_data;
for ( end_id = new_id + 6; new_id < end_id && *new_id; new_id++, dest++ )
if ( *new_id != '.' && *new_id != *dest )
{
*dest = *new_id;
done |= 1;
}
}
if ( new_title && *new_title )
{
char buf[WII_TITLE_SIZE];
memset(buf,0,WII_TITLE_SIZE);
const size_t slen = strlen(new_title);
memcpy(buf, new_title, slen < WII_TITLE_SIZE ? slen : WII_TITLE_SIZE-1 );
u8 *dest = p_data;
dest += WII_TITLE_OFF;
if ( memcmp(dest,buf,WII_TITLE_SIZE) )
{
memcpy(dest,buf,WII_TITLE_SIZE);
done |= 2;
}
}
return done;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// read functions ///////////////
///////////////////////////////////////////////////////////////////////////////
enumError wd_read_raw
(
wd_disc_t * disc, // valid disc pointer
u32 disc_offset4, // disc offset/4
void * dest_buf, // destination buffer
u32 read_size, // number of bytes to read
wd_usage_t usage_id // not 0: mark usage usage_table with this value
)
{
DASSERT(disc);
DASSERT(disc->read_func);
TRACE("#WD# wd_read_raw() %9llx %8x dest=%p\n",
(u64)disc_offset4<<2, read_size, dest_buf );
if (!read_size)
return ERR_OK;
if (dest_buf)
{
noTRACE("WD-READ: ++ %9llx %5x ++\n",(u64)disc_offset4<<2, read_size );
u8 *dest = dest_buf;
u32 len = read_size;
u32 offset4 = disc_offset4;
// force reading only whole HD SECTORS
char hd_buf[HD_SECTOR_SIZE];
const int HD_SECTOR_SIZE4 = HD_SECTOR_SIZE >> 2;
u32 skip4 = offset4 - offset4/HD_SECTOR_SIZE4 * HD_SECTOR_SIZE4;
if (skip4)
{
const u32 off1 = offset4 - skip4;
const int ret = disc->read_func(disc->read_data,off1,HD_SECTOR_SIZE,hd_buf);
if (ret)
{
WD_ERROR(ERR_READ_FAILED,"Error while reading disc%s",disc->error_term);
return ERR_READ_FAILED;
}
const u32 skip = skip4 << 2;
DASSERT( skip < HD_SECTOR_SIZE );
u32 copy_len = HD_SECTOR_SIZE - skip;
if ( copy_len > len )
copy_len = len;
noTRACE("WD-READ/PRE: %9llx %5x -> %5zx %5x, skip=%x,%x\n",
(u64)off1<<2, HD_SECTOR_SIZE, dest-(u8*)dest_buf, copy_len, skip,skip4 );
memcpy(dest,hd_buf+skip,copy_len);
dest += copy_len;
len -= copy_len;
offset4 += copy_len >> 2;
}
const u32 copy_len = len / HD_SECTOR_SIZE * HD_SECTOR_SIZE;
if ( copy_len > 0 )
{
noTRACE("WD-READ/MAIN: %9llx %5x -> %5zx %5x\n",
(u64)offset4<<2, copy_len, dest-(u8*)dest_buf, copy_len );
const int ret = disc->read_func(disc->read_data,offset4,copy_len,dest);
if (ret)
{
WD_ERROR(ERR_READ_FAILED,"Error while reading disc%s",disc->error_term);
return ERR_READ_FAILED;
}
dest += copy_len;
len -= copy_len;
offset4 += copy_len >> 2;
}
if ( len > 0 )
{
ASSERT( len < HD_SECTOR_SIZE );
noTRACE("WD-READ/POST: %9llx %5x -> %5zx %5x\n",
(u64)offset4<<2, HD_SECTOR_SIZE, dest-(u8*)dest_buf, len );
const int ret = disc->read_func(disc->read_data,offset4,HD_SECTOR_SIZE,hd_buf);
if (ret)
{
WD_ERROR(ERR_READ_FAILED,"Error while reading disc%s",disc->error_term);
return ERR_READ_FAILED;
}
memcpy(dest,hd_buf,len);
}
}
if ( usage_id )
{
u32 first_block = disc_offset4 >> WII_SECTOR_SIZE_SHIFT-2;
u32 end_block = disc_offset4 + ( read_size + WII_SECTOR_SIZE - 1 >> 2 )
>> WII_SECTOR_SIZE_SHIFT-2;
if ( end_block > WII_MAX_SECTORS )
end_block = WII_MAX_SECTORS;
noTRACE("mark %x+%x => %x..%x [%x]\n",
disc_offset4, read_size, first_block, end_block, end_block-first_block );
if ( first_block < end_block )
{
memset( disc->usage_table + first_block,
usage_id, end_block - first_block );
if ( disc->usage_max < end_block )
disc->usage_max = end_block;
}
}
return ERR_OK;
}
///////////////////////////////////////////////////////////////////////////////
enumError wd_read_part_raw
(
wd_part_t * part, // valid pointer to a disc partition
u32 offset4, // offset/4 to partition start
void * dest_buf, // destination buffer
u32 read_size, // number of bytes to read
bool mark_block // true: mark block in 'usage_table'
)
{
DASSERT(part);
return wd_read_raw( part->disc,
part->part_off4 + offset4,
dest_buf,
read_size,
mark_block ? part->usage_id : 0 );
}
///////////////////////////////////////////////////////////////////////////////
enumError wd_read_part_block
(
wd_part_t * part, // valid pointer to a disc partition
u32 block_num, // block number of partition
u8 * block, // destination buf
bool mark_block // true: mark block in 'usage_table'
)
{
TRACE("#WD# #%08x wd_read_part_block()\n",block_num);
DASSERT(part);
DASSERT(part->disc);
wd_disc_t * disc = part->disc;
enumError err = ERR_OK;
if ( disc->block_part != part || disc->block_num != block_num )
{
err = wd_read_raw( part->disc,
part->data_off4 + block_num * WII_SECTOR_SIZE4,
disc->temp_buf,
WII_SECTOR_SIZE,
mark_block ? part->usage_id | WD_USAGE_F_CRYPT : 0 );
if (err)
{
memset(disc->block_buf,0,sizeof(disc->block_buf));
disc->block_part = 0;
}
else
{
disc->block_part = part;
disc->block_num = block_num;
if (part->is_encrypted)
{
if ( disc->akey_part != part )
{
disc->akey_part = part;
wd_aes_set_key(&disc->akey,part->key);
}
// decrypt data
wd_aes_decrypt( &disc->akey,
disc->temp_buf + WII_SECTOR_IV_OFF,
disc->temp_buf + WII_SECTOR_HASH_SIZE,
disc->block_buf,
WII_SECTOR_DATA_SIZE );
}
else
memcpy( disc->block_buf,
disc->temp_buf + WII_SECTOR_HASH_SIZE,
WII_SECTOR_DATA_SIZE );
}
}
memcpy( block, disc->block_buf, WII_SECTOR_DATA_SIZE );
return err;
}
///////////////////////////////////////////////////////////////////////////////
enumError wd_read_part
(
wd_part_t * part, // valid pointer to a disc partition
u32 data_offset4, // partition data offset/4
void * dest_buf, // destination buffer
u32 read_size, // number of bytes to read
bool mark_block // true: mark block in 'usage_table'
)
{
TRACE("#WD# %8x %8x wd_read_part()\n",data_offset4,read_size);
DASSERT(part);
DASSERT(part->disc);
DASSERT(dest_buf);
wd_disc_t * disc = part->disc;
if (part->is_gc)
{
noPRINT("%llx+%x + %8x\n", (u64)part->part_off4<<2, data_offset4, read_size );
ASSERT(!(data_offset4&3));
const u64 off = part->part_off4 + ( data_offset4 >> 2 );
const u64 mark_end = ( off << 2 ) + read_size;
if ( part->max_marked < mark_end )
part->max_marked = mark_end;
return wd_read_raw ( disc, off, dest_buf, read_size,
mark_block ? part->usage_id : 0 );
}
const u64 mark_end = ( (u64)( part->data_off4 + data_offset4 ) << 2 ) + read_size;
if ( part->max_marked < mark_end )
part->max_marked = mark_end;
u8 * temp = disc->temp_buf;
u8 * dest = dest_buf;
enumError err = ERR_OK;
while ( read_size && err == ERR_OK )
{
u32 offset4_in_block = data_offset4 % WII_SECTOR_DATA_SIZE4;
u32 len_in_block = WII_SECTOR_DATA_SIZE - ( offset4_in_block << 2 );
if ( len_in_block > read_size )
len_in_block = read_size;
err = wd_read_part_block( part,
data_offset4 / WII_SECTOR_DATA_SIZE4,
temp,
mark_block );
memcpy( dest, temp + (offset4_in_block<<2), len_in_block );
dest += len_in_block;
data_offset4 += len_in_block >> 2;
read_size -= len_in_block;
}
return err;
}
///////////////////////////////////////////////////////////////////////////////
void wd_mark_part
(
wd_part_t * part, // valid pointer to a disc partition
u32 data_offset4, // partition data offset/4
u32 data_size // number of bytes to mark
)
{
DASSERT(part);
DASSERT(part->disc);
u32 first_block, end_block;
wd_usage_t marker;
if (part->is_gc)
{
const u32 block_delta = part->data_off4 / WII_SECTOR_SIZE4;
first_block = block_delta + data_offset4 / WII_SECTOR_SIZE;
end_block = ( data_offset4 + data_size + WII_SECTOR_SIZE - 1 )
/ WII_SECTOR_SIZE + block_delta;
marker = part->usage_id;
const u64 mark_end = data_offset4 + data_size;
if ( part->max_marked < mark_end )
part->max_marked = mark_end;
}
else
{
const u32 block_delta = part->data_off4 / WII_SECTOR_SIZE4;
first_block = data_offset4 / WII_SECTOR_DATA_SIZE4 + block_delta;
end_block = (( data_size + WII_SECTOR_DATA_SIZE - 1 >> 2 )
+ data_offset4 ) / WII_SECTOR_DATA_SIZE4
+ block_delta;
marker = part->usage_id | WD_USAGE_F_CRYPT;
const u64 mark_end = ( (u64)data_offset4 << 2 ) + data_size;
if ( part->max_marked < mark_end )
part->max_marked = mark_end;
}
if ( end_block > WII_MAX_SECTORS )
end_block = WII_MAX_SECTORS;
if ( first_block < end_block )
{
wd_disc_t * disc = part->disc;
memset( disc->usage_table + first_block, marker, end_block - first_block );
if ( part->end_sector < end_block )
part->end_sector = end_block;
if ( disc->usage_max < end_block )
disc->usage_max = end_block;
}
}
///////////////////////////////////////////////////////////////////////////////
u64 wd_calc_disc_offset
(
wd_part_t * part, // valid pointer to a disc partition
u64 data_offset4 // partition offset
)
{
DASSERT(part);
if (part->is_gc) // GC: non shifted offsets
return (u64)part->part_off4 + data_offset4;
//return (u64)( part->part_off4 + data_offset4 ) << 2;
const u32 offset_in_block = data_offset4 % WII_SECTOR_DATA_SIZE4;
const u32 block = data_offset4 / WII_SECTOR_DATA_SIZE4;
return (u64)( part->data_sector + block ) * WII_SECTOR_SIZE
+ ( offset_in_block << 2 )
+ WII_SECTOR_HASH_SIZE;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// get sector status ///////////////
///////////////////////////////////////////////////////////////////////////////
wd_sector_status_t wd_get_part_sector_status
(
wd_part_t * part, // valid pointer to a disc partition
u32 block_num, // block number of partition
bool silent // true: don't print error messages
)
{
DASSERT(part);
DASSERT(part->disc);
TRACE("wd_get_part_sector_status(%p,%d,%d)\n",part,block_num,silent);
//----- setup
// extra test '!part->is_loaded' to avoid circulary calling
if ( !part->is_loaded && wd_load_part(part,false,false,silent) )