├── exploit ├── head.b ├── gciB1.bin ├── gz2e.hdr ├── gz2j.hdr ├── gz2p.hdr ├── tools │ ├── zelda-cksum.bat │ ├── zelda-cksum.exe │ ├── zelda-cksum.c │ ├── tools.h │ ├── bn.c │ ├── ec.c │ └── tools.c ├── pack.sh ├── gz2e.ld ├── gz2j.ld ├── gz2p.ld ├── start.S ├── Makefile └── twilight.c ├── .gitmodules ├── README.md ├── Makefile └── LICENSE /exploit/head.b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/twilight-hack-gc/HEAD/exploit/head.b -------------------------------------------------------------------------------- /exploit/gciB1.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/twilight-hack-gc/HEAD/exploit/gciB1.bin -------------------------------------------------------------------------------- /exploit/gz2e.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/twilight-hack-gc/HEAD/exploit/gz2e.hdr -------------------------------------------------------------------------------- /exploit/gz2j.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/twilight-hack-gc/HEAD/exploit/gz2j.hdr -------------------------------------------------------------------------------- /exploit/gz2p.hdr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/twilight-hack-gc/HEAD/exploit/gz2p.hdr -------------------------------------------------------------------------------- /exploit/tools/zelda-cksum.bat: -------------------------------------------------------------------------------- 1 | gcc -s -O1 zelda-cksum.c tools.c bn.c ec.c -lcrypto -o zelda-cksum.exe -------------------------------------------------------------------------------- /exploit/tools/zelda-cksum.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/twilight-hack-gc/HEAD/exploit/tools/zelda-cksum.exe -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "loader"] 2 | path = loader 3 | url = https://github.com/FIX94/gc-exploit-common-loader.git 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # twilight-hack-gc 2 | This is a port of the original twilight hack, you only need a method of injecting the .gci onto your memory card. 3 | This exploit will load a boot.dol converted with dol2gci (included in Home Bros.) from your memory card. -------------------------------------------------------------------------------- /exploit/pack.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | out=$1; shift 3 | dd if=/dev/zero bs=1 count=$((0x4000)) of=$out 2>/dev/null 4 | start=0 5 | for save in $@; do 6 | dd if=$save of=$out bs=1 seek=$start conv=notrunc 2>/dev/null 7 | start=$((start+0xa94)) 8 | done 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright 2016 FIX94 2 | # This code is licensed to you under the terms of the GNU GPL, version 2; 3 | # see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | all: 6 | @$(MAKE) --no-print-directory -C loader 7 | @mv -f loader/loader.h exploit/loader.h 8 | @$(MAKE) --no-print-directory -C exploit 9 | @mkdir -p gci 10 | @mv -f exploit/*.gci gci 11 | 12 | clean: 13 | @$(MAKE) --no-print-directory -C loader clean 14 | @$(MAKE) --no-print-directory -C exploit clean 15 | rm -rf gci 16 | -------------------------------------------------------------------------------- /exploit/gz2e.ld: -------------------------------------------------------------------------------- 1 | /* Copyright 2008-2009 Segher Boessenkool 2 | GameCube Port Copyright 2016 FIX94 3 | This code is licensed to you under the terms of the GNU GPL, version 2; 4 | see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt */ 5 | 6 | OUTPUT_FORMAT("elf32-powerpc") 7 | OUTPUT_ARCH(powerpc:common) 8 | 9 | SECTIONS { 10 | card_open = 0x80357F98; 11 | card_read = 0x803588CC; 12 | card_close = 0x803580B4; 13 | card_unmount = 0x803573D8; 14 | os_stopaudiosystem = 0x8033B494; 15 | gx_drawdone = 0x8035C2F4; 16 | 17 | .twilight baddr : 18 | { 19 | gz2*.o(.start) 20 | *(.text) 21 | *(.rodata .rodata.*) 22 | *(.data) 23 | *(.bss) 24 | . = 0x0a94; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /exploit/gz2j.ld: -------------------------------------------------------------------------------- 1 | /* Copyright 2008-2009 Segher Boessenkool 2 | GameCube Port Copyright 2016 FIX94 3 | This code is licensed to you under the terms of the GNU GPL, version 2; 4 | see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt */ 5 | 6 | OUTPUT_FORMAT("elf32-powerpc") 7 | OUTPUT_ARCH(powerpc:common) 8 | 9 | SECTIONS { 10 | card_open = 0x8035A434; 11 | card_read = 0x8035AD68; 12 | card_close = 0x8035A550; 13 | card_unmount = 0x80359874; 14 | os_stopaudiosystem = 0x8033D930; 15 | gx_drawdone = 0x8035E790; 16 | 17 | .twilight baddr : 18 | { 19 | gz2*.o(.start) 20 | *(.text) 21 | *(.rodata .rodata.*) 22 | *(.data) 23 | *(.bss) 24 | . = 0x0a94; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /exploit/gz2p.ld: -------------------------------------------------------------------------------- 1 | /* Copyright 2008-2009 Segher Boessenkool 2 | GameCube Port Copyright 2016 FIX94 3 | This code is licensed to you under the terms of the GNU GPL, version 2; 4 | see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt */ 5 | 6 | OUTPUT_FORMAT("elf32-powerpc") 7 | OUTPUT_ARCH(powerpc:common) 8 | 9 | SECTIONS { 10 | card_open = 0x80358DC8; 11 | card_read = 0x803596FC; 12 | card_close = 0x80358EE4; 13 | card_unmount = 0x80358208; 14 | os_stopaudiosystem = 0x8033C294; 15 | gx_drawdone = 0x8035D124; 16 | 17 | .twilight baddr : 18 | { 19 | gz2*.o(.start) 20 | *(.text) 21 | *(.rodata .rodata.*) 22 | *(.data) 23 | *(.bss) 24 | . = 0x0a94; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /exploit/start.S: -------------------------------------------------------------------------------- 1 | // Copyright 2008-2009 Segher Boessenkool 2 | // GameCube Port Copyright 2016 FIX94 3 | // This code is licensed to you under the terms of the GNU GPL, version 2; 4 | // see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 5 | 6 | #define XSTR(x) #x 7 | #define ISTR(x) XSTR(x) 8 | 9 | .section .start,"ax" 10 | 11 | // Uninteresting stuff. 12 | .incbin "head.b" 13 | 14 | // "Link". Overflow right at the title screen. 15 | 0: .fill 0x148,1,'3' 16 | 17 | addr8r12: 18 | // callback address we overwrite 19 | .long addr8r12 20 | // Padding for callback address 21 | .long 0 22 | // pointer to our callback code 23 | .long start 24 | 25 | start: 26 | // Disable interrupts 27 | mfmsr 3 ; rlwinm 3,3,0,17,15 ; mtmsr 3 28 | 29 | // Go for it! 30 | b _main 31 | -------------------------------------------------------------------------------- /exploit/tools/zelda-cksum.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | 7 | #include "tools.h" 8 | 9 | static u8 buf[0x4000]; 10 | 11 | static void slot_checksum(u8 *x) 12 | { 13 | u32 i; 14 | u32 sum, nsum; 15 | 16 | sum = 0; 17 | for (i = 0; i < 0x0a8c; i++) 18 | sum += x[i]; 19 | nsum = -(sum + 0x0a8c); 20 | 21 | wbe32(x + 0x0a8c, sum); 22 | wbe32(x + 0x0a90, nsum); 23 | } 24 | 25 | static void save_checksum(u8 *x) 26 | { 27 | u32 i; 28 | u16 sum, nsum; 29 | 30 | slot_checksum(x + 8); 31 | slot_checksum(x + 8 + 0x0a94); 32 | slot_checksum(x + 8 + 2*0x0a94); 33 | 34 | sum = 0; 35 | for (i = 0; i < 0x0ffe; i++) 36 | sum += be16(x + 2*i); 37 | nsum = -(sum + 0x0ffe); 38 | 39 | wbe16(x + 0x1ffc, sum); 40 | wbe16(x + 0x1ffe, nsum); 41 | } 42 | 43 | int main(int argc, char **argv) 44 | { 45 | FILE *fp; 46 | 47 | if (argc != 2) { 48 | fprintf(stderr, "Usage: %s \n", argv[0]); 49 | return 1; 50 | } 51 | 52 | fp = fopen(argv[1], "rb+"); 53 | if (!fp) 54 | fatal("open %s", argv[1]); 55 | if (fread(buf, 0x4000, 1, fp) != 1) 56 | fatal("read %s", argv[1]); 57 | 58 | save_checksum(buf); 59 | save_checksum(buf + 0x2000); 60 | 61 | if (fseek(fp, 0, SEEK_SET)) 62 | fatal("seek"); 63 | if (fwrite(buf, 0x4000, 1, fp) != 1) 64 | fatal("write %s", argv[1]); 65 | fclose(fp); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /exploit/tools/tools.h: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #ifndef _TOOLS_H 6 | #define _TOOLS_H 7 | 8 | // basic data types 9 | typedef unsigned char u8; 10 | typedef unsigned short u16; 11 | typedef unsigned int u32; 12 | typedef unsigned long long u64; 13 | 14 | u16 be16(const u8 *p); 15 | u32 be32(const u8 *p); 16 | u64 be64(const u8 *p); 17 | u64 be34(const u8 *p); 18 | 19 | void wbe16(u8 *p, u16 x); 20 | void wbe32(u8 *p, u32 x); 21 | void wbe64(u8 *p, u64 x); 22 | 23 | //#define round_down(x,n) ((x) & -(n)) 24 | #define round_up(x,n) (-(-(x) & -(n))) 25 | 26 | // bignum 27 | int bn_compare(u8 *a, u8 *b, u32 n); 28 | void bn_sub_modulus(u8 *a, u8 *N, u32 n); 29 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 30 | void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 31 | void bn_inv(u8 *d, u8 *a, u8 *N, u32 n); // only for prime N 32 | void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en); 33 | 34 | // crypto 35 | void md5(u8 *data, u32 len, u8 *hash); 36 | void sha(u8 *data, u32 len, u8 *hash); 37 | void get_key(const char *name, u8 *key, u32 len); 38 | void aes_cbc_dec(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out); 39 | void aes_cbc_enc(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out); 40 | void decrypt_title_key(u8 *tik, u8 *title_key); 41 | int check_cert_chain(u8 *data, u32 data_len, u8 *cert, u32 cert_len); 42 | int check_ec(u8 *ng, u8 *ap, u8 *sig, u8 *sig_hash); 43 | void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash); 44 | int check_ecdsa(u8 *Q, u8 *R, u8 *S, u8 *hash); 45 | void ec_priv_to_pub(u8 *k, u8 *Q); 46 | 47 | // compression 48 | void do_yaz0(u8 *in, u32 in_size, u8 *out, u32 out_size); 49 | 50 | // error handling 51 | void fatal(const char *s, ...); 52 | 53 | // output formatting 54 | void print_bytes(u8 *x, u32 n); 55 | void hexdump(u8 *x, u32 n); 56 | void dump_tmd(u8 *tmd); 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /exploit/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright 2008-2009 Segher Boessenkool 2 | # GameCube Port Copyright 2016 FIX94 3 | # This code is licensed to you under the terms of the GNU GPL, version 2; 4 | # see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 5 | 6 | 7 | # Configuration: 8 | 9 | # What toolchain prefix should we use 10 | CROSS ?= powerpc-eabi- 11 | 12 | # Where are the tools (http://git.infradead.org/users/segher/wii.git) 13 | TOOLS ?= C:\segher_wii_tools 14 | 15 | # End of configuration. 16 | 17 | 18 | 19 | # Set CC, LD, OBJCOPY based on CROSS, unless they are set already 20 | 21 | ifeq ($(origin CC), default) 22 | CC := $(CROSS)gcc -m32 23 | endif 24 | ifeq ($(origin LD), default) 25 | LD := $(CROSS)ld 26 | endif 27 | OBJCOPY ?= $(CROSS)objcopy 28 | 29 | 30 | # The compiler flags we need. 31 | 32 | CFLAGS := -Wall -W -O1 -ffreestanding -mno-eabi -mno-sdata -mcpu=750 33 | 34 | 35 | # Build with "V=1" to see the commands executed; be quiet otherwise. 36 | 37 | ifeq ($(V),1) 38 | Q := 39 | else 40 | Q := @ 41 | MAKEFLAGS += --no-print-directory 42 | endif 43 | 44 | 45 | targets := gz2e.gci gz2j.gci gz2p.gci 46 | targets-short := gz2e gz2j gz2p 47 | 48 | objs := twilight.o 49 | 50 | all: $(targets) 51 | 52 | $(targets): %.gci : %.dat %.hdr 53 | @echo " OUTPUT $@" 54 | $(Q)dd if=$(filter %.hdr,$^) of=$@ bs=1 count=64 2>/dev/null 55 | $(Q)dd if=gciB1.bin of=$@ bs=1 seek=64 count=16384 2>/dev/null 56 | $(Q)dd if=$(filter %.dat,$^) of=$@ bs=1 seek=16448 count=16384 2>/dev/null 57 | 58 | dats := gz2e.dat gz2j.dat gz2p.dat 59 | 60 | $(dats): %.dat : %.slot 61 | @echo " ZELDAPACK $@" 62 | $(Q)./pack.sh $@ $(filter %.slot,$^) 63 | $(Q)tools/zelda-cksum $@ 64 | 65 | slots := gz2e.slot gz2j.slot gz2p.slot 66 | 67 | $(slots): %.slot: %.elf 68 | @echo " OBJCOPY $@" 69 | $(Q)$(OBJCOPY) -Obinary $< $@ 70 | 71 | elfs := $(slots:.slot=.elf) 72 | 73 | gz2e.elf: baddr := 0x803ECF40+0 74 | gz2j.elf: baddr := 0x803E7080+0 75 | gz2p.elf: baddr := 0x803EEEE0+0 76 | $(elfs): %.elf: %.ld %.o $(objs) 77 | @echo " LINK $@" 78 | $(Q)$(LD) --defsym baddr=$(baddr) -T $^ -o $@ 79 | 80 | exploit-objs := $(elfs:.elf=.o) 81 | 82 | $(exploit-objs): %.o: start.S head.b 83 | @echo " ASSEMBLE $@" 84 | $(Q)$(CC) $(CFLAGS) -c $< -o $@ 85 | 86 | %.o: %.c 87 | @echo " COMPILE $@" 88 | $(Q)$(CC) $(CFLAGS) -c $< -o $@ 89 | 90 | clean: 91 | -rm -f $(targets) $(saves) $(elfs) $(exploit-objs) $(objs) $(slots) $(dats) loader.h 92 | -------------------------------------------------------------------------------- /exploit/tools/bn.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | 8 | #include "tools.h" 9 | 10 | static void bn_print(char *name, u8 *a, u32 n) 11 | { 12 | u32 i; 13 | 14 | printf("%s = ", name); 15 | 16 | for (i = 0; i < n; i++) 17 | printf("%02x", a[i]); 18 | 19 | printf("\n"); 20 | } 21 | 22 | static void bn_zero(u8 *d, u32 n) 23 | { 24 | memset(d, 0, n); 25 | } 26 | 27 | static void bn_copy(u8 *d, u8 *a, u32 n) 28 | { 29 | memcpy(d, a, n); 30 | } 31 | 32 | int bn_compare(u8 *a, u8 *b, u32 n) 33 | { 34 | u32 i; 35 | 36 | for (i = 0; i < n; i++) { 37 | if (a[i] < b[i]) 38 | return -1; 39 | if (a[i] > b[i]) 40 | return 1; 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | void bn_sub_modulus(u8 *a, u8 *N, u32 n) 47 | { 48 | u32 i; 49 | u32 dig; 50 | u8 c; 51 | 52 | c = 0; 53 | for (i = n - 1; i < n; i--) { 54 | dig = N[i] + c; 55 | c = (a[i] < dig); 56 | a[i] -= dig; 57 | } 58 | } 59 | 60 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 61 | { 62 | u32 i; 63 | u32 dig; 64 | u8 c; 65 | 66 | c = 0; 67 | for (i = n - 1; i < n; i--) { 68 | dig = a[i] + b[i] + c; 69 | c = (dig >= 0x100); 70 | d[i] = dig; 71 | } 72 | 73 | if (c) 74 | bn_sub_modulus(d, N, n); 75 | 76 | if (bn_compare(d, N, n) >= 0) 77 | bn_sub_modulus(d, N, n); 78 | } 79 | 80 | void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 81 | { 82 | u32 i; 83 | u8 mask; 84 | 85 | bn_zero(d, n); 86 | 87 | for (i = 0; i < n; i++) 88 | for (mask = 0x80; mask != 0; mask >>= 1) { 89 | bn_add(d, d, d, N, n); 90 | if ((a[i] & mask) != 0) 91 | bn_add(d, d, b, N, n); 92 | } 93 | } 94 | 95 | void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en) 96 | { 97 | u8 t[512]; 98 | u32 i; 99 | u8 mask; 100 | 101 | bn_zero(d, n); 102 | d[n-1] = 1; 103 | for (i = 0; i < en; i++) 104 | for (mask = 0x80; mask != 0; mask >>= 1) { 105 | bn_mul(t, d, d, N, n); 106 | if ((e[i] & mask) != 0) 107 | bn_mul(d, t, a, N, n); 108 | else 109 | bn_copy(d, t, n); 110 | } 111 | } 112 | 113 | // only for prime N -- stupid but lazy, see if I care 114 | void bn_inv(u8 *d, u8 *a, u8 *N, u32 n) 115 | { 116 | u8 t[512], s[512]; 117 | 118 | bn_copy(t, N, n); 119 | bn_zero(s, n); 120 | s[n-1] = 2; 121 | bn_sub_modulus(t, s, n); 122 | bn_exp(d, a, N, n, t, n); 123 | } 124 | -------------------------------------------------------------------------------- /exploit/twilight.c: -------------------------------------------------------------------------------- 1 | // Copyright 2008-2009 Segher Boessenkool 2 | // GameCube Port Copyright 2016 FIX94 3 | // This code is licensed to you under the terms of the GNU GPL, version 2; 4 | // see file LICENSE or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 5 | 6 | #include "loader.h" 7 | 8 | #define TO_ARAM 0 9 | #define TO_MRAM 1 10 | 11 | typedef unsigned int u32; 12 | typedef unsigned short u16; 13 | typedef unsigned char u8; 14 | typedef volatile unsigned int vu32; 15 | typedef volatile unsigned short vu16; 16 | 17 | typedef struct _cInfo 18 | { 19 | int chan; 20 | int fnum; 21 | int offset; 22 | int length; 23 | u16 block; 24 | } cInfo; 25 | 26 | int card_open(int chan, const char *path, cInfo *inf); 27 | int card_read(cInfo *inf, void* addr, int length, int offset); 28 | int card_close(cInfo *inf); 29 | int card_unmount(int chan); 30 | void os_stopaudiosystem(void); 31 | void gx_drawdone(void); 32 | 33 | static void sync_cache(void *p, u32 n) 34 | { 35 | u32 start, end; 36 | 37 | start = (u32)p & ~31; 38 | end = ((u32)p + n + 31) & ~31; 39 | n = (end - start) >> 5; 40 | 41 | while (n--) { 42 | asm("dcbst 0,%0 ; icbi 0,%0" : : "b"(p)); 43 | p += 32; 44 | } 45 | asm("sync ; isync"); 46 | } 47 | 48 | void *_memcpy(void *ptr, const void *src, int size) 49 | { 50 | char* ptr2 = ptr; 51 | const char* src2 = src; 52 | while(size--) *ptr2++ = *src2++; 53 | return ptr; 54 | } 55 | 56 | void ar_dma(u32 type, u32 mram, u32 aram, u32 len) 57 | { 58 | *(vu16*)0xCC005020 = (mram>>16); 59 | *(vu16*)0xCC005022 = (mram&0xFFFF); 60 | *(vu16*)0xCC005024 = (aram>>16); 61 | *(vu16*)0xCC005026 = (aram&0xFFFF); 62 | *(vu16*)0xCC005028 = (type<<15)|(len>>16); 63 | *(vu16*)0xCC00502A = (len&0xFFFF); 64 | while(*(vu16*)0xCC00500A & 0x200) ; 65 | } 66 | 67 | void __attribute__ ((noreturn)) _main() 68 | { 69 | //prevent BEEPing 70 | os_stopaudiosystem(); 71 | //ends whatever frame we are on 72 | gx_drawdone(); 73 | //game id DOLX00 for boot.dol 74 | *(volatile u32*)0x80000000 = 0x444F4C58; 75 | *(volatile u16*)0x80000004 = 0x3030; 76 | sync_cache((void*)0x80000000, 6); 77 | //no need to mount card, game did not unmount it 78 | //open up boot.dol from memory card 79 | cInfo mInf; 80 | card_open(0, "boot.dol", &mInf); 81 | u32 curOffset = 0; 82 | //copy until it returns error 83 | while(card_read(&mInf, (void*)0x80001800, 0x200, curOffset) == 0) 84 | { 85 | sync_cache((void*)0x80001800, 0x200); 86 | ar_dma(TO_ARAM, 0x1800, curOffset, 0x200); 87 | curOffset += 0x200; 88 | } 89 | //we are done 90 | card_close(&mInf); 91 | card_unmount(0); 92 | //copy our loader into mem 93 | _memcpy((void*)0x80001800, loader, loader_size); 94 | sync_cache((void*)0x80001800, loader_size); 95 | //jump to it 96 | __asm__ volatile( 97 | "lis 3, 0x8000\n" 98 | "ori 3, 3, 0x1800\n" 99 | "mtlr 3\n" 100 | "blr\n" 101 | ); 102 | __builtin_unreachable(); 103 | } 104 | -------------------------------------------------------------------------------- /exploit/tools/ec.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | 8 | #include "tools.h" 9 | 10 | // y**2 + x*y = x**3 + x**2 + b 11 | static u8 ec_b[30] = 12 | "\x00\x66\x64\x7e\xde\x6c\x33\x2c\x7f\x8c\x09\x23\xbb\x58\x21" 13 | "\x3b\x33\x3b\x20\xe9\xce\x42\x81\xfe\x11\x5f\x7d\x8f\x90\xad"; 14 | 15 | // order of the addition group of points 16 | static u8 ec_N[30] = 17 | "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 18 | "\x13\xe9\x74\xe7\x2f\x8a\x69\x22\x03\x1d\x26\x03\xcf\xe0\xd7"; 19 | 20 | // base point 21 | static u8 ec_G[60] = 22 | "\x00\xfa\xc9\xdf\xcb\xac\x83\x13\xbb\x21\x39\xf1\xbb\x75\x5f" 23 | "\xef\x65\xbc\x39\x1f\x8b\x36\xf8\xf8\xeb\x73\x71\xfd\x55\x8b" 24 | "\x01\x00\x6a\x08\xa4\x19\x03\x35\x06\x78\xe5\x85\x28\xbe\xbf" 25 | "\x8a\x0b\xef\xf8\x67\xa7\xca\x36\x71\x6f\x7e\x01\xf8\x10\x52"; 26 | 27 | static void elt_print(char *name, u8 *a) 28 | { 29 | u32 i; 30 | 31 | printf("%s = ", name); 32 | 33 | for (i = 0; i < 30; i++) 34 | printf("%02x", a[i]); 35 | 36 | printf("\n"); 37 | } 38 | 39 | static void elt_copy(u8 *d, u8 *a) 40 | { 41 | memcpy(d, a, 30); 42 | } 43 | 44 | static void elt_zero(u8 *d) 45 | { 46 | memset(d, 0, 30); 47 | } 48 | 49 | static int elt_is_zero(u8 *d) 50 | { 51 | u32 i; 52 | 53 | for (i = 0; i < 30; i++) 54 | if (d[i] != 0) 55 | return 0; 56 | 57 | return 1; 58 | } 59 | 60 | static void elt_add(u8 *d, u8 *a, u8 *b) 61 | { 62 | u32 i; 63 | 64 | for (i = 0; i < 30; i++) 65 | d[i] = a[i] ^ b[i]; 66 | } 67 | 68 | static void elt_mul_x(u8 *d, u8 *a) 69 | { 70 | u8 carry, x, y; 71 | u32 i; 72 | 73 | carry = a[0] & 1; 74 | 75 | x = 0; 76 | for (i = 0; i < 29; i++) { 77 | y = a[i + 1]; 78 | d[i] = x ^ (y >> 7); 79 | x = y << 1; 80 | } 81 | d[29] = x ^ carry; 82 | 83 | d[20] ^= carry << 2; 84 | } 85 | 86 | static void elt_mul(u8 *d, u8 *a, u8 *b) 87 | { 88 | u32 i, n; 89 | u8 mask; 90 | 91 | elt_zero(d); 92 | 93 | i = 0; 94 | mask = 1; 95 | for (n = 0; n < 233; n++) { 96 | elt_mul_x(d, d); 97 | 98 | if ((a[i] & mask) != 0) 99 | elt_add(d, d, b); 100 | 101 | mask >>= 1; 102 | if (mask == 0) { 103 | mask = 0x80; 104 | i++; 105 | } 106 | } 107 | } 108 | 109 | static const u8 square[16] = 110 | "\x00\x01\x04\x05\x10\x11\x14\x15\x40\x41\x44\x45\x50\x51\x54\x55"; 111 | 112 | static void elt_square_to_wide(u8 *d, u8 *a) 113 | { 114 | u32 i; 115 | 116 | for (i = 0; i < 30; i++) { 117 | d[2*i] = square[a[i] >> 4]; 118 | d[2*i + 1] = square[a[i] & 15]; 119 | } 120 | } 121 | 122 | static void wide_reduce(u8 *d) 123 | { 124 | u32 i; 125 | u8 x; 126 | 127 | for (i = 0; i < 30; i++) { 128 | x = d[i]; 129 | 130 | d[i + 19] ^= x >> 7; 131 | d[i + 20] ^= x << 1; 132 | 133 | d[i + 29] ^= x >> 1; 134 | d[i + 30] ^= x << 7; 135 | } 136 | 137 | x = d[30] & ~1; 138 | 139 | d[49] ^= x >> 7; 140 | d[50] ^= x << 1; 141 | 142 | d[59] ^= x >> 1; 143 | 144 | d[30] &= 1; 145 | } 146 | 147 | static void elt_square(u8 *d, u8 *a) 148 | { 149 | u8 wide[60]; 150 | 151 | elt_square_to_wide(wide, a); 152 | wide_reduce(wide); 153 | 154 | elt_copy(d, wide + 30); 155 | } 156 | 157 | static void itoh_tsujii(u8 *d, u8 *a, u8 *b, u32 j) 158 | { 159 | u8 t[30]; 160 | 161 | elt_copy(t, a); 162 | while (j--) { 163 | elt_square(d, t); 164 | elt_copy(t, d); 165 | } 166 | 167 | elt_mul(d, t, b); 168 | } 169 | 170 | static void elt_inv(u8 *d, u8 *a) 171 | { 172 | u8 t[30]; 173 | u8 s[30]; 174 | 175 | itoh_tsujii(t, a, a, 1); 176 | itoh_tsujii(s, t, a, 1); 177 | itoh_tsujii(t, s, s, 3); 178 | itoh_tsujii(s, t, a, 1); 179 | itoh_tsujii(t, s, s, 7); 180 | itoh_tsujii(s, t, t, 14); 181 | itoh_tsujii(t, s, a, 1); 182 | itoh_tsujii(s, t, t, 29); 183 | itoh_tsujii(t, s, s, 58); 184 | itoh_tsujii(s, t, t, 116); 185 | elt_square(d, s); 186 | } 187 | 188 | static int point_is_on_curve(u8 *p) 189 | { 190 | u8 s[30], t[30]; 191 | u8 *x, *y; 192 | 193 | x = p; 194 | y = p + 30; 195 | 196 | elt_square(t, x); 197 | elt_mul(s, t, x); 198 | 199 | elt_add(s, s, t); 200 | 201 | elt_square(t, y); 202 | elt_add(s, s, t); 203 | 204 | elt_mul(t, x, y); 205 | elt_add(s, s, t); 206 | 207 | elt_add(s, s, ec_b); 208 | 209 | return elt_is_zero(s); 210 | } 211 | 212 | static int point_is_zero(u8 *p) 213 | { 214 | return elt_is_zero(p) && elt_is_zero(p + 30); 215 | } 216 | 217 | static void point_double(u8 *r, u8 *p) 218 | { 219 | u8 s[30], t[30]; 220 | u8 *px, *py, *rx, *ry; 221 | 222 | px = p; 223 | py = p + 30; 224 | rx = r; 225 | ry = r + 30; 226 | 227 | if (elt_is_zero(px)) { 228 | elt_zero(rx); 229 | elt_zero(ry); 230 | 231 | return; 232 | } 233 | 234 | elt_inv(t, px); 235 | elt_mul(s, py, t); 236 | elt_add(s, s, px); 237 | 238 | elt_square(t, px); 239 | 240 | elt_square(rx, s); 241 | elt_add(rx, rx, s); 242 | rx[29] ^= 1; 243 | 244 | elt_mul(ry, s, rx); 245 | elt_add(ry, ry, rx); 246 | elt_add(ry, ry, t); 247 | } 248 | 249 | static void point_add(u8 *r, u8 *p, u8 *q) 250 | { 251 | u8 s[30], t[30], u[30]; 252 | u8 *px, *py, *qx, *qy, *rx, *ry; 253 | 254 | px = p; 255 | py = p + 30; 256 | qx = q; 257 | qy = q + 30; 258 | rx = r; 259 | ry = r + 30; 260 | 261 | if (point_is_zero(p)) { 262 | elt_copy(rx, qx); 263 | elt_copy(ry, qy); 264 | return; 265 | } 266 | 267 | if (point_is_zero(q)) { 268 | elt_copy(rx, px); 269 | elt_copy(ry, py); 270 | return; 271 | } 272 | 273 | elt_add(u, px, qx); 274 | 275 | if (elt_is_zero(u)) { 276 | elt_add(u, py, qy); 277 | if (elt_is_zero(u)) 278 | point_double(r, p); 279 | else { 280 | elt_zero(rx); 281 | elt_zero(ry); 282 | } 283 | 284 | return; 285 | } 286 | 287 | elt_inv(t, u); 288 | elt_add(u, py, qy); 289 | elt_mul(s, t, u); 290 | 291 | elt_square(t, s); 292 | elt_add(t, t, s); 293 | elt_add(t, t, qx); 294 | t[29] ^= 1; 295 | 296 | elt_mul(u, s, t); 297 | elt_add(s, u, py); 298 | elt_add(rx, t, px); 299 | elt_add(ry, s, rx); 300 | } 301 | 302 | static void point_mul(u8 *d, u8 *a, u8 *b) // a is bignum 303 | { 304 | u32 i; 305 | u8 mask; 306 | 307 | elt_zero(d); 308 | elt_zero(d + 30); 309 | 310 | for (i = 0; i < 30; i++) 311 | for (mask = 0x80; mask != 0; mask >>= 1) { 312 | point_double(d, d); 313 | if ((a[i] & mask) != 0) 314 | point_add(d, d, b); 315 | } 316 | } 317 | 318 | static void doRand(u8 *buf, size_t size) 319 | { 320 | size_t fullInts = size/4; 321 | size_t i; 322 | u32 *intBuf = (u32*)buf; 323 | for(i = 0; i < fullInts; i++) 324 | intBuf[i] = rand(); 325 | size -= (fullInts*4); 326 | u16 *shortBuf = (u16*)(intBuf+i); 327 | u8 *charBuf = (u8*)shortBuf; 328 | if(size >= 2) 329 | { 330 | *shortBuf = (u16)rand(); 331 | charBuf = (u8*)(shortBuf+1); 332 | size -= 2; 333 | } 334 | if(size == 1) 335 | *charBuf = (u8)rand(); 336 | } 337 | void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash) 338 | { 339 | u8 e[30]; 340 | u8 kk[30]; 341 | u8 m[30]; 342 | u8 minv[30]; 343 | u8 mG[60]; 344 | FILE *fp; 345 | 346 | elt_zero(e); 347 | memcpy(e + 10, hash, 20); 348 | 349 | //fp = fopen("/dev/random", "rb"); 350 | //if (fread(m, sizeof m, 1, fp) != 1) 351 | // fatal("reading random"); 352 | //fclose(fp); 353 | doRand(m,30); 354 | m[0] = 0; 355 | 356 | // R = (mG).x 357 | 358 | point_mul(mG, m, ec_G); 359 | elt_copy(R, mG); 360 | if (bn_compare(R, ec_N, 30) >= 0) 361 | bn_sub_modulus(R, ec_N, 30); 362 | 363 | // S = m**-1*(e + Rk) (mod N) 364 | 365 | elt_copy(kk, k); 366 | if (bn_compare(kk, ec_N, 30) >= 0) 367 | bn_sub_modulus(kk, ec_N, 30); 368 | bn_mul(S, R, kk, ec_N, 30); 369 | bn_add(kk, S, e, ec_N, 30); 370 | bn_inv(minv, m, ec_N, 30); 371 | bn_mul(S, minv, kk, ec_N, 30); 372 | } 373 | 374 | int check_ecdsa(u8 *Q, u8 *R, u8 *S, u8 *hash) 375 | { 376 | u8 Sinv[30]; 377 | u8 e[30]; 378 | u8 w1[30], w2[30]; 379 | u8 r1[60], r2[60]; 380 | 381 | bn_inv(Sinv, S, ec_N, 30); 382 | 383 | elt_zero(e); 384 | memcpy(e + 10, hash, 20); 385 | 386 | bn_mul(w1, e, Sinv, ec_N, 30); 387 | bn_mul(w2, R, Sinv, ec_N, 30); 388 | 389 | point_mul(r1, w1, ec_G); 390 | point_mul(r2, w2, Q); 391 | 392 | point_add(r1, r1, r2); 393 | 394 | if (bn_compare(r1, ec_N, 30) >= 0) 395 | bn_sub_modulus(r1, ec_N, 30); 396 | 397 | return (bn_compare(r1, R, 30) == 0); 398 | } 399 | 400 | void ec_priv_to_pub(u8 *k, u8 *Q) 401 | { 402 | point_mul(Q, k, ec_G); 403 | } 404 | -------------------------------------------------------------------------------- /exploit/tools/tools.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | 7 | #include // to accommodate certain broken versions of openssl 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // 17 | // basic data types 18 | // 19 | 20 | u16 be16(const u8 *p) 21 | { 22 | return (p[0] << 8) | p[1]; 23 | } 24 | 25 | u32 be32(const u8 *p) 26 | { 27 | return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; 28 | } 29 | 30 | u64 be64(const u8 *p) 31 | { 32 | return ((u64)be32(p) << 32) | be32(p + 4); 33 | } 34 | 35 | u64 be34(const u8 *p) 36 | { 37 | return 4 * (u64)be32(p); 38 | } 39 | 40 | void wbe16(u8 *p, u16 x) 41 | { 42 | p[0] = x >> 8; 43 | p[1] = x; 44 | } 45 | 46 | void wbe32(u8 *p, u32 x) 47 | { 48 | wbe16(p, x >> 16); 49 | wbe16(p + 2, x); 50 | } 51 | 52 | void wbe64(u8 *p, u64 x) 53 | { 54 | wbe32(p, x >> 32); 55 | wbe32(p + 4, x); 56 | } 57 | 58 | // 59 | // crypto 60 | // 61 | 62 | void md5(u8 *data, u32 len, u8 *hash) 63 | { 64 | MD5(data, len, hash); 65 | } 66 | 67 | void sha(u8 *data, u32 len, u8 *hash) 68 | { 69 | SHA1(data, len, hash); 70 | } 71 | 72 | void get_key(const char *name, u8 *key, u32 len) 73 | { 74 | char path[256]; 75 | char *home; 76 | FILE *fp; 77 | 78 | home = getenv("HOME"); 79 | if (home == 0) 80 | fatal("cannot find HOME"); 81 | snprintf(path, sizeof path, "%s/.wii/%s", home, name); 82 | 83 | fp = fopen(path, "rb"); 84 | if (fp == 0) 85 | fatal("cannot open %s", name); 86 | if (fread(key, len, 1, fp) != 1) 87 | fatal("error reading %s", name); 88 | fclose(fp); 89 | } 90 | 91 | void aes_cbc_dec(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out) 92 | { 93 | AES_KEY aes_key; 94 | 95 | AES_set_decrypt_key(key, 128, &aes_key); 96 | AES_cbc_encrypt(in, out, len, &aes_key, iv, AES_DECRYPT); 97 | } 98 | 99 | void aes_cbc_enc(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out) 100 | { 101 | AES_KEY aes_key; 102 | 103 | AES_set_encrypt_key(key, 128, &aes_key); 104 | AES_cbc_encrypt(in, out, len, &aes_key, iv, AES_ENCRYPT); 105 | } 106 | 107 | void decrypt_title_key(u8 *tik, u8 *title_key) 108 | { 109 | u8 common_key[16]; 110 | u8 iv[16]; 111 | 112 | get_key("common-key", common_key, 16); 113 | 114 | memset(iv, 0, sizeof iv); 115 | memcpy(iv, tik + 0x01dc, 8); 116 | aes_cbc_dec(common_key, iv, tik + 0x01bf, 16, title_key); 117 | } 118 | 119 | static u8 root_key[0x204]; 120 | static u8 *get_root_key(void) 121 | { 122 | get_key("root-key", root_key, sizeof root_key); 123 | return root_key; 124 | } 125 | 126 | static u32 get_sig_len(u8 *sig) 127 | { 128 | u32 type; 129 | 130 | type = be32(sig); 131 | switch (type - 0x10000) { 132 | case 0: 133 | return 0x240; 134 | 135 | case 1: 136 | return 0x140; 137 | 138 | case 2: 139 | return 0x80; 140 | } 141 | 142 | fprintf(stderr, "get_sig_len(): unhandled sig type %08x\n", type); 143 | return 0; 144 | } 145 | 146 | static u32 get_sub_len(u8 *sub) 147 | { 148 | u32 type; 149 | 150 | type = be32(sub + 0x40); 151 | switch (type) { 152 | case 0: 153 | return 0x2c0; 154 | 155 | case 1: 156 | return 0x1c0; 157 | 158 | case 2: 159 | return 0x100; 160 | } 161 | 162 | fprintf(stderr, "get_sub_len(): unhandled sub type %08x\n", type); 163 | return 0; 164 | } 165 | 166 | int check_ec(u8 *ng, u8 *ap, u8 *sig, u8 *sig_hash) 167 | { 168 | u8 ap_hash[20]; 169 | u8 *ng_Q, *ap_R, *ap_S; 170 | u8 *ap_Q, *sig_R, *sig_S; 171 | 172 | ng_Q = ng + 0x0108; 173 | ap_R = ap + 0x04; 174 | ap_S = ap + 0x22; 175 | 176 | SHA1(ap + 0x80, 0x100, ap_hash); 177 | 178 | ap_Q = ap + 0x0108; 179 | sig_R = sig; 180 | sig_S = sig + 30; 181 | 182 | return check_ecdsa(ng_Q, ap_R, ap_S, ap_hash) 183 | && check_ecdsa(ap_Q, sig_R, sig_S, sig_hash); 184 | } 185 | 186 | static int check_rsa(u8 *h, u8 *sig, u8 *key, u32 n) 187 | { 188 | u8 correct[0x200]; 189 | u8 x[0x200]; 190 | static const u8 ber[16] = "\x00\x30\x21\x30\x09\x06\x05\x2b" 191 | "\x0e\x03\x02\x1a\x05\x00\x04\x14"; 192 | 193 | //fprintf(stderr, "n = %x\n", n); 194 | //fprintf(stderr, "key:\n"); 195 | //hexdump(key, n); 196 | //fprintf(stderr, "sig:\n"); 197 | //hexdump(sig, n); 198 | 199 | correct[0] = 0; 200 | correct[1] = 1; 201 | memset(correct + 2, 0xff, n - 38); 202 | memcpy(correct + n - 36, ber, 16); 203 | memcpy(correct + n - 20, h, 20); 204 | //fprintf(stderr, "correct is:\n"); 205 | //hexdump(correct, n); 206 | 207 | bn_exp(x, sig, key, n, key + n, 4); 208 | //fprintf(stderr, "x is:\n"); 209 | //hexdump(x, n); 210 | 211 | if (memcmp(correct, x, n) == 0) 212 | return 0; 213 | 214 | return -5; 215 | } 216 | 217 | static int check_hash(u8 *h, u8 *sig, u8 *key) 218 | { 219 | u32 type; 220 | 221 | type = be32(sig) - 0x10000; 222 | if (type != be32(key + 0x40)) 223 | return -6; 224 | 225 | switch (type) { 226 | case 1: 227 | return check_rsa(h, sig + 4, key + 0x88, 0x100); 228 | } 229 | 230 | return -7; 231 | } 232 | 233 | static u8 *find_cert_in_chain(u8 *sub, u8 *cert, u32 cert_len) 234 | { 235 | char parent[64]; 236 | char *child; 237 | u32 sig_len, sub_len; 238 | u8 *p; 239 | u8 *issuer; 240 | 241 | strncpy(parent, sub, sizeof parent); 242 | parent[sizeof parent - 1] = 0; 243 | child = strrchr(parent, '-'); 244 | if (child) 245 | *child++ = 0; 246 | else { 247 | *parent = 0; 248 | child = sub; 249 | } 250 | 251 | for (p = cert; p < cert + cert_len; p += sig_len + sub_len) { 252 | sig_len = get_sig_len(p); 253 | if (sig_len == 0) 254 | return 0; 255 | issuer = p + sig_len; 256 | sub_len = get_sub_len(issuer); 257 | if (sub_len == 0) 258 | return 0; 259 | 260 | if (strcmp(parent, issuer) == 0 261 | && strcmp(child, issuer + 0x44) == 0) 262 | return p; 263 | } 264 | 265 | return 0; 266 | } 267 | 268 | int check_cert_chain(u8 *data, u32 data_len, u8 *cert, u32 cert_len) 269 | { 270 | u8 *sig; 271 | u8 *sub; 272 | u32 sig_len; 273 | u32 sub_len; 274 | u8 h[20]; 275 | u8 *key_cert; 276 | u8 *key; 277 | int ret; 278 | 279 | sig = data; 280 | sig_len = get_sig_len(sig); 281 | if (sig_len == 0) 282 | return -1; 283 | sub = data + sig_len; 284 | sub_len = data_len - sig_len; 285 | if (sub_len == 0) 286 | return -2; 287 | 288 | for (;;) { 289 | fprintf(stderr, ">>>>>> checking sig by %s...\n", sub); 290 | if (strcmp(sub, "Root") == 0) { 291 | key = get_root_key(); 292 | sha(sub, sub_len, h); 293 | if (be32(sig) != 0x10000) 294 | return -8; 295 | return check_rsa(h, sig + 4, key, 0x200); 296 | } 297 | 298 | key_cert = find_cert_in_chain(sub, cert, cert_len); 299 | if (key_cert == 0) 300 | return -3; 301 | 302 | key = key_cert + get_sig_len(key_cert); 303 | 304 | sha(sub, sub_len, h); 305 | ret = check_hash(h, sig, key); 306 | if (ret) 307 | return ret; 308 | 309 | sig = key_cert; 310 | sig_len = get_sig_len(sig); 311 | if (sig_len == 0) 312 | return -4; 313 | sub = sig + sig_len; 314 | sub_len = get_sub_len(sub); 315 | if (sub_len == 0) 316 | return -5; 317 | } 318 | } 319 | 320 | // 321 | // compression 322 | // 323 | 324 | void do_yaz0(u8 *in, u32 in_size, u8 *out, u32 out_size) 325 | { 326 | u32 nout; 327 | u8 bits; 328 | u32 nbits; 329 | u32 n, d, i; 330 | 331 | bits = 0; 332 | nbits = 0; 333 | in += 0x10; 334 | for (nout = 0; nout < out_size; ) { 335 | if (nbits == 0) { 336 | bits = *in++; 337 | nbits = 8; 338 | } 339 | 340 | if ((bits & 0x80) != 0) { 341 | *out++ = *in++; 342 | nout++; 343 | } else { 344 | n = *in++; 345 | d = *in++; 346 | d |= (n << 8) & 0xf00; 347 | n >>= 4; 348 | if (n == 0) 349 | n = 0x10 + *in++; 350 | n += 2; 351 | d++; 352 | 353 | for (i = 0; i < n; i++) { 354 | *out = *(out - d); 355 | out++; 356 | } 357 | nout += n; 358 | } 359 | 360 | nbits--; 361 | bits <<= 1; 362 | }; 363 | } 364 | 365 | // 366 | // error handling 367 | // 368 | 369 | void fatal(const char *s, ...) 370 | { 371 | char message[256]; 372 | va_list ap; 373 | 374 | va_start(ap, s); 375 | vsnprintf(message, sizeof message, s, ap); 376 | 377 | perror(message); 378 | 379 | exit(1); 380 | } 381 | 382 | // 383 | // output formatting 384 | // 385 | 386 | void print_bytes(u8 *x, u32 n) 387 | { 388 | u32 i; 389 | 390 | for (i = 0; i < n; i++) 391 | fprintf(stderr, "%02x", x[i]); 392 | } 393 | 394 | void hexdump(u8 *x, u32 n) 395 | { 396 | u32 i, j; 397 | 398 | for (i = 0; i < n; i += 16) { 399 | fprintf(stderr, "%04x:", i); 400 | for (j = 0; j < 16 && i + j < n; j++) { 401 | if ((j & 3) == 0) 402 | fprintf(stderr, " "); 403 | fprintf(stderr, "%02x", *x++); 404 | } 405 | fprintf(stderr, "\n"); 406 | } 407 | } 408 | 409 | void dump_tmd(u8 *tmd) 410 | { 411 | u32 i, n; 412 | u8 *p; 413 | 414 | printf(" issuer: %s\n", tmd + 0x140); 415 | printf(" sys_version: %016llx\n", be64(tmd + 0x0184)); 416 | printf(" title_id: %016llx\n", be64(tmd + 0x018c)); 417 | printf(" title_type: %08x\n", be32(tmd + 0x0194)); 418 | printf(" group_id: %04x\n", be16(tmd + 0x0198)); 419 | printf("title_version: %04x\n", be16(tmd + 0x01dc)); 420 | printf(" num_contents: %04x\n", be16(tmd + 0x01de)); 421 | printf(" boot_index: %04x\n", be16(tmd + 0x01e0)); 422 | 423 | n = be16(tmd + 0x01de); 424 | p = tmd + 0x01e4; 425 | for (i = 0; i < n; i++) { 426 | printf("cid %08x index %04x type %04x size %08llx\n", 427 | be32(p), be16(p + 4), be16(p + 6), be64(p + 8)); 428 | p += 0x24; 429 | } 430 | } 431 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------