-
Notifications
You must be signed in to change notification settings - Fork 13
/
iso-interface.c
5985 lines (4940 loc) · 156 KB
/
iso-interface.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-2021 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 *
* *
***************************************************************************/
#define _GNU_SOURCE 1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include "dclib/dclib-debug.h"
#include "dclib/dclib-file.h"
#include "dclib/dclib-xdump.h"
#include "iso-interface.h"
#include "wbfs-interface.h"
#include "titles.h"
#include "match-pattern.h"
#include "dirent.h"
#include "crypt.h"
//
///////////////////////////////////////////////////////////////////////////////
/////////////// dump helpers ///////////////
///////////////////////////////////////////////////////////////////////////////
static void dump_data
( FILE * f, int indent, off_t base, u32 off4, off_t size, ccp text )
{
const u64 off = (off_t)off4 << 2;
const u64 end = off + size;
fprintf(f,"%*s"
" %-5s %9llx .. %9llx -> %9llx .. %9llx, size:%10llx/hex =%11llu\n",
indent,"", text, off, end,
(u64)base+off, (u64)base+end, (u64)size, (u64)size );
}
//-----------------------------------------------------------------------------
static void dump_size
(
FILE * f, // output stream
int indent, // indent
ccp title, // header
u64 size, // size to print
u64 percent_base1, // >0: print percent value based on 'percent_base'
u64 percent_base2, // >0: print percent value based on 'percent_base'
ccp append_text // NULL or text to append
)
{
fprintf(f,"%*s%-17s%11llx/hex =%11llu",
indent,"", title, size, size );
if ( size >= 10240 )
fprintf(f," = %s",wd_print_size_1024(0,0,size,true));
if ( percent_base1 > 0 )
{
double percent = size * 100.0 / percent_base1;
if ( percent >= 9.95 )
fprintf(f,", %4.1f%%",percent);
else
fprintf(f,", %4.2f%%",percent);
}
if ( percent_base2 > 0 )
{
double percent = size * 100.0 / percent_base2;
if ( percent >= 9.95 )
fprintf(f,", %4.1f%%",percent);
else
fprintf(f,", %4.2f%%",percent);
}
if (append_text)
fputs(append_text,f);
fputc('\n',f);
}
//-----------------------------------------------------------------------------
static void dump_sys_version
(
FILE * f, // output stream
int indent, // indent
u64 sys_version, // system version to print
int title_fw // field width of title
)
{
const u32 hi = sys_version >> 32;
const u32 lo = (u32)sys_version;
if ( hi == 1 && lo < 0x100 )
fprintf(f,"%*s%-*s%08x-%08x = IOS 0x%02x = IOS %u\n",
indent, "", title_fw, "System version: ", hi, lo, lo, lo );
else
fprintf(f,"%*s%-*s%08x-%08x\n",
indent, "", title_fw, "System version: ", hi, lo );
}
//-----------------------------------------------------------------------------
static void dump_id
(
FILE * f, // output stream
int indent, // indent
ccp disc_id6, // NULL or disc id
ccp wbfs_id6, // NULL or wbfs id
wd_part_t * part, // not NULL: retrieve IDs from partition
int title_fw // field width of title
)
{
PRINT("dump_id() id=%s,%s, part=%p\n",disc_id6,wbfs_id6,part);
char wbfs[20] = {0};
if ( wbfs_id6 && *wbfs_id6 )
snprintf(wbfs,sizeof(wbfs),", wbfs=%s",wd_print_id(wbfs_id6,6,0));
if (part)
{
if ( disc_id6 && *disc_id6 )
fprintf(f,"%*s%-*sdisc=%s, ticket=%s, tmd=%s, boot=%s%s\n",
indent,"", title_fw, "Disc & part IDs: ",
wd_print_id(disc_id6,6,0),
wd_print_id(part->ph.ticket.title_id+4,4,0),
part->tmd ? wd_print_id(part->tmd->title_id+4,4,0) : "-",
wd_print_id(&part->boot,6,0), wbfs );
else
fprintf(f,"%*s%-*sticket=%s, tmd=%s, boot=%s%s\n",
indent,"", title_fw, "Partition IDs: ",
wd_print_id(part->ph.ticket.title_id+4,4,0),
part->tmd ? wd_print_id(part->tmd->title_id+4,4,0) : "-",
wd_print_id(&part->boot,6,0), wbfs );
}
else if ( disc_id6 && *disc_id6 )
fprintf(f,"%*s%-*s%s%s\n",
indent,"", title_fw, "Disc ID: ",
wd_print_id(disc_id6,6,0), wbfs );
}
//-----------------------------------------------------------------------------
static int dump_header
(
FILE * f, // output stream
int indent, // indent
SuperFile_t * sf, // file to dump
ccp id6, // NULL or id6
ccp real_path, // NULL or pointer to real path
wd_disc_t * disc // not NULL: retrieve info from disc
)
{
ASSERT(f);
ASSERT(sf);
indent = NormalizeIndent(indent);
fprintf(f,"\n%*sDump of file %s\n\n",indent,"",sf->f.fname);
indent += 2;
if ( real_path && *real_path && strcmp(sf->f.fname,real_path) )
fprintf(f,"%*sReal path: %s\n",indent,"",real_path);
const u64 virt_size = sf->file_size;
const u64 real_size = sf->f.st.st_size;
if ( sf->iod.oft != OFT_UNKNOWN && sf->disc2 )
{
if ( sf->iod.oft == OFT_PLAIN )
dump_size(f,indent,"ISO file size:",real_size,0,0,0);
else
dump_size(f,indent,"Virtual size:",virt_size,0,0,0);
char buf[50];
const u32 n_blocks = wd_count_used_disc_blocks(sf->disc2,1,0);
snprintf(buf,sizeof(buf),", %u*%uK",n_blocks,WII_SECTOR_SIZE/KiB);
const u64 scrubbed_size = n_blocks * (u64)WII_SECTOR_SIZE;
dump_size(f,indent,"Scrubbed size:",scrubbed_size,virt_size,0,buf);
if ( sf->iod.oft != OFT_PLAIN )
{
snprintf(buf,sizeof(buf),"%s file size:",oft_info[sf->iod.oft].name);
dump_size(f,indent,buf,real_size,virt_size,scrubbed_size,0);
}
if (sf->wbfs_fragments)
fprintf(f,"%*sWBFS fragments: 1+%u"
" = a ratio of %4.2f additional_fragments/GiB\n",
indent,"", sf->wbfs_fragments-1,
(sf->wbfs_fragments-1) * (double)(GiB) / scrubbed_size );
}
else
dump_size(f,indent,"File size:",real_size,0,0,0);
//----- file and disc type
char ftype_info[30] = {0};
if (sf->wia)
{
wia_disc_t * disc = &sf->wia->disc;
if (disc)
snprintf( ftype_info, sizeof(ftype_info), " (v%s,%s)",
PrintVersionWIA(0,0,sf->wia->fhead.version),
wd_print_compression(0,0,disc->compression,
disc->compr_level,disc->chunk_size,2) );
}
if (disc)
{
fprintf(f,"%*sFile & disc type: %s%s & %s%s%s%s\n",
indent,"",
GetNameFT(sf->f.ftype,0), ftype_info,
wd_get_disc_type_name(disc->disc_type,"?"),
disc->disc_attrib & WD_DA_GC_MULTIBOOT ? ", MultiBoot" : "",
disc->disc_attrib & WD_DA_GC_START_PART ? "+" : "",
disc->disc_attrib & WD_DA_GC_DVD9 ? ", DVD9" : "" );
if ( !id6 && sf )
id6 = sf->f.id6_dest;
dump_id(f,indent,id6,sf->wbfs_id6,disc->main_part,19);
}
else
fprintf(f,"%*sFile type: %s%s\n",
indent,"", GetNameFT(sf->f.ftype,0), ftype_info );
return indent;
}
//-----------------------------------------------------------------------------
static void dump_sig_type
(
FILE * f, // output stream
int indent, // indent
u32 sig_type, // signature or public key type
cert_stat_t sig_status, // not NULL: cert status of signature
bool is_pubkey // true: not sig but public key
)
{
const int size = is_pubkey
? cert_get_pubkey_size(sig_type)
: cert_get_signature_size(sig_type);
fprintf(f,"%*s%-17s%11x/hex =%11u [%s, size=%u%s%s]\n",
indent, "",
is_pubkey ? "Public key type:" : "Signature type:",
sig_type, sig_type,
cert_get_signature_name(sig_type|0x10000,"?"),
size,
sig_status ? ", " : "",
sig_status ? cert_get_status_name(sig_status,"?") : "" );
#if 0
if (sig_status)
fprintf(f,"%*s%-19s%s\n",
indent, "",
is_pubkey ? "Public key status:" : "Signature status:",
cert_get_status_message(sig_status,"?") );
#endif
}
//-----------------------------------------------------------------------------
static void dump_hex
(
FILE * f, // output file
const void * p_data, // pointer to data
size_t dsize, // size of data, must be <100
size_t asc_indent, // if >0: print ":...:" with entered indent
ccp tail // print this test and end of line (eg. LF)
)
{
DASSERT(f);
const u8 * data = p_data;
DASSERT(p_data);
char buf[100];
ASSERT(dsize < sizeof(buf) );
int count = 0, fw = 0;
while ( dsize-- > 0 )
{
if ( count && !(count&3) )
{
fputc(' ',f);
fw++;
}
buf[count++] = *data >= ' ' && *data < 0x7f ? *data : '.';
fprintf(f,"%02x",*data++);
fw += 2;
}
if ( asc_indent > 0 )
{
buf[count] = 0;
fprintf(f,"%*s= '%s'%s",(int)(asc_indent-fw), "", buf, tail ? tail : "" );
}
else if (tail)
fputs(tail,f);
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Dump_ISO() ///////////////
///////////////////////////////////////////////////////////////////////////////
static void dump_gc_part
(
FILE * f, // valid output stream
int indent, // indent of output
wd_disc_t * disc, // disc pointer
ShowMode show_mode // what should be printed
)
{
DASSERT(f);
DASSERT(disc);
if ( !( disc->disc_attrib & WD_DA_GC_MULTIBOOT ) )
return;
//----- partitions
if ( show_mode & SHOW_P_INFO && disc->n_part )
{
wd_part_t * part = disc->part;
DASSERT(part);
int i;
u32 np = disc->n_part;
for ( i = 0; i < np; i++, part++ )
{
if ( !part->is_valid || !part->is_enabled )
continue;
fprintf(f,"\n%*sGameCube partition #%d, offset 0x%llx, size %s:\n",
indent,"", part->index,
(u64)part->part_off4 << 2,
wd_print_size_1024(0,0,part->part_size,false));
if (part->is_overlay)
fprintf(f,"%*s Partition overlays other partitions.\n", indent,"" );
fprintf(f,"%*s Partition type: %s\n",
indent, "",
wd_print_part_name(0,0,part->part_type,WD_PNAME_NUM_INFO) );
fprintf(f,"%*s boot.bin, title: %.64s\n",
indent, "", part->boot.dhead.disc_title);
ccp title = GetTitle(part->boot.dhead.id6.id6,0);
if (title)
fprintf(f,"%*s Title DB: %s\n", indent, "", title );
fprintf(f,"%*s Region: %7u [%s]\n",indent,"",
part->region, GetRegionName(part->region,"?") );
fprintf(f,"%*s Directories: %7u\n",indent,"",part->fst_dir_count);
fprintf(f,"%*s Files: %7u\n",indent,"",part->fst_file_count);
}
}
}
///////////////////////////////////////////////////////////////////////////////
static void dump_wii_part
(
FILE * f, // valid output stream
int indent, // indent of output
wd_disc_t * disc, // disc pointer
ShowMode show_mode // what should be printed
)
{
DASSERT(f);
DASSERT(disc);
int i;
u32 nt = disc->n_ptab;
u32 np = disc->n_part;
char pname[100];
if ( show_mode & SHOW_P_TAB && wd_disc_has_ptab(disc) )
{
ccp sep = " -------------------------------------------"
"---------------------------------------------";
fprintf(f,"\n%*s%d partition table%s with %d partition%s%s:\n\n"
"%*s index type offset .. end off size/hex = size/dec = MiB status\n"
"%*s%s\n",
indent,"", nt, nt == 1 ? "" : "s",
np, np == 1 ? "" : "s",
disc->patch_ptab_recommended ? " (patching recommended)" : "",
indent,"", indent,"", sep );
wd_ptab_info_t * pc = disc->ptab_info;
for ( i = 0; i < WII_MAX_PTAB; i++, pc++ )
{
const u32 np = ntohl(pc->n_part);
if (np)
{
const u64 off = (u64)ntohl(pc->off4) << 2;
const u32 size = np * sizeof(wd_ptab_entry_t);
fprintf(f,
"%*s%4d part.tab%10llx ..%10llx %10x =%11u %u partition%s\n",
indent,"", i, off, off+size, size, size,
np, np == 1 ? "" : "s" );
}
}
fprintf(f,"%*s%s\n",indent,"",sep);
for ( i = 0; i < np; i++ )
{
wd_part_t * part = disc->part + i;
wd_print_part_name(pname,sizeof(pname),part->part_type,WD_PNAME_COLUMN_9);
const u64 off = (u64)part->part_off4 << 2;
if (part->is_valid)
{
const u64 size = part->part_size;
fprintf(f,"%*s%4d.%-2d %s %9llx ..%10llx %10llx =%11llu =%5llu %s\n",
indent,"", part->ptab_index, part->ptab_part_index,
pname, off, off + size, size, size, (size+MiB/2)/MiB,
wd_print_part_status(0,0,part,true) );
}
else
fprintf(f,"%*s%4d.%-2d %s %9llx "
"** INVALID PARTITION ** invalid\n",
indent,"", part->ptab_index, part->ptab_part_index,
pname, off );
}
fprintf(f,"%*s%s\n",indent,"",sep);
}
//----- partitions
if ( show_mode & SHOW__PART && disc->n_part && wd_disc_has_ptab(disc) )
{
for ( i = 0; i < np; i++ )
{
wd_part_t * part = disc->part + i;
if ( !part->is_valid || !part->is_enabled )
continue;
const wd_part_header_t * ph = &part->ph;
wd_print_part_name(pname,sizeof(pname),part->part_type,WD_PNAME_NUM_INFO);
fprintf(f,"\n%*sPartition table #%d, partition #%d, type %s:\n",
indent,"", part->ptab_index, part->ptab_part_index, pname );
if ( show_mode & SHOW_P_INFO )
{
if (part->is_overlay)
fprintf(f,"%*s Partition overlays other partitions.\n", indent,"" );
if ( tmd_is_marked_not_encrypted(part->tmd) )
fprintf(f,"%*s Partition is marked as 'not encrypted'.\n", indent,"" );
else
fprintf(f,"%*s %s\n", indent,"", wd_print_sig_status(0,0,part,false,true) );
u8 * p8 = part->key;
fprintf(f,"%*s Partition key: %02x%02x%02x%02x %02x%02x%02x%02x"
" %02x%02x%02x%02x %02x%02x%02x%02x\n",
indent,"",
p8[0], p8[1], p8[2], p8[3], p8[4], p8[5], p8[6], p8[7],
p8[8], p8[9], p8[10], p8[11], p8[12], p8[13], p8[14], p8[15] );
if ( !(show_mode & SHOW_F_PRIMARY) )
{
dump_id(f,indent+2,0,0,part,18);
fprintf(f,"%*s boot.bin, title: %.64s\n",
indent, "", part->boot.dhead.disc_title);
if ( !(show_mode & SHOW_TMD) && part->tmd )
dump_sys_version(f,indent+2,ntoh64(part->tmd->sys_version),18);
}
fprintf(f,"%*s Directories: %7u\n",indent,"",part->fst_dir_count);
fprintf(f,"%*s Files: %7u\n",indent,"",part->fst_file_count);
}
else if ( show_mode & SHOW_P_ID )
dump_id(f,indent+2,0,0,part,18);
if ( show_mode & SHOW_P_MAP )
{
const u64 off = (u64) part->part_off4 << 2;
dump_data(f,indent, off, 0, sizeof(*ph), "TIK:" );
dump_data(f,indent, off, ph->tmd_off4, ph->tmd_size, "TMD:" );
dump_data(f,indent, off, ph->cert_off4, ph->cert_size, "CERT:" );
dump_data(f,indent, off, ph->h3_off4, WII_H3_SIZE, "H3:" );
dump_data(f,indent, off, ph->data_off4, (u64)ph->data_size4<<2, "Data:" );
}
if ( show_mode & SHOW_CERT )
Dump_CERT(f,indent+2,wd_load_cert_chain(part,true),0);
if ( show_mode & SHOW_TICKET )
{
fprintf(f,"%*s Ticket:\n",indent,"");
Dump_TIK_MEM(f,indent+4,&ph->ticket,wd_get_cert_ticket_stat(part,false));
}
if ( show_mode & SHOW_TMD && part->tmd )
{
fprintf(f,"%*s TMD:\n",indent,"");
Dump_TMD_MEM(f,indent+4,part->tmd,100,wd_get_cert_tmd_stat(part,false));
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
enumError Dump_ISO
(
FILE * f, // valid output stream
int indent, // indent of output
SuperFile_t * sf, // file to dump
ccp real_path, // NULL or pointer to real path
ShowMode show_mode, // what should be printed
int dump_level // dump level: 0..3, ignored if show_mode is set
)
{
//----- setup
ASSERT(sf);
if ( !f || !sf->f.id6_dest[0] )
return ERR_OK;
sf->f.read_behind_eof = 2;
wd_disc_t * disc = OpenDiscSF(sf,true,true);
if ( show_mode & SHOW_F_PRIMARY )
disc = sf->disc1;
if (!disc)
return ERR_WDISC_NOT_FOUND;
const bool is_gc = disc->disc_type == WD_DT_GAMECUBE;
FilePattern_t * pat = GetDefaultFilePattern();
indent = NormalizeIndent(indent) + 2;
//----- options --show and --long
if ( show_mode & SHOW__DEFAULT )
{
show_mode = SHOW__ALL;
switch (dump_level)
{
case 0:
show_mode = SHOW_INTRO | SHOW_P_TAB;
break;
case 1:
show_mode &= ~(SHOW_D_MAP|SHOW_USAGE|SHOW_TICKET|SHOW_TMD);
// fall through
case 2:
show_mode &= ~SHOW_FILES;
// fall through
case 3:
show_mode &= ~(SHOW_PATCH|SHOW_RELOCATE);
break;
}
show_mode |= SHOW_F_HEAD;
if (pat->is_active)
show_mode |= SHOW_FILES;
}
if ( show_mode & SHOW_F_PRIMARY )
show_mode &= ~(SHOW_TICKET|SHOW_TMD); // TICKET and TMD data is patched
//----- print header
if ( show_mode & SHOW_INTRO )
{
dump_header(f,indent-2,sf,disc->dhead.id6.id6,real_path,disc);
fprintf(f,"%*sDisc name: %.64s\n",indent,"",disc->dhead.disc_title);
ccp title = GetTitle((ccp)&disc->dhead,0);
if (title)
fprintf(f,"%*sDB title: %s\n",indent,"",title);
const RegionInfo_t * rinfo = GetRegionInfo(disc->dhead.id6.region_code);
fprintf(f,"%*sID Region: %s [%s]\n",indent,"",rinfo->name,rinfo->name4);
if (is_gc)
{
const enumRegion reg = ntohl(disc->region.region);
fprintf(f,"%*sBI2 Region: %x [%s]\n",
indent,"", reg, GetRegionName(reg,"?") );
}
else
{
const enumRegion reg = ntohl(disc->region.region);
fprintf(f,"%*sRegion setting: %x [%s] / %s\n",
indent,"", reg, GetRegionName(reg,"?"),
wd_print_age_rating(0,0,disc->region.age_rating) );
if (disc)
{
if (disc->system_menu)
fprintf(f,"%*sSystem menu: v%u = %s\n",
indent,"", disc->system_menu,
GetSystemMenu(disc->system_menu,"?") );
if ( disc->main_part && disc->main_part->tmd )
dump_sys_version(f,indent,ntoh64(disc->main_part->tmd->sys_version),19);
}
}
wd_calc_disc_status(disc,true);
fprintf(f,"%*sPartitions: %7u [%s]\n",
indent,"", disc->n_part,
wd_print_sector_status(0,0,disc->sector_stat,disc->cert_summary) );
fprintf(f,"%*sDirectories: %7u\n",indent,"",disc->fst_dir_count);
fprintf(f,"%*sFiles: %7u\n",indent,"",disc->fst_file_count);
}
else
{
fprintf(f,"\n%*sDump of file %s\n",indent-2,"",sf->f.fname);
if ( show_mode & SHOW_D_ID )
dump_id(f,indent,disc->dhead.id6.id6,sf->wbfs_id6,disc->main_part,19);
}
//----- partition tables & partitions
if ( disc->disc_type == WD_DT_GAMECUBE )
dump_gc_part(f,indent,disc,show_mode);
else
dump_wii_part(f,indent,disc,show_mode);
//----- FST (files)
if ( show_mode & SHOW_FILES )
{
SetupFilePattern(pat);
const u32 used_mib = ( wd_count_used_disc_blocks(disc,1,0)
+ WII_SECTORS_PER_MIB/2 ) / WII_SECTORS_PER_MIB;
fprintf(f,"\n\n%*s%u director%s with %u file%s, disk usage %u MiB:\n\n",
indent,"",
disc->fst_dir_count, disc->fst_dir_count==1 ? "y" : "ies",
disc->fst_file_count, disc->fst_file_count==1 ? "" : "s",
used_mib );
wd_print_fst( f, indent+2, disc,
prefix_mode == WD_IPM_DEFAULT ? WD_IPM_PART_NAME : prefix_mode,
ConvertShow2PFST(show_mode,SHOW__ALL&~SHOW_UNUSED) | WD_PFST_PART,
pat->match_all ? 0 : MatchFilePatternFST, pat );
}
//----- disc usage
if ( show_mode & SHOW_USAGE )
{
fprintf(f,"\n\n%*sSector Usage Map:\n\n",indent,"");
wd_filter_usage_table(disc,wdisc_usage_tab,0);
wd_print_usage_tab(f,indent+2,wdisc_usage_tab,disc->iso_size,false);
}
//----- memory map
if ( show_mode & SHOW_D_MAP )
{
MemMap_t mm;
InitializeMemMap(&mm);
InsertDiscMemMap(&mm,disc);
fprintf(f,"\n\n%*sISO Memory Map:\n\n",indent,"");
PrintMemMap(&mm,f,indent+2,0);
ResetMemMap(&mm);
}
if ( sf->disc1 && sf->disc1 != sf->disc2 )
{
//----- patching tables
if ( show_mode & SHOW_PATCH )
{
putc('\n',f);
wd_print_disc_patch(f,indent,sf->disc1,true,true);
}
//----- relocating table
if ( show_mode & SHOW_RELOCATE )
{
putc('\n',f);
wd_print_disc_relocation(f,indent,sf->disc1,true);
}
}
//----- terminate
if (disc->invalid_part)
{
fprintf(f,"\n\nWARNING: Disc contains %u invalid partition%s!\n\n",
disc->invalid_part, disc->invalid_part == 1 ? "" : "s" );
return ERR_WPART_INVALID;
}
putc('\n',f);
return ERR_OK;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Dump_DOL() ///////////////
///////////////////////////////////////////////////////////////////////////////
enumError Dump_DOL
(
FILE * f, // valid output stream
int indent, // indent of output
SuperFile_t * sf, // file to dump
ccp real_path, // NULL or pointer to real path
uint dump_mode // bit field:
// 1: print header
// 2: print file position table
// 4: print virtual position table
// 8: print virtual-to-file translation table
)
{
ASSERT(sf);
if (!f)
return ERR_OK;
if ( dump_mode & 1 )
indent = dump_header(f,indent,sf,0,real_path,0);
else
indent = NormalizeIndent(indent);
dol_header_t dol;
enumError err = ReadSF(sf,0,&dol,sizeof(dol));
if (!err)
{
ntoh_dol_header(&dol,&dol);
MemMap_t mm1, mm2, mm3;
InitializeMemMap(&mm1);
InitializeMemMap(&mm2);
InitializeMemMap(&mm3);
MemMapItem_t * mi = InsertMemMap(&mm1,0,sizeof(dol_header_t));
StringCopyS(mi->info,sizeof(mi->info),"DOL header");
mi = InsertMemMap(&mm1,sf->file_size,0);
StringCopyS(mi->info,sizeof(mi->info),"--- end of file ---");
int i;
for ( i = 0; i < DOL_N_SECTIONS; i++ )
{
const u32 size = dol.sect_size[i];
if (size)
{
const u32 off = dol.sect_off[i];
const u32 addr = dol.sect_addr[i];
char buf[sizeof(mi->info)], buf3[sizeof(mi->info)];
if ( i < DOL_N_TEXT_SECTIONS )
{
snprintf(buf,sizeof(buf),"text section T%u",i);
snprintf(buf3,sizeof(buf3),"%8x : T%u",addr-off,i);
}
else
{
const int j = i - DOL_N_TEXT_SECTIONS;
snprintf(buf,sizeof(buf),"data section D%u",j);
snprintf(buf3,sizeof(buf3),"%8x : D%u",addr-off,j);
}
mi = InsertMemMap(&mm1,off,size);
strcpy(mi->info,buf);
mi = InsertMemMap(&mm2,addr,size);
strcpy(mi->info,buf);
mi = InsertMemMap(&mm3,addr,size);
strcpy(mi->info,buf3);
}
}
if ( dump_mode & 2 )
{
fprintf(f,"\n%*sMemory map of DOL file:\n\n",indent,"");
PrintMemMap(&mm1,f,indent+3,"section");
}
if ( dump_mode & 4 )
{
mi = InsertMemMap(&mm2,dol.bss_addr,dol.bss_size);
snprintf(mi->info,sizeof(mi->info),"bss section");
mi = InsertMemMap(&mm2,dol.entry_addr,0);
snprintf(mi->info,sizeof(mi->info),"entry point");
fprintf(f,"\n%*sMemory map of DOL image:\n\n",indent,"");
mm2.begin = 0xffffffff;
PrintMemMap(&mm2,f,indent+3,"section");
}
if ( dump_mode & 8 )
{
mm3.begin = 0xffffffff;
fprintf(f,"\n%*sDelta between file offset and virtual address:\n\n",
indent,"" );
PrintMemMap(&mm3,f,indent+3," delta : section");
}
ResetMemMap(&mm1);
ResetMemMap(&mm2);
ResetMemMap(&mm3);
}
putc('\n',f);
return err;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// Dump_CERT_BIN() + Dump_CERT() ///////////////
///////////////////////////////////////////////////////////////////////////////
enumError Dump_CERT_BIN
(
FILE * f, // valid output stream
int indent, // indent of output
SuperFile_t * sf, // file to dump
ccp real_path, // NULL or pointer to real path
int print_ext // 0: off
// 1: print extended version
// 2: + hexdump the keys
// 3: + base64 the keys
)
{
ASSERT(sf);
if (!f)
return ERR_OK;
indent = dump_header(f,indent,sf,0,real_path,0);
u8 * buf = MALLOC(sf->file_size);
enumError err = ReadSF(sf,0,buf,sf->file_size);
if (!err)
{
putc('\n',f);
Dump_CERT_MEM(f,indent,buf,sf->file_size,print_ext);
}
FREE(buf);
return err;
}
///////////////////////////////////////////////////////////////////////////////
enumError Dump_CERT_MEM
(
FILE * f, // valid output stream
int indent, // indent of output
const u8 * cert_data, // valid pointer to cert data
size_t cert_size, // size of 'cert_data'
int print_ext // 0: off
// 1: print extended version
// 2: + hexdump the keys
// 3: + base64 the keys
)
{
ASSERT(f);
ASSERT(cert_data);
indent = NormalizeIndent(indent);
cert_chain_t cc;
cert_initialize(&cc);
cert_append_data(&cc,cert_data,cert_size,false);
enumError err = Dump_CERT(f,indent,&cc,print_ext);
cert_reset(&cc);
return err;
}
///////////////////////////////////////////////////////////////////////////////
enumError Dump_CERT
(
FILE * f, // valid output stream
int indent, // indent of output
const cert_chain_t * cc, // valid pinter to cert chain
int print_ext // 0: off
// 1: print extended version
// 2: + hexdump the keys
// 3: + base64 the keys
)
{
DASSERT(f);
DASSERT(cc);
indent = NormalizeIndent(indent);
if (!cc->used)
fprintf(f,"%*sNo certificates found!%s\n",
indent, "", print_ext ? "\n" : "" );
int i;
for ( i = 0; i < cc->used; i++ )
{
Dump_CERT_Item(f,indent,cc->cert+i,i,print_ext,cc);
if (print_ext)
fputc('\n',f);
}
return ERR_OK;
}
///////////////////////////////////////////////////////////////////////////////
void Dump_CERT_Item
(
FILE * f, // valid output stream
int indent, // normalized indent of output
const cert_item_t * item, // valid item pointer
int cert_index, // >=0: print title with certificate index
int print_ext, // 0: off
// 1: print extended version
// 2: + hexdump the keys
// 3: + base64 the keys
const cert_chain_t * cc // not NULL: verify signature
)
{
XDump_t xd;
InitializeXDumpEx(&xd,f,indent+4,0,4,16);
xd.print_text = false;
if ( cert_index >= 0)
{
fprintf(f,"%*sCertificate #%u, size=0x%x=%u\n",
indent, "", cert_index,
item->cert_size, item->cert_size );
indent += 2;
}
fprintf(f,"%*sIssuer & Key ID: %s\n", indent, "", item->name );
if (item->head)
{
const cert_stat_t stat = cc ? cert_check_cert(cc,item,0) : 0;
dump_sig_type(f,indent,ntohl(item->head->sig_type),stat,false);
}
dump_sig_type(f,indent,ntohl(item->data->key_type),0,true);
if (print_ext)
{
const u32 pub_exp = be32(item->data->public_key+item->key_size);
fprintf(f,"%*sPublic exponent: %11x/hex =%11u\n",indent,"",pub_exp,pub_exp);
if ( print_ext > 1 )
{
if ( print_ext > 2 )
{
fprintf(f,"%*sPublic key (base64):\n",indent,"");
PrintEncode64(f,item->data->public_key,item->key_size,indent+5,70);
}
else
{
fprintf(f,"%*sPublic key (hex):\n",indent,"");
XDump(&xd,item->data->public_key,item->key_size,true);
}