1 2 /*************************************************************************** 3 * __ __ _ ___________ * 4 * \ \ / /| |____ ____| * 5 * \ \ / / | | | | * 6 * \ \ /\ / / | | | | * 7 * \ \/ \/ / | | | | * 8 * \ /\ / | | | | * 9 * \/ \/ |_| |_| * 10 * * 11 * Wiimms ISO Tools * 12 * http://wit.wiimm.de/ * 13 * * 14 *************************************************************************** 15 * * 16 * This file is part of the WIT project. * 17 * Visit http://wit.wiimm.de/ for project details and sources. * 18 * * 19 * Copyright (c) 2009-2013 by Dirk Clemens <wiimm@wiimm.de> * 20 * * 21 *************************************************************************** 22 * * 23 * This program is free software; you can redistribute it and/or modify * 24 * it under the terms of the GNU General Public License as published by * 25 * the Free Software Foundation; either version 2 of the License, or * 26 * (at your option) any later version. * 27 * * 28 * This program is distributed in the hope that it will be useful, * 29 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 31 * GNU General Public License for more details. * 32 * * 33 * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt * 34 * * 35 ***************************************************************************/ 36 37 #ifndef WIIDISC_H 38 #define WIIDISC_H 39 40 #include "cert.h" 41 #include "rijndael.h" 42 43 // 44 /////////////////////////////////////////////////////////////////////////////// 45 /////////////// Introduction /////////////// 46 /////////////////////////////////////////////////////////////////////////////// 47 48 /**************************************************************************** 49 * * 50 * This is a new wiidisc lib written by Dirk Clemens. It support GameCube * 51 * and Wii ISO images. If the name of a offset or size variable ends with * 52 * a '4' than the real value must be calculated with: (u64)value4<<2 * 53 * But this not true for GameCube discs. GameCube discs store the * 54 * unshifted value. * 55 * * 56 ****************************************************************************/ 57 58 // 59 /////////////////////////////////////////////////////////////////////////////// 60 /////////////// enum wd_part_type_t /////////////// 61 /////////////////////////////////////////////////////////////////////////////// 62 63 typedef enum wd_part_type_t // known partition types 64 { 65 WD_PART_DATA = 0, // DATA partition 66 WD_PART_UPDATE = 1, // UPDATE partition 67 WD_PART_CHANNEL = 2, // CHANNEL partition 68 69 } wd_part_type_t; 70 71 // 72 /////////////////////////////////////////////////////////////////////////////// 73 /////////////// enum wd_ckey_index_t /////////////// 74 /////////////////////////////////////////////////////////////////////////////// 75 76 typedef enum wd_ckey_index_t // known partition types 77 { 78 WD_CKEY_STANDARD, // standard common key 79 WD_CKEY_KOREA, // common key for korea 80 WD_CKEY__N // number of common keys 81 82 } wd_ckey_index_t; 83 84 // 85 /////////////////////////////////////////////////////////////////////////////// 86 /////////////// enum wd_select_mode_t /////////////// 87 /////////////////////////////////////////////////////////////////////////////// 88 89 typedef enum wd_select_mode_t // modes of selection 90 { 91 WD_SM_IGNORE, // ignore this selection 92 93 //--- allow modes 94 95 WD_SM_ALLOW_PTYPE, // allow partition type 96 WD_SM_ALLOW_PTAB, // allow partition table 97 WD_SM_ALLOW_INDEX, // allow partition #index in absolut partition count 98 WD_SM_ALLOW_LT_INDEX, // allow partitions < #index in absolut partition count 99 WD_SM_ALLOW_GT_INDEX, // allow partitions > #index in absolut partition count 100 WD_SM_ALLOW_PTAB_INDEX, // allow partition #index in given table 101 WD_SM_ALLOW_ID, // allow ID 102 WD_SM_ALLOW_GC_BOOT, // allow GameCube boot partition 103 WD_SM_ALLOW_ALL, // allow all partitions 104 105 WD_SM_N_MODE, // number of modes 106 107 //--- the deny flag and masks 108 109 WD_SM_F_DENY = 0x100, // this bit is set for DENY modes 110 WD_SM_M_MODE = 0x0ff, // mask for base modes 111 112 //--- deny modes 113 114 WD_SM_DENY_PTYPE = WD_SM_F_DENY | WD_SM_ALLOW_PTYPE, 115 WD_SM_DENY_PTAB = WD_SM_F_DENY | WD_SM_ALLOW_PTAB, 116 WD_SM_DENY_INDEX = WD_SM_F_DENY | WD_SM_ALLOW_INDEX, 117 WD_SM_DENY_LT_INDEX = WD_SM_F_DENY | WD_SM_ALLOW_LT_INDEX, 118 WD_SM_DENY_GT_INDEX = WD_SM_F_DENY | WD_SM_ALLOW_GT_INDEX, 119 WD_SM_DENY_PTAB_INDEX = WD_SM_F_DENY | WD_SM_ALLOW_PTAB_INDEX, 120 WD_SM_DENY_ID = WD_SM_F_DENY | WD_SM_ALLOW_ID, 121 WD_SM_DENY_GC_BOOT = WD_SM_F_DENY | WD_SM_ALLOW_GC_BOOT, 122 WD_SM_DENY_ALL = WD_SM_F_DENY | WD_SM_ALLOW_ALL, 123 124 } wd_select_mode_t; 125 126 // 127 /////////////////////////////////////////////////////////////////////////////// 128 /////////////// enum wd_trim_mode_t /////////////// 129 /////////////////////////////////////////////////////////////////////////////// 130 131 typedef enum wd_trim_mode_t 132 { 133 //--- main trimming modes 134 135 WD_TRIM_DEFAULT = 0x001, // default mode (no user value) 136 137 WD_TRIM_DISC = 0x002, // trim disc: move whole partitions 138 WD_TRIM_PART = 0x004, // trim partition: move sectors 139 WD_TRIM_FST = 0x008, // trim filesystem: move files 140 141 WD_TRIM_NONE = 0x000, 142 WD_TRIM_ALL = WD_TRIM_DISC | WD_TRIM_PART | WD_TRIM_FST, 143 WD_TRIM_FAST = WD_TRIM_DISC | WD_TRIM_PART, 144 145 //--- trimming flags 146 147 WD_TRIM_F_END = 0x100, // flags for WD_TRIM_DISC: move to disc end 148 149 WD_TRIM_M_FLAGS = 0x100, 150 151 //--- all valid bits 152 153 WD_TRIM_M_ALL = WD_TRIM_DEFAULT | WD_TRIM_ALL | WD_TRIM_M_FLAGS 154 155 } wd_trim_mode_t; 156 157 // 158 /////////////////////////////////////////////////////////////////////////////// 159 /////////////// enum wd_icm_t /////////////// 160 /////////////////////////////////////////////////////////////////////////////// 161 162 typedef enum wd_icm_t // iterator call mode 163 { 164 WD_ICM_OPEN_PART, // called for each openening of a partition 165 WD_ICM_CLOSE_PART, // called if partitions closed 166 167 WD_ICM_DIRECTORY, // item is a directory; this is the first non hint message 168 169 WD_ICM_FILE, // item is a partition file -> extract with key 170 WD_ICM_COPY, // item is a disc area -> disc raw copy 171 WD_ICM_DATA, // item contains pure data 172 173 } wd_icm_t; 174 175 // 176 /////////////////////////////////////////////////////////////////////////////// 177 /////////////// enum wd_ipm_t /////////////// 178 /////////////////////////////////////////////////////////////////////////////// 179 180 typedef enum wd_ipm_t // iterator prefix mode 181 { 182 WD_IPM_DEFAULT, // not defined -> like AUTO in wiidisc 183 WD_IPM_AUTO, // for single partitions: WD_IPM_PART_NAME, else: WD_IPM_POINT 184 185 WD_IPM_NONE, // no prefix: "" 186 WD_IPM_SLASH, // prefix with "/" 187 WD_IPM_POINT, // prefix with "./" 188 WD_IPM_PART_ID, // prefix with 'P' and partition id: "P%u/" 189 WD_IPM_PART_NAME, // prefix with partition name or "P<id>": "NAME/" 190 WD_IPM_PART_INDEX, // prefix with 'P' and table + partition index: "P%u.%u/" 191 WD_IPM_COMBI, // WD_IPM_PART_INDEX + ID|NAME: "P%u.%u-%s/" 192 193 } wd_ipm_t; 194 195 // 196 /////////////////////////////////////////////////////////////////////////////// 197 /////////////// enum wd_pfst_t /////////////// 198 /////////////////////////////////////////////////////////////////////////////// 199 200 typedef enum wd_pfst_t // print-fst mode 201 { 202 WD_PFST_HEADER = 0x01, // print table header 203 WD_PFST_PART = 0x02, // print partition intro 204 WD_PFST_UNUSED = 0x04, // print unused area 205 WD_PFST_OFFSET = 0x08, // print offset 206 WD_PFST_SIZE_HEX = 0x10, // print size in hex 207 WD_PFST_SIZE_DEC = 0x20, // print size in dec 208 209 WD_PFST__ALL = 0x3f, 210 211 WD_PFST__OFF_SIZE = WD_PFST_UNUSED 212 | WD_PFST_OFFSET 213 | WD_PFST_SIZE_HEX 214 | WD_PFST_SIZE_DEC, 215 216 } wd_pfst_t; 217 218 // 219 /////////////////////////////////////////////////////////////////////////////// 220 /////////////// enum wd_sector_status_t /////////////// 221 /////////////////////////////////////////////////////////////////////////////// 222 223 typedef enum wd_sector_status_t 224 { 225 //----- cleared status 226 227 WD_SS_HASH_CLEARED = 0x0001, // hash area cleard with any constant 228 WD_SS_DATA_CLEARED = 0x0002, // data area cleard with any constant 229 WD_SS_SECTOR_CLEARED = 0x0004, // complete sector cleard with any constant 230 // if set: WD_SS_HASH_CLEARED 231 // & WD_SS_DATA_CLEARED are set too 232 233 WD_SS_HASH_ZEROED = 0x0010, // hash area zeroed ==> WD_SS_HASH_CLEARED 234 WD_SS_DATA_ZEROED = 0x0020, // data area zeroed ==> WD_SS_DATA_CLEARED 235 236 WD_SS_M_CLEARED = WD_SS_HASH_CLEARED 237 | WD_SS_DATA_CLEARED 238 | WD_SS_SECTOR_CLEARED 239 | WD_SS_HASH_ZEROED 240 | WD_SS_DATA_ZEROED, 241 242 //----- encryption status 243 244 WD_SS_ENCRYPTED = 0x0040, // sector encrypted (decrypted H0 hash is ok) 245 WD_SS_DECRYPTED = 0x0080, // sector decrypted (H0 hash is ok) 246 247 WD_SS_M_CRYPT = WD_SS_ENCRYPTED 248 | WD_SS_DECRYPTED, 249 250 //----- flags 251 252 WD_SS_F_PART_DATA = 0x0100, // sector contains partition data 253 WD_SS_F_SCRUBBED = 0x0200, // sector is scrubbed 254 WD_SS_F_SKELETON = 0x0400, // sector is marked 'SKELETON' 255 256 WD_SS_M_FLAGS = WD_SS_F_PART_DATA 257 | WD_SS_F_SCRUBBED 258 | WD_SS_F_SKELETON, 259 260 //----- error status 261 262 WD_SS_INVALID_SECTOR = 0x1000, // invalid sector index 263 WD_SS_READ_ERROR = 0x2000, // error while reading sector 264 265 WD_SS_M_ERROR = WD_SS_INVALID_SECTOR 266 | WD_SS_READ_ERROR, 267 268 } wd_sector_status_t; 269 270 // 271 /////////////////////////////////////////////////////////////////////////////// 272 /////////////// enum wd_scrubbed_t /////////////// 273 /////////////////////////////////////////////////////////////////////////////// 274 275 typedef enum wd_scrubbed_t // scrubbing mode 276 { 277 WD_SCRUBBED_NONE = 0, 278 WD_SCRUBBED_HASH = 1, 279 WD_SCRUBBED_DATA = 2, 280 WD_SCRUBBED_ALL = WD_SCRUBBED_HASH | WD_SCRUBBED_DATA, 281 282 } wd_scrubbed_t; 283 284 // 285 /////////////////////////////////////////////////////////////////////////////// 286 /////////////// enum wd_usage_t /////////////// 287 /////////////////////////////////////////////////////////////////////////////// 288 289 typedef enum wd_usage_t // usage table values 290 { 291 WD_USAGE_UNUSED, // block is not used, always = 0 292 WD_USAGE_DISC, // block is used for disc managment 293 WD_USAGE_PART_0, // index for first partition 294 295 WD_USAGE__MASK = 0x7f, // mask for codes above 296 WD_USAGE_F_CRYPT = 0x80, // flag: encryption candidate 297 298 } wd_usage_t; 299 300 // 301 /////////////////////////////////////////////////////////////////////////////// 302 /////////////// enum wd_pname_mode_t /////////////// 303 /////////////////////////////////////////////////////////////////////////////// 304 305 typedef enum wd_pname_mode_t // partition name modes 306 { 307 WD_PNAME_NAME_NUM_9, // NAME+NUM if possible, ID or NUM else, fw=9 308 WD_PNAME_COLUMN_9, // NAME+NUM|NUM|ID, fw=9 309 WD_PNAME_NUM, // ID or NUM 310 WD_PNAME_NAME, // NAME if possible, ID or NUM else 311 WD_PNAME_P_NAME, // NAME if possible, 'P-'ID or 'P'NUM else 312 WD_PNAME_NUM_INFO, // NUM + '[INFO]' 313 314 WD_PNAME__N // number of supported formats 315 316 } wd_pname_mode_t; 317 318 // 319 /////////////////////////////////////////////////////////////////////////////// 320 /////////////// enum wd_patch_mode_t /////////////// 321 /////////////////////////////////////////////////////////////////////////////// 322 323 typedef enum wd_patch_mode_t // patching modes 324 { 325 WD_PAT_IGNORE = 0, // non used value, always zero 326 327 WD_PAT_ZERO, // zero data area 328 WD_PAT_DATA, // known raw data 329 WD_PAT_PART_TICKET, // partition TICKET (-> fake sign) 330 WD_PAT_PART_TMD, // partition TMD (-> fake sign) 331 332 WD_PAT__N // number of valid codes 333 334 } wd_patch_mode_t; 335 336 // 337 /////////////////////////////////////////////////////////////////////////////// 338 /////////////// enum wd_reloc_t /////////////// 339 /////////////////////////////////////////////////////////////////////////////// 340 341 typedef enum wd_reloc_t // relocation modes 342 { 343 WD_RELOC_M_DELTA = 0x7ffff, // mask for source sector delta 344 345 WD_RELOC_M_PART = 0x3f80000, // mask for partition index 346 WD_RELOC_S_PART = 19, // shift for partition index 347 348 WD_RELOC_F_ENCRYPT = 0x04000000, // set if partition data, encrypted 349 WD_RELOC_F_DECRYPT = 0x08000000, // set if partition data, decrypted 350 WD_RELOC_F_COPY = 0x10000000, // set if source must be copied 351 WD_RELOC_F_PATCH = 0x20000000, // set if patching is needed 352 WD_RELOC_F_HASH = 0x40000000, // set if hash calculation is needed 353 WD_RELOC_F_LAST = 0x80000000, // set if must be copied after all other 354 WD_RELOC_M_FLAGS = 0xfc000000, // sum of all flags 355 356 WD_RELOC_M_CRYPT = WD_RELOC_F_ENCRYPT | WD_RELOC_F_DECRYPT, 357 358 WD_RELOC_M_DISCLOAD = WD_RELOC_M_DELTA 359 | WD_RELOC_M_PART 360 | WD_RELOC_F_ENCRYPT 361 | WD_RELOC_F_DECRYPT 362 | WD_RELOC_F_COPY, 363 364 WD_RELOC_M_PARTLOAD = WD_RELOC_M_DELTA 365 | WD_RELOC_M_PART 366 | WD_RELOC_F_ENCRYPT 367 | WD_RELOC_F_DECRYPT, 368 369 } wd_reloc_t; 370 371 // 372 /////////////////////////////////////////////////////////////////////////////// 373 /////////////// enum wd_modify_t /////////////// 374 /////////////////////////////////////////////////////////////////////////////// 375 376 typedef enum wd_modify_t // objects to modify 377 { 378 //--- standard values 379 380 WD_MODIFY_DISC = 0x001, // modify disc header 381 WD_MODIFY_BOOT = 0x002, // modify boot.bin 382 WD_MODIFY_TICKET = 0x004, // modify ticket.bin 383 WD_MODIFY_TMD = 0x008, // modify tmd.bin 384 385 //--- for external extensions 386 387 WD_MODIFY_WBFS = 0x010, // modify WBFS inode [obsolete?] 388 389 //--- special flags 390 391 WD_MODIFY__NONE = 0, // modify nothing 392 WD_MODIFY__AUTO = 0x100, // automatic mode 393 WD_MODIFY__ALWAYS = 0x200, // this bit is always set 394 395 //--- summary 396 397 WD_MODIFY__ALL = 0x01f, // modify all 398 WD_MODIFY__MASK = WD_MODIFY__ALL | WD_MODIFY__AUTO | WD_MODIFY__ALWAYS, 399 400 } wd_modify_t; 401 402 // 403 /////////////////////////////////////////////////////////////////////////////// 404 /////////////// callback functions /////////////// 405 /////////////////////////////////////////////////////////////////////////////// 406 407 struct wd_disc_t; 408 struct wd_part_t; 409 struct wd_iterator_t; 410 struct wd_memmap_t; 411 struct wd_memmap_item_t; 412 413 //----------------------------------------------------------------------------- 414 // Callback read function. Returns 0 on success and any other value on failutre. 415 416 typedef int (*wd_read_func_t) 417 ( 418 void * read_data, // user defined data 419 u32 offset4, // offset/4 to read 420 u32 count, // num of bytes to read 421 void * iobuf // buffer, alloced with wbfs_ioalloc 422 ); 423 424 //----------------------------------------------------------------------------- 425 // Callback definition for memory dump with wd_print_mem() 426 427 typedef void (*wd_mem_func_t) 428 ( 429 void * param, // user defined parameter 430 u64 offset, // offset of object 431 u64 size, // size of object 432 ccp info // info about object 433 ); 434 435 //----------------------------------------------------------------------------- 436 // Callback definition for file iteration. if return != 0 => abort 437 438 typedef int (*wd_file_func_t) 439 ( 440 struct wd_iterator_t *it // iterator struct with all infos 441 ); 442 443 //----------------------------------------------------------------------------- 444 // Callback definition for wd_insert_memmap_disc_part() & wd_insert_memmap_part() 445 446 typedef void (*wd_memmap_func_t) 447 ( 448 void * param, // user defined parameter 449 struct wd_memmap_t * patch, // valid pointer to patch object 450 struct wd_memmap_item_t * item // valid pointer to inserted item 451 ); 452 453 // 454 /////////////////////////////////////////////////////////////////////////////// 455 /////////////// struct wd_select_item_t /////////////// 456 /////////////////////////////////////////////////////////////////////////////// 457 458 typedef struct wd_select_item_t // a select sub item 459 { 460 wd_select_mode_t mode; // select mode 461 u32 table; // partition table 462 u32 part; // partition type or index 463 464 } wd_select_item_t; 465 466 // 467 /////////////////////////////////////////////////////////////////////////////// 468 /////////////// struct wd_select_t /////////////// 469 /////////////////////////////////////////////////////////////////////////////// 470 471 typedef struct wd_select_t // a selector 472 { 473 bool whole_disc; // flag: copy whole disc, ignore others 474 bool whole_part; // flag: copy whole partitions 475 476 u32 used; // number of used elements in list 477 u32 size; // number of alloced elements in list 478 wd_select_item_t * list; // list of select items 479 480 } wd_select_t; 481 482 // 483 /////////////////////////////////////////////////////////////////////////////// 484 /////////////// struct wd_memmap_item_t /////////////// 485 /////////////////////////////////////////////////////////////////////////////// 486 487 typedef struct wd_memmap_item_t // patching data item 488 { 489 u32 mode; // memmap mode (e.g. wd_patch_mode_t) 490 u32 index; // user defined index (e.g. partition index) 491 u64 offset; // offset 492 u64 size; // size 493 void * data; // NULL or pointer to data/filename 494 bool data_alloced; // true if data must be freed 495 char info[51]; // comment for dumps 496 497 } wd_memmap_item_t; 498 499 // 500 /////////////////////////////////////////////////////////////////////////////// 501 /////////////// struct wd_memmap_t /////////////// 502 /////////////////////////////////////////////////////////////////////////////// 503 504 typedef struct wd_memmap_t // patching data structure 505 { 506 wd_memmap_item_t * item; // pointer to first item 507 u32 used; // number of used items 508 u32 size; // number of allocated items 509 510 } wd_memmap_t; 511 512 // 513 /////////////////////////////////////////////////////////////////////////////// 514 /////////////// struct wd_file_t /////////////// 515 /////////////////////////////////////////////////////////////////////////////// 516 517 typedef struct wd_file_t 518 { 519 u32 src_off4; // offset of source in 4*bytes steps 520 u32 dest_off4; // offset of dest in 4*bytes steps 521 u32 size; // size of file 522 ccp iso_path; // path within iso image, never NULL 523 ccp file_path; // NULL or path of file to load 524 525 // For directories all values but iso_path are NULL 526 // and the 'iso_path' terminates with a slash ('/') 527 // For files the value of 'dest_off4' is never NULL. 528 // Use the function wd_file_is_directory() to distinguish between both. 529 530 } wd_file_t; 531 532 533 // 534 /////////////////////////////////////////////////////////////////////////////// 535 /////////////// struct wd_file_list_t /////////////// 536 /////////////////////////////////////////////////////////////////////////////// 537 538 typedef struct wd_file_list_t 539 { 540 wd_file_t * file; // list with 'size' files 541 u32 used; // number of used and 542 // initialized elements in 'list' 543 u32 size; // number of alloced elements in 'list' 544 545 } wd_file_list_t; 546 547 // 548 /////////////////////////////////////////////////////////////////////////////// 549 /////////////// struct wd_part_t /////////////// 550 /////////////////////////////////////////////////////////////////////////////// 551 552 typedef struct wd_part_t 553 { 554 //----- base infos 555 556 int index; // zero based index within wd_disc_t 557 wd_usage_t usage_id; // marker for usage_table 558 int ptab_index; // zero based index of owning partition table 559 int ptab_part_index;// zero based index within owning partition table 560 u32 part_type; // partition type 561 562 u32 part_off4; // offset/4 of partition relative to disc start 563 u64 max_marked; // max marked offset relative to disc start 564 u64 part_size; // total size of partition 565 566 struct wd_disc_t * disc; // pointer to disc 567 wd_memmap_t patch; // patching data 568 569 //----- partition status 570 571 bool is_loaded; // true if this partition info was loaded 572 bool is_valid; // true if this partition is valid 573 bool is_enabled; // true if this partition is enabled 574 bool is_ok; // true if is_loaded && is_valid && is_enabled 575 bool is_encrypted; // true if this partition is encrypted 576 bool is_overlay; // true if this partition overlays other partitions 577 bool is_gc; // true for GC partition => no crypt, no hash 578 579 wd_sector_status_t sector_stat; // sector status: OR'ed for different sectors 580 bool scrub_test_done;// true if scrubbing test already done 581 582 //----- to do status 583 584 bool sign_ticket; // true if ticket must be signed 585 bool sign_tmd; // true if tmd must be signed 586 bool h3_dirty; // true if h3 is dirty -> calc h4 and sign tmd 587 588 589 //----- partition data, only valid if 'is_valid' is true 590 591 wd_part_header_t ph // partition header (incl. ticket), host endian 592 __attribute__ ((aligned(4))); 593 wd_tmd_t * tmd; // NULL or pointer to tmd, size = ph.tmd_size 594 u8 * cert; // NULL or pointer to cert, size = ph.cert_size 595 u8 * h3; // NULL or pointer to h3, size = WII_H3_SIZE 596 597 char * setup_txt; // NULL or pointer to content of file setup.txt 598 u32 setup_txt_len; // = strlen(setup_txt) 599 char * setup_sh; // NULL or pointer to content of file setup.sh 600 u32 setup_sh_len; // = strlen(setup_sh) 601 char * setup_bat; // NULL or pointer to content of file setup.bat 602 u32 setup_bat_len; // = strlen(setup_bat) 603 604 cert_chain_t cert_chain; // list of ceritificates 605 cert_stat_t cert_tik_stat; // certificate state of ticket, NULL=not set 606 cert_stat_t cert_tmd_stat; // certificate state of tmd, NULL=not set 607 608 u8 key[WII_KEY_SIZE]; 609 // partition key, needed to build aes key 610 u32 data_off4; // offset/4 of partition data relative to disc start 611 u32 data_sector; // index of first data sector 612 u32 end_mgr_sector; // index of last management sector + 1 613 u32 end_sector; // index of last data sector + 1 614 615 wd_boot_t boot; // copy of boot.bin, host endian 616 u32 dol_size; // size of main.dol 617 u32 apl_size; // size of apploader.img 618 u32 region; // gamecube region code, extract of bi2 619 620 wd_fst_item_t * fst; // pointer to fst data 621 u32 fst_n; // number or elements in fst 622 u32 fst_max_off4; // informative: maximal offset4 value of all files 623 u32 fst_max_size; // informative: maximal size value of all files 624 u32 fst_dir_count; // informative: number or directories in fst 625 u32 fst_file_count; // informative: number or real files in fst 626 627 } wd_part_t; 628 629 // 630 /////////////////////////////////////////////////////////////////////////////// 631 /////////////// struct wd_disc_t /////////////// 632 /////////////////////////////////////////////////////////////////////////////// 633 634 typedef struct wd_disc_t 635 { 636 //----- open parameters 637 638 wd_read_func_t read_func; // read function, always valid 639 void * read_data; // data pointer for read function 640 u64 iso_size; // size of file, 0=unknown 641 int open_count; // open counter 642 int force; // >0: force opening even if invalid 643 644 //----- errror support 645 646 char error_term[200]; // termination string for error messages 647 648 //----- raw data 649 650 wd_disc_type_t disc_type; // disc type 651 wd_disc_attrib_t disc_attrib; // disc attrib 652 653 wd_header_t dhead; // copy of disc header 654 wd_region_t region; // copy of disc region settings 655 wd_ptab_info_t ptab_info[WII_MAX_PTAB]; 656 // copy of partion table info 657 wd_ptab_entry_t *ptab_entry; // collected copy of all partion entries 658 u32 magic2; // second magic @ WII_MAGIC2_OFF 659 660 //----- user data 661 662 ccp image_type; // NULL or string of image type 663 ccp image_ext; // NULL or recommended image extension 664 665 //----- patching data 666 667 bool whole_disc; // selection flag: copy whole disc (raw mode) 668 bool whole_part; // selection flag: copy whole partitions 669 wd_trim_mode_t trim_mode; // active trim mode 670 u32 trim_align; // alignment value for trimming 671 wd_memmap_t patch; // patching data 672 wd_ptab_t ptab; // partition tables / GC: copy of dhead 673 wd_reloc_t * reloc; // relocation data 674 675 //----- partitions 676 677 int n_ptab; // number of valid partition tables 678 int n_part; // number of partitions 679 wd_part_t * part; // list of partitions (N='n_part') 680 wd_part_t * data_part; // NULL or pointer to first DATA partition 681 wd_part_t * update_part; // NULL or pointer to first UPDATE partition 682 wd_part_t * channel_part; // NULL or pointer to first CHANNEL partition 683 wd_part_t * main_part; // NULL or pointer to main partition 684 // This is the first of: 685 // first DATA partition 686 // first UPDATE partition 687 // first CHANNEL partition 688 // first partition at all 689 u32 invalid_part; // number of invalid partitions 690 u32 fst_n; // informative: number or elements in fst 691 u32 fst_max_off4; // informative: maximal offset4 value of all files 692 u32 fst_max_size; // informative: maximal size value of all files 693 u32 fst_dir_count; // informative: number or directories in fst 694 u32 fst_file_count; // informative: number or real files in fst 695 u32 system_menu; // version of system menu file in update partition 696 697 cert_stat_t cert_summary; // summary of partitions stats 698 wd_sector_status_t sector_stat; // sector status: OR'ed for different sectors 699 bool scrub_test_done;// true if scrubbing test already done 700 bool have_overlays; // overlayed partitions found 701 bool patch_ptab_recommended; 702 // informative: patch ptab is recommended 703 704 //----- usage table 705 706 u8 usage_table[WII_MAX_SECTORS]; 707 // usage table of disc 708 int usage_max; // ( max used index of 'usage_table' ) + 1 709 710 //----- block cache 711 712 u8 block_buf[WII_SECTOR_DATA_SIZE]; 713 // cache for partition blocks 714 u32 block_num; // block number of last loaded 'block_buf' 715 wd_part_t * block_part; // partition of last loaded 'block_buf' 716 717 //----- akey cache 718 719 aes_key_t akey; // aes key for 'akey_part' 720 wd_part_t * akey_part; // partition of 'akey' 721 722 //----- temp buffer 723 724 u8 temp_buf[2*WII_SECTOR_SIZE]; // temp buffer for reading operations 725 726 u32 cache_sector; // sector number of 'cache' 727 u8 cache[WII_SECTOR_SIZE]; // cache for wd_read_and_patch() 728 729 wd_part_t * group_cache_part; // parttion of 'group_cache' 730 u32 group_cache_sector; // sector number of 'group_cache' 731 u8 * group_cache; // cache for sector groups 732 733 734 } wd_disc_t; 735 736 // 737 /////////////////////////////////////////////////////////////////////////////// 738 /////////////// struct wd_iterator_t /////////////// 739 /////////////////////////////////////////////////////////////////////////////// 740 741 typedef struct wd_iterator_t 742 { 743 //----- global parameters 744 745 void * param; // user defined parameter 746 wd_disc_t * disc; // valid disc pointer 747 wd_part_t * part; // valid disc partition pointer 748 749 //----- settings 750 751 bool select_mode; // true: run in select mode 752 // func() return 0: don't select 753 // func() return 1: select and mark 754 // func() return other: abort 755 756 //----- file specific parameters 757 758 wd_icm_t icm; // iterator call mode 759 u32 off4; // offset/4 to read (GC: offset/1) 760 u32 size; // size of object 761 const void * data; // NULL or pointer to data 762 wd_fst_item_t * fst_item; // NULL or pointer to FST item 763 764 //----- filename 765 766 wd_ipm_t prefix_mode; // prefix mode 767 char prefix[20]; // prefix of path 768 int prefix_len; // := strlen(prefix) 769 char path[2000]; // full path including prefix 770 char *fst_name; // pointer := path + prefix_len 771 772 } wd_iterator_t; 773 774 // 775 /////////////////////////////////////////////////////////////////////////////// 776 /////////////// struct wd_print_fst_t /////////////// 777 /////////////////////////////////////////////////////////////////////////////// 778 779 typedef struct wd_print_fst_t 780 { 781 //----- base data 782 783 FILE * f; // valid output file 784 int indent; // indention of the output 785 wd_pfst_t mode; // print mode 786 787 //----- field widths and opther helper data 788 789 int fw_unused; // field width or 0 if hidden 790 int fw_offset; // field width or 0 if hidden 791 int fw_size_dec; // field width or 0 if hidden 792 int fw_size_hex; // field width or 0 if hidden 793 794 u64 last_end; // 'offset + size' of last printed element 795 wd_icm_t last_icm; // 'icm' of last printed element 796 797 //----- filter function, used by wd_print_fst_item_wrapper() 798 799 wd_file_func_t filter_func; // NULL or filter function 800 void * filter_param; // user defined parameter 801 802 } wd_print_fst_t; 803 804 // 805 /////////////////////////////////////////////////////////////////////////////// 806 /////////////// interface: common key & encryption /////////////// 807 /////////////////////////////////////////////////////////////////////////////// 808 809 const u8 * wd_get_common_key 810 ( 811 wd_ckey_index_t ckey_index // index of common key 812 ); 813 814 //----------------------------------------------------------------------------- 815 816 const u8 * wd_set_common_key 817 ( 818 wd_ckey_index_t ckey_index, // index of common key 819 const void * new_key // the new key 820 ); 821 822 //----------------------------------------------------------------------------- 823 824 void wd_decrypt_title_key 825 ( 826 const wd_ticket_t * tik, // pointer to ticket 827 u8 * title_key // the result 828 ); 829 830 //----------------------------------------------------------------------------- 831 832 void wd_encrypt_title_key 833 ( 834 wd_ticket_t * tik, // pointer to ticket (modified) 835 const u8 * title_key // valid pointer to wanted title key 836 ); 837 838 //----------------------------------------------------------------------------- 839 840 void wd_decrypt_sectors 841 ( 842 wd_part_t * part, // NULL or pointer to partition 843 const aes_key_t * akey, // aes key, if NULL use 'part' 844 const void * sect_src, // pointer to first source sector 845 void * sect_dest, // pointer to first destination sector 846 void * hash_dest, // if not NULL: store hash tables here 847 u32 n_sectors // number of wii sectors to decrypt 848 ); 849 850 //----------------------------------------------------------------------------- 851 852 void wd_encrypt_sectors 853 ( 854 wd_part_t * part, // NULL or pointer to partition 855 const aes_key_t * akey, // aes key, if NULL use 'part' 856 const void * sect_src, // pointer to first source sector 857 const void * hash_src, // if not NULL: source of hash tables 858 void * sect_dest, // pointer to first destination sector 859 u32 n_sectors // number of wii sectors to encrypt 860 ); 861 862 //----------------------------------------------------------------------------- 863 864 void wd_split_sectors 865 ( 866 const void * sect_src, // pointer to first source sector 867 void * data_dest, // pointer to data destination 868 void * hash_dest, // pointer to hash destination 869 u32 n_sectors // number of wii sectors to decrypt 870 ); 871 872 //----------------------------------------------------------------------------- 873 874 void wd_join_sectors 875 ( 876 const void * data_src, // pointer to data source 877 const void * hash_src, // pointer to hash source 878 void * sect_dest, // pointer to first destination sector 879 u32 n_sectors // number of wii sectors to encrypt 880 ); 881 882 // 883 /////////////////////////////////////////////////////////////////////////////// 884 /////////////// interface: names, ids, titles, ... /////////////// 885 /////////////////////////////////////////////////////////////////////////////// 886 887 extern const char * wd_disc_type_name[]; 888 extern const char * wd_part_name[]; 889 890 //----------------------------------------------------------------------------- 891 892 const char * wd_get_disc_type_name 893 ( 894 wd_disc_type_t disc_type, // disc type 895 ccp not_found // result if no name found 896 ); 897 898 //----------------------------------------------------------------------------- 899 900 const char * wd_get_part_name 901 ( 902 u32 ptype, // partition type 903 ccp not_found // result if no name found 904 ); 905 906 //----------------------------------------------------------------------------- 907 908 char * wd_print_part_name 909 ( 910 char * buf, // result buffer 911 // If NULL, a local circulary static buffer is used 912 size_t buf_size, // size of 'buf', ignored if buf==NULL 913 u32 ptype, // partition type 914 wd_pname_mode_t mode // print mode 915 ); 916 917 //----------------------------------------------------------------------------- 918 // returns a pointer to a printable ID, teminated with 0 919 920 char * wd_print_id 921 ( 922 const void * id, // ID to convert in printable format 923 size_t id_len, // len of 'id' 924 void * buf // Pointer to buffer, size >= id_len + 1 925 // If NULL, a local circulary static buffer is used 926 ); 927 928 //----------------------------------------------------------------------------- 929 // rename ID and title of a ISO header 930 931 int wd_rename 932 ( 933 void * data, // pointer to ISO data 934 ccp new_id, // if !NULL: take the first 6 chars as ID 935 ccp new_title // if !NULL: take the first 0x39 chars as title 936 ); 937 938 // 939 /////////////////////////////////////////////////////////////////////////////// 940 /////////////// interface: read functions /////////////// 941 /////////////////////////////////////////////////////////////////////////////// 942 943 enumError wd_read_raw 944 ( 945 wd_disc_t * disc, // valid disc pointer 946 u32 disc_offset4, // disc offset/4 947 void * dest_buf, // destination buffer 948 u32 read_size, // number of bytes to read 949 wd_usage_t usage_id // not 0: mark usage usage_tab with this value 950 ); 951 952 //----------------------------------------------------------------------------- 953 954 enumError wd_read_part_raw 955 ( 956 wd_part_t * part, // valid pointer to a disc partition 957 u32 offset4, // offset/4 to partition start 958 void * dest_buf, // destination buffer 959 u32 read_size, // number of bytes to read 960 bool mark_block // true: mark block in 'usage_table' 961 ); 962 963 //----------------------------------------------------------------------------- 964 965 enumError wd_read_part_block 966 ( 967 wd_part_t * part, // valid pointer to a disc partition 968 u32 block_num, // block number of partition 969 u8 * block, // destination buf 970 bool mark_block // true: mark block in 'usage_table' 971 ); 972 973 //----------------------------------------------------------------------------- 974 975 enumError wd_read_part 976 ( 977 wd_part_t * part, // valid pointer to a disc partition 978 u32 data_offset4, // partition data offset/4 979 void * dest_buf, // estination buffer 980 u32 read_size, // number of bytes to read 981 bool mark_block // true: mark block in 'usage_table' 982 ); 983 984 //----------------------------------------------------------------------------- 985 986 void wd_mark_part 987 ( 988 wd_part_t * part, // valid pointer to a disc partition 989 u32 data_offset4, // partition data offset/4 990 u32 data_size // number of bytes to mark 991 ); 992 993 //----------------------------------------------------------------------------- 994 995 u64 wd_calc_disc_offset 996 ( 997 wd_part_t * part, // valid pointer to a disc partition 998 u64 data_offset4 // partition data offset 999 ); 1000 1001 // 1002 /////////////////////////////////////////////////////////////////////////////// 1003 /////////////// get sector status /////////////// 1004 /////////////////////////////////////////////////////////////////////////////// 1005 1006 wd_sector_status_t wd_get_part_sector_status 1007 ( 1008 wd_part_t * part, // valid pointer to a disc partition 1009 u32 block_num, // block number of disc 1010 bool silent // true: don't print error messages 1011 ); 1012 1013 //----------------------------------------------------------------------------- 1014 1015 wd_sector_status_t wd_get_disc_sector_status 1016 ( 1017 wd_disc_t * disc, // valid disc pointer 1018 u32 block_num, // block number of partition 1019 bool silent // true: don't print error messages 1020 ); 1021 1022 //----------------------------------------------------------------------------- 1023 1024 ccp wd_log_sector_status 1025 ( 1026 char * buf, // result buffer 1027 // If NULL, a local circulary static buffer is used 1028 size_t buf_size, // size of 'buf', ignored if buf==NULL 1029 wd_sector_status_t sect_stat // sector status 1030 ); 1031 1032 //----------------------------------------------------------------------------- 1033 1034 ccp wd_print_sector_status 1035 ( 1036 char * buf, // result buffer 1037 // If NULL, a local circulary static buffer is used 1038 size_t buf_size, // size of 'buf', ignored if buf==NULL 1039 wd_sector_status_t sect_stat, // sector status 1040 cert_stat_t sig_stat // not NULL: add signature status 1041 ); 1042 1043 //----------------------------------------------------------------------------- 1044 1045 int wd_is_block_encrypted 1046 ( 1047 // returns -1 on read error 1048 1049 wd_part_t * part, // valid pointer to a disc partition 1050 u32 block_num, // block number of partition 1051 int unknown_result, // result if status is unknown 1052 bool silent // true: don't print error messages 1053 ); 1054 1055 //----------------------------------------------------------------------------- 1056 1057 bool wd_is_part_scrubbed 1058 ( 1059 wd_part_t * part, // valid pointer to a disc partition 1060 bool silent // true: don't print error messages 1061 ); 1062 1063 //----------------------------------------------------------------------------- 1064 1065 bool wd_is_disc_scrubbed 1066 ( 1067 wd_disc_t * disc, // valid disc pointer 1068 bool silent // true: don't print error messages 1069 ); 1070 1071 // 1072 /////////////////////////////////////////////////////////////////////////////// 1073 /////////////// interface: open and close /////////////// 1074 /////////////////////////////////////////////////////////////////////////////// 1075 1076 wd_disc_t * wd_open_disc 1077 ( 1078 wd_read_func_t read_func, // read function, always valid 1079 void * read_data, // data pointer for read function 1080 u64 iso_size, // size of iso file, unknown if 0 1081 ccp file_name, // used for error messages if not NULL 1082 int force, // force level 1083 enumError * error_code // store error code if not NULL 1084 ); 1085 1086 //----------------------------------------------------------------------------- 1087 1088 wd_disc_t * wd_dup_disc 1089 ( 1090 wd_disc_t * disc // NULL or a valid disc pointer 1091 ); 1092 1093 //----------------------------------------------------------------------------- 1094 1095 void wd_close_disc 1096 ( 1097 wd_disc_t * disc // NULL or valid disc pointer 1098 ); 1099 1100 // 1101 /////////////////////////////////////////////////////////////////////////////// 1102 /////////////// interface: get status /////////////// 1103 /////////////////////////////////////////////////////////////////////////////// 1104 1105 bool wd_disc_has_ptab 1106 ( 1107 wd_disc_t *disc // valid disc pointer 1108 ); 1109 1110 //----------------------------------------------------------------------------- 1111 1112 bool wd_disc_has_region_setting 1113 ( 1114 wd_disc_t *disc // valid disc pointer 1115 ); 1116 1117 //----------------------------------------------------------------------------- 1118 1119 bool wd_part_has_ticket 1120 ( 1121 wd_part_t *part // valid disc partition pointer 1122 ); 1123 1124 //----------------------------------------------------------------------------- 1125 1126 bool wd_part_has_tmd 1127 ( 1128 wd_part_t *part // valid disc partition pointer 1129 ); 1130 1131 //----------------------------------------------------------------------------- 1132 1133 bool wd_part_has_cert 1134 ( 1135 wd_part_t *part // valid disc partition pointer 1136 ); 1137 1138 //----------------------------------------------------------------------------- 1139 1140 bool wd_part_has_h3 1141 ( 1142 wd_part_t *part // valid disc partition pointer 1143 ); 1144 1145 // 1146 /////////////////////////////////////////////////////////////////////////////// 1147 /////////////// interface: get partition /////////////// 1148 /////////////////////////////////////////////////////////////////////////////// 1149 1150 wd_part_t * wd_get_part_by_index 1151 ( 1152 wd_disc_t * disc, // valid disc pointer 1153 int index, // zero based partition index within wd_disc_t 1154 int load_data // >0: load partition data from disc 1155 // >1: load cert too 1156 // >2: load h3 too 1157 ); 1158 1159 //----------------------------------------------------------------------------- 1160 1161 wd_part_t * wd_get_part_by_usage 1162 ( 1163 wd_disc_t * disc, // valid disc pointer 1164 u8 usage_id, // value of 'usage_table' 1165 int load_data // >0: load partition data from disc 1166 // >1: load cert too 1167 // >2: load h3 too 1168 ); 1169 1170 //----------------------------------------------------------------------------- 1171 1172 wd_part_t * wd_get_part_by_ptab 1173 ( 1174 wd_disc_t * disc, // valid disc pointer 1175 int ptab_index, // zero based index of partition table 1176 int ptab_part_index,// zero based index within owning partition table 1177 int load_data // >0: load partition data from disc 1178 // >1: load cert too 1179 // >2: load h3 too 1180 ); 1181 1182 //----------------------------------------------------------------------------- 1183 1184 wd_part_t * wd_get_part_by_type 1185 ( 1186 wd_disc_t * disc, // valid disc pointer 1187 u32 part_type, // find first partition with this type 1188 int load_data // >0: load partition data from disc 1189 // >1: load cert too 1190 // >2: load h3 too 1191 ); 1192 1193 // 1194 /////////////////////////////////////////////////////////////////////////////// 1195 /////////////// interface: load partition data /////////////// 1196 /////////////////////////////////////////////////////////////////////////////// 1197 1198 enumError wd_load_part 1199 ( 1200 wd_part_t * part, // valid disc partition pointer 1201 bool load_cert, // true: load cert data too 1202 bool load_h3, // true: load h3 data too 1203 bool silent // true: don't print error messages 1204 ); 1205 1206 //----------------------------------------------------------------------------- 1207 1208 enumError wd_load_all_part 1209 ( 1210 wd_disc_t * disc, // valid disc pointer 1211 bool load_cert, // true: load cert data too 1212 bool load_h3, // true: load h3 data too 1213 bool silent // true: don't print error messages 1214 ); 1215 1216 //----------------------------------------------------------------------------- 1217 1218 enumError wd_calc_fst_statistics 1219 ( 1220 wd_disc_t * disc, // valid disc pointer 1221 bool sum_all // false: summarize only enabled partitions 1222 ); 1223 1224 // 1225 /////////////////////////////////////////////////////////////////////////////// 1226 /////////////// interface: certificate support /////////////// 1227 /////////////////////////////////////////////////////////////////////////////// 1228 1229 const cert_chain_t * wd_load_cert_chain 1230 ( 1231 wd_part_t * part, // valid disc partition pointer 1232 bool silent // true: don't print errors while loading cert 1233 ); 1234 1235 //----------------------------------------------------------------------------- 1236 1237 cert_stat_t wd_get_cert_ticket_stat 1238 ( 1239 wd_part_t * part, // valid disc partition pointer 1240 bool silent // true: don't print errors while loading cert 1241 ); 1242 1243 //----------------------------------------------------------------------------- 1244 1245 cert_stat_t wd_get_cert_tmd_stat 1246 ( 1247 wd_part_t * part, // valid disc partition pointer 1248 bool silent // true: don't print errors while loading cert 1249 ); 1250 1251 //----------------------------------------------------------------------------- 1252 1253 ccp wd_get_sig_status_short_text 1254 ( 1255 cert_stat_t sig_stat 1256 ); 1257 1258 //----------------------------------------------------------------------------- 1259 1260 ccp wd_get_sig_status_text 1261 ( 1262 cert_stat_t sig_stat 1263 ); 1264 1265 //----------------------------------------------------------------------------- 1266 1267 char * wd_print_sig_status 1268 ( 1269 char * buf, // result buffer 1270 // If NULL, a local circulary static buffer is used 1271 size_t buf_size, // size of 'buf', ignored if buf==NULL 1272 wd_part_t * part, // valid disc partition pointer 1273 bool silent, // true: don't print errors while loading data 1274 bool add_enc_info // true: append " Partition is *crypted." 1275 ); 1276 1277 //----------------------------------------------------------------------------- 1278 1279 char * wd_print_part_status 1280 ( 1281 char * buf, // result buffer 1282 // If NULL, a local circulary static buffer is used 1283 size_t buf_size, // size of 'buf', ignored if buf==NULL 1284 wd_part_t * part, // valid disc partition pointer 1285 bool silent // true: don't print errors while loading cert 1286 ); 1287 1288 //----------------------------------------------------------------------------- 1289 1290 enumError wd_calc_part_status 1291 ( 1292 wd_part_t * part, // valid pointer to a disc partition 1293 bool silent // true: don't print error messages 1294 ); 1295 1296 //----------------------------------------------------------------------------- 1297 1298 enumError wd_calc_disc_status 1299 ( 1300 wd_disc_t * disc, // valid disc pointer 1301 bool silent // true: don't print error messages 1302 ); 1303 1304 // 1305 /////////////////////////////////////////////////////////////////////////////// 1306 /////////////// interface: select partitions /////////////// 1307 /////////////////////////////////////////////////////////////////////////////// 1308 1309 void wd_initialize_select 1310 ( 1311 wd_select_t * select // valid pointer to a partition selector 1312 ); 1313 1314 //----------------------------------------------------------------------------- 1315 1316 void wd_reset_select 1317 ( 1318 wd_select_t * select // valid pointer to a partition selector 1319 ); 1320 1321 //----------------------------------------------------------------------------- 1322 1323 wd_select_item_t * wd_append_select_item 1324 ( 1325 wd_select_t * select, // valid pointer to a partition selector 1326 wd_select_mode_t mode, // select mode of new item 1327 u32 table, // partition table of new item 1328 u32 part // partition type or index of new item 1329 ); 1330 1331 //----------------------------------------------------------------------------- 1332 1333 void wd_copy_select 1334 ( 1335 wd_select_t * dest, // valid pointer to a partition selector 1336 const wd_select_t * source // NULL or pointer to a partition selector 1337 ); 1338 1339 //----------------------------------------------------------------------------- 1340 1341 void wd_move_select 1342 ( 1343 wd_select_t * dest, // valid pointer to a partition selector 1344 wd_select_t * source // NULL or pointer to a partition selector 1345 ); 1346 1347 //----------------------------------------------------------------------------- 1348 1349 void wd_print_select 1350 ( 1351 FILE * f, // valid output file 1352 int indent, // indention of the output 1353 wd_select_t * select // valid pointer to a partition selector 1354 ); 1355 1356 //----------------------------------------------------------------------------- 1357 1358 bool wd_is_part_selected 1359 ( 1360 wd_part_t * part, // valid pointer to a disc partition 1361 const wd_select_t * select // NULL or pointer to a partition selector 1362 ); 1363 1364 //----------------------------------------------------------------------------- 1365 1366 bool wd_select // return true if selection changed 1367 ( 1368 wd_disc_t * disc, // valid disc pointer 1369 const wd_select_t * select // NULL or pointer to a partition selector 1370 ); 1371 1372 // 1373 /////////////////////////////////////////////////////////////////////////////// 1374 /////////////// interface: usage table /////////////// 1375 /////////////////////////////////////////////////////////////////////////////// 1376 1377 extern const char wd_usage_name_tab[256]; 1378 extern const u8 wd_usage_class_tab[256]; 1379 1380 //----------------------------------------------------------------------------- 1381 1382 u8 * wd_calc_usage_table 1383 ( 1384 wd_disc_t * disc // valid disc partition pointer 1385 ); 1386 1387 //----------------------------------------------------------------------------- 1388 1389 u8 * wd_filter_usage_table 1390 ( 1391 wd_disc_t * disc, // valid disc pointer 1392 u8 * usage_table, // NULL or result. If NULL -> MALLOC() 1393 const wd_select_t * select // NULL or a new selector 1394 ); 1395 1396 //----------------------------------------------------------------------------- 1397 1398 u32 wd_pack_disc_usage_table // returns the index if the 'last_used_sector + 1' 1399 ( 1400 u8 * dest_table, // valid pointer to destination table 1401 wd_disc_t * disc, // valid pointer to a disc 1402 u32 block_size, // if >1: count every 'block_size' 1403 // continuous sectors as one block 1404 const wd_select_t * select // NULL or a new selector 1405 ); 1406 1407 //----------------------------------------------------------------------------- 1408 1409 u32 wd_pack_usage_table // returns the index if the 'last_used_sector + 1' 1410 ( 1411 u8 * dest_table, // valid pointer to destination table 1412 const u8 * usage_table, // valid pointer to usage table 1413 u32 block_size // if >1: count every 'block_size' 1414 // continuous sectors as one block 1415 ); 1416 1417 //----------------------------------------------------------------------------- 1418 1419 u64 wd_count_used_disc_size 1420 ( 1421 wd_disc_t * disc, // valid pointer to a disc 1422 int block_size, // if >1: count every 'block_size' 1423 // continuous sectors as one block 1424 // and return the block count 1425 // if <0: like >1, but give the result as multiple 1426 // of WII_SECTOR_SIZE and reduce the count 1427 // for non needed sectors at the end. 1428 const wd_select_t * select // NULL or a new selector 1429 ); 1430 1431 //----------------------------------------------------------------------------- 1432 1433 u32 wd_count_used_disc_blocks 1434 ( 1435 wd_disc_t * disc, // valid pointer to a disc 1436 int block_size, // if >1: count every 'block_size' 1437 // continuous sectors as one block 1438 // and return the block count 1439 // if <0: like >1, but give the result as multiple 1440 // of WII_SECTOR_SIZE and reduce the count 1441 // for non needed sectors at the end. 1442 const wd_select_t * select // NULL or a new selector 1443 ); 1444 1445 //----------------------------------------------------------------------------- 1446 1447 u32 wd_count_used_blocks 1448 ( 1449 const u8 * usage_table, // valid pointer to usage table 1450 int block_size // if >1: count every 'block_size' 1451 // continuous sectors as one block 1452 // and return the block count 1453 // if <0: like >1, but give the result as multiple 1454 // of WII_SECTOR_SIZE and reduce the count 1455 // for non needed sectors at the end. 1456 ); 1457 1458 //----------------------------------------------------------------------------- 1459 1460 bool wd_is_block_used 1461 ( 1462 const u8 * usage_table, // valid pointer to usage table 1463 u32 block_index, // index of block 1464 u32 block_size // if >1: number of sectors per block 1465 ); 1466 1467 // 1468 /////////////////////////////////////////////////////////////////////////////// 1469 /////////////// interface: file iteration /////////////// 1470 /////////////////////////////////////////////////////////////////////////////// 1471 1472 int wd_iterate_files 1473 ( 1474 wd_disc_t * disc, // valid pointer to a disc 1475 wd_file_func_t func, // call back function 1476 void * param, // user defined parameter 1477 int ignore_files, // >0: ignore all real files 1478 // >1: ignore fst.bin + main.dol too 1479 wd_ipm_t prefix_mode // prefix mode 1480 ); 1481 1482 //----------------------------------------------------------------------------- 1483 1484 int wd_iterate_fst_files 1485 ( 1486 const wd_fst_item_t *fst_base, // valid pointer to FST data 1487 wd_file_func_t func, // call back function 1488 void * param // user defined parameter 1489 ); 1490 1491 //----------------------------------------------------------------------------- 1492 1493 int wd_remove_disc_files 1494 ( 1495 // Call wd_remove_part_files() for each enabled partition. 1496 // Returns 0 if nothing is removed, 1 if at least one file is removed 1497 // Other values are abort codes from func() 1498 1499 wd_disc_t * disc, // valid pointer to a disc 1500 wd_file_func_t func, // call back function 1501 // return 0 for don't touch file 1502 // return 1 for zero file 1503 // return other for abort 1504 void * param, // user defined parameter 1505 bool calc_usage_tab // true: calc usage table again by using 1506 // wd_select_part_files(func:=NULL) 1507 // if at least one file was removed 1508 ); 1509 1510 //----------------------------------------------------------------------------- 1511 1512 int wd_remove_part_files 1513 ( 1514 // Remove files and directories from internal FST copy. 1515 // Only empty directories are removed. If at least 1 files/dir 1516 // is removed the new FST.BIN is added to the patching map. 1517 // Returns 0 if nothing is removed, 1 if at least one file is removed 1518 // Other values are abort codes from func() 1519 1520 wd_part_t * part, // valid pointer to a partition 1521 wd_file_func_t func, // call back function 1522 // return 0 for don't touch file 1523 // return 1 for zero file 1524 // return other for abort 1525 void * param, // user defined parameter 1526 bool calc_usage_tab // true: calc usage table again by using 1527 // wd_select_part_files(func:=NULL) 1528 // if at least one file was removed 1529 ); 1530 1531 //----------------------------------------------------------------------------- 1532 1533 int wd_zero_disc_files 1534 ( 1535 // Call wd_remove_part_files() for each enabled partition. 1536 // Returns 0 if nothing is zeroed, 1 if at least one file is zeroed 1537 // Other values are abort codes from func() 1538 1539 wd_disc_t * disc, // valid pointer to a disc 1540 wd_file_func_t func, // call back function 1541 // return 0 for don't touch file 1542 // return 1 for zero file 1543 // return other for abort 1544 void * param, // user defined parameter 1545 bool calc_usage_tab // true: calc usage table again by using 1546 // wd_select_part_files(func:=NULL) 1547 // if at least one file was zeroed 1548 ); 1549 1550 //----------------------------------------------------------------------------- 1551 1552 int wd_zero_part_files 1553 ( 1554 // Zero files from internal FST copy (set offset and size to NULL). 1555 // If at least 1 file is zeroed the new FST.BIN is added to the patching map. 1556 // Returns 0 if nothing is removed, 1 if at least one file is removed 1557 // Other values are abort codes from func() 1558 1559 wd_part_t * part, // valid pointer to a partition 1560 wd_file_func_t func, // call back function 1561 // return 0 for don't touch file 1562 // return 1 for zero file 1563 // return other for abort 1564 void * param, // user defined parameter 1565 bool calc_usage_tab // true: calc usage table again by using 1566 // wd_select_part_files(func:=NULL) 1567 // if at least one file was zeroed 1568 ); 1569 1570 //----------------------------------------------------------------------------- 1571 1572 int wd_select_disc_files 1573 ( 1574 // Call wd_remove_part_files() for each enabled partition. 1575 // Returns 0 if nothing is ignored, 1 if at least one file is ignored 1576 // Other values are abort codes from func() 1577 1578 wd_disc_t * disc, // valid pointer to a disc 1579 wd_file_func_t func, // call back function 1580 // return 0 for don't touch file 1581 // return 1 for zero file 1582 // return other for abort 1583 void * param // user defined parameter 1584 ); 1585 1586 //----------------------------------------------------------------------------- 1587 1588 int wd_select_part_files 1589 ( 1590 // Calculate the usage map for the partition by using the internal 1591 // and perhaps modified FST.BIN. If func() is not NULL ignore system 1592 // and real files (/sys/... and /files/...) for return value 1. 1593 // If at least 1 file is zeroed the new FST.BIN is added to the patching map. 1594 // Returns 0 if nothing is removed, 1 if at least one file is removed 1595 // Other values are abort codes from func() 1596 1597 wd_part_t * part, // valid pointer to a partition 1598 wd_file_func_t func, // call back function 1599 // If NULL nor file is ignored and only 1600 // the usage map is re calculated. 1601 // return 0 for don't touch file 1602 // return 1 for zero file 1603 // return other for abort 1604 void * param // user defined parameter 1605 ); 1606 1607 // 1608 /////////////////////////////////////////////////////////////////////////////// 1609 /////////////// interface: print files /////////////// 1610 /////////////////////////////////////////////////////////////////////////////// 1611 1612 extern const char wd_sep_200[201]; // 200 * '-' + NULL 1613 1614 //----------------------------------------------------------------------------- 1615 1616 void wd_initialize_print_fst 1617 ( 1618 wd_print_fst_t * pf, // valid pointer 1619 wd_pfst_t mode, // mode for setup 1620 FILE * f, // NULL or output file 1621 int indent, // indention of the output 1622 u32 max_off4, // NULL or maximal offset4 value of all files 1623 u32 max_size // NULL or maximal size value of all files 1624 ); 1625 1626 //----------------------------------------------------------------------------- 1627 1628 void wd_print_fst_header 1629 ( 1630 wd_print_fst_t * pf, // valid pointer 1631 int max_name_len // max name len, needed for separator line 1632 ); 1633 1634 //----------------------------------------------------------------------------- 1635 1636 void wd_print_fst_item 1637 ( 1638 wd_print_fst_t * pf, // valid pointer 1639 wd_part_t * part, // valid pointer to a disc partition 1640 wd_icm_t icm, // iterator call mode 1641 u32 offset4, // offset/4 to read 1642 u32 size, // size of object 1643 ccp fname1, // NULL or file name, part 1 1644 ccp fname2 // NULL or file name, part 2 1645 ); 1646 1647 //----------------------------------------------------------------------------- 1648 1649 void wd_print_fst 1650 ( 1651 FILE * f, // valid output file 1652 int indent, // indention of the output 1653 wd_disc_t * disc, // valid pointer to a disc 1654 wd_ipm_t prefix_mode, // prefix mode 1655 wd_pfst_t pfst_mode, // print mode 1656 wd_file_func_t filter_func, // NULL or filter function 1657 void * filter_param // user defined parameter 1658 ); 1659 1660 // 1661 /////////////////////////////////////////////////////////////////////////////// 1662 /////////////// interface: memmap helpers /////////////// 1663 /////////////////////////////////////////////////////////////////////////////// 1664 1665 void wd_reset_memmap 1666 ( 1667 wd_memmap_t * mm // NULL or patching data 1668 ); 1669 1670 //----------------------------------------------------------------------------- 1671 1672 wd_memmap_item_t * wd_find_memmap 1673 ( 1674 wd_memmap_t * mm, // patching data 1675 u32 mode, // memmap mode (e.g. wd_patch_mode_t) 1676 u64 offset, // offset of object 1677 u64 size // size of object 1678 ); 1679 1680 //----------------------------------------------------------------------------- 1681 1682 wd_memmap_item_t * wd_insert_memmap 1683 ( 1684 wd_memmap_t * mm, // patching data 1685 u32 mode, // memmap mode (e.g. wd_patch_mode_t) 1686 u64 offset, // offset of object 1687 u64 size // size of object 1688 ); 1689 1690 //----------------------------------------------------------------------------- 1691 1692 wd_memmap_item_t * wd_insert_memmap_alloc 1693 ( 1694 wd_memmap_t * mm, // patching data 1695 u32 mode, // memmap mode (e.g. wd_patch_mode_t) 1696 u64 offset, // offset of object 1697 u64 size // size of object 1698 ); 1699 1700 //----------------------------------------------------------------------------- 1701 1702 int wd_insert_memmap_disc_part 1703 ( 1704 wd_memmap_t * mm, // patching data 1705 wd_disc_t * disc, // valid disc pointer 1706 1707 wd_memmap_func_t func, // not NULL: Call func() for each inserted item 1708 void * param, // user defined paramater for 'func()' 1709 1710 // creation modes: 1711 // value WD_PAT_IGNORE means: do not create such entires 1712 1713 wd_patch_mode_t wii_head_mode, // value for the Wii partition header 1714 wd_patch_mode_t wii_mgr_mode, // value for the Wii partition mgr data 1715 wd_patch_mode_t wii_data_mode, // value for the Wii partition data 1716 wd_patch_mode_t gc_mgr_mode, // value for the GC partition mgr header 1717 wd_patch_mode_t gc_data_mode // value for the GC partition header 1718 ); 1719 1720 //----------------------------------------------------------------------------- 1721 1722 int wd_insert_memmap_part 1723 ( 1724 wd_memmap_t * mm, // patching data 1725 wd_part_t * part, // valid pointer to a disc partition 1726 1727 wd_memmap_func_t func, // not NULL: Call func() for each inserted item 1728 void * param, // user defined paramater for 'func()' 1729 1730 // creation modes: 1731 // value WD_PAT_IGNORE means: do not create such entires 1732 1733 wd_patch_mode_t wii_head_mode, // value for the Wii partition header 1734 wd_patch_mode_t wii_mgr_mode, // value for the Wii partition mgr data 1735 wd_patch_mode_t wii_data_mode, // value for the Wii partition data 1736 wd_patch_mode_t gc_mgr_mode, // value for the GC partition mgr header 1737 wd_patch_mode_t gc_data_mode // value for the GC partition header 1738 ); 1739 1740 //----------------------------------------------------------------------------- 1741 1742 void wd_print_memmap 1743 ( 1744 FILE * f, // valid output file 1745 int indent, // indention of the output 1746 wd_memmap_t * mm // patching data 1747 ); 1748 1749 // 1750 /////////////////////////////////////////////////////////////////////////////// 1751 /////////////// interface: patch helpers /////////////// 1752 /////////////////////////////////////////////////////////////////////////////// 1753 1754 wd_memmap_item_t * wd_insert_patch_ticket 1755 ( 1756 wd_part_t * part // valid pointer to a disc partition 1757 ); 1758 1759 //----------------------------------------------------------------------------- 1760 1761 wd_memmap_item_t * wd_insert_patch_tmd 1762 ( 1763 wd_part_t * part // valid pointer to a disc partition 1764 ); 1765 1766 //----------------------------------------------------------------------------- 1767 1768 wd_memmap_item_t * wd_insert_patch_fst 1769 ( 1770 wd_part_t * part // valid pointer to a disc partition 1771 ); 1772 1773 //----------------------------------------------------------------------------- 1774 1775 void wd_print_disc_patch 1776 ( 1777 FILE * f, // valid output file 1778 int indent, // indention of the output 1779 wd_disc_t * disc, // valid pointer to a disc 1780 bool print_title, // true: print table titles 1781 bool print_part // true: print partitions too 1782 ); 1783 1784 // 1785 /////////////////////////////////////////////////////////////////////////////// 1786 /////////////// interface: read and patch /////////////// 1787 /////////////////////////////////////////////////////////////////////////////// 1788 1789 enumError wd_read_and_patch 1790 ( 1791 wd_disc_t * disc, // valid disc pointer 1792 u64 offset, // offset to read 1793 void * dest_buf, // destination buffer 1794 u32 count // number of bytes to read 1795 ); 1796 1797 //----------------------------------------------------------------------------- 1798 1799 void wd_calc_group_hashes 1800 ( 1801 const u8 * group_data, // group data space 1802 u8 * group_hash, // group hash space 1803 u8 * h3, // NULL or H3 element to change 1804 const u8 dirty[WII_GROUP_SECTORS] 1805 // NULL or 'dirty sector' flags 1806 ); 1807 1808 // 1809 /////////////////////////////////////////////////////////////////////////////// 1810 /////////////// interface: patching /////////////// 1811 /////////////////////////////////////////////////////////////////////////////// 1812 1813 u32 wd_get_ptab_sector 1814 ( 1815 wd_disc_t * disc // valid disc pointer 1816 ); 1817 1818 //----------------------------------------------------------------------------- 1819 1820 bool wd_patch_ptab // result = true if something changed 1821 ( 1822 wd_disc_t * disc, // valid disc pointer 1823 void * sector_data, // valid pointer to sector data 1824 // GC: GC_MULTIBOOT_PTAB_OFF + GC_MULTIBOOT_PTAB_SIZE 1825 // Wii: WII_MAX_PTAB_SIZE 1826 bool force_patch // false: patch only if needed 1827 ); 1828 1829 //----------------------------------------------------------------------------- 1830 1831 bool wd_patch_id 1832 ( 1833 void * dest_id, // destination, size=id_len, not 0 term 1834 const void * source_id, // NULL or source id, length=id_len 1835 const void * new_id, // NULL or new ID / 0 term / '.': don't change 1836 u32 id_len // max length of id 1837 ); 1838 1839 //----------------------------------------------------------------------------- 1840 1841 bool wd_patch_disc_header // result = true if something changed 1842 ( 1843 wd_disc_t * disc, // valid pointer to a disc 1844 ccp new_id, // NULL or new ID / '.': don't change 1845 ccp new_name // NULL or new disc name 1846 ); 1847 1848 //----------------------------------------------------------------------------- 1849 1850 bool wd_patch_region // result = true if something changed 1851 ( 1852 wd_disc_t * disc, // valid pointer to a disc 1853 u32 new_region // new region id 1854 ); 1855 1856 //----------------------------------------------------------------------------- 1857 1858 bool wd_patch_common_key // result = true if something changed 1859 ( 1860 wd_part_t * part, // valid pointer to a disc partition 1861 wd_ckey_index_t ckey_index // new common key index 1862 ); 1863 1864 //----------------------------------------------------------------------------- 1865 1866 bool wd_patch_part_id // result = true if something changed 1867 ( 1868 wd_part_t * part, // valid pointer to a disc partition 1869 wd_modify_t modify, // objects to modify 1870 ccp new_disc_id, // NULL or new disc ID / '.': don't change 1871 ccp new_boot_id, // NULL or new boot ID / '.': don't change 1872 ccp new_ticket_id, // NULL or new ticket ID / '.': don't change 1873 ccp new_tmd_id // NULL or new tmd ID / '.': don't change 1874 ); 1875 1876 //----------------------------------------------------------------------------- 1877 1878 bool wd_patch_part_name // result = true if something changed 1879 ( 1880 wd_part_t * part, // valid pointer to a disc partition 1881 ccp new_name, // NULL or new disc name 1882 wd_modify_t modify // objects to modify 1883 ); 1884 1885 //----------------------------------------------------------------------------- 1886 1887 bool wd_patch_part_system // result = true if something changed 1888 ( 1889 wd_part_t * part, // valid pointer to a disc partition 1890 u64 system // new system id (IOS) 1891 ); 1892 1893 // 1894 /////////////////////////////////////////////////////////////////////////////// 1895 /////////////// interface: relocation /////////////// 1896 /////////////////////////////////////////////////////////////////////////////// 1897 1898 wd_reloc_t * wd_calc_relocation 1899 ( 1900 wd_disc_t * disc, // valid disc pointer 1901 bool encrypt, // true: encrypt partition data 1902 wd_trim_mode_t trim_mode, // trim mode 1903 u32 trim_align, // alignment value for trimming 1904 const wd_select_t * select, // NULL or a new selector 1905 bool force // true: force new calculation 1906 ); 1907 1908 //----------------------------------------------------------------------------- 1909 1910 void wd_print_relocation 1911 ( 1912 FILE * f, // valid output file 1913 int indent, // indention of the output 1914 const wd_reloc_t * reloc, // valid pointer to relocation table 1915 bool print_title // true: print table titles 1916 ); 1917 1918 //----------------------------------------------------------------------------- 1919 1920 void wd_print_disc_relocation 1921 ( 1922 FILE * f, // valid output file 1923 int indent, // indention of the output 1924 wd_disc_t * disc, // valid pointer to a disc 1925 bool print_title // true: print table titles 1926 ); 1927 1928 //----------------------------------------------------------------------------- 1929 1930 wd_trim_mode_t wd_get_relococation_trim 1931 ( 1932 wd_trim_mode_t trim_mode, // trim mode to check 1933 u32 * trim_align, // NULL or trim alignment (modify) 1934 wd_disc_type_t disc_type // type of disc for align calculation 1935 ); 1936 1937 // 1938 /////////////////////////////////////////////////////////////////////////////// 1939 /////////////// interface: file relocation /////////////// 1940 /////////////////////////////////////////////////////////////////////////////// 1941 1942 wd_file_list_t * wd_initialize_file_list 1943 ( 1944 wd_file_list_t * fl // NULL or working file list 1945 // If NULL: allocate a file list 1946 ); 1947 1948 //----------------------------------------------------------------------------- 1949 1950 void wd_reset_file_list 1951 ( 1952 wd_file_list_t * fl, // NULL or working file list to reset (free data) 1953 bool free_fl // true: call 'FREE(fl)' 1954 ); 1955 1956 //----------------------------------------------------------------------------- 1957 1958 wd_file_list_t * wd_create_file_list 1959 ( 1960 wd_file_list_t * fl, // NULL or working file list 1961 // If NULL: allocate a file list 1962 wd_fst_item_t * fst, // valid fst data structure 1963 bool fst_is_gc // true: 'fst' is a GameCube source 1964 ); 1965 1966 //----------------------------------------------------------------------------- 1967 1968 wd_file_t * wd_insert_file 1969 ( 1970 wd_file_list_t * fl, // working file list 1971 u32 src_off4, // source offset 1972 u32 size, // size of file 1973 ccp iso_path // valid path of iso file 1974 ); 1975 1976 //----------------------------------------------------------------------------- 1977 1978 wd_file_t * wd_insert_directory 1979 ( 1980 wd_file_list_t * fl, // working file list 1981 ccp dir_path // valid path of iso directory 1982 ); 1983 1984 //----------------------------------------------------------------------------- 1985 1986 wd_fst_item_t * wd_create_file_list_fst 1987 ( 1988 wd_file_list_t * fl, // working file list 1989 u32 * n_fst, // not NULL: store number of created fst elements 1990 bool create_gc_fst, // true: create a GameCube fst 1991 1992 u32 align1, // minimal alignment 1993 u32 align2, // alignment for files with size >= align2 1994 u32 align3 // alignment for files with size >= align3 1995 // All aligment values are rounded 1996 // up to the next power of 2. 1997 // The values are normalized (increased) 1998 // to align1 <= align2 <= align3 1999 ); 2000 2001 //----------------------------------------------------------------------------- 2002 2003 bool wd_is_directory 2004 ( 2005 wd_file_t * file // pointer to a file 2006 ); 2007 2008 // 2009 /////////////////////////////////////////////////////////////////////////////// 2010 /////////////// interface: dump data structures /////////////// 2011 /////////////////////////////////////////////////////////////////////////////// 2012 2013 void wd_print_disc 2014 ( 2015 FILE * f, // valid output file 2016 int indent, // indention of the output 2017 wd_disc_t * disc, // valid disc pointer 2018 int dump_level // dump level 2019 // >0: print extended part info 2020 // >1: print usage table 2021 ); 2022 2023 //----------------------------------------------------------------------------- 2024 2025 void wd_print_disc_usage_tab 2026 ( 2027 FILE * f, // valid output file 2028 int indent, // indention of the output 2029 wd_disc_t * disc, // valid disc pointer 2030 bool print_all // false: ignore const lines 2031 ); 2032 2033 //----------------------------------------------------------------------------- 2034 2035 void wd_print_usage_tab 2036 ( 2037 FILE * f, // valid output file 2038 int indent, // indention of the output 2039 const u8 * usage_tab, // valid pointer, size = WII_MAX_SECTORS 2040 u64 iso_size, // NULL or size of iso file 2041 bool print_all // false: ignore const lines 2042 ); 2043 2044 //----------------------------------------------------------------------------- 2045 2046 void wd_print_mem 2047 ( 2048 wd_disc_t * disc, // valid disc pointer 2049 wd_mem_func_t func, // valid function pointer 2050 void * param // user defined parameter 2051 ); 2052 2053 // 2054 /////////////////////////////////////////////////////////////////////////////// 2055 /////////////// E N D /////////////// 2056 /////////////////////////////////////////////////////////////////////////////// 2057 2058 #endif // WIIDISC_H 2059 2060