-
Notifications
You must be signed in to change notification settings - Fork 9
/
decode-wii.go
167 lines (145 loc) · 5.06 KB
/
decode-wii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bytes"
"crypto/md5"
"encoding/binary"
"fmt"
"io"
"os"
"reflect"
)
type partitionInfo struct {
PartitionType uint
PartitionOffset uint64
PartitionEndOffset uint64
DataOffset uint64
DataSize uint64
PartitionKey []byte
}
func decodeWii(r *os.File, outPath string, sectorSize uint) {
startSector := make([]byte, sectorSize)
_, err := io.ReadFull(r, startSector)
errorExit(err)
startBuffer := bytes.NewBuffer(startSector)
sig := startBuffer.Next(4)
id := startBuffer.Next(4)
hashValue := startBuffer.Next(16)
numPartitions := binary.LittleEndian.Uint32(startBuffer.Next(4))
var partitions = make([]partitionInfo, numPartitions)
fmt.Println("Wii Disc")
fmt.Printf("Signature: %s\n", string(sig))
fmt.Printf("ID: %s\n", string(id))
fmt.Printf("MD5: %x\n", hashValue)
for i := uint32(0); i < numPartitions; i++ {
partitions[i].DataOffset = uint64(binary.LittleEndian.Uint32(startBuffer.Next(4))) << 2
partitions[i].DataSize = uint64(binary.LittleEndian.Uint32(startBuffer.Next(4))) << 2
partitions[i].PartitionOffset = uint64(binary.LittleEndian.Uint32(startBuffer.Next(4))) << 2
partitions[i].PartitionEndOffset = uint64(binary.LittleEndian.Uint32(startBuffer.Next(4))) << 2
partitions[i].PartitionKey = startBuffer.Next(16)
fmt.Printf("Partition %d of %d\n", i+1, numPartitions)
fmt.Printf("--------------------\n")
fmt.Printf("Data Offset: 0x%x\n", partitions[i].DataOffset)
fmt.Printf("Data Size: 0x%x\n", partitions[i].DataSize)
fmt.Printf("Partition Offset: 0x%x\n", partitions[i].PartitionOffset)
fmt.Printf("Partition End: 0x%x\n", partitions[i].PartitionEndOffset)
fmt.Printf("Partition Key: 0x%x\n", partitions[i].PartitionKey)
fmt.Printf("====================\n")
}
w, err := os.Create(outPath)
errorExit(err)
defer w.Close()
bytesWritten := uint64(0)
transfer := make([]byte, 1024)
hash := md5.New()
fmt.Print("Writing Disc Header...")
for i := uint64(0); i < partitions[0].PartitionOffset; i += 1024 {
readNextOffset(r, startBuffer) // we don't need this
bytesWritten += uint64(blockTransferWithHash(r, w, transfer, hash))
vLog("WRITE: offset: %x\n", bytesWritten)
}
fmt.Println("Done")
for j := uint32(0); j < numPartitions; j++ {
fmt.Printf("Writing Partition %d Header...", j)
for i := uint64(0); i < partitions[j].DataOffset; i += 1024 {
setNextOffset(r, startBuffer)
bytesWritten += uint64(blockTransferWithHash(r, w, transfer, hash))
vLog("WRITE: offset: %x\n", bytesWritten)
}
fmt.Println("Done")
padBlock := uint32(0)
padOffset := uint64(0)
dataSize := uint64(0)
padding := make([]byte, 0x40000)
fmt.Printf("Writing Partition %d Data.....", j)
for dataSize < partitions[j].DataSize {
setNextOffset(r, startBuffer)
wrote := uint64(blockTransferWithHash(r, w, transfer, hash))
bytesWritten += wrote
dataSize += wrote
vLog("WRITE: offset: %x\n", bytesWritten)
writeBuffer := NewFixedRecord(0x7C00)
transfer2 := make([]byte, 1024)
for k := 0; k < 31; k++ {
if (padOffset & 0x3FFFF) == 0 {
vLog("PADDING: block: %d id: %s\n", padBlock, id)
padding = generatePaddingBlock(padBlock, id, 0)
padBlock++
padOffset = 0
}
if setNextOffset(r, startBuffer) {
padOffset += uint64(blockTransfer(r, writeBuffer, transfer2))
vLog("tfr %d poffset: %x buffer: %x\n", k, padOffset, writeBuffer.Len())
} else {
slice := padding[padOffset : padOffset+1024]
_, err = writeBuffer.Write(slice)
errorExit(err)
padOffset += 1024
vLog("pad %d poffset: %x buffer: %x\n", k, padOffset, writeBuffer.Len())
}
}
iv := getIV(transfer)
output := writeBuffer.Record()
encodeAES(output, partitions[j].PartitionKey, iv)
io.Copy(hash, bytes.NewBuffer(output))
_, err = w.Write(output)
errorExit(err)
bytesWritten += 0x7C00
vLog("WRITE: offset: %x\n", bytesWritten)
dataSize += 0x7C00
vLog("DATA SIZE: %d / %d\n", dataSize, partitions[j].DataSize)
}
fmt.Println("Done")
fmt.Printf("Writing Partition %d Fill.....", j)
padOffset = 0
padBlock = uint32(bytesWritten / 0x40000)
for bytesWritten != partitions[j].PartitionEndOffset {
if (padOffset & 0x3FFFF) == 0 {
vLog("generate padding: %d %s\n", padBlock, id)
padding = generatePaddingBlock(padBlock, id, 0)
padBlock++
padOffset = bytesWritten % 0x40000
}
if setNextOffset(r, startBuffer) {
wrote := uint64(blockTransferWithHash(r, w, transfer, hash))
bytesWritten += wrote
padOffset += wrote
vLog("WRITE: tfr - poffset: %x offset: %x\n", padOffset, bytesWritten)
} else {
slice := padding[padOffset : padOffset+1024]
io.Copy(hash, bytes.NewBuffer(slice))
_, err = w.Write(slice)
errorExit(err)
bytesWritten += 1024
padOffset += 1024
vLog("WRITE: pad - poffset: %x offset: %x\n", padOffset, bytesWritten)
}
}
fmt.Println("Done")
}
calcValue := hash.Sum(nil)
if reflect.DeepEqual(hashValue, calcValue) {
fmt.Printf("Decode OK: %x\n", hashValue)
} else {
fmt.Printf("Decode FAIL: expected: %x calculated: %x\n", hashValue, calcValue)
}
}